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