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