[Mlir-commits] [mlir] [mlir][python] expose isAttached (PR #153045)

Maksim Levental llvmlistbot at llvm.org
Mon Aug 11 09:42:55 PDT 2025


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/153045

>From ed063cfa6974db32d2a6c6acf1028ace31b4851f Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Mon, 11 Aug 2025 12:08:16 -0400
Subject: [PATCH] [mlir][python] expose isAttached

---
 mlir/lib/Bindings/Python/IRCore.cpp      | 8 ++++++++
 mlir/python/mlir/_mlir_libs/_mlir/ir.pyi | 7 +++++++
 mlir/test/python/ir/operation.py         | 2 ++
 3 files changed, 17 insertions(+)

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