1 //===- llvm/LLVMContext.h - Class for managing "global" state ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares LLVMContext, a container of "global" state in LLVM, such
11 // as the global type and constant uniquing tables.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_LLVMCONTEXT_H
16 #define LLVM_IR_LLVMCONTEXT_H
18 #include "llvm-c/Types.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Options.h"
28 enum DiagnosticSeverity : char;
31 class LLVMContextImpl;
34 template <typename T> class SmallVectorImpl;
43 } // end namespace yaml
49 /// Known synchronization scope IDs, which always have the same value. All
50 /// synchronization scope IDs that LLVM has special knowledge of are listed
51 /// here. Additionally, this scheme allows LLVM to efficiently check for
52 /// specific synchronization scope ID without comparing strings.
54 /// Synchronized with respect to signal handlers executing in the same thread.
57 /// Synchronized with respect to all concurrently executing threads.
61 } // end namespace SyncScope
63 /// This is an important class for using LLVM in a threaded context. It
64 /// (opaquely) owns and manages the core "global" data of LLVM's core
65 /// infrastructure, including the type and constant uniquing tables.
66 /// LLVMContext itself provides no locking guarantees, so you should be careful
67 /// to have one context per thread.
70 LLVMContextImpl *const pImpl;
72 LLVMContext(LLVMContext &) = delete;
73 LLVMContext &operator=(const LLVMContext &) = delete;
76 // Pinned metadata names, which always have the same value. This is a
77 // compile-time performance optimization, not a correctness optimization.
80 MD_tbaa = 1, // "tbaa"
81 MD_prof = 2, // "prof"
82 MD_fpmath = 3, // "fpmath"
83 MD_range = 4, // "range"
84 MD_tbaa_struct = 5, // "tbaa.struct"
85 MD_invariant_load = 6, // "invariant.load"
86 MD_alias_scope = 7, // "alias.scope"
87 MD_noalias = 8, // "noalias",
88 MD_nontemporal = 9, // "nontemporal"
89 MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
90 MD_nonnull = 11, // "nonnull"
91 MD_dereferenceable = 12, // "dereferenceable"
92 MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
93 MD_make_implicit = 14, // "make.implicit"
94 MD_unpredictable = 15, // "unpredictable"
95 MD_invariant_group = 16, // "invariant.group"
96 MD_align = 17, // "align"
97 MD_loop = 18, // "llvm.loop"
98 MD_type = 19, // "type"
99 MD_section_prefix = 20, // "section_prefix"
100 MD_absolute_symbol = 21, // "absolute_symbol"
101 MD_associated = 22, // "associated"
104 /// Known operand bundle tag IDs, which always have the same value. All
105 /// operand bundle tags that LLVM has special knowledge of are listed here.
106 /// Additionally, this scheme allows LLVM to efficiently check for specific
107 /// operand bundle tags without comparing strings.
109 OB_deopt = 0, // "deopt"
110 OB_funclet = 1, // "funclet"
111 OB_gc_transition = 2, // "gc-transition"
114 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
115 /// This ID is uniqued across modules in the current LLVMContext.
116 unsigned getMDKindID(StringRef Name) const;
118 /// getMDKindNames - Populate client supplied SmallVector with the name for
119 /// custom metadata IDs registered in this LLVMContext.
120 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
122 /// getOperandBundleTags - Populate client supplied SmallVector with the
123 /// bundle tags registered in this LLVMContext. The bundle tags are ordered
124 /// by increasing bundle IDs.
125 /// \see LLVMContext::getOperandBundleTagID
126 void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
128 /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle
129 /// tag registered with an LLVMContext has an unique ID.
130 uint32_t getOperandBundleTagID(StringRef Tag) const;
132 /// getOrInsertSyncScopeID - Maps synchronization scope name to
133 /// synchronization scope ID. Every synchronization scope registered with
134 /// LLVMContext has unique ID except pre-defined ones.
135 SyncScope::ID getOrInsertSyncScopeID(StringRef SSN);
137 /// getSyncScopeNames - Populates client supplied SmallVector with
138 /// synchronization scope names registered with LLVMContext. Synchronization
139 /// scope names are ordered by increasing synchronization scope IDs.
140 void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const;
142 /// Define the GC for a function
143 void setGC(const Function &Fn, std::string GCName);
145 /// Return the GC for a function
146 const std::string &getGC(const Function &Fn);
148 /// Remove the GC for a function
149 void deleteGC(const Function &Fn);
151 /// Return true if the Context runtime configuration is set to discard all
152 /// value names. When true, only GlobalValue names will be available in the
154 bool shouldDiscardValueNames() const;
156 /// Set the Context runtime configuration to discard all value name (but
157 /// GlobalValue). Clients can use this flag to save memory and runtime,
158 /// especially in release mode.
159 void setDiscardValueNames(bool Discard);
161 /// Whether there is a string map for uniquing debug info
162 /// identifiers across the context. Off by default.
163 bool isODRUniquingDebugTypes() const;
164 void enableDebugTypeODRUniquing();
165 void disableDebugTypeODRUniquing();
167 using InlineAsmDiagHandlerTy = void (*)(const SMDiagnostic&, void *Context,
170 /// Defines the type of a diagnostic handler.
171 /// \see LLVMContext::setDiagnosticHandler.
172 /// \see LLVMContext::diagnose.
173 using DiagnosticHandlerTy = void (*)(const DiagnosticInfo &DI, void *Context);
175 /// Defines the type of a yield callback.
176 /// \see LLVMContext::setYieldCallback.
177 using YieldCallbackTy = void (*)(LLVMContext *Context, void *OpaqueHandle);
179 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
180 /// when problems with inline asm are detected by the backend. The first
181 /// argument is a function pointer and the second is a context pointer that
182 /// gets passed into the DiagHandler.
184 /// LLVMContext doesn't take ownership or interpret either of these
186 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
187 void *DiagContext = nullptr);
189 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
190 /// setInlineAsmDiagnosticHandler.
191 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
193 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
194 /// setInlineAsmDiagnosticHandler.
195 void *getInlineAsmDiagnosticContext() const;
197 /// setDiagnosticHandler - This method sets a handler that is invoked
198 /// when the backend needs to report anything to the user. The first
199 /// argument is a function pointer and the second is a context pointer that
200 /// gets passed into the DiagHandler. The third argument should be set to
201 /// true if the handler only expects enabled diagnostics.
203 /// LLVMContext doesn't take ownership or interpret either of these
205 void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
206 void *DiagContext = nullptr,
207 bool RespectFilters = false);
209 /// getDiagnosticHandler - Return the diagnostic handler set by
210 /// setDiagnosticHandler.
211 DiagnosticHandlerTy getDiagnosticHandler() const;
213 /// getDiagnosticContext - Return the diagnostic context set by
214 /// setDiagnosticContext.
215 void *getDiagnosticContext() const;
217 /// \brief Return if a code hotness metric should be included in optimization
219 bool getDiagnosticsHotnessRequested() const;
220 /// \brief Set if a code hotness metric should be included in optimization
222 void setDiagnosticsHotnessRequested(bool Requested);
224 /// \brief Return the minimum hotness value a diagnostic would need in order
225 /// to be included in optimization diagnostics. If there is no minimum, this
227 uint64_t getDiagnosticsHotnessThreshold() const;
229 /// \brief Set the minimum hotness value a diagnostic needs in order to be
230 /// included in optimization diagnostics.
231 void setDiagnosticsHotnessThreshold(uint64_t Threshold);
233 /// \brief Return the YAML file used by the backend to save optimization
234 /// diagnostics. If null, diagnostics are not saved in a file but only
235 /// emitted via the diagnostic handler.
236 yaml::Output *getDiagnosticsOutputFile();
237 /// Set the diagnostics output file used for optimization diagnostics.
239 /// By default or if invoked with null, diagnostics are not saved in a file
240 /// but only emitted via the diagnostic handler. Even if an output file is
241 /// set, the handler is invoked for each diagnostic message.
242 void setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F);
244 /// \brief Get the prefix that should be printed in front of a diagnostic of
245 /// the given \p Severity
246 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity);
248 /// \brief Report a message to the currently installed diagnostic handler.
250 /// This function returns, in particular in the case of error reporting
251 /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
252 /// process in a self-consistent state, even though the generated code
253 /// need not be correct.
255 /// The diagnostic message will be implicitly prefixed with a severity keyword
256 /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
257 /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
258 void diagnose(const DiagnosticInfo &DI);
260 /// \brief Registers a yield callback with the given context.
262 /// The yield callback function may be called by LLVM to transfer control back
263 /// to the client that invoked the LLVM compilation. This can be used to yield
264 /// control of the thread, or perform periodic work needed by the client.
265 /// There is no guaranteed frequency at which callbacks must occur; in fact,
266 /// the client is not guaranteed to ever receive this callback. It is at the
267 /// sole discretion of LLVM to do so and only if it can guarantee that
268 /// suspending the thread won't block any forward progress in other LLVM
269 /// contexts in the same process.
271 /// At a suspend point, the state of the current LLVM context is intentionally
272 /// undefined. No assumptions about it can or should be made. Only LLVM
273 /// context API calls that explicitly state that they can be used during a
274 /// yield callback are allowed to be used. Any other API calls into the
275 /// context are not supported until the yield callback function returns
276 /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
277 void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
279 /// \brief Calls the yield callback (if applicable).
281 /// This transfers control of the current thread back to the client, which may
282 /// suspend the current thread. Only call this method when LLVM doesn't hold
283 /// any global mutex or cannot block the execution in another LLVM context.
286 /// emitError - Emit an error message to the currently installed error handler
287 /// with optional location information. This function returns, so code should
288 /// be prepared to drop the erroneous construct on the floor and "not crash".
289 /// The generated code need not be correct. The error message will be
290 /// implicitly prefixed with "error: " and should not end with a ".".
291 void emitError(unsigned LocCookie, const Twine &ErrorStr);
292 void emitError(const Instruction *I, const Twine &ErrorStr);
293 void emitError(const Twine &ErrorStr);
295 /// \brief Query for a debug option's value.
297 /// This function returns typed data populated from command line parsing.
298 template <typename ValT, typename Base, ValT(Base::*Mem)>
299 ValT getOption() const {
300 return OptionRegistry::instance().template get<ValT, Base, Mem>();
303 /// \brief Access the object which manages optimization bisection for failure
305 OptBisect &getOptBisect();
307 // Module needs access to the add/removeModule methods.
310 /// addModule - Register a module as being instantiated in this context. If
311 /// the context is deleted, the module will be deleted as well.
312 void addModule(Module*);
314 /// removeModule - Unregister a module from this context.
315 void removeModule(Module*);
318 // Create wrappers for C Binding types (see CBindingWrapping.h).
319 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
321 /* Specialized opaque context conversions.
323 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
324 return reinterpret_cast<LLVMContext**>(Tys);
327 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
328 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
331 } // end namespace llvm
333 #endif // LLVM_IR_LLVMCONTEXT_H