Skip to content

Commit b4ed8b4

Browse files
authored
[transform_ext] Generalize convert_func_results_to_args (llvm#150)
Generalizes operand replacing mechanism in `convert_func_results_to_args` transform op. Fixes `LLVM ERROR: operation destroyed but still has uses` errors seen in KernelBench test cases.
1 parent 1b08cb1 commit b4ed8b4

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,24 @@ def f(*args):
5858
t = emit_buf_to_tensor(input, restrict=True)
5959
input_tensors.append(t)
6060

61-
# clone function body and map args and return values
61+
# keep track of cloned ops to replace operands
6262
cloned_map = {}
63+
64+
def replace_operands(op: ir.Operation) -> ir.WalkResult:
65+
for i, oprnd in enumerate(op.operands):
66+
if isinstance(oprnd, ir.BlockArgument):
67+
# operand is block argument
68+
owner = oprnd.owner.owner # op that owns the block
69+
if owner == target:
70+
# payload func argument, replace with input tensor
71+
op.operands[i] = input_tensors[oprnd.arg_number]
72+
else:
73+
# replace operands with cloned values
74+
if oprnd.owner in cloned_map:
75+
iresult = oprnd.result_number
76+
op.operands[i] = cloned_map[oprnd.owner].results[iresult]
77+
return ir.WalkResult.ADVANCE
78+
6379
for op in target.regions[0].blocks[0].operations:
6480
if isinstance(op, func.ReturnOp):
6581
# emit materialize_in_destination for each return value
@@ -77,18 +93,7 @@ def f(*args):
7793
)
7894
else:
7995
new_op = op.clone()
80-
for i, oo in enumerate(op.operands):
81-
if isinstance(oo, ir.BlockArgument):
82-
# operand is func argument
83-
# replace with new input tensors
84-
new_op.operands[i] = input_tensors[oo.arg_number]
85-
else:
86-
# replace operands with cloned values
87-
if oo.owner in cloned_map:
88-
iresult = oo.result_number
89-
new_op.operands[i] = cloned_map[oo.owner].results[
90-
iresult
91-
]
96+
new_op.walk(replace_operands, ir.WalkOrder.PRE_ORDER)
9297
cloned_map[op] = new_op
9398

9499
return f.func_op
@@ -130,7 +135,7 @@ def get_effects(op: "ConvertFuncResultsToArgsOp", effects):
130135

131136

132137
def convert_func_results_to_args(
133-
target: ir.Value[transform.AnyOpType], bench_name: str | None = None
138+
target: ir.Value[transform.AnyOpType],
134139
) -> ir.Value[transform.AnyOpType]:
135140
"""snake_case wrapper to create a ConvertFuncResultsToArgsOp."""
136141
op = ConvertFuncResultsToArgsOp(target=target)

0 commit comments

Comments
 (0)