[Mlir-commits] [mlir] 7fb8a44 - [mlir][python] expose isAttached (#153045)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Aug 11 10:22:02 PDT 2025
Author: Maksim Levental
Date: 2025-08-11T12:21:59-05:00
New Revision: 7fb8a44ad58a399dfa66efe0ea3071448394457f
URL: https://github.com/llvm/llvm-project/commit/7fb8a44ad58a399dfa66efe0ea3071448394457f
DIFF: https://github.com/llvm/llvm-project/commit/7fb8a44ad58a399dfa66efe0ea3071448394457f.diff
LOG: [mlir][python] expose isAttached (#153045)
Added:
Modified:
mlir/lib/Bindings/Python/IRCore.cpp
mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
mlir/test/python/ir/operation.py
Removed:
################################################################################
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 5feed95f96f53..ee88aa4753d00 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -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);
diff --git a/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi b/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
index be71737e4b5b4..dcae3dd742940 100644
--- a/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
+++ b/mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
@@ -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
diff --git a/mlir/test/python/ir/operation.py b/mlir/test/python/ir/operation.py
index c6b5dafe792ca..bf16e3f75d60d 100644
--- a/mlir/test/python/ir/operation.py
+++ b/mlir/test/python/ir/operation.py
@@ -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()
More information about the Mlir-commits
mailing list