1 //===- IRModule.cpp - IR 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 "IRModule.h" 10 #include "Globals.h" 11 #include "PybindUtils.h" 12 13 #include <optional> 14 #include <vector> 15 16 #include "mlir-c/Bindings/Python/Interop.h" 17 #include "mlir-c/Support.h" 18 19 namespace py = pybind11; 20 using namespace mlir; 21 using namespace mlir::python; 22 23 // ----------------------------------------------------------------------------- 24 // PyGlobals 25 // ----------------------------------------------------------------------------- 26 27 PyGlobals *PyGlobals::instance = nullptr; 28 29 PyGlobals::PyGlobals() { 30 assert(!instance && "PyGlobals already constructed"); 31 instance = this; 32 // The default search path include {mlir.}dialects, where {mlir.} is the 33 // package prefix configured at compile time. 34 dialectSearchPrefixes.emplace_back(MAKE_MLIR_PYTHON_QUALNAME("dialects")); 35 } 36 37 PyGlobals::~PyGlobals() { instance = nullptr; } 38 39 void PyGlobals::loadDialectModule(llvm::StringRef dialectNamespace) { 40 if (loadedDialectModulesCache.contains(dialectNamespace)) 41 return; 42 // Since re-entrancy is possible, make a copy of the search prefixes. 43 std::vector<std::string> localSearchPrefixes = dialectSearchPrefixes; 44 py::object loaded; 45 for (std::string moduleName : localSearchPrefixes) { 46 moduleName.push_back('.'); 47 moduleName.append(dialectNamespace.data(), dialectNamespace.size()); 48 49 try { 50 loaded = py::module::import(moduleName.c_str()); 51 } catch (py::error_already_set &e) { 52 if (e.matches(PyExc_ModuleNotFoundError)) { 53 continue; 54 } 55 throw; 56 } 57 break; 58 } 59 60 // Note: Iterator cannot be shared from prior to loading, since re-entrancy 61 // may have occurred, which may do anything. 62 loadedDialectModulesCache.insert(dialectNamespace); 63 } 64 65 void PyGlobals::registerAttributeBuilder(const std::string &attributeKind, 66 py::function pyFunc, bool replace) { 67 py::object &found = attributeBuilderMap[attributeKind]; 68 if (found && !found.is_none() && !replace) { 69 throw std::runtime_error((llvm::Twine("Attribute builder for '") + 70 attributeKind + 71 "' is already registered with func: " + 72 py::str(found).operator std::string()) 73 .str()); 74 } 75 found = std::move(pyFunc); 76 } 77 78 void PyGlobals::registerTypeCaster(MlirTypeID mlirTypeID, 79 pybind11::function typeCaster, 80 bool replace) { 81 pybind11::object &found = typeCasterMap[mlirTypeID]; 82 if (found && !found.is_none() && !replace) 83 throw std::runtime_error("Type caster is already registered"); 84 found = std::move(typeCaster); 85 const auto foundIt = typeCasterMapCache.find(mlirTypeID); 86 if (foundIt != typeCasterMapCache.end() && !foundIt->second.is_none()) { 87 typeCasterMapCache[mlirTypeID] = found; 88 } 89 } 90 91 void PyGlobals::registerDialectImpl(const std::string &dialectNamespace, 92 py::object pyClass) { 93 py::object &found = dialectClassMap[dialectNamespace]; 94 if (found) { 95 throw std::runtime_error((llvm::Twine("Dialect namespace '") + 96 dialectNamespace + "' is already registered.") 97 .str()); 98 } 99 found = std::move(pyClass); 100 } 101 102 void PyGlobals::registerOperationImpl(const std::string &operationName, 103 py::object pyClass, bool replace) { 104 py::object &found = operationClassMap[operationName]; 105 if (found && !replace) { 106 throw std::runtime_error((llvm::Twine("Operation '") + operationName + 107 "' is already registered.") 108 .str()); 109 } 110 found = std::move(pyClass); 111 auto foundIt = operationClassMapCache.find(operationName); 112 if (foundIt != operationClassMapCache.end() && !foundIt->second.is_none()) { 113 operationClassMapCache[operationName] = found; 114 } 115 } 116 117 std::optional<py::function> 118 PyGlobals::lookupAttributeBuilder(const std::string &attributeKind) { 119 // Fast match against the class map first (common case). 120 const auto foundIt = attributeBuilderMap.find(attributeKind); 121 if (foundIt != attributeBuilderMap.end()) { 122 if (foundIt->second.is_none()) 123 return std::nullopt; 124 assert(foundIt->second && "py::function is defined"); 125 return foundIt->second; 126 } 127 128 // Not found and loading did not yield a registration. Negative cache. 129 attributeBuilderMap[attributeKind] = py::none(); 130 return std::nullopt; 131 } 132 133 std::optional<py::function> PyGlobals::lookupTypeCaster(MlirTypeID mlirTypeID, 134 MlirDialect dialect) { 135 { 136 // Fast match against the class map first (common case). 137 const auto foundIt = typeCasterMapCache.find(mlirTypeID); 138 if (foundIt != typeCasterMapCache.end()) { 139 if (foundIt->second.is_none()) 140 return std::nullopt; 141 assert(foundIt->second && "py::function is defined"); 142 return foundIt->second; 143 } 144 } 145 146 // Not found. Load the dialect namespace. 147 loadDialectModule(unwrap(mlirDialectGetNamespace(dialect))); 148 149 // Attempt to find from the canonical map and cache. 150 { 151 const auto foundIt = typeCasterMap.find(mlirTypeID); 152 if (foundIt != typeCasterMap.end()) { 153 if (foundIt->second.is_none()) 154 return std::nullopt; 155 assert(foundIt->second && "py::object is defined"); 156 // Positive cache. 157 typeCasterMapCache[mlirTypeID] = foundIt->second; 158 return foundIt->second; 159 } 160 // Negative cache. 161 typeCasterMap[mlirTypeID] = py::none(); 162 return std::nullopt; 163 } 164 } 165 166 std::optional<py::object> 167 PyGlobals::lookupDialectClass(const std::string &dialectNamespace) { 168 loadDialectModule(dialectNamespace); 169 // Fast match against the class map first (common case). 170 const auto foundIt = dialectClassMap.find(dialectNamespace); 171 if (foundIt != dialectClassMap.end()) { 172 if (foundIt->second.is_none()) 173 return std::nullopt; 174 assert(foundIt->second && "py::object is defined"); 175 return foundIt->second; 176 } 177 178 // Not found and loading did not yield a registration. Negative cache. 179 dialectClassMap[dialectNamespace] = py::none(); 180 return std::nullopt; 181 } 182 183 std::optional<pybind11::object> 184 PyGlobals::lookupOperationClass(llvm::StringRef operationName) { 185 { 186 auto foundIt = operationClassMapCache.find(operationName); 187 if (foundIt != operationClassMapCache.end()) { 188 if (foundIt->second.is_none()) 189 return std::nullopt; 190 assert(foundIt->second && "py::object is defined"); 191 return foundIt->second; 192 } 193 } 194 195 // Not found. Load the dialect namespace. 196 auto split = operationName.split('.'); 197 llvm::StringRef dialectNamespace = split.first; 198 loadDialectModule(dialectNamespace); 199 200 // Attempt to find from the canonical map and cache. 201 { 202 auto foundIt = operationClassMap.find(operationName); 203 if (foundIt != operationClassMap.end()) { 204 if (foundIt->second.is_none()) 205 return std::nullopt; 206 assert(foundIt->second && "py::object is defined"); 207 // Positive cache. 208 operationClassMapCache[operationName] = foundIt->second; 209 return foundIt->second; 210 } 211 // Negative cache. 212 operationClassMap[operationName] = py::none(); 213 return std::nullopt; 214 } 215 } 216 217 void PyGlobals::clearImportCache() { 218 loadedDialectModulesCache.clear(); 219 operationClassMapCache.clear(); 220 typeCasterMapCache.clear(); 221 } 222