Skip to content

Commit 7fb8a44

Browse files
authored
[mlir][python] expose isAttached (#153045)
1 parent ea14ee4 commit 7fb8a44

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,6 +3442,14 @@ void mlir::python::populateIRCore(nb::module_ &m) {
34423442
return operation.createOpView();
34433443
},
34443444
"Detaches the operation from its parent block.")
3445+
.def_prop_ro(
3446+
"attached",
3447+
[](PyOperationBase &self) {
3448+
PyOperation &operation = self.getOperation();
3449+
operation.checkValid();
3450+
return operation.isAttached();
3451+
},
3452+
"Reports if the operation is attached to its parent block.")
34453453
.def("erase", [](PyOperationBase &self) { self.getOperation().erase(); })
34463454
.def("walk", &PyOperationBase::walk, nb::arg("callback"),
34473455
nb::arg("walk_order") = MlirWalkPostOrder);

mlir/python/mlir/_mlir_libs/_mlir/ir.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ class _OperationBase:
194194
"""
195195
Detaches the operation from its parent block.
196196
"""
197+
198+
@property
199+
def attached(self) -> bool:
200+
"""
201+
Reports if the operation is attached to its parent block.
202+
"""
203+
197204
def erase(self) -> None: ...
198205

199206
@overload

mlir/test/python/ir/operation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ def testDetachFromParent():
10211021
with Context():
10221022
m1 = Module.parse("func.func private @foo()")
10231023
func = m1.body.operations[0].detach_from_parent()
1024+
# CHECK: func.attached=False
1025+
print(f"{func.attached=}")
10241026

10251027
try:
10261028
func.detach_from_parent()

0 commit comments

Comments
 (0)