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 
16 namespace nb = nanobind;
17 using namespace mlir;
18 using namespace nb::literals;
19 using namespace mlir::python;
20 
21 // -----------------------------------------------------------------------------
22 // Module initialization.
23 // -----------------------------------------------------------------------------
24 
25 NB_MODULE(_mlir, m) {
26  m.doc() = "MLIR Python Native Extension";
27 
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,
33  "module_name"_a)
34  .def(
35  "_check_dialect_module_loaded",
36  [](PyGlobals &self, const std::string &dialectNamespace) {
37  return self.loadDialectModule(dialectNamespace);
38  },
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(),
45  "replace"_a = false,
46  "Testing hook for directly registering an operation")
47  .def("loc_tracebacks_enabled",
48  [](PyGlobals &self) {
49  return self.getTracebackLoc().locTracebacksEnabled();
50  })
51  .def("set_loc_tracebacks_enabled",
52  [](PyGlobals &self, bool enabled) {
53  self.getTracebackLoc().setLocTracebacksEnabled(enabled);
54  })
55  .def("set_loc_tracebacks_frame_limit",
56  [](PyGlobals &self, int n) {
57  self.getTracebackLoc().setLocTracebackFramesLimit(n);
58  })
59  .def("register_traceback_file_inclusion",
60  [](PyGlobals &self, const std::string &filename) {
61  self.getTracebackLoc().registerTracebackFileInclusion(filename);
62  })
63  .def("register_traceback_file_exclusion",
64  [](PyGlobals &self, const std::string &filename) {
65  self.getTracebackLoc().registerTracebackFileExclusion(filename);
66  });
67 
68  // Aside from making the globals accessible to python, having python manage
69  // it is necessary to make sure it is destroyed (and releases its python
70  // resources) properly.
71  m.attr("globals") = nb::cast(new PyGlobals, nb::rv_policy::take_ownership);
72 
73  // Registration decorators.
74  m.def(
75  "register_dialect",
76  [](nb::type_object pyClass) {
77  std::string dialectNamespace =
78  nanobind::cast<std::string>(pyClass.attr("DIALECT_NAMESPACE"));
79  PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
80  return pyClass;
81  },
82  "dialect_class"_a,
83  "Class decorator for registering a custom Dialect wrapper");
84  m.def(
85  "register_operation",
86  [](const nb::type_object &dialectClass, bool replace) -> nb::object {
87  return nb::cpp_function(
88  [dialectClass,
89  replace](nb::type_object opClass) -> nb::type_object {
90  std::string operationName =
91  nanobind::cast<std::string>(opClass.attr("OPERATION_NAME"));
92  PyGlobals::get().registerOperationImpl(operationName, opClass,
93  replace);
94  // Dict-stuff the new opClass by name onto the dialect class.
95  nb::object opClassName = opClass.attr("__name__");
96  dialectClass.attr(opClassName) = opClass;
97  return opClass;
98  });
99  },
100  "dialect_class"_a, nb::kw_only(), "replace"_a = false,
101  "Produce a class decorator for registering an Operation class as part of "
102  "a dialect");
103  m.def(
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);
109  return typeCaster;
110  });
111  },
112  "typeid"_a, nb::kw_only(), "replace"_a = false,
113  "Register a type caster for casting MLIR types to custom user types.");
114  m.def(
116  [](MlirTypeID mlirTypeID, bool replace) -> nb::object {
117  return nb::cpp_function(
118  [mlirTypeID, replace](nb::callable valueCaster) -> nb::object {
119  PyGlobals::get().registerValueCaster(mlirTypeID, valueCaster,
120  replace);
121  return valueCaster;
122  });
123  },
124  "typeid"_a, nb::kw_only(), "replace"_a = false,
125  "Register a value caster for casting MLIR values to custom user values.");
126 
127  // Define and populate IR submodule.
128  auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
129  populateIRCore(irModule);
130  populateIRAffine(irModule);
131  populateIRAttributes(irModule);
132  populateIRInterfaces(irModule);
133  populateIRTypes(irModule);
134 
135  auto rewriteModule = m.def_submodule("rewrite", "MLIR Rewrite Bindings");
136  populateRewriteSubmodule(rewriteModule);
137 
138  // Define and populate PassManager submodule.
139  auto passModule =
140  m.def_submodule("passmanager", "MLIR Pass Management Bindings");
141  populatePassManagerSubmodule(passModule);
142 }
#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)
Definition: MainModule.cpp:25
Globals that are always accessible once the extension has been initialized.
Definition: Globals.h:32
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...