diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp --- a/mlir/lib/Bindings/Python/IRAttributes.cpp +++ b/mlir/lib/Bindings/Python/IRAttributes.cpp @@ -320,6 +320,42 @@ } }; +class PyOpaqueAttribute : public PyConcreteAttribute { +public: + static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAOpaque; + static constexpr const char *pyClassName = "OpaqueAttr"; + using PyConcreteAttribute::PyConcreteAttribute; + + static void bindDerived(ClassTy &c) { + c.def_static( + "get", + [](std::string dialectNamespace, DefaultingPyMlirContext context, + intptr_t dataLength, const char *data, PyType &type) { + MlirAttribute attr = mlirOpaqueAttrGet( + context->get(), toMlirStringRef(dialectNamespace), dataLength, + data, type); + return PyOpaqueAttribute(context->getRef(), attr); + }, + py::arg("dialect_namespace"), py::arg("context") = py::none(), + py::arg("data_length") = py::int_(0), py::arg("data") = py::str(""), + py::arg("type") = py::none(), "Gets an Opaque attribute."); + c.def_property_readonly( + "dialect_namespace", + [](PyOpaqueAttribute &self) { + MlirStringRef stringRef = mlirOpaqueAttrGetDialectNamespace(self); + return py::str(stringRef.data, stringRef.length); + }, + "Returns the dialect namespace for the Opaque attribute as a string"); + c.def_property_readonly( + "data", + [](PyOpaqueAttribute &self) { + MlirStringRef stringRef = mlirOpaqueAttrGetData(self); + return py::str(stringRef.data, stringRef.length); + }, + "Returns the data for the Opaqued attributes as a string"); + } +}; + class PyStringAttribute : public PyConcreteAttribute { public: static constexpr IsAFunctionTy isaFunction = mlirAttributeIsAString; @@ -862,6 +898,7 @@ PyDenseIntElementsAttribute::bind(m); PyDictAttribute::bind(m); PyFlatSymbolRefAttribute::bind(m); + PyOpaqueAttribute::bind(m); PyFloatAttribute::bind(m); PyIntegerAttribute::bind(m); PyStringAttribute::bind(m); diff --git a/mlir/test/python/ir/attributes.py b/mlir/test/python/ir/attributes.py --- a/mlir/test/python/ir/attributes.py +++ b/mlir/test/python/ir/attributes.py @@ -236,6 +236,24 @@ print("default_get:", FlatSymbolRefAttr.get("foobar")) +# CHECK-LABEL: TEST: testOpaqueAttr +@run +def testOpaqueAttr(): + with Context() as ctx: + ctx.allow_unregistered_dialects = True + oattr = OpaqueAttr(Attribute.parse("#pytest_dummy.dummyattr<>")) + # CHECK: oattr value: pytest_dummy + print("oattr value:", oattr.dialect_namespace) + # CHECK: oattr value: dummyattr<> + print("oattr value:", oattr.data) + + # Test factory methods. + # CHECK: default_get: #foobar<"12345"> : i32 + print( + "default_get:", + OpaqueAttr.get("foobar", ctx, 5, "12345", IntegerType.get_signless(32))) + + # CHECK-LABEL: TEST: testStringAttr @run def testStringAttr():