18 using namespace nb::literals;
26 m.doc() =
"MLIR Python Native Extension";
28 nb::class_<PyGlobals>(m,
"_Globals")
29 .def_prop_rw(
"dialect_search_modules",
30 &PyGlobals::getDialectSearchPrefixes,
31 &PyGlobals::setDialectSearchPrefixes)
32 .def(
"append_dialect_search_prefix", &PyGlobals::addDialectSearchPrefix,
35 "_check_dialect_module_loaded",
36 [](
PyGlobals &
self,
const std::string &dialectNamespace) {
37 return self.loadDialectModule(dialectNamespace);
39 "dialect_namespace"_a)
40 .def(
"_register_dialect_impl", &PyGlobals::registerDialectImpl,
41 "dialect_namespace"_a,
"dialect_class"_a,
42 "Testing hook for directly registering a dialect")
43 .def(
"_register_operation_impl", &PyGlobals::registerOperationImpl,
44 "operation_name"_a,
"operation_class"_a, nb::kw_only(),
46 "Testing hook for directly registering an operation")
47 .def(
"loc_tracebacks_enabled",
49 return self.getTracebackLoc().locTracebacksEnabled();
51 .def(
"set_loc_tracebacks_enabled",
53 self.getTracebackLoc().setLocTracebacksEnabled(enabled);
55 .def(
"set_loc_tracebacks_frame_limit",
57 self.getTracebackLoc().setLocTracebackFramesLimit(n);
59 .def(
"register_traceback_file_inclusion",
60 [](
PyGlobals &
self,
const std::string &filename) {
61 self.getTracebackLoc().registerTracebackFileInclusion(filename);
63 .def(
"register_traceback_file_exclusion",
64 [](
PyGlobals &
self,
const std::string &filename) {
65 self.getTracebackLoc().registerTracebackFileExclusion(filename);
71 m.attr(
"globals") = nb::cast(
new PyGlobals, nb::rv_policy::take_ownership);
76 [](nb::type_object pyClass) {
77 std::string dialectNamespace =
78 nanobind::cast<std::string>(pyClass.attr(
"DIALECT_NAMESPACE"));
83 "Class decorator for registering a custom Dialect wrapper");
86 [](
const nb::type_object &dialectClass,
bool replace) -> nb::object {
87 return nb::cpp_function(
89 replace](nb::type_object opClass) -> nb::type_object {
90 std::string operationName =
91 nanobind::cast<std::string>(opClass.attr(
"OPERATION_NAME"));
95 nb::object opClassName = opClass.attr(
"__name__");
96 dialectClass.attr(opClassName) = opClass;
100 "dialect_class"_a, nb::kw_only(),
"replace"_a =
false,
101 "Produce a class decorator for registering an Operation class as part of "
105 [](MlirTypeID mlirTypeID,
bool replace) -> nb::object {
106 return nb::cpp_function([mlirTypeID, replace](
107 nb::callable typeCaster) -> nb::object {
108 PyGlobals::get().registerTypeCaster(mlirTypeID, typeCaster, replace);
112 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
113 "Register a type caster for casting MLIR types to custom user types.");
116 [](MlirTypeID mlirTypeID,
bool replace) -> nb::object {
117 return nb::cpp_function(
118 [mlirTypeID, replace](nb::callable valueCaster) -> nb::object {
124 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
125 "Register a value caster for casting MLIR values to custom user values.");
128 auto irModule = m.def_submodule(
"ir",
"MLIR IR Bindings");
135 auto rewriteModule = m.def_submodule(
"rewrite",
"MLIR Rewrite Bindings");
140 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...