1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements LLVMContext, as a wrapper around the opaque
10 // class LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
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/LLVMRemarkStreamer.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Remarks/RemarkStreamer.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
36 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
37 // Create the fixed metadata kinds. This is done in the same order as the
38 // MD_* enum values so that they correspond.
39 std::pair<unsigned, StringRef> MDKinds[] = {
40 #define LLVM_FIXED_MD_KIND(EnumID, Name, Value) {EnumID, Name},
41 #include "llvm/IR/FixedMetadataKinds.def"
42 #undef LLVM_FIXED_MD_KIND
45 for (auto &MDKind : MDKinds) {
46 unsigned ID = getMDKindID(MDKind.second);
47 assert(ID == MDKind.first && "metadata kind id drifted");
51 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt");
52 assert(DeoptEntry->second == LLVMContext::OB_deopt &&
53 "deopt operand bundle id drifted!");
56 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet");
57 assert(FuncletEntry->second == LLVMContext::OB_funclet &&
58 "funclet operand bundle id drifted!");
61 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition");
62 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition &&
63 "gc-transition operand bundle id drifted!");
64 (void)GCTransitionEntry;
66 auto *CFGuardTargetEntry = pImpl->getOrInsertBundleTag("cfguardtarget");
67 assert(CFGuardTargetEntry->second == LLVMContext::OB_cfguardtarget &&
68 "cfguardtarget operand bundle id drifted!");
69 (void)CFGuardTargetEntry;
71 auto *PreallocatedEntry = pImpl->getOrInsertBundleTag("preallocated");
72 assert(PreallocatedEntry->second == LLVMContext::OB_preallocated &&
73 "preallocated operand bundle id drifted!");
74 (void)PreallocatedEntry;
76 auto *GCLiveEntry = pImpl->getOrInsertBundleTag("gc-live");
77 assert(GCLiveEntry->second == LLVMContext::OB_gc_live &&
78 "gc-transition operand bundle id drifted!");
81 SyncScope::ID SingleThreadSSID =
82 pImpl->getOrInsertSyncScopeID("singlethread");
83 assert(SingleThreadSSID == SyncScope::SingleThread &&
84 "singlethread synchronization scope ID drifted!");
85 (void)SingleThreadSSID;
87 SyncScope::ID SystemSSID =
88 pImpl->getOrInsertSyncScopeID("");
89 assert(SystemSSID == SyncScope::System &&
90 "system synchronization scope ID drifted!");
94 LLVMContext::~LLVMContext() { delete pImpl; }
96 void LLVMContext::addModule(Module *M) {
97 pImpl->OwnedModules.insert(M);
100 void LLVMContext::removeModule(Module *M) {
101 pImpl->OwnedModules.erase(M);
104 //===----------------------------------------------------------------------===//
105 // Recoverable Backend Errors
106 //===----------------------------------------------------------------------===//
109 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
111 pImpl->InlineAsmDiagHandler = DiagHandler;
112 pImpl->InlineAsmDiagContext = DiagContext;
115 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
116 /// setInlineAsmDiagnosticHandler.
117 LLVMContext::InlineAsmDiagHandlerTy
118 LLVMContext::getInlineAsmDiagnosticHandler() const {
119 return pImpl->InlineAsmDiagHandler;
122 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
123 /// setInlineAsmDiagnosticHandler.
124 void *LLVMContext::getInlineAsmDiagnosticContext() const {
125 return pImpl->InlineAsmDiagContext;
128 void LLVMContext::setDiagnosticHandlerCallBack(
129 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler,
130 void *DiagnosticContext, bool RespectFilters) {
131 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler;
132 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext;
133 pImpl->RespectDiagnosticFilters = RespectFilters;
136 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH,
137 bool RespectFilters) {
138 pImpl->DiagHandler = std::move(DH);
139 pImpl->RespectDiagnosticFilters = RespectFilters;
142 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) {
143 pImpl->DiagnosticsHotnessRequested = Requested;
145 bool LLVMContext::getDiagnosticsHotnessRequested() const {
146 return pImpl->DiagnosticsHotnessRequested;
149 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) {
150 pImpl->DiagnosticsHotnessThreshold = Threshold;
152 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const {
153 return pImpl->DiagnosticsHotnessThreshold;
156 remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() {
157 return pImpl->MainRemarkStreamer.get();
159 const remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() const {
160 return const_cast<LLVMContext *>(this)->getMainRemarkStreamer();
162 void LLVMContext::setMainRemarkStreamer(
163 std::unique_ptr<remarks::RemarkStreamer> RemarkStreamer) {
164 pImpl->MainRemarkStreamer = std::move(RemarkStreamer);
167 LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() {
168 return pImpl->LLVMRS.get();
170 const LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() const {
171 return const_cast<LLVMContext *>(this)->getLLVMRemarkStreamer();
173 void LLVMContext::setLLVMRemarkStreamer(
174 std::unique_ptr<LLVMRemarkStreamer> RemarkStreamer) {
175 pImpl->LLVMRS = std::move(RemarkStreamer);
178 DiagnosticHandler::DiagnosticHandlerTy
179 LLVMContext::getDiagnosticHandlerCallBack() const {
180 return pImpl->DiagHandler->DiagHandlerCallback;
183 void *LLVMContext::getDiagnosticContext() const {
184 return pImpl->DiagHandler->DiagnosticContext;
187 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
189 pImpl->YieldCallback = Callback;
190 pImpl->YieldOpaqueHandle = OpaqueHandle;
193 void LLVMContext::yield() {
194 if (pImpl->YieldCallback)
195 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
198 void LLVMContext::emitError(const Twine &ErrorStr) {
199 diagnose(DiagnosticInfoInlineAsm(ErrorStr));
202 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
203 assert (I && "Invalid instruction");
204 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
207 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
208 // Optimization remarks are selective. They need to check whether the regexp
209 // pattern, passed via one of the -pass-remarks* flags, matches the name of
210 // the pass that is emitting the diagnostic. If there is no match, ignore the
211 // diagnostic and return.
213 // Also noisy remarks are only enabled if we have hotness information to sort
215 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
216 return Remark->isEnabled() &&
217 (!Remark->isVerbose() || Remark->getHotness());
223 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
234 llvm_unreachable("Unknown DiagnosticSeverity");
237 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
238 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
239 if (LLVMRemarkStreamer *RS = getLLVMRemarkStreamer())
240 RS->emit(*OptDiagBase);
242 // If there is a report handler, use it.
243 if (pImpl->DiagHandler &&
244 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) &&
245 pImpl->DiagHandler->handleDiagnostics(DI))
248 if (!isDiagnosticEnabled(DI))
251 // Otherwise, print the message with a prefix based on the severity.
252 DiagnosticPrinterRawOStream DP(errs());
253 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
256 if (DI.getSeverity() == DS_Error)
260 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
261 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
264 //===----------------------------------------------------------------------===//
265 // Metadata Kind Uniquing
266 //===----------------------------------------------------------------------===//
268 /// Return a unique non-zero ID for the specified metadata kind.
269 unsigned LLVMContext::getMDKindID(StringRef Name) const {
270 // If this is new, assign it its ID.
271 return pImpl->CustomMDKindNames.insert(
273 Name, pImpl->CustomMDKindNames.size()))
277 /// getHandlerNames - Populate client-supplied smallvector using custom
278 /// metadata name and ID.
279 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
280 Names.resize(pImpl->CustomMDKindNames.size());
281 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
282 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
283 Names[I->second] = I->first();
286 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
287 pImpl->getOperandBundleTags(Tags);
290 StringMapEntry<uint32_t> *
291 LLVMContext::getOrInsertBundleTag(StringRef TagName) const {
292 return pImpl->getOrInsertBundleTag(TagName);
295 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const {
296 return pImpl->getOperandBundleTagID(Tag);
299 SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) {
300 return pImpl->getOrInsertSyncScopeID(SSN);
303 void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const {
304 pImpl->getSyncScopeNames(SSNs);
307 void LLVMContext::setGC(const Function &Fn, std::string GCName) {
308 auto It = pImpl->GCNames.find(&Fn);
310 if (It == pImpl->GCNames.end()) {
311 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName)));
314 It->second = std::move(GCName);
317 const std::string &LLVMContext::getGC(const Function &Fn) {
318 return pImpl->GCNames[&Fn];
321 void LLVMContext::deleteGC(const Function &Fn) {
322 pImpl->GCNames.erase(&Fn);
325 bool LLVMContext::shouldDiscardValueNames() const {
326 return pImpl->DiscardValueNames;
329 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; }
331 void LLVMContext::enableDebugTypeODRUniquing() {
332 if (pImpl->DITypeMap)
335 pImpl->DITypeMap.emplace();
338 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); }
340 void LLVMContext::setDiscardValueNames(bool Discard) {
341 pImpl->DiscardValueNames = Discard;
344 OptPassGate &LLVMContext::getOptPassGate() const {
345 return pImpl->getOptPassGate();
348 void LLVMContext::setOptPassGate(OptPassGate& OPG) {
349 pImpl->setOptPassGate(OPG);
352 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const {
353 return pImpl->DiagHandler.get();
356 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() {
357 return std::move(pImpl->DiagHandler);