[mlir][python] expose isAttached (#153045)

This commit is contained in:
Maksim Levental
2025-08-11 12:21:59 -05:00
committed by GitHub
parent ea14ee4464
commit 7fb8a44ad5
3 changed files with 17 additions and 0 deletions

View File

@@ -3442,6 +3442,14 @@ void mlir::python::populateIRCore(nb::module_ &m) {
return operation.createOpView();
},
"Detaches the operation from its parent block.")
.def_prop_ro(
"attached",
[](PyOperationBase &self) {
PyOperation &operation = self.getOperation();
operation.checkValid();
return operation.isAttached();
},
"Reports if the operation is attached to its parent block.")
.def("erase", [](PyOperationBase &self) { self.getOperation().erase(); })
.def("walk", &PyOperationBase::walk, nb::arg("callback"),
nb::arg("walk_order") = MlirWalkPostOrder);

View File

@@ -194,6 +194,13 @@ class _OperationBase:
"""
Detaches the operation from its parent block.
"""
@property
def attached(self) -> bool:
"""
Reports if the operation is attached to its parent block.
"""
def erase(self) -> None: ...
@overload

View File

@@ -1021,6 +1021,8 @@ def testDetachFromParent():
with Context():
m1 = Module.parse("func.func private @foo()")
func = m1.body.operations[0].detach_from_parent()
# CHECK: func.attached=False
print(f"{func.attached=}")
try:
func.detach_from_parent()