LLVMContext.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Twine.h"
  20. #include "llvm/IR/DiagnosticInfo.h"
  21. #include "llvm/IR/DiagnosticPrinter.h"
  22. #include "llvm/IR/Metadata.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/Support/Casting.h"
  25. #include "llvm/Support/ErrorHandling.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <cassert>
  28. #include <cstdlib>
  29. #include <string>
  30. #include <utility>
  31. using namespace llvm;
  32. LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
  33. // Create the fixed metadata kinds. This is done in the same order as the
  34. // MD_* enum values so that they correspond.
  35. std::pair<unsigned, StringRef> MDKinds[] = {
  36. {MD_dbg, "dbg"},
  37. {MD_tbaa, "tbaa"},
  38. {MD_prof, "prof"},
  39. {MD_fpmath, "fpmath"},
  40. {MD_range, "range"},
  41. {MD_tbaa_struct, "tbaa.struct"},
  42. {MD_invariant_load, "invariant.load"},
  43. {MD_alias_scope, "alias.scope"},
  44. {MD_noalias, "noalias"},
  45. {MD_nontemporal, "nontemporal"},
  46. {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"},
  47. {MD_nonnull, "nonnull"},
  48. {MD_dereferenceable, "dereferenceable"},
  49. {MD_dereferenceable_or_null, "dereferenceable_or_null"},
  50. {MD_make_implicit, "make.implicit"},
  51. {MD_unpredictable, "unpredictable"},
  52. {MD_invariant_group, "invariant.group"},
  53. {MD_align, "align"},
  54. {MD_loop, "llvm.loop"},
  55. {MD_type, "type"},
  56. {MD_section_prefix, "section_prefix"},
  57. {MD_absolute_symbol, "absolute_symbol"},
  58. {MD_associated, "associated"},
  59. {MD_callees, "callees"},
  60. {MD_irr_loop, "irr_loop"},
  61. {MD_access_group, "llvm.access.group"},
  62. };
  63. for (auto &MDKind : MDKinds) {
  64. unsigned ID = getMDKindID(MDKind.second);
  65. assert(ID == MDKind.first && "metadata kind id drifted");
  66. (void)ID;
  67. }
  68. auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
  69. assert(DeoptEntry->second == LLVMContext::OB_deopt &&
  70. "deopt operand bundle id drifted!");
  71. (void)DeoptEntry;
  72. auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
  73. assert(FuncletEntry->second == LLVMContext::OB_funclet &&
  74. "funclet operand bundle id drifted!");
  75. (void)FuncletEntry;
  76. auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
  77. assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
  78. "gc-transition operand bundle id drifted!");
  79. (void)GCTransitionEntry;
  80. SyncScope::ID SingleThreadSSID =
  81. pImpl->getOrInsertSyncScopeID("singlethread");
  82. assert(SingleThreadSSID == SyncScope::SingleThread &&
  83. "singlethread synchronization scope ID drifted!");
  84. (void)SingleThreadSSID;
  85. SyncScope::ID SystemSSID =
  86. pImpl->getOrInsertSyncScopeID("");
  87. assert(SystemSSID == SyncScope::System &&
  88. "system synchronization scope ID drifted!");
  89. (void)SystemSSID;
  90. }
  91. LLVMContext::~LLVMContext() { delete pImpl; }
  92. void LLVMContext::addModule(Module *M) {
  93. pImpl->OwnedModules.insert(M);
  94. }
  95. void LLVMContext::removeModule(Module *M) {
  96. pImpl->OwnedModules.erase(M);
  97. }
  98. //===----------------------------------------------------------------------===//
  99. // Recoverable Backend Errors
  100. //===----------------------------------------------------------------------===//
  101. void LLVMContext::
  102. setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
  103. void *DiagContext) {
  104. pImpl->InlineAsmDiagHandler = DiagHandler;
  105. pImpl->InlineAsmDiagContext = DiagContext;
  106. }
  107. /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
  108. /// setInlineAsmDiagnosticHandler.
  109. LLVMContext::InlineAsmDiagHandlerTy
  110. LLVMContext::getInlineAsmDiagnosticHandler() const {
  111. return pImpl->InlineAsmDiagHandler;
  112. }
  113. /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
  114. /// setInlineAsmDiagnosticHandler.
  115. void *LLVMContext::getInlineAsmDiagnosticContext() const {
  116. return pImpl->InlineAsmDiagContext;
  117. }
  118. void LLVMContext::setDiagnosticHandlerCallBack(
  119. DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler,
  120. void *DiagnosticContext, bool RespectFilters) {
  121. pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
  122. pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
  123. pImpl->RespectDiagnosticFilters = RespectFilters;
  124. }
  125. void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
  126. bool RespectFilters) {
  127. pImpl->DiagHandler = std::move(DH);
  128. pImpl->RespectDiagnosticFilters = RespectFilters;
  129. }
  130. void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
  131. pImpl->DiagnosticsHotnessRequested = Requested;
  132. }
  133. bool LLVMContext::getDiagnosticsHotnessRequested() const {
  134. return pImpl->DiagnosticsHotnessRequested;
  135. }
  136. void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
  137. pImpl->DiagnosticsHotnessThreshold = Threshold;
  138. }
  139. uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
  140. return pImpl->DiagnosticsHotnessThreshold;
  141. }
  142. yaml::Output *LLVMContext::getDiagnosticsOutputFile() {
  143. return pImpl->DiagnosticsOutputFile.get();
  144. }
  145. void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) {
  146. pImpl->DiagnosticsOutputFile = std::move(F);
  147. }
  148. DiagnosticHandler::DiagnosticHandlerTy
  149. LLVMContext::getDiagnosticHandlerCallBack() const {
  150. return pImpl->DiagHandler->DiagHandlerCallback;
  151. }
  152. void *LLVMContext::getDiagnosticContext() const {
  153. return pImpl->DiagHandler->DiagnosticContext;
  154. }
  155. void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
  156. {
  157. pImpl->YieldCallback = Callback;
  158. pImpl->YieldOpaqueHandle = OpaqueHandle;
  159. }
  160. void LLVMContext::yield() {
  161. if (pImpl->YieldCallback)
  162. pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
  163. }
  164. void LLVMContext::emitError(const Twine &ErrorStr) {
  165. diagnose(DiagnosticInfoInlineAsm(ErrorStr));
  166. }
  167. void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
  168. assert (I && "Invalid instruction");
  169. diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
  170. }
  171. static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
  172. // Optimization remarks are selective. They need to check whether the regexp
  173. // pattern, passed via one of the -pass-remarks* flags, matches the name of
  174. // the pass that is emitting the diagnostic. If there is no match, ignore the
  175. // diagnostic and return.
  176. //
  177. // Also noisy remarks are only enabled if we have hotness information to sort
  178. // them.
  179. if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
  180. return Remark->isEnabled() &&
  181. (!Remark->isVerbose() || Remark->getHotness());
  182. return true;
  183. }
  184. const char *
  185. LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
  186. switch (Severity) {
  187. case DS_Error:
  188. return "error";
  189. case DS_Warning:
  190. return "warning";
  191. case DS_Remark:
  192. return "remark";
  193. case DS_Note:
  194. return "note";
  195. }
  196. llvm_unreachable("Unknown DiagnosticSeverity");
  197. }
  198. void LLVMContext::diagnose(const DiagnosticInfo &DI) {
  199. if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) {
  200. yaml::Output *Out = getDiagnosticsOutputFile();
  201. if (Out) {
  202. // For remarks the << operator takes a reference to a pointer.
  203. auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase);
  204. *Out << P;
  205. }
  206. }
  207. // If there is a report handler, use it.
  208. if (pImpl->DiagHandler &&
  209. (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
  210. pImpl->DiagHandler->handleDiagnostics(DI))
  211. return;
  212. if (!isDiagnosticEnabled(DI))
  213. return;
  214. // Otherwise, print the message with a prefix based on the severity.
  215. DiagnosticPrinterRawOStream DP(errs());
  216. errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
  217. DI.print(DP);
  218. errs() << "\n";
  219. if (DI.getSeverity() == DS_Error)
  220. exit(1);
  221. }
  222. void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
  223. diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
  224. }
  225. //===----------------------------------------------------------------------===//
  226. // Metadata Kind Uniquing
  227. //===----------------------------------------------------------------------===//
  228. /// Return a unique non-zero ID for the specified metadata kind.
  229. unsigned LLVMContext::getMDKindID(StringRef Name) const {
  230. // If this is new, assign it its ID.
  231. return pImpl->CustomMDKindNames.insert(
  232. std::make_pair(
  233. Name, pImpl->CustomMDKindNames.size()))
  234. .first->second;
  235. }
  236. /// getHandlerNames - Populate client-supplied smallvector using custom
  237. /// metadata name and ID.
  238. void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
  239. Names.resize(pImpl->CustomMDKindNames.size());
  240. for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
  241. E = pImpl->CustomMDKindNames.end(); I != E; ++I)
  242. Names[I->second] = I->first();
  243. }
  244. void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
  245. pImpl->getOperandBundleTags(Tags);
  246. }
  247. uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
  248. return pImpl->getOperandBundleTagID(Tag);
  249. }
  250. SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
  251. return pImpl->getOrInsertSyncScopeID(SSN);
  252. }
  253. void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
  254. pImpl->getSyncScopeNames(SSNs);
  255. }
  256. void LLVMContext::setGC(const Function &Fn, std::string GCName) {
  257. auto It = pImpl->GCNames.find(&Fn);
  258. if (It == pImpl->GCNames.end()) {
  259. pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
  260. return;
  261. }
  262. It->second = std::move(GCName);
  263. }
  264. const std::string &LLVMContext::getGC(const Function &Fn) {
  265. return pImpl->GCNames[&Fn];
  266. }
  267. void LLVMContext::deleteGC(const Function &Fn) {
  268. pImpl->GCNames.erase(&Fn);
  269. }
  270. bool LLVMContext::shouldDiscardValueNames() const {
  271. return pImpl->DiscardValueNames;
  272. }
  273. bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
  274. void LLVMContext::enableDebugTypeODRUniquing() {
  275. if (pImpl->DITypeMap)
  276. return;
  277. pImpl->DITypeMap.emplace();
  278. }
  279. void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
  280. void LLVMContext::setDiscardValueNames(bool Discard) {
  281. pImpl->DiscardValueNames = Discard;
  282. }
  283. OptPassGate &LLVMContext::getOptPassGate() const {
  284. return pImpl->getOptPassGate();
  285. }
  286. void LLVMContext::setOptPassGate(OptPassGate& OPG) {
  287. pImpl->setOptPassGate(OPG);
  288. }
  289. const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
  290. return pImpl->DiagHandler.get();
  291. }
  292. std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
  293. return std::move(pImpl->DiagHandler);
  294. }