LLVMContext.h 8.8 KB

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