1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements LLVMContext, as a wrapper around the opaque 10 // class LLVMContextImpl. 11 // 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/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" 29 #include <cassert> 30 #include <cstdlib> 31 #include <string> 32 #include <utility> 33 34 using namespace llvm; 35 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 43 }; 44 45 for (auto &MDKind : MDKinds) { 46 unsigned ID = getMDKindID(MDKind.second); 47 assert(ID == MDKind.first && "metadata kind id drifted"); 48 (void)ID; 49 } 50 51 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt"); 52 assert(DeoptEntry->second == LLVMContext::OB_deopt && 53 "deopt operand bundle id drifted!"); 54 (void)DeoptEntry; 55 56 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet"); 57 assert(FuncletEntry->second == LLVMContext::OB_funclet && 58 "funclet operand bundle id drifted!"); 59 (void)FuncletEntry; 60 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; 65 66 auto *CFGuardTargetEntry = pImpl->getOrInsertBundleTag("cfguardtarget"); 67 assert(CFGuardTargetEntry->second == LLVMContext::OB_cfguardtarget && 68 "cfguardtarget operand bundle id drifted!"); 69 (void)CFGuardTargetEntry; 70 71 auto *PreallocatedEntry = pImpl->getOrInsertBundleTag("preallocated"); 72 assert(PreallocatedEntry->second == LLVMContext::OB_preallocated && 73 "preallocated operand bundle id drifted!"); 74 (void)PreallocatedEntry; 75 76 auto *GCLiveEntry = pImpl->getOrInsertBundleTag("gc-live"); 77 assert(GCLiveEntry->second == LLVMContext::OB_gc_live && 78 "gc-transition operand bundle id drifted!"); 79 (void)GCLiveEntry; 80 81 SyncScope::ID SingleThreadSSID = 82 pImpl->getOrInsertSyncScopeID("singlethread"); 83 assert(SingleThreadSSID == SyncScope::SingleThread && 84 "singlethread synchronization scope ID drifted!"); 85 (void)SingleThreadSSID; 86 87 SyncScope::ID SystemSSID = 88 pImpl->getOrInsertSyncScopeID(""); 89 assert(SystemSSID == SyncScope::System && 90 "system synchronization scope ID drifted!"); 91 (void)SystemSSID; 92 } 93 94 LLVMContext::~LLVMContext() { delete pImpl; } 95 96 void LLVMContext::addModule(Module *M) { 97 pImpl->OwnedModules.insert(M); 98 } 99 100 void LLVMContext::removeModule(Module *M) { 101 pImpl->OwnedModules.erase(M); 102 } 103 104 //===----------------------------------------------------------------------===// 105 // Recoverable Backend Errors 106 //===----------------------------------------------------------------------===// 107 108 void LLVMContext:: 109 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 110 void *DiagContext) { 111 pImpl->InlineAsmDiagHandler = DiagHandler; 112 pImpl->InlineAsmDiagContext = DiagContext; 113 } 114 115 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 116 /// setInlineAsmDiagnosticHandler. 117 LLVMContext::InlineAsmDiagHandlerTy 118 LLVMContext::getInlineAsmDiagnosticHandler() const { 119 return pImpl->InlineAsmDiagHandler; 120 } 121 122 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 123 /// setInlineAsmDiagnosticHandler. 124 void *LLVMContext::getInlineAsmDiagnosticContext() const { 125 return pImpl->InlineAsmDiagContext; 126 } 127 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; 134 } 135 136 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH, 137 bool RespectFilters) { 138 pImpl->DiagHandler = std::move(DH); 139 pImpl->RespectDiagnosticFilters = RespectFilters; 140 } 141 142 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) { 143 pImpl->DiagnosticsHotnessRequested = Requested; 144 } 145 bool LLVMContext::getDiagnosticsHotnessRequested() const { 146 return pImpl->DiagnosticsHotnessRequested; 147 } 148 149 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) { 150 pImpl->DiagnosticsHotnessThreshold = Threshold; 151 } 152 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const { 153 return pImpl->DiagnosticsHotnessThreshold; 154 } 155 156 remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() { 157 return pImpl->MainRemarkStreamer.get(); 158 } 159 const remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() const { 160 return const_cast<LLVMContext *>(this)->getMainRemarkStreamer(); 161 } 162 void LLVMContext::setMainRemarkStreamer( 163 std::unique_ptr<remarks::RemarkStreamer> RemarkStreamer) { 164 pImpl->MainRemarkStreamer = std::move(RemarkStreamer); 165 } 166 167 LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() { 168 return pImpl->LLVMRS.get(); 169 } 170 const LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() const { 171 return const_cast<LLVMContext *>(this)->getLLVMRemarkStreamer(); 172 } 173 void LLVMContext::setLLVMRemarkStreamer( 174 std::unique_ptr<LLVMRemarkStreamer> RemarkStreamer) { 175 pImpl->LLVMRS = std::move(RemarkStreamer); 176 } 177 178 DiagnosticHandler::DiagnosticHandlerTy 179 LLVMContext::getDiagnosticHandlerCallBack() const { 180 return pImpl->DiagHandler->DiagHandlerCallback; 181 } 182 183 void *LLVMContext::getDiagnosticContext() const { 184 return pImpl->DiagHandler->DiagnosticContext; 185 } 186 187 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 188 { 189 pImpl->YieldCallback = Callback; 190 pImpl->YieldOpaqueHandle = OpaqueHandle; 191 } 192 193 void LLVMContext::yield() { 194 if (pImpl->YieldCallback) 195 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 196 } 197 198 void LLVMContext::emitError(const Twine &ErrorStr) { 199 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 200 } 201 202 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 203 assert (I && "Invalid instruction"); 204 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 205 } 206 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. 212 // 213 // Also noisy remarks are only enabled if we have hotness information to sort 214 // them. 215 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 216 return Remark->isEnabled() && 217 (!Remark->isVerbose() || Remark->getHotness()); 218 219 return true; 220 } 221 222 const char * 223 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 224 switch (Severity) { 225 case DS_Error: 226 return "error"; 227 case DS_Warning: 228 return "warning"; 229 case DS_Remark: 230 return "remark"; 231 case DS_Note: 232 return "note"; 233 } 234 llvm_unreachable("Unknown DiagnosticSeverity"); 235 } 236 237 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 238 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 239 if (LLVMRemarkStreamer *RS = getLLVMRemarkStreamer()) 240 RS->emit(*OptDiagBase); 241 242 // If there is a report handler, use it. 243 if (pImpl->DiagHandler && 244 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) && 245 pImpl->DiagHandler->handleDiagnostics(DI)) 246 return; 247 248 if (!isDiagnosticEnabled(DI)) 249 return; 250 251 // Otherwise, print the message with a prefix based on the severity. 252 DiagnosticPrinterRawOStream DP(errs()); 253 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 254 DI.print(DP); 255 errs() << "\n"; 256 if (DI.getSeverity() == DS_Error) 257 exit(1); 258 } 259 260 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 261 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 262 } 263 264 //===----------------------------------------------------------------------===// 265 // Metadata Kind Uniquing 266 //===----------------------------------------------------------------------===// 267 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( 272 std::make_pair( 273 Name, pImpl->CustomMDKindNames.size())) 274 .first->second; 275 } 276 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(); 284 } 285 286 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 287 pImpl->getOperandBundleTags(Tags); 288 } 289 290 StringMapEntry<uint32_t> * 291 LLVMContext::getOrInsertBundleTag(StringRef TagName) const { 292 return pImpl->getOrInsertBundleTag(TagName); 293 } 294 295 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 296 return pImpl->getOperandBundleTagID(Tag); 297 } 298 299 SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) { 300 return pImpl->getOrInsertSyncScopeID(SSN); 301 } 302 303 void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const { 304 pImpl->getSyncScopeNames(SSNs); 305 } 306 307 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 308 auto It = pImpl->GCNames.find(&Fn); 309 310 if (It == pImpl->GCNames.end()) { 311 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 312 return; 313 } 314 It->second = std::move(GCName); 315 } 316 317 const std::string &LLVMContext::getGC(const Function &Fn) { 318 return pImpl->GCNames[&Fn]; 319 } 320 321 void LLVMContext::deleteGC(const Function &Fn) { 322 pImpl->GCNames.erase(&Fn); 323 } 324 325 bool LLVMContext::shouldDiscardValueNames() const { 326 return pImpl->DiscardValueNames; 327 } 328 329 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 330 331 void LLVMContext::enableDebugTypeODRUniquing() { 332 if (pImpl->DITypeMap) 333 return; 334 335 pImpl->DITypeMap.emplace(); 336 } 337 338 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 339 340 void LLVMContext::setDiscardValueNames(bool Discard) { 341 pImpl->DiscardValueNames = Discard; 342 } 343 344 OptPassGate &LLVMContext::getOptPassGate() const { 345 return pImpl->getOptPassGate(); 346 } 347 348 void LLVMContext::setOptPassGate(OptPassGate& OPG) { 349 pImpl->setOptPassGate(OPG); 350 } 351 352 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const { 353 return pImpl->DiagHandler.get(); 354 } 355 356 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() { 357 return std::move(pImpl->DiagHandler); 358 } 359