Skip to content

Commit 177072a

Browse files
authored
[MLIR][Python] Update the scf.if interface to be consistent with affine.if (#173171)
This is a follow-up of #171957 that updates the argument names of `scf.if` Python binding to be consistent with `affine.if`. Basically, both operations should use `has_else` to determine whether the `if` block is presented. cc @makslevental
1 parent 4fec702 commit 177072a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

mlir/python/mlir/dialects/scf.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ def block(self) -> Block:
193193
class IfOp(IfOp):
194194
"""Specialization for the SCF if op class."""
195195

196-
def __init__(self, cond, results_=None, *, hasElse=False, loc=None, ip=None):
196+
def __init__(self, cond, results_=None, *, has_else=False, loc=None, ip=None):
197197
"""Creates an SCF `if` operation.
198198
199199
- `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed.
200-
- `hasElse` determines whether the if operation has the else branch.
200+
- `has_else` determines whether the if operation has the else branch.
201201
"""
202202
if results_ is None:
203203
results_ = []
@@ -207,17 +207,19 @@ def __init__(self, cond, results_=None, *, hasElse=False, loc=None, ip=None):
207207
results.extend(results_)
208208
super().__init__(results, cond, loc=loc, ip=ip)
209209
self.regions[0].blocks.append(*[])
210-
if hasElse:
210+
if has_else:
211211
self.regions[1].blocks.append(*[])
212212

213213
@property
214-
def then_block(self):
214+
def then_block(self) -> Block:
215215
"""Returns the then block of the if operation."""
216216
return self.regions[0].blocks[0]
217217

218218
@property
219-
def else_block(self):
219+
def else_block(self) -> Optional[Block]:
220220
"""Returns the else block of the if operation."""
221+
if len(self.regions[1].blocks) == 0:
222+
return None
221223
return self.regions[1].blocks[0]
222224

223225

mlir/test/python/dialects/scf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def testIfWithElse():
335335

336336
@func.FuncOp.from_py_func(bool)
337337
def simple_if_else(cond):
338-
if_op = scf.IfOp(cond, [i32, i32], hasElse=True)
338+
if_op = scf.IfOp(cond, [i32, i32], has_else=True)
339339
with InsertionPoint(if_op.then_block):
340340
x_true = arith.ConstantOp(i32, 0)
341341
y_true = arith.ConstantOp(i32, 1)

0 commit comments

Comments
 (0)