MLIR 22.0.0git
MainModule.cpp
Go to the documentation of this file.
1//===- MainModule.cpp - Main pybind module --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Globals.h"
10#include "IRModule.h"
11#include "NanobindUtils.h"
12#include "Pass.h"
13#include "Rewrite.h"
15
16namespace nb = nanobind;
17using namespace mlir;
18using namespace nb::literals;
19using namespace mlir::python;
20
21// -----------------------------------------------------------------------------
22// Module initialization.
23// -----------------------------------------------------------------------------
24
25NB_MODULE(_mlir, m) {
26 m.doc() = "MLIR Python Native Extension";
27 m.attr("T") = nb::type_var("T");
28 m.attr("U") = nb::type_var("U");
29
30 nb::class_<PyGlobals>(m, "_Globals")
31 .def_prop_rw("dialect_search_modules",
34 .def("append_dialect_search_prefix", &PyGlobals::addDialectSearchPrefix,
35 "module_name"_a)
36 .def(
37 "_check_dialect_module_loaded",
38 [](PyGlobals &self, const std::string &dialectNamespace) {
39 return self.loadDialectModule(dialectNamespace);
40 },
41 "dialect_namespace"_a)
42 .def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
43 "dialect_namespace"_a, "dialect_class"_a,
44 "Testing hook for directly registering a dialect")
45 .def("_register_operation_impl", &PyGlobals::registerOperationImpl,
46 "operation_name"_a, "operation_class"_a, nb::kw_only(),
47 "replace"_a = false,
48 "Testing hook for directly registering an operation")
49 .def("loc_tracebacks_enabled",
50 [](PyGlobals &self) {
52 })
53 .def("set_loc_tracebacks_enabled",
54 [](PyGlobals &self, bool enabled) {
56 })
57 .def("loc_tracebacks_frame_limit",
58 [](PyGlobals &self) {
60 })
61 .def("set_loc_tracebacks_frame_limit",
62 [](PyGlobals &self, std::optional<int> n) {
65 })
66 .def("register_traceback_file_inclusion",
67 [](PyGlobals &self, const std::string &filename) {
69 })
70 .def("register_traceback_file_exclusion",
71 [](PyGlobals &self, const std::string &filename) {
73 });
74
75 // Aside from making the globals accessible to python, having python manage
76 // it is necessary to make sure it is destroyed (and releases its python
77 // resources) properly.
78 m.attr("globals") = nb::cast(new PyGlobals, nb::rv_policy::take_ownership);
79
80 // Registration decorators.
81 m.def(
82 "register_dialect",
83 [](nb::type_object pyClass) {
84 std::string dialectNamespace =
85 nanobind::cast<std::string>(pyClass.attr("DIALECT_NAMESPACE"));
86 PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
87 return pyClass;
88 },
89 "dialect_class"_a,
90 "Class decorator for registering a custom Dialect wrapper");
91 m.def(
92 "register_operation",
93 [](const nb::type_object &dialectClass, bool replace) -> nb::object {
94 return nb::cpp_function(
95 [dialectClass,
96 replace](nb::type_object opClass) -> nb::type_object {
97 std::string operationName =
98 nanobind::cast<std::string>(opClass.attr("OPERATION_NAME"));
99 PyGlobals::get().registerOperationImpl(operationName, opClass,
100 replace);
101 // Dict-stuff the new opClass by name onto the dialect class.
102 nb::object opClassName = opClass.attr("__name__");
103 dialectClass.attr(opClassName) = opClass;
104 return opClass;
105 });
106 },
107 // clang-format off
108 nb::sig("def register_operation(dialect_class: type, *, replace: bool = False) "
109 "-> typing.Callable[[type[T]], type[T]]"),
110 // clang-format on
111 "dialect_class"_a, nb::kw_only(), "replace"_a = false,
112 "Produce a class decorator for registering an Operation class as part of "
113 "a dialect");
114 m.def(
116 [](MlirTypeID mlirTypeID, bool replace) -> nb::object {
117 return nb::cpp_function([mlirTypeID, replace](
118 nb::callable typeCaster) -> nb::object {
119 PyGlobals::get().registerTypeCaster(mlirTypeID, typeCaster, replace);
120 return typeCaster;
121 });
122 },
123 // clang-format off
124 nb::sig("def register_type_caster(typeid: _mlir.ir.TypeID, *, replace: bool = False) "
125 "-> typing.Callable[[typing.Callable[[T], U]], typing.Callable[[T], U]]"),
126 // clang-format on
127 "typeid"_a, nb::kw_only(), "replace"_a = false,
128 "Register a type caster for casting MLIR types to custom user types.");
129 m.def(
131 [](MlirTypeID mlirTypeID, bool replace) -> nb::object {
132 return nb::cpp_function(
133 [mlirTypeID, replace](nb::callable valueCaster) -> nb::object {
134 PyGlobals::get().registerValueCaster(mlirTypeID, valueCaster,
135 replace);
136 return valueCaster;
137 });
138 },
139 // clang-format off
140 nb::sig("def register_value_caster(typeid: _mlir.ir.TypeID, *, replace: bool = False) "
141 "-> typing.Callable[[typing.Callable[[T], U]], typing.Callable[[T], U]]"),
142 // clang-format on
143 "typeid"_a, nb::kw_only(), "replace"_a = false,
144 "Register a value caster for casting MLIR values to custom user values.");
145
146 // Define and populate IR submodule.
147 auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
148 populateIRCore(irModule);
149 populateIRAffine(irModule);
150 populateIRAttributes(irModule);
151 populateIRInterfaces(irModule);
152 populateIRTypes(irModule);
153
154 auto rewriteModule = m.def_submodule("rewrite", "MLIR Rewrite Bindings");
155 populateRewriteSubmodule(rewriteModule);
156
157 // Define and populate PassManager submodule.
158 auto passManagerModule =
159 m.def_submodule("passmanager", "MLIR Pass Management Bindings");
160 populatePassManagerSubmodule(passManagerModule);
161}
#define MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the value caster registration bindin...
Definition Interop.h:142
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
Definition Interop.h:130
NB_MODULE(_mlir, m)
void registerTracebackFileExclusion(const std::string &file)
Definition IRModule.cpp:233
static constexpr size_t kMaxFrames
Definition Globals.h:138
void setLocTracebackFramesLimit(size_t value)
Definition IRModule.cpp:216
void registerTracebackFileInclusion(const std::string &file)
Definition IRModule.cpp:221
Globals that are always accessible once the extension has been initialized.
Definition Globals.h:33
bool loadDialectModule(llvm::StringRef dialectNamespace)
Loads a python module corresponding to the given dialect namespace.
Definition IRModule.cpp:40
void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster, bool replace=false)
Adds a user-friendly type caster.
Definition IRModule.cpp:87
std::vector< std::string > getDialectSearchPrefixes()
Get and set the list of parent modules to search for dialect implementation classes.
Definition Globals.h:46
void addDialectSearchPrefix(std::string value)
Definition Globals.h:54
void registerOperationImpl(const std::string &operationName, nanobind::object pyClass, bool replace=false)
Adds a concrete implementation operation class.
Definition IRModule.cpp:119
TracebackLoc & getTracebackLoc()
Definition Globals.h:153
static PyGlobals & get()
Most code should get the globals via this static accessor.
Definition Globals.h:39
void registerValueCaster(MlirTypeID mlirTypeID, nanobind::callable valueCaster, bool replace=false)
Adds a user-friendly value caster.
Definition IRModule.cpp:97
void setDialectSearchPrefixes(std::vector< std::string > newValues)
Definition Globals.h:50
void registerDialectImpl(const std::string &dialectNamespace, nanobind::object pyClass)
Adds a concrete implementation dialect class.
Definition IRModule.cpp:107
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.