MLIR 23.0.0git
Globals.h
Go to the documentation of this file.
1//===- Globals.h - MLIR Python extension globals --------------------------===//
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#ifndef MLIR_BINDINGS_PYTHON_GLOBALS_H
10#define MLIR_BINDINGS_PYTHON_GLOBALS_H
11
12#include <optional>
13#include <regex>
14#include <string>
15#include <string_view>
16#include <unordered_map>
17#include <unordered_set>
18#include <vector>
19
20#include "mlir-c/IR.h"
21#include "mlir-c/Support.h"
23
24namespace mlir {
25namespace python {
27/// Globals that are always accessible once the extension has been initialized.
28/// Methods of this class are thread-safe.
30public:
31 PyGlobals();
32 ~PyGlobals();
33
34 /// Most code should get the globals via this static accessor.
35 static PyGlobals &get();
36
37 /// Get and set the list of parent modules to search for dialect
38 /// implementation classes.
39 std::vector<std::string> getDialectSearchPrefixes() {
40 nanobind::ft_lock_guard lock(mutex);
41 return dialectSearchPrefixes;
42 }
43 void setDialectSearchPrefixes(std::vector<std::string> newValues) {
44 nanobind::ft_lock_guard lock(mutex);
45 dialectSearchPrefixes.swap(newValues);
46 }
47 void addDialectSearchPrefix(std::string value) {
48 nanobind::ft_lock_guard lock(mutex);
49 dialectSearchPrefixes.push_back(std::move(value));
50 }
51
52 /// Loads a python module corresponding to the given dialect namespace.
53 /// No-ops if the module has already been loaded or is not found. Raises
54 /// an error on any evaluation issues.
55 /// Note that this returns void because it is expected that the module
56 /// contains calls to decorators and helpers that register the salient
57 /// entities. Returns true if dialect is successfully loaded.
58 bool loadDialectModule(std::string_view dialectNamespace);
59
60 /// Adds a user-friendly Attribute builder.
61 /// Raises an exception if the mapping already exists and replace == false.
62 /// This is intended to be called by implementation code.
63 void registerAttributeBuilder(const std::string &attributeKind,
64 nanobind::callable pyFunc,
65 bool replace = false);
66
67 /// Adds a user-friendly type caster. Raises an exception if the mapping
68 /// already exists and replace == false. This is intended to be called by
69 /// implementation code.
70 void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster,
71 bool replace = false);
72
73 /// Adds a user-friendly value caster. Raises an exception if the mapping
74 /// already exists and replace == false. This is intended to be called by
75 /// implementation code.
76 void registerValueCaster(MlirTypeID mlirTypeID,
77 nanobind::callable valueCaster,
78 bool replace = false);
79
80 /// Adds a concrete implementation dialect class.
81 /// Raises an exception if the mapping already exists.
82 /// This is intended to be called by implementation code.
83 void registerDialectImpl(const std::string &dialectNamespace,
84 nanobind::object pyClass);
85
86 /// Adds a concrete implementation operation class.
87 /// Raises an exception if the mapping already exists and replace == false.
88 /// This is intended to be called by implementation code.
89 void registerOperationImpl(const std::string &operationName,
90 nanobind::object pyClass, bool replace = false);
91
92 /// Adds an operation adaptor class.
93 /// Raises an exception if the mapping already exists and replace == false.
94 /// This is intended to be called by implementation code.
95 void registerOpAdaptorImpl(const std::string &operationName,
96 nanobind::object pyClass, bool replace = false);
97
98 /// Returns the custom Attribute builder for Attribute kind.
99 std::optional<nanobind::callable>
100 lookupAttributeBuilder(const std::string &attributeKind);
101
102 /// Returns the custom type caster for MlirTypeID mlirTypeID.
103 std::optional<nanobind::callable> lookupTypeCaster(MlirTypeID mlirTypeID,
104 MlirDialect dialect);
105
106 /// Returns the custom value caster for MlirTypeID mlirTypeID.
107 std::optional<nanobind::callable> lookupValueCaster(MlirTypeID mlirTypeID,
108 MlirDialect dialect);
109
110 /// Looks up a registered dialect class by namespace. Note that this may
111 /// trigger loading of the defining module and can arbitrarily re-enter.
112 std::optional<nanobind::object>
113 lookupDialectClass(const std::string &dialectNamespace);
114
115 /// Looks up a registered operation class (deriving from OpView) by operation
116 /// name. Note that this may trigger a load of the dialect, which can
117 /// arbitrarily re-enter.
118 std::optional<nanobind::object>
119 lookupOperationClass(std::string_view operationName);
120
121 /// Looks up a registered operation adaptor class by operation
122 /// name. Note that this may trigger a load of the dialect, which can
123 /// arbitrarily re-enter.
124 std::optional<nanobind::object>
125 lookupOpAdaptorClass(std::string_view operationName);
126
128 public:
130
131 void setLocTracebacksEnabled(bool value);
132
134
135 void setLocTracebackFramesLimit(size_t value);
136
137 void registerTracebackFileInclusion(const std::string &file);
138
139 void registerTracebackFileExclusion(const std::string &file);
140
141 bool isUserTracebackFilename(std::string_view file);
142
143 static constexpr size_t kMaxFrames = 512;
144
145 private:
146 nanobind::ft_mutex mutex;
147 bool locTracebackEnabled_ = false;
148 size_t locTracebackFramesLimit_ = 10;
149 std::unordered_set<std::string> userTracebackIncludeFiles;
150 std::unordered_set<std::string> userTracebackExcludeFiles;
151 std::regex userTracebackIncludeRegex;
152 bool rebuildUserTracebackIncludeRegex = false;
153 std::regex userTracebackExcludeRegex;
154 bool rebuildUserTracebackExcludeRegex = false;
155 std::unordered_map<std::string, bool> isUserTracebackFilenameCache;
156 };
157
158 TracebackLoc &getTracebackLoc() { return tracebackLoc; }
159
161 public:
164 if (allocator.ptr)
166 }
168 TypeIDAllocator(TypeIDAllocator &&other) : allocator(other.allocator) {
169 other.allocator.ptr = nullptr;
170 }
171
172 MlirTypeIDAllocator get() { return allocator; }
173 MlirTypeID allocate() {
174 return mlirTypeIDAllocatorAllocateTypeID(allocator);
175 }
176
177 private:
178 MlirTypeIDAllocator allocator;
179 };
180
181 MlirTypeID allocateTypeID() { return typeIDAllocator.allocate(); }
182
183private:
184 static PyGlobals *instance;
185
186 nanobind::ft_mutex mutex;
187
188 /// Module name prefixes to search under for dialect implementation modules.
189 std::vector<std::string> dialectSearchPrefixes;
190 /// Map of dialect namespace to external dialect class object.
191 std::unordered_map<std::string, nanobind::object> dialectClassMap;
192 /// Map of full operation name to external operation class object.
193 std::unordered_map<std::string, nanobind::object> operationClassMap;
194 /// Map of full operation name to external operation adaptor class object.
195 std::unordered_map<std::string, nanobind::object> opAdaptorClassMap;
196 /// Map of attribute ODS name to custom builder.
197 std::unordered_map<std::string, nanobind::callable> attributeBuilderMap;
198 /// Map of MlirTypeID to custom type caster.
199 std::unordered_map<MlirTypeID, nanobind::callable, MlirTypeIDHash,
201 typeCasterMap;
202 /// Map of MlirTypeID to custom value caster.
203 std::unordered_map<MlirTypeID, nanobind::callable, MlirTypeIDHash,
205 valueCasterMap;
206 /// Set of dialect namespaces that we have attempted to import implementation
207 /// modules for.
208 std::unordered_set<std::string> loadedDialectModules;
209
210 TracebackLoc tracebackLoc;
211 TypeIDAllocator typeIDAllocator;
212};
213} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
214} // namespace python
215} // namespace mlir
216
217#endif // MLIR_BINDINGS_PYTHON_GLOBALS_H
This class provides a way to define new TypeIDs at runtime.
Definition TypeID.h:349
Globals that are always accessible once the extension has been initialized.
Definition Globals.h:29
static PyGlobals & get()
Most code should get the globals via this static accessor.
Definition Globals.cpp:59
void setDialectSearchPrefixes(std::vector< std::string > newValues)
Definition Globals.h:43
std::vector< std::string > getDialectSearchPrefixes()
Get and set the list of parent modules to search for dialect implementation classes.
Definition Globals.h:39
MLIR_CAPI_EXPORTED void mlirTypeIDAllocatorDestroy(MlirTypeIDAllocator allocator)
Deallocates the allocator and all allocated type ids.
Definition Support.cpp:105
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeIDAllocatorAllocateTypeID(MlirTypeIDAllocator allocator)
Allocates a type id that is valid for the lifetime of the allocator.
Definition Support.cpp:109
MLIR_CAPI_EXPORTED MlirTypeIDAllocator mlirTypeIDAllocatorCreate(void)
Creates a type id allocator for dynamic type id creation.
Definition Support.cpp:101
#define MLIR_PYTHON_API_EXPORTED
Definition Support.h:49
Include the generated interface declarations.