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 @@ -902,7 +902,8 @@ // Apply unpacked/validated to the operation state. Beyond this // point, exceptions cannot be thrown or else the state will leak. - MlirOperationState state = mlirOperationStateGet(name.c_str(), location->loc); + MlirOperationState state = mlirOperationStateGet( + mlirStringRefCreateFromCString(name.c_str()), location->loc); if (!mlirOperands.empty()) mlirOperationStateAddOperands(&state, mlirOperands.size(), mlirOperands.data()); @@ -916,8 +917,8 @@ llvm::SmallVector mlirNamedAttributes; mlirNamedAttributes.reserve(mlirAttributes.size()); for (auto &it : mlirAttributes) - mlirNamedAttributes.push_back( - mlirNamedAttributeGet(it.first.c_str(), it.second)); + mlirNamedAttributes.push_back(mlirNamedAttributeGet( + mlirStringRefCreateFromCString(it.first.c_str()), it.second)); mlirOperationStateAddAttributes(&state, mlirNamedAttributes.size(), mlirNamedAttributes.data()); } @@ -1076,7 +1077,8 @@ PyNamedAttribute::PyNamedAttribute(MlirAttribute attr, std::string ownedName) : ownedName(new std::string(std::move(ownedName))) { - namedAttr = mlirNamedAttributeGet(this->ownedName->c_str(), attr); + namedAttr = mlirNamedAttributeGet( + mlirStringRefCreateFromCString(this->ownedName->c_str()), attr); } //------------------------------------------------------------------------------ @@ -1287,8 +1289,8 @@ PyOpAttributeMap(PyOperationRef operation) : operation(operation) {} PyAttribute dunderGetItemNamed(const std::string &name) { - MlirAttribute attr = - mlirOperationGetAttributeByName(operation->get(), name.c_str()); + MlirAttribute attr = mlirOperationGetAttributeByName( + operation->get(), mlirStringRefCreateFromCString(name.c_str())); if (mlirAttributeIsNull(attr)) { throw SetPyError(PyExc_KeyError, "attempt to access a non-existent attribute"); @@ -1303,7 +1305,8 @@ } MlirNamedAttribute namedAttr = mlirOperationGetAttribute(operation->get(), index); - return PyNamedAttribute(namedAttr.attribute, std::string(namedAttr.name)); + return PyNamedAttribute(namedAttr.attribute, + std::string(namedAttr.name.data)); } intptr_t dunderLen() { @@ -1311,8 +1314,8 @@ } bool dunderContains(const std::string &name) { - return !mlirAttributeIsNull( - mlirOperationGetAttributeByName(operation->get(), name.c_str())); + return !mlirAttributeIsNull(mlirOperationGetAttributeByName( + operation->get(), mlirStringRefCreateFromCString(name.c_str()))); } static void bind(py::module &m) { @@ -2585,9 +2588,12 @@ "file", [](std::string filename, int line, int col, DefaultingPyMlirContext context) { - return PyLocation(context->getRef(), - mlirLocationFileLineColGet( - context->get(), filename.c_str(), line, col)); + return PyLocation( + context->getRef(), + mlirLocationFileLineColGet( + context->get(), + mlirStringRefCreateFromCString(filename.c_str()), line, + col)); }, py::arg("filename"), py::arg("line"), py::arg("col"), py::arg("context") = py::none(), kContextGetFileLocationDocstring) @@ -2611,8 +2617,9 @@ .def_static( "parse", [](const std::string moduleAsm, DefaultingPyMlirContext context) { - MlirModule module = - mlirModuleCreateParse(context->get(), moduleAsm.c_str()); + MlirModule module = mlirModuleCreateParse( + context->get(), + mlirStringRefCreateFromCString(moduleAsm.c_str())); // TODO: Rework error reporting once diagnostic engine is exposed // in C API. if (mlirModuleIsNull(module)) { @@ -2861,8 +2868,9 @@ .def_static( "parse", [](std::string attrSpec, DefaultingPyMlirContext context) { - MlirAttribute type = - mlirAttributeParseGet(context->get(), attrSpec.c_str()); + MlirAttribute type = mlirAttributeParseGet( + context->get(), + mlirStringRefCreateFromCString(attrSpec.c_str())); // TODO: Rework error reporting once diagnostic engine is exposed // in C API. if (mlirAttributeIsNull(type)) { @@ -2926,7 +2934,7 @@ [](PyNamedAttribute &self) { PyPrintAccumulator printAccum; printAccum.parts.append("NamedAttribute("); - printAccum.parts.append(self.namedAttr.name); + printAccum.parts.append(self.namedAttr.name.data); printAccum.parts.append("="); mlirAttributePrint(self.namedAttr.attribute, printAccum.getCallback(), @@ -2937,7 +2945,8 @@ .def_property_readonly( "name", [](PyNamedAttribute &self) { - return py::str(self.namedAttr.name, strlen(self.namedAttr.name)); + return py::str(self.namedAttr.name.data, + self.namedAttr.name.length); }, "The name of the NamedAttribute binding") .def_property_readonly( @@ -2969,7 +2978,9 @@ .def_static( "parse", [](std::string typeSpec, DefaultingPyMlirContext context) { - MlirType type = mlirTypeParseGet(context->get(), typeSpec.c_str()); + MlirType type = mlirTypeParseGet( + context->get(), + mlirStringRefCreateFromCString(typeSpec.c_str())); // TODO: Rework error reporting once diagnostic engine is exposed // in C API. if (mlirTypeIsNull(type)) { diff --git a/mlir/lib/Bindings/Python/PybindUtils.h b/mlir/lib/Bindings/Python/PybindUtils.h --- a/mlir/lib/Bindings/Python/PybindUtils.h +++ b/mlir/lib/Bindings/Python/PybindUtils.h @@ -16,7 +16,6 @@ #include #include - namespace mlir { namespace python { @@ -115,10 +114,11 @@ void *getUserData() { return this; } MlirStringCallback getCallback() { - return [](const char *part, intptr_t size, void *userData) { + return [](MlirStringRef part, void *userData) { PyPrintAccumulator *printAccum = static_cast(userData); - pybind11::str pyPart(part, size); // Decodes as UTF-8 by default. + pybind11::str pyPart(part.data, + part.length); // Decodes as UTF-8 by default. printAccum->parts.append(std::move(pyPart)); }; } @@ -139,15 +139,16 @@ void *getUserData() { return this; } MlirStringCallback getCallback() { - return [](const char *part, intptr_t size, void *userData) { + return [](MlirStringRef part, void *userData) { pybind11::gil_scoped_acquire(); PyFileAccumulator *accum = static_cast(userData); if (accum->binary) { // Note: Still has to copy and not avoidable with this API. - pybind11::bytes pyBytes(part, size); + pybind11::bytes pyBytes(part.data, part.length); accum->pyWriteFunction(pyBytes); } else { - pybind11::str pyStr(part, size); // Decodes as UTF-8 by default. + pybind11::str pyStr(part.data, + part.length); // Decodes as UTF-8 by default. accum->pyWriteFunction(pyStr); } }; @@ -165,13 +166,13 @@ void *getUserData() { return this; } MlirStringCallback getCallback() { - return [](const char *part, intptr_t size, void *userData) { + return [](MlirStringRef part, void *userData) { PySinglePartStringAccumulator *accum = static_cast(userData); assert(!accum->invoked && "PySinglePartStringAccumulator called back multiple times"); accum->invoked = true; - accum->value = pybind11::str(part, size); + accum->value = pybind11::str(part.data, part.length); }; }