Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/LLVMContext.h - Class for managing "global" state ---*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares LLVMContext, a container of "global" state in LLVM, such |
| 10 | // as the global type and constant uniquing tables. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_IR_LLVMCONTEXT_H |
| 15 | #define LLVM_IR_LLVMCONTEXT_H |
| 16 | |
| 17 | #include "llvm-c/Types.h" |
| 18 | #include "llvm/IR/DiagnosticHandler.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 OptPassGate; |
| 34 | template <typename T> class SmallVectorImpl; |
| 35 | class SMDiagnostic; |
| 36 | class StringRef; |
| 37 | class Twine; |
| 38 | |
| 39 | namespace yaml { |
| 40 | |
| 41 | class Output; |
| 42 | |
| 43 | } // end namespace yaml |
| 44 | |
| 45 | namespace SyncScope { |
| 46 | |
| 47 | typedef uint8_t ID; |
| 48 | |
| 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. |
| 53 | enum { |
| 54 | /// Synchronized with respect to signal handlers executing in the same thread. |
| 55 | SingleThread = 0, |
| 56 | |
| 57 | /// Synchronized with respect to all concurrently executing threads. |
| 58 | System = 1 |
| 59 | }; |
| 60 | |
| 61 | } // end namespace SyncScope |
| 62 | |
| 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. |
| 68 | class LLVMContext { |
| 69 | public: |
| 70 | LLVMContextImpl *const pImpl; |
| 71 | LLVMContext(); |
| 72 | LLVMContext(LLVMContext &) = delete; |
| 73 | LLVMContext &operator=(const LLVMContext &) = delete; |
| 74 | ~LLVMContext(); |
| 75 | |
| 76 | // Pinned metadata names, which always have the same value. This is a |
| 77 | // compile-time performance optimization, not a correctness optimization. |
| 78 | enum : unsigned { |
| 79 | MD_dbg = 0, // "dbg" |
| 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" |
| 102 | MD_callees = 23, // "callees" |
| 103 | MD_irr_loop = 24, // "irr_loop" |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 104 | MD_access_group = 25, // "llvm.access.group" |
| 105 | MD_callback = 26, // "callback" |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | /// Known operand bundle tag IDs, which always have the same value. All |
| 109 | /// operand bundle tags that LLVM has special knowledge of are listed here. |
| 110 | /// Additionally, this scheme allows LLVM to efficiently check for specific |
| 111 | /// operand bundle tags without comparing strings. |
| 112 | enum : unsigned { |
| 113 | OB_deopt = 0, // "deopt" |
| 114 | OB_funclet = 1, // "funclet" |
| 115 | OB_gc_transition = 2, // "gc-transition" |
| 116 | }; |
| 117 | |
| 118 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 119 | /// This ID is uniqued across modules in the current LLVMContext. |
| 120 | unsigned getMDKindID(StringRef Name) const; |
| 121 | |
| 122 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| 123 | /// custom metadata IDs registered in this LLVMContext. |
| 124 | void getMDKindNames(SmallVectorImpl<StringRef> &Result) const; |
| 125 | |
| 126 | /// getOperandBundleTags - Populate client supplied SmallVector with the |
| 127 | /// bundle tags registered in this LLVMContext. The bundle tags are ordered |
| 128 | /// by increasing bundle IDs. |
| 129 | /// \see LLVMContext::getOperandBundleTagID |
| 130 | void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const; |
| 131 | |
| 132 | /// getOperandBundleTagID - Maps a bundle tag to an integer ID. Every bundle |
| 133 | /// tag registered with an LLVMContext has an unique ID. |
| 134 | uint32_t getOperandBundleTagID(StringRef Tag) const; |
| 135 | |
| 136 | /// getOrInsertSyncScopeID - Maps synchronization scope name to |
| 137 | /// synchronization scope ID. Every synchronization scope registered with |
| 138 | /// LLVMContext has unique ID except pre-defined ones. |
| 139 | SyncScope::ID getOrInsertSyncScopeID(StringRef SSN); |
| 140 | |
| 141 | /// getSyncScopeNames - Populates client supplied SmallVector with |
| 142 | /// synchronization scope names registered with LLVMContext. Synchronization |
| 143 | /// scope names are ordered by increasing synchronization scope IDs. |
| 144 | void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const; |
| 145 | |
| 146 | /// Define the GC for a function |
| 147 | void setGC(const Function &Fn, std::string GCName); |
| 148 | |
| 149 | /// Return the GC for a function |
| 150 | const std::string &getGC(const Function &Fn); |
| 151 | |
| 152 | /// Remove the GC for a function |
| 153 | void deleteGC(const Function &Fn); |
| 154 | |
| 155 | /// Return true if the Context runtime configuration is set to discard all |
| 156 | /// value names. When true, only GlobalValue names will be available in the |
| 157 | /// IR. |
| 158 | bool shouldDiscardValueNames() const; |
| 159 | |
| 160 | /// Set the Context runtime configuration to discard all value name (but |
| 161 | /// GlobalValue). Clients can use this flag to save memory and runtime, |
| 162 | /// especially in release mode. |
| 163 | void setDiscardValueNames(bool Discard); |
| 164 | |
| 165 | /// Whether there is a string map for uniquing debug info |
| 166 | /// identifiers across the context. Off by default. |
| 167 | bool isODRUniquingDebugTypes() const; |
| 168 | void enableDebugTypeODRUniquing(); |
| 169 | void disableDebugTypeODRUniquing(); |
| 170 | |
| 171 | using InlineAsmDiagHandlerTy = void (*)(const SMDiagnostic&, void *Context, |
| 172 | unsigned LocCookie); |
| 173 | |
| 174 | /// Defines the type of a yield callback. |
| 175 | /// \see LLVMContext::setYieldCallback. |
| 176 | using YieldCallbackTy = void (*)(LLVMContext *Context, void *OpaqueHandle); |
| 177 | |
| 178 | /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked |
| 179 | /// when problems with inline asm are detected by the backend. The first |
| 180 | /// argument is a function pointer and the second is a context pointer that |
| 181 | /// gets passed into the DiagHandler. |
| 182 | /// |
| 183 | /// LLVMContext doesn't take ownership or interpret either of these |
| 184 | /// pointers. |
| 185 | void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, |
| 186 | void *DiagContext = nullptr); |
| 187 | |
| 188 | /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by |
| 189 | /// setInlineAsmDiagnosticHandler. |
| 190 | InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const; |
| 191 | |
| 192 | /// getInlineAsmDiagnosticContext - Return the diagnostic context set by |
| 193 | /// setInlineAsmDiagnosticHandler. |
| 194 | void *getInlineAsmDiagnosticContext() const; |
| 195 | |
| 196 | /// setDiagnosticHandlerCallBack - This method sets a handler call back |
| 197 | /// that is invoked when the backend needs to report anything to the user. |
| 198 | /// The first argument is a function pointer and the second is a context pointer |
| 199 | /// that gets passed into the DiagHandler. The third argument should be set to |
| 200 | /// true if the handler only expects enabled diagnostics. |
| 201 | /// |
| 202 | /// LLVMContext doesn't take ownership or interpret either of these |
| 203 | /// pointers. |
| 204 | void setDiagnosticHandlerCallBack( |
| 205 | DiagnosticHandler::DiagnosticHandlerTy DiagHandler, |
| 206 | void *DiagContext = nullptr, bool RespectFilters = false); |
| 207 | |
| 208 | /// setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler |
| 209 | /// to provide custom diagnostic handling. The first argument is unique_ptr of object |
| 210 | /// of type DiagnosticHandler or a derived of that. The third argument should be |
| 211 | /// set to true if the handler only expects enabled diagnostics. |
| 212 | /// |
| 213 | /// Ownership of this pointer is moved to LLVMContextImpl. |
| 214 | void setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH, |
| 215 | bool RespectFilters = false); |
| 216 | |
| 217 | /// getDiagnosticHandlerCallBack - Return the diagnostic handler call back set by |
| 218 | /// setDiagnosticHandlerCallBack. |
| 219 | DiagnosticHandler::DiagnosticHandlerTy getDiagnosticHandlerCallBack() const; |
| 220 | |
| 221 | /// getDiagnosticContext - Return the diagnostic context set by |
| 222 | /// setDiagnosticContext. |
| 223 | void *getDiagnosticContext() const; |
| 224 | |
| 225 | /// getDiagHandlerPtr - Returns const raw pointer of DiagnosticHandler set by |
| 226 | /// setDiagnosticHandler. |
| 227 | const DiagnosticHandler *getDiagHandlerPtr() const; |
| 228 | |
| 229 | /// getDiagnosticHandler - transfers owenership of DiagnosticHandler unique_ptr |
| 230 | /// to caller. |
| 231 | std::unique_ptr<DiagnosticHandler> getDiagnosticHandler(); |
| 232 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 233 | /// Return if a code hotness metric should be included in optimization |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 234 | /// diagnostics. |
| 235 | bool getDiagnosticsHotnessRequested() const; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 236 | /// Set if a code hotness metric should be included in optimization |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 237 | /// diagnostics. |
| 238 | void setDiagnosticsHotnessRequested(bool Requested); |
| 239 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 240 | /// Return the minimum hotness value a diagnostic would need in order |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 241 | /// to be included in optimization diagnostics. If there is no minimum, this |
| 242 | /// returns None. |
| 243 | uint64_t getDiagnosticsHotnessThreshold() const; |
| 244 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 245 | /// Set the minimum hotness value a diagnostic needs in order to be |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 246 | /// included in optimization diagnostics. |
| 247 | void setDiagnosticsHotnessThreshold(uint64_t Threshold); |
| 248 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 249 | /// Return the YAML file used by the backend to save optimization |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 250 | /// diagnostics. If null, diagnostics are not saved in a file but only |
| 251 | /// emitted via the diagnostic handler. |
| 252 | yaml::Output *getDiagnosticsOutputFile(); |
| 253 | /// Set the diagnostics output file used for optimization diagnostics. |
| 254 | /// |
| 255 | /// By default or if invoked with null, diagnostics are not saved in a file |
| 256 | /// but only emitted via the diagnostic handler. Even if an output file is |
| 257 | /// set, the handler is invoked for each diagnostic message. |
| 258 | void setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F); |
| 259 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 260 | /// Get the prefix that should be printed in front of a diagnostic of |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 261 | /// the given \p Severity |
| 262 | static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity); |
| 263 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 264 | /// Report a message to the currently installed diagnostic handler. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 265 | /// |
| 266 | /// This function returns, in particular in the case of error reporting |
| 267 | /// (DI.Severity == \a DS_Error), so the caller should leave the compilation |
| 268 | /// process in a self-consistent state, even though the generated code |
| 269 | /// need not be correct. |
| 270 | /// |
| 271 | /// The diagnostic message will be implicitly prefixed with a severity keyword |
| 272 | /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error, |
| 273 | /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note. |
| 274 | void diagnose(const DiagnosticInfo &DI); |
| 275 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 276 | /// Registers a yield callback with the given context. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 277 | /// |
| 278 | /// The yield callback function may be called by LLVM to transfer control back |
| 279 | /// to the client that invoked the LLVM compilation. This can be used to yield |
| 280 | /// control of the thread, or perform periodic work needed by the client. |
| 281 | /// There is no guaranteed frequency at which callbacks must occur; in fact, |
| 282 | /// the client is not guaranteed to ever receive this callback. It is at the |
| 283 | /// sole discretion of LLVM to do so and only if it can guarantee that |
| 284 | /// suspending the thread won't block any forward progress in other LLVM |
| 285 | /// contexts in the same process. |
| 286 | /// |
| 287 | /// At a suspend point, the state of the current LLVM context is intentionally |
| 288 | /// undefined. No assumptions about it can or should be made. Only LLVM |
| 289 | /// context API calls that explicitly state that they can be used during a |
| 290 | /// yield callback are allowed to be used. Any other API calls into the |
| 291 | /// context are not supported until the yield callback function returns |
| 292 | /// control to LLVM. Other LLVM contexts are unaffected by this restriction. |
| 293 | void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle); |
| 294 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 295 | /// Calls the yield callback (if applicable). |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 296 | /// |
| 297 | /// This transfers control of the current thread back to the client, which may |
| 298 | /// suspend the current thread. Only call this method when LLVM doesn't hold |
| 299 | /// any global mutex or cannot block the execution in another LLVM context. |
| 300 | void yield(); |
| 301 | |
| 302 | /// emitError - Emit an error message to the currently installed error handler |
| 303 | /// with optional location information. This function returns, so code should |
| 304 | /// be prepared to drop the erroneous construct on the floor and "not crash". |
| 305 | /// The generated code need not be correct. The error message will be |
| 306 | /// implicitly prefixed with "error: " and should not end with a ".". |
| 307 | void emitError(unsigned LocCookie, const Twine &ErrorStr); |
| 308 | void emitError(const Instruction *I, const Twine &ErrorStr); |
| 309 | void emitError(const Twine &ErrorStr); |
| 310 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 311 | /// Query for a debug option's value. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 312 | /// |
| 313 | /// This function returns typed data populated from command line parsing. |
| 314 | template <typename ValT, typename Base, ValT(Base::*Mem)> |
| 315 | ValT getOption() const { |
| 316 | return OptionRegistry::instance().template get<ValT, Base, Mem>(); |
| 317 | } |
| 318 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 319 | /// Access the object which can disable optional passes and individual |
| 320 | /// optimizations at compile time. |
| 321 | OptPassGate &getOptPassGate() const; |
| 322 | |
| 323 | /// Set the object which can disable optional passes and individual |
| 324 | /// optimizations at compile time. |
| 325 | /// |
| 326 | /// The lifetime of the object must be guaranteed to extend as long as the |
| 327 | /// LLVMContext is used by compilation. |
| 328 | void setOptPassGate(OptPassGate&); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 329 | |
| 330 | private: |
| 331 | // Module needs access to the add/removeModule methods. |
| 332 | friend class Module; |
| 333 | |
| 334 | /// addModule - Register a module as being instantiated in this context. If |
| 335 | /// the context is deleted, the module will be deleted as well. |
| 336 | void addModule(Module*); |
| 337 | |
| 338 | /// removeModule - Unregister a module from this context. |
| 339 | void removeModule(Module*); |
| 340 | }; |
| 341 | |
| 342 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 343 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef) |
| 344 | |
| 345 | /* Specialized opaque context conversions. |
| 346 | */ |
| 347 | inline LLVMContext **unwrap(LLVMContextRef* Tys) { |
| 348 | return reinterpret_cast<LLVMContext**>(Tys); |
| 349 | } |
| 350 | |
| 351 | inline LLVMContextRef *wrap(const LLVMContext **Tys) { |
| 352 | return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys)); |
| 353 | } |
| 354 | |
| 355 | } // end namespace llvm |
| 356 | |
| 357 | #endif // LLVM_IR_LLVMCONTEXT_H |