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 <tuple> 10 11 #include "PybindUtils.h" 12 13 #include "Globals.h" 14 #include "IRModule.h" 15 #include "Pass.h" 16 17 namespace py = pybind11; 18 using namespace mlir; 19 using namespace py::literals; 20 using namespace mlir::python; 21 22 // ----------------------------------------------------------------------------- 23 // Module initialization. 24 // ----------------------------------------------------------------------------- 25 26 PYBIND11_MODULE(_mlir, m) { 27 m.doc() = "MLIR Python Native Extension"; 28 29 py::class_<PyGlobals>(m, "_Globals", py::module_local()) 30 .def_property("dialect_search_modules", 31 &PyGlobals::getDialectSearchPrefixes, 32 &PyGlobals::setDialectSearchPrefixes) 33 .def( 34 "append_dialect_search_prefix", 35 [](PyGlobals &self, std::string moduleName) { 36 self.getDialectSearchPrefixes().push_back(std::move(moduleName)); 37 self.clearImportCache(); 38 }, 39 "module_name"_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, "replace"_a = false, 45 "Testing hook for directly registering an operation"); 46 47 // Aside from making the globals accessible to python, having python manage 48 // it is necessary to make sure it is destroyed (and releases its python 49 // resources) properly. 50 m.attr("globals") = 51 py::cast(new PyGlobals, py::return_value_policy::take_ownership); 52 53 // Registration decorators. 54 m.def( 55 "register_dialect", 56 [](py::object pyClass) { 57 std::string dialectNamespace = 58 pyClass.attr("DIALECT_NAMESPACE").cast<std::string>(); 59 PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass); 60 return pyClass; 61 }, 62 "dialect_class"_a, 63 "Class decorator for registering a custom Dialect wrapper"); 64 m.def( 65 "register_operation", 66 [](const py::object &dialectClass, bool replace) -> py::cpp_function { 67 return py::cpp_function( 68 [dialectClass, replace](py::object opClass) -> py::object { 69 std::string operationName = 70 opClass.attr("OPERATION_NAME").cast<std::string>(); 71 PyGlobals::get().registerOperationImpl(operationName, opClass, 72 replace); 73 74 // Dict-stuff the new opClass by name onto the dialect class. 75 py::object opClassName = opClass.attr("__name__"); 76 dialectClass.attr(opClassName) = opClass; 77 return opClass; 78 }); 79 }, 80 "dialect_class"_a, "replace"_a = false, 81 "Produce a class decorator for registering an Operation class as part of " 82 "a dialect"); 83 m.def( 84 MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR, 85 [](MlirTypeID mlirTypeID, py::function typeCaster, bool replace) { 86 PyGlobals::get().registerTypeCaster(mlirTypeID, std::move(typeCaster), 87 replace); 88 }, 89 "typeid"_a, "type_caster"_a, "replace"_a = false, 90 "Register a type caster for casting MLIR types to custom user types."); 91 92 // Define and populate IR submodule. 93 auto irModule = m.def_submodule("ir", "MLIR IR Bindings"); 94 populateIRCore(irModule); 95 populateIRAffine(irModule); 96 populateIRAttributes(irModule); 97 populateIRInterfaces(irModule); 98 populateIRTypes(irModule); 99 100 // Define and populate PassManager submodule. 101 auto passModule = 102 m.def_submodule("passmanager", "MLIR Pass Management Bindings"); 103 populatePassManagerSubmodule(passModule); 104 } 105