[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.
This commit is contained in:
Matt Hofmann 2024-09-05 00:12:03 -04:00 committed by GitHub
parent aad6997764
commit ad89e617c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -87,7 +87,7 @@ class IfOp(IfOp):
operands.append(cond)
results = []
results.extend(results_)
super().__init__(results, cond)
super().__init__(results, cond, loc=loc, ip=ip)
self.regions[0].blocks.append(*[])
if hasElse:
self.regions[1].blocks.append(*[])

View File

@ -278,6 +278,32 @@ def testIfWithoutElse():
# CHECK: return
@constructAndPrintInModule
def testNestedIf():
bool = IntegerType.get_signless(1)
i32 = IntegerType.get_signless(32)
@func.FuncOp.from_py_func(bool, bool)
def nested_if(b, c):
if_op = scf.IfOp(b)
with InsertionPoint(if_op.then_block) as ip:
if_op = scf.IfOp(c, ip=ip)
with InsertionPoint(if_op.then_block):
one = arith.ConstantOp(i32, 1)
add = arith.AddIOp(one, one)
scf.YieldOp([])
scf.YieldOp([])
return
# CHECK: func @nested_if(%[[ARG0:.*]]: i1, %[[ARG1:.*]]: i1)
# CHECK: scf.if %[[ARG0:.*]]
# CHECK: scf.if %[[ARG1:.*]]
# CHECK: %[[ONE:.*]] = arith.constant 1
# CHECK: %[[ADD:.*]] = arith.addi %[[ONE]], %[[ONE]]
# CHECK: return
@constructAndPrintInModule
def testIfWithElse():
bool = IntegerType.get_signless(1)