Skip to content

Commit ca541f7

Browse files
authored
[transform_ext] Avoid stale op handles in prepare_subgraphs (#323)
`_prepare` iterates over a snapshot of `block.operations`, but `_fold_gemm_cast_reshape` erases some of those ops (the `tensor.expand_shape` and the cast), so the loop then reads erased ops through stale Python handles. On macOS this segfaults `test/transform/test_subgraph_tiling.py` and five `examples/xegpu/llama-bench_1B_*` tests. On Linux it happens to work because the freed memory still holds the old ops. To reproduce it on Linux, make glibc overwrite freed memory. ``` GLIBC_TUNABLES=glibc.malloc.perturb=11:glibc.malloc.tcache_count=0 python test/transform/test_subgraph_tiling.py ``` Collect the matmuls before folding instead. Part of #269.
1 parent 6164aaf commit ca541f7

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎lighthouse/dialects/transform/transform_ext/ops/prepare_subgraphs.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,13 @@ def _isolate_producers(members, anchor):
108108

109109
def _prepare(block, kinds, rewriter):
110110
if "gemm" in kinds:
111-
for op in list(block.operations):
112-
if op.name == "linalg.matmul":
113-
_fold_gemm_cast_reshape(op.operation, rewriter)
111+
# Folding erases users of a matmul but never another matmul, so these
112+
# handles stay valid.
113+
matmuls = [
114+
op.operation for op in block.operations if op.name == "linalg.matmul"
115+
]
116+
for matmul in matmuls:
117+
_fold_gemm_cast_reshape(matmul, rewriter)
114118
claimed = {op.operation for op in block.operations if sa.is_tagged(op)}
115119
for kind in kinds:
116120
for op in list(block.operations):

0 commit comments

Comments
 (0)