Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 373f4ed

Browse files
authored
[MLIR][Python] Python binding support for AffineIfOp (#108323)
Fix the AffineIfOp's default builder such that it takes in an IntegerSetAttr. AffineIfOp has skipDefaultBuilders=1 which effectively skips the creation of the default AffineIfOp::builder on the C++ side. (AffineIfOp has two custom OpBuilder defined in the extraClassDeclaration.) However, on the python side, _affine_ops_gen.py shows that the default builder is being created, but it does not accept IntegerSet and thus is useless. This fix at line 411 makes the default python AffineIfOp builder take in an IntegerSet input and does not impact the C++ side of things.
1 parent 44f2ed7 commit 373f4ed

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

mlir/python/mlir/dialects/affine.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,61 @@ def for_(
156156
yield iv, iter_args[0]
157157
else:
158158
yield iv
159+
160+
161+
@_ods_cext.register_operation(_Dialect, replace=True)
162+
class AffineIfOp(AffineIfOp):
163+
"""Specialization for the Affine if op class."""
164+
165+
def __init__(
166+
self,
167+
cond: IntegerSet,
168+
results_: Optional[Type] = None,
169+
*,
170+
cond_operands: Optional[_VariadicResultValueT] = None,
171+
has_else: bool = False,
172+
loc=None,
173+
ip=None,
174+
):
175+
"""Creates an Affine `if` operation.
176+
177+
- `cond` is the integer set used to determine which regions of code
178+
will be executed.
179+
- `results` are the list of types to be yielded by the operand.
180+
- `cond_operands` is the list of arguments to substitute the
181+
dimensions, then symbols in the `cond` integer set expression to
182+
determine whether they are in the set.
183+
- `has_else` determines whether the affine if operation has the else
184+
branch.
185+
"""
186+
if results_ is None:
187+
results_ = []
188+
if cond_operands is None:
189+
cond_operands = []
190+
191+
if cond.n_inputs != len(cond_operands):
192+
raise ValueError(
193+
f"expected {cond.n_inputs} condition operands, got {len(cond_operands)}"
194+
)
195+
196+
operands = []
197+
operands.extend(cond_operands)
198+
results = []
199+
results.extend(results_)
200+
201+
super().__init__(results, cond_operands, cond)
202+
self.regions[0].blocks.append(*[])
203+
if has_else:
204+
self.regions[1].blocks.append(*[])
205+
206+
@property
207+
def then_block(self) -> Block:
208+
"""Returns the then block of the if operation."""
209+
return self.regions[0].blocks[0]
210+
211+
@property
212+
def else_block(self) -> Optional[Block]:
213+
"""Returns the else block of the if operation."""
214+
if len(self.regions[1].blocks) == 0:
215+
return None
216+
return self.regions[1].blocks[0]

0 commit comments

Comments
 (0)