[Mlir-commits] [mlir] 177072a - [MLIR][Python] Update the scf.if interface to be consistent with affine.if (#173171)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Dec 20 21:33:40 PST 2025


Author: Hongzheng Chen
Date: 2025-12-20T21:33:37-08:00
New Revision: 177072a763419f3a54804bbece998f08cb0ed481

URL: https://github.com/llvm/llvm-project/commit/177072a763419f3a54804bbece998f08cb0ed481
DIFF: https://github.com/llvm/llvm-project/commit/177072a763419f3a54804bbece998f08cb0ed481.diff

LOG: [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

Added: 
    

Modified: 
    mlir/python/mlir/dialects/scf.py
    mlir/test/python/dialects/scf.py

Removed: 
    


################################################################################
diff  --git a/mlir/python/mlir/dialects/scf.py b/mlir/python/mlir/dialects/scf.py
index 9e22df3dd50a9..27e8c6f5dd05f 100644
--- a/mlir/python/mlir/dialects/scf.py
+++ b/mlir/python/mlir/dialects/scf.py
@@ -193,11 +193,11 @@ def block(self) -> Block:
 class IfOp(IfOp):
     """Specialization for the SCF if op class."""
 
-    def __init__(self, cond, results_=None, *, hasElse=False, loc=None, ip=None):
+    def __init__(self, cond, results_=None, *, has_else=False, loc=None, ip=None):
         """Creates an SCF `if` operation.
 
         - `cond` is a MLIR value of 'i1' type to determine which regions of code will be executed.
-        - `hasElse` determines whether the if operation has the else branch.
+        - `has_else` determines whether the if operation has the else branch.
         """
         if results_ is None:
             results_ = []
@@ -207,17 +207,19 @@ def __init__(self, cond, results_=None, *, hasElse=False, loc=None, ip=None):
         results.extend(results_)
         super().__init__(results, cond, loc=loc, ip=ip)
         self.regions[0].blocks.append(*[])
-        if hasElse:
+        if has_else:
             self.regions[1].blocks.append(*[])
 
     @property
-    def then_block(self):
+    def then_block(self) -> Block:
         """Returns the then block of the if operation."""
         return self.regions[0].blocks[0]
 
     @property
-    def else_block(self):
+    def else_block(self) -> Optional[Block]:
         """Returns the else block of the if operation."""
+        if len(self.regions[1].blocks) == 0:
+            return None
         return self.regions[1].blocks[0]
 
 

diff  --git a/mlir/test/python/dialects/scf.py b/mlir/test/python/dialects/scf.py
index 0c0c9b986562b..e53a299d1f869 100644
--- a/mlir/test/python/dialects/scf.py
+++ b/mlir/test/python/dialects/scf.py
@@ -335,7 +335,7 @@ def testIfWithElse():
 
     @func.FuncOp.from_py_func(bool)
     def simple_if_else(cond):
-        if_op = scf.IfOp(cond, [i32, i32], hasElse=True)
+        if_op = scf.IfOp(cond, [i32, i32], has_else=True)
         with InsertionPoint(if_op.then_block):
             x_true = arith.ConstantOp(i32, 0)
             y_true = arith.ConstantOp(i32, 1)


        


More information about the Mlir-commits mailing list