19 using namespace nb::literals;
27 m.doc() =
"MLIR Python Native Extension";
29 nb::class_<PyGlobals>(m,
"_Globals")
30 .def_prop_rw(
"dialect_search_modules",
31 &PyGlobals::getDialectSearchPrefixes,
32 &PyGlobals::setDialectSearchPrefixes)
34 "append_dialect_search_prefix",
35 [](
PyGlobals &
self, std::string moduleName) {
36 self.getDialectSearchPrefixes().push_back(std::move(moduleName));
40 "_check_dialect_module_loaded",
41 [](
PyGlobals &
self,
const std::string &dialectNamespace) {
42 return self.loadDialectModule(dialectNamespace);
44 "dialect_namespace"_a)
45 .def(
"_register_dialect_impl", &PyGlobals::registerDialectImpl,
46 "dialect_namespace"_a,
"dialect_class"_a,
47 "Testing hook for directly registering a dialect")
48 .def(
"_register_operation_impl", &PyGlobals::registerOperationImpl,
49 "operation_name"_a,
"operation_class"_a, nb::kw_only(),
51 "Testing hook for directly registering an operation");
56 m.attr(
"globals") = nb::cast(
new PyGlobals, nb::rv_policy::take_ownership);
61 [](nb::type_object pyClass) {
62 std::string dialectNamespace =
63 nanobind::cast<std::string>(pyClass.attr(
"DIALECT_NAMESPACE"));
68 "Class decorator for registering a custom Dialect wrapper");
71 [](
const nb::type_object &dialectClass,
bool replace) -> nb::object {
72 return nb::cpp_function(
74 replace](nb::type_object opClass) -> nb::type_object {
75 std::string operationName =
76 nanobind::cast<std::string>(opClass.attr(
"OPERATION_NAME"));
81 nb::object opClassName = opClass.attr(
"__name__");
82 dialectClass.attr(opClassName) = opClass;
86 "dialect_class"_a, nb::kw_only(),
"replace"_a =
false,
87 "Produce a class decorator for registering an Operation class as part of "
91 [](MlirTypeID mlirTypeID,
bool replace) -> nb::object {
92 return nb::cpp_function([mlirTypeID, replace](
93 nb::callable typeCaster) -> nb::object {
94 PyGlobals::get().registerTypeCaster(mlirTypeID, typeCaster, replace);
98 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
99 "Register a type caster for casting MLIR types to custom user types.");
102 [](MlirTypeID mlirTypeID,
bool replace) -> nb::object {
103 return nb::cpp_function(
104 [mlirTypeID, replace](nb::callable valueCaster) -> nb::object {
110 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
111 "Register a value caster for casting MLIR values to custom user values.");
114 auto irModule = m.def_submodule(
"ir",
"MLIR IR Bindings");
121 auto rewriteModule = m.def_submodule(
"rewrite",
"MLIR Rewrite Bindings");
126 m.def_submodule(
"passmanager",
"MLIR Pass Management Bindings");
#define MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the value caster registration bindin...
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
Globals that are always accessible once the extension has been initialized.
void populateIRAttributes(nanobind::module_ &m)
void populateIRInterfaces(nb::module_ &m)
void populatePassManagerSubmodule(nanobind::module_ &m)
void populateIRAffine(nanobind::module_ &m)
void populateRewriteSubmodule(nanobind::module_ &m)
void populateIRTypes(nanobind::module_ &m)
void populateIRCore(nanobind::module_ &m)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...