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 
13*f13893f6SStella Laurenzo #include "Dialects.h"
1413cb4317SMehdi Amini #include "ExecutionEngine.h"
15013b9322SStella Laurenzo #include "Globals.h"
16436c6c9cSStella Laurenzo #include "IRModule.h"
17dc43f785SMehdi Amini #include "Pass.h"
18722475a3SStella Laurenzo 
1995b77f2eSStella Laurenzo namespace py = pybind11;
20722475a3SStella Laurenzo using namespace mlir;
2195b77f2eSStella Laurenzo using namespace mlir::python;
22722475a3SStella Laurenzo 
23013b9322SStella Laurenzo // -----------------------------------------------------------------------------
24013b9322SStella Laurenzo // Module initialization.
25013b9322SStella Laurenzo // -----------------------------------------------------------------------------
26013b9322SStella Laurenzo 
27722475a3SStella Laurenzo PYBIND11_MODULE(_mlir, m) {
28722475a3SStella Laurenzo   m.doc() = "MLIR Python Native Extension";
29722475a3SStella Laurenzo 
30013b9322SStella Laurenzo   py::class_<PyGlobals>(m, "_Globals")
31013b9322SStella Laurenzo       .def_property("dialect_search_modules",
32013b9322SStella Laurenzo                     &PyGlobals::getDialectSearchPrefixes,
33013b9322SStella Laurenzo                     &PyGlobals::setDialectSearchPrefixes)
34013b9322SStella Laurenzo       .def("append_dialect_search_prefix",
35013b9322SStella Laurenzo            [](PyGlobals &self, std::string moduleName) {
36013b9322SStella Laurenzo              self.getDialectSearchPrefixes().push_back(std::move(moduleName));
378260db75SStella Laurenzo              self.clearImportCache();
38013b9322SStella Laurenzo            })
39013b9322SStella Laurenzo       .def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
40013b9322SStella Laurenzo            "Testing hook for directly registering a dialect")
41013b9322SStella Laurenzo       .def("_register_operation_impl", &PyGlobals::registerOperationImpl,
42013b9322SStella Laurenzo            "Testing hook for directly registering an operation");
43013b9322SStella Laurenzo 
44013b9322SStella Laurenzo   // Aside from making the globals accessible to python, having python manage
45013b9322SStella Laurenzo   // it is necessary to make sure it is destroyed (and releases its python
46013b9322SStella Laurenzo   // resources) properly.
47013b9322SStella Laurenzo   m.attr("globals") =
48013b9322SStella Laurenzo       py::cast(new PyGlobals, py::return_value_policy::take_ownership);
49013b9322SStella Laurenzo 
50013b9322SStella Laurenzo   // Registration decorators.
51013b9322SStella Laurenzo   m.def(
52013b9322SStella Laurenzo       "register_dialect",
53013b9322SStella Laurenzo       [](py::object pyClass) {
54013b9322SStella Laurenzo         std::string dialectNamespace =
55013b9322SStella Laurenzo             pyClass.attr("DIALECT_NAMESPACE").cast<std::string>();
56013b9322SStella Laurenzo         PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
57013b9322SStella Laurenzo         return pyClass;
58013b9322SStella Laurenzo       },
59013b9322SStella Laurenzo       "Class decorator for registering a custom Dialect wrapper");
60013b9322SStella Laurenzo   m.def(
61013b9322SStella Laurenzo       "register_operation",
62013b9322SStella Laurenzo       [](py::object dialectClass) -> py::cpp_function {
63013b9322SStella Laurenzo         return py::cpp_function(
64013b9322SStella Laurenzo             [dialectClass](py::object opClass) -> py::object {
65013b9322SStella Laurenzo               std::string operationName =
66013b9322SStella Laurenzo                   opClass.attr("OPERATION_NAME").cast<std::string>();
67013b9322SStella Laurenzo               auto rawSubclass = PyOpView::createRawSubclass(opClass);
68013b9322SStella Laurenzo               PyGlobals::get().registerOperationImpl(operationName, opClass,
69013b9322SStella Laurenzo                                                      rawSubclass);
70013b9322SStella Laurenzo 
71013b9322SStella Laurenzo               // Dict-stuff the new opClass by name onto the dialect class.
72013b9322SStella Laurenzo               py::object opClassName = opClass.attr("__name__");
73013b9322SStella Laurenzo               dialectClass.attr(opClassName) = opClass;
74013b9322SStella Laurenzo 
75013b9322SStella Laurenzo               // Now create a special "Raw" subclass that passes through
76013b9322SStella Laurenzo               // construction to the OpView parent (bypasses the intermediate
77013b9322SStella Laurenzo               // child's __init__).
78013b9322SStella Laurenzo               opClass.attr("_Raw") = rawSubclass;
79013b9322SStella Laurenzo               return opClass;
80013b9322SStella Laurenzo             });
81013b9322SStella Laurenzo       },
82013b9322SStella Laurenzo       "Class decorator for registering a custom Operation wrapper");
83013b9322SStella Laurenzo 
84fcd2969dSzhanghb97   // Define and populate IR submodule.
85fcd2969dSzhanghb97   auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
86436c6c9cSStella Laurenzo   populateIRCore(irModule);
87436c6c9cSStella Laurenzo   populateIRAffine(irModule);
88436c6c9cSStella Laurenzo   populateIRAttributes(irModule);
89436c6c9cSStella Laurenzo   populateIRTypes(irModule);
90dc43f785SMehdi Amini 
91dc43f785SMehdi Amini   // Define and populate PassManager submodule.
92dc43f785SMehdi Amini   auto passModule =
93dc43f785SMehdi Amini       m.def_submodule("passmanager", "MLIR Pass Management Bindings");
94dc43f785SMehdi Amini   populatePassManagerSubmodule(passModule);
9513cb4317SMehdi Amini 
9613cb4317SMehdi Amini   // Define and populate ExecutionEngine submodule.
9713cb4317SMehdi Amini   auto executionEngineModule =
9813cb4317SMehdi Amini       m.def_submodule("execution_engine", "MLIR JIT Execution Engine");
9913cb4317SMehdi Amini   populateExecutionEngineSubmodule(executionEngineModule);
10043b9fa3cSNicolas Vasilache 
101*f13893f6SStella Laurenzo   // Define and populate dialect submodules.
10243b9fa3cSNicolas Vasilache   auto dialectsModule = m.def_submodule("dialects");
10343b9fa3cSNicolas Vasilache   auto linalgModule = dialectsModule.def_submodule("linalg");
10443b9fa3cSNicolas Vasilache   populateDialectLinalgSubmodule(linalgModule);
105*f13893f6SStella Laurenzo   populateDialectSparseTensorSubmodule(
106*f13893f6SStella Laurenzo       dialectsModule.def_submodule("sparse_tensor"), irModule);
107722475a3SStella Laurenzo }
108