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