MLIR  14.0.0git
MLIRContext.h
Go to the documentation of this file.
1 //===- MLIRContext.h - MLIR Global Context Class ----------------*- C++ -*-===//
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_IR_MLIRCONTEXT_H
10 #define MLIR_IR_MLIRCONTEXT_H
11 
12 #include "mlir/Support/LLVM.h"
13 #include "mlir/Support/TypeID.h"
14 #include <functional>
15 #include <memory>
16 #include <vector>
17 
18 namespace llvm {
19 class ThreadPool;
20 } // namespace llvm
21 
22 namespace mlir {
23 class DebugActionManager;
24 class DiagnosticEngine;
25 class Dialect;
26 class DialectRegistry;
27 class InFlightDiagnostic;
28 class Location;
29 class MLIRContextImpl;
30 class RegisteredOperationName;
31 class StorageUniquer;
32 
33 /// MLIRContext is the top-level object for a collection of MLIR operations. It
34 /// holds immortal uniqued objects like types, and the tables used to unique
35 /// them.
36 ///
37 /// MLIRContext gets a redundant "MLIR" prefix because otherwise it ends up with
38 /// a very generic name ("Context") and because it is uncommon for clients to
39 /// interact with it.
40 ///
41 /// The context wrap some multi-threading facilities, and in particular by
42 /// default it will implicitly create a thread pool.
43 /// This can be undesirable if multiple context exists at the same time or if a
44 /// process will be long-lived and create and destroy contexts.
45 /// To control better thread spawning, an externally owned ThreadPool can be
46 /// injected in the context. For example:
47 ///
48 /// llvm::ThreadPool myThreadPool;
49 /// while (auto *request = nextCompilationRequests()) {
50 /// MLIRContext ctx(registry, MLIRContext::Threading::DISABLED);
51 /// ctx.setThreadPool(myThreadPool);
52 /// processRequest(request, cxt);
53 /// }
54 ///
55 class MLIRContext {
56 public:
57  enum class Threading { DISABLED, ENABLED };
58  /// Create a new Context.
59  explicit MLIRContext(Threading multithreading = Threading::ENABLED);
60  explicit MLIRContext(const DialectRegistry &registry,
61  Threading multithreading = Threading::ENABLED);
62  ~MLIRContext();
63 
64  /// Return information about all IR dialects loaded in the context.
65  std::vector<Dialect *> getLoadedDialects();
66 
67  /// Return the dialect registry associated with this context.
68  const DialectRegistry &getDialectRegistry();
69 
70  /// Append the contents of the given dialect registry to the registry
71  /// associated with this context.
72  void appendDialectRegistry(const DialectRegistry &registry);
73 
74  /// Return information about all available dialects in the registry in this
75  /// context.
76  std::vector<StringRef> getAvailableDialects();
77 
78  /// Get a registered IR dialect with the given namespace. If an exact match is
79  /// not found, then return nullptr.
80  Dialect *getLoadedDialect(StringRef name);
81 
82  /// Get a registered IR dialect for the given derived dialect type. The
83  /// derived type must provide a static 'getDialectNamespace' method.
84  template <typename T>
86  return static_cast<T *>(getLoadedDialect(T::getDialectNamespace()));
87  }
88 
89  /// Get (or create) a dialect for the given derived dialect type. The derived
90  /// type must provide a static 'getDialectNamespace' method.
91  template <typename T>
93  return static_cast<T *>(
94  getOrLoadDialect(T::getDialectNamespace(), TypeID::get<T>(), [this]() {
95  std::unique_ptr<T> dialect(new T(this));
96  return dialect;
97  }));
98  }
99 
100  /// Load a dialect in the context.
101  template <typename Dialect>
102  void loadDialect() {
103  getOrLoadDialect<Dialect>();
104  }
105 
106  /// Load a list dialects in the context.
107  template <typename Dialect, typename OtherDialect, typename... MoreDialects>
108  void loadDialect() {
109  getOrLoadDialect<Dialect>();
110  loadDialect<OtherDialect, MoreDialects...>();
111  }
112 
113  /// Load all dialects available in the registry in this context.
114  void loadAllAvailableDialects();
115 
116  /// Get (or create) a dialect for the given derived dialect name.
117  /// The dialect will be loaded from the registry if no dialect is found.
118  /// If no dialect is loaded for this name and none is available in the
119  /// registry, returns nullptr.
120  Dialect *getOrLoadDialect(StringRef name);
121 
122  /// Return true if we allow to create operation for unregistered dialects.
123  bool allowsUnregisteredDialects();
124 
125  /// Enables creating operations in unregistered dialects.
126  void allowUnregisteredDialects(bool allow = true);
127 
128  /// Return true if multi-threading is enabled by the context.
129  bool isMultithreadingEnabled();
130 
131  /// Set the flag specifying if multi-threading is disabled by the context.
132  /// The command line debugging flag `--mlir-disable-threading` is overriding
133  /// this call and making it a no-op!
134  void disableMultithreading(bool disable = true);
135  void enableMultithreading(bool enable = true) {
136  disableMultithreading(!enable);
137  }
138 
139  /// Set a new thread pool to be used in this context. This method requires
140  /// that multithreading is disabled for this context prior to the call. This
141  /// allows to share a thread pool across multiple contexts, as well as
142  /// decoupling the lifetime of the threads from the contexts. The thread pool
143  /// must outlive the context. Multi-threading will be enabled as part of this
144  /// method.
145  /// The command line debugging flag `--mlir-disable-threading` will still
146  /// prevent threading from being enabled and threading won't be enabled after
147  /// this call in this case.
148  void setThreadPool(llvm::ThreadPool &pool);
149 
150  /// Return the number of threads used by the thread pool in this context. The
151  /// number of computed hardware threads can change over the lifetime of a
152  /// process based on affinity changes, so users should use the number of
153  /// threads actually in the thread pool for dispatching work. Returns 1 if
154  /// multithreading is disabled.
155  unsigned getNumThreads();
156 
157  /// Return the thread pool used by this context. This method requires that
158  /// multithreading be enabled within the context, and should generally not be
159  /// used directly. Users should instead prefer the threading utilities within
160  /// Threading.h.
161  llvm::ThreadPool &getThreadPool();
162 
163  /// Return true if we should attach the operation to diagnostics emitted via
164  /// Operation::emit.
165  bool shouldPrintOpOnDiagnostic();
166 
167  /// Set the flag specifying if we should attach the operation to diagnostics
168  /// emitted via Operation::emit.
169  void printOpOnDiagnostic(bool enable);
170 
171  /// Return true if we should attach the current stacktrace to diagnostics when
172  /// emitted.
173  bool shouldPrintStackTraceOnDiagnostic();
174 
175  /// Set the flag specifying if we should attach the current stacktrace when
176  /// emitting diagnostics.
177  void printStackTraceOnDiagnostic(bool enable);
178 
179  /// Return information about all registered operations. This isn't very
180  /// efficient: typically you should ask the operations about their properties
181  /// directly.
182  std::vector<RegisteredOperationName> getRegisteredOperations();
183 
184  /// Return true if this operation name is registered in this context.
185  bool isOperationRegistered(StringRef name);
186 
187  // This is effectively private given that only MLIRContext.cpp can see the
188  // MLIRContextImpl type.
189  MLIRContextImpl &getImpl() { return *impl; }
190 
191  /// Returns the diagnostic engine for this context.
192  DiagnosticEngine &getDiagEngine();
193 
194  /// Returns the storage uniquer used for creating affine constructs.
195  StorageUniquer &getAffineUniquer();
196 
197  /// Returns the storage uniquer used for constructing type storage instances.
198  /// This should not be used directly.
199  StorageUniquer &getTypeUniquer();
200 
201  /// Returns the storage uniquer used for constructing attribute storage
202  /// instances. This should not be used directly.
203  StorageUniquer &getAttributeUniquer();
204 
205  /// Returns the manager of debug actions within the context.
206  DebugActionManager &getDebugActionManager();
207 
208  /// These APIs are tracking whether the context will be used in a
209  /// multithreading environment: this has no effect other than enabling
210  /// assertions on misuses of some APIs.
211  void enterMultiThreadedExecution();
212  void exitMultiThreadedExecution();
213 
214  /// Get a dialect for the provided namespace and TypeID: abort the program if
215  /// a dialect exist for this namespace with different TypeID. If a dialect has
216  /// not been loaded for this namespace/TypeID yet, use the provided ctor to
217  /// create one on the fly and load it. Returns a pointer to the dialect owned
218  /// by the context.
219  /// The use of this method is in general discouraged in favor of
220  /// 'getOrLoadDialect<DialectClass>()'.
221  Dialect *getOrLoadDialect(StringRef dialectNamespace, TypeID dialectID,
222  function_ref<std::unique_ptr<Dialect>()> ctor);
223 
224  /// Returns a hash of the registry of the context that may be used to give
225  /// a rough indicator of if the state of the context registry has changed. The
226  /// context registry correlates to loaded dialects and their entities
227  /// (attributes, operations, types, etc.).
228  llvm::hash_code getRegistryHash();
229 
230 private:
231  const std::unique_ptr<MLIRContextImpl> impl;
232 
233  MLIRContext(const MLIRContext &) = delete;
234  void operator=(const MLIRContext &) = delete;
235 };
236 
237 //===----------------------------------------------------------------------===//
238 // MLIRContext CommandLine Options
239 //===----------------------------------------------------------------------===//
240 
241 /// Register a set of useful command-line options that can be used to configure
242 /// various flags within the MLIRContext. These flags are used when constructing
243 /// an MLIR context for initialization.
245 
246 } // namespace mlir
247 
248 #endif // MLIR_IR_MLIRCONTEXT_H
Include the generated interface declarations.
This is the implementation of the MLIRContext class, using the pImpl idiom.
void registerMLIRContextCLOptions()
Register a set of useful command-line options that can be used to configure various flags within the ...
Definition: MLIRContext.cpp:86
void loadDialect()
Load a dialect in the context.
Definition: MLIRContext.h:102
void enableMultithreading(bool enable=true)
Definition: MLIRContext.h:135
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:52
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:42
A utility class to get or create instances of "storage classes".
T * getLoadedDialect()
Get a registered IR dialect for the given derived dialect type.
Definition: MLIRContext.h:85
This class is the main interface for diagnostics.
Definition: Diagnostics.h:381
void loadDialect()
Load a list dialects in the context.
Definition: MLIRContext.h:108
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
Definition: MLIRContext.h:92
MLIRContextImpl & getImpl()
Definition: MLIRContext.h:189
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
Definition: Dialect.h:282
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
This class represents manages debug actions, and orchestrates the communication between action querie...
Definition: DebugAction.h:42