[Mlir-commits] [mlir] c65bb76 - [MLIR] [Python] Add Operation.parent

John Demme llvmlistbot at llvm.org
Sun May 23 20:53:32 PDT 2021


Author: John Demme
Date: 2021-05-23T20:52:43-07:00
New Revision: c65bb760df1f7b2b30e3a6b40b91b9a6294e1619

URL: https://github.com/llvm/llvm-project/commit/c65bb760df1f7b2b30e3a6b40b91b9a6294e1619
DIFF: https://github.com/llvm/llvm-project/commit/c65bb760df1f7b2b30e3a6b40b91b9a6294e1619.diff

LOG: [MLIR] [Python] Add Operation.parent

Attribute to get the parent operation of an operation.

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D102981

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/IRCore.cpp
    mlir/test/python/ir/operation.py

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index d11edb1c688e8..3f08522b9c1de 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -2113,6 +2113,10 @@ void mlir::python::populateIRCore(py::module &m) {
                   py::arg("successors") = py::none(), py::arg("regions") = 0,
                   py::arg("loc") = py::none(), py::arg("ip") = py::none(),
                   kOperationCreateDocstring)
+      .def_property_readonly("parent",
+                             [](PyOperation &self) {
+                               return self.getParentOperation().getObject();
+                             })
       .def("erase", &PyOperation::erase)
       .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
                              &PyOperation::getCapsule)

diff  --git a/mlir/test/python/ir/operation.py b/mlir/test/python/ir/operation.py
index 83e4a4fdfca69..8bd6c9aef6283 100644
--- a/mlir/test/python/ir/operation.py
+++ b/mlir/test/python/ir/operation.py
@@ -91,15 +91,19 @@ def walk_operations(indent, op):
         for k in range(len(block.operations)):
           child_op = block.operations[k]
           print(f"{indent}    OP {k}: {child_op}")
+          print(f"{indent}    OP {k}: parent {child_op.operation.parent.name}")
           walk_operations(indent + "      ", child_op)
 
   # CHECK: REGION 0:
   # CHECK:   BLOCK 0:
   # CHECK:     OP 0: func
+  # CHECK:     OP 0: parent module
   # CHECK:       REGION 0:
   # CHECK:         BLOCK 0:
   # CHECK:           OP 0: %0 = "custom.addi"
+  # CHECK:           OP 0: parent func
   # CHECK:           OP 1: return
+  # CHECK:           OP 1: parent func
   walk_operations("", module.operation)
 
 run(testTraverseOpRegionBlockIndices)


        


More information about the Mlir-commits mailing list