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::setDiagnosticHandlerCallBack( 109 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler, 110 void *DiagnosticContext, bool RespectFilters) { 111 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler; 112 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext; 113 pImpl->RespectDiagnosticFilters = RespectFilters; 114 } 115 116 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH, 117 bool RespectFilters) { 118 pImpl->DiagHandler = std::move(DH); 119 pImpl->RespectDiagnosticFilters = RespectFilters; 120 } 121 122 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) { 123 pImpl->DiagnosticsHotnessRequested = Requested; 124 } 125 bool LLVMContext::getDiagnosticsHotnessRequested() const { 126 return pImpl->DiagnosticsHotnessRequested; 127 } 128 129 void LLVMContext::setDiagnosticsHotnessThreshold(Optional<uint64_t> Threshold) { 130 pImpl->DiagnosticsHotnessThreshold = Threshold; 131 } 132 133 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const { 134 return pImpl->DiagnosticsHotnessThreshold.getValueOr(UINT64_MAX); 135 } 136 137 bool LLVMContext::isDiagnosticsHotnessThresholdSetFromPSI() const { 138 return !pImpl->DiagnosticsHotnessThreshold.hasValue(); 139 } 140 141 remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() { 142 return pImpl->MainRemarkStreamer.get(); 143 } 144 const remarks::RemarkStreamer *LLVMContext::getMainRemarkStreamer() const { 145 return const_cast<LLVMContext *>(this)->getMainRemarkStreamer(); 146 } 147 void LLVMContext::setMainRemarkStreamer( 148 std::unique_ptr<remarks::RemarkStreamer> RemarkStreamer) { 149 pImpl->MainRemarkStreamer = std::move(RemarkStreamer); 150 } 151 152 LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() { 153 return pImpl->LLVMRS.get(); 154 } 155 const LLVMRemarkStreamer *LLVMContext::getLLVMRemarkStreamer() const { 156 return const_cast<LLVMContext *>(this)->getLLVMRemarkStreamer(); 157 } 158 void LLVMContext::setLLVMRemarkStreamer( 159 std::unique_ptr<LLVMRemarkStreamer> RemarkStreamer) { 160 pImpl->LLVMRS = std::move(RemarkStreamer); 161 } 162 163 DiagnosticHandler::DiagnosticHandlerTy 164 LLVMContext::getDiagnosticHandlerCallBack() const { 165 return pImpl->DiagHandler->DiagHandlerCallback; 166 } 167 168 void *LLVMContext::getDiagnosticContext() const { 169 return pImpl->DiagHandler->DiagnosticContext; 170 } 171 172 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 173 { 174 pImpl->YieldCallback = Callback; 175 pImpl->YieldOpaqueHandle = OpaqueHandle; 176 } 177 178 void LLVMContext::yield() { 179 if (pImpl->YieldCallback) 180 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 181 } 182 183 void LLVMContext::emitError(const Twine &ErrorStr) { 184 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 185 } 186 187 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 188 assert (I && "Invalid instruction"); 189 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 190 } 191 192 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 193 // Optimization remarks are selective. They need to check whether the regexp 194 // pattern, passed via one of the -pass-remarks* flags, matches the name of 195 // the pass that is emitting the diagnostic. If there is no match, ignore the 196 // diagnostic and return. 197 // 198 // Also noisy remarks are only enabled if we have hotness information to sort 199 // them. 200 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 201 return Remark->isEnabled() && 202 (!Remark->isVerbose() || Remark->getHotness()); 203 204 return true; 205 } 206 207 const char * 208 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 209 switch (Severity) { 210 case DS_Error: 211 return "error"; 212 case DS_Warning: 213 return "warning"; 214 case DS_Remark: 215 return "remark"; 216 case DS_Note: 217 return "note"; 218 } 219 llvm_unreachable("Unknown DiagnosticSeverity"); 220 } 221 222 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 223 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 224 if (LLVMRemarkStreamer *RS = getLLVMRemarkStreamer()) 225 RS->emit(*OptDiagBase); 226 227 // If there is a report handler, use it. 228 if (pImpl->DiagHandler && 229 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) && 230 pImpl->DiagHandler->handleDiagnostics(DI)) 231 return; 232 233 if (!isDiagnosticEnabled(DI)) 234 return; 235 236 // Otherwise, print the message with a prefix based on the severity. 237 DiagnosticPrinterRawOStream DP(errs()); 238 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 239 DI.print(DP); 240 errs() << "\n"; 241 if (DI.getSeverity() == DS_Error) 242 exit(1); 243 } 244 245 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 246 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 247 } 248 249 //===----------------------------------------------------------------------===// 250 // Metadata Kind Uniquing 251 //===----------------------------------------------------------------------===// 252 253 /// Return a unique non-zero ID for the specified metadata kind. 254 unsigned LLVMContext::getMDKindID(StringRef Name) const { 255 // If this is new, assign it its ID. 256 return pImpl->CustomMDKindNames.insert( 257 std::make_pair( 258 Name, pImpl->CustomMDKindNames.size())) 259 .first->second; 260 } 261 262 /// getHandlerNames - Populate client-supplied smallvector using custom 263 /// metadata name and ID. 264 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 265 Names.resize(pImpl->CustomMDKindNames.size()); 266 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 267 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 268 Names[I->second] = I->first(); 269 } 270 271 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 272 pImpl->getOperandBundleTags(Tags); 273 } 274 275 StringMapEntry<uint32_t> * 276 LLVMContext::getOrInsertBundleTag(StringRef TagName) const { 277 return pImpl->getOrInsertBundleTag(TagName); 278 } 279 280 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 281 return pImpl->getOperandBundleTagID(Tag); 282 } 283 284 SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) { 285 return pImpl->getOrInsertSyncScopeID(SSN); 286 } 287 288 void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const { 289 pImpl->getSyncScopeNames(SSNs); 290 } 291 292 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 293 auto It = pImpl->GCNames.find(&Fn); 294 295 if (It == pImpl->GCNames.end()) { 296 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 297 return; 298 } 299 It->second = std::move(GCName); 300 } 301 302 const std::string &LLVMContext::getGC(const Function &Fn) { 303 return pImpl->GCNames[&Fn]; 304 } 305 306 void LLVMContext::deleteGC(const Function &Fn) { 307 pImpl->GCNames.erase(&Fn); 308 } 309 310 bool LLVMContext::shouldDiscardValueNames() const { 311 return pImpl->DiscardValueNames; 312 } 313 314 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 315 316 void LLVMContext::enableDebugTypeODRUniquing() { 317 if (pImpl->DITypeMap) 318 return; 319 320 pImpl->DITypeMap.emplace(); 321 } 322 323 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 324 325 void LLVMContext::setDiscardValueNames(bool Discard) { 326 pImpl->DiscardValueNames = Discard; 327 } 328 329 OptPassGate &LLVMContext::getOptPassGate() const { 330 return pImpl->getOptPassGate(); 331 } 332 333 void LLVMContext::setOptPassGate(OptPassGate& OPG) { 334 pImpl->setOptPassGate(OPG); 335 } 336 337 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const { 338 return pImpl->DiagHandler.get(); 339 } 340 341 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() { 342 return std::move(pImpl->DiagHandler); 343 } 344