31 assert(!instance &&
"PyGlobals already constructed");
42 nb::ft_lock_guard lock(mutex);
43 if (loadedDialectModules.contains(dialectNamespace))
47 std::vector<std::string> localSearchPrefixes = dialectSearchPrefixes;
48 nb::object loaded = nb::none();
49 for (std::string moduleName : localSearchPrefixes) {
50 moduleName.push_back(
'.');
51 moduleName.append(dialectNamespace.data(), dialectNamespace.size());
54 loaded = nb::module_::import_(moduleName.c_str());
55 }
catch (nb::python_error &e) {
56 if (e.matches(PyExc_ModuleNotFoundError)) {
68 nb::ft_lock_guard lock(mutex);
69 loadedDialectModules.insert(dialectNamespace);
74 nb::callable pyFunc,
bool replace) {
75 nb::ft_lock_guard lock(mutex);
76 nb::object &found = attributeBuilderMap[attributeKind];
77 if (found && !replace) {
78 throw std::runtime_error((llvm::Twine(
"Attribute builder for '") +
80 "' is already registered with func: " +
81 nb::cast<std::string>(nb::str(found)))
84 found = std::move(pyFunc);
88 nb::callable typeCaster,
bool replace) {
89 nb::ft_lock_guard lock(mutex);
90 nb::object &found = typeCasterMap[mlirTypeID];
91 if (found && !replace)
92 throw std::runtime_error(
"Type caster is already registered with caster: " +
93 nb::cast<std::string>(nb::str(found)));
94 found = std::move(typeCaster);
98 nb::callable valueCaster,
bool replace) {
99 nb::ft_lock_guard lock(mutex);
100 nb::object &found = valueCasterMap[mlirTypeID];
101 if (found && !replace)
102 throw std::runtime_error(
"Value caster is already registered: " +
103 nb::cast<std::string>(nb::repr(found)));
104 found = std::move(valueCaster);
108 nb::object pyClass) {
109 nb::ft_lock_guard lock(mutex);
110 nb::object &found = dialectClassMap[dialectNamespace];
112 throw std::runtime_error((llvm::Twine(
"Dialect namespace '") +
113 dialectNamespace +
"' is already registered.")
116 found = std::move(pyClass);
120 nb::object pyClass,
bool replace) {
121 nb::ft_lock_guard lock(mutex);
122 nb::object &found = operationClassMap[operationName];
123 if (found && !replace) {
124 throw std::runtime_error((llvm::Twine(
"Operation '") + operationName +
125 "' is already registered.")
128 found = std::move(pyClass);
131 std::optional<nb::callable>
133 nb::ft_lock_guard lock(mutex);
134 const auto foundIt = attributeBuilderMap.find(attributeKind);
135 if (foundIt != attributeBuilderMap.end()) {
136 assert(foundIt->second &&
"attribute builder is defined");
137 return foundIt->second;
143 MlirDialect dialect) {
146 nb::ft_lock_guard lock(mutex);
147 const auto foundIt = typeCasterMap.find(mlirTypeID);
148 if (foundIt != typeCasterMap.end()) {
149 assert(foundIt->second &&
"type caster is defined");
150 return foundIt->second;
156 MlirDialect dialect) {
159 nb::ft_lock_guard lock(mutex);
160 const auto foundIt = valueCasterMap.find(mlirTypeID);
161 if (foundIt != valueCasterMap.end()) {
162 assert(foundIt->second &&
"value caster is defined");
163 return foundIt->second;
168 std::optional<nb::object>
173 nb::ft_lock_guard lock(mutex);
174 const auto foundIt = dialectClassMap.find(dialectNamespace);
175 if (foundIt != dialectClassMap.end()) {
176 assert(foundIt->second &&
"dialect class is defined");
177 return foundIt->second;
183 std::optional<nb::object>
186 auto split = operationName.split(
'.');
187 llvm::StringRef dialectNamespace = split.first;
191 nb::ft_lock_guard lock(mutex);
192 auto foundIt = operationClassMap.find(operationName);
193 if (foundIt != operationClassMap.end()) {
194 assert(foundIt->second &&
"OpView is defined");
195 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.