Skip to content

Commit ad89e61

Browse files
authored
[MLIR][Python] Fix detached operation coming from IfOp constructor (#107286)
Without this fix, `scf.if` operations would be created without a parent. Since `scf.if` operations often have no results, this caused silent bugs where the generated code was straight-up missing the operation.
1 parent aad6997 commit ad89e61

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

mlir/python/mlir/dialects/scf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, cond, results_=None, *, hasElse=False, loc=None, ip=None):
8787
operands.append(cond)
8888
results = []
8989
results.extend(results_)
90-
super().__init__(results, cond)
90+
super().__init__(results, cond, loc=loc, ip=ip)
9191
self.regions[0].blocks.append(*[])
9292
if hasElse:
9393
self.regions[1].blocks.append(*[])

mlir/test/python/dialects/scf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,32 @@ def simple_if(cond):
278278
# CHECK: return
279279

280280

281+
@constructAndPrintInModule
282+
def testNestedIf():
283+
bool = IntegerType.get_signless(1)
284+
i32 = IntegerType.get_signless(32)
285+
286+
@func.FuncOp.from_py_func(bool, bool)
287+
def nested_if(b, c):
288+
if_op = scf.IfOp(b)
289+
with InsertionPoint(if_op.then_block) as ip:
290+
if_op = scf.IfOp(c, ip=ip)
291+
with InsertionPoint(if_op.then_block):
292+
one = arith.ConstantOp(i32, 1)
293+
add = arith.AddIOp(one, one)
294+
scf.YieldOp([])
295+
scf.YieldOp([])
296+
return
297+
298+
299+
# CHECK: func @nested_if(%[[ARG0:.*]]: i1, %[[ARG1:.*]]: i1)
300+
# CHECK: scf.if %[[ARG0:.*]]
301+
# CHECK: scf.if %[[ARG1:.*]]
302+
# CHECK: %[[ONE:.*]] = arith.constant 1
303+
# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]]
304+
# CHECK: return
305+
306+
281307
@constructAndPrintInModule
282308
def testIfWithElse():
283309
bool = IntegerType.get_signless(1)

0 commit comments

Comments
 (0)