31 assert(!instance &&
"PyGlobals already constructed");
41 if (loadedDialectModules.contains(dialectNamespace))
44 std::vector<std::string> localSearchPrefixes = dialectSearchPrefixes;
45 nb::object loaded = nb::none();
46 for (std::string moduleName : localSearchPrefixes) {
47 moduleName.push_back(
'.');
48 moduleName.append(dialectNamespace.data(), dialectNamespace.size());
51 loaded = nb::module_::import_(moduleName.c_str());
52 }
catch (nb::python_error &e) {
53 if (e.matches(PyExc_ModuleNotFoundError)) {
65 loadedDialectModules.insert(dialectNamespace);
70 nb::callable pyFunc,
bool replace) {
71 nb::object &found = attributeBuilderMap[attributeKind];
72 if (found && !replace) {
73 throw std::runtime_error((llvm::Twine(
"Attribute builder for '") +
75 "' is already registered with func: " +
76 nb::cast<std::string>(nb::str(found)))
79 found = std::move(pyFunc);
83 nb::callable typeCaster,
bool replace) {
84 nb::object &found = typeCasterMap[mlirTypeID];
85 if (found && !replace)
86 throw std::runtime_error(
"Type caster is already registered with caster: " +
87 nb::cast<std::string>(nb::str(found)));
88 found = std::move(typeCaster);
92 nb::callable valueCaster,
bool replace) {
93 nb::object &found = valueCasterMap[mlirTypeID];
94 if (found && !replace)
95 throw std::runtime_error(
"Value caster is already registered: " +
96 nb::cast<std::string>(nb::repr(found)));
97 found = std::move(valueCaster);
101 nb::object pyClass) {
102 nb::object &found = dialectClassMap[dialectNamespace];
104 throw std::runtime_error((llvm::Twine(
"Dialect namespace '") +
105 dialectNamespace +
"' is already registered.")
108 found = std::move(pyClass);
112 nb::object pyClass,
bool replace) {
113 nb::object &found = operationClassMap[operationName];
114 if (found && !replace) {
115 throw std::runtime_error((llvm::Twine(
"Operation '") + operationName +
116 "' is already registered.")
119 found = std::move(pyClass);
122 std::optional<nb::callable>
124 const auto foundIt = attributeBuilderMap.find(attributeKind);
125 if (foundIt != attributeBuilderMap.end()) {
126 assert(foundIt->second &&
"attribute builder is defined");
127 return foundIt->second;
133 MlirDialect dialect) {
136 const auto foundIt = typeCasterMap.find(mlirTypeID);
137 if (foundIt != typeCasterMap.end()) {
138 assert(foundIt->second &&
"type caster is defined");
139 return foundIt->second;
145 MlirDialect dialect) {
148 const auto foundIt = valueCasterMap.find(mlirTypeID);
149 if (foundIt != valueCasterMap.end()) {
150 assert(foundIt->second &&
"value caster is defined");
151 return foundIt->second;
156 std::optional<nb::object>
161 const auto foundIt = dialectClassMap.find(dialectNamespace);
162 if (foundIt != dialectClassMap.end()) {
163 assert(foundIt->second &&
"dialect class is defined");
164 return foundIt->second;
170 std::optional<nb::object>
173 auto split = operationName.split(
'.');
174 llvm::StringRef dialectNamespace = split.first;
178 auto foundIt = operationClassMap.find(operationName);
179 if (foundIt != operationClassMap.end()) {
180 assert(foundIt->second &&
"OpView is defined");
181 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.
void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster, bool replace=false)
Adds a user-friendly type caster.
void registerOperationImpl(const std::string &operationName, nanobind::object pyClass, bool replace=false)
Adds a concrete implementation operation class.
void registerAttributeBuilder(const std::string &attributeKind, nanobind::callable pyFunc, bool replace=false)
Adds a user-friendly Attribute builder.
void registerValueCaster(MlirTypeID mlirTypeID, nanobind::callable valueCaster, bool replace=false)
Adds a user-friendly value caster.
std::optional< nanobind::callable > lookupValueCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom value caster for MlirTypeID mlirTypeID.
std::optional< nanobind::object > lookupDialectClass(const std::string &dialectNamespace)
Looks up a registered dialect class by namespace.
std::optional< nanobind::object > lookupOperationClass(llvm::StringRef operationName)
Looks up a registered operation class (deriving from OpView) by operation name.
std::optional< nanobind::callable > lookupAttributeBuilder(const std::string &attributeKind)
Returns the custom Attribute builder for Attribute kind.
void registerDialectImpl(const std::string &dialectNamespace, nanobind::object pyClass)
Adds a concrete implementation dialect class.
std::optional< nanobind::callable > lookupTypeCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom type 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.