30 assert(!instance &&
"PyGlobals already constructed");
40 if (loadedDialectModules.contains(dialectNamespace))
43 std::vector<std::string> localSearchPrefixes = dialectSearchPrefixes;
44 py::object loaded = py::none();
45 for (std::string moduleName : localSearchPrefixes) {
46 moduleName.push_back(
'.');
47 moduleName.append(dialectNamespace.data(), dialectNamespace.size());
50 loaded = py::module::import(moduleName.c_str());
51 }
catch (py::error_already_set &e) {
52 if (e.matches(PyExc_ModuleNotFoundError)) {
64 loadedDialectModules.insert(dialectNamespace);
69 py::function pyFunc,
bool replace) {
70 py::object &found = attributeBuilderMap[attributeKind];
71 if (found && !replace) {
72 throw std::runtime_error((llvm::Twine(
"Attribute builder for '") +
74 "' is already registered with func: " +
75 py::str(found).
operator std::string())
78 found = std::move(pyFunc);
82 pybind11::function typeCaster,
84 pybind11::object &found = typeCasterMap[mlirTypeID];
85 if (found && !replace)
86 throw std::runtime_error(
"Type caster is already registered with caster: " +
87 py::str(found).
operator std::string());
88 found = std::move(typeCaster);
92 pybind11::function valueCaster,
94 pybind11::object &found = valueCasterMap[mlirTypeID];
95 if (found && !replace)
96 throw std::runtime_error(
"Value caster is already registered: " +
97 py::repr(found).cast<std::string>());
98 found = std::move(valueCaster);
102 py::object pyClass) {
103 py::object &found = dialectClassMap[dialectNamespace];
105 throw std::runtime_error((llvm::Twine(
"Dialect namespace '") +
106 dialectNamespace +
"' is already registered.")
109 found = std::move(pyClass);
113 py::object pyClass,
bool replace) {
114 py::object &found = operationClassMap[operationName];
115 if (found && !replace) {
116 throw std::runtime_error((llvm::Twine(
"Operation '") + operationName +
117 "' is already registered.")
120 found = std::move(pyClass);
123 std::optional<py::function>
125 const auto foundIt = attributeBuilderMap.find(attributeKind);
126 if (foundIt != attributeBuilderMap.end()) {
127 assert(foundIt->second &&
"attribute builder is defined");
128 return foundIt->second;
134 MlirDialect dialect) {
137 const auto foundIt = typeCasterMap.find(mlirTypeID);
138 if (foundIt != typeCasterMap.end()) {
139 assert(foundIt->second &&
"type caster is defined");
140 return foundIt->second;
146 MlirDialect dialect) {
149 const auto foundIt = valueCasterMap.find(mlirTypeID);
150 if (foundIt != valueCasterMap.end()) {
151 assert(foundIt->second &&
"value caster is defined");
152 return foundIt->second;
157 std::optional<py::object>
162 const auto foundIt = dialectClassMap.find(dialectNamespace);
163 if (foundIt != dialectClassMap.end()) {
164 assert(foundIt->second &&
"dialect class is defined");
165 return foundIt->second;
171 std::optional<pybind11::object>
174 auto split = operationName.split(
'.');
175 llvm::StringRef dialectNamespace = split.first;
179 auto foundIt = operationClassMap.find(operationName);
180 if (foundIt != operationClassMap.end()) {
181 assert(foundIt->second &&
"OpView is defined");
182 return foundIt->second;
#define MAKE_MLIR_PYTHON_QUALNAME(local)
Globals that are always accessible once the extension has been initialized.
bool loadDialectModule(llvm::StringRef dialectNamespace)
Loads a python module corresponding to the given dialect namespace.
std::optional< pybind11::function > lookupAttributeBuilder(const std::string &attributeKind)
Returns the custom Attribute builder for Attribute kind.
std::optional< pybind11::function > lookupTypeCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom type caster for MlirTypeID mlirTypeID.
void registerAttributeBuilder(const std::string &attributeKind, pybind11::function pyFunc, bool replace=false)
Adds a user-friendly Attribute builder.
std::optional< pybind11::object > lookupDialectClass(const std::string &dialectNamespace)
Looks up a registered dialect class by namespace.
void registerTypeCaster(MlirTypeID mlirTypeID, pybind11::function typeCaster, bool replace=false)
Adds a user-friendly type caster.
void registerValueCaster(MlirTypeID mlirTypeID, pybind11::function valueCaster, bool replace=false)
Adds a user-friendly value caster.
void registerOperationImpl(const std::string &operationName, pybind11::object pyClass, bool replace=false)
Adds a concrete implementation operation class.
void registerDialectImpl(const std::string &dialectNamespace, pybind11::object pyClass)
Adds a concrete implementation dialect class.
std::optional< pybind11::object > lookupOperationClass(llvm::StringRef operationName)
Looks up a registered operation class (deriving from OpView) by operation name.
std::optional< pybind11::function > lookupValueCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom value caster for MlirTypeID mlirTypeID.
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect)
Returns the namespace of the given dialect.
Include the generated interface declarations.