From 14056c88d668fe396eaa59439ddf56f59dd188ec Mon Sep 17 00:00:00 2001 From: Siddharth Krishna Date: Tue, 29 Dec 2020 14:14:08 -0800 Subject: [PATCH] [mlir][Python] Add an Operation.name property Reviewed By: stellaraccident, mehdi_amini Differential Revision: https://reviews.llvm.org/D93474 --- mlir/lib/Bindings/Python/IRModules.cpp | 7 +++++++ mlir/test/Bindings/Python/ir_operation.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/mlir/lib/Bindings/Python/IRModules.cpp b/mlir/lib/Bindings/Python/IRModules.cpp index cd58f29..8fe63f3 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 1aa365a..ba54e83 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) -- 2.7.4