diff --git a/mlir/lib/Bindings/Python/IRModules.cpp b/mlir/lib/Bindings/Python/IRModules.cpp --- a/mlir/lib/Bindings/Python/IRModules.cpp +++ b/mlir/lib/Bindings/Python/IRModules.cpp @@ -3008,6 +3008,13 @@ 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) { + auto operation = self.get(); + auto 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 --- a/mlir/test/Bindings/Python/ir_operation.py +++ b/mlir/test/Bindings/Python/ir_operation.py @@ -578,3 +578,22 @@ # 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)