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 
13f13893f6SStella Laurenzo #include "Dialects.h"
14013b9322SStella Laurenzo #include "Globals.h"
15436c6c9cSStella Laurenzo #include "IRModule.h"
16dc43f785SMehdi Amini #include "Pass.h"
17722475a3SStella Laurenzo 
1895b77f2eSStella Laurenzo namespace py = pybind11;
19722475a3SStella Laurenzo using namespace mlir;
2095b77f2eSStella Laurenzo using namespace mlir::python;
21722475a3SStella Laurenzo 
22013b9322SStella Laurenzo // -----------------------------------------------------------------------------
23013b9322SStella Laurenzo // Module initialization.
24013b9322SStella Laurenzo // -----------------------------------------------------------------------------
25013b9322SStella Laurenzo 
26722475a3SStella Laurenzo PYBIND11_MODULE(_mlir, m) {
27722475a3SStella Laurenzo   m.doc() = "MLIR Python Native Extension";
28722475a3SStella Laurenzo 
29*f05ff4f7SStella Laurenzo   py::class_<PyGlobals>(m, "_Globals", py::module_local())
30013b9322SStella Laurenzo       .def_property("dialect_search_modules",
31013b9322SStella Laurenzo                     &PyGlobals::getDialectSearchPrefixes,
32013b9322SStella Laurenzo                     &PyGlobals::setDialectSearchPrefixes)
33013b9322SStella Laurenzo       .def("append_dialect_search_prefix",
34013b9322SStella Laurenzo            [](PyGlobals &self, std::string moduleName) {
35013b9322SStella Laurenzo              self.getDialectSearchPrefixes().push_back(std::move(moduleName));
368260db75SStella Laurenzo              self.clearImportCache();
37013b9322SStella Laurenzo            })
38013b9322SStella Laurenzo       .def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
39013b9322SStella Laurenzo            "Testing hook for directly registering a dialect")
40013b9322SStella Laurenzo       .def("_register_operation_impl", &PyGlobals::registerOperationImpl,
41013b9322SStella Laurenzo            "Testing hook for directly registering an operation");
42013b9322SStella Laurenzo 
43013b9322SStella Laurenzo   // Aside from making the globals accessible to python, having python manage
44013b9322SStella Laurenzo   // it is necessary to make sure it is destroyed (and releases its python
45013b9322SStella Laurenzo   // resources) properly.
46013b9322SStella Laurenzo   m.attr("globals") =
47013b9322SStella Laurenzo       py::cast(new PyGlobals, py::return_value_policy::take_ownership);
48013b9322SStella Laurenzo 
49013b9322SStella Laurenzo   // Registration decorators.
50013b9322SStella Laurenzo   m.def(
51013b9322SStella Laurenzo       "register_dialect",
52013b9322SStella Laurenzo       [](py::object pyClass) {
53013b9322SStella Laurenzo         std::string dialectNamespace =
54013b9322SStella Laurenzo             pyClass.attr("DIALECT_NAMESPACE").cast<std::string>();
55013b9322SStella Laurenzo         PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
56013b9322SStella Laurenzo         return pyClass;
57013b9322SStella Laurenzo       },
58013b9322SStella Laurenzo       "Class decorator for registering a custom Dialect wrapper");
59013b9322SStella Laurenzo   m.def(
60013b9322SStella Laurenzo       "register_operation",
61013b9322SStella Laurenzo       [](py::object dialectClass) -> py::cpp_function {
62013b9322SStella Laurenzo         return py::cpp_function(
63013b9322SStella Laurenzo             [dialectClass](py::object opClass) -> py::object {
64013b9322SStella Laurenzo               std::string operationName =
65013b9322SStella Laurenzo                   opClass.attr("OPERATION_NAME").cast<std::string>();
66013b9322SStella Laurenzo               auto rawSubclass = PyOpView::createRawSubclass(opClass);
67013b9322SStella Laurenzo               PyGlobals::get().registerOperationImpl(operationName, opClass,
68013b9322SStella Laurenzo                                                      rawSubclass);
69013b9322SStella Laurenzo 
70013b9322SStella Laurenzo               // Dict-stuff the new opClass by name onto the dialect class.
71013b9322SStella Laurenzo               py::object opClassName = opClass.attr("__name__");
72013b9322SStella Laurenzo               dialectClass.attr(opClassName) = opClass;
73013b9322SStella Laurenzo 
74013b9322SStella Laurenzo               // Now create a special "Raw" subclass that passes through
75013b9322SStella Laurenzo               // construction to the OpView parent (bypasses the intermediate
76013b9322SStella Laurenzo               // child's __init__).
77013b9322SStella Laurenzo               opClass.attr("_Raw") = rawSubclass;
78013b9322SStella Laurenzo               return opClass;
79013b9322SStella Laurenzo             });
80013b9322SStella Laurenzo       },
81013b9322SStella Laurenzo       "Class decorator for registering a custom Operation wrapper");
82013b9322SStella Laurenzo 
83fcd2969dSzhanghb97   // Define and populate IR submodule.
84fcd2969dSzhanghb97   auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
85436c6c9cSStella Laurenzo   populateIRCore(irModule);
86436c6c9cSStella Laurenzo   populateIRAffine(irModule);
87436c6c9cSStella Laurenzo   populateIRAttributes(irModule);
88436c6c9cSStella Laurenzo   populateIRTypes(irModule);
89dc43f785SMehdi Amini 
90dc43f785SMehdi Amini   // Define and populate PassManager submodule.
91dc43f785SMehdi Amini   auto passModule =
92dc43f785SMehdi Amini       m.def_submodule("passmanager", "MLIR Pass Management Bindings");
93dc43f785SMehdi Amini   populatePassManagerSubmodule(passModule);
9413cb4317SMehdi Amini 
95f13893f6SStella Laurenzo   // Define and populate dialect submodules.
9643b9fa3cSNicolas Vasilache   auto dialectsModule = m.def_submodule("dialects");
9743b9fa3cSNicolas Vasilache   auto linalgModule = dialectsModule.def_submodule("linalg");
9843b9fa3cSNicolas Vasilache   populateDialectLinalgSubmodule(linalgModule);
99f13893f6SStella Laurenzo   populateDialectSparseTensorSubmodule(
100f13893f6SStella Laurenzo       dialectsModule.def_submodule("sparse_tensor"), irModule);
101722475a3SStella Laurenzo }
102