diff --git a/mlir/include/mlir-c/BuiltinTypes.h b/mlir/include/mlir-c/BuiltinTypes.h --- a/mlir/include/mlir-c/BuiltinTypes.h +++ b/mlir/include/mlir-c/BuiltinTypes.h @@ -337,8 +337,7 @@ /// specified length (data need not be null-terminated). MLIR_CAPI_EXPORTED MlirType mlirOpaqueTypeGet(MlirContext ctx, MlirStringRef dialectNamespace, - intptr_t dataLength, - const char *data); + MlirStringRef typeData); /// Returns the namespace of the dialect with which the given opaque type /// is associated. The namespace string is owned by the context. diff --git a/mlir/lib/Bindings/Python/IRTypes.cpp b/mlir/lib/Bindings/Python/IRTypes.cpp --- a/mlir/lib/Bindings/Python/IRTypes.cpp +++ b/mlir/lib/Bindings/Python/IRTypes.cpp @@ -622,32 +622,30 @@ static void bindDerived(ClassTy &c) { c.def_static( "get", - [](std::string dialectNamespace, py::buffer buffer, + [](std::string dialectNamespace, std::string typeData, DefaultingPyMlirContext context) { - const py::buffer_info bufferInfo = buffer.request(); - intptr_t bufferSize = bufferInfo.size; - MlirType type = mlirOpaqueTypeGet( - context->get(), toMlirStringRef(dialectNamespace), bufferSize, - static_cast(bufferInfo.ptr)); + MlirType type = mlirOpaqueTypeGet(context->get(), + toMlirStringRef(dialectNamespace), + toMlirStringRef(typeData)); return PyOpaqueType(context->getRef(), type); }, py::arg("dialect_namespace"), py::arg("buffer"), py::arg("context") = py::none(), - "Create an unregistered (opaque) dialect type"); + "Create an unregistered (opaque) dialect type."); c.def_property_readonly( "dialect_namespace", [](PyOpaqueType &self) { MlirStringRef stringRef = mlirOpaqueTypeGetDialectNamespace(self); return py::str(stringRef.data, stringRef.length); }, - "Returns the dialect namespace for the Opaque type as a string"); + "Returns the dialect namespace for the Opaque type as a string."); c.def_property_readonly( "data", [](PyOpaqueType &self) { MlirStringRef stringRef = mlirOpaqueTypeGetData(self); return py::str(stringRef.data, stringRef.length); }, - "Returns the data for the Opaque type as a string"); + "Returns the data for the Opaque type as a string."); } }; diff --git a/mlir/lib/CAPI/IR/BuiltinTypes.cpp b/mlir/lib/CAPI/IR/BuiltinTypes.cpp --- a/mlir/lib/CAPI/IR/BuiltinTypes.cpp +++ b/mlir/lib/CAPI/IR/BuiltinTypes.cpp @@ -366,10 +366,10 @@ bool mlirTypeIsAOpaque(MlirType type) { return unwrap(type).isa(); } MlirType mlirOpaqueTypeGet(MlirContext ctx, MlirStringRef dialectNamespace, - intptr_t dataLength, const char *data) { + MlirStringRef typeData) { return wrap( OpaqueType::get(StringAttr::get(unwrap(ctx), unwrap(dialectNamespace)), - StringRef(data, dataLength))); + unwrap(typeData))); } MlirStringRef mlirOpaqueTypeGetDialectNamespace(MlirType type) { diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -795,6 +795,19 @@ fprintf(stderr, "\n"); // CHECK: (index, i1) -> (i16, i32, i64) + // Opaque type. + MlirStringRef namespace = mlirStringRefCreate("dialect", 7); + MlirStringRef data = mlirStringRefCreate("type", 4); + MlirType opaque = mlirOpaqueTypeGet(ctx, namespace, data); + if (!mlirTypeIsAOpaque(opaque) || + !mlirStringRefEqual(mlirOpaqueTypeGetDialectNamespace(opaque), + namespace) || + !mlirStringRefEqual(mlirOpaqueTypeGetData(opaque), data)) + return 25; + mlirTypeDump(opaque); + fprintf(stderr, "\n"); + // CHECK: !dialect.type + return 0; } diff --git a/mlir/test/python/ir/builtin_types.py b/mlir/test/python/ir/builtin_types.py --- a/mlir/test/python/ir/builtin_types.py +++ b/mlir/test/python/ir/builtin_types.py @@ -474,12 +474,13 @@ # CHECK: RESULTS: [Type(index)] print("RESULTS:", func.results) + # CHECK-LABEL: TEST: testOpaqueType @run def testOpaqueType(): with Context() as ctx: ctx.allow_unregistered_dialects = True - opaque = OpaqueType.get("dialect", b"type") + opaque = OpaqueType.get("dialect", "type") # CHECK: opaque type: !dialect.type print("opaque type:", opaque) # CHECK: dialect namespace: dialect