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