[Mlir-commits] [llvm] [mlir] [mlir][Python] create MLIRPythonSupport (PR #171775)
Maksim Levental
llvmlistbot at llvm.org
Sat Dec 27 10:25:57 PST 2025
================
@@ -1315,34 +1340,1052 @@ class PySymbolTable {
/// Custom exception that allows access to error diagnostic information. This is
/// converted to the `ir.MLIRError` python exception when thrown.
-struct MLIRError {
+struct MLIR_PYTHON_API_EXPORTED MLIRError {
MLIRError(llvm::Twine message,
std::vector<PyDiagnostic::DiagnosticInfo> &&errorDiagnostics = {})
: message(message.str()), errorDiagnostics(std::move(errorDiagnostics)) {}
std::string message;
std::vector<PyDiagnostic::DiagnosticInfo> errorDiagnostics;
};
-void populateIRAffine(nanobind::module_ &m);
-void populateIRAttributes(nanobind::module_ &m);
-void populateIRCore(nanobind::module_ &m);
-void populateIRInterfaces(nanobind::module_ &m);
-void populateIRTypes(nanobind::module_ &m);
+//------------------------------------------------------------------------------
+// Utilities.
+//------------------------------------------------------------------------------
+
+/// Helper for creating an @classmethod.
+template <class Func, typename... Args>
+nanobind::object classmethod(Func f, Args... args) {
+ nanobind::object cf = nanobind::cpp_function(f, args...);
+ return nanobind::borrow<nanobind::object>((PyClassMethod_New(cf.ptr())));
+}
+
+inline nanobind::object
+createCustomDialectWrapper(const std::string &dialectNamespace,
+ nanobind::object dialectDescriptor) {
+ auto dialectClass = PyGlobals::get().lookupDialectClass(dialectNamespace);
+ if (!dialectClass) {
+ // Use the base class.
+ return nanobind::cast(PyDialect(std::move(dialectDescriptor)));
+ }
+
+ // Create the custom implementation.
+ return (*dialectClass)(std::move(dialectDescriptor));
+}
+
+inline MlirStringRef toMlirStringRef(const std::string &s) {
+ return mlirStringRefCreate(s.data(), s.size());
+}
+
+inline MlirStringRef toMlirStringRef(std::string_view s) {
+ return mlirStringRefCreate(s.data(), s.size());
+}
+
+inline MlirStringRef toMlirStringRef(const nanobind::bytes &s) {
+ return mlirStringRefCreate(static_cast<const char *>(s.data()), s.size());
+}
+
+/// Create a block, using the current location context if no locations are
+/// specified.
+inline MlirBlock
+createBlock(const nanobind::sequence &pyArgTypes,
+ const std::optional<nanobind::sequence> &pyArgLocs) {
+ SmallVector<MlirType> argTypes;
+ argTypes.reserve(nanobind::len(pyArgTypes));
+ for (const auto &pyType : pyArgTypes)
+ argTypes.push_back(nanobind::cast<PyType &>(pyType));
+
+ SmallVector<MlirLocation> argLocs;
+ if (pyArgLocs) {
+ argLocs.reserve(nanobind::len(*pyArgLocs));
+ for (const auto &pyLoc : *pyArgLocs)
+ argLocs.push_back(nanobind::cast<PyLocation &>(pyLoc));
+ } else if (!argTypes.empty()) {
+ argLocs.assign(argTypes.size(), DefaultingPyLocation::resolve());
+ }
+
+ if (argTypes.size() != argLocs.size())
+ throw nanobind::value_error(("Expected " + Twine(argTypes.size()) +
----------------
makslevental wrote:
done
https://github.com/llvm/llvm-project/pull/171775
More information about the Mlir-commits
mailing list