LLVMContext.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
  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 implements LLVMContext, as a wrapper around the opaque
  11. // class LLVMContextImpl.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "LLVMContextImpl.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DebugLoc.h"
  18. #include "llvm/IR/DiagnosticInfo.h"
  19. #include "llvm/IR/DiagnosticPrinter.h"
  20. #include "llvm/IR/Instruction.h"
  21. #include "llvm/IR/Metadata.h"
  22. #include "llvm/Support/ManagedStatic.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include <cctype>
  25. using namespace llvm;
  26. static ManagedStatic<LLVMContext> GlobalContext;
  27. LLVMContext& llvm::getGlobalContext() {
  28. return *GlobalContext;
  29. }
  30. LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
  31. std::unique_ptr<LLVMContextImpl> implPtrGuard(pImpl); // HLSL Change: Don't leak if constructor throws.
  32. // Create the fixed metadata kinds. This is done in the same order as the
  33. // MD_* enum values so that they correspond.
  34. // Create the 'dbg' metadata kind.
  35. unsigned DbgID = getMDKindID("dbg");
  36. assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
  37. // Create the 'tbaa' metadata kind.
  38. unsigned TBAAID = getMDKindID("tbaa");
  39. assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
  40. // Create the 'prof' metadata kind.
  41. unsigned ProfID = getMDKindID("prof");
  42. assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
  43. // Create the 'fpmath' metadata kind.
  44. unsigned FPAccuracyID = getMDKindID("fpmath");
  45. assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
  46. (void)FPAccuracyID;
  47. // Create the 'range' metadata kind.
  48. unsigned RangeID = getMDKindID("range");
  49. assert(RangeID == MD_range && "range kind id drifted");
  50. (void)RangeID;
  51. // Create the 'tbaa.struct' metadata kind.
  52. unsigned TBAAStructID = getMDKindID("tbaa.struct");
  53. assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
  54. (void)TBAAStructID;
  55. // Create the 'invariant.load' metadata kind.
  56. unsigned InvariantLdId = getMDKindID("invariant.load");
  57. assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
  58. (void)InvariantLdId;
  59. // Create the 'alias.scope' metadata kind.
  60. unsigned AliasScopeID = getMDKindID("alias.scope");
  61. assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted");
  62. (void)AliasScopeID;
  63. // Create the 'noalias' metadata kind.
  64. unsigned NoAliasID = getMDKindID("noalias");
  65. assert(NoAliasID == MD_noalias && "noalias kind id drifted");
  66. (void)NoAliasID;
  67. // Create the 'nontemporal' metadata kind.
  68. unsigned NonTemporalID = getMDKindID("nontemporal");
  69. assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted");
  70. (void)NonTemporalID;
  71. // Create the 'llvm.mem.parallel_loop_access' metadata kind.
  72. unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access");
  73. assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access &&
  74. "mem_parallel_loop_access kind id drifted");
  75. (void)MemParallelLoopAccessID;
  76. // Create the 'nonnull' metadata kind.
  77. unsigned NonNullID = getMDKindID("nonnull");
  78. assert(NonNullID == MD_nonnull && "nonnull kind id drifted");
  79. (void)NonNullID;
  80. // Create the 'dereferenceable' metadata kind.
  81. unsigned DereferenceableID = getMDKindID("dereferenceable");
  82. assert(DereferenceableID == MD_dereferenceable &&
  83. "dereferenceable kind id drifted");
  84. (void)DereferenceableID;
  85. // Create the 'dereferenceable_or_null' metadata kind.
  86. unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null");
  87. assert(DereferenceableOrNullID == MD_dereferenceable_or_null &&
  88. "dereferenceable_or_null kind id drifted");
  89. (void)DereferenceableOrNullID;
  90. implPtrGuard.release(); // HLSL Change: Destructor now on the hook for destruction
  91. }
  92. LLVMContext::~LLVMContext() { delete pImpl; }
  93. void LLVMContext::addModule(Module *M) {
  94. pImpl->OwnedModules.insert(M);
  95. }
  96. void LLVMContext::removeModule(Module *M) {
  97. pImpl->OwnedModules.erase(M);
  98. }
  99. //===----------------------------------------------------------------------===//
  100. // Recoverable Backend Errors
  101. //===----------------------------------------------------------------------===//
  102. void LLVMContext::
  103. setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
  104. void *DiagContext) {
  105. pImpl->InlineAsmDiagHandler = DiagHandler;
  106. pImpl->InlineAsmDiagContext = DiagContext;
  107. }
  108. /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
  109. /// setInlineAsmDiagnosticHandler.
  110. LLVMContext::InlineAsmDiagHandlerTy
  111. LLVMContext::getInlineAsmDiagnosticHandler() const {
  112. return pImpl->InlineAsmDiagHandler;
  113. }
  114. /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
  115. /// setInlineAsmDiagnosticHandler.
  116. void *LLVMContext::getInlineAsmDiagnosticContext() const {
  117. return pImpl->InlineAsmDiagContext;
  118. }
  119. void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
  120. void *DiagnosticContext,
  121. bool RespectFilters) {
  122. pImpl->DiagnosticHandler = DiagnosticHandler;
  123. pImpl->DiagnosticContext = DiagnosticContext;
  124. pImpl->RespectDiagnosticFilters = RespectFilters;
  125. }
  126. LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
  127. return pImpl->DiagnosticHandler;
  128. }
  129. void *LLVMContext::getDiagnosticContext() const {
  130. return pImpl->DiagnosticContext;
  131. }
  132. void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
  133. {
  134. pImpl->YieldCallback = Callback;
  135. pImpl->YieldOpaqueHandle = OpaqueHandle;
  136. }
  137. void LLVMContext::yield() {
  138. if (pImpl->YieldCallback)
  139. pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
  140. }
  141. void LLVMContext::emitError(const Twine &ErrorStr) {
  142. diagnose(DiagnosticInfoInlineAsm(ErrorStr));
  143. }
  144. // HLSL Change Start
  145. void LLVMContext::emitWarning(const Twine &WarningStr) {
  146. diagnose(DiagnosticInfoInlineAsm(WarningStr, DiagnosticSeverity::DS_Warning));
  147. }
  148. // HLSL Change End
  149. void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
  150. assert (I && "Invalid instruction");
  151. diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
  152. }
  153. static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
  154. // Optimization remarks are selective. They need to check whether the regexp
  155. // pattern, passed via one of the -pass-remarks* flags, matches the name of
  156. // the pass that is emitting the diagnostic. If there is no match, ignore the
  157. // diagnostic and return.
  158. switch (DI.getKind()) {
  159. case llvm::DK_OptimizationRemark:
  160. if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled())
  161. return false;
  162. break;
  163. case llvm::DK_OptimizationRemarkMissed:
  164. if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled())
  165. return false;
  166. break;
  167. case llvm::DK_OptimizationRemarkAnalysis:
  168. if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled())
  169. return false;
  170. break;
  171. default:
  172. break;
  173. }
  174. return true;
  175. }
  176. static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
  177. switch (Severity) {
  178. case DS_Error:
  179. return "error";
  180. case DS_Warning:
  181. return "warning";
  182. case DS_Remark:
  183. return "remark";
  184. case DS_Note:
  185. return "note";
  186. }
  187. llvm_unreachable("Unknown DiagnosticSeverity");
  188. }
  189. void LLVMContext::diagnose(const DiagnosticInfo &DI) {
  190. // If there is a report handler, use it.
  191. if (pImpl->DiagnosticHandler) {
  192. if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI))
  193. pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
  194. return;
  195. }
  196. if (!isDiagnosticEnabled(DI))
  197. return;
  198. // Otherwise, print the message with a prefix based on the severity.
  199. DiagnosticPrinterRawOStream DP(errs());
  200. errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
  201. DI.print(DP);
  202. errs() << "\n";
  203. if (DI.getSeverity() == DS_Error)
  204. // exit(1); // HLSL Change - unwind if necessary, but don't terminate the process
  205. throw std::exception();
  206. }
  207. void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
  208. diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
  209. }
  210. //===----------------------------------------------------------------------===//
  211. // Metadata Kind Uniquing
  212. //===----------------------------------------------------------------------===//
  213. // HLSL Change - Begin
  214. /// Return a unique non-zero ID for the specified metadata kind if it exists.
  215. bool LLVMContext::findMDKindID(StringRef Name, unsigned *ID) const {
  216. auto it = pImpl->CustomMDKindNames.find(Name);
  217. if (it != pImpl->CustomMDKindNames.end()) {
  218. *ID = it->second;
  219. return true;
  220. }
  221. return false;
  222. }
  223. // HLSL Change - End
  224. /// Return a unique non-zero ID for the specified metadata kind.
  225. unsigned LLVMContext::getMDKindID(StringRef Name) const {
  226. // If this is new, assign it its ID.
  227. return pImpl->CustomMDKindNames.insert(
  228. std::make_pair(
  229. Name, pImpl->CustomMDKindNames.size()))
  230. .first->second;
  231. }
  232. /// getHandlerNames - Populate client supplied smallvector using custome
  233. /// metadata name and ID.
  234. void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
  235. Names.resize(pImpl->CustomMDKindNames.size());
  236. for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
  237. E = pImpl->CustomMDKindNames.end(); I != E; ++I)
  238. Names[I->second] = I->first();
  239. }