Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir][python] Expose PyInsertionPoint's reference operation #69082

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,18 @@ void mlir::python::populateIRCore(py::module &m) {
"Inserts an operation.")
.def_property_readonly(
"block", [](PyInsertionPoint &self) { return self.getBlock(); },
"Returns the block that this InsertionPoint points to.");
"Returns the block that this InsertionPoint points to.")
.def_property_readonly(
"ref_operation",
[](PyInsertionPoint &self) -> py::object {
auto ref_operation = self.getRefOperation();
if (ref_operation)
return ref_operation->getObject();
return py::none();
},
"The reference operation before which new operations are "
"inserted, or None if the insertion point is at the end of "
"the block");

//----------------------------------------------------------------------------
// Mapping of PyAttribute.
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Bindings/Python/IRModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ class PyInsertionPoint {
const pybind11::object &excTb);

PyBlock &getBlock() { return block; }
std::optional<PyOperationRef> &getRefOperation() { return refOperation; }

private:
// Trampoline constructor that avoids null initializing members while
Expand Down
2 changes: 2 additions & 0 deletions mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,8 @@ class InsertionPoint:
def __exit__(self, arg0: object, arg1: object, arg2: object) -> None: ...
@property
def block(self) -> Block: ...
@property
def ref_operation(self) -> Optional[_OperationBase]: ...

# TODO: Auto-generated. Audit and fix.
class IntegerAttr(Attribute):
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/python/ir/insertion_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def test_insert_at_block_end():
)
entry_block = module.body.operations[0].regions[0].blocks[0]
ip = InsertionPoint(entry_block)
assert ip.block == entry_block
assert ip.ref_operation is None
ip.insert(Operation.create("custom.op2"))
# CHECK: "custom.op1"
# CHECK: "custom.op2"
Expand All @@ -51,6 +53,8 @@ def test_insert_before_operation():
)
entry_block = module.body.operations[0].regions[0].blocks[0]
ip = InsertionPoint(entry_block.operations[1])
assert ip.block == entry_block
assert ip.ref_operation == entry_block.operations[1]
ip.insert(Operation.create("custom.op3"))
# CHECK: "custom.op1"
# CHECK: "custom.op3"
Expand All @@ -75,6 +79,8 @@ def test_insert_at_block_begin():
)
entry_block = module.body.operations[0].regions[0].blocks[0]
ip = InsertionPoint.at_block_begin(entry_block)
assert ip.block == entry_block
assert ip.ref_operation == entry_block.operations[0]
ip.insert(Operation.create("custom.op1"))
# CHECK: "custom.op1"
# CHECK: "custom.op2"
Expand Down Expand Up @@ -108,6 +114,8 @@ def test_insert_at_terminator():
)
entry_block = module.body.operations[0].regions[0].blocks[0]
ip = InsertionPoint.at_block_terminator(entry_block)
assert ip.block == entry_block
assert ip.ref_operation == entry_block.operations[1]
ip.insert(Operation.create("custom.op2"))
# CHECK: "custom.op1"
# CHECK: "custom.op2"
Expand Down
Loading