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/Core.h"
19 #include "llvm/Support/CBindingWrapping.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Options.h"
25 class LLVMContextImpl;
32 template <typename T> class SmallVectorImpl;
36 /// This is an important class for using LLVM in a threaded context. It
37 /// (opaquely) owns and manages the core "global" data of LLVM's core
38 /// infrastructure, including the type and constant uniquing tables.
39 /// LLVMContext itself provides no locking guarantees, so you should be careful
40 /// to have one context per thread.
43 LLVMContextImpl *const pImpl;
47 // Pinned metadata names, which always have the same value. This is a
48 // compile-time performance optimization, not a correctness optimization.
51 MD_tbaa = 1, // "tbaa"
52 MD_prof = 2, // "prof"
53 MD_fpmath = 3, // "fpmath"
54 MD_range = 4, // "range"
55 MD_tbaa_struct = 5, // "tbaa.struct"
56 MD_invariant_load = 6, // "invariant.load"
57 MD_alias_scope = 7, // "alias.scope"
58 MD_noalias = 8, // "noalias",
59 MD_nontemporal = 9, // "nontemporal"
60 MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
61 MD_nonnull = 11, // "nonnull"
62 MD_dereferenceable = 12, // "dereferenceable"
63 MD_dereferenceable_or_null = 13, // "dereferenceable_or_null"
64 MD_make_implicit = 14, // "make.implicit"
65 MD_unpredictable = 15, // "unpredictable"
66 MD_invariant_group = 16, // "invariant.group"
67 MD_align = 17 // "align"
70 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
71 /// This ID is uniqued across modules in the current LLVMContext.
72 unsigned getMDKindID(StringRef Name) const;
74 /// getMDKindNames - Populate client supplied SmallVector with the name for
75 /// custom metadata IDs registered in this LLVMContext.
76 void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
78 /// getOperandBundleTags - Populate client supplied SmallVector with the
79 /// bundle tags registered in this LLVMContext. The bundle tags are ordered
80 /// by increasing bundle IDs.
81 /// \see LLVMContext::getOperandBundleTagID
82 void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
84 /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle
85 /// tag registered with an LLVMContext has an unique ID.
86 uint32_t getOperandBundleTagID(StringRef Tag) const;
88 typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
91 /// Defines the type of a diagnostic handler.
92 /// \see LLVMContext::setDiagnosticHandler.
93 /// \see LLVMContext::diagnose.
94 typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
96 /// Defines the type of a yield callback.
97 /// \see LLVMContext::setYieldCallback.
98 typedef void (*YieldCallbackTy)(LLVMContext *Context, void *OpaqueHandle);
100 /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
101 /// when problems with inline asm are detected by the backend. The first
102 /// argument is a function pointer and the second is a context pointer that
103 /// gets passed into the DiagHandler.
105 /// LLVMContext doesn't take ownership or interpret either of these
107 void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
108 void *DiagContext = nullptr);
110 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
111 /// setInlineAsmDiagnosticHandler.
112 InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
114 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
115 /// setInlineAsmDiagnosticHandler.
116 void *getInlineAsmDiagnosticContext() const;
118 /// setDiagnosticHandler - This method sets a handler that is invoked
119 /// when the backend needs to report anything to the user. The first
120 /// argument is a function pointer and the second is a context pointer that
121 /// gets passed into the DiagHandler. The third argument should be set to
122 /// true if the handler only expects enabled diagnostics.
124 /// LLVMContext doesn't take ownership or interpret either of these
126 void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
127 void *DiagContext = nullptr,
128 bool RespectFilters = false);
130 /// getDiagnosticHandler - Return the diagnostic handler set by
131 /// setDiagnosticHandler.
132 DiagnosticHandlerTy getDiagnosticHandler() const;
134 /// getDiagnosticContext - Return the diagnostic context set by
135 /// setDiagnosticContext.
136 void *getDiagnosticContext() const;
138 /// \brief Report a message to the currently installed diagnostic handler.
140 /// This function returns, in particular in the case of error reporting
141 /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
142 /// process in a self-consistent state, even though the generated code
143 /// need not be correct.
145 /// The diagnostic message will be implicitly prefixed with a severity keyword
146 /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
147 /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
148 void diagnose(const DiagnosticInfo &DI);
150 /// \brief Registers a yield callback with the given context.
152 /// The yield callback function may be called by LLVM to transfer control back
153 /// to the client that invoked the LLVM compilation. This can be used to yield
154 /// control of the thread, or perform periodic work needed by the client.
155 /// There is no guaranteed frequency at which callbacks must occur; in fact,
156 /// the client is not guaranteed to ever receive this callback. It is at the
157 /// sole discretion of LLVM to do so and only if it can guarantee that
158 /// suspending the thread won't block any forward progress in other LLVM
159 /// contexts in the same process.
161 /// At a suspend point, the state of the current LLVM context is intentionally
162 /// undefined. No assumptions about it can or should be made. Only LLVM
163 /// context API calls that explicitly state that they can be used during a
164 /// yield callback are allowed to be used. Any other API calls into the
165 /// context are not supported until the yield callback function returns
166 /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
167 void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
169 /// \brief Calls the yield callback (if applicable).
171 /// This transfers control of the current thread back to the client, which may
172 /// suspend the current thread. Only call this method when LLVM doesn't hold
173 /// any global mutex or cannot block the execution in another LLVM context.
176 /// emitError - Emit an error message to the currently installed error handler
177 /// with optional location information. This function returns, so code should
178 /// be prepared to drop the erroneous construct on the floor and "not crash".
179 /// The generated code need not be correct. The error message will be
180 /// implicitly prefixed with "error: " and should not end with a ".".
181 void emitError(unsigned LocCookie, const Twine &ErrorStr);
182 void emitError(const Instruction *I, const Twine &ErrorStr);
183 void emitError(const Twine &ErrorStr);
185 /// \brief Query for a debug option's value.
187 /// This function returns typed data populated from command line parsing.
188 template <typename ValT, typename Base, ValT(Base::*Mem)>
189 ValT getOption() const {
190 return OptionRegistry::instance().template get<ValT, Base, Mem>();
194 LLVMContext(LLVMContext&) = delete;
195 void operator=(LLVMContext&) = delete;
197 /// addModule - Register a module as being instantiated in this context. If
198 /// the context is deleted, the module will be deleted as well.
199 void addModule(Module*);
201 /// removeModule - Unregister a module from this context.
202 void removeModule(Module*);
204 // Module needs access to the add/removeModule methods.
208 /// getGlobalContext - Returns a global context. This is for LLVM clients that
209 /// only care about operating on a single thread.
210 extern LLVMContext &getGlobalContext();
212 // Create wrappers for C Binding types (see CBindingWrapping.h).
213 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
215 /* Specialized opaque context conversions.
217 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
218 return reinterpret_cast<LLVMContext**>(Tys);
221 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
222 return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));