[MLIR][Python] Add SCFIfOp Python binding

Current generated Python binding for the SCF dialect does not allow
users to call IfOp to create if-else branches on their own.
This PR sets up the default binding generation for scf.if operation
to address this problem.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D121076
This commit is contained in:
chhzh123
2022-03-13 05:24:00 +00:00
committed by Mehdi Amini
parent fd4d584d6b
commit 036088fd6e
2 changed files with 96 additions and 0 deletions

View File

@@ -64,3 +64,44 @@ class ForOp:
To obtain the loop-carried operands, use `iter_args`.
"""
return self.body.arguments[1:]
class IfOp:
"""Specialization for the SCF if op class."""
def __init__(self,
cond,
results_=[],
*,
hasElse=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.
"""
operands = []
operands.append(cond)
results = []
results.extend(results_)
super().__init__(
self.build_generic(
regions=2,
results=results,
operands=operands,
loc=loc,
ip=ip))
self.regions[0].blocks.append(*[])
if hasElse:
self.regions[1].blocks.append(*[])
@property
def then_block(self):
"""Returns the then block of the if operation."""
return self.regions[0].blocks[0]
@property
def else_block(self):
"""Returns the else block of the if operation."""
return self.regions[1].blocks[0]