1722475a3SStella Laurenzo //===- MainModule.cpp - Main pybind module --------------------------------===//
2722475a3SStella Laurenzo //
3722475a3SStella Laurenzo // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4722475a3SStella Laurenzo // See https://llvm.org/LICENSE.txt for license information.
5722475a3SStella Laurenzo // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6722475a3SStella Laurenzo //
7722475a3SStella Laurenzo //===----------------------------------------------------------------------===//
8722475a3SStella Laurenzo
9722475a3SStella Laurenzo #include <tuple>
10722475a3SStella Laurenzo
11013b9322SStella Laurenzo #include "PybindUtils.h"
12722475a3SStella Laurenzo
13013b9322SStella Laurenzo #include "Globals.h"
14436c6c9cSStella Laurenzo #include "IRModule.h"
15dc43f785SMehdi Amini #include "Pass.h"
16722475a3SStella Laurenzo
1795b77f2eSStella Laurenzo namespace py = pybind11;
18722475a3SStella Laurenzo using namespace mlir;
1995b77f2eSStella Laurenzo using namespace mlir::python;
20722475a3SStella Laurenzo
21013b9322SStella Laurenzo // -----------------------------------------------------------------------------
22013b9322SStella Laurenzo // Module initialization.
23013b9322SStella Laurenzo // -----------------------------------------------------------------------------
24013b9322SStella Laurenzo
PYBIND11_MODULE(_mlir,m)25722475a3SStella Laurenzo PYBIND11_MODULE(_mlir, m) {
26722475a3SStella Laurenzo m.doc() = "MLIR Python Native Extension";
27722475a3SStella Laurenzo
28f05ff4f7SStella Laurenzo py::class_<PyGlobals>(m, "_Globals", py::module_local())
29013b9322SStella Laurenzo .def_property("dialect_search_modules",
30013b9322SStella Laurenzo &PyGlobals::getDialectSearchPrefixes,
31013b9322SStella Laurenzo &PyGlobals::setDialectSearchPrefixes)
32*a6e7d024SStella Laurenzo .def(
33*a6e7d024SStella Laurenzo "append_dialect_search_prefix",
34013b9322SStella Laurenzo [](PyGlobals &self, std::string moduleName) {
35013b9322SStella Laurenzo self.getDialectSearchPrefixes().push_back(std::move(moduleName));
368260db75SStella Laurenzo self.clearImportCache();
37*a6e7d024SStella Laurenzo },
38*a6e7d024SStella Laurenzo py::arg("module_name"))
39013b9322SStella Laurenzo .def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
40*a6e7d024SStella Laurenzo py::arg("dialect_namespace"), py::arg("dialect_class"),
41013b9322SStella Laurenzo "Testing hook for directly registering a dialect")
42013b9322SStella Laurenzo .def("_register_operation_impl", &PyGlobals::registerOperationImpl,
43*a6e7d024SStella Laurenzo py::arg("operation_name"), py::arg("operation_class"),
44*a6e7d024SStella Laurenzo py::arg("raw_opview_class"),
45013b9322SStella Laurenzo "Testing hook for directly registering an operation");
46013b9322SStella Laurenzo
47013b9322SStella Laurenzo // Aside from making the globals accessible to python, having python manage
48013b9322SStella Laurenzo // it is necessary to make sure it is destroyed (and releases its python
49013b9322SStella Laurenzo // resources) properly.
50013b9322SStella Laurenzo m.attr("globals") =
51013b9322SStella Laurenzo py::cast(new PyGlobals, py::return_value_policy::take_ownership);
52013b9322SStella Laurenzo
53013b9322SStella Laurenzo // Registration decorators.
54013b9322SStella Laurenzo m.def(
55013b9322SStella Laurenzo "register_dialect",
56013b9322SStella Laurenzo [](py::object pyClass) {
57013b9322SStella Laurenzo std::string dialectNamespace =
58013b9322SStella Laurenzo pyClass.attr("DIALECT_NAMESPACE").cast<std::string>();
59013b9322SStella Laurenzo PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
60013b9322SStella Laurenzo return pyClass;
61013b9322SStella Laurenzo },
62*a6e7d024SStella Laurenzo py::arg("dialect_class"),
63013b9322SStella Laurenzo "Class decorator for registering a custom Dialect wrapper");
64013b9322SStella Laurenzo m.def(
65013b9322SStella Laurenzo "register_operation",
66013b9322SStella Laurenzo [](py::object dialectClass) -> py::cpp_function {
67013b9322SStella Laurenzo return py::cpp_function(
68013b9322SStella Laurenzo [dialectClass](py::object opClass) -> py::object {
69013b9322SStella Laurenzo std::string operationName =
70013b9322SStella Laurenzo opClass.attr("OPERATION_NAME").cast<std::string>();
71013b9322SStella Laurenzo auto rawSubclass = PyOpView::createRawSubclass(opClass);
72013b9322SStella Laurenzo PyGlobals::get().registerOperationImpl(operationName, opClass,
73013b9322SStella Laurenzo rawSubclass);
74013b9322SStella Laurenzo
75013b9322SStella Laurenzo // Dict-stuff the new opClass by name onto the dialect class.
76013b9322SStella Laurenzo py::object opClassName = opClass.attr("__name__");
77013b9322SStella Laurenzo dialectClass.attr(opClassName) = opClass;
78013b9322SStella Laurenzo
79013b9322SStella Laurenzo // Now create a special "Raw" subclass that passes through
80013b9322SStella Laurenzo // construction to the OpView parent (bypasses the intermediate
81013b9322SStella Laurenzo // child's __init__).
82013b9322SStella Laurenzo opClass.attr("_Raw") = rawSubclass;
83013b9322SStella Laurenzo return opClass;
84013b9322SStella Laurenzo });
85013b9322SStella Laurenzo },
86*a6e7d024SStella Laurenzo py::arg("dialect_class"),
87*a6e7d024SStella Laurenzo "Produce a class decorator for registering an Operation class as part of "
88*a6e7d024SStella Laurenzo "a dialect");
89013b9322SStella Laurenzo
90fcd2969dSzhanghb97 // Define and populate IR submodule.
91fcd2969dSzhanghb97 auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
92436c6c9cSStella Laurenzo populateIRCore(irModule);
93436c6c9cSStella Laurenzo populateIRAffine(irModule);
94436c6c9cSStella Laurenzo populateIRAttributes(irModule);
9514c92070SAlex Zinenko populateIRInterfaces(irModule);
96436c6c9cSStella Laurenzo populateIRTypes(irModule);
97dc43f785SMehdi Amini
98dc43f785SMehdi Amini // Define and populate PassManager submodule.
99dc43f785SMehdi Amini auto passModule =
100dc43f785SMehdi Amini m.def_submodule("passmanager", "MLIR Pass Management Bindings");
101dc43f785SMehdi Amini populatePassManagerSubmodule(passModule);
102722475a3SStella Laurenzo }
103