[Mlir-commits] [mlir] 14056c8 - [mlir][Python] Add an Operation.name property

Stella Laurenzo llvmlistbot at llvm.org
Tue Dec 29 14:16:47 PST 2020


Author: Siddharth Krishna
Date: 2020-12-29T14:14:32-08:00
New Revision: 14056c88d668fe396eaa59439ddf56f59dd188ec

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

LOG: [mlir][Python] Add an Operation.name property

Reviewed By: stellaraccident, mehdi_amini

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

Added: 
    

Modified: 
    mlir/lib/Bindings/Python/IRModules.cpp
    mlir/test/Bindings/Python/ir_operation.py

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Bindings/Python/IRModules.cpp b/mlir/lib/Bindings/Python/IRModules.cpp
index cd58f291d2ec..8fe63f3d1a40 100644
--- a/mlir/lib/Bindings/Python/IRModules.cpp
+++ b/mlir/lib/Bindings/Python/IRModules.cpp
@@ -3042,6 +3042,13 @@ void mlir::python::populateIRSubmodule(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("name",
+                             [](PyOperation &self) {
+                               MlirOperation operation = self.get();
+                               MlirStringRef name = mlirIdentifierStr(
+                                   mlirOperationGetName(operation));
+                               return py::str(name.data, name.length);
+                             })
       .def_property_readonly(
           "context",
           [](PyOperation &self) { return self.getContext().getObject(); },

diff  --git a/mlir/test/Bindings/Python/ir_operation.py b/mlir/test/Bindings/Python/ir_operation.py
index 1aa365a1f35d..ba54e83f65e8 100644
--- a/mlir/test/Bindings/Python/ir_operation.py
+++ b/mlir/test/Bindings/Python/ir_operation.py
@@ -584,3 +584,22 @@ def testCreateWithInvalidAttributes():
       # CHECK: Found an invalid (`None`?) attribute value for the key "some_key" when attempting to create the operation "module"
       print(e)
 run(testCreateWithInvalidAttributes)
+
+
+# CHECK-LABEL: TEST: testOperationName
+def testOperationName():
+  ctx = Context()
+  ctx.allow_unregistered_dialects = True
+  module = Module.parse(r"""
+    %0 = "custom.op1"() : () -> f32
+    %1 = "custom.op2"() : () -> i32
+    %2 = "custom.op1"() : () -> f32
+  """, ctx)
+
+  # CHECK: custom.op1
+  # CHECK: custom.op2
+  # CHECK: custom.op1
+  for op in module.body.operations:
+    print(op.operation.name)
+
+run(testOperationName)


        


More information about the Mlir-commits mailing list