1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===// 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 coordinates the per-module state used while generating code. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CodeGenModule.h" 14 #include "ABIInfo.h" 15 #include "CGBlocks.h" 16 #include "CGCUDARuntime.h" 17 #include "CGCXXABI.h" 18 #include "CGCall.h" 19 #include "CGDebugInfo.h" 20 #include "CGHLSLRuntime.h" 21 #include "CGObjCRuntime.h" 22 #include "CGOpenCLRuntime.h" 23 #include "CGOpenMPRuntime.h" 24 #include "CGOpenMPRuntimeGPU.h" 25 #include "CodeGenFunction.h" 26 #include "CodeGenPGO.h" 27 #include "ConstantEmitter.h" 28 #include "CoverageMappingGen.h" 29 #include "TargetInfo.h" 30 #include "clang/AST/ASTContext.h" 31 #include "clang/AST/CharUnits.h" 32 #include "clang/AST/DeclCXX.h" 33 #include "clang/AST/DeclObjC.h" 34 #include "clang/AST/DeclTemplate.h" 35 #include "clang/AST/Mangle.h" 36 #include "clang/AST/RecursiveASTVisitor.h" 37 #include "clang/AST/StmtVisitor.h" 38 #include "clang/Basic/Builtins.h" 39 #include "clang/Basic/CharInfo.h" 40 #include "clang/Basic/CodeGenOptions.h" 41 #include "clang/Basic/Diagnostic.h" 42 #include "clang/Basic/FileManager.h" 43 #include "clang/Basic/Module.h" 44 #include "clang/Basic/SourceManager.h" 45 #include "clang/Basic/TargetInfo.h" 46 #include "clang/Basic/Version.h" 47 #include "clang/CodeGen/BackendUtil.h" 48 #include "clang/CodeGen/ConstantInitBuilder.h" 49 #include "clang/Frontend/FrontendDiagnostic.h" 50 #include "llvm/ADT/STLExtras.h" 51 #include "llvm/ADT/StringExtras.h" 52 #include "llvm/ADT/StringSwitch.h" 53 #include "llvm/Analysis/TargetLibraryInfo.h" 54 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" 55 #include "llvm/IR/CallingConv.h" 56 #include "llvm/IR/DataLayout.h" 57 #include "llvm/IR/Intrinsics.h" 58 #include "llvm/IR/LLVMContext.h" 59 #include "llvm/IR/Module.h" 60 #include "llvm/IR/ProfileSummary.h" 61 #include "llvm/ProfileData/InstrProfReader.h" 62 #include "llvm/ProfileData/SampleProf.h" 63 #include "llvm/Support/CRC.h" 64 #include "llvm/Support/CodeGen.h" 65 #include "llvm/Support/CommandLine.h" 66 #include "llvm/Support/ConvertUTF.h" 67 #include "llvm/Support/ErrorHandling.h" 68 #include "llvm/Support/TimeProfiler.h" 69 #include "llvm/Support/xxhash.h" 70 #include "llvm/TargetParser/Triple.h" 71 #include "llvm/TargetParser/X86TargetParser.h" 72 #include <optional> 73 74 using namespace clang; 75 using namespace CodeGen; 76 77 static llvm::cl::opt<bool> LimitedCoverage( 78 "limited-coverage-experimental", llvm::cl::Hidden, 79 llvm::cl::desc("Emit limited coverage mapping information (experimental)")); 80 81 static const char AnnotationSection[] = "llvm.metadata"; 82 83 static CGCXXABI *createCXXABI(CodeGenModule &CGM) { 84 switch (CGM.getContext().getCXXABIKind()) { 85 case TargetCXXABI::AppleARM64: 86 case TargetCXXABI::Fuchsia: 87 case TargetCXXABI::GenericAArch64: 88 case TargetCXXABI::GenericARM: 89 case TargetCXXABI::iOS: 90 case TargetCXXABI::WatchOS: 91 case TargetCXXABI::GenericMIPS: 92 case TargetCXXABI::GenericItanium: 93 case TargetCXXABI::WebAssembly: 94 case TargetCXXABI::XL: 95 return CreateItaniumCXXABI(CGM); 96 case TargetCXXABI::Microsoft: 97 return CreateMicrosoftCXXABI(CGM); 98 } 99 100 llvm_unreachable("invalid C++ ABI kind"); 101 } 102 103 CodeGenModule::CodeGenModule(ASTContext &C, 104 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS, 105 const HeaderSearchOptions &HSO, 106 const PreprocessorOptions &PPO, 107 const CodeGenOptions &CGO, llvm::Module &M, 108 DiagnosticsEngine &diags, 109 CoverageSourceInfo *CoverageInfo) 110 : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO), 111 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags), 112 Target(C.getTargetInfo()), ABI(createCXXABI(*this)), 113 VMContext(M.getContext()), Types(*this), VTables(*this), 114 SanitizerMD(new SanitizerMetadata(*this)) { 115 116 // Initialize the type cache. 117 llvm::LLVMContext &LLVMContext = M.getContext(); 118 VoidTy = llvm::Type::getVoidTy(LLVMContext); 119 Int8Ty = llvm::Type::getInt8Ty(LLVMContext); 120 Int16Ty = llvm::Type::getInt16Ty(LLVMContext); 121 Int32Ty = llvm::Type::getInt32Ty(LLVMContext); 122 Int64Ty = llvm::Type::getInt64Ty(LLVMContext); 123 HalfTy = llvm::Type::getHalfTy(LLVMContext); 124 BFloatTy = llvm::Type::getBFloatTy(LLVMContext); 125 FloatTy = llvm::Type::getFloatTy(LLVMContext); 126 DoubleTy = llvm::Type::getDoubleTy(LLVMContext); 127 PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default); 128 PointerAlignInBytes = 129 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default)) 130 .getQuantity(); 131 SizeSizeInBytes = 132 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity(); 133 IntAlignInBytes = 134 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity(); 135 CharTy = 136 llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth()); 137 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth()); 138 IntPtrTy = llvm::IntegerType::get(LLVMContext, 139 C.getTargetInfo().getMaxPointerWidth()); 140 Int8PtrTy = Int8Ty->getPointerTo(0); 141 Int8PtrPtrTy = Int8PtrTy->getPointerTo(0); 142 const llvm::DataLayout &DL = M.getDataLayout(); 143 AllocaInt8PtrTy = Int8Ty->getPointerTo(DL.getAllocaAddrSpace()); 144 GlobalsInt8PtrTy = Int8Ty->getPointerTo(DL.getDefaultGlobalsAddressSpace()); 145 ConstGlobalsPtrTy = Int8Ty->getPointerTo( 146 C.getTargetAddressSpace(GetGlobalConstantAddressSpace())); 147 ASTAllocaAddressSpace = getTargetCodeGenInfo().getASTAllocaAddressSpace(); 148 149 // Build C++20 Module initializers. 150 // TODO: Add Microsoft here once we know the mangling required for the 151 // initializers. 152 CXX20ModuleInits = 153 LangOpts.CPlusPlusModules && getCXXABI().getMangleContext().getKind() == 154 ItaniumMangleContext::MK_Itanium; 155 156 RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC(); 157 158 if (LangOpts.ObjC) 159 createObjCRuntime(); 160 if (LangOpts.OpenCL) 161 createOpenCLRuntime(); 162 if (LangOpts.OpenMP) 163 createOpenMPRuntime(); 164 if (LangOpts.CUDA) 165 createCUDARuntime(); 166 if (LangOpts.HLSL) 167 createHLSLRuntime(); 168 169 // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0. 170 if (LangOpts.Sanitize.has(SanitizerKind::Thread) || 171 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0)) 172 TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(), 173 getCXXABI().getMangleContext())); 174 175 // If debug info or coverage generation is enabled, create the CGDebugInfo 176 // object. 177 if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo || 178 CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes) 179 DebugInfo.reset(new CGDebugInfo(*this)); 180 181 Block.GlobalUniqueCount = 0; 182 183 if (C.getLangOpts().ObjC) 184 ObjCData.reset(new ObjCEntrypoints()); 185 186 if (CodeGenOpts.hasProfileClangUse()) { 187 auto ReaderOrErr = llvm::IndexedInstrProfReader::create( 188 CodeGenOpts.ProfileInstrumentUsePath, *FS, 189 CodeGenOpts.ProfileRemappingFile); 190 // We're checking for profile read errors in CompilerInvocation, so if 191 // there was an error it should've already been caught. If it hasn't been 192 // somehow, trip an assertion. 193 assert(ReaderOrErr); 194 PGOReader = std::move(ReaderOrErr.get()); 195 } 196 197 // If coverage mapping generation is enabled, create the 198 // CoverageMappingModuleGen object. 199 if (CodeGenOpts.CoverageMapping) 200 CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo)); 201 202 // Generate the module name hash here if needed. 203 if (CodeGenOpts.UniqueInternalLinkageNames && 204 !getModule().getSourceFileName().empty()) { 205 std::string Path = getModule().getSourceFileName(); 206 // Check if a path substitution is needed from the MacroPrefixMap. 207 for (const auto &Entry : LangOpts.MacroPrefixMap) 208 if (Path.rfind(Entry.first, 0) != std::string::npos) { 209 Path = Entry.second + Path.substr(Entry.first.size()); 210 break; 211 } 212 ModuleNameHash = llvm::getUniqueInternalLinkagePostfix(Path); 213 } 214 } 215 216 CodeGenModule::~CodeGenModule() {} 217 218 void CodeGenModule::createObjCRuntime() { 219 // This is just isGNUFamily(), but we want to force implementors of 220 // new ABIs to decide how best to do this. 221 switch (LangOpts.ObjCRuntime.getKind()) { 222 case ObjCRuntime::GNUstep: 223 case ObjCRuntime::GCC: 224 case ObjCRuntime::ObjFW: 225 ObjCRuntime.reset(CreateGNUObjCRuntime(*this)); 226 return; 227 228 case ObjCRuntime::FragileMacOSX: 229 case ObjCRuntime::MacOSX: 230 case ObjCRuntime::iOS: 231 case ObjCRuntime::WatchOS: 232 ObjCRuntime.reset(CreateMacObjCRuntime(*this)); 233 return; 234 } 235 llvm_unreachable("bad runtime kind"); 236 } 237 238 void CodeGenModule::createOpenCLRuntime() { 239 OpenCLRuntime.reset(new CGOpenCLRuntime(*this)); 240 } 241 242 void CodeGenModule::createOpenMPRuntime() { 243 // Select a specialized code generation class based on the target, if any. 244 // If it does not exist use the default implementation. 245 switch (getTriple().getArch()) { 246 case llvm::Triple::nvptx: 247 case llvm::Triple::nvptx64: 248 case llvm::Triple::amdgcn: 249 assert(getLangOpts().OpenMPIsDevice && 250 "OpenMP AMDGPU/NVPTX is only prepared to deal with device code."); 251 OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this)); 252 break; 253 default: 254 if (LangOpts.OpenMPSimd) 255 OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this)); 256 else 257 OpenMPRuntime.reset(new CGOpenMPRuntime(*this)); 258 break; 259 } 260 } 261 262 void CodeGenModule::createCUDARuntime() { 263 CUDARuntime.reset(CreateNVCUDARuntime(*this)); 264 } 265 266 void CodeGenModule::createHLSLRuntime() { 267 HLSLRuntime.reset(new CGHLSLRuntime(*this)); 268 } 269 270 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) { 271 Replacements[Name] = C; 272 } 273 274 void CodeGenModule::applyReplacements() { 275 for (auto &I : Replacements) { 276 StringRef MangledName = I.first(); 277 llvm::Constant *Replacement = I.second; 278 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 279 if (!Entry) 280 continue; 281 auto *OldF = cast<llvm::Function>(Entry); 282 auto *NewF = dyn_cast<llvm::Function>(Replacement); 283 if (!NewF) { 284 if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) { 285 NewF = dyn_cast<llvm::Function>(Alias->getAliasee()); 286 } else { 287 auto *CE = cast<llvm::ConstantExpr>(Replacement); 288 assert(CE->getOpcode() == llvm::Instruction::BitCast || 289 CE->getOpcode() == llvm::Instruction::GetElementPtr); 290 NewF = dyn_cast<llvm::Function>(CE->getOperand(0)); 291 } 292 } 293 294 // Replace old with new, but keep the old order. 295 OldF->replaceAllUsesWith(Replacement); 296 if (NewF) { 297 NewF->removeFromParent(); 298 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(), 299 NewF); 300 } 301 OldF->eraseFromParent(); 302 } 303 } 304 305 void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) { 306 GlobalValReplacements.push_back(std::make_pair(GV, C)); 307 } 308 309 void CodeGenModule::applyGlobalValReplacements() { 310 for (auto &I : GlobalValReplacements) { 311 llvm::GlobalValue *GV = I.first; 312 llvm::Constant *C = I.second; 313 314 GV->replaceAllUsesWith(C); 315 GV->eraseFromParent(); 316 } 317 } 318 319 // This is only used in aliases that we created and we know they have a 320 // linear structure. 321 static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) { 322 const llvm::Constant *C; 323 if (auto *GA = dyn_cast<llvm::GlobalAlias>(GV)) 324 C = GA->getAliasee(); 325 else if (auto *GI = dyn_cast<llvm::GlobalIFunc>(GV)) 326 C = GI->getResolver(); 327 else 328 return GV; 329 330 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts()); 331 if (!AliaseeGV) 332 return nullptr; 333 334 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject(); 335 if (FinalGV == GV) 336 return nullptr; 337 338 return FinalGV; 339 } 340 341 static bool checkAliasedGlobal( 342 DiagnosticsEngine &Diags, SourceLocation Location, bool IsIFunc, 343 const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV, 344 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames, 345 SourceRange AliasRange) { 346 GV = getAliasedGlobal(Alias); 347 if (!GV) { 348 Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc; 349 return false; 350 } 351 352 if (GV->isDeclaration()) { 353 Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc; 354 Diags.Report(Location, diag::note_alias_requires_mangled_name) 355 << IsIFunc << IsIFunc; 356 // Provide a note if the given function is not found and exists as a 357 // mangled name. 358 for (const auto &[Decl, Name] : MangledDeclNames) { 359 if (const auto *ND = dyn_cast<NamedDecl>(Decl.getDecl())) { 360 if (ND->getName() == GV->getName()) { 361 Diags.Report(Location, diag::note_alias_mangled_name_alternative) 362 << Name 363 << FixItHint::CreateReplacement( 364 AliasRange, 365 (Twine(IsIFunc ? "ifunc" : "alias") + "(\"" + Name + "\")") 366 .str()); 367 } 368 } 369 } 370 return false; 371 } 372 373 if (IsIFunc) { 374 // Check resolver function type. 375 const auto *F = dyn_cast<llvm::Function>(GV); 376 if (!F) { 377 Diags.Report(Location, diag::err_alias_to_undefined) 378 << IsIFunc << IsIFunc; 379 return false; 380 } 381 382 llvm::FunctionType *FTy = F->getFunctionType(); 383 if (!FTy->getReturnType()->isPointerTy()) { 384 Diags.Report(Location, diag::err_ifunc_resolver_return); 385 return false; 386 } 387 } 388 389 return true; 390 } 391 392 void CodeGenModule::checkAliases() { 393 // Check if the constructed aliases are well formed. It is really unfortunate 394 // that we have to do this in CodeGen, but we only construct mangled names 395 // and aliases during codegen. 396 bool Error = false; 397 DiagnosticsEngine &Diags = getDiags(); 398 for (const GlobalDecl &GD : Aliases) { 399 const auto *D = cast<ValueDecl>(GD.getDecl()); 400 SourceLocation Location; 401 SourceRange Range; 402 bool IsIFunc = D->hasAttr<IFuncAttr>(); 403 if (const Attr *A = D->getDefiningAttr()) { 404 Location = A->getLocation(); 405 Range = A->getRange(); 406 } else 407 llvm_unreachable("Not an alias or ifunc?"); 408 409 StringRef MangledName = getMangledName(GD); 410 llvm::GlobalValue *Alias = GetGlobalValue(MangledName); 411 const llvm::GlobalValue *GV = nullptr; 412 if (!checkAliasedGlobal(Diags, Location, IsIFunc, Alias, GV, 413 MangledDeclNames, Range)) { 414 Error = true; 415 continue; 416 } 417 418 llvm::Constant *Aliasee = 419 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver() 420 : cast<llvm::GlobalAlias>(Alias)->getAliasee(); 421 422 llvm::GlobalValue *AliaseeGV; 423 if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) 424 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0)); 425 else 426 AliaseeGV = cast<llvm::GlobalValue>(Aliasee); 427 428 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { 429 StringRef AliasSection = SA->getName(); 430 if (AliasSection != AliaseeGV->getSection()) 431 Diags.Report(SA->getLocation(), diag::warn_alias_with_section) 432 << AliasSection << IsIFunc << IsIFunc; 433 } 434 435 // We have to handle alias to weak aliases in here. LLVM itself disallows 436 // this since the object semantics would not match the IL one. For 437 // compatibility with gcc we implement it by just pointing the alias 438 // to its aliasee's aliasee. We also warn, since the user is probably 439 // expecting the link to be weak. 440 if (auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) { 441 if (GA->isInterposable()) { 442 Diags.Report(Location, diag::warn_alias_to_weak_alias) 443 << GV->getName() << GA->getName() << IsIFunc; 444 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( 445 GA->getAliasee(), Alias->getType()); 446 447 if (IsIFunc) 448 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee); 449 else 450 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee); 451 } 452 } 453 } 454 if (!Error) 455 return; 456 457 for (const GlobalDecl &GD : Aliases) { 458 StringRef MangledName = getMangledName(GD); 459 llvm::GlobalValue *Alias = GetGlobalValue(MangledName); 460 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType())); 461 Alias->eraseFromParent(); 462 } 463 } 464 465 void CodeGenModule::clear() { 466 DeferredDeclsToEmit.clear(); 467 EmittedDeferredDecls.clear(); 468 if (OpenMPRuntime) 469 OpenMPRuntime->clear(); 470 } 471 472 void InstrProfStats::reportDiagnostics(DiagnosticsEngine &Diags, 473 StringRef MainFile) { 474 if (!hasDiagnostics()) 475 return; 476 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) { 477 if (MainFile.empty()) 478 MainFile = "<stdin>"; 479 Diags.Report(diag::warn_profile_data_unprofiled) << MainFile; 480 } else { 481 if (Mismatched > 0) 482 Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched; 483 484 if (Missing > 0) 485 Diags.Report(diag::warn_profile_data_missing) << Visited << Missing; 486 } 487 } 488 489 static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO, 490 llvm::Module &M) { 491 if (!LO.VisibilityFromDLLStorageClass) 492 return; 493 494 llvm::GlobalValue::VisibilityTypes DLLExportVisibility = 495 CodeGenModule::GetLLVMVisibility(LO.getDLLExportVisibility()); 496 llvm::GlobalValue::VisibilityTypes NoDLLStorageClassVisibility = 497 CodeGenModule::GetLLVMVisibility(LO.getNoDLLStorageClassVisibility()); 498 llvm::GlobalValue::VisibilityTypes ExternDeclDLLImportVisibility = 499 CodeGenModule::GetLLVMVisibility(LO.getExternDeclDLLImportVisibility()); 500 llvm::GlobalValue::VisibilityTypes ExternDeclNoDLLStorageClassVisibility = 501 CodeGenModule::GetLLVMVisibility( 502 LO.getExternDeclNoDLLStorageClassVisibility()); 503 504 for (llvm::GlobalValue &GV : M.global_values()) { 505 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage()) 506 continue; 507 508 // Reset DSO locality before setting the visibility. This removes 509 // any effects that visibility options and annotations may have 510 // had on the DSO locality. Setting the visibility will implicitly set 511 // appropriate globals to DSO Local; however, this will be pessimistic 512 // w.r.t. to the normal compiler IRGen. 513 GV.setDSOLocal(false); 514 515 if (GV.isDeclarationForLinker()) { 516 GV.setVisibility(GV.getDLLStorageClass() == 517 llvm::GlobalValue::DLLImportStorageClass 518 ? ExternDeclDLLImportVisibility 519 : ExternDeclNoDLLStorageClassVisibility); 520 } else { 521 GV.setVisibility(GV.getDLLStorageClass() == 522 llvm::GlobalValue::DLLExportStorageClass 523 ? DLLExportVisibility 524 : NoDLLStorageClassVisibility); 525 } 526 527 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 528 } 529 } 530 531 void CodeGenModule::Release() { 532 Module *Primary = getContext().getNamedModuleForCodeGen(); 533 if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule()) 534 EmitModuleInitializers(Primary); 535 EmitDeferred(); 536 DeferredDecls.insert(EmittedDeferredDecls.begin(), 537 EmittedDeferredDecls.end()); 538 EmittedDeferredDecls.clear(); 539 EmitVTablesOpportunistically(); 540 applyGlobalValReplacements(); 541 applyReplacements(); 542 emitMultiVersionFunctions(); 543 544 if (Context.getLangOpts().IncrementalExtensions && 545 GlobalTopLevelStmtBlockInFlight.first) { 546 const TopLevelStmtDecl *TLSD = GlobalTopLevelStmtBlockInFlight.second; 547 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->getEndLoc()); 548 GlobalTopLevelStmtBlockInFlight = {nullptr, nullptr}; 549 } 550 551 // Module implementations are initialized the same way as a regular TU that 552 // imports one or more modules. 553 if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition()) 554 EmitCXXModuleInitFunc(Primary); 555 else 556 EmitCXXGlobalInitFunc(); 557 EmitCXXGlobalCleanUpFunc(); 558 registerGlobalDtorsWithAtExit(); 559 EmitCXXThreadLocalInitFunc(); 560 if (ObjCRuntime) 561 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction()) 562 AddGlobalCtor(ObjCInitFunction); 563 if (Context.getLangOpts().CUDA && CUDARuntime) { 564 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule()) 565 AddGlobalCtor(CudaCtorFunction); 566 } 567 if (OpenMPRuntime) { 568 if (llvm::Function *OpenMPRequiresDirectiveRegFun = 569 OpenMPRuntime->emitRequiresDirectiveRegFun()) { 570 AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0); 571 } 572 OpenMPRuntime->createOffloadEntriesAndInfoMetadata(); 573 OpenMPRuntime->clear(); 574 } 575 if (PGOReader) { 576 getModule().setProfileSummary( 577 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext), 578 llvm::ProfileSummary::PSK_Instr); 579 if (PGOStats.hasDiagnostics()) 580 PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName); 581 } 582 llvm::stable_sort(GlobalCtors, [](const Structor &L, const Structor &R) { 583 return L.LexOrder < R.LexOrder; 584 }); 585 EmitCtorList(GlobalCtors, "llvm.global_ctors"); 586 EmitCtorList(GlobalDtors, "llvm.global_dtors"); 587 EmitGlobalAnnotations(); 588 EmitStaticExternCAliases(); 589 checkAliases(); 590 EmitDeferredUnusedCoverageMappings(); 591 CodeGenPGO(*this).setValueProfilingFlag(getModule()); 592 if (CoverageMapping) 593 CoverageMapping->emit(); 594 if (CodeGenOpts.SanitizeCfiCrossDso) { 595 CodeGenFunction(*this).EmitCfiCheckFail(); 596 CodeGenFunction(*this).EmitCfiCheckStub(); 597 } 598 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) 599 finalizeKCFITypes(); 600 emitAtAvailableLinkGuard(); 601 if (Context.getTargetInfo().getTriple().isWasm()) 602 EmitMainVoidAlias(); 603 604 if (getTriple().isAMDGPU()) { 605 // Emit amdgpu_code_object_version module flag, which is code object version 606 // times 100. 607 if (getTarget().getTargetOpts().CodeObjectVersion != 608 TargetOptions::COV_None) { 609 getModule().addModuleFlag(llvm::Module::Error, 610 "amdgpu_code_object_version", 611 getTarget().getTargetOpts().CodeObjectVersion); 612 } 613 } 614 615 // Emit a global array containing all external kernels or device variables 616 // used by host functions and mark it as used for CUDA/HIP. This is necessary 617 // to get kernels or device variables in archives linked in even if these 618 // kernels or device variables are only used in host functions. 619 if (!Context.CUDAExternalDeviceDeclODRUsedByHost.empty()) { 620 SmallVector<llvm::Constant *, 8> UsedArray; 621 for (auto D : Context.CUDAExternalDeviceDeclODRUsedByHost) { 622 GlobalDecl GD; 623 if (auto *FD = dyn_cast<FunctionDecl>(D)) 624 GD = GlobalDecl(FD, KernelReferenceKind::Kernel); 625 else 626 GD = GlobalDecl(D); 627 UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( 628 GetAddrOfGlobal(GD), Int8PtrTy)); 629 } 630 631 llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size()); 632 633 auto *GV = new llvm::GlobalVariable( 634 getModule(), ATy, false, llvm::GlobalValue::InternalLinkage, 635 llvm::ConstantArray::get(ATy, UsedArray), "__clang_gpu_used_external"); 636 addCompilerUsedGlobal(GV); 637 } 638 639 emitLLVMUsed(); 640 if (SanStats) 641 SanStats->finish(); 642 643 if (CodeGenOpts.Autolink && 644 (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) { 645 EmitModuleLinkOptions(); 646 } 647 648 // On ELF we pass the dependent library specifiers directly to the linker 649 // without manipulating them. This is in contrast to other platforms where 650 // they are mapped to a specific linker option by the compiler. This 651 // difference is a result of the greater variety of ELF linkers and the fact 652 // that ELF linkers tend to handle libraries in a more complicated fashion 653 // than on other platforms. This forces us to defer handling the dependent 654 // libs to the linker. 655 // 656 // CUDA/HIP device and host libraries are different. Currently there is no 657 // way to differentiate dependent libraries for host or device. Existing 658 // usage of #pragma comment(lib, *) is intended for host libraries on 659 // Windows. Therefore emit llvm.dependent-libraries only for host. 660 if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) { 661 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries"); 662 for (auto *MD : ELFDependentLibraries) 663 NMD->addOperand(MD); 664 } 665 666 // Record mregparm value now so it is visible through rest of codegen. 667 if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) 668 getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters", 669 CodeGenOpts.NumRegisterParameters); 670 671 if (CodeGenOpts.DwarfVersion) { 672 getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version", 673 CodeGenOpts.DwarfVersion); 674 } 675 676 if (CodeGenOpts.Dwarf64) 677 getModule().addModuleFlag(llvm::Module::Max, "DWARF64", 1); 678 679 if (Context.getLangOpts().SemanticInterposition) 680 // Require various optimization to respect semantic interposition. 681 getModule().setSemanticInterposition(true); 682 683 if (CodeGenOpts.EmitCodeView) { 684 // Indicate that we want CodeView in the metadata. 685 getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); 686 } 687 if (CodeGenOpts.CodeViewGHash) { 688 getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1); 689 } 690 if (CodeGenOpts.ControlFlowGuard) { 691 // Function ID tables and checks for Control Flow Guard (cfguard=2). 692 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2); 693 } else if (CodeGenOpts.ControlFlowGuardNoChecks) { 694 // Function ID tables for Control Flow Guard (cfguard=1). 695 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1); 696 } 697 if (CodeGenOpts.EHContGuard) { 698 // Function ID tables for EH Continuation Guard. 699 getModule().addModuleFlag(llvm::Module::Warning, "ehcontguard", 1); 700 } 701 if (Context.getLangOpts().Kernel) { 702 // Note if we are compiling with /kernel. 703 getModule().addModuleFlag(llvm::Module::Warning, "ms-kernel", 1); 704 } 705 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) { 706 // We don't support LTO with 2 with different StrictVTablePointers 707 // FIXME: we could support it by stripping all the information introduced 708 // by StrictVTablePointers. 709 710 getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1); 711 712 llvm::Metadata *Ops[2] = { 713 llvm::MDString::get(VMContext, "StrictVTablePointers"), 714 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 715 llvm::Type::getInt32Ty(VMContext), 1))}; 716 717 getModule().addModuleFlag(llvm::Module::Require, 718 "StrictVTablePointersRequirement", 719 llvm::MDNode::get(VMContext, Ops)); 720 } 721 if (getModuleDebugInfo()) 722 // We support a single version in the linked module. The LLVM 723 // parser will drop debug info with a different version number 724 // (and warn about it, too). 725 getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version", 726 llvm::DEBUG_METADATA_VERSION); 727 728 // We need to record the widths of enums and wchar_t, so that we can generate 729 // the correct build attributes in the ARM backend. wchar_size is also used by 730 // TargetLibraryInfo. 731 uint64_t WCharWidth = 732 Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity(); 733 getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth); 734 735 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); 736 if ( Arch == llvm::Triple::arm 737 || Arch == llvm::Triple::armeb 738 || Arch == llvm::Triple::thumb 739 || Arch == llvm::Triple::thumbeb) { 740 // The minimum width of an enum in bytes 741 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; 742 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); 743 } 744 745 if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) { 746 StringRef ABIStr = Target.getABI(); 747 llvm::LLVMContext &Ctx = TheModule.getContext(); 748 getModule().addModuleFlag(llvm::Module::Error, "target-abi", 749 llvm::MDString::get(Ctx, ABIStr)); 750 } 751 752 if (CodeGenOpts.SanitizeCfiCrossDso) { 753 // Indicate that we want cross-DSO control flow integrity checks. 754 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); 755 } 756 757 if (CodeGenOpts.WholeProgramVTables) { 758 // Indicate whether VFE was enabled for this module, so that the 759 // vcall_visibility metadata added under whole program vtables is handled 760 // appropriately in the optimizer. 761 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim", 762 CodeGenOpts.VirtualFunctionElimination); 763 } 764 765 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) { 766 getModule().addModuleFlag(llvm::Module::Override, 767 "CFI Canonical Jump Tables", 768 CodeGenOpts.SanitizeCfiCanonicalJumpTables); 769 } 770 771 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) { 772 getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1); 773 // KCFI assumes patchable-function-prefix is the same for all indirectly 774 // called functions. Store the expected offset for code generation. 775 if (CodeGenOpts.PatchableFunctionEntryOffset) 776 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset", 777 CodeGenOpts.PatchableFunctionEntryOffset); 778 } 779 780 if (CodeGenOpts.CFProtectionReturn && 781 Target.checkCFProtectionReturnSupported(getDiags())) { 782 // Indicate that we want to instrument return control flow protection. 783 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return", 784 1); 785 } 786 787 if (CodeGenOpts.CFProtectionBranch && 788 Target.checkCFProtectionBranchSupported(getDiags())) { 789 // Indicate that we want to instrument branch control flow protection. 790 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch", 791 1); 792 } 793 794 if (CodeGenOpts.FunctionReturnThunks) 795 getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1); 796 797 if (CodeGenOpts.IndirectBranchCSPrefix) 798 getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1); 799 800 // Add module metadata for return address signing (ignoring 801 // non-leaf/all) and stack tagging. These are actually turned on by function 802 // attributes, but we use module metadata to emit build attributes. This is 803 // needed for LTO, where the function attributes are inside bitcode 804 // serialised into a global variable by the time build attributes are 805 // emitted, so we can't access them. LTO objects could be compiled with 806 // different flags therefore module flags are set to "Min" behavior to achieve 807 // the same end result of the normal build where e.g BTI is off if any object 808 // doesn't support it. 809 if (Context.getTargetInfo().hasFeature("ptrauth") && 810 LangOpts.getSignReturnAddressScope() != 811 LangOptions::SignReturnAddressScopeKind::None) 812 getModule().addModuleFlag(llvm::Module::Override, 813 "sign-return-address-buildattr", 1); 814 if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack)) 815 getModule().addModuleFlag(llvm::Module::Override, 816 "tag-stack-memory-buildattr", 1); 817 818 if (Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb || 819 Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb || 820 Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 || 821 Arch == llvm::Triple::aarch64_be) { 822 if (LangOpts.BranchTargetEnforcement) 823 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement", 824 1); 825 if (LangOpts.hasSignReturnAddress()) 826 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1); 827 if (LangOpts.isSignReturnAddressScopeAll()) 828 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all", 829 1); 830 if (!LangOpts.isSignReturnAddressWithAKey()) 831 getModule().addModuleFlag(llvm::Module::Min, 832 "sign-return-address-with-bkey", 1); 833 } 834 835 if (!CodeGenOpts.MemoryProfileOutput.empty()) { 836 llvm::LLVMContext &Ctx = TheModule.getContext(); 837 getModule().addModuleFlag( 838 llvm::Module::Error, "MemProfProfileFilename", 839 llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput)); 840 } 841 842 if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) { 843 // Indicate whether __nvvm_reflect should be configured to flush denormal 844 // floating point values to 0. (This corresponds to its "__CUDA_FTZ" 845 // property.) 846 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz", 847 CodeGenOpts.FP32DenormalMode.Output != 848 llvm::DenormalMode::IEEE); 849 } 850 851 if (LangOpts.EHAsynch) 852 getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1); 853 854 // Indicate whether this Module was compiled with -fopenmp 855 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd) 856 getModule().addModuleFlag(llvm::Module::Max, "openmp", LangOpts.OpenMP); 857 if (getLangOpts().OpenMPIsDevice) 858 getModule().addModuleFlag(llvm::Module::Max, "openmp-device", 859 LangOpts.OpenMP); 860 861 // Emit OpenCL specific module metadata: OpenCL/SPIR version. 862 if (LangOpts.OpenCL || (LangOpts.CUDAIsDevice && getTriple().isSPIRV())) { 863 EmitOpenCLMetadata(); 864 // Emit SPIR version. 865 if (getTriple().isSPIR()) { 866 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the 867 // opencl.spir.version named metadata. 868 // C++ for OpenCL has a distinct mapping for version compatibility with 869 // OpenCL. 870 auto Version = LangOpts.getOpenCLCompatibleVersion(); 871 llvm::Metadata *SPIRVerElts[] = { 872 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 873 Int32Ty, Version / 100)), 874 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 875 Int32Ty, (Version / 100 > 1) ? 0 : 2))}; 876 llvm::NamedMDNode *SPIRVerMD = 877 TheModule.getOrInsertNamedMetadata("opencl.spir.version"); 878 llvm::LLVMContext &Ctx = TheModule.getContext(); 879 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts)); 880 } 881 } 882 883 // HLSL related end of code gen work items. 884 if (LangOpts.HLSL) 885 getHLSLRuntime().finishCodeGen(); 886 887 if (uint32_t PLevel = Context.getLangOpts().PICLevel) { 888 assert(PLevel < 3 && "Invalid PIC Level"); 889 getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel)); 890 if (Context.getLangOpts().PIE) 891 getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel)); 892 } 893 894 if (getCodeGenOpts().CodeModel.size() > 0) { 895 unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel) 896 .Case("tiny", llvm::CodeModel::Tiny) 897 .Case("small", llvm::CodeModel::Small) 898 .Case("kernel", llvm::CodeModel::Kernel) 899 .Case("medium", llvm::CodeModel::Medium) 900 .Case("large", llvm::CodeModel::Large) 901 .Default(~0u); 902 if (CM != ~0u) { 903 llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM); 904 getModule().setCodeModel(codeModel); 905 } 906 } 907 908 if (CodeGenOpts.NoPLT) 909 getModule().setRtLibUseGOT(); 910 if (CodeGenOpts.UnwindTables) 911 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables)); 912 913 switch (CodeGenOpts.getFramePointer()) { 914 case CodeGenOptions::FramePointerKind::None: 915 // 0 ("none") is the default. 916 break; 917 case CodeGenOptions::FramePointerKind::NonLeaf: 918 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf); 919 break; 920 case CodeGenOptions::FramePointerKind::All: 921 getModule().setFramePointer(llvm::FramePointerKind::All); 922 break; 923 } 924 925 SimplifyPersonality(); 926 927 if (getCodeGenOpts().EmitDeclMetadata) 928 EmitDeclMetadata(); 929 930 if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes) 931 EmitCoverageFile(); 932 933 if (CGDebugInfo *DI = getModuleDebugInfo()) 934 DI->finalize(); 935 936 if (getCodeGenOpts().EmitVersionIdentMetadata) 937 EmitVersionIdentMetadata(); 938 939 if (!getCodeGenOpts().RecordCommandLine.empty()) 940 EmitCommandLineMetadata(); 941 942 if (!getCodeGenOpts().StackProtectorGuard.empty()) 943 getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard); 944 if (!getCodeGenOpts().StackProtectorGuardReg.empty()) 945 getModule().setStackProtectorGuardReg( 946 getCodeGenOpts().StackProtectorGuardReg); 947 if (!getCodeGenOpts().StackProtectorGuardSymbol.empty()) 948 getModule().setStackProtectorGuardSymbol( 949 getCodeGenOpts().StackProtectorGuardSymbol); 950 if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX) 951 getModule().setStackProtectorGuardOffset( 952 getCodeGenOpts().StackProtectorGuardOffset); 953 if (getCodeGenOpts().StackAlignment) 954 getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment); 955 if (getCodeGenOpts().SkipRaxSetup) 956 getModule().addModuleFlag(llvm::Module::Override, "SkipRaxSetup", 1); 957 958 if (getContext().getTargetInfo().getMaxTLSAlign()) 959 getModule().addModuleFlag(llvm::Module::Error, "MaxTLSAlign", 960 getContext().getTargetInfo().getMaxTLSAlign()); 961 962 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames); 963 964 EmitBackendOptionsMetadata(getCodeGenOpts()); 965 966 // If there is device offloading code embed it in the host now. 967 EmbedObject(&getModule(), CodeGenOpts, getDiags()); 968 969 // Set visibility from DLL storage class 970 // We do this at the end of LLVM IR generation; after any operation 971 // that might affect the DLL storage class or the visibility, and 972 // before anything that might act on these. 973 setVisibilityFromDLLStorageClass(LangOpts, getModule()); 974 } 975 976 void CodeGenModule::EmitOpenCLMetadata() { 977 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the 978 // opencl.ocl.version named metadata node. 979 // C++ for OpenCL has a distinct mapping for versions compatibile with OpenCL. 980 auto Version = LangOpts.getOpenCLCompatibleVersion(); 981 llvm::Metadata *OCLVerElts[] = { 982 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 983 Int32Ty, Version / 100)), 984 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( 985 Int32Ty, (Version % 100) / 10))}; 986 llvm::NamedMDNode *OCLVerMD = 987 TheModule.getOrInsertNamedMetadata("opencl.ocl.version"); 988 llvm::LLVMContext &Ctx = TheModule.getContext(); 989 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts)); 990 } 991 992 void CodeGenModule::EmitBackendOptionsMetadata( 993 const CodeGenOptions CodeGenOpts) { 994 if (getTriple().isRISCV()) { 995 getModule().addModuleFlag(llvm::Module::Min, "SmallDataLimit", 996 CodeGenOpts.SmallDataLimit); 997 } 998 } 999 1000 void CodeGenModule::UpdateCompletedType(const TagDecl *TD) { 1001 // Make sure that this type is translated. 1002 Types.UpdateCompletedType(TD); 1003 } 1004 1005 void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) { 1006 // Make sure that this type is translated. 1007 Types.RefreshTypeCacheForClass(RD); 1008 } 1009 1010 llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) { 1011 if (!TBAA) 1012 return nullptr; 1013 return TBAA->getTypeInfo(QTy); 1014 } 1015 1016 TBAAAccessInfo CodeGenModule::getTBAAAccessInfo(QualType AccessType) { 1017 if (!TBAA) 1018 return TBAAAccessInfo(); 1019 if (getLangOpts().CUDAIsDevice) { 1020 // As CUDA builtin surface/texture types are replaced, skip generating TBAA 1021 // access info. 1022 if (AccessType->isCUDADeviceBuiltinSurfaceType()) { 1023 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() != 1024 nullptr) 1025 return TBAAAccessInfo(); 1026 } else if (AccessType->isCUDADeviceBuiltinTextureType()) { 1027 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() != 1028 nullptr) 1029 return TBAAAccessInfo(); 1030 } 1031 } 1032 return TBAA->getAccessInfo(AccessType); 1033 } 1034 1035 TBAAAccessInfo 1036 CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType) { 1037 if (!TBAA) 1038 return TBAAAccessInfo(); 1039 return TBAA->getVTablePtrAccessInfo(VTablePtrType); 1040 } 1041 1042 llvm::MDNode *CodeGenModule::getTBAAStructInfo(QualType QTy) { 1043 if (!TBAA) 1044 return nullptr; 1045 return TBAA->getTBAAStructInfo(QTy); 1046 } 1047 1048 llvm::MDNode *CodeGenModule::getTBAABaseTypeInfo(QualType QTy) { 1049 if (!TBAA) 1050 return nullptr; 1051 return TBAA->getBaseTypeInfo(QTy); 1052 } 1053 1054 llvm::MDNode *CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info) { 1055 if (!TBAA) 1056 return nullptr; 1057 return TBAA->getAccessTagInfo(Info); 1058 } 1059 1060 TBAAAccessInfo CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, 1061 TBAAAccessInfo TargetInfo) { 1062 if (!TBAA) 1063 return TBAAAccessInfo(); 1064 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo); 1065 } 1066 1067 TBAAAccessInfo 1068 CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, 1069 TBAAAccessInfo InfoB) { 1070 if (!TBAA) 1071 return TBAAAccessInfo(); 1072 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB); 1073 } 1074 1075 TBAAAccessInfo 1076 CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, 1077 TBAAAccessInfo SrcInfo) { 1078 if (!TBAA) 1079 return TBAAAccessInfo(); 1080 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo); 1081 } 1082 1083 void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst, 1084 TBAAAccessInfo TBAAInfo) { 1085 if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo)) 1086 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag); 1087 } 1088 1089 void CodeGenModule::DecorateInstructionWithInvariantGroup( 1090 llvm::Instruction *I, const CXXRecordDecl *RD) { 1091 I->setMetadata(llvm::LLVMContext::MD_invariant_group, 1092 llvm::MDNode::get(getLLVMContext(), {})); 1093 } 1094 1095 void CodeGenModule::Error(SourceLocation loc, StringRef message) { 1096 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0"); 1097 getDiags().Report(Context.getFullLoc(loc), diagID) << message; 1098 } 1099 1100 /// ErrorUnsupported - Print out an error that codegen doesn't support the 1101 /// specified stmt yet. 1102 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) { 1103 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 1104 "cannot compile this %0 yet"); 1105 std::string Msg = Type; 1106 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID) 1107 << Msg << S->getSourceRange(); 1108 } 1109 1110 /// ErrorUnsupported - Print out an error that codegen doesn't support the 1111 /// specified decl yet. 1112 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) { 1113 unsigned DiagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, 1114 "cannot compile this %0 yet"); 1115 std::string Msg = Type; 1116 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg; 1117 } 1118 1119 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) { 1120 return llvm::ConstantInt::get(SizeTy, size.getQuantity()); 1121 } 1122 1123 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV, 1124 const NamedDecl *D) const { 1125 // Internal definitions always have default visibility. 1126 if (GV->hasLocalLinkage()) { 1127 GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 1128 return; 1129 } 1130 if (!D) 1131 return; 1132 // Set visibility for definitions, and for declarations if requested globally 1133 // or set explicitly. 1134 LinkageInfo LV = D->getLinkageAndVisibility(); 1135 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) { 1136 // Reject incompatible dlllstorage and visibility annotations. 1137 if (!LV.isVisibilityExplicit()) 1138 return; 1139 if (GV->hasDLLExportStorageClass()) { 1140 if (LV.getVisibility() == HiddenVisibility) 1141 getDiags().Report(D->getLocation(), 1142 diag::err_hidden_visibility_dllexport); 1143 } else if (LV.getVisibility() != DefaultVisibility) { 1144 getDiags().Report(D->getLocation(), 1145 diag::err_non_default_visibility_dllimport); 1146 } 1147 return; 1148 } 1149 1150 if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls || 1151 !GV->isDeclarationForLinker()) 1152 GV->setVisibility(GetLLVMVisibility(LV.getVisibility())); 1153 } 1154 1155 static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, 1156 llvm::GlobalValue *GV) { 1157 if (GV->hasLocalLinkage()) 1158 return true; 1159 1160 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage()) 1161 return true; 1162 1163 // DLLImport explicitly marks the GV as external. 1164 if (GV->hasDLLImportStorageClass()) 1165 return false; 1166 1167 const llvm::Triple &TT = CGM.getTriple(); 1168 if (TT.isWindowsGNUEnvironment()) { 1169 // In MinGW, variables without DLLImport can still be automatically 1170 // imported from a DLL by the linker; don't mark variables that 1171 // potentially could come from another DLL as DSO local. 1172 1173 // With EmulatedTLS, TLS variables can be autoimported from other DLLs 1174 // (and this actually happens in the public interface of libstdc++), so 1175 // such variables can't be marked as DSO local. (Native TLS variables 1176 // can't be dllimported at all, though.) 1177 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) && 1178 (!GV->isThreadLocal() || CGM.getCodeGenOpts().EmulatedTLS)) 1179 return false; 1180 } 1181 1182 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols 1183 // remain unresolved in the link, they can be resolved to zero, which is 1184 // outside the current DSO. 1185 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage()) 1186 return false; 1187 1188 // Every other GV is local on COFF. 1189 // Make an exception for windows OS in the triple: Some firmware builds use 1190 // *-win32-macho triples. This (accidentally?) produced windows relocations 1191 // without GOT tables in older clang versions; Keep this behaviour. 1192 // FIXME: even thread local variables? 1193 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) 1194 return true; 1195 1196 // Only handle COFF and ELF for now. 1197 if (!TT.isOSBinFormatELF()) 1198 return false; 1199 1200 // If this is not an executable, don't assume anything is local. 1201 const auto &CGOpts = CGM.getCodeGenOpts(); 1202 llvm::Reloc::Model RM = CGOpts.RelocationModel; 1203 const auto &LOpts = CGM.getLangOpts(); 1204 if (RM != llvm::Reloc::Static && !LOpts.PIE) { 1205 // On ELF, if -fno-semantic-interposition is specified and the target 1206 // supports local aliases, there will be neither CC1 1207 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set 1208 // dso_local on the function if using a local alias is preferable (can avoid 1209 // PLT indirection). 1210 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias())) 1211 return false; 1212 return !(CGM.getLangOpts().SemanticInterposition || 1213 CGM.getLangOpts().HalfNoSemanticInterposition); 1214 } 1215 1216 // A definition cannot be preempted from an executable. 1217 if (!GV->isDeclarationForLinker()) 1218 return true; 1219 1220 // Most PIC code sequences that assume that a symbol is local cannot produce a 1221 // 0 if it turns out the symbol is undefined. While this is ABI and relocation 1222 // depended, it seems worth it to handle it here. 1223 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage()) 1224 return false; 1225 1226 // PowerPC64 prefers TOC indirection to avoid copy relocations. 1227 if (TT.isPPC64()) 1228 return false; 1229 1230 if (CGOpts.DirectAccessExternalData) { 1231 // If -fdirect-access-external-data (default for -fno-pic), set dso_local 1232 // for non-thread-local variables. If the symbol is not defined in the 1233 // executable, a copy relocation will be needed at link time. dso_local is 1234 // excluded for thread-local variables because they generally don't support 1235 // copy relocations. 1236 if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV)) 1237 if (!Var->isThreadLocal()) 1238 return true; 1239 1240 // -fno-pic sets dso_local on a function declaration to allow direct 1241 // accesses when taking its address (similar to a data symbol). If the 1242 // function is not defined in the executable, a canonical PLT entry will be 1243 // needed at link time. -fno-direct-access-external-data can avoid the 1244 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as 1245 // it could just cause trouble without providing perceptible benefits. 1246 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) 1247 return true; 1248 } 1249 1250 // If we can use copy relocations we can assume it is local. 1251 1252 // Otherwise don't assume it is local. 1253 return false; 1254 } 1255 1256 void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const { 1257 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV)); 1258 } 1259 1260 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, 1261 GlobalDecl GD) const { 1262 const auto *D = dyn_cast<NamedDecl>(GD.getDecl()); 1263 // C++ destructors have a few C++ ABI specific special cases. 1264 if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) { 1265 getCXXABI().setCXXDestructorDLLStorage(GV, Dtor, GD.getDtorType()); 1266 return; 1267 } 1268 setDLLImportDLLExport(GV, D); 1269 } 1270 1271 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV, 1272 const NamedDecl *D) const { 1273 if (D && D->isExternallyVisible()) { 1274 if (D->hasAttr<DLLImportAttr>()) 1275 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 1276 else if ((D->hasAttr<DLLExportAttr>() || 1277 shouldMapVisibilityToDLLExport(D)) && 1278 !GV->isDeclarationForLinker()) 1279 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); 1280 } 1281 } 1282 1283 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, 1284 GlobalDecl GD) const { 1285 setDLLImportDLLExport(GV, GD); 1286 setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl())); 1287 } 1288 1289 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV, 1290 const NamedDecl *D) const { 1291 setDLLImportDLLExport(GV, D); 1292 setGVPropertiesAux(GV, D); 1293 } 1294 1295 void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV, 1296 const NamedDecl *D) const { 1297 setGlobalVisibility(GV, D); 1298 setDSOLocal(GV); 1299 GV->setPartition(CodeGenOpts.SymbolPartition); 1300 } 1301 1302 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) { 1303 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S) 1304 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel) 1305 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel) 1306 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel) 1307 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel); 1308 } 1309 1310 llvm::GlobalVariable::ThreadLocalMode 1311 CodeGenModule::GetDefaultLLVMTLSModel() const { 1312 switch (CodeGenOpts.getDefaultTLSModel()) { 1313 case CodeGenOptions::GeneralDynamicTLSModel: 1314 return llvm::GlobalVariable::GeneralDynamicTLSModel; 1315 case CodeGenOptions::LocalDynamicTLSModel: 1316 return llvm::GlobalVariable::LocalDynamicTLSModel; 1317 case CodeGenOptions::InitialExecTLSModel: 1318 return llvm::GlobalVariable::InitialExecTLSModel; 1319 case CodeGenOptions::LocalExecTLSModel: 1320 return llvm::GlobalVariable::LocalExecTLSModel; 1321 } 1322 llvm_unreachable("Invalid TLS model!"); 1323 } 1324 1325 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const { 1326 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!"); 1327 1328 llvm::GlobalValue::ThreadLocalMode TLM; 1329 TLM = GetDefaultLLVMTLSModel(); 1330 1331 // Override the TLS model if it is explicitly specified. 1332 if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) { 1333 TLM = GetLLVMTLSModel(Attr->getModel()); 1334 } 1335 1336 GV->setThreadLocalMode(TLM); 1337 } 1338 1339 static std::string getCPUSpecificMangling(const CodeGenModule &CGM, 1340 StringRef Name) { 1341 const TargetInfo &Target = CGM.getTarget(); 1342 return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str(); 1343 } 1344 1345 static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM, 1346 const CPUSpecificAttr *Attr, 1347 unsigned CPUIndex, 1348 raw_ostream &Out) { 1349 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is 1350 // supported. 1351 if (Attr) 1352 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName()); 1353 else if (CGM.getTarget().supportsIFunc()) 1354 Out << ".resolver"; 1355 } 1356 1357 static void AppendTargetVersionMangling(const CodeGenModule &CGM, 1358 const TargetVersionAttr *Attr, 1359 raw_ostream &Out) { 1360 if (Attr->isDefaultVersion()) 1361 return; 1362 Out << "._"; 1363 llvm::SmallVector<StringRef, 8> Feats; 1364 Attr->getFeatures(Feats); 1365 for (const auto &Feat : Feats) { 1366 Out << 'M'; 1367 Out << Feat; 1368 } 1369 } 1370 1371 static void AppendTargetMangling(const CodeGenModule &CGM, 1372 const TargetAttr *Attr, raw_ostream &Out) { 1373 if (Attr->isDefaultVersion()) 1374 return; 1375 1376 Out << '.'; 1377 const TargetInfo &Target = CGM.getTarget(); 1378 ParsedTargetAttr Info = Target.parseTargetAttr(Attr->getFeaturesStr()); 1379 llvm::sort(Info.Features, [&Target](StringRef LHS, StringRef RHS) { 1380 // Multiversioning doesn't allow "no-${feature}", so we can 1381 // only have "+" prefixes here. 1382 assert(LHS.startswith("+") && RHS.startswith("+") && 1383 "Features should always have a prefix."); 1384 return Target.multiVersionSortPriority(LHS.substr(1)) > 1385 Target.multiVersionSortPriority(RHS.substr(1)); 1386 }); 1387 1388 bool IsFirst = true; 1389 1390 if (!Info.CPU.empty()) { 1391 IsFirst = false; 1392 Out << "arch_" << Info.CPU; 1393 } 1394 1395 for (StringRef Feat : Info.Features) { 1396 if (!IsFirst) 1397 Out << '_'; 1398 IsFirst = false; 1399 Out << Feat.substr(1); 1400 } 1401 } 1402 1403 // Returns true if GD is a function decl with internal linkage and 1404 // needs a unique suffix after the mangled name. 1405 static bool isUniqueInternalLinkageDecl(GlobalDecl GD, 1406 CodeGenModule &CGM) { 1407 const Decl *D = GD.getDecl(); 1408 return !CGM.getModuleNameHash().empty() && isa<FunctionDecl>(D) && 1409 (CGM.getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage); 1410 } 1411 1412 static void AppendTargetClonesMangling(const CodeGenModule &CGM, 1413 const TargetClonesAttr *Attr, 1414 unsigned VersionIndex, 1415 raw_ostream &Out) { 1416 if (CGM.getTarget().getTriple().isAArch64()) { 1417 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex); 1418 if (FeatureStr == "default") 1419 return; 1420 Out << "._"; 1421 SmallVector<StringRef, 8> Features; 1422 FeatureStr.split(Features, "+"); 1423 for (auto &Feat : Features) { 1424 Out << 'M'; 1425 Out << Feat; 1426 } 1427 } else { 1428 Out << '.'; 1429 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex); 1430 if (FeatureStr.startswith("arch=")) 1431 Out << "arch_" << FeatureStr.substr(sizeof("arch=") - 1); 1432 else 1433 Out << FeatureStr; 1434 1435 Out << '.' << Attr->getMangledIndex(VersionIndex); 1436 } 1437 } 1438 1439 static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD, 1440 const NamedDecl *ND, 1441 bool OmitMultiVersionMangling = false) { 1442 SmallString<256> Buffer; 1443 llvm::raw_svector_ostream Out(Buffer); 1444 MangleContext &MC = CGM.getCXXABI().getMangleContext(); 1445 if (!CGM.getModuleNameHash().empty()) 1446 MC.needsUniqueInternalLinkageNames(); 1447 bool ShouldMangle = MC.shouldMangleDeclName(ND); 1448 if (ShouldMangle) 1449 MC.mangleName(GD.getWithDecl(ND), Out); 1450 else { 1451 IdentifierInfo *II = ND->getIdentifier(); 1452 assert(II && "Attempt to mangle unnamed decl."); 1453 const auto *FD = dyn_cast<FunctionDecl>(ND); 1454 1455 if (FD && 1456 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) { 1457 Out << "__regcall3__" << II->getName(); 1458 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() && 1459 GD.getKernelReferenceKind() == KernelReferenceKind::Stub) { 1460 Out << "__device_stub__" << II->getName(); 1461 } else { 1462 Out << II->getName(); 1463 } 1464 } 1465 1466 // Check if the module name hash should be appended for internal linkage 1467 // symbols. This should come before multi-version target suffixes are 1468 // appended. This is to keep the name and module hash suffix of the 1469 // internal linkage function together. The unique suffix should only be 1470 // added when name mangling is done to make sure that the final name can 1471 // be properly demangled. For example, for C functions without prototypes, 1472 // name mangling is not done and the unique suffix should not be appeneded 1473 // then. 1474 if (ShouldMangle && isUniqueInternalLinkageDecl(GD, CGM)) { 1475 assert(CGM.getCodeGenOpts().UniqueInternalLinkageNames && 1476 "Hash computed when not explicitly requested"); 1477 Out << CGM.getModuleNameHash(); 1478 } 1479 1480 if (const auto *FD = dyn_cast<FunctionDecl>(ND)) 1481 if (FD->isMultiVersion() && !OmitMultiVersionMangling) { 1482 switch (FD->getMultiVersionKind()) { 1483 case MultiVersionKind::CPUDispatch: 1484 case MultiVersionKind::CPUSpecific: 1485 AppendCPUSpecificCPUDispatchMangling(CGM, 1486 FD->getAttr<CPUSpecificAttr>(), 1487 GD.getMultiVersionIndex(), Out); 1488 break; 1489 case MultiVersionKind::Target: 1490 AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out); 1491 break; 1492 case MultiVersionKind::TargetVersion: 1493 AppendTargetVersionMangling(CGM, FD->getAttr<TargetVersionAttr>(), Out); 1494 break; 1495 case MultiVersionKind::TargetClones: 1496 AppendTargetClonesMangling(CGM, FD->getAttr<TargetClonesAttr>(), 1497 GD.getMultiVersionIndex(), Out); 1498 break; 1499 case MultiVersionKind::None: 1500 llvm_unreachable("None multiversion type isn't valid here"); 1501 } 1502 } 1503 1504 // Make unique name for device side static file-scope variable for HIP. 1505 if (CGM.getContext().shouldExternalize(ND) && 1506 CGM.getLangOpts().GPURelocatableDeviceCode && 1507 CGM.getLangOpts().CUDAIsDevice) 1508 CGM.printPostfixForExternalizedDecl(Out, ND); 1509 1510 return std::string(Out.str()); 1511 } 1512 1513 void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD, 1514 const FunctionDecl *FD, 1515 StringRef &CurName) { 1516 if (!FD->isMultiVersion()) 1517 return; 1518 1519 // Get the name of what this would be without the 'target' attribute. This 1520 // allows us to lookup the version that was emitted when this wasn't a 1521 // multiversion function. 1522 std::string NonTargetName = 1523 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true); 1524 GlobalDecl OtherGD; 1525 if (lookupRepresentativeDecl(NonTargetName, OtherGD)) { 1526 assert(OtherGD.getCanonicalDecl() 1527 .getDecl() 1528 ->getAsFunction() 1529 ->isMultiVersion() && 1530 "Other GD should now be a multiversioned function"); 1531 // OtherFD is the version of this function that was mangled BEFORE 1532 // becoming a MultiVersion function. It potentially needs to be updated. 1533 const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl() 1534 .getDecl() 1535 ->getAsFunction() 1536 ->getMostRecentDecl(); 1537 std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD); 1538 // This is so that if the initial version was already the 'default' 1539 // version, we don't try to update it. 1540 if (OtherName != NonTargetName) { 1541 // Remove instead of erase, since others may have stored the StringRef 1542 // to this. 1543 const auto ExistingRecord = Manglings.find(NonTargetName); 1544 if (ExistingRecord != std::end(Manglings)) 1545 Manglings.remove(&(*ExistingRecord)); 1546 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD)); 1547 StringRef OtherNameRef = MangledDeclNames[OtherGD.getCanonicalDecl()] = 1548 Result.first->first(); 1549 // If this is the current decl is being created, make sure we update the name. 1550 if (GD.getCanonicalDecl() == OtherGD.getCanonicalDecl()) 1551 CurName = OtherNameRef; 1552 if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName)) 1553 Entry->setName(OtherName); 1554 } 1555 } 1556 } 1557 1558 StringRef CodeGenModule::getMangledName(GlobalDecl GD) { 1559 GlobalDecl CanonicalGD = GD.getCanonicalDecl(); 1560 1561 // Some ABIs don't have constructor variants. Make sure that base and 1562 // complete constructors get mangled the same. 1563 if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) { 1564 if (!getTarget().getCXXABI().hasConstructorVariants()) { 1565 CXXCtorType OrigCtorType = GD.getCtorType(); 1566 assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete); 1567 if (OrigCtorType == Ctor_Base) 1568 CanonicalGD = GlobalDecl(CD, Ctor_Complete); 1569 } 1570 } 1571 1572 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a 1573 // static device variable depends on whether the variable is referenced by 1574 // a host or device host function. Therefore the mangled name cannot be 1575 // cached. 1576 if (!LangOpts.CUDAIsDevice || !getContext().mayExternalize(GD.getDecl())) { 1577 auto FoundName = MangledDeclNames.find(CanonicalGD); 1578 if (FoundName != MangledDeclNames.end()) 1579 return FoundName->second; 1580 } 1581 1582 // Keep the first result in the case of a mangling collision. 1583 const auto *ND = cast<NamedDecl>(GD.getDecl()); 1584 std::string MangledName = getMangledNameImpl(*this, GD, ND); 1585 1586 // Ensure either we have different ABIs between host and device compilations, 1587 // says host compilation following MSVC ABI but device compilation follows 1588 // Itanium C++ ABI or, if they follow the same ABI, kernel names after 1589 // mangling should be the same after name stubbing. The later checking is 1590 // very important as the device kernel name being mangled in host-compilation 1591 // is used to resolve the device binaries to be executed. Inconsistent naming 1592 // result in undefined behavior. Even though we cannot check that naming 1593 // directly between host- and device-compilations, the host- and 1594 // device-mangling in host compilation could help catching certain ones. 1595 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() || 1596 getContext().shouldExternalize(ND) || getLangOpts().CUDAIsDevice || 1597 (getContext().getAuxTargetInfo() && 1598 (getContext().getAuxTargetInfo()->getCXXABI() != 1599 getContext().getTargetInfo().getCXXABI())) || 1600 getCUDARuntime().getDeviceSideName(ND) == 1601 getMangledNameImpl( 1602 *this, 1603 GD.getWithKernelReferenceKind(KernelReferenceKind::Kernel), 1604 ND)); 1605 1606 auto Result = Manglings.insert(std::make_pair(MangledName, GD)); 1607 return MangledDeclNames[CanonicalGD] = Result.first->first(); 1608 } 1609 1610 StringRef CodeGenModule::getBlockMangledName(GlobalDecl GD, 1611 const BlockDecl *BD) { 1612 MangleContext &MangleCtx = getCXXABI().getMangleContext(); 1613 const Decl *D = GD.getDecl(); 1614 1615 SmallString<256> Buffer; 1616 llvm::raw_svector_ostream Out(Buffer); 1617 if (!D) 1618 MangleCtx.mangleGlobalBlock(BD, 1619 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out); 1620 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D)) 1621 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out); 1622 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) 1623 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out); 1624 else 1625 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out); 1626 1627 auto Result = Manglings.insert(std::make_pair(Out.str(), BD)); 1628 return Result.first->first(); 1629 } 1630 1631 const GlobalDecl CodeGenModule::getMangledNameDecl(StringRef Name) { 1632 auto it = MangledDeclNames.begin(); 1633 while (it != MangledDeclNames.end()) { 1634 if (it->second == Name) 1635 return it->first; 1636 it++; 1637 } 1638 return GlobalDecl(); 1639 } 1640 1641 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) { 1642 return getModule().getNamedValue(Name); 1643 } 1644 1645 /// AddGlobalCtor - Add a function to the list that will be called before 1646 /// main() runs. 1647 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority, 1648 unsigned LexOrder, 1649 llvm::Constant *AssociatedData) { 1650 // FIXME: Type coercion of void()* types. 1651 GlobalCtors.push_back(Structor(Priority, LexOrder, Ctor, AssociatedData)); 1652 } 1653 1654 /// AddGlobalDtor - Add a function to the list that will be called 1655 /// when the module is unloaded. 1656 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority, 1657 bool IsDtorAttrFunc) { 1658 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit && 1659 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc)) { 1660 DtorsUsingAtExit[Priority].push_back(Dtor); 1661 return; 1662 } 1663 1664 // FIXME: Type coercion of void()* types. 1665 GlobalDtors.push_back(Structor(Priority, ~0U, Dtor, nullptr)); 1666 } 1667 1668 void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) { 1669 if (Fns.empty()) return; 1670 1671 // Ctor function type is void()*. 1672 llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false); 1673 llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy, 1674 TheModule.getDataLayout().getProgramAddressSpace()); 1675 1676 // Get the type of a ctor entry, { i32, void ()*, i8* }. 1677 llvm::StructType *CtorStructTy = llvm::StructType::get( 1678 Int32Ty, CtorPFTy, VoidPtrTy); 1679 1680 // Construct the constructor and destructor arrays. 1681 ConstantInitBuilder builder(*this); 1682 auto ctors = builder.beginArray(CtorStructTy); 1683 for (const auto &I : Fns) { 1684 auto ctor = ctors.beginStruct(CtorStructTy); 1685 ctor.addInt(Int32Ty, I.Priority); 1686 ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy)); 1687 if (I.AssociatedData) 1688 ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy)); 1689 else 1690 ctor.addNullPointer(VoidPtrTy); 1691 ctor.finishAndAddTo(ctors); 1692 } 1693 1694 auto list = 1695 ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(), 1696 /*constant*/ false, 1697 llvm::GlobalValue::AppendingLinkage); 1698 1699 // The LTO linker doesn't seem to like it when we set an alignment 1700 // on appending variables. Take it off as a workaround. 1701 list->setAlignment(std::nullopt); 1702 1703 Fns.clear(); 1704 } 1705 1706 llvm::GlobalValue::LinkageTypes 1707 CodeGenModule::getFunctionLinkage(GlobalDecl GD) { 1708 const auto *D = cast<FunctionDecl>(GD.getDecl()); 1709 1710 GVALinkage Linkage = getContext().GetGVALinkageForFunction(D); 1711 1712 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D)) 1713 return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType()); 1714 1715 if (isa<CXXConstructorDecl>(D) && 1716 cast<CXXConstructorDecl>(D)->isInheritingConstructor() && 1717 Context.getTargetInfo().getCXXABI().isMicrosoft()) { 1718 // Our approach to inheriting constructors is fundamentally different from 1719 // that used by the MS ABI, so keep our inheriting constructor thunks 1720 // internal rather than trying to pick an unambiguous mangling for them. 1721 return llvm::GlobalValue::InternalLinkage; 1722 } 1723 1724 return getLLVMLinkageForDeclarator(D, Linkage, /*IsConstantVariable=*/false); 1725 } 1726 1727 llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) { 1728 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD); 1729 if (!MDS) return nullptr; 1730 1731 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString())); 1732 } 1733 1734 llvm::ConstantInt *CodeGenModule::CreateKCFITypeId(QualType T) { 1735 if (auto *FnType = T->getAs<FunctionProtoType>()) 1736 T = getContext().getFunctionType( 1737 FnType->getReturnType(), FnType->getParamTypes(), 1738 FnType->getExtProtoInfo().withExceptionSpec(EST_None)); 1739 1740 std::string OutName; 1741 llvm::raw_string_ostream Out(OutName); 1742 getCXXABI().getMangleContext().mangleTypeName( 1743 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers); 1744 1745 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers) 1746 Out << ".normalized"; 1747 1748 return llvm::ConstantInt::get(Int32Ty, 1749 static_cast<uint32_t>(llvm::xxHash64(OutName))); 1750 } 1751 1752 void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD, 1753 const CGFunctionInfo &Info, 1754 llvm::Function *F, bool IsThunk) { 1755 unsigned CallingConv; 1756 llvm::AttributeList PAL; 1757 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, 1758 /*AttrOnCallSite=*/false, IsThunk); 1759 F->setAttributes(PAL); 1760 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 1761 } 1762 1763 static void removeImageAccessQualifier(std::string& TyName) { 1764 std::string ReadOnlyQual("__read_only"); 1765 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual); 1766 if (ReadOnlyPos != std::string::npos) 1767 // "+ 1" for the space after access qualifier. 1768 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1); 1769 else { 1770 std::string WriteOnlyQual("__write_only"); 1771 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual); 1772 if (WriteOnlyPos != std::string::npos) 1773 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1); 1774 else { 1775 std::string ReadWriteQual("__read_write"); 1776 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual); 1777 if (ReadWritePos != std::string::npos) 1778 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1); 1779 } 1780 } 1781 } 1782 1783 // Returns the address space id that should be produced to the 1784 // kernel_arg_addr_space metadata. This is always fixed to the ids 1785 // as specified in the SPIR 2.0 specification in order to differentiate 1786 // for example in clGetKernelArgInfo() implementation between the address 1787 // spaces with targets without unique mapping to the OpenCL address spaces 1788 // (basically all single AS CPUs). 1789 static unsigned ArgInfoAddressSpace(LangAS AS) { 1790 switch (AS) { 1791 case LangAS::opencl_global: 1792 return 1; 1793 case LangAS::opencl_constant: 1794 return 2; 1795 case LangAS::opencl_local: 1796 return 3; 1797 case LangAS::opencl_generic: 1798 return 4; // Not in SPIR 2.0 specs. 1799 case LangAS::opencl_global_device: 1800 return 5; 1801 case LangAS::opencl_global_host: 1802 return 6; 1803 default: 1804 return 0; // Assume private. 1805 } 1806 } 1807 1808 void CodeGenModule::GenKernelArgMetadata(llvm::Function *Fn, 1809 const FunctionDecl *FD, 1810 CodeGenFunction *CGF) { 1811 assert(((FD && CGF) || (!FD && !CGF)) && 1812 "Incorrect use - FD and CGF should either be both null or not!"); 1813 // Create MDNodes that represent the kernel arg metadata. 1814 // Each MDNode is a list in the form of "key", N number of values which is 1815 // the same number of values as their are kernel arguments. 1816 1817 const PrintingPolicy &Policy = Context.getPrintingPolicy(); 1818 1819 // MDNode for the kernel argument address space qualifiers. 1820 SmallVector<llvm::Metadata *, 8> addressQuals; 1821 1822 // MDNode for the kernel argument access qualifiers (images only). 1823 SmallVector<llvm::Metadata *, 8> accessQuals; 1824 1825 // MDNode for the kernel argument type names. 1826 SmallVector<llvm::Metadata *, 8> argTypeNames; 1827 1828 // MDNode for the kernel argument base type names. 1829 SmallVector<llvm::Metadata *, 8> argBaseTypeNames; 1830 1831 // MDNode for the kernel argument type qualifiers. 1832 SmallVector<llvm::Metadata *, 8> argTypeQuals; 1833 1834 // MDNode for the kernel argument names. 1835 SmallVector<llvm::Metadata *, 8> argNames; 1836 1837 if (FD && CGF) 1838 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { 1839 const ParmVarDecl *parm = FD->getParamDecl(i); 1840 // Get argument name. 1841 argNames.push_back(llvm::MDString::get(VMContext, parm->getName())); 1842 1843 if (!getLangOpts().OpenCL) 1844 continue; 1845 QualType ty = parm->getType(); 1846 std::string typeQuals; 1847 1848 // Get image and pipe access qualifier: 1849 if (ty->isImageType() || ty->isPipeType()) { 1850 const Decl *PDecl = parm; 1851 if (const auto *TD = ty->getAs<TypedefType>()) 1852 PDecl = TD->getDecl(); 1853 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>(); 1854 if (A && A->isWriteOnly()) 1855 accessQuals.push_back(llvm::MDString::get(VMContext, "write_only")); 1856 else if (A && A->isReadWrite()) 1857 accessQuals.push_back(llvm::MDString::get(VMContext, "read_write")); 1858 else 1859 accessQuals.push_back(llvm::MDString::get(VMContext, "read_only")); 1860 } else 1861 accessQuals.push_back(llvm::MDString::get(VMContext, "none")); 1862 1863 auto getTypeSpelling = [&](QualType Ty) { 1864 auto typeName = Ty.getUnqualifiedType().getAsString(Policy); 1865 1866 if (Ty.isCanonical()) { 1867 StringRef typeNameRef = typeName; 1868 // Turn "unsigned type" to "utype" 1869 if (typeNameRef.consume_front("unsigned ")) 1870 return std::string("u") + typeNameRef.str(); 1871 if (typeNameRef.consume_front("signed ")) 1872 return typeNameRef.str(); 1873 } 1874 1875 return typeName; 1876 }; 1877 1878 if (ty->isPointerType()) { 1879 QualType pointeeTy = ty->getPointeeType(); 1880 1881 // Get address qualifier. 1882 addressQuals.push_back( 1883 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32( 1884 ArgInfoAddressSpace(pointeeTy.getAddressSpace())))); 1885 1886 // Get argument type name. 1887 std::string typeName = getTypeSpelling(pointeeTy) + "*"; 1888 std::string baseTypeName = 1889 getTypeSpelling(pointeeTy.getCanonicalType()) + "*"; 1890 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName)); 1891 argBaseTypeNames.push_back( 1892 llvm::MDString::get(VMContext, baseTypeName)); 1893 1894 // Get argument type qualifiers: 1895 if (ty.isRestrictQualified()) 1896 typeQuals = "restrict"; 1897 if (pointeeTy.isConstQualified() || 1898 (pointeeTy.getAddressSpace() == LangAS::opencl_constant)) 1899 typeQuals += typeQuals.empty() ? "const" : " const"; 1900 if (pointeeTy.isVolatileQualified()) 1901 typeQuals += typeQuals.empty() ? "volatile" : " volatile"; 1902 } else { 1903 uint32_t AddrSpc = 0; 1904 bool isPipe = ty->isPipeType(); 1905 if (ty->isImageType() || isPipe) 1906 AddrSpc = ArgInfoAddressSpace(LangAS::opencl_global); 1907 1908 addressQuals.push_back( 1909 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc))); 1910 1911 // Get argument type name. 1912 ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty; 1913 std::string typeName = getTypeSpelling(ty); 1914 std::string baseTypeName = getTypeSpelling(ty.getCanonicalType()); 1915 1916 // Remove access qualifiers on images 1917 // (as they are inseparable from type in clang implementation, 1918 // but OpenCL spec provides a special query to get access qualifier 1919 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER): 1920 if (ty->isImageType()) { 1921 removeImageAccessQualifier(typeName); 1922 removeImageAccessQualifier(baseTypeName); 1923 } 1924 1925 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName)); 1926 argBaseTypeNames.push_back( 1927 llvm::MDString::get(VMContext, baseTypeName)); 1928 1929 if (isPipe) 1930 typeQuals = "pipe"; 1931 } 1932 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals)); 1933 } 1934 1935 if (getLangOpts().OpenCL) { 1936 Fn->setMetadata("kernel_arg_addr_space", 1937 llvm::MDNode::get(VMContext, addressQuals)); 1938 Fn->setMetadata("kernel_arg_access_qual", 1939 llvm::MDNode::get(VMContext, accessQuals)); 1940 Fn->setMetadata("kernel_arg_type", 1941 llvm::MDNode::get(VMContext, argTypeNames)); 1942 Fn->setMetadata("kernel_arg_base_type", 1943 llvm::MDNode::get(VMContext, argBaseTypeNames)); 1944 Fn->setMetadata("kernel_arg_type_qual", 1945 llvm::MDNode::get(VMContext, argTypeQuals)); 1946 } 1947 if (getCodeGenOpts().EmitOpenCLArgMetadata || 1948 getCodeGenOpts().HIPSaveKernelArgName) 1949 Fn->setMetadata("kernel_arg_name", 1950 llvm::MDNode::get(VMContext, argNames)); 1951 } 1952 1953 /// Determines whether the language options require us to model 1954 /// unwind exceptions. We treat -fexceptions as mandating this 1955 /// except under the fragile ObjC ABI with only ObjC exceptions 1956 /// enabled. This means, for example, that C with -fexceptions 1957 /// enables this. 1958 static bool hasUnwindExceptions(const LangOptions &LangOpts) { 1959 // If exceptions are completely disabled, obviously this is false. 1960 if (!LangOpts.Exceptions) return false; 1961 1962 // If C++ exceptions are enabled, this is true. 1963 if (LangOpts.CXXExceptions) return true; 1964 1965 // If ObjC exceptions are enabled, this depends on the ABI. 1966 if (LangOpts.ObjCExceptions) { 1967 return LangOpts.ObjCRuntime.hasUnwindExceptions(); 1968 } 1969 1970 return true; 1971 } 1972 1973 static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM, 1974 const CXXMethodDecl *MD) { 1975 // Check that the type metadata can ever actually be used by a call. 1976 if (!CGM.getCodeGenOpts().LTOUnit || 1977 !CGM.HasHiddenLTOVisibility(MD->getParent())) 1978 return false; 1979 1980 // Only functions whose address can be taken with a member function pointer 1981 // need this sort of type metadata. 1982 return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) && 1983 !isa<CXXDestructorDecl>(MD); 1984 } 1985 1986 std::vector<const CXXRecordDecl *> 1987 CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) { 1988 llvm::SetVector<const CXXRecordDecl *> MostBases; 1989 1990 std::function<void (const CXXRecordDecl *)> CollectMostBases; 1991 CollectMostBases = [&](const CXXRecordDecl *RD) { 1992 if (RD->getNumBases() == 0) 1993 MostBases.insert(RD); 1994 for (const CXXBaseSpecifier &B : RD->bases()) 1995 CollectMostBases(B.getType()->getAsCXXRecordDecl()); 1996 }; 1997 CollectMostBases(RD); 1998 return MostBases.takeVector(); 1999 } 2000 2001 llvm::GlobalVariable * 2002 CodeGenModule::GetOrCreateRTTIProxyGlobalVariable(llvm::Constant *Addr) { 2003 auto It = RTTIProxyMap.find(Addr); 2004 if (It != RTTIProxyMap.end()) 2005 return It->second; 2006 2007 auto *FTRTTIProxy = new llvm::GlobalVariable( 2008 TheModule, Addr->getType(), 2009 /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Addr, 2010 "__llvm_rtti_proxy"); 2011 FTRTTIProxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2012 2013 RTTIProxyMap[Addr] = FTRTTIProxy; 2014 return FTRTTIProxy; 2015 } 2016 2017 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D, 2018 llvm::Function *F) { 2019 llvm::AttrBuilder B(F->getContext()); 2020 2021 if ((!D || !D->hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables) 2022 B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables)); 2023 2024 if (CodeGenOpts.StackClashProtector) 2025 B.addAttribute("probe-stack", "inline-asm"); 2026 2027 if (!hasUnwindExceptions(LangOpts)) 2028 B.addAttribute(llvm::Attribute::NoUnwind); 2029 2030 if (D && D->hasAttr<NoStackProtectorAttr>()) 2031 ; // Do nothing. 2032 else if (D && D->hasAttr<StrictGuardStackCheckAttr>() && 2033 LangOpts.getStackProtector() == LangOptions::SSPOn) 2034 B.addAttribute(llvm::Attribute::StackProtectStrong); 2035 else if (LangOpts.getStackProtector() == LangOptions::SSPOn) 2036 B.addAttribute(llvm::Attribute::StackProtect); 2037 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) 2038 B.addAttribute(llvm::Attribute::StackProtectStrong); 2039 else if (LangOpts.getStackProtector() == LangOptions::SSPReq) 2040 B.addAttribute(llvm::Attribute::StackProtectReq); 2041 2042 if (!D) { 2043 // If we don't have a declaration to control inlining, the function isn't 2044 // explicitly marked as alwaysinline for semantic reasons, and inlining is 2045 // disabled, mark the function as noinline. 2046 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) && 2047 CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) 2048 B.addAttribute(llvm::Attribute::NoInline); 2049 2050 F->addFnAttrs(B); 2051 return; 2052 } 2053 2054 // Track whether we need to add the optnone LLVM attribute, 2055 // starting with the default for this optimization level. 2056 bool ShouldAddOptNone = 2057 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0; 2058 // We can't add optnone in the following cases, it won't pass the verifier. 2059 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>(); 2060 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>(); 2061 2062 // Add optnone, but do so only if the function isn't always_inline. 2063 if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) && 2064 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { 2065 B.addAttribute(llvm::Attribute::OptimizeNone); 2066 2067 // OptimizeNone implies noinline; we should not be inlining such functions. 2068 B.addAttribute(llvm::Attribute::NoInline); 2069 2070 // We still need to handle naked functions even though optnone subsumes 2071 // much of their semantics. 2072 if (D->hasAttr<NakedAttr>()) 2073 B.addAttribute(llvm::Attribute::Naked); 2074 2075 // OptimizeNone wins over OptimizeForSize and MinSize. 2076 F->removeFnAttr(llvm::Attribute::OptimizeForSize); 2077 F->removeFnAttr(llvm::Attribute::MinSize); 2078 } else if (D->hasAttr<NakedAttr>()) { 2079 // Naked implies noinline: we should not be inlining such functions. 2080 B.addAttribute(llvm::Attribute::Naked); 2081 B.addAttribute(llvm::Attribute::NoInline); 2082 } else if (D->hasAttr<NoDuplicateAttr>()) { 2083 B.addAttribute(llvm::Attribute::NoDuplicate); 2084 } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { 2085 // Add noinline if the function isn't always_inline. 2086 B.addAttribute(llvm::Attribute::NoInline); 2087 } else if (D->hasAttr<AlwaysInlineAttr>() && 2088 !F->hasFnAttribute(llvm::Attribute::NoInline)) { 2089 // (noinline wins over always_inline, and we can't specify both in IR) 2090 B.addAttribute(llvm::Attribute::AlwaysInline); 2091 } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) { 2092 // If we're not inlining, then force everything that isn't always_inline to 2093 // carry an explicit noinline attribute. 2094 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline)) 2095 B.addAttribute(llvm::Attribute::NoInline); 2096 } else { 2097 // Otherwise, propagate the inline hint attribute and potentially use its 2098 // absence to mark things as noinline. 2099 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 2100 // Search function and template pattern redeclarations for inline. 2101 auto CheckForInline = [](const FunctionDecl *FD) { 2102 auto CheckRedeclForInline = [](const FunctionDecl *Redecl) { 2103 return Redecl->isInlineSpecified(); 2104 }; 2105 if (any_of(FD->redecls(), CheckRedeclForInline)) 2106 return true; 2107 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern(); 2108 if (!Pattern) 2109 return false; 2110 return any_of(Pattern->redecls(), CheckRedeclForInline); 2111 }; 2112 if (CheckForInline(FD)) { 2113 B.addAttribute(llvm::Attribute::InlineHint); 2114 } else if (CodeGenOpts.getInlining() == 2115 CodeGenOptions::OnlyHintInlining && 2116 !FD->isInlined() && 2117 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) { 2118 B.addAttribute(llvm::Attribute::NoInline); 2119 } 2120 } 2121 } 2122 2123 // Add other optimization related attributes if we are optimizing this 2124 // function. 2125 if (!D->hasAttr<OptimizeNoneAttr>()) { 2126 if (D->hasAttr<ColdAttr>()) { 2127 if (!ShouldAddOptNone) 2128 B.addAttribute(llvm::Attribute::OptimizeForSize); 2129 B.addAttribute(llvm::Attribute::Cold); 2130 } 2131 if (D->hasAttr<HotAttr>()) 2132 B.addAttribute(llvm::Attribute::Hot); 2133 if (D->hasAttr<MinSizeAttr>()) 2134 B.addAttribute(llvm::Attribute::MinSize); 2135 } 2136 2137 F->addFnAttrs(B); 2138 2139 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); 2140 if (alignment) 2141 F->setAlignment(llvm::Align(alignment)); 2142 2143 if (!D->hasAttr<AlignedAttr>()) 2144 if (LangOpts.FunctionAlignment) 2145 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment)); 2146 2147 // Some C++ ABIs require 2-byte alignment for member functions, in order to 2148 // reserve a bit for differentiating between virtual and non-virtual member 2149 // functions. If the current target's C++ ABI requires this and this is a 2150 // member function, set its alignment accordingly. 2151 if (getTarget().getCXXABI().areMemberFunctionsAligned()) { 2152 if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D)) 2153 F->setAlignment(llvm::Align(2)); 2154 } 2155 2156 // In the cross-dso CFI mode with canonical jump tables, we want !type 2157 // attributes on definitions only. 2158 if (CodeGenOpts.SanitizeCfiCrossDso && 2159 CodeGenOpts.SanitizeCfiCanonicalJumpTables) { 2160 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 2161 // Skip available_externally functions. They won't be codegen'ed in the 2162 // current module anyway. 2163 if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally) 2164 CreateFunctionTypeMetadataForIcall(FD, F); 2165 } 2166 } 2167 2168 // Emit type metadata on member functions for member function pointer checks. 2169 // These are only ever necessary on definitions; we're guaranteed that the 2170 // definition will be present in the LTO unit as a result of LTO visibility. 2171 auto *MD = dyn_cast<CXXMethodDecl>(D); 2172 if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) { 2173 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) { 2174 llvm::Metadata *Id = 2175 CreateMetadataIdentifierForType(Context.getMemberPointerType( 2176 MD->getType(), Context.getRecordType(Base).getTypePtr())); 2177 F->addTypeMetadata(0, Id); 2178 } 2179 } 2180 } 2181 2182 void CodeGenModule::setLLVMFunctionFEnvAttributes(const FunctionDecl *D, 2183 llvm::Function *F) { 2184 if (D->hasAttr<StrictFPAttr>()) { 2185 llvm::AttrBuilder FuncAttrs(F->getContext()); 2186 FuncAttrs.addAttribute("strictfp"); 2187 F->addFnAttrs(FuncAttrs); 2188 } 2189 } 2190 2191 void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) { 2192 const Decl *D = GD.getDecl(); 2193 if (isa_and_nonnull<NamedDecl>(D)) 2194 setGVProperties(GV, GD); 2195 else 2196 GV->setVisibility(llvm::GlobalValue::DefaultVisibility); 2197 2198 if (D && D->hasAttr<UsedAttr>()) 2199 addUsedOrCompilerUsedGlobal(GV); 2200 2201 if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) { 2202 const auto *VD = cast<VarDecl>(D); 2203 if (VD->getType().isConstQualified() && 2204 VD->getStorageDuration() == SD_Static) 2205 addUsedOrCompilerUsedGlobal(GV); 2206 } 2207 } 2208 2209 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD, 2210 llvm::AttrBuilder &Attrs) { 2211 // Add target-cpu and target-features attributes to functions. If 2212 // we have a decl for the function and it has a target attribute then 2213 // parse that and add it to the feature set. 2214 StringRef TargetCPU = getTarget().getTargetOpts().CPU; 2215 StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU; 2216 std::vector<std::string> Features; 2217 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl()); 2218 FD = FD ? FD->getMostRecentDecl() : FD; 2219 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr; 2220 const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr; 2221 assert((!TD || !TV) && "both target_version and target specified"); 2222 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr; 2223 const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr; 2224 bool AddedAttr = false; 2225 if (TD || TV || SD || TC) { 2226 llvm::StringMap<bool> FeatureMap; 2227 getContext().getFunctionFeatureMap(FeatureMap, GD); 2228 2229 // Produce the canonical string for this set of features. 2230 for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap) 2231 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str()); 2232 2233 // Now add the target-cpu and target-features to the function. 2234 // While we populated the feature map above, we still need to 2235 // get and parse the target attribute so we can get the cpu for 2236 // the function. 2237 if (TD) { 2238 ParsedTargetAttr ParsedAttr = 2239 Target.parseTargetAttr(TD->getFeaturesStr()); 2240 if (!ParsedAttr.CPU.empty() && 2241 getTarget().isValidCPUName(ParsedAttr.CPU)) { 2242 TargetCPU = ParsedAttr.CPU; 2243 TuneCPU = ""; // Clear the tune CPU. 2244 } 2245 if (!ParsedAttr.Tune.empty() && 2246 getTarget().isValidCPUName(ParsedAttr.Tune)) 2247 TuneCPU = ParsedAttr.Tune; 2248 } 2249 2250 if (SD) { 2251 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can 2252 // favor this processor. 2253 TuneCPU = getTarget().getCPUSpecificTuneName( 2254 SD->getCPUName(GD.getMultiVersionIndex())->getName()); 2255 } 2256 } else { 2257 // Otherwise just add the existing target cpu and target features to the 2258 // function. 2259 Features = getTarget().getTargetOpts().Features; 2260 } 2261 2262 if (!TargetCPU.empty()) { 2263 Attrs.addAttribute("target-cpu", TargetCPU); 2264 AddedAttr = true; 2265 } 2266 if (!TuneCPU.empty()) { 2267 Attrs.addAttribute("tune-cpu", TuneCPU); 2268 AddedAttr = true; 2269 } 2270 if (!Features.empty()) { 2271 llvm::sort(Features); 2272 Attrs.addAttribute("target-features", llvm::join(Features, ",")); 2273 AddedAttr = true; 2274 } 2275 2276 return AddedAttr; 2277 } 2278 2279 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD, 2280 llvm::GlobalObject *GO) { 2281 const Decl *D = GD.getDecl(); 2282 SetCommonAttributes(GD, GO); 2283 2284 if (D) { 2285 if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) { 2286 if (D->hasAttr<RetainAttr>()) 2287 addUsedGlobal(GV); 2288 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>()) 2289 GV->addAttribute("bss-section", SA->getName()); 2290 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>()) 2291 GV->addAttribute("data-section", SA->getName()); 2292 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>()) 2293 GV->addAttribute("rodata-section", SA->getName()); 2294 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>()) 2295 GV->addAttribute("relro-section", SA->getName()); 2296 } 2297 2298 if (auto *F = dyn_cast<llvm::Function>(GO)) { 2299 if (D->hasAttr<RetainAttr>()) 2300 addUsedGlobal(F); 2301 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>()) 2302 if (!D->getAttr<SectionAttr>()) 2303 F->addFnAttr("implicit-section-name", SA->getName()); 2304 2305 llvm::AttrBuilder Attrs(F->getContext()); 2306 if (GetCPUAndFeaturesAttributes(GD, Attrs)) { 2307 // We know that GetCPUAndFeaturesAttributes will always have the 2308 // newest set, since it has the newest possible FunctionDecl, so the 2309 // new ones should replace the old. 2310 llvm::AttributeMask RemoveAttrs; 2311 RemoveAttrs.addAttribute("target-cpu"); 2312 RemoveAttrs.addAttribute("target-features"); 2313 RemoveAttrs.addAttribute("tune-cpu"); 2314 F->removeFnAttrs(RemoveAttrs); 2315 F->addFnAttrs(Attrs); 2316 } 2317 } 2318 2319 if (const auto *CSA = D->getAttr<CodeSegAttr>()) 2320 GO->setSection(CSA->getName()); 2321 else if (const auto *SA = D->getAttr<SectionAttr>()) 2322 GO->setSection(SA->getName()); 2323 } 2324 2325 getTargetCodeGenInfo().setTargetAttributes(D, GO, *this); 2326 } 2327 2328 void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD, 2329 llvm::Function *F, 2330 const CGFunctionInfo &FI) { 2331 const Decl *D = GD.getDecl(); 2332 SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false); 2333 SetLLVMFunctionAttributesForDefinition(D, F); 2334 2335 F->setLinkage(llvm::Function::InternalLinkage); 2336 2337 setNonAliasAttributes(GD, F); 2338 } 2339 2340 static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) { 2341 // Set linkage and visibility in case we never see a definition. 2342 LinkageInfo LV = ND->getLinkageAndVisibility(); 2343 // Don't set internal linkage on declarations. 2344 // "extern_weak" is overloaded in LLVM; we probably should have 2345 // separate linkage types for this. 2346 if (isExternallyVisible(LV.getLinkage()) && 2347 (ND->hasAttr<WeakAttr>() || ND->isWeakImported())) 2348 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); 2349 } 2350 2351 void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD, 2352 llvm::Function *F) { 2353 // Only if we are checking indirect calls. 2354 if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall)) 2355 return; 2356 2357 // Non-static class methods are handled via vtable or member function pointer 2358 // checks elsewhere. 2359 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) 2360 return; 2361 2362 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType()); 2363 F->addTypeMetadata(0, MD); 2364 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType())); 2365 2366 // Emit a hash-based bit set entry for cross-DSO calls. 2367 if (CodeGenOpts.SanitizeCfiCrossDso) 2368 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) 2369 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId)); 2370 } 2371 2372 void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) { 2373 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic()) 2374 return; 2375 2376 llvm::LLVMContext &Ctx = F->getContext(); 2377 llvm::MDBuilder MDB(Ctx); 2378 F->setMetadata(llvm::LLVMContext::MD_kcfi_type, 2379 llvm::MDNode::get( 2380 Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType())))); 2381 } 2382 2383 static bool allowKCFIIdentifier(StringRef Name) { 2384 // KCFI type identifier constants are only necessary for external assembly 2385 // functions, which means it's safe to skip unusual names. Subset of 2386 // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar(). 2387 return llvm::all_of(Name, [](const char &C) { 2388 return llvm::isAlnum(C) || C == '_' || C == '.'; 2389 }); 2390 } 2391 2392 void CodeGenModule::finalizeKCFITypes() { 2393 llvm::Module &M = getModule(); 2394 for (auto &F : M.functions()) { 2395 // Remove KCFI type metadata from non-address-taken local functions. 2396 bool AddressTaken = F.hasAddressTaken(); 2397 if (!AddressTaken && F.hasLocalLinkage()) 2398 F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type); 2399 2400 // Generate a constant with the expected KCFI type identifier for all 2401 // address-taken function declarations to support annotating indirectly 2402 // called assembly functions. 2403 if (!AddressTaken || !F.isDeclaration()) 2404 continue; 2405 2406 const llvm::ConstantInt *Type; 2407 if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type)) 2408 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0)); 2409 else 2410 continue; 2411 2412 StringRef Name = F.getName(); 2413 if (!allowKCFIIdentifier(Name)) 2414 continue; 2415 2416 std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" + 2417 Name + ", " + Twine(Type->getZExtValue()) + "\n") 2418 .str(); 2419 M.appendModuleInlineAsm(Asm); 2420 } 2421 } 2422 2423 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F, 2424 bool IsIncompleteFunction, 2425 bool IsThunk) { 2426 2427 if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) { 2428 // If this is an intrinsic function, set the function's attributes 2429 // to the intrinsic's attributes. 2430 F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID)); 2431 return; 2432 } 2433 2434 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 2435 2436 if (!IsIncompleteFunction) 2437 SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F, 2438 IsThunk); 2439 2440 // Add the Returned attribute for "this", except for iOS 5 and earlier 2441 // where substantial code, including the libstdc++ dylib, was compiled with 2442 // GCC and does not actually return "this". 2443 if (!IsThunk && getCXXABI().HasThisReturn(GD) && 2444 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) { 2445 assert(!F->arg_empty() && 2446 F->arg_begin()->getType() 2447 ->canLosslesslyBitCastTo(F->getReturnType()) && 2448 "unexpected this return"); 2449 F->addParamAttr(0, llvm::Attribute::Returned); 2450 } 2451 2452 // Only a few attributes are set on declarations; these may later be 2453 // overridden by a definition. 2454 2455 setLinkageForGV(F, FD); 2456 setGVProperties(F, FD); 2457 2458 // Setup target-specific attributes. 2459 if (!IsIncompleteFunction && F->isDeclaration()) 2460 getTargetCodeGenInfo().setTargetAttributes(FD, F, *this); 2461 2462 if (const auto *CSA = FD->getAttr<CodeSegAttr>()) 2463 F->setSection(CSA->getName()); 2464 else if (const auto *SA = FD->getAttr<SectionAttr>()) 2465 F->setSection(SA->getName()); 2466 2467 if (const auto *EA = FD->getAttr<ErrorAttr>()) { 2468 if (EA->isError()) 2469 F->addFnAttr("dontcall-error", EA->getUserDiagnostic()); 2470 else if (EA->isWarning()) 2471 F->addFnAttr("dontcall-warn", EA->getUserDiagnostic()); 2472 } 2473 2474 // If we plan on emitting this inline builtin, we can't treat it as a builtin. 2475 if (FD->isInlineBuiltinDeclaration()) { 2476 const FunctionDecl *FDBody; 2477 bool HasBody = FD->hasBody(FDBody); 2478 (void)HasBody; 2479 assert(HasBody && "Inline builtin declarations should always have an " 2480 "available body!"); 2481 if (shouldEmitFunction(FDBody)) 2482 F->addFnAttr(llvm::Attribute::NoBuiltin); 2483 } 2484 2485 if (FD->isReplaceableGlobalAllocationFunction()) { 2486 // A replaceable global allocation function does not act like a builtin by 2487 // default, only if it is invoked by a new-expression or delete-expression. 2488 F->addFnAttr(llvm::Attribute::NoBuiltin); 2489 } 2490 2491 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD)) 2492 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2493 else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) 2494 if (MD->isVirtual()) 2495 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2496 2497 // Don't emit entries for function declarations in the cross-DSO mode. This 2498 // is handled with better precision by the receiving DSO. But if jump tables 2499 // are non-canonical then we need type metadata in order to produce the local 2500 // jump table. 2501 if (!CodeGenOpts.SanitizeCfiCrossDso || 2502 !CodeGenOpts.SanitizeCfiCanonicalJumpTables) 2503 CreateFunctionTypeMetadataForIcall(FD, F); 2504 2505 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) 2506 setKCFIType(FD, F); 2507 2508 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>()) 2509 getOpenMPRuntime().emitDeclareSimdFunction(FD, F); 2510 2511 if (CodeGenOpts.InlineMaxStackSize != UINT_MAX) 2512 F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize)); 2513 2514 if (const auto *CB = FD->getAttr<CallbackAttr>()) { 2515 // Annotate the callback behavior as metadata: 2516 // - The callback callee (as argument number). 2517 // - The callback payloads (as argument numbers). 2518 llvm::LLVMContext &Ctx = F->getContext(); 2519 llvm::MDBuilder MDB(Ctx); 2520 2521 // The payload indices are all but the first one in the encoding. The first 2522 // identifies the callback callee. 2523 int CalleeIdx = *CB->encoding_begin(); 2524 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end()); 2525 F->addMetadata(llvm::LLVMContext::MD_callback, 2526 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding( 2527 CalleeIdx, PayloadIndices, 2528 /* VarArgsArePassed */ false)})); 2529 } 2530 } 2531 2532 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) { 2533 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) && 2534 "Only globals with definition can force usage."); 2535 LLVMUsed.emplace_back(GV); 2536 } 2537 2538 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) { 2539 assert(!GV->isDeclaration() && 2540 "Only globals with definition can force usage."); 2541 LLVMCompilerUsed.emplace_back(GV); 2542 } 2543 2544 void CodeGenModule::addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV) { 2545 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) && 2546 "Only globals with definition can force usage."); 2547 if (getTriple().isOSBinFormatELF()) 2548 LLVMCompilerUsed.emplace_back(GV); 2549 else 2550 LLVMUsed.emplace_back(GV); 2551 } 2552 2553 static void emitUsed(CodeGenModule &CGM, StringRef Name, 2554 std::vector<llvm::WeakTrackingVH> &List) { 2555 // Don't create llvm.used if there is no need. 2556 if (List.empty()) 2557 return; 2558 2559 // Convert List to what ConstantArray needs. 2560 SmallVector<llvm::Constant*, 8> UsedArray; 2561 UsedArray.resize(List.size()); 2562 for (unsigned i = 0, e = List.size(); i != e; ++i) { 2563 UsedArray[i] = 2564 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast( 2565 cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy); 2566 } 2567 2568 if (UsedArray.empty()) 2569 return; 2570 llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size()); 2571 2572 auto *GV = new llvm::GlobalVariable( 2573 CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage, 2574 llvm::ConstantArray::get(ATy, UsedArray), Name); 2575 2576 GV->setSection("llvm.metadata"); 2577 } 2578 2579 void CodeGenModule::emitLLVMUsed() { 2580 emitUsed(*this, "llvm.used", LLVMUsed); 2581 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed); 2582 } 2583 2584 void CodeGenModule::AppendLinkerOptions(StringRef Opts) { 2585 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts); 2586 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); 2587 } 2588 2589 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) { 2590 llvm::SmallString<32> Opt; 2591 getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt); 2592 if (Opt.empty()) 2593 return; 2594 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); 2595 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts)); 2596 } 2597 2598 void CodeGenModule::AddDependentLib(StringRef Lib) { 2599 auto &C = getLLVMContext(); 2600 if (getTarget().getTriple().isOSBinFormatELF()) { 2601 ELFDependentLibraries.push_back( 2602 llvm::MDNode::get(C, llvm::MDString::get(C, Lib))); 2603 return; 2604 } 2605 2606 llvm::SmallString<24> Opt; 2607 getTargetCodeGenInfo().getDependentLibraryOption(Lib, Opt); 2608 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt); 2609 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts)); 2610 } 2611 2612 /// Add link options implied by the given module, including modules 2613 /// it depends on, using a postorder walk. 2614 static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, 2615 SmallVectorImpl<llvm::MDNode *> &Metadata, 2616 llvm::SmallPtrSet<Module *, 16> &Visited) { 2617 // Import this module's parent. 2618 if (Mod->Parent && Visited.insert(Mod->Parent).second) { 2619 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited); 2620 } 2621 2622 // Import this module's dependencies. 2623 for (Module *Import : llvm::reverse(Mod->Imports)) { 2624 if (Visited.insert(Import).second) 2625 addLinkOptionsPostorder(CGM, Import, Metadata, Visited); 2626 } 2627 2628 // Add linker options to link against the libraries/frameworks 2629 // described by this module. 2630 llvm::LLVMContext &Context = CGM.getLLVMContext(); 2631 bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF(); 2632 2633 // For modules that use export_as for linking, use that module 2634 // name instead. 2635 if (Mod->UseExportAsModuleLinkName) 2636 return; 2637 2638 for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) { 2639 // Link against a framework. Frameworks are currently Darwin only, so we 2640 // don't to ask TargetCodeGenInfo for the spelling of the linker option. 2641 if (LL.IsFramework) { 2642 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"), 2643 llvm::MDString::get(Context, LL.Library)}; 2644 2645 Metadata.push_back(llvm::MDNode::get(Context, Args)); 2646 continue; 2647 } 2648 2649 // Link against a library. 2650 if (IsELF) { 2651 llvm::Metadata *Args[2] = { 2652 llvm::MDString::get(Context, "lib"), 2653 llvm::MDString::get(Context, LL.Library), 2654 }; 2655 Metadata.push_back(llvm::MDNode::get(Context, Args)); 2656 } else { 2657 llvm::SmallString<24> Opt; 2658 CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt); 2659 auto *OptString = llvm::MDString::get(Context, Opt); 2660 Metadata.push_back(llvm::MDNode::get(Context, OptString)); 2661 } 2662 } 2663 } 2664 2665 void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) { 2666 // Emit the initializers in the order that sub-modules appear in the 2667 // source, first Global Module Fragments, if present. 2668 if (auto GMF = Primary->getGlobalModuleFragment()) { 2669 for (Decl *D : getContext().getModuleInitializers(GMF)) { 2670 if (isa<ImportDecl>(D)) 2671 continue; 2672 assert(isa<VarDecl>(D) && "GMF initializer decl is not a var?"); 2673 EmitTopLevelDecl(D); 2674 } 2675 } 2676 // Second any associated with the module, itself. 2677 for (Decl *D : getContext().getModuleInitializers(Primary)) { 2678 // Skip import decls, the inits for those are called explicitly. 2679 if (isa<ImportDecl>(D)) 2680 continue; 2681 EmitTopLevelDecl(D); 2682 } 2683 // Third any associated with the Privat eMOdule Fragment, if present. 2684 if (auto PMF = Primary->getPrivateModuleFragment()) { 2685 for (Decl *D : getContext().getModuleInitializers(PMF)) { 2686 assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?"); 2687 EmitTopLevelDecl(D); 2688 } 2689 } 2690 } 2691 2692 void CodeGenModule::EmitModuleLinkOptions() { 2693 // Collect the set of all of the modules we want to visit to emit link 2694 // options, which is essentially the imported modules and all of their 2695 // non-explicit child modules. 2696 llvm::SetVector<clang::Module *> LinkModules; 2697 llvm::SmallPtrSet<clang::Module *, 16> Visited; 2698 SmallVector<clang::Module *, 16> Stack; 2699 2700 // Seed the stack with imported modules. 2701 for (Module *M : ImportedModules) { 2702 // Do not add any link flags when an implementation TU of a module imports 2703 // a header of that same module. 2704 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule && 2705 !getLangOpts().isCompilingModule()) 2706 continue; 2707 if (Visited.insert(M).second) 2708 Stack.push_back(M); 2709 } 2710 2711 // Find all of the modules to import, making a little effort to prune 2712 // non-leaf modules. 2713 while (!Stack.empty()) { 2714 clang::Module *Mod = Stack.pop_back_val(); 2715 2716 bool AnyChildren = false; 2717 2718 // Visit the submodules of this module. 2719 for (const auto &SM : Mod->submodules()) { 2720 // Skip explicit children; they need to be explicitly imported to be 2721 // linked against. 2722 if (SM->IsExplicit) 2723 continue; 2724 2725 if (Visited.insert(SM).second) { 2726 Stack.push_back(SM); 2727 AnyChildren = true; 2728 } 2729 } 2730 2731 // We didn't find any children, so add this module to the list of 2732 // modules to link against. 2733 if (!AnyChildren) { 2734 LinkModules.insert(Mod); 2735 } 2736 } 2737 2738 // Add link options for all of the imported modules in reverse topological 2739 // order. We don't do anything to try to order import link flags with respect 2740 // to linker options inserted by things like #pragma comment(). 2741 SmallVector<llvm::MDNode *, 16> MetadataArgs; 2742 Visited.clear(); 2743 for (Module *M : LinkModules) 2744 if (Visited.insert(M).second) 2745 addLinkOptionsPostorder(*this, M, MetadataArgs, Visited); 2746 std::reverse(MetadataArgs.begin(), MetadataArgs.end()); 2747 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end()); 2748 2749 // Add the linker options metadata flag. 2750 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options"); 2751 for (auto *MD : LinkerOptionsMetadata) 2752 NMD->addOperand(MD); 2753 } 2754 2755 void CodeGenModule::EmitDeferred() { 2756 // Emit deferred declare target declarations. 2757 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd) 2758 getOpenMPRuntime().emitDeferredTargetDecls(); 2759 2760 // Emit code for any potentially referenced deferred decls. Since a 2761 // previously unused static decl may become used during the generation of code 2762 // for a static function, iterate until no changes are made. 2763 2764 if (!DeferredVTables.empty()) { 2765 EmitDeferredVTables(); 2766 2767 // Emitting a vtable doesn't directly cause more vtables to 2768 // become deferred, although it can cause functions to be 2769 // emitted that then need those vtables. 2770 assert(DeferredVTables.empty()); 2771 } 2772 2773 // Emit CUDA/HIP static device variables referenced by host code only. 2774 // Note we should not clear CUDADeviceVarODRUsedByHost since it is still 2775 // needed for further handling. 2776 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice) 2777 llvm::append_range(DeferredDeclsToEmit, 2778 getContext().CUDADeviceVarODRUsedByHost); 2779 2780 // Stop if we're out of both deferred vtables and deferred declarations. 2781 if (DeferredDeclsToEmit.empty()) 2782 return; 2783 2784 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more 2785 // work, it will not interfere with this. 2786 std::vector<GlobalDecl> CurDeclsToEmit; 2787 CurDeclsToEmit.swap(DeferredDeclsToEmit); 2788 2789 for (GlobalDecl &D : CurDeclsToEmit) { 2790 // We should call GetAddrOfGlobal with IsForDefinition set to true in order 2791 // to get GlobalValue with exactly the type we need, not something that 2792 // might had been created for another decl with the same mangled name but 2793 // different type. 2794 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>( 2795 GetAddrOfGlobal(D, ForDefinition)); 2796 2797 // In case of different address spaces, we may still get a cast, even with 2798 // IsForDefinition equal to true. Query mangled names table to get 2799 // GlobalValue. 2800 if (!GV) 2801 GV = GetGlobalValue(getMangledName(D)); 2802 2803 // Make sure GetGlobalValue returned non-null. 2804 assert(GV); 2805 2806 // Check to see if we've already emitted this. This is necessary 2807 // for a couple of reasons: first, decls can end up in the 2808 // deferred-decls queue multiple times, and second, decls can end 2809 // up with definitions in unusual ways (e.g. by an extern inline 2810 // function acquiring a strong function redefinition). Just 2811 // ignore these cases. 2812 if (!GV->isDeclaration()) 2813 continue; 2814 2815 // If this is OpenMP, check if it is legal to emit this global normally. 2816 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D)) 2817 continue; 2818 2819 // Otherwise, emit the definition and move on to the next one. 2820 EmitGlobalDefinition(D, GV); 2821 2822 // If we found out that we need to emit more decls, do that recursively. 2823 // This has the advantage that the decls are emitted in a DFS and related 2824 // ones are close together, which is convenient for testing. 2825 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) { 2826 EmitDeferred(); 2827 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty()); 2828 } 2829 } 2830 } 2831 2832 void CodeGenModule::EmitVTablesOpportunistically() { 2833 // Try to emit external vtables as available_externally if they have emitted 2834 // all inlined virtual functions. It runs after EmitDeferred() and therefore 2835 // is not allowed to create new references to things that need to be emitted 2836 // lazily. Note that it also uses fact that we eagerly emitting RTTI. 2837 2838 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables()) 2839 && "Only emit opportunistic vtables with optimizations"); 2840 2841 for (const CXXRecordDecl *RD : OpportunisticVTables) { 2842 assert(getVTables().isVTableExternal(RD) && 2843 "This queue should only contain external vtables"); 2844 if (getCXXABI().canSpeculativelyEmitVTable(RD)) 2845 VTables.GenerateClassData(RD); 2846 } 2847 OpportunisticVTables.clear(); 2848 } 2849 2850 void CodeGenModule::EmitGlobalAnnotations() { 2851 if (Annotations.empty()) 2852 return; 2853 2854 // Create a new global variable for the ConstantStruct in the Module. 2855 llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get( 2856 Annotations[0]->getType(), Annotations.size()), Annotations); 2857 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false, 2858 llvm::GlobalValue::AppendingLinkage, 2859 Array, "llvm.global.annotations"); 2860 gv->setSection(AnnotationSection); 2861 } 2862 2863 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) { 2864 llvm::Constant *&AStr = AnnotationStrings[Str]; 2865 if (AStr) 2866 return AStr; 2867 2868 // Not found yet, create a new global. 2869 llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str); 2870 auto *gv = new llvm::GlobalVariable( 2871 getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s, 2872 ".str", nullptr, llvm::GlobalValue::NotThreadLocal, 2873 ConstGlobalsPtrTy->getAddressSpace()); 2874 gv->setSection(AnnotationSection); 2875 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2876 AStr = gv; 2877 return gv; 2878 } 2879 2880 llvm::Constant *CodeGenModule::EmitAnnotationUnit(SourceLocation Loc) { 2881 SourceManager &SM = getContext().getSourceManager(); 2882 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 2883 if (PLoc.isValid()) 2884 return EmitAnnotationString(PLoc.getFilename()); 2885 return EmitAnnotationString(SM.getBufferName(Loc)); 2886 } 2887 2888 llvm::Constant *CodeGenModule::EmitAnnotationLineNo(SourceLocation L) { 2889 SourceManager &SM = getContext().getSourceManager(); 2890 PresumedLoc PLoc = SM.getPresumedLoc(L); 2891 unsigned LineNo = PLoc.isValid() ? PLoc.getLine() : 2892 SM.getExpansionLineNumber(L); 2893 return llvm::ConstantInt::get(Int32Ty, LineNo); 2894 } 2895 2896 llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) { 2897 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()}; 2898 if (Exprs.empty()) 2899 return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy); 2900 2901 llvm::FoldingSetNodeID ID; 2902 for (Expr *E : Exprs) { 2903 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult()); 2904 } 2905 llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()]; 2906 if (Lookup) 2907 return Lookup; 2908 2909 llvm::SmallVector<llvm::Constant *, 4> LLVMArgs; 2910 LLVMArgs.reserve(Exprs.size()); 2911 ConstantEmitter ConstEmiter(*this); 2912 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) { 2913 const auto *CE = cast<clang::ConstantExpr>(E); 2914 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(), 2915 CE->getType()); 2916 }); 2917 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs); 2918 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true, 2919 llvm::GlobalValue::PrivateLinkage, Struct, 2920 ".args"); 2921 GV->setSection(AnnotationSection); 2922 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 2923 auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, GlobalsInt8PtrTy); 2924 2925 Lookup = Bitcasted; 2926 return Bitcasted; 2927 } 2928 2929 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, 2930 const AnnotateAttr *AA, 2931 SourceLocation L) { 2932 // Get the globals for file name, annotation, and the line number. 2933 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()), 2934 *UnitGV = EmitAnnotationUnit(L), 2935 *LineNoCst = EmitAnnotationLineNo(L), 2936 *Args = EmitAnnotationArgs(AA); 2937 2938 llvm::Constant *GVInGlobalsAS = GV; 2939 if (GV->getAddressSpace() != 2940 getDataLayout().getDefaultGlobalsAddressSpace()) { 2941 GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast( 2942 GV, GV->getValueType()->getPointerTo( 2943 getDataLayout().getDefaultGlobalsAddressSpace())); 2944 } 2945 2946 // Create the ConstantStruct for the global annotation. 2947 llvm::Constant *Fields[] = { 2948 llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy), 2949 llvm::ConstantExpr::getBitCast(AnnoGV, ConstGlobalsPtrTy), 2950 llvm::ConstantExpr::getBitCast(UnitGV, ConstGlobalsPtrTy), 2951 LineNoCst, 2952 Args, 2953 }; 2954 return llvm::ConstantStruct::getAnon(Fields); 2955 } 2956 2957 void CodeGenModule::AddGlobalAnnotations(const ValueDecl *D, 2958 llvm::GlobalValue *GV) { 2959 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute"); 2960 // Get the struct elements for these annotations. 2961 for (const auto *I : D->specific_attrs<AnnotateAttr>()) 2962 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation())); 2963 } 2964 2965 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, 2966 SourceLocation Loc) const { 2967 const auto &NoSanitizeL = getContext().getNoSanitizeList(); 2968 // NoSanitize by function name. 2969 if (NoSanitizeL.containsFunction(Kind, Fn->getName())) 2970 return true; 2971 // NoSanitize by location. Check "mainfile" prefix. 2972 auto &SM = Context.getSourceManager(); 2973 const FileEntry &MainFile = *SM.getFileEntryForID(SM.getMainFileID()); 2974 if (NoSanitizeL.containsMainFile(Kind, MainFile.getName())) 2975 return true; 2976 2977 // Check "src" prefix. 2978 if (Loc.isValid()) 2979 return NoSanitizeL.containsLocation(Kind, Loc); 2980 // If location is unknown, this may be a compiler-generated function. Assume 2981 // it's located in the main file. 2982 return NoSanitizeL.containsFile(Kind, MainFile.getName()); 2983 } 2984 2985 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind, 2986 llvm::GlobalVariable *GV, 2987 SourceLocation Loc, QualType Ty, 2988 StringRef Category) const { 2989 const auto &NoSanitizeL = getContext().getNoSanitizeList(); 2990 if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category)) 2991 return true; 2992 auto &SM = Context.getSourceManager(); 2993 if (NoSanitizeL.containsMainFile( 2994 Kind, SM.getFileEntryForID(SM.getMainFileID())->getName(), Category)) 2995 return true; 2996 if (NoSanitizeL.containsLocation(Kind, Loc, Category)) 2997 return true; 2998 2999 // Check global type. 3000 if (!Ty.isNull()) { 3001 // Drill down the array types: if global variable of a fixed type is 3002 // not sanitized, we also don't instrument arrays of them. 3003 while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr())) 3004 Ty = AT->getElementType(); 3005 Ty = Ty.getCanonicalType().getUnqualifiedType(); 3006 // Only record types (classes, structs etc.) are ignored. 3007 if (Ty->isRecordType()) { 3008 std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy()); 3009 if (NoSanitizeL.containsType(Kind, TypeStr, Category)) 3010 return true; 3011 } 3012 } 3013 return false; 3014 } 3015 3016 bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, 3017 StringRef Category) const { 3018 const auto &XRayFilter = getContext().getXRayFilter(); 3019 using ImbueAttr = XRayFunctionFilter::ImbueAttribute; 3020 auto Attr = ImbueAttr::NONE; 3021 if (Loc.isValid()) 3022 Attr = XRayFilter.shouldImbueLocation(Loc, Category); 3023 if (Attr == ImbueAttr::NONE) 3024 Attr = XRayFilter.shouldImbueFunction(Fn->getName()); 3025 switch (Attr) { 3026 case ImbueAttr::NONE: 3027 return false; 3028 case ImbueAttr::ALWAYS: 3029 Fn->addFnAttr("function-instrument", "xray-always"); 3030 break; 3031 case ImbueAttr::ALWAYS_ARG1: 3032 Fn->addFnAttr("function-instrument", "xray-always"); 3033 Fn->addFnAttr("xray-log-args", "1"); 3034 break; 3035 case ImbueAttr::NEVER: 3036 Fn->addFnAttr("function-instrument", "xray-never"); 3037 break; 3038 } 3039 return true; 3040 } 3041 3042 ProfileList::ExclusionType 3043 CodeGenModule::isFunctionBlockedByProfileList(llvm::Function *Fn, 3044 SourceLocation Loc) const { 3045 const auto &ProfileList = getContext().getProfileList(); 3046 // If the profile list is empty, then instrument everything. 3047 if (ProfileList.isEmpty()) 3048 return ProfileList::Allow; 3049 CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr(); 3050 // First, check the function name. 3051 if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind)) 3052 return *V; 3053 // Next, check the source location. 3054 if (Loc.isValid()) 3055 if (auto V = ProfileList.isLocationExcluded(Loc, Kind)) 3056 return *V; 3057 // If location is unknown, this may be a compiler-generated function. Assume 3058 // it's located in the main file. 3059 auto &SM = Context.getSourceManager(); 3060 if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) 3061 if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind)) 3062 return *V; 3063 return ProfileList.getDefault(Kind); 3064 } 3065 3066 ProfileList::ExclusionType 3067 CodeGenModule::isFunctionBlockedFromProfileInstr(llvm::Function *Fn, 3068 SourceLocation Loc) const { 3069 auto V = isFunctionBlockedByProfileList(Fn, Loc); 3070 if (V != ProfileList::Allow) 3071 return V; 3072 3073 auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups; 3074 if (NumGroups > 1) { 3075 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups; 3076 if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup) 3077 return ProfileList::Skip; 3078 } 3079 return ProfileList::Allow; 3080 } 3081 3082 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { 3083 // Never defer when EmitAllDecls is specified. 3084 if (LangOpts.EmitAllDecls) 3085 return true; 3086 3087 if (CodeGenOpts.KeepStaticConsts) { 3088 const auto *VD = dyn_cast<VarDecl>(Global); 3089 if (VD && VD->getType().isConstQualified() && 3090 VD->getStorageDuration() == SD_Static) 3091 return true; 3092 } 3093 3094 return getContext().DeclMustBeEmitted(Global); 3095 } 3096 3097 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) { 3098 // In OpenMP 5.0 variables and function may be marked as 3099 // device_type(host/nohost) and we should not emit them eagerly unless we sure 3100 // that they must be emitted on the host/device. To be sure we need to have 3101 // seen a declare target with an explicit mentioning of the function, we know 3102 // we have if the level of the declare target attribute is -1. Note that we 3103 // check somewhere else if we should emit this at all. 3104 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) { 3105 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr = 3106 OMPDeclareTargetDeclAttr::getActiveAttr(Global); 3107 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1) 3108 return false; 3109 } 3110 3111 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { 3112 if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) 3113 // Implicit template instantiations may change linkage if they are later 3114 // explicitly instantiated, so they should not be emitted eagerly. 3115 return false; 3116 } 3117 if (const auto *VD = dyn_cast<VarDecl>(Global)) { 3118 if (Context.getInlineVariableDefinitionKind(VD) == 3119 ASTContext::InlineVariableDefinitionKind::WeakUnknown) 3120 // A definition of an inline constexpr static data member may change 3121 // linkage later if it's redeclared outside the class. 3122 return false; 3123 if (CXX20ModuleInits && VD->getOwningModule() && 3124 !VD->getOwningModule()->isModuleMapModule()) { 3125 // For CXX20, module-owned initializers need to be deferred, since it is 3126 // not known at this point if they will be run for the current module or 3127 // as part of the initializer for an imported one. 3128 return false; 3129 } 3130 } 3131 // If OpenMP is enabled and threadprivates must be generated like TLS, delay 3132 // codegen for global variables, because they may be marked as threadprivate. 3133 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS && 3134 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) && 3135 !isTypeConstant(Global->getType(), false, false) && 3136 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global)) 3137 return false; 3138 3139 return true; 3140 } 3141 3142 ConstantAddress CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl *GD) { 3143 StringRef Name = getMangledName(GD); 3144 3145 // The UUID descriptor should be pointer aligned. 3146 CharUnits Alignment = CharUnits::fromQuantity(PointerAlignInBytes); 3147 3148 // Look for an existing global. 3149 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) 3150 return ConstantAddress(GV, GV->getValueType(), Alignment); 3151 3152 ConstantEmitter Emitter(*this); 3153 llvm::Constant *Init; 3154 3155 APValue &V = GD->getAsAPValue(); 3156 if (!V.isAbsent()) { 3157 // If possible, emit the APValue version of the initializer. In particular, 3158 // this gets the type of the constant right. 3159 Init = Emitter.emitForInitializer( 3160 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType()); 3161 } else { 3162 // As a fallback, directly construct the constant. 3163 // FIXME: This may get padding wrong under esoteric struct layout rules. 3164 // MSVC appears to create a complete type 'struct __s_GUID' that it 3165 // presumably uses to represent these constants. 3166 MSGuidDecl::Parts Parts = GD->getParts(); 3167 llvm::Constant *Fields[4] = { 3168 llvm::ConstantInt::get(Int32Ty, Parts.Part1), 3169 llvm::ConstantInt::get(Int16Ty, Parts.Part2), 3170 llvm::ConstantInt::get(Int16Ty, Parts.Part3), 3171 llvm::ConstantDataArray::getRaw( 3172 StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8, 3173 Int8Ty)}; 3174 Init = llvm::ConstantStruct::getAnon(Fields); 3175 } 3176 3177 auto *GV = new llvm::GlobalVariable( 3178 getModule(), Init->getType(), 3179 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name); 3180 if (supportsCOMDAT()) 3181 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 3182 setDSOLocal(GV); 3183 3184 if (!V.isAbsent()) { 3185 Emitter.finalize(GV); 3186 return ConstantAddress(GV, GV->getValueType(), Alignment); 3187 } 3188 3189 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType()); 3190 llvm::Constant *Addr = llvm::ConstantExpr::getBitCast( 3191 GV, Ty->getPointerTo(GV->getAddressSpace())); 3192 return ConstantAddress(Addr, Ty, Alignment); 3193 } 3194 3195 ConstantAddress CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl( 3196 const UnnamedGlobalConstantDecl *GCD) { 3197 CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType()); 3198 3199 llvm::GlobalVariable **Entry = nullptr; 3200 Entry = &UnnamedGlobalConstantDeclMap[GCD]; 3201 if (*Entry) 3202 return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment); 3203 3204 ConstantEmitter Emitter(*this); 3205 llvm::Constant *Init; 3206 3207 const APValue &V = GCD->getValue(); 3208 3209 assert(!V.isAbsent()); 3210 Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(), 3211 GCD->getType()); 3212 3213 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(), 3214 /*isConstant=*/true, 3215 llvm::GlobalValue::PrivateLinkage, Init, 3216 ".constant"); 3217 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 3218 GV->setAlignment(Alignment.getAsAlign()); 3219 3220 Emitter.finalize(GV); 3221 3222 *Entry = GV; 3223 return ConstantAddress(GV, GV->getValueType(), Alignment); 3224 } 3225 3226 ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject( 3227 const TemplateParamObjectDecl *TPO) { 3228 StringRef Name = getMangledName(TPO); 3229 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType()); 3230 3231 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name)) 3232 return ConstantAddress(GV, GV->getValueType(), Alignment); 3233 3234 ConstantEmitter Emitter(*this); 3235 llvm::Constant *Init = Emitter.emitForInitializer( 3236 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType()); 3237 3238 if (!Init) { 3239 ErrorUnsupported(TPO, "template parameter object"); 3240 return ConstantAddress::invalid(); 3241 } 3242 3243 llvm::GlobalValue::LinkageTypes Linkage = 3244 isExternallyVisible(TPO->getLinkageAndVisibility().getLinkage()) 3245 ? llvm::GlobalValue::LinkOnceODRLinkage 3246 : llvm::GlobalValue::InternalLinkage; 3247 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(), 3248 /*isConstant=*/true, Linkage, Init, Name); 3249 setGVProperties(GV, TPO); 3250 if (supportsCOMDAT()) 3251 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 3252 Emitter.finalize(GV); 3253 3254 return ConstantAddress(GV, GV->getValueType(), Alignment); 3255 } 3256 3257 ConstantAddress CodeGenModule::GetWeakRefReference(const ValueDecl *VD) { 3258 const AliasAttr *AA = VD->getAttr<AliasAttr>(); 3259 assert(AA && "No alias?"); 3260 3261 CharUnits Alignment = getContext().getDeclAlign(VD); 3262 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType()); 3263 3264 // See if there is already something with the target's name in the module. 3265 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee()); 3266 if (Entry) { 3267 unsigned AS = getTypes().getTargetAddressSpace(VD->getType()); 3268 auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS)); 3269 return ConstantAddress(Ptr, DeclTy, Alignment); 3270 } 3271 3272 llvm::Constant *Aliasee; 3273 if (isa<llvm::FunctionType>(DeclTy)) 3274 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, 3275 GlobalDecl(cast<FunctionDecl>(VD)), 3276 /*ForVTable=*/false); 3277 else 3278 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default, 3279 nullptr); 3280 3281 auto *F = cast<llvm::GlobalValue>(Aliasee); 3282 F->setLinkage(llvm::Function::ExternalWeakLinkage); 3283 WeakRefReferences.insert(F); 3284 3285 return ConstantAddress(Aliasee, DeclTy, Alignment); 3286 } 3287 3288 void CodeGenModule::EmitGlobal(GlobalDecl GD) { 3289 const auto *Global = cast<ValueDecl>(GD.getDecl()); 3290 3291 // Weak references don't produce any output by themselves. 3292 if (Global->hasAttr<WeakRefAttr>()) 3293 return; 3294 3295 // If this is an alias definition (which otherwise looks like a declaration) 3296 // emit it now. 3297 if (Global->hasAttr<AliasAttr>()) 3298 return EmitAliasDefinition(GD); 3299 3300 // IFunc like an alias whose value is resolved at runtime by calling resolver. 3301 if (Global->hasAttr<IFuncAttr>()) 3302 return emitIFuncDefinition(GD); 3303 3304 // If this is a cpu_dispatch multiversion function, emit the resolver. 3305 if (Global->hasAttr<CPUDispatchAttr>()) 3306 return emitCPUDispatchDefinition(GD); 3307 3308 // If this is CUDA, be selective about which declarations we emit. 3309 if (LangOpts.CUDA) { 3310 if (LangOpts.CUDAIsDevice) { 3311 if (!Global->hasAttr<CUDADeviceAttr>() && 3312 !Global->hasAttr<CUDAGlobalAttr>() && 3313 !Global->hasAttr<CUDAConstantAttr>() && 3314 !Global->hasAttr<CUDASharedAttr>() && 3315 !Global->getType()->isCUDADeviceBuiltinSurfaceType() && 3316 !Global->getType()->isCUDADeviceBuiltinTextureType()) 3317 return; 3318 } else { 3319 // We need to emit host-side 'shadows' for all global 3320 // device-side variables because the CUDA runtime needs their 3321 // size and host-side address in order to provide access to 3322 // their device-side incarnations. 3323 3324 // So device-only functions are the only things we skip. 3325 if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() && 3326 Global->hasAttr<CUDADeviceAttr>()) 3327 return; 3328 3329 assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) && 3330 "Expected Variable or Function"); 3331 } 3332 } 3333 3334 if (LangOpts.OpenMP) { 3335 // If this is OpenMP, check if it is legal to emit this global normally. 3336 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD)) 3337 return; 3338 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) { 3339 if (MustBeEmitted(Global)) 3340 EmitOMPDeclareReduction(DRD); 3341 return; 3342 } 3343 if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) { 3344 if (MustBeEmitted(Global)) 3345 EmitOMPDeclareMapper(DMD); 3346 return; 3347 } 3348 } 3349 3350 // Ignore declarations, they will be emitted on their first use. 3351 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) { 3352 // Forward declarations are emitted lazily on first use. 3353 if (!FD->doesThisDeclarationHaveABody()) { 3354 if (!FD->doesDeclarationForceExternallyVisibleDefinition()) 3355 return; 3356 3357 StringRef MangledName = getMangledName(GD); 3358 3359 // Compute the function info and LLVM type. 3360 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 3361 llvm::Type *Ty = getTypes().GetFunctionType(FI); 3362 3363 GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false, 3364 /*DontDefer=*/false); 3365 return; 3366 } 3367 } else { 3368 const auto *VD = cast<VarDecl>(Global); 3369 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global."); 3370 if (VD->isThisDeclarationADefinition() != VarDecl::Definition && 3371 !Context.isMSStaticDataMemberInlineDefinition(VD)) { 3372 if (LangOpts.OpenMP) { 3373 // Emit declaration of the must-be-emitted declare target variable. 3374 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res = 3375 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) { 3376 bool UnifiedMemoryEnabled = 3377 getOpenMPRuntime().hasRequiresUnifiedSharedMemory(); 3378 if ((*Res == OMPDeclareTargetDeclAttr::MT_To || 3379 *Res == OMPDeclareTargetDeclAttr::MT_Enter) && 3380 !UnifiedMemoryEnabled) { 3381 (void)GetAddrOfGlobalVar(VD); 3382 } else { 3383 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) || 3384 ((*Res == OMPDeclareTargetDeclAttr::MT_To || 3385 *Res == OMPDeclareTargetDeclAttr::MT_Enter) && 3386 UnifiedMemoryEnabled)) && 3387 "Link clause or to clause with unified memory expected."); 3388 (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD); 3389 } 3390 3391 return; 3392 } 3393 } 3394 // If this declaration may have caused an inline variable definition to 3395 // change linkage, make sure that it's emitted. 3396 if (Context.getInlineVariableDefinitionKind(VD) == 3397 ASTContext::InlineVariableDefinitionKind::Strong) 3398 GetAddrOfGlobalVar(VD); 3399 return; 3400 } 3401 } 3402 3403 // Defer code generation to first use when possible, e.g. if this is an inline 3404 // function. If the global must always be emitted, do it eagerly if possible 3405 // to benefit from cache locality. 3406 if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) { 3407 // Emit the definition if it can't be deferred. 3408 EmitGlobalDefinition(GD); 3409 return; 3410 } 3411 3412 // If we're deferring emission of a C++ variable with an 3413 // initializer, remember the order in which it appeared in the file. 3414 if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) && 3415 cast<VarDecl>(Global)->hasInit()) { 3416 DelayedCXXInitPosition[Global] = CXXGlobalInits.size(); 3417 CXXGlobalInits.push_back(nullptr); 3418 } 3419 3420 StringRef MangledName = getMangledName(GD); 3421 if (GetGlobalValue(MangledName) != nullptr) { 3422 // The value has already been used and should therefore be emitted. 3423 addDeferredDeclToEmit(GD); 3424 } else if (MustBeEmitted(Global)) { 3425 // The value must be emitted, but cannot be emitted eagerly. 3426 assert(!MayBeEmittedEagerly(Global)); 3427 addDeferredDeclToEmit(GD); 3428 EmittedDeferredDecls[MangledName] = GD; 3429 } else { 3430 // Otherwise, remember that we saw a deferred decl with this name. The 3431 // first use of the mangled name will cause it to move into 3432 // DeferredDeclsToEmit. 3433 DeferredDecls[MangledName] = GD; 3434 } 3435 } 3436 3437 // Check if T is a class type with a destructor that's not dllimport. 3438 static bool HasNonDllImportDtor(QualType T) { 3439 if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>()) 3440 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) 3441 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>()) 3442 return true; 3443 3444 return false; 3445 } 3446 3447 namespace { 3448 struct FunctionIsDirectlyRecursive 3449 : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> { 3450 const StringRef Name; 3451 const Builtin::Context &BI; 3452 FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C) 3453 : Name(N), BI(C) {} 3454 3455 bool VisitCallExpr(const CallExpr *E) { 3456 const FunctionDecl *FD = E->getDirectCallee(); 3457 if (!FD) 3458 return false; 3459 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); 3460 if (Attr && Name == Attr->getLabel()) 3461 return true; 3462 unsigned BuiltinID = FD->getBuiltinID(); 3463 if (!BuiltinID || !BI.isLibFunction(BuiltinID)) 3464 return false; 3465 StringRef BuiltinName = BI.getName(BuiltinID); 3466 if (BuiltinName.startswith("__builtin_") && 3467 Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) { 3468 return true; 3469 } 3470 return false; 3471 } 3472 3473 bool VisitStmt(const Stmt *S) { 3474 for (const Stmt *Child : S->children()) 3475 if (Child && this->Visit(Child)) 3476 return true; 3477 return false; 3478 } 3479 }; 3480 3481 // Make sure we're not referencing non-imported vars or functions. 3482 struct DLLImportFunctionVisitor 3483 : public RecursiveASTVisitor<DLLImportFunctionVisitor> { 3484 bool SafeToInline = true; 3485 3486 bool shouldVisitImplicitCode() const { return true; } 3487 3488 bool VisitVarDecl(VarDecl *VD) { 3489 if (VD->getTLSKind()) { 3490 // A thread-local variable cannot be imported. 3491 SafeToInline = false; 3492 return SafeToInline; 3493 } 3494 3495 // A variable definition might imply a destructor call. 3496 if (VD->isThisDeclarationADefinition()) 3497 SafeToInline = !HasNonDllImportDtor(VD->getType()); 3498 3499 return SafeToInline; 3500 } 3501 3502 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { 3503 if (const auto *D = E->getTemporary()->getDestructor()) 3504 SafeToInline = D->hasAttr<DLLImportAttr>(); 3505 return SafeToInline; 3506 } 3507 3508 bool VisitDeclRefExpr(DeclRefExpr *E) { 3509 ValueDecl *VD = E->getDecl(); 3510 if (isa<FunctionDecl>(VD)) 3511 SafeToInline = VD->hasAttr<DLLImportAttr>(); 3512 else if (VarDecl *V = dyn_cast<VarDecl>(VD)) 3513 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>(); 3514 return SafeToInline; 3515 } 3516 3517 bool VisitCXXConstructExpr(CXXConstructExpr *E) { 3518 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>(); 3519 return SafeToInline; 3520 } 3521 3522 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { 3523 CXXMethodDecl *M = E->getMethodDecl(); 3524 if (!M) { 3525 // Call through a pointer to member function. This is safe to inline. 3526 SafeToInline = true; 3527 } else { 3528 SafeToInline = M->hasAttr<DLLImportAttr>(); 3529 } 3530 return SafeToInline; 3531 } 3532 3533 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { 3534 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>(); 3535 return SafeToInline; 3536 } 3537 3538 bool VisitCXXNewExpr(CXXNewExpr *E) { 3539 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>(); 3540 return SafeToInline; 3541 } 3542 }; 3543 } 3544 3545 // isTriviallyRecursive - Check if this function calls another 3546 // decl that, because of the asm attribute or the other decl being a builtin, 3547 // ends up pointing to itself. 3548 bool 3549 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) { 3550 StringRef Name; 3551 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) { 3552 // asm labels are a special kind of mangling we have to support. 3553 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>(); 3554 if (!Attr) 3555 return false; 3556 Name = Attr->getLabel(); 3557 } else { 3558 Name = FD->getName(); 3559 } 3560 3561 FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo); 3562 const Stmt *Body = FD->getBody(); 3563 return Body ? Walker.Visit(Body) : false; 3564 } 3565 3566 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) { 3567 if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage) 3568 return true; 3569 const auto *F = cast<FunctionDecl>(GD.getDecl()); 3570 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>()) 3571 return false; 3572 3573 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) { 3574 // Check whether it would be safe to inline this dllimport function. 3575 DLLImportFunctionVisitor Visitor; 3576 Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F)); 3577 if (!Visitor.SafeToInline) 3578 return false; 3579 3580 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) { 3581 // Implicit destructor invocations aren't captured in the AST, so the 3582 // check above can't see them. Check for them manually here. 3583 for (const Decl *Member : Dtor->getParent()->decls()) 3584 if (isa<FieldDecl>(Member)) 3585 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType())) 3586 return false; 3587 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases()) 3588 if (HasNonDllImportDtor(B.getType())) 3589 return false; 3590 } 3591 } 3592 3593 // Inline builtins declaration must be emitted. They often are fortified 3594 // functions. 3595 if (F->isInlineBuiltinDeclaration()) 3596 return true; 3597 3598 // PR9614. Avoid cases where the source code is lying to us. An available 3599 // externally function should have an equivalent function somewhere else, 3600 // but a function that calls itself through asm label/`__builtin_` trickery is 3601 // clearly not equivalent to the real implementation. 3602 // This happens in glibc's btowc and in some configure checks. 3603 return !isTriviallyRecursive(F); 3604 } 3605 3606 bool CodeGenModule::shouldOpportunisticallyEmitVTables() { 3607 return CodeGenOpts.OptimizationLevel > 0; 3608 } 3609 3610 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD, 3611 llvm::GlobalValue *GV) { 3612 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 3613 3614 if (FD->isCPUSpecificMultiVersion()) { 3615 auto *Spec = FD->getAttr<CPUSpecificAttr>(); 3616 for (unsigned I = 0; I < Spec->cpus_size(); ++I) 3617 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr); 3618 } else if (FD->isTargetClonesMultiVersion()) { 3619 auto *Clone = FD->getAttr<TargetClonesAttr>(); 3620 for (unsigned I = 0; I < Clone->featuresStrs_size(); ++I) 3621 if (Clone->isFirstOfVersion(I)) 3622 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr); 3623 // Ensure that the resolver function is also emitted. 3624 GetOrCreateMultiVersionResolver(GD); 3625 } else 3626 EmitGlobalFunctionDefinition(GD, GV); 3627 } 3628 3629 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) { 3630 const auto *D = cast<ValueDecl>(GD.getDecl()); 3631 3632 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(), 3633 Context.getSourceManager(), 3634 "Generating code for declaration"); 3635 3636 if (const auto *FD = dyn_cast<FunctionDecl>(D)) { 3637 // At -O0, don't generate IR for functions with available_externally 3638 // linkage. 3639 if (!shouldEmitFunction(GD)) 3640 return; 3641 3642 llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() { 3643 std::string Name; 3644 llvm::raw_string_ostream OS(Name); 3645 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(), 3646 /*Qualified=*/true); 3647 return Name; 3648 }); 3649 3650 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { 3651 // Make sure to emit the definition(s) before we emit the thunks. 3652 // This is necessary for the generation of certain thunks. 3653 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method)) 3654 ABI->emitCXXStructor(GD); 3655 else if (FD->isMultiVersion()) 3656 EmitMultiVersionFunctionDefinition(GD, GV); 3657 else 3658 EmitGlobalFunctionDefinition(GD, GV); 3659 3660 if (Method->isVirtual()) 3661 getVTables().EmitThunks(GD); 3662 3663 return; 3664 } 3665 3666 if (FD->isMultiVersion()) 3667 return EmitMultiVersionFunctionDefinition(GD, GV); 3668 return EmitGlobalFunctionDefinition(GD, GV); 3669 } 3670 3671 if (const auto *VD = dyn_cast<VarDecl>(D)) 3672 return EmitGlobalVarDefinition(VD, !VD->hasDefinition()); 3673 3674 llvm_unreachable("Invalid argument to EmitGlobalDefinition()"); 3675 } 3676 3677 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 3678 llvm::Function *NewFn); 3679 3680 static unsigned 3681 TargetMVPriority(const TargetInfo &TI, 3682 const CodeGenFunction::MultiVersionResolverOption &RO) { 3683 unsigned Priority = 0; 3684 unsigned NumFeatures = 0; 3685 for (StringRef Feat : RO.Conditions.Features) { 3686 Priority = std::max(Priority, TI.multiVersionSortPriority(Feat)); 3687 NumFeatures++; 3688 } 3689 3690 if (!RO.Conditions.Architecture.empty()) 3691 Priority = std::max( 3692 Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture)); 3693 3694 Priority += TI.multiVersionFeatureCost() * NumFeatures; 3695 3696 return Priority; 3697 } 3698 3699 // Multiversion functions should be at most 'WeakODRLinkage' so that a different 3700 // TU can forward declare the function without causing problems. Particularly 3701 // in the cases of CPUDispatch, this causes issues. This also makes sure we 3702 // work with internal linkage functions, so that the same function name can be 3703 // used with internal linkage in multiple TUs. 3704 llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM, 3705 GlobalDecl GD) { 3706 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 3707 if (FD->getFormalLinkage() == InternalLinkage) 3708 return llvm::GlobalValue::InternalLinkage; 3709 return llvm::GlobalValue::WeakODRLinkage; 3710 } 3711 3712 void CodeGenModule::emitMultiVersionFunctions() { 3713 std::vector<GlobalDecl> MVFuncsToEmit; 3714 MultiVersionFuncs.swap(MVFuncsToEmit); 3715 for (GlobalDecl GD : MVFuncsToEmit) { 3716 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 3717 assert(FD && "Expected a FunctionDecl"); 3718 3719 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; 3720 if (FD->isTargetMultiVersion()) { 3721 getContext().forEachMultiversionedFunctionVersion( 3722 FD, [this, &GD, &Options](const FunctionDecl *CurFD) { 3723 GlobalDecl CurGD{ 3724 (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)}; 3725 StringRef MangledName = getMangledName(CurGD); 3726 llvm::Constant *Func = GetGlobalValue(MangledName); 3727 if (!Func) { 3728 if (CurFD->isDefined()) { 3729 EmitGlobalFunctionDefinition(CurGD, nullptr); 3730 Func = GetGlobalValue(MangledName); 3731 } else { 3732 const CGFunctionInfo &FI = 3733 getTypes().arrangeGlobalDeclaration(GD); 3734 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 3735 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false, 3736 /*DontDefer=*/false, ForDefinition); 3737 } 3738 assert(Func && "This should have just been created"); 3739 } 3740 if (CurFD->getMultiVersionKind() == MultiVersionKind::Target) { 3741 const auto *TA = CurFD->getAttr<TargetAttr>(); 3742 llvm::SmallVector<StringRef, 8> Feats; 3743 TA->getAddedFeatures(Feats); 3744 Options.emplace_back(cast<llvm::Function>(Func), 3745 TA->getArchitecture(), Feats); 3746 } else { 3747 const auto *TVA = CurFD->getAttr<TargetVersionAttr>(); 3748 llvm::SmallVector<StringRef, 8> Feats; 3749 TVA->getFeatures(Feats); 3750 Options.emplace_back(cast<llvm::Function>(Func), 3751 /*Architecture*/ "", Feats); 3752 } 3753 }); 3754 } else if (FD->isTargetClonesMultiVersion()) { 3755 const auto *TC = FD->getAttr<TargetClonesAttr>(); 3756 for (unsigned VersionIndex = 0; VersionIndex < TC->featuresStrs_size(); 3757 ++VersionIndex) { 3758 if (!TC->isFirstOfVersion(VersionIndex)) 3759 continue; 3760 GlobalDecl CurGD{(FD->isDefined() ? FD->getDefinition() : FD), 3761 VersionIndex}; 3762 StringRef Version = TC->getFeatureStr(VersionIndex); 3763 StringRef MangledName = getMangledName(CurGD); 3764 llvm::Constant *Func = GetGlobalValue(MangledName); 3765 if (!Func) { 3766 if (FD->isDefined()) { 3767 EmitGlobalFunctionDefinition(CurGD, nullptr); 3768 Func = GetGlobalValue(MangledName); 3769 } else { 3770 const CGFunctionInfo &FI = 3771 getTypes().arrangeGlobalDeclaration(CurGD); 3772 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 3773 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false, 3774 /*DontDefer=*/false, ForDefinition); 3775 } 3776 assert(Func && "This should have just been created"); 3777 } 3778 3779 StringRef Architecture; 3780 llvm::SmallVector<StringRef, 1> Feature; 3781 3782 if (getTarget().getTriple().isAArch64()) { 3783 if (Version != "default") { 3784 llvm::SmallVector<StringRef, 8> VerFeats; 3785 Version.split(VerFeats, "+"); 3786 for (auto &CurFeat : VerFeats) 3787 Feature.push_back(CurFeat.trim()); 3788 } 3789 } else { 3790 if (Version.startswith("arch=")) 3791 Architecture = Version.drop_front(sizeof("arch=") - 1); 3792 else if (Version != "default") 3793 Feature.push_back(Version); 3794 } 3795 3796 Options.emplace_back(cast<llvm::Function>(Func), Architecture, Feature); 3797 } 3798 } else { 3799 assert(0 && "Expected a target or target_clones multiversion function"); 3800 continue; 3801 } 3802 3803 llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD); 3804 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant)) 3805 ResolverConstant = IFunc->getResolver(); 3806 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant); 3807 3808 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD)); 3809 3810 if (supportsCOMDAT()) 3811 ResolverFunc->setComdat( 3812 getModule().getOrInsertComdat(ResolverFunc->getName())); 3813 3814 const TargetInfo &TI = getTarget(); 3815 llvm::stable_sort( 3816 Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, 3817 const CodeGenFunction::MultiVersionResolverOption &RHS) { 3818 return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS); 3819 }); 3820 CodeGenFunction CGF(*this); 3821 CGF.EmitMultiVersionResolver(ResolverFunc, Options); 3822 } 3823 3824 // Ensure that any additions to the deferred decls list caused by emitting a 3825 // variant are emitted. This can happen when the variant itself is inline and 3826 // calls a function without linkage. 3827 if (!MVFuncsToEmit.empty()) 3828 EmitDeferred(); 3829 3830 // Ensure that any additions to the multiversion funcs list from either the 3831 // deferred decls or the multiversion functions themselves are emitted. 3832 if (!MultiVersionFuncs.empty()) 3833 emitMultiVersionFunctions(); 3834 } 3835 3836 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) { 3837 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 3838 assert(FD && "Not a FunctionDecl?"); 3839 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?"); 3840 const auto *DD = FD->getAttr<CPUDispatchAttr>(); 3841 assert(DD && "Not a cpu_dispatch Function?"); 3842 3843 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 3844 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI); 3845 3846 StringRef ResolverName = getMangledName(GD); 3847 UpdateMultiVersionNames(GD, FD, ResolverName); 3848 3849 llvm::Type *ResolverType; 3850 GlobalDecl ResolverGD; 3851 if (getTarget().supportsIFunc()) { 3852 ResolverType = llvm::FunctionType::get( 3853 llvm::PointerType::get(DeclTy, 3854 getTypes().getTargetAddressSpace(FD->getType())), 3855 false); 3856 } 3857 else { 3858 ResolverType = DeclTy; 3859 ResolverGD = GD; 3860 } 3861 3862 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction( 3863 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false)); 3864 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD)); 3865 if (supportsCOMDAT()) 3866 ResolverFunc->setComdat( 3867 getModule().getOrInsertComdat(ResolverFunc->getName())); 3868 3869 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; 3870 const TargetInfo &Target = getTarget(); 3871 unsigned Index = 0; 3872 for (const IdentifierInfo *II : DD->cpus()) { 3873 // Get the name of the target function so we can look it up/create it. 3874 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) + 3875 getCPUSpecificMangling(*this, II->getName()); 3876 3877 llvm::Constant *Func = GetGlobalValue(MangledName); 3878 3879 if (!Func) { 3880 GlobalDecl ExistingDecl = Manglings.lookup(MangledName); 3881 if (ExistingDecl.getDecl() && 3882 ExistingDecl.getDecl()->getAsFunction()->isDefined()) { 3883 EmitGlobalFunctionDefinition(ExistingDecl, nullptr); 3884 Func = GetGlobalValue(MangledName); 3885 } else { 3886 if (!ExistingDecl.getDecl()) 3887 ExistingDecl = GD.getWithMultiVersionIndex(Index); 3888 3889 Func = GetOrCreateLLVMFunction( 3890 MangledName, DeclTy, ExistingDecl, 3891 /*ForVTable=*/false, /*DontDefer=*/true, 3892 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition); 3893 } 3894 } 3895 3896 llvm::SmallVector<StringRef, 32> Features; 3897 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features); 3898 llvm::transform(Features, Features.begin(), 3899 [](StringRef Str) { return Str.substr(1); }); 3900 llvm::erase_if(Features, [&Target](StringRef Feat) { 3901 return !Target.validateCpuSupports(Feat); 3902 }); 3903 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features); 3904 ++Index; 3905 } 3906 3907 llvm::stable_sort( 3908 Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS, 3909 const CodeGenFunction::MultiVersionResolverOption &RHS) { 3910 return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) > 3911 llvm::X86::getCpuSupportsMask(RHS.Conditions.Features); 3912 }); 3913 3914 // If the list contains multiple 'default' versions, such as when it contains 3915 // 'pentium' and 'generic', don't emit the call to the generic one (since we 3916 // always run on at least a 'pentium'). We do this by deleting the 'least 3917 // advanced' (read, lowest mangling letter). 3918 while (Options.size() > 1 && 3919 llvm::X86::getCpuSupportsMask( 3920 (Options.end() - 2)->Conditions.Features) == 0) { 3921 StringRef LHSName = (Options.end() - 2)->Function->getName(); 3922 StringRef RHSName = (Options.end() - 1)->Function->getName(); 3923 if (LHSName.compare(RHSName) < 0) 3924 Options.erase(Options.end() - 2); 3925 else 3926 Options.erase(Options.end() - 1); 3927 } 3928 3929 CodeGenFunction CGF(*this); 3930 CGF.EmitMultiVersionResolver(ResolverFunc, Options); 3931 3932 if (getTarget().supportsIFunc()) { 3933 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD); 3934 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD)); 3935 3936 // Fix up function declarations that were created for cpu_specific before 3937 // cpu_dispatch was known 3938 if (!isa<llvm::GlobalIFunc>(IFunc)) { 3939 assert(cast<llvm::Function>(IFunc)->isDeclaration()); 3940 auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc, 3941 &getModule()); 3942 GI->takeName(IFunc); 3943 IFunc->replaceAllUsesWith(GI); 3944 IFunc->eraseFromParent(); 3945 IFunc = GI; 3946 } 3947 3948 std::string AliasName = getMangledNameImpl( 3949 *this, GD, FD, /*OmitMultiVersionMangling=*/true); 3950 llvm::Constant *AliasFunc = GetGlobalValue(AliasName); 3951 if (!AliasFunc) { 3952 auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc, 3953 &getModule()); 3954 SetCommonAttributes(GD, GA); 3955 } 3956 } 3957 } 3958 3959 /// If a dispatcher for the specified mangled name is not in the module, create 3960 /// and return an llvm Function with the specified type. 3961 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) { 3962 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 3963 assert(FD && "Not a FunctionDecl?"); 3964 3965 std::string MangledName = 3966 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true); 3967 3968 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has 3969 // a separate resolver). 3970 std::string ResolverName = MangledName; 3971 if (getTarget().supportsIFunc()) 3972 ResolverName += ".ifunc"; 3973 else if (FD->isTargetMultiVersion()) 3974 ResolverName += ".resolver"; 3975 3976 // If the resolver has already been created, just return it. 3977 if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName)) 3978 return ResolverGV; 3979 3980 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 3981 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI); 3982 3983 // The resolver needs to be created. For target and target_clones, defer 3984 // creation until the end of the TU. 3985 if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion()) 3986 MultiVersionFuncs.push_back(GD); 3987 3988 // For cpu_specific, don't create an ifunc yet because we don't know if the 3989 // cpu_dispatch will be emitted in this translation unit. 3990 if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) { 3991 llvm::Type *ResolverType = llvm::FunctionType::get( 3992 llvm::PointerType::get(DeclTy, 3993 getTypes().getTargetAddressSpace(FD->getType())), 3994 false); 3995 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 3996 MangledName + ".resolver", ResolverType, GlobalDecl{}, 3997 /*ForVTable=*/false); 3998 llvm::GlobalIFunc *GIF = 3999 llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD), 4000 "", Resolver, &getModule()); 4001 GIF->setName(ResolverName); 4002 SetCommonAttributes(FD, GIF); 4003 4004 return GIF; 4005 } 4006 4007 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 4008 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false); 4009 assert(isa<llvm::GlobalValue>(Resolver) && 4010 "Resolver should be created for the first time"); 4011 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver)); 4012 return Resolver; 4013 } 4014 4015 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the 4016 /// module, create and return an llvm Function with the specified type. If there 4017 /// is something in the module with the specified name, return it potentially 4018 /// bitcasted to the right type. 4019 /// 4020 /// If D is non-null, it specifies a decl that correspond to this. This is used 4021 /// to set the attributes on the function when it is first created. 4022 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( 4023 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, 4024 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs, 4025 ForDefinition_t IsForDefinition) { 4026 const Decl *D = GD.getDecl(); 4027 4028 // Any attempts to use a MultiVersion function should result in retrieving 4029 // the iFunc instead. Name Mangling will handle the rest of the changes. 4030 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) { 4031 // For the device mark the function as one that should be emitted. 4032 if (getLangOpts().OpenMPIsDevice && OpenMPRuntime && 4033 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() && 4034 !DontDefer && !IsForDefinition) { 4035 if (const FunctionDecl *FDDef = FD->getDefinition()) { 4036 GlobalDecl GDDef; 4037 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef)) 4038 GDDef = GlobalDecl(CD, GD.getCtorType()); 4039 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef)) 4040 GDDef = GlobalDecl(DD, GD.getDtorType()); 4041 else 4042 GDDef = GlobalDecl(FDDef); 4043 EmitGlobal(GDDef); 4044 } 4045 } 4046 4047 if (FD->isMultiVersion()) { 4048 UpdateMultiVersionNames(GD, FD, MangledName); 4049 if (!IsForDefinition) 4050 return GetOrCreateMultiVersionResolver(GD); 4051 } 4052 } 4053 4054 // Lookup the entry, lazily creating it if necessary. 4055 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 4056 if (Entry) { 4057 if (WeakRefReferences.erase(Entry)) { 4058 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); 4059 if (FD && !FD->hasAttr<WeakAttr>()) 4060 Entry->setLinkage(llvm::Function::ExternalLinkage); 4061 } 4062 4063 // Handle dropped DLL attributes. 4064 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() && 4065 !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) { 4066 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 4067 setDSOLocal(Entry); 4068 } 4069 4070 // If there are two attempts to define the same mangled name, issue an 4071 // error. 4072 if (IsForDefinition && !Entry->isDeclaration()) { 4073 GlobalDecl OtherGD; 4074 // Check that GD is not yet in DiagnosedConflictingDefinitions is required 4075 // to make sure that we issue an error only once. 4076 if (lookupRepresentativeDecl(MangledName, OtherGD) && 4077 (GD.getCanonicalDecl().getDecl() != 4078 OtherGD.getCanonicalDecl().getDecl()) && 4079 DiagnosedConflictingDefinitions.insert(GD).second) { 4080 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) 4081 << MangledName; 4082 getDiags().Report(OtherGD.getDecl()->getLocation(), 4083 diag::note_previous_definition); 4084 } 4085 } 4086 4087 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && 4088 (Entry->getValueType() == Ty)) { 4089 return Entry; 4090 } 4091 4092 // Make sure the result is of the correct type. 4093 // (If function is requested for a definition, we always need to create a new 4094 // function, not just return a bitcast.) 4095 if (!IsForDefinition) 4096 return llvm::ConstantExpr::getBitCast( 4097 Entry, Ty->getPointerTo(Entry->getAddressSpace())); 4098 } 4099 4100 // This function doesn't have a complete type (for example, the return 4101 // type is an incomplete struct). Use a fake type instead, and make 4102 // sure not to try to set attributes. 4103 bool IsIncompleteFunction = false; 4104 4105 llvm::FunctionType *FTy; 4106 if (isa<llvm::FunctionType>(Ty)) { 4107 FTy = cast<llvm::FunctionType>(Ty); 4108 } else { 4109 FTy = llvm::FunctionType::get(VoidTy, false); 4110 IsIncompleteFunction = true; 4111 } 4112 4113 llvm::Function *F = 4114 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, 4115 Entry ? StringRef() : MangledName, &getModule()); 4116 4117 // If we already created a function with the same mangled name (but different 4118 // type) before, take its name and add it to the list of functions to be 4119 // replaced with F at the end of CodeGen. 4120 // 4121 // This happens if there is a prototype for a function (e.g. "int f()") and 4122 // then a definition of a different type (e.g. "int f(int x)"). 4123 if (Entry) { 4124 F->takeName(Entry); 4125 4126 // This might be an implementation of a function without a prototype, in 4127 // which case, try to do special replacement of calls which match the new 4128 // prototype. The really key thing here is that we also potentially drop 4129 // arguments from the call site so as to make a direct call, which makes the 4130 // inliner happier and suppresses a number of optimizer warnings (!) about 4131 // dropping arguments. 4132 if (!Entry->use_empty()) { 4133 ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); 4134 Entry->removeDeadConstantUsers(); 4135 } 4136 4137 llvm::Constant *BC = llvm::ConstantExpr::getBitCast( 4138 F, Entry->getValueType()->getPointerTo(Entry->getAddressSpace())); 4139 addGlobalValReplacement(Entry, BC); 4140 } 4141 4142 assert(F->getName() == MangledName && "name was uniqued!"); 4143 if (D) 4144 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); 4145 if (ExtraAttrs.hasFnAttrs()) { 4146 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs()); 4147 F->addFnAttrs(B); 4148 } 4149 4150 if (!DontDefer) { 4151 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to 4152 // each other bottoming out with the base dtor. Therefore we emit non-base 4153 // dtors on usage, even if there is no dtor definition in the TU. 4154 if (isa_and_nonnull<CXXDestructorDecl>(D) && 4155 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), 4156 GD.getDtorType())) 4157 addDeferredDeclToEmit(GD); 4158 4159 // This is the first use or definition of a mangled name. If there is a 4160 // deferred decl with this name, remember that we need to emit it at the end 4161 // of the file. 4162 auto DDI = DeferredDecls.find(MangledName); 4163 if (DDI != DeferredDecls.end()) { 4164 // Move the potentially referenced deferred decl to the 4165 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we 4166 // don't need it anymore). 4167 addDeferredDeclToEmit(DDI->second); 4168 EmittedDeferredDecls[DDI->first] = DDI->second; 4169 DeferredDecls.erase(DDI); 4170 4171 // Otherwise, there are cases we have to worry about where we're 4172 // using a declaration for which we must emit a definition but where 4173 // we might not find a top-level definition: 4174 // - member functions defined inline in their classes 4175 // - friend functions defined inline in some class 4176 // - special member functions with implicit definitions 4177 // If we ever change our AST traversal to walk into class methods, 4178 // this will be unnecessary. 4179 // 4180 // We also don't emit a definition for a function if it's going to be an 4181 // entry in a vtable, unless it's already marked as used. 4182 } else if (getLangOpts().CPlusPlus && D) { 4183 // Look for a declaration that's lexically in a record. 4184 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; 4185 FD = FD->getPreviousDecl()) { 4186 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { 4187 if (FD->doesThisDeclarationHaveABody()) { 4188 addDeferredDeclToEmit(GD.getWithDecl(FD)); 4189 break; 4190 } 4191 } 4192 } 4193 } 4194 } 4195 4196 // Make sure the result is of the requested type. 4197 if (!IsIncompleteFunction) { 4198 assert(F->getFunctionType() == Ty); 4199 return F; 4200 } 4201 4202 return llvm::ConstantExpr::getBitCast(F, 4203 Ty->getPointerTo(F->getAddressSpace())); 4204 } 4205 4206 /// GetAddrOfFunction - Return the address of the given function. If Ty is 4207 /// non-null, then this function will use the specified type if it has to 4208 /// create it (this occurs when we see a definition of the function). 4209 llvm::Constant *CodeGenModule::GetAddrOfFunction(GlobalDecl GD, 4210 llvm::Type *Ty, 4211 bool ForVTable, 4212 bool DontDefer, 4213 ForDefinition_t IsForDefinition) { 4214 assert(!cast<FunctionDecl>(GD.getDecl())->isConsteval() && 4215 "consteval function should never be emitted"); 4216 // If there was no specific requested type, just convert it now. 4217 if (!Ty) { 4218 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 4219 Ty = getTypes().ConvertType(FD->getType()); 4220 } 4221 4222 // Devirtualized destructor calls may come through here instead of via 4223 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead 4224 // of the complete destructor when necessary. 4225 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) { 4226 if (getTarget().getCXXABI().isMicrosoft() && 4227 GD.getDtorType() == Dtor_Complete && 4228 DD->getParent()->getNumVBases() == 0) 4229 GD = GlobalDecl(DD, Dtor_Base); 4230 } 4231 4232 StringRef MangledName = getMangledName(GD); 4233 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, 4234 /*IsThunk=*/false, llvm::AttributeList(), 4235 IsForDefinition); 4236 // Returns kernel handle for HIP kernel stub function. 4237 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice && 4238 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) { 4239 auto *Handle = getCUDARuntime().getKernelHandle( 4240 cast<llvm::Function>(F->stripPointerCasts()), GD); 4241 if (IsForDefinition) 4242 return F; 4243 return llvm::ConstantExpr::getBitCast(Handle, Ty->getPointerTo()); 4244 } 4245 return F; 4246 } 4247 4248 llvm::Constant *CodeGenModule::GetFunctionStart(const ValueDecl *Decl) { 4249 llvm::GlobalValue *F = 4250 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts()); 4251 4252 return llvm::ConstantExpr::getBitCast( 4253 llvm::NoCFIValue::get(F), 4254 llvm::Type::getInt8PtrTy(VMContext, F->getAddressSpace())); 4255 } 4256 4257 static const FunctionDecl * 4258 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { 4259 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); 4260 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); 4261 4262 IdentifierInfo &CII = C.Idents.get(Name); 4263 for (const auto *Result : DC->lookup(&CII)) 4264 if (const auto *FD = dyn_cast<FunctionDecl>(Result)) 4265 return FD; 4266 4267 if (!C.getLangOpts().CPlusPlus) 4268 return nullptr; 4269 4270 // Demangle the premangled name from getTerminateFn() 4271 IdentifierInfo &CXXII = 4272 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ") 4273 ? C.Idents.get("terminate") 4274 : C.Idents.get(Name); 4275 4276 for (const auto &N : {"__cxxabiv1", "std"}) { 4277 IdentifierInfo &NS = C.Idents.get(N); 4278 for (const auto *Result : DC->lookup(&NS)) { 4279 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); 4280 if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result)) 4281 for (const auto *Result : LSD->lookup(&NS)) 4282 if ((ND = dyn_cast<NamespaceDecl>(Result))) 4283 break; 4284 4285 if (ND) 4286 for (const auto *Result : ND->lookup(&CXXII)) 4287 if (const auto *FD = dyn_cast<FunctionDecl>(Result)) 4288 return FD; 4289 } 4290 } 4291 4292 return nullptr; 4293 } 4294 4295 /// CreateRuntimeFunction - Create a new runtime function with the specified 4296 /// type and name. 4297 llvm::FunctionCallee 4298 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, 4299 llvm::AttributeList ExtraAttrs, bool Local, 4300 bool AssumeConvergent) { 4301 if (AssumeConvergent) { 4302 ExtraAttrs = 4303 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent); 4304 } 4305 4306 llvm::Constant *C = 4307 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, 4308 /*DontDefer=*/false, /*IsThunk=*/false, 4309 ExtraAttrs); 4310 4311 if (auto *F = dyn_cast<llvm::Function>(C)) { 4312 if (F->empty()) { 4313 F->setCallingConv(getRuntimeCC()); 4314 4315 // In Windows Itanium environments, try to mark runtime functions 4316 // dllimport. For Mingw and MSVC, don't. We don't really know if the user 4317 // will link their standard library statically or dynamically. Marking 4318 // functions imported when they are not imported can cause linker errors 4319 // and warnings. 4320 if (!Local && getTriple().isWindowsItaniumEnvironment() && 4321 !getCodeGenOpts().LTOVisibilityPublicStd) { 4322 const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); 4323 if (!FD || FD->hasAttr<DLLImportAttr>()) { 4324 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 4325 F->setLinkage(llvm::GlobalValue::ExternalLinkage); 4326 } 4327 } 4328 setDSOLocal(F); 4329 } 4330 } 4331 4332 return {FTy, C}; 4333 } 4334 4335 /// isTypeConstant - Determine whether an object of this type can be emitted 4336 /// as a constant. 4337 /// 4338 /// If ExcludeCtor is true, the duration when the object's constructor runs 4339 /// will not be considered. The caller will need to verify that the object is 4340 /// not written to during its construction. ExcludeDtor works similarly. 4341 bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor, 4342 bool ExcludeDtor) { 4343 if (!Ty.isConstant(Context) && !Ty->isReferenceType()) 4344 return false; 4345 4346 if (Context.getLangOpts().CPlusPlus) { 4347 if (const CXXRecordDecl *Record 4348 = Context.getBaseElementType(Ty)->getAsCXXRecordDecl()) 4349 return ExcludeCtor && !Record->hasMutableFields() && 4350 (Record->hasTrivialDestructor() || ExcludeDtor); 4351 } 4352 4353 return true; 4354 } 4355 4356 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 4357 /// create and return an llvm GlobalVariable with the specified type and address 4358 /// space. If there is something in the module with the specified name, return 4359 /// it potentially bitcasted to the right type. 4360 /// 4361 /// If D is non-null, it specifies a decl that correspond to this. This is used 4362 /// to set the attributes on the global when it is first created. 4363 /// 4364 /// If IsForDefinition is true, it is guaranteed that an actual global with 4365 /// type Ty will be returned, not conversion of a variable with the same 4366 /// mangled name but some other type. 4367 llvm::Constant * 4368 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, 4369 LangAS AddrSpace, const VarDecl *D, 4370 ForDefinition_t IsForDefinition) { 4371 // Lookup the entry, lazily creating it if necessary. 4372 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 4373 unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace); 4374 if (Entry) { 4375 if (WeakRefReferences.erase(Entry)) { 4376 if (D && !D->hasAttr<WeakAttr>()) 4377 Entry->setLinkage(llvm::Function::ExternalLinkage); 4378 } 4379 4380 // Handle dropped DLL attributes. 4381 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() && 4382 !shouldMapVisibilityToDLLExport(D)) 4383 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 4384 4385 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) 4386 getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); 4387 4388 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS) 4389 return Entry; 4390 4391 // If there are two attempts to define the same mangled name, issue an 4392 // error. 4393 if (IsForDefinition && !Entry->isDeclaration()) { 4394 GlobalDecl OtherGD; 4395 const VarDecl *OtherD; 4396 4397 // Check that D is not yet in DiagnosedConflictingDefinitions is required 4398 // to make sure that we issue an error only once. 4399 if (D && lookupRepresentativeDecl(MangledName, OtherGD) && 4400 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && 4401 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && 4402 OtherD->hasInit() && 4403 DiagnosedConflictingDefinitions.insert(D).second) { 4404 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) 4405 << MangledName; 4406 getDiags().Report(OtherGD.getDecl()->getLocation(), 4407 diag::note_previous_definition); 4408 } 4409 } 4410 4411 // Make sure the result is of the correct type. 4412 if (Entry->getType()->getAddressSpace() != TargetAS) { 4413 return llvm::ConstantExpr::getAddrSpaceCast(Entry, 4414 Ty->getPointerTo(TargetAS)); 4415 } 4416 4417 // (If global is requested for a definition, we always need to create a new 4418 // global, not just return a bitcast.) 4419 if (!IsForDefinition) 4420 return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo(TargetAS)); 4421 } 4422 4423 auto DAddrSpace = GetGlobalVarAddressSpace(D); 4424 4425 auto *GV = new llvm::GlobalVariable( 4426 getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr, 4427 MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal, 4428 getContext().getTargetAddressSpace(DAddrSpace)); 4429 4430 // If we already created a global with the same mangled name (but different 4431 // type) before, take its name and remove it from its parent. 4432 if (Entry) { 4433 GV->takeName(Entry); 4434 4435 if (!Entry->use_empty()) { 4436 llvm::Constant *NewPtrForOldDecl = 4437 llvm::ConstantExpr::getBitCast(GV, Entry->getType()); 4438 Entry->replaceAllUsesWith(NewPtrForOldDecl); 4439 } 4440 4441 Entry->eraseFromParent(); 4442 } 4443 4444 // This is the first use or definition of a mangled name. If there is a 4445 // deferred decl with this name, remember that we need to emit it at the end 4446 // of the file. 4447 auto DDI = DeferredDecls.find(MangledName); 4448 if (DDI != DeferredDecls.end()) { 4449 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 4450 // list, and remove it from DeferredDecls (since we don't need it anymore). 4451 addDeferredDeclToEmit(DDI->second); 4452 EmittedDeferredDecls[DDI->first] = DDI->second; 4453 DeferredDecls.erase(DDI); 4454 } 4455 4456 // Handle things which are present even on external declarations. 4457 if (D) { 4458 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd) 4459 getOpenMPRuntime().registerTargetGlobalVariable(D, GV); 4460 4461 // FIXME: This code is overly simple and should be merged with other global 4462 // handling. 4463 GV->setConstant(isTypeConstant(D->getType(), false, false)); 4464 4465 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign()); 4466 4467 setLinkageForGV(GV, D); 4468 4469 if (D->getTLSKind()) { 4470 if (D->getTLSKind() == VarDecl::TLS_Dynamic) 4471 CXXThreadLocals.push_back(D); 4472 setTLSMode(GV, *D); 4473 } 4474 4475 setGVProperties(GV, D); 4476 4477 // If required by the ABI, treat declarations of static data members with 4478 // inline initializers as definitions. 4479 if (getContext().isMSStaticDataMemberInlineDefinition(D)) { 4480 EmitGlobalVarDefinition(D); 4481 } 4482 4483 // Emit section information for extern variables. 4484 if (D->hasExternalStorage()) { 4485 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 4486 GV->setSection(SA->getName()); 4487 } 4488 4489 // Handle XCore specific ABI requirements. 4490 if (getTriple().getArch() == llvm::Triple::xcore && 4491 D->getLanguageLinkage() == CLanguageLinkage && 4492 D->getType().isConstant(Context) && 4493 isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) 4494 GV->setSection(".cp.rodata"); 4495 4496 // Check if we a have a const declaration with an initializer, we may be 4497 // able to emit it as available_externally to expose it's value to the 4498 // optimizer. 4499 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && 4500 D->getType().isConstQualified() && !GV->hasInitializer() && 4501 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { 4502 const auto *Record = 4503 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); 4504 bool HasMutableFields = Record && Record->hasMutableFields(); 4505 if (!HasMutableFields) { 4506 const VarDecl *InitDecl; 4507 const Expr *InitExpr = D->getAnyInitializer(InitDecl); 4508 if (InitExpr) { 4509 ConstantEmitter emitter(*this); 4510 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl); 4511 if (Init) { 4512 auto *InitType = Init->getType(); 4513 if (GV->getValueType() != InitType) { 4514 // The type of the initializer does not match the definition. 4515 // This happens when an initializer has a different type from 4516 // the type of the global (because of padding at the end of a 4517 // structure for instance). 4518 GV->setName(StringRef()); 4519 // Make a new global with the correct type, this is now guaranteed 4520 // to work. 4521 auto *NewGV = cast<llvm::GlobalVariable>( 4522 GetAddrOfGlobalVar(D, InitType, IsForDefinition) 4523 ->stripPointerCasts()); 4524 4525 // Erase the old global, since it is no longer used. 4526 GV->eraseFromParent(); 4527 GV = NewGV; 4528 } else { 4529 GV->setInitializer(Init); 4530 GV->setConstant(true); 4531 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); 4532 } 4533 emitter.finalize(GV); 4534 } 4535 } 4536 } 4537 } 4538 } 4539 4540 if (GV->isDeclaration()) { 4541 getTargetCodeGenInfo().setTargetAttributes(D, GV, *this); 4542 // External HIP managed variables needed to be recorded for transformation 4543 // in both device and host compilations. 4544 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() && 4545 D->hasExternalStorage()) 4546 getCUDARuntime().handleVarRegistration(D, *GV); 4547 } 4548 4549 if (D) 4550 SanitizerMD->reportGlobal(GV, *D); 4551 4552 LangAS ExpectedAS = 4553 D ? D->getType().getAddressSpace() 4554 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); 4555 assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS); 4556 if (DAddrSpace != ExpectedAS) { 4557 return getTargetCodeGenInfo().performAddrSpaceCast( 4558 *this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(TargetAS)); 4559 } 4560 4561 return GV; 4562 } 4563 4564 llvm::Constant * 4565 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) { 4566 const Decl *D = GD.getDecl(); 4567 4568 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) 4569 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr, 4570 /*DontDefer=*/false, IsForDefinition); 4571 4572 if (isa<CXXMethodDecl>(D)) { 4573 auto FInfo = 4574 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D)); 4575 auto Ty = getTypes().GetFunctionType(*FInfo); 4576 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, 4577 IsForDefinition); 4578 } 4579 4580 if (isa<FunctionDecl>(D)) { 4581 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 4582 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 4583 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, 4584 IsForDefinition); 4585 } 4586 4587 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition); 4588 } 4589 4590 llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable( 4591 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, 4592 llvm::Align Alignment) { 4593 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); 4594 llvm::GlobalVariable *OldGV = nullptr; 4595 4596 if (GV) { 4597 // Check if the variable has the right type. 4598 if (GV->getValueType() == Ty) 4599 return GV; 4600 4601 // Because C++ name mangling, the only way we can end up with an already 4602 // existing global with the same name is if it has been declared extern "C". 4603 assert(GV->isDeclaration() && "Declaration has wrong type!"); 4604 OldGV = GV; 4605 } 4606 4607 // Create a new variable. 4608 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, 4609 Linkage, nullptr, Name); 4610 4611 if (OldGV) { 4612 // Replace occurrences of the old variable if needed. 4613 GV->takeName(OldGV); 4614 4615 if (!OldGV->use_empty()) { 4616 llvm::Constant *NewPtrForOldDecl = 4617 llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); 4618 OldGV->replaceAllUsesWith(NewPtrForOldDecl); 4619 } 4620 4621 OldGV->eraseFromParent(); 4622 } 4623 4624 if (supportsCOMDAT() && GV->isWeakForLinker() && 4625 !GV->hasAvailableExternallyLinkage()) 4626 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 4627 4628 GV->setAlignment(Alignment); 4629 4630 return GV; 4631 } 4632 4633 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 4634 /// given global variable. If Ty is non-null and if the global doesn't exist, 4635 /// then it will be created with the specified type instead of whatever the 4636 /// normal requested type would be. If IsForDefinition is true, it is guaranteed 4637 /// that an actual global with type Ty will be returned, not conversion of a 4638 /// variable with the same mangled name but some other type. 4639 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 4640 llvm::Type *Ty, 4641 ForDefinition_t IsForDefinition) { 4642 assert(D->hasGlobalStorage() && "Not a global variable"); 4643 QualType ASTTy = D->getType(); 4644 if (!Ty) 4645 Ty = getTypes().ConvertTypeForMem(ASTTy); 4646 4647 StringRef MangledName = getMangledName(D); 4648 return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D, 4649 IsForDefinition); 4650 } 4651 4652 /// CreateRuntimeVariable - Create a new runtime global variable with the 4653 /// specified type and name. 4654 llvm::Constant * 4655 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, 4656 StringRef Name) { 4657 LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global 4658 : LangAS::Default; 4659 auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr); 4660 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts())); 4661 return Ret; 4662 } 4663 4664 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 4665 assert(!D->getInit() && "Cannot emit definite definitions here!"); 4666 4667 StringRef MangledName = getMangledName(D); 4668 llvm::GlobalValue *GV = GetGlobalValue(MangledName); 4669 4670 // We already have a definition, not declaration, with the same mangled name. 4671 // Emitting of declaration is not required (and actually overwrites emitted 4672 // definition). 4673 if (GV && !GV->isDeclaration()) 4674 return; 4675 4676 // If we have not seen a reference to this variable yet, place it into the 4677 // deferred declarations table to be emitted if needed later. 4678 if (!MustBeEmitted(D) && !GV) { 4679 DeferredDecls[MangledName] = D; 4680 return; 4681 } 4682 4683 // The tentative definition is the only definition. 4684 EmitGlobalVarDefinition(D); 4685 } 4686 4687 void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) { 4688 EmitExternalVarDeclaration(D); 4689 } 4690 4691 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { 4692 return Context.toCharUnitsFromBits( 4693 getDataLayout().getTypeStoreSizeInBits(Ty)); 4694 } 4695 4696 LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { 4697 if (LangOpts.OpenCL) { 4698 LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global; 4699 assert(AS == LangAS::opencl_global || 4700 AS == LangAS::opencl_global_device || 4701 AS == LangAS::opencl_global_host || 4702 AS == LangAS::opencl_constant || 4703 AS == LangAS::opencl_local || 4704 AS >= LangAS::FirstTargetAddressSpace); 4705 return AS; 4706 } 4707 4708 if (LangOpts.SYCLIsDevice && 4709 (!D || D->getType().getAddressSpace() == LangAS::Default)) 4710 return LangAS::sycl_global; 4711 4712 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { 4713 if (D) { 4714 if (D->hasAttr<CUDAConstantAttr>()) 4715 return LangAS::cuda_constant; 4716 if (D->hasAttr<CUDASharedAttr>()) 4717 return LangAS::cuda_shared; 4718 if (D->hasAttr<CUDADeviceAttr>()) 4719 return LangAS::cuda_device; 4720 if (D->getType().isConstQualified()) 4721 return LangAS::cuda_constant; 4722 } 4723 return LangAS::cuda_device; 4724 } 4725 4726 if (LangOpts.OpenMP) { 4727 LangAS AS; 4728 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS)) 4729 return AS; 4730 } 4731 return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); 4732 } 4733 4734 LangAS CodeGenModule::GetGlobalConstantAddressSpace() const { 4735 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. 4736 if (LangOpts.OpenCL) 4737 return LangAS::opencl_constant; 4738 if (LangOpts.SYCLIsDevice) 4739 return LangAS::sycl_global; 4740 if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV()) 4741 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V) 4742 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up 4743 // with OpVariable instructions with Generic storage class which is not 4744 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V 4745 // UniformConstant storage class is not viable as pointers to it may not be 4746 // casted to Generic pointers which are used to model HIP's "flat" pointers. 4747 return LangAS::cuda_device; 4748 if (auto AS = getTarget().getConstantAddressSpace()) 4749 return *AS; 4750 return LangAS::Default; 4751 } 4752 4753 // In address space agnostic languages, string literals are in default address 4754 // space in AST. However, certain targets (e.g. amdgcn) request them to be 4755 // emitted in constant address space in LLVM IR. To be consistent with other 4756 // parts of AST, string literal global variables in constant address space 4757 // need to be casted to default address space before being put into address 4758 // map and referenced by other part of CodeGen. 4759 // In OpenCL, string literals are in constant address space in AST, therefore 4760 // they should not be casted to default address space. 4761 static llvm::Constant * 4762 castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, 4763 llvm::GlobalVariable *GV) { 4764 llvm::Constant *Cast = GV; 4765 if (!CGM.getLangOpts().OpenCL) { 4766 auto AS = CGM.GetGlobalConstantAddressSpace(); 4767 if (AS != LangAS::Default) 4768 Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( 4769 CGM, GV, AS, LangAS::Default, 4770 GV->getValueType()->getPointerTo( 4771 CGM.getContext().getTargetAddressSpace(LangAS::Default))); 4772 } 4773 return Cast; 4774 } 4775 4776 template<typename SomeDecl> 4777 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, 4778 llvm::GlobalValue *GV) { 4779 if (!getLangOpts().CPlusPlus) 4780 return; 4781 4782 // Must have 'used' attribute, or else inline assembly can't rely on 4783 // the name existing. 4784 if (!D->template hasAttr<UsedAttr>()) 4785 return; 4786 4787 // Must have internal linkage and an ordinary name. 4788 if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage) 4789 return; 4790 4791 // Must be in an extern "C" context. Entities declared directly within 4792 // a record are not extern "C" even if the record is in such a context. 4793 const SomeDecl *First = D->getFirstDecl(); 4794 if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) 4795 return; 4796 4797 // OK, this is an internal linkage entity inside an extern "C" linkage 4798 // specification. Make a note of that so we can give it the "expected" 4799 // mangled name if nothing else is using that name. 4800 std::pair<StaticExternCMap::iterator, bool> R = 4801 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); 4802 4803 // If we have multiple internal linkage entities with the same name 4804 // in extern "C" regions, none of them gets that name. 4805 if (!R.second) 4806 R.first->second = nullptr; 4807 } 4808 4809 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { 4810 if (!CGM.supportsCOMDAT()) 4811 return false; 4812 4813 if (D.hasAttr<SelectAnyAttr>()) 4814 return true; 4815 4816 GVALinkage Linkage; 4817 if (auto *VD = dyn_cast<VarDecl>(&D)) 4818 Linkage = CGM.getContext().GetGVALinkageForVariable(VD); 4819 else 4820 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); 4821 4822 switch (Linkage) { 4823 case GVA_Internal: 4824 case GVA_AvailableExternally: 4825 case GVA_StrongExternal: 4826 return false; 4827 case GVA_DiscardableODR: 4828 case GVA_StrongODR: 4829 return true; 4830 } 4831 llvm_unreachable("No such linkage"); 4832 } 4833 4834 void CodeGenModule::maybeSetTrivialComdat(const Decl &D, 4835 llvm::GlobalObject &GO) { 4836 if (!shouldBeInCOMDAT(*this, D)) 4837 return; 4838 GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); 4839 } 4840 4841 /// Pass IsTentative as true if you want to create a tentative definition. 4842 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, 4843 bool IsTentative) { 4844 // OpenCL global variables of sampler type are translated to function calls, 4845 // therefore no need to be translated. 4846 QualType ASTTy = D->getType(); 4847 if (getLangOpts().OpenCL && ASTTy->isSamplerT()) 4848 return; 4849 4850 // If this is OpenMP device, check if it is legal to emit this global 4851 // normally. 4852 if (LangOpts.OpenMPIsDevice && OpenMPRuntime && 4853 OpenMPRuntime->emitTargetGlobalVariable(D)) 4854 return; 4855 4856 llvm::TrackingVH<llvm::Constant> Init; 4857 bool NeedsGlobalCtor = false; 4858 // Whether the definition of the variable is available externally. 4859 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable 4860 // since this is the job for its original source. 4861 bool IsDefinitionAvailableExternally = 4862 getContext().GetGVALinkageForVariable(D) == GVA_AvailableExternally; 4863 bool NeedsGlobalDtor = 4864 !IsDefinitionAvailableExternally && 4865 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor; 4866 4867 const VarDecl *InitDecl; 4868 const Expr *InitExpr = D->getAnyInitializer(InitDecl); 4869 4870 std::optional<ConstantEmitter> emitter; 4871 4872 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization 4873 // as part of their declaration." Sema has already checked for 4874 // error cases, so we just need to set Init to UndefValue. 4875 bool IsCUDASharedVar = 4876 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>(); 4877 // Shadows of initialized device-side global variables are also left 4878 // undefined. 4879 // Managed Variables should be initialized on both host side and device side. 4880 bool IsCUDAShadowVar = 4881 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() && 4882 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() || 4883 D->hasAttr<CUDASharedAttr>()); 4884 bool IsCUDADeviceShadowVar = 4885 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() && 4886 (D->getType()->isCUDADeviceBuiltinSurfaceType() || 4887 D->getType()->isCUDADeviceBuiltinTextureType()); 4888 if (getLangOpts().CUDA && 4889 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) 4890 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy)); 4891 else if (D->hasAttr<LoaderUninitializedAttr>()) 4892 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy)); 4893 else if (!InitExpr) { 4894 // This is a tentative definition; tentative definitions are 4895 // implicitly initialized with { 0 }. 4896 // 4897 // Note that tentative definitions are only emitted at the end of 4898 // a translation unit, so they should never have incomplete 4899 // type. In addition, EmitTentativeDefinition makes sure that we 4900 // never attempt to emit a tentative definition if a real one 4901 // exists. A use may still exists, however, so we still may need 4902 // to do a RAUW. 4903 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 4904 Init = EmitNullConstant(D->getType()); 4905 } else { 4906 initializedGlobalDecl = GlobalDecl(D); 4907 emitter.emplace(*this); 4908 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl); 4909 if (!Initializer) { 4910 QualType T = InitExpr->getType(); 4911 if (D->getType()->isReferenceType()) 4912 T = D->getType(); 4913 4914 if (getLangOpts().CPlusPlus) { 4915 if (InitDecl->hasFlexibleArrayInit(getContext())) 4916 ErrorUnsupported(D, "flexible array initializer"); 4917 Init = EmitNullConstant(T); 4918 4919 if (!IsDefinitionAvailableExternally) 4920 NeedsGlobalCtor = true; 4921 } else { 4922 ErrorUnsupported(D, "static initializer"); 4923 Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 4924 } 4925 } else { 4926 Init = Initializer; 4927 // We don't need an initializer, so remove the entry for the delayed 4928 // initializer position (just in case this entry was delayed) if we 4929 // also don't need to register a destructor. 4930 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) 4931 DelayedCXXInitPosition.erase(D); 4932 4933 #ifndef NDEBUG 4934 CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) + 4935 InitDecl->getFlexibleArrayInitChars(getContext()); 4936 CharUnits CstSize = CharUnits::fromQuantity( 4937 getDataLayout().getTypeAllocSize(Init->getType())); 4938 assert(VarSize == CstSize && "Emitted constant has unexpected size"); 4939 #endif 4940 } 4941 } 4942 4943 llvm::Type* InitType = Init->getType(); 4944 llvm::Constant *Entry = 4945 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); 4946 4947 // Strip off pointer casts if we got them. 4948 Entry = Entry->stripPointerCasts(); 4949 4950 // Entry is now either a Function or GlobalVariable. 4951 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); 4952 4953 // We have a definition after a declaration with the wrong type. 4954 // We must make a new GlobalVariable* and update everything that used OldGV 4955 // (a declaration or tentative definition) with the new GlobalVariable* 4956 // (which will be a definition). 4957 // 4958 // This happens if there is a prototype for a global (e.g. 4959 // "extern int x[];") and then a definition of a different type (e.g. 4960 // "int x[10];"). This also happens when an initializer has a different type 4961 // from the type of the global (this happens with unions). 4962 if (!GV || GV->getValueType() != InitType || 4963 GV->getType()->getAddressSpace() != 4964 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { 4965 4966 // Move the old entry aside so that we'll create a new one. 4967 Entry->setName(StringRef()); 4968 4969 // Make a new global with the correct type, this is now guaranteed to work. 4970 GV = cast<llvm::GlobalVariable>( 4971 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)) 4972 ->stripPointerCasts()); 4973 4974 // Replace all uses of the old global with the new global 4975 llvm::Constant *NewPtrForOldDecl = 4976 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, 4977 Entry->getType()); 4978 Entry->replaceAllUsesWith(NewPtrForOldDecl); 4979 4980 // Erase the old global, since it is no longer used. 4981 cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 4982 } 4983 4984 MaybeHandleStaticInExternC(D, GV); 4985 4986 if (D->hasAttr<AnnotateAttr>()) 4987 AddGlobalAnnotations(D, GV); 4988 4989 // Set the llvm linkage type as appropriate. 4990 llvm::GlobalValue::LinkageTypes Linkage = 4991 getLLVMLinkageVarDefinition(D, GV->isConstant()); 4992 4993 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on 4994 // the device. [...]" 4995 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with 4996 // __device__, declares a variable that: [...] 4997 // Is accessible from all the threads within the grid and from the host 4998 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() 4999 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())." 5000 if (GV && LangOpts.CUDA) { 5001 if (LangOpts.CUDAIsDevice) { 5002 if (Linkage != llvm::GlobalValue::InternalLinkage && 5003 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || 5004 D->getType()->isCUDADeviceBuiltinSurfaceType() || 5005 D->getType()->isCUDADeviceBuiltinTextureType())) 5006 GV->setExternallyInitialized(true); 5007 } else { 5008 getCUDARuntime().internalizeDeviceSideVar(D, Linkage); 5009 } 5010 getCUDARuntime().handleVarRegistration(D, *GV); 5011 } 5012 5013 GV->setInitializer(Init); 5014 if (emitter) 5015 emitter->finalize(GV); 5016 5017 // If it is safe to mark the global 'constant', do so now. 5018 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && 5019 isTypeConstant(D->getType(), true, true)); 5020 5021 // If it is in a read-only section, mark it 'constant'. 5022 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { 5023 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; 5024 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) 5025 GV->setConstant(true); 5026 } 5027 5028 CharUnits AlignVal = getContext().getDeclAlign(D); 5029 // Check for alignment specifed in an 'omp allocate' directive. 5030 if (std::optional<CharUnits> AlignValFromAllocate = 5031 getOMPAllocateAlignment(D)) 5032 AlignVal = *AlignValFromAllocate; 5033 GV->setAlignment(AlignVal.getAsAlign()); 5034 5035 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper 5036 // function is only defined alongside the variable, not also alongside 5037 // callers. Normally, all accesses to a thread_local go through the 5038 // thread-wrapper in order to ensure initialization has occurred, underlying 5039 // variable will never be used other than the thread-wrapper, so it can be 5040 // converted to internal linkage. 5041 // 5042 // However, if the variable has the 'constinit' attribute, it _can_ be 5043 // referenced directly, without calling the thread-wrapper, so the linkage 5044 // must not be changed. 5045 // 5046 // Additionally, if the variable isn't plain external linkage, e.g. if it's 5047 // weak or linkonce, the de-duplication semantics are important to preserve, 5048 // so we don't change the linkage. 5049 if (D->getTLSKind() == VarDecl::TLS_Dynamic && 5050 Linkage == llvm::GlobalValue::ExternalLinkage && 5051 Context.getTargetInfo().getTriple().isOSDarwin() && 5052 !D->hasAttr<ConstInitAttr>()) 5053 Linkage = llvm::GlobalValue::InternalLinkage; 5054 5055 GV->setLinkage(Linkage); 5056 if (D->hasAttr<DLLImportAttr>()) 5057 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 5058 else if (D->hasAttr<DLLExportAttr>()) 5059 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); 5060 else 5061 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); 5062 5063 if (Linkage == llvm::GlobalVariable::CommonLinkage) { 5064 // common vars aren't constant even if declared const. 5065 GV->setConstant(false); 5066 // Tentative definition of global variables may be initialized with 5067 // non-zero null pointers. In this case they should have weak linkage 5068 // since common linkage must have zero initializer and must not have 5069 // explicit section therefore cannot have non-zero initial value. 5070 if (!GV->getInitializer()->isNullValue()) 5071 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 5072 } 5073 5074 setNonAliasAttributes(D, GV); 5075 5076 if (D->getTLSKind() && !GV->isThreadLocal()) { 5077 if (D->getTLSKind() == VarDecl::TLS_Dynamic) 5078 CXXThreadLocals.push_back(D); 5079 setTLSMode(GV, *D); 5080 } 5081 5082 maybeSetTrivialComdat(*D, *GV); 5083 5084 // Emit the initializer function if necessary. 5085 if (NeedsGlobalCtor || NeedsGlobalDtor) 5086 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); 5087 5088 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor); 5089 5090 // Emit global variable debug information. 5091 if (CGDebugInfo *DI = getModuleDebugInfo()) 5092 if (getCodeGenOpts().hasReducedDebugInfo()) 5093 DI->EmitGlobalVariable(GV, D); 5094 } 5095 5096 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) { 5097 if (CGDebugInfo *DI = getModuleDebugInfo()) 5098 if (getCodeGenOpts().hasReducedDebugInfo()) { 5099 QualType ASTTy = D->getType(); 5100 llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType()); 5101 llvm::Constant *GV = 5102 GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D); 5103 DI->EmitExternalVariable( 5104 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D); 5105 } 5106 } 5107 5108 static bool isVarDeclStrongDefinition(const ASTContext &Context, 5109 CodeGenModule &CGM, const VarDecl *D, 5110 bool NoCommon) { 5111 // Don't give variables common linkage if -fno-common was specified unless it 5112 // was overridden by a NoCommon attribute. 5113 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) 5114 return true; 5115 5116 // C11 6.9.2/2: 5117 // A declaration of an identifier for an object that has file scope without 5118 // an initializer, and without a storage-class specifier or with the 5119 // storage-class specifier static, constitutes a tentative definition. 5120 if (D->getInit() || D->hasExternalStorage()) 5121 return true; 5122 5123 // A variable cannot be both common and exist in a section. 5124 if (D->hasAttr<SectionAttr>()) 5125 return true; 5126 5127 // A variable cannot be both common and exist in a section. 5128 // We don't try to determine which is the right section in the front-end. 5129 // If no specialized section name is applicable, it will resort to default. 5130 if (D->hasAttr<PragmaClangBSSSectionAttr>() || 5131 D->hasAttr<PragmaClangDataSectionAttr>() || 5132 D->hasAttr<PragmaClangRelroSectionAttr>() || 5133 D->hasAttr<PragmaClangRodataSectionAttr>()) 5134 return true; 5135 5136 // Thread local vars aren't considered common linkage. 5137 if (D->getTLSKind()) 5138 return true; 5139 5140 // Tentative definitions marked with WeakImportAttr are true definitions. 5141 if (D->hasAttr<WeakImportAttr>()) 5142 return true; 5143 5144 // A variable cannot be both common and exist in a comdat. 5145 if (shouldBeInCOMDAT(CGM, *D)) 5146 return true; 5147 5148 // Declarations with a required alignment do not have common linkage in MSVC 5149 // mode. 5150 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { 5151 if (D->hasAttr<AlignedAttr>()) 5152 return true; 5153 QualType VarType = D->getType(); 5154 if (Context.isAlignmentRequired(VarType)) 5155 return true; 5156 5157 if (const auto *RT = VarType->getAs<RecordType>()) { 5158 const RecordDecl *RD = RT->getDecl(); 5159 for (const FieldDecl *FD : RD->fields()) { 5160 if (FD->isBitField()) 5161 continue; 5162 if (FD->hasAttr<AlignedAttr>()) 5163 return true; 5164 if (Context.isAlignmentRequired(FD->getType())) 5165 return true; 5166 } 5167 } 5168 } 5169 5170 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for 5171 // common symbols, so symbols with greater alignment requirements cannot be 5172 // common. 5173 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two 5174 // alignments for common symbols via the aligncomm directive, so this 5175 // restriction only applies to MSVC environments. 5176 if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() && 5177 Context.getTypeAlignIfKnown(D->getType()) > 5178 Context.toBits(CharUnits::fromQuantity(32))) 5179 return true; 5180 5181 return false; 5182 } 5183 5184 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator( 5185 const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) { 5186 if (Linkage == GVA_Internal) 5187 return llvm::Function::InternalLinkage; 5188 5189 if (D->hasAttr<WeakAttr>()) 5190 return llvm::GlobalVariable::WeakAnyLinkage; 5191 5192 if (const auto *FD = D->getAsFunction()) 5193 if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally) 5194 return llvm::GlobalVariable::LinkOnceAnyLinkage; 5195 5196 // We are guaranteed to have a strong definition somewhere else, 5197 // so we can use available_externally linkage. 5198 if (Linkage == GVA_AvailableExternally) 5199 return llvm::GlobalValue::AvailableExternallyLinkage; 5200 5201 // Note that Apple's kernel linker doesn't support symbol 5202 // coalescing, so we need to avoid linkonce and weak linkages there. 5203 // Normally, this means we just map to internal, but for explicit 5204 // instantiations we'll map to external. 5205 5206 // In C++, the compiler has to emit a definition in every translation unit 5207 // that references the function. We should use linkonce_odr because 5208 // a) if all references in this translation unit are optimized away, we 5209 // don't need to codegen it. b) if the function persists, it needs to be 5210 // merged with other definitions. c) C++ has the ODR, so we know the 5211 // definition is dependable. 5212 if (Linkage == GVA_DiscardableODR) 5213 return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage 5214 : llvm::Function::InternalLinkage; 5215 5216 // An explicit instantiation of a template has weak linkage, since 5217 // explicit instantiations can occur in multiple translation units 5218 // and must all be equivalent. However, we are not allowed to 5219 // throw away these explicit instantiations. 5220 // 5221 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU, 5222 // so say that CUDA templates are either external (for kernels) or internal. 5223 // This lets llvm perform aggressive inter-procedural optimizations. For 5224 // -fgpu-rdc case, device function calls across multiple TU's are allowed, 5225 // therefore we need to follow the normal linkage paradigm. 5226 if (Linkage == GVA_StrongODR) { 5227 if (getLangOpts().AppleKext) 5228 return llvm::Function::ExternalLinkage; 5229 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice && 5230 !getLangOpts().GPURelocatableDeviceCode) 5231 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage 5232 : llvm::Function::InternalLinkage; 5233 return llvm::Function::WeakODRLinkage; 5234 } 5235 5236 // C++ doesn't have tentative definitions and thus cannot have common 5237 // linkage. 5238 if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && 5239 !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), 5240 CodeGenOpts.NoCommon)) 5241 return llvm::GlobalVariable::CommonLinkage; 5242 5243 // selectany symbols are externally visible, so use weak instead of 5244 // linkonce. MSVC optimizes away references to const selectany globals, so 5245 // all definitions should be the same and ODR linkage should be used. 5246 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx 5247 if (D->hasAttr<SelectAnyAttr>()) 5248 return llvm::GlobalVariable::WeakODRLinkage; 5249 5250 // Otherwise, we have strong external linkage. 5251 assert(Linkage == GVA_StrongExternal); 5252 return llvm::GlobalVariable::ExternalLinkage; 5253 } 5254 5255 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition( 5256 const VarDecl *VD, bool IsConstant) { 5257 GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); 5258 return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant); 5259 } 5260 5261 /// Replace the uses of a function that was declared with a non-proto type. 5262 /// We want to silently drop extra arguments from call sites 5263 static void replaceUsesOfNonProtoConstant(llvm::Constant *old, 5264 llvm::Function *newFn) { 5265 // Fast path. 5266 if (old->use_empty()) return; 5267 5268 llvm::Type *newRetTy = newFn->getReturnType(); 5269 SmallVector<llvm::Value*, 4> newArgs; 5270 5271 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); 5272 ui != ue; ) { 5273 llvm::Value::use_iterator use = ui++; // Increment before the use is erased. 5274 llvm::User *user = use->getUser(); 5275 5276 // Recognize and replace uses of bitcasts. Most calls to 5277 // unprototyped functions will use bitcasts. 5278 if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { 5279 if (bitcast->getOpcode() == llvm::Instruction::BitCast) 5280 replaceUsesOfNonProtoConstant(bitcast, newFn); 5281 continue; 5282 } 5283 5284 // Recognize calls to the function. 5285 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user); 5286 if (!callSite) continue; 5287 if (!callSite->isCallee(&*use)) 5288 continue; 5289 5290 // If the return types don't match exactly, then we can't 5291 // transform this call unless it's dead. 5292 if (callSite->getType() != newRetTy && !callSite->use_empty()) 5293 continue; 5294 5295 // Get the call site's attribute list. 5296 SmallVector<llvm::AttributeSet, 8> newArgAttrs; 5297 llvm::AttributeList oldAttrs = callSite->getAttributes(); 5298 5299 // If the function was passed too few arguments, don't transform. 5300 unsigned newNumArgs = newFn->arg_size(); 5301 if (callSite->arg_size() < newNumArgs) 5302 continue; 5303 5304 // If extra arguments were passed, we silently drop them. 5305 // If any of the types mismatch, we don't transform. 5306 unsigned argNo = 0; 5307 bool dontTransform = false; 5308 for (llvm::Argument &A : newFn->args()) { 5309 if (callSite->getArgOperand(argNo)->getType() != A.getType()) { 5310 dontTransform = true; 5311 break; 5312 } 5313 5314 // Add any parameter attributes. 5315 newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo)); 5316 argNo++; 5317 } 5318 if (dontTransform) 5319 continue; 5320 5321 // Okay, we can transform this. Create the new call instruction and copy 5322 // over the required information. 5323 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo); 5324 5325 // Copy over any operand bundles. 5326 SmallVector<llvm::OperandBundleDef, 1> newBundles; 5327 callSite->getOperandBundlesAsDefs(newBundles); 5328 5329 llvm::CallBase *newCall; 5330 if (isa<llvm::CallInst>(callSite)) { 5331 newCall = 5332 llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite); 5333 } else { 5334 auto *oldInvoke = cast<llvm::InvokeInst>(callSite); 5335 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(), 5336 oldInvoke->getUnwindDest(), newArgs, 5337 newBundles, "", callSite); 5338 } 5339 newArgs.clear(); // for the next iteration 5340 5341 if (!newCall->getType()->isVoidTy()) 5342 newCall->takeName(callSite); 5343 newCall->setAttributes( 5344 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(), 5345 oldAttrs.getRetAttrs(), newArgAttrs)); 5346 newCall->setCallingConv(callSite->getCallingConv()); 5347 5348 // Finally, remove the old call, replacing any uses with the new one. 5349 if (!callSite->use_empty()) 5350 callSite->replaceAllUsesWith(newCall); 5351 5352 // Copy debug location attached to CI. 5353 if (callSite->getDebugLoc()) 5354 newCall->setDebugLoc(callSite->getDebugLoc()); 5355 5356 callSite->eraseFromParent(); 5357 } 5358 } 5359 5360 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 5361 /// implement a function with no prototype, e.g. "int foo() {}". If there are 5362 /// existing call uses of the old function in the module, this adjusts them to 5363 /// call the new function directly. 5364 /// 5365 /// This is not just a cleanup: the always_inline pass requires direct calls to 5366 /// functions to be able to inline them. If there is a bitcast in the way, it 5367 /// won't inline them. Instcombine normally deletes these calls, but it isn't 5368 /// run at -O0. 5369 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 5370 llvm::Function *NewFn) { 5371 // If we're redefining a global as a function, don't transform it. 5372 if (!isa<llvm::Function>(Old)) return; 5373 5374 replaceUsesOfNonProtoConstant(Old, NewFn); 5375 } 5376 5377 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { 5378 auto DK = VD->isThisDeclarationADefinition(); 5379 if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) 5380 return; 5381 5382 TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); 5383 // If we have a definition, this might be a deferred decl. If the 5384 // instantiation is explicit, make sure we emit it at the end. 5385 if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) 5386 GetAddrOfGlobalVar(VD); 5387 5388 EmitTopLevelDecl(VD); 5389 } 5390 5391 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, 5392 llvm::GlobalValue *GV) { 5393 const auto *D = cast<FunctionDecl>(GD.getDecl()); 5394 5395 // Compute the function info and LLVM type. 5396 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 5397 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 5398 5399 // Get or create the prototype for the function. 5400 if (!GV || (GV->getValueType() != Ty)) 5401 GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, 5402 /*DontDefer=*/true, 5403 ForDefinition)); 5404 5405 // Already emitted. 5406 if (!GV->isDeclaration()) 5407 return; 5408 5409 // We need to set linkage and visibility on the function before 5410 // generating code for it because various parts of IR generation 5411 // want to propagate this information down (e.g. to local static 5412 // declarations). 5413 auto *Fn = cast<llvm::Function>(GV); 5414 setFunctionLinkage(GD, Fn); 5415 5416 // FIXME: this is redundant with part of setFunctionDefinitionAttributes 5417 setGVProperties(Fn, GD); 5418 5419 MaybeHandleStaticInExternC(D, Fn); 5420 5421 maybeSetTrivialComdat(*D, *Fn); 5422 5423 // Set CodeGen attributes that represent floating point environment. 5424 setLLVMFunctionFEnvAttributes(D, Fn); 5425 5426 CodeGenFunction(*this).GenerateCode(GD, Fn, FI); 5427 5428 setNonAliasAttributes(GD, Fn); 5429 SetLLVMFunctionAttributesForDefinition(D, Fn); 5430 5431 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 5432 AddGlobalCtor(Fn, CA->getPriority()); 5433 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 5434 AddGlobalDtor(Fn, DA->getPriority(), true); 5435 if (D->hasAttr<AnnotateAttr>()) 5436 AddGlobalAnnotations(D, Fn); 5437 } 5438 5439 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 5440 const auto *D = cast<ValueDecl>(GD.getDecl()); 5441 const AliasAttr *AA = D->getAttr<AliasAttr>(); 5442 assert(AA && "Not an alias?"); 5443 5444 StringRef MangledName = getMangledName(GD); 5445 5446 if (AA->getAliasee() == MangledName) { 5447 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; 5448 return; 5449 } 5450 5451 // If there is a definition in the module, then it wins over the alias. 5452 // This is dubious, but allow it to be safe. Just ignore the alias. 5453 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 5454 if (Entry && !Entry->isDeclaration()) 5455 return; 5456 5457 Aliases.push_back(GD); 5458 5459 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 5460 5461 // Create a reference to the named value. This ensures that it is emitted 5462 // if a deferred decl. 5463 llvm::Constant *Aliasee; 5464 llvm::GlobalValue::LinkageTypes LT; 5465 if (isa<llvm::FunctionType>(DeclTy)) { 5466 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, 5467 /*ForVTable=*/false); 5468 LT = getFunctionLinkage(GD); 5469 } else { 5470 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default, 5471 /*D=*/nullptr); 5472 if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl())) 5473 LT = getLLVMLinkageVarDefinition(VD, D->getType().isConstQualified()); 5474 else 5475 LT = getFunctionLinkage(GD); 5476 } 5477 5478 // Create the new alias itself, but don't set a name yet. 5479 unsigned AS = Aliasee->getType()->getPointerAddressSpace(); 5480 auto *GA = 5481 llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule()); 5482 5483 if (Entry) { 5484 if (GA->getAliasee() == Entry) { 5485 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; 5486 return; 5487 } 5488 5489 assert(Entry->isDeclaration()); 5490 5491 // If there is a declaration in the module, then we had an extern followed 5492 // by the alias, as in: 5493 // extern int test6(); 5494 // ... 5495 // int test6() __attribute__((alias("test7"))); 5496 // 5497 // Remove it and replace uses of it with the alias. 5498 GA->takeName(Entry); 5499 5500 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA, 5501 Entry->getType())); 5502 Entry->eraseFromParent(); 5503 } else { 5504 GA->setName(MangledName); 5505 } 5506 5507 // Set attributes which are particular to an alias; this is a 5508 // specialization of the attributes which may be set on a global 5509 // variable/function. 5510 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || 5511 D->isWeakImported()) { 5512 GA->setLinkage(llvm::Function::WeakAnyLinkage); 5513 } 5514 5515 if (const auto *VD = dyn_cast<VarDecl>(D)) 5516 if (VD->getTLSKind()) 5517 setTLSMode(GA, *VD); 5518 5519 SetCommonAttributes(GD, GA); 5520 5521 // Emit global alias debug information. 5522 if (isa<VarDecl>(D)) 5523 if (CGDebugInfo *DI = getModuleDebugInfo()) 5524 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD); 5525 } 5526 5527 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { 5528 const auto *D = cast<ValueDecl>(GD.getDecl()); 5529 const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); 5530 assert(IFA && "Not an ifunc?"); 5531 5532 StringRef MangledName = getMangledName(GD); 5533 5534 if (IFA->getResolver() == MangledName) { 5535 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; 5536 return; 5537 } 5538 5539 // Report an error if some definition overrides ifunc. 5540 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 5541 if (Entry && !Entry->isDeclaration()) { 5542 GlobalDecl OtherGD; 5543 if (lookupRepresentativeDecl(MangledName, OtherGD) && 5544 DiagnosedConflictingDefinitions.insert(GD).second) { 5545 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name) 5546 << MangledName; 5547 Diags.Report(OtherGD.getDecl()->getLocation(), 5548 diag::note_previous_definition); 5549 } 5550 return; 5551 } 5552 5553 Aliases.push_back(GD); 5554 5555 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 5556 llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy); 5557 llvm::Constant *Resolver = 5558 GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {}, 5559 /*ForVTable=*/false); 5560 llvm::GlobalIFunc *GIF = 5561 llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, 5562 "", Resolver, &getModule()); 5563 if (Entry) { 5564 if (GIF->getResolver() == Entry) { 5565 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; 5566 return; 5567 } 5568 assert(Entry->isDeclaration()); 5569 5570 // If there is a declaration in the module, then we had an extern followed 5571 // by the ifunc, as in: 5572 // extern int test(); 5573 // ... 5574 // int test() __attribute__((ifunc("resolver"))); 5575 // 5576 // Remove it and replace uses of it with the ifunc. 5577 GIF->takeName(Entry); 5578 5579 Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF, 5580 Entry->getType())); 5581 Entry->eraseFromParent(); 5582 } else 5583 GIF->setName(MangledName); 5584 5585 SetCommonAttributes(GD, GIF); 5586 } 5587 5588 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, 5589 ArrayRef<llvm::Type*> Tys) { 5590 return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, 5591 Tys); 5592 } 5593 5594 static llvm::StringMapEntry<llvm::GlobalVariable *> & 5595 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, 5596 const StringLiteral *Literal, bool TargetIsLSB, 5597 bool &IsUTF16, unsigned &StringLength) { 5598 StringRef String = Literal->getString(); 5599 unsigned NumBytes = String.size(); 5600 5601 // Check for simple case. 5602 if (!Literal->containsNonAsciiOrNull()) { 5603 StringLength = NumBytes; 5604 return *Map.insert(std::make_pair(String, nullptr)).first; 5605 } 5606 5607 // Otherwise, convert the UTF8 literals into a string of shorts. 5608 IsUTF16 = true; 5609 5610 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. 5611 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); 5612 llvm::UTF16 *ToPtr = &ToBuf[0]; 5613 5614 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, 5615 ToPtr + NumBytes, llvm::strictConversion); 5616 5617 // ConvertUTF8toUTF16 returns the length in ToPtr. 5618 StringLength = ToPtr - &ToBuf[0]; 5619 5620 // Add an explicit null. 5621 *ToPtr = 0; 5622 return *Map.insert(std::make_pair( 5623 StringRef(reinterpret_cast<const char *>(ToBuf.data()), 5624 (StringLength + 1) * 2), 5625 nullptr)).first; 5626 } 5627 5628 ConstantAddress 5629 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 5630 unsigned StringLength = 0; 5631 bool isUTF16 = false; 5632 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = 5633 GetConstantCFStringEntry(CFConstantStringMap, Literal, 5634 getDataLayout().isLittleEndian(), isUTF16, 5635 StringLength); 5636 5637 if (auto *C = Entry.second) 5638 return ConstantAddress( 5639 C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment())); 5640 5641 llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); 5642 llvm::Constant *Zeros[] = { Zero, Zero }; 5643 5644 const ASTContext &Context = getContext(); 5645 const llvm::Triple &Triple = getTriple(); 5646 5647 const auto CFRuntime = getLangOpts().CFRuntime; 5648 const bool IsSwiftABI = 5649 static_cast<unsigned>(CFRuntime) >= 5650 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift); 5651 const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1; 5652 5653 // If we don't already have it, get __CFConstantStringClassReference. 5654 if (!CFConstantStringClassRef) { 5655 const char *CFConstantStringClassName = "__CFConstantStringClassReference"; 5656 llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 5657 Ty = llvm::ArrayType::get(Ty, 0); 5658 5659 switch (CFRuntime) { 5660 default: break; 5661 case LangOptions::CoreFoundationABI::Swift: [[fallthrough]]; 5662 case LangOptions::CoreFoundationABI::Swift5_0: 5663 CFConstantStringClassName = 5664 Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN" 5665 : "$s10Foundation19_NSCFConstantStringCN"; 5666 Ty = IntPtrTy; 5667 break; 5668 case LangOptions::CoreFoundationABI::Swift4_2: 5669 CFConstantStringClassName = 5670 Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN" 5671 : "$S10Foundation19_NSCFConstantStringCN"; 5672 Ty = IntPtrTy; 5673 break; 5674 case LangOptions::CoreFoundationABI::Swift4_1: 5675 CFConstantStringClassName = 5676 Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN" 5677 : "__T010Foundation19_NSCFConstantStringCN"; 5678 Ty = IntPtrTy; 5679 break; 5680 } 5681 5682 llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName); 5683 5684 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) { 5685 llvm::GlobalValue *GV = nullptr; 5686 5687 if ((GV = dyn_cast<llvm::GlobalValue>(C))) { 5688 IdentifierInfo &II = Context.Idents.get(GV->getName()); 5689 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl(); 5690 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); 5691 5692 const VarDecl *VD = nullptr; 5693 for (const auto *Result : DC->lookup(&II)) 5694 if ((VD = dyn_cast<VarDecl>(Result))) 5695 break; 5696 5697 if (Triple.isOSBinFormatELF()) { 5698 if (!VD) 5699 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 5700 } else { 5701 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 5702 if (!VD || !VD->hasAttr<DLLExportAttr>()) 5703 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 5704 else 5705 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 5706 } 5707 5708 setDSOLocal(GV); 5709 } 5710 } 5711 5712 // Decay array -> ptr 5713 CFConstantStringClassRef = 5714 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) 5715 : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros); 5716 } 5717 5718 QualType CFTy = Context.getCFConstantStringType(); 5719 5720 auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 5721 5722 ConstantInitBuilder Builder(*this); 5723 auto Fields = Builder.beginStruct(STy); 5724 5725 // Class pointer. 5726 Fields.add(cast<llvm::Constant>(CFConstantStringClassRef)); 5727 5728 // Flags. 5729 if (IsSwiftABI) { 5730 Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01); 5731 Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8); 5732 } else { 5733 Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); 5734 } 5735 5736 // String pointer. 5737 llvm::Constant *C = nullptr; 5738 if (isUTF16) { 5739 auto Arr = llvm::ArrayRef( 5740 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), 5741 Entry.first().size() / 2); 5742 C = llvm::ConstantDataArray::get(VMContext, Arr); 5743 } else { 5744 C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); 5745 } 5746 5747 // Note: -fwritable-strings doesn't make the backing store strings of 5748 // CFStrings writable. (See <rdar://problem/10657500>) 5749 auto *GV = 5750 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, 5751 llvm::GlobalValue::PrivateLinkage, C, ".str"); 5752 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 5753 // Don't enforce the target's minimum global alignment, since the only use 5754 // of the string is via this class initializer. 5755 CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy) 5756 : Context.getTypeAlignInChars(Context.CharTy); 5757 GV->setAlignment(Align.getAsAlign()); 5758 5759 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. 5760 // Without it LLVM can merge the string with a non unnamed_addr one during 5761 // LTO. Doing that changes the section it ends in, which surprises ld64. 5762 if (Triple.isOSBinFormatMachO()) 5763 GV->setSection(isUTF16 ? "__TEXT,__ustring" 5764 : "__TEXT,__cstring,cstring_literals"); 5765 // Make sure the literal ends up in .rodata to allow for safe ICF and for 5766 // the static linker to adjust permissions to read-only later on. 5767 else if (Triple.isOSBinFormatELF()) 5768 GV->setSection(".rodata"); 5769 5770 // String. 5771 llvm::Constant *Str = 5772 llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); 5773 5774 if (isUTF16) 5775 // Cast the UTF16 string to the correct type. 5776 Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy); 5777 Fields.add(Str); 5778 5779 // String length. 5780 llvm::IntegerType *LengthTy = 5781 llvm::IntegerType::get(getModule().getContext(), 5782 Context.getTargetInfo().getLongWidth()); 5783 if (IsSwiftABI) { 5784 if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || 5785 CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) 5786 LengthTy = Int32Ty; 5787 else 5788 LengthTy = IntPtrTy; 5789 } 5790 Fields.addInt(LengthTy, StringLength); 5791 5792 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is 5793 // properly aligned on 32-bit platforms. 5794 CharUnits Alignment = 5795 IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign(); 5796 5797 // The struct. 5798 GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, 5799 /*isConstant=*/false, 5800 llvm::GlobalVariable::PrivateLinkage); 5801 GV->addAttribute("objc_arc_inert"); 5802 switch (Triple.getObjectFormat()) { 5803 case llvm::Triple::UnknownObjectFormat: 5804 llvm_unreachable("unknown file format"); 5805 case llvm::Triple::DXContainer: 5806 case llvm::Triple::GOFF: 5807 case llvm::Triple::SPIRV: 5808 case llvm::Triple::XCOFF: 5809 llvm_unreachable("unimplemented"); 5810 case llvm::Triple::COFF: 5811 case llvm::Triple::ELF: 5812 case llvm::Triple::Wasm: 5813 GV->setSection("cfstring"); 5814 break; 5815 case llvm::Triple::MachO: 5816 GV->setSection("__DATA,__cfstring"); 5817 break; 5818 } 5819 Entry.second = GV; 5820 5821 return ConstantAddress(GV, GV->getValueType(), Alignment); 5822 } 5823 5824 bool CodeGenModule::getExpressionLocationsEnabled() const { 5825 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo; 5826 } 5827 5828 QualType CodeGenModule::getObjCFastEnumerationStateType() { 5829 if (ObjCFastEnumerationStateType.isNull()) { 5830 RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); 5831 D->startDefinition(); 5832 5833 QualType FieldTypes[] = { 5834 Context.UnsignedLongTy, 5835 Context.getPointerType(Context.getObjCIdType()), 5836 Context.getPointerType(Context.UnsignedLongTy), 5837 Context.getConstantArrayType(Context.UnsignedLongTy, 5838 llvm::APInt(32, 5), nullptr, ArrayType::Normal, 0) 5839 }; 5840 5841 for (size_t i = 0; i < 4; ++i) { 5842 FieldDecl *Field = FieldDecl::Create(Context, 5843 D, 5844 SourceLocation(), 5845 SourceLocation(), nullptr, 5846 FieldTypes[i], /*TInfo=*/nullptr, 5847 /*BitWidth=*/nullptr, 5848 /*Mutable=*/false, 5849 ICIS_NoInit); 5850 Field->setAccess(AS_public); 5851 D->addDecl(Field); 5852 } 5853 5854 D->completeDefinition(); 5855 ObjCFastEnumerationStateType = Context.getTagDeclType(D); 5856 } 5857 5858 return ObjCFastEnumerationStateType; 5859 } 5860 5861 llvm::Constant * 5862 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { 5863 assert(!E->getType()->isPointerType() && "Strings are always arrays"); 5864 5865 // Don't emit it as the address of the string, emit the string data itself 5866 // as an inline array. 5867 if (E->getCharByteWidth() == 1) { 5868 SmallString<64> Str(E->getString()); 5869 5870 // Resize the string to the right size, which is indicated by its type. 5871 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); 5872 Str.resize(CAT->getSize().getZExtValue()); 5873 return llvm::ConstantDataArray::getString(VMContext, Str, false); 5874 } 5875 5876 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); 5877 llvm::Type *ElemTy = AType->getElementType(); 5878 unsigned NumElements = AType->getNumElements(); 5879 5880 // Wide strings have either 2-byte or 4-byte elements. 5881 if (ElemTy->getPrimitiveSizeInBits() == 16) { 5882 SmallVector<uint16_t, 32> Elements; 5883 Elements.reserve(NumElements); 5884 5885 for(unsigned i = 0, e = E->getLength(); i != e; ++i) 5886 Elements.push_back(E->getCodeUnit(i)); 5887 Elements.resize(NumElements); 5888 return llvm::ConstantDataArray::get(VMContext, Elements); 5889 } 5890 5891 assert(ElemTy->getPrimitiveSizeInBits() == 32); 5892 SmallVector<uint32_t, 32> Elements; 5893 Elements.reserve(NumElements); 5894 5895 for(unsigned i = 0, e = E->getLength(); i != e; ++i) 5896 Elements.push_back(E->getCodeUnit(i)); 5897 Elements.resize(NumElements); 5898 return llvm::ConstantDataArray::get(VMContext, Elements); 5899 } 5900 5901 static llvm::GlobalVariable * 5902 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, 5903 CodeGenModule &CGM, StringRef GlobalName, 5904 CharUnits Alignment) { 5905 unsigned AddrSpace = CGM.getContext().getTargetAddressSpace( 5906 CGM.GetGlobalConstantAddressSpace()); 5907 5908 llvm::Module &M = CGM.getModule(); 5909 // Create a global variable for this string 5910 auto *GV = new llvm::GlobalVariable( 5911 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, 5912 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); 5913 GV->setAlignment(Alignment.getAsAlign()); 5914 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 5915 if (GV->isWeakForLinker()) { 5916 assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); 5917 GV->setComdat(M.getOrInsertComdat(GV->getName())); 5918 } 5919 CGM.setDSOLocal(GV); 5920 5921 return GV; 5922 } 5923 5924 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a 5925 /// constant array for the given string literal. 5926 ConstantAddress 5927 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, 5928 StringRef Name) { 5929 CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); 5930 5931 llvm::Constant *C = GetConstantArrayFromStringLiteral(S); 5932 llvm::GlobalVariable **Entry = nullptr; 5933 if (!LangOpts.WritableStrings) { 5934 Entry = &ConstantStringMap[C]; 5935 if (auto GV = *Entry) { 5936 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment()) 5937 GV->setAlignment(Alignment.getAsAlign()); 5938 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 5939 GV->getValueType(), Alignment); 5940 } 5941 } 5942 5943 SmallString<256> MangledNameBuffer; 5944 StringRef GlobalVariableName; 5945 llvm::GlobalValue::LinkageTypes LT; 5946 5947 // Mangle the string literal if that's how the ABI merges duplicate strings. 5948 // Don't do it if they are writable, since we don't want writes in one TU to 5949 // affect strings in another. 5950 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) && 5951 !LangOpts.WritableStrings) { 5952 llvm::raw_svector_ostream Out(MangledNameBuffer); 5953 getCXXABI().getMangleContext().mangleStringLiteral(S, Out); 5954 LT = llvm::GlobalValue::LinkOnceODRLinkage; 5955 GlobalVariableName = MangledNameBuffer; 5956 } else { 5957 LT = llvm::GlobalValue::PrivateLinkage; 5958 GlobalVariableName = Name; 5959 } 5960 5961 auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); 5962 5963 CGDebugInfo *DI = getModuleDebugInfo(); 5964 if (DI && getCodeGenOpts().hasReducedDebugInfo()) 5965 DI->AddStringLiteralDebugInfo(GV, S); 5966 5967 if (Entry) 5968 *Entry = GV; 5969 5970 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>"); 5971 5972 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 5973 GV->getValueType(), Alignment); 5974 } 5975 5976 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 5977 /// array for the given ObjCEncodeExpr node. 5978 ConstantAddress 5979 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 5980 std::string Str; 5981 getContext().getObjCEncodingForType(E->getEncodedType(), Str); 5982 5983 return GetAddrOfConstantCString(Str); 5984 } 5985 5986 /// GetAddrOfConstantCString - Returns a pointer to a character array containing 5987 /// the literal and a terminating '\0' character. 5988 /// The result has pointer to array type. 5989 ConstantAddress CodeGenModule::GetAddrOfConstantCString( 5990 const std::string &Str, const char *GlobalName) { 5991 StringRef StrWithNull(Str.c_str(), Str.size() + 1); 5992 CharUnits Alignment = 5993 getContext().getAlignOfGlobalVarInChars(getContext().CharTy); 5994 5995 llvm::Constant *C = 5996 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); 5997 5998 // Don't share any string literals if strings aren't constant. 5999 llvm::GlobalVariable **Entry = nullptr; 6000 if (!LangOpts.WritableStrings) { 6001 Entry = &ConstantStringMap[C]; 6002 if (auto GV = *Entry) { 6003 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment()) 6004 GV->setAlignment(Alignment.getAsAlign()); 6005 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6006 GV->getValueType(), Alignment); 6007 } 6008 } 6009 6010 // Get the default prefix if a name wasn't specified. 6011 if (!GlobalName) 6012 GlobalName = ".str"; 6013 // Create a global variable for this. 6014 auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, 6015 GlobalName, Alignment); 6016 if (Entry) 6017 *Entry = GV; 6018 6019 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6020 GV->getValueType(), Alignment); 6021 } 6022 6023 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( 6024 const MaterializeTemporaryExpr *E, const Expr *Init) { 6025 assert((E->getStorageDuration() == SD_Static || 6026 E->getStorageDuration() == SD_Thread) && "not a global temporary"); 6027 const auto *VD = cast<VarDecl>(E->getExtendingDecl()); 6028 6029 // If we're not materializing a subobject of the temporary, keep the 6030 // cv-qualifiers from the type of the MaterializeTemporaryExpr. 6031 QualType MaterializedType = Init->getType(); 6032 if (Init == E->getSubExpr()) 6033 MaterializedType = E->getType(); 6034 6035 CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); 6036 6037 auto InsertResult = MaterializedGlobalTemporaryMap.insert({E, nullptr}); 6038 if (!InsertResult.second) { 6039 // We've seen this before: either we already created it or we're in the 6040 // process of doing so. 6041 if (!InsertResult.first->second) { 6042 // We recursively re-entered this function, probably during emission of 6043 // the initializer. Create a placeholder. We'll clean this up in the 6044 // outer call, at the end of this function. 6045 llvm::Type *Type = getTypes().ConvertTypeForMem(MaterializedType); 6046 InsertResult.first->second = new llvm::GlobalVariable( 6047 getModule(), Type, false, llvm::GlobalVariable::InternalLinkage, 6048 nullptr); 6049 } 6050 return ConstantAddress(InsertResult.first->second, 6051 llvm::cast<llvm::GlobalVariable>( 6052 InsertResult.first->second->stripPointerCasts()) 6053 ->getValueType(), 6054 Align); 6055 } 6056 6057 // FIXME: If an externally-visible declaration extends multiple temporaries, 6058 // we need to give each temporary the same name in every translation unit (and 6059 // we also need to make the temporaries externally-visible). 6060 SmallString<256> Name; 6061 llvm::raw_svector_ostream Out(Name); 6062 getCXXABI().getMangleContext().mangleReferenceTemporary( 6063 VD, E->getManglingNumber(), Out); 6064 6065 APValue *Value = nullptr; 6066 if (E->getStorageDuration() == SD_Static && VD && VD->evaluateValue()) { 6067 // If the initializer of the extending declaration is a constant 6068 // initializer, we should have a cached constant initializer for this 6069 // temporary. Note that this might have a different value from the value 6070 // computed by evaluating the initializer if the surrounding constant 6071 // expression modifies the temporary. 6072 Value = E->getOrCreateValue(false); 6073 } 6074 6075 // Try evaluating it now, it might have a constant initializer. 6076 Expr::EvalResult EvalResult; 6077 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && 6078 !EvalResult.hasSideEffects()) 6079 Value = &EvalResult.Val; 6080 6081 LangAS AddrSpace = 6082 VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace(); 6083 6084 std::optional<ConstantEmitter> emitter; 6085 llvm::Constant *InitialValue = nullptr; 6086 bool Constant = false; 6087 llvm::Type *Type; 6088 if (Value) { 6089 // The temporary has a constant initializer, use it. 6090 emitter.emplace(*this); 6091 InitialValue = emitter->emitForInitializer(*Value, AddrSpace, 6092 MaterializedType); 6093 Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/ Value, 6094 /*ExcludeDtor*/ false); 6095 Type = InitialValue->getType(); 6096 } else { 6097 // No initializer, the initialization will be provided when we 6098 // initialize the declaration which performed lifetime extension. 6099 Type = getTypes().ConvertTypeForMem(MaterializedType); 6100 } 6101 6102 // Create a global variable for this lifetime-extended temporary. 6103 llvm::GlobalValue::LinkageTypes Linkage = 6104 getLLVMLinkageVarDefinition(VD, Constant); 6105 if (Linkage == llvm::GlobalVariable::ExternalLinkage) { 6106 const VarDecl *InitVD; 6107 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && 6108 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { 6109 // Temporaries defined inside a class get linkonce_odr linkage because the 6110 // class can be defined in multiple translation units. 6111 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; 6112 } else { 6113 // There is no need for this temporary to have external linkage if the 6114 // VarDecl has external linkage. 6115 Linkage = llvm::GlobalVariable::InternalLinkage; 6116 } 6117 } 6118 auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); 6119 auto *GV = new llvm::GlobalVariable( 6120 getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), 6121 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); 6122 if (emitter) emitter->finalize(GV); 6123 // Don't assign dllimport or dllexport to local linkage globals. 6124 if (!llvm::GlobalValue::isLocalLinkage(Linkage)) { 6125 setGVProperties(GV, VD); 6126 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass) 6127 // The reference temporary should never be dllexport. 6128 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); 6129 } 6130 GV->setAlignment(Align.getAsAlign()); 6131 if (supportsCOMDAT() && GV->isWeakForLinker()) 6132 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 6133 if (VD->getTLSKind()) 6134 setTLSMode(GV, *VD); 6135 llvm::Constant *CV = GV; 6136 if (AddrSpace != LangAS::Default) 6137 CV = getTargetCodeGenInfo().performAddrSpaceCast( 6138 *this, GV, AddrSpace, LangAS::Default, 6139 Type->getPointerTo( 6140 getContext().getTargetAddressSpace(LangAS::Default))); 6141 6142 // Update the map with the new temporary. If we created a placeholder above, 6143 // replace it with the new global now. 6144 llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E]; 6145 if (Entry) { 6146 Entry->replaceAllUsesWith( 6147 llvm::ConstantExpr::getBitCast(CV, Entry->getType())); 6148 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent(); 6149 } 6150 Entry = CV; 6151 6152 return ConstantAddress(CV, Type, Align); 6153 } 6154 6155 /// EmitObjCPropertyImplementations - Emit information for synthesized 6156 /// properties for an implementation. 6157 void CodeGenModule::EmitObjCPropertyImplementations(const 6158 ObjCImplementationDecl *D) { 6159 for (const auto *PID : D->property_impls()) { 6160 // Dynamic is just for type-checking. 6161 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 6162 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 6163 6164 // Determine which methods need to be implemented, some may have 6165 // been overridden. Note that ::isPropertyAccessor is not the method 6166 // we want, that just indicates if the decl came from a 6167 // property. What we want to know is if the method is defined in 6168 // this implementation. 6169 auto *Getter = PID->getGetterMethodDecl(); 6170 if (!Getter || Getter->isSynthesizedAccessorStub()) 6171 CodeGenFunction(*this).GenerateObjCGetter( 6172 const_cast<ObjCImplementationDecl *>(D), PID); 6173 auto *Setter = PID->getSetterMethodDecl(); 6174 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub())) 6175 CodeGenFunction(*this).GenerateObjCSetter( 6176 const_cast<ObjCImplementationDecl *>(D), PID); 6177 } 6178 } 6179 } 6180 6181 static bool needsDestructMethod(ObjCImplementationDecl *impl) { 6182 const ObjCInterfaceDecl *iface = impl->getClassInterface(); 6183 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 6184 ivar; ivar = ivar->getNextIvar()) 6185 if (ivar->getType().isDestructedType()) 6186 return true; 6187 6188 return false; 6189 } 6190 6191 static bool AllTrivialInitializers(CodeGenModule &CGM, 6192 ObjCImplementationDecl *D) { 6193 CodeGenFunction CGF(CGM); 6194 for (ObjCImplementationDecl::init_iterator B = D->init_begin(), 6195 E = D->init_end(); B != E; ++B) { 6196 CXXCtorInitializer *CtorInitExp = *B; 6197 Expr *Init = CtorInitExp->getInit(); 6198 if (!CGF.isTrivialInitializer(Init)) 6199 return false; 6200 } 6201 return true; 6202 } 6203 6204 /// EmitObjCIvarInitializations - Emit information for ivar initialization 6205 /// for an implementation. 6206 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 6207 // We might need a .cxx_destruct even if we don't have any ivar initializers. 6208 if (needsDestructMethod(D)) { 6209 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 6210 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 6211 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create( 6212 getContext(), D->getLocation(), D->getLocation(), cxxSelector, 6213 getContext().VoidTy, nullptr, D, 6214 /*isInstance=*/true, /*isVariadic=*/false, 6215 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, 6216 /*isImplicitlyDeclared=*/true, 6217 /*isDefined=*/false, ObjCMethodDecl::Required); 6218 D->addInstanceMethod(DTORMethod); 6219 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 6220 D->setHasDestructors(true); 6221 } 6222 6223 // If the implementation doesn't have any ivar initializers, we don't need 6224 // a .cxx_construct. 6225 if (D->getNumIvarInitializers() == 0 || 6226 AllTrivialInitializers(*this, D)) 6227 return; 6228 6229 IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); 6230 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 6231 // The constructor returns 'self'. 6232 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create( 6233 getContext(), D->getLocation(), D->getLocation(), cxxSelector, 6234 getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true, 6235 /*isVariadic=*/false, 6236 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, 6237 /*isImplicitlyDeclared=*/true, 6238 /*isDefined=*/false, ObjCMethodDecl::Required); 6239 D->addInstanceMethod(CTORMethod); 6240 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 6241 D->setHasNonZeroConstructors(true); 6242 } 6243 6244 // EmitLinkageSpec - Emit all declarations in a linkage spec. 6245 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 6246 if (LSD->getLanguage() != LinkageSpecDecl::lang_c && 6247 LSD->getLanguage() != LinkageSpecDecl::lang_cxx) { 6248 ErrorUnsupported(LSD, "linkage spec"); 6249 return; 6250 } 6251 6252 EmitDeclContext(LSD); 6253 } 6254 6255 void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) { 6256 std::unique_ptr<CodeGenFunction> &CurCGF = 6257 GlobalTopLevelStmtBlockInFlight.first; 6258 6259 // We emitted a top-level stmt but after it there is initialization. 6260 // Stop squashing the top-level stmts into a single function. 6261 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) { 6262 CurCGF->FinishFunction(D->getEndLoc()); 6263 CurCGF = nullptr; 6264 } 6265 6266 if (!CurCGF) { 6267 // void __stmts__N(void) 6268 // FIXME: Ask the ABI name mangler to pick a name. 6269 std::string Name = "__stmts__" + llvm::utostr(CXXGlobalInits.size()); 6270 FunctionArgList Args; 6271 QualType RetTy = getContext().VoidTy; 6272 const CGFunctionInfo &FnInfo = 6273 getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); 6274 llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo); 6275 llvm::Function *Fn = llvm::Function::Create( 6276 FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule()); 6277 6278 CurCGF.reset(new CodeGenFunction(*this)); 6279 GlobalTopLevelStmtBlockInFlight.second = D; 6280 CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, 6281 D->getBeginLoc(), D->getBeginLoc()); 6282 CXXGlobalInits.push_back(Fn); 6283 } 6284 6285 CurCGF->EmitStmt(D->getStmt()); 6286 } 6287 6288 void CodeGenModule::EmitDeclContext(const DeclContext *DC) { 6289 for (auto *I : DC->decls()) { 6290 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope 6291 // are themselves considered "top-level", so EmitTopLevelDecl on an 6292 // ObjCImplDecl does not recursively visit them. We need to do that in 6293 // case they're nested inside another construct (LinkageSpecDecl / 6294 // ExportDecl) that does stop them from being considered "top-level". 6295 if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { 6296 for (auto *M : OID->methods()) 6297 EmitTopLevelDecl(M); 6298 } 6299 6300 EmitTopLevelDecl(I); 6301 } 6302 } 6303 6304 /// EmitTopLevelDecl - Emit code for a single top level declaration. 6305 void CodeGenModule::EmitTopLevelDecl(Decl *D) { 6306 // Ignore dependent declarations. 6307 if (D->isTemplated()) 6308 return; 6309 6310 // Consteval function shouldn't be emitted. 6311 if (auto *FD = dyn_cast<FunctionDecl>(D)) 6312 if (FD->isConsteval()) 6313 return; 6314 6315 switch (D->getKind()) { 6316 case Decl::CXXConversion: 6317 case Decl::CXXMethod: 6318 case Decl::Function: 6319 EmitGlobal(cast<FunctionDecl>(D)); 6320 // Always provide some coverage mapping 6321 // even for the functions that aren't emitted. 6322 AddDeferredUnusedCoverageMapping(D); 6323 break; 6324 6325 case Decl::CXXDeductionGuide: 6326 // Function-like, but does not result in code emission. 6327 break; 6328 6329 case Decl::Var: 6330 case Decl::Decomposition: 6331 case Decl::VarTemplateSpecialization: 6332 EmitGlobal(cast<VarDecl>(D)); 6333 if (auto *DD = dyn_cast<DecompositionDecl>(D)) 6334 for (auto *B : DD->bindings()) 6335 if (auto *HD = B->getHoldingVar()) 6336 EmitGlobal(HD); 6337 break; 6338 6339 // Indirect fields from global anonymous structs and unions can be 6340 // ignored; only the actual variable requires IR gen support. 6341 case Decl::IndirectField: 6342 break; 6343 6344 // C++ Decls 6345 case Decl::Namespace: 6346 EmitDeclContext(cast<NamespaceDecl>(D)); 6347 break; 6348 case Decl::ClassTemplateSpecialization: { 6349 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); 6350 if (CGDebugInfo *DI = getModuleDebugInfo()) 6351 if (Spec->getSpecializationKind() == 6352 TSK_ExplicitInstantiationDefinition && 6353 Spec->hasDefinition()) 6354 DI->completeTemplateDefinition(*Spec); 6355 } [[fallthrough]]; 6356 case Decl::CXXRecord: { 6357 CXXRecordDecl *CRD = cast<CXXRecordDecl>(D); 6358 if (CGDebugInfo *DI = getModuleDebugInfo()) { 6359 if (CRD->hasDefinition()) 6360 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D))); 6361 if (auto *ES = D->getASTContext().getExternalSource()) 6362 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) 6363 DI->completeUnusedClass(*CRD); 6364 } 6365 // Emit any static data members, they may be definitions. 6366 for (auto *I : CRD->decls()) 6367 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) 6368 EmitTopLevelDecl(I); 6369 break; 6370 } 6371 // No code generation needed. 6372 case Decl::UsingShadow: 6373 case Decl::ClassTemplate: 6374 case Decl::VarTemplate: 6375 case Decl::Concept: 6376 case Decl::VarTemplatePartialSpecialization: 6377 case Decl::FunctionTemplate: 6378 case Decl::TypeAliasTemplate: 6379 case Decl::Block: 6380 case Decl::Empty: 6381 case Decl::Binding: 6382 break; 6383 case Decl::Using: // using X; [C++] 6384 if (CGDebugInfo *DI = getModuleDebugInfo()) 6385 DI->EmitUsingDecl(cast<UsingDecl>(*D)); 6386 break; 6387 case Decl::UsingEnum: // using enum X; [C++] 6388 if (CGDebugInfo *DI = getModuleDebugInfo()) 6389 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D)); 6390 break; 6391 case Decl::NamespaceAlias: 6392 if (CGDebugInfo *DI = getModuleDebugInfo()) 6393 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); 6394 break; 6395 case Decl::UsingDirective: // using namespace X; [C++] 6396 if (CGDebugInfo *DI = getModuleDebugInfo()) 6397 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); 6398 break; 6399 case Decl::CXXConstructor: 6400 getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 6401 break; 6402 case Decl::CXXDestructor: 6403 getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 6404 break; 6405 6406 case Decl::StaticAssert: 6407 // Nothing to do. 6408 break; 6409 6410 // Objective-C Decls 6411 6412 // Forward declarations, no (immediate) code generation. 6413 case Decl::ObjCInterface: 6414 case Decl::ObjCCategory: 6415 break; 6416 6417 case Decl::ObjCProtocol: { 6418 auto *Proto = cast<ObjCProtocolDecl>(D); 6419 if (Proto->isThisDeclarationADefinition()) 6420 ObjCRuntime->GenerateProtocol(Proto); 6421 break; 6422 } 6423 6424 case Decl::ObjCCategoryImpl: 6425 // Categories have properties but don't support synthesize so we 6426 // can ignore them here. 6427 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 6428 break; 6429 6430 case Decl::ObjCImplementation: { 6431 auto *OMD = cast<ObjCImplementationDecl>(D); 6432 EmitObjCPropertyImplementations(OMD); 6433 EmitObjCIvarInitializations(OMD); 6434 ObjCRuntime->GenerateClass(OMD); 6435 // Emit global variable debug information. 6436 if (CGDebugInfo *DI = getModuleDebugInfo()) 6437 if (getCodeGenOpts().hasReducedDebugInfo()) 6438 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( 6439 OMD->getClassInterface()), OMD->getLocation()); 6440 break; 6441 } 6442 case Decl::ObjCMethod: { 6443 auto *OMD = cast<ObjCMethodDecl>(D); 6444 // If this is not a prototype, emit the body. 6445 if (OMD->getBody()) 6446 CodeGenFunction(*this).GenerateObjCMethod(OMD); 6447 break; 6448 } 6449 case Decl::ObjCCompatibleAlias: 6450 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); 6451 break; 6452 6453 case Decl::PragmaComment: { 6454 const auto *PCD = cast<PragmaCommentDecl>(D); 6455 switch (PCD->getCommentKind()) { 6456 case PCK_Unknown: 6457 llvm_unreachable("unexpected pragma comment kind"); 6458 case PCK_Linker: 6459 AppendLinkerOptions(PCD->getArg()); 6460 break; 6461 case PCK_Lib: 6462 AddDependentLib(PCD->getArg()); 6463 break; 6464 case PCK_Compiler: 6465 case PCK_ExeStr: 6466 case PCK_User: 6467 break; // We ignore all of these. 6468 } 6469 break; 6470 } 6471 6472 case Decl::PragmaDetectMismatch: { 6473 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); 6474 AddDetectMismatch(PDMD->getName(), PDMD->getValue()); 6475 break; 6476 } 6477 6478 case Decl::LinkageSpec: 6479 EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 6480 break; 6481 6482 case Decl::FileScopeAsm: { 6483 // File-scope asm is ignored during device-side CUDA compilation. 6484 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) 6485 break; 6486 // File-scope asm is ignored during device-side OpenMP compilation. 6487 if (LangOpts.OpenMPIsDevice) 6488 break; 6489 // File-scope asm is ignored during device-side SYCL compilation. 6490 if (LangOpts.SYCLIsDevice) 6491 break; 6492 auto *AD = cast<FileScopeAsmDecl>(D); 6493 getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); 6494 break; 6495 } 6496 6497 case Decl::TopLevelStmt: 6498 EmitTopLevelStmt(cast<TopLevelStmtDecl>(D)); 6499 break; 6500 6501 case Decl::Import: { 6502 auto *Import = cast<ImportDecl>(D); 6503 6504 // If we've already imported this module, we're done. 6505 if (!ImportedModules.insert(Import->getImportedModule())) 6506 break; 6507 6508 // Emit debug information for direct imports. 6509 if (!Import->getImportedOwningModule()) { 6510 if (CGDebugInfo *DI = getModuleDebugInfo()) 6511 DI->EmitImportDecl(*Import); 6512 } 6513 6514 // For C++ standard modules we are done - we will call the module 6515 // initializer for imported modules, and that will likewise call those for 6516 // any imports it has. 6517 if (CXX20ModuleInits && Import->getImportedOwningModule() && 6518 !Import->getImportedOwningModule()->isModuleMapModule()) 6519 break; 6520 6521 // For clang C++ module map modules the initializers for sub-modules are 6522 // emitted here. 6523 6524 // Find all of the submodules and emit the module initializers. 6525 llvm::SmallPtrSet<clang::Module *, 16> Visited; 6526 SmallVector<clang::Module *, 16> Stack; 6527 Visited.insert(Import->getImportedModule()); 6528 Stack.push_back(Import->getImportedModule()); 6529 6530 while (!Stack.empty()) { 6531 clang::Module *Mod = Stack.pop_back_val(); 6532 if (!EmittedModuleInitializers.insert(Mod).second) 6533 continue; 6534 6535 for (auto *D : Context.getModuleInitializers(Mod)) 6536 EmitTopLevelDecl(D); 6537 6538 // Visit the submodules of this module. 6539 for (clang::Module::submodule_iterator Sub = Mod->submodule_begin(), 6540 SubEnd = Mod->submodule_end(); 6541 Sub != SubEnd; ++Sub) { 6542 // Skip explicit children; they need to be explicitly imported to emit 6543 // the initializers. 6544 if ((*Sub)->IsExplicit) 6545 continue; 6546 6547 if (Visited.insert(*Sub).second) 6548 Stack.push_back(*Sub); 6549 } 6550 } 6551 break; 6552 } 6553 6554 case Decl::Export: 6555 EmitDeclContext(cast<ExportDecl>(D)); 6556 break; 6557 6558 case Decl::OMPThreadPrivate: 6559 EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); 6560 break; 6561 6562 case Decl::OMPAllocate: 6563 EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D)); 6564 break; 6565 6566 case Decl::OMPDeclareReduction: 6567 EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); 6568 break; 6569 6570 case Decl::OMPDeclareMapper: 6571 EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D)); 6572 break; 6573 6574 case Decl::OMPRequires: 6575 EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D)); 6576 break; 6577 6578 case Decl::Typedef: 6579 case Decl::TypeAlias: // using foo = bar; [C++11] 6580 if (CGDebugInfo *DI = getModuleDebugInfo()) 6581 DI->EmitAndRetainType( 6582 getContext().getTypedefType(cast<TypedefNameDecl>(D))); 6583 break; 6584 6585 case Decl::Record: 6586 if (CGDebugInfo *DI = getModuleDebugInfo()) 6587 if (cast<RecordDecl>(D)->getDefinition()) 6588 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D))); 6589 break; 6590 6591 case Decl::Enum: 6592 if (CGDebugInfo *DI = getModuleDebugInfo()) 6593 if (cast<EnumDecl>(D)->getDefinition()) 6594 DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D))); 6595 break; 6596 6597 case Decl::HLSLBuffer: 6598 getHLSLRuntime().addBuffer(cast<HLSLBufferDecl>(D)); 6599 break; 6600 6601 default: 6602 // Make sure we handled everything we should, every other kind is a 6603 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 6604 // function. Need to recode Decl::Kind to do that easily. 6605 assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 6606 break; 6607 } 6608 } 6609 6610 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { 6611 // Do we need to generate coverage mapping? 6612 if (!CodeGenOpts.CoverageMapping) 6613 return; 6614 switch (D->getKind()) { 6615 case Decl::CXXConversion: 6616 case Decl::CXXMethod: 6617 case Decl::Function: 6618 case Decl::ObjCMethod: 6619 case Decl::CXXConstructor: 6620 case Decl::CXXDestructor: { 6621 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) 6622 break; 6623 SourceManager &SM = getContext().getSourceManager(); 6624 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) 6625 break; 6626 auto I = DeferredEmptyCoverageMappingDecls.find(D); 6627 if (I == DeferredEmptyCoverageMappingDecls.end()) 6628 DeferredEmptyCoverageMappingDecls[D] = true; 6629 break; 6630 } 6631 default: 6632 break; 6633 }; 6634 } 6635 6636 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { 6637 // Do we need to generate coverage mapping? 6638 if (!CodeGenOpts.CoverageMapping) 6639 return; 6640 if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { 6641 if (Fn->isTemplateInstantiation()) 6642 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); 6643 } 6644 auto I = DeferredEmptyCoverageMappingDecls.find(D); 6645 if (I == DeferredEmptyCoverageMappingDecls.end()) 6646 DeferredEmptyCoverageMappingDecls[D] = false; 6647 else 6648 I->second = false; 6649 } 6650 6651 void CodeGenModule::EmitDeferredUnusedCoverageMappings() { 6652 // We call takeVector() here to avoid use-after-free. 6653 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because 6654 // we deserialize function bodies to emit coverage info for them, and that 6655 // deserializes more declarations. How should we handle that case? 6656 for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) { 6657 if (!Entry.second) 6658 continue; 6659 const Decl *D = Entry.first; 6660 switch (D->getKind()) { 6661 case Decl::CXXConversion: 6662 case Decl::CXXMethod: 6663 case Decl::Function: 6664 case Decl::ObjCMethod: { 6665 CodeGenPGO PGO(*this); 6666 GlobalDecl GD(cast<FunctionDecl>(D)); 6667 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 6668 getFunctionLinkage(GD)); 6669 break; 6670 } 6671 case Decl::CXXConstructor: { 6672 CodeGenPGO PGO(*this); 6673 GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); 6674 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 6675 getFunctionLinkage(GD)); 6676 break; 6677 } 6678 case Decl::CXXDestructor: { 6679 CodeGenPGO PGO(*this); 6680 GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); 6681 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 6682 getFunctionLinkage(GD)); 6683 break; 6684 } 6685 default: 6686 break; 6687 }; 6688 } 6689 } 6690 6691 void CodeGenModule::EmitMainVoidAlias() { 6692 // In order to transition away from "__original_main" gracefully, emit an 6693 // alias for "main" in the no-argument case so that libc can detect when 6694 // new-style no-argument main is in used. 6695 if (llvm::Function *F = getModule().getFunction("main")) { 6696 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() && 6697 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) { 6698 auto *GA = llvm::GlobalAlias::create("__main_void", F); 6699 GA->setVisibility(llvm::GlobalValue::HiddenVisibility); 6700 } 6701 } 6702 } 6703 6704 /// Turns the given pointer into a constant. 6705 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, 6706 const void *Ptr) { 6707 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); 6708 llvm::Type *i64 = llvm::Type::getInt64Ty(Context); 6709 return llvm::ConstantInt::get(i64, PtrInt); 6710 } 6711 6712 static void EmitGlobalDeclMetadata(CodeGenModule &CGM, 6713 llvm::NamedMDNode *&GlobalMetadata, 6714 GlobalDecl D, 6715 llvm::GlobalValue *Addr) { 6716 if (!GlobalMetadata) 6717 GlobalMetadata = 6718 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); 6719 6720 // TODO: should we report variant information for ctors/dtors? 6721 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), 6722 llvm::ConstantAsMetadata::get(GetPointerConstant( 6723 CGM.getLLVMContext(), D.getDecl()))}; 6724 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); 6725 } 6726 6727 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem, 6728 llvm::GlobalValue *CppFunc) { 6729 // Store the list of ifuncs we need to replace uses in. 6730 llvm::SmallVector<llvm::GlobalIFunc *> IFuncs; 6731 // List of ConstantExprs that we should be able to delete when we're done 6732 // here. 6733 llvm::SmallVector<llvm::ConstantExpr *> CEs; 6734 6735 // It isn't valid to replace the extern-C ifuncs if all we find is itself! 6736 if (Elem == CppFunc) 6737 return false; 6738 6739 // First make sure that all users of this are ifuncs (or ifuncs via a 6740 // bitcast), and collect the list of ifuncs and CEs so we can work on them 6741 // later. 6742 for (llvm::User *User : Elem->users()) { 6743 // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an 6744 // ifunc directly. In any other case, just give up, as we don't know what we 6745 // could break by changing those. 6746 if (auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) { 6747 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast) 6748 return false; 6749 6750 for (llvm::User *CEUser : ConstExpr->users()) { 6751 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) { 6752 IFuncs.push_back(IFunc); 6753 } else { 6754 return false; 6755 } 6756 } 6757 CEs.push_back(ConstExpr); 6758 } else if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) { 6759 IFuncs.push_back(IFunc); 6760 } else { 6761 // This user is one we don't know how to handle, so fail redirection. This 6762 // will result in an ifunc retaining a resolver name that will ultimately 6763 // fail to be resolved to a defined function. 6764 return false; 6765 } 6766 } 6767 6768 // Now we know this is a valid case where we can do this alias replacement, we 6769 // need to remove all of the references to Elem (and the bitcasts!) so we can 6770 // delete it. 6771 for (llvm::GlobalIFunc *IFunc : IFuncs) 6772 IFunc->setResolver(nullptr); 6773 for (llvm::ConstantExpr *ConstExpr : CEs) 6774 ConstExpr->destroyConstant(); 6775 6776 // We should now be out of uses for the 'old' version of this function, so we 6777 // can erase it as well. 6778 Elem->eraseFromParent(); 6779 6780 for (llvm::GlobalIFunc *IFunc : IFuncs) { 6781 // The type of the resolver is always just a function-type that returns the 6782 // type of the IFunc, so create that here. If the type of the actual 6783 // resolver doesn't match, it just gets bitcast to the right thing. 6784 auto *ResolverTy = 6785 llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false); 6786 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 6787 CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false); 6788 IFunc->setResolver(Resolver); 6789 } 6790 return true; 6791 } 6792 6793 /// For each function which is declared within an extern "C" region and marked 6794 /// as 'used', but has internal linkage, create an alias from the unmangled 6795 /// name to the mangled name if possible. People expect to be able to refer 6796 /// to such functions with an unmangled name from inline assembly within the 6797 /// same translation unit. 6798 void CodeGenModule::EmitStaticExternCAliases() { 6799 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases()) 6800 return; 6801 for (auto &I : StaticExternCValues) { 6802 IdentifierInfo *Name = I.first; 6803 llvm::GlobalValue *Val = I.second; 6804 6805 // If Val is null, that implies there were multiple declarations that each 6806 // had a claim to the unmangled name. In this case, generation of the alias 6807 // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC. 6808 if (!Val) 6809 break; 6810 6811 llvm::GlobalValue *ExistingElem = 6812 getModule().getNamedValue(Name->getName()); 6813 6814 // If there is either not something already by this name, or we were able to 6815 // replace all uses from IFuncs, create the alias. 6816 if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val)) 6817 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); 6818 } 6819 } 6820 6821 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, 6822 GlobalDecl &Result) const { 6823 auto Res = Manglings.find(MangledName); 6824 if (Res == Manglings.end()) 6825 return false; 6826 Result = Res->getValue(); 6827 return true; 6828 } 6829 6830 /// Emits metadata nodes associating all the global values in the 6831 /// current module with the Decls they came from. This is useful for 6832 /// projects using IR gen as a subroutine. 6833 /// 6834 /// Since there's currently no way to associate an MDNode directly 6835 /// with an llvm::GlobalValue, we create a global named metadata 6836 /// with the name 'clang.global.decl.ptrs'. 6837 void CodeGenModule::EmitDeclMetadata() { 6838 llvm::NamedMDNode *GlobalMetadata = nullptr; 6839 6840 for (auto &I : MangledDeclNames) { 6841 llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); 6842 // Some mangled names don't necessarily have an associated GlobalValue 6843 // in this module, e.g. if we mangled it for DebugInfo. 6844 if (Addr) 6845 EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); 6846 } 6847 } 6848 6849 /// Emits metadata nodes for all the local variables in the current 6850 /// function. 6851 void CodeGenFunction::EmitDeclMetadata() { 6852 if (LocalDeclMap.empty()) return; 6853 6854 llvm::LLVMContext &Context = getLLVMContext(); 6855 6856 // Find the unique metadata ID for this name. 6857 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); 6858 6859 llvm::NamedMDNode *GlobalMetadata = nullptr; 6860 6861 for (auto &I : LocalDeclMap) { 6862 const Decl *D = I.first; 6863 llvm::Value *Addr = I.second.getPointer(); 6864 if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { 6865 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); 6866 Alloca->setMetadata( 6867 DeclPtrKind, llvm::MDNode::get( 6868 Context, llvm::ValueAsMetadata::getConstant(DAddr))); 6869 } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { 6870 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); 6871 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); 6872 } 6873 } 6874 } 6875 6876 void CodeGenModule::EmitVersionIdentMetadata() { 6877 llvm::NamedMDNode *IdentMetadata = 6878 TheModule.getOrInsertNamedMetadata("llvm.ident"); 6879 std::string Version = getClangFullVersion(); 6880 llvm::LLVMContext &Ctx = TheModule.getContext(); 6881 6882 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; 6883 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); 6884 } 6885 6886 void CodeGenModule::EmitCommandLineMetadata() { 6887 llvm::NamedMDNode *CommandLineMetadata = 6888 TheModule.getOrInsertNamedMetadata("llvm.commandline"); 6889 std::string CommandLine = getCodeGenOpts().RecordCommandLine; 6890 llvm::LLVMContext &Ctx = TheModule.getContext(); 6891 6892 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)}; 6893 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode)); 6894 } 6895 6896 void CodeGenModule::EmitCoverageFile() { 6897 if (getCodeGenOpts().CoverageDataFile.empty() && 6898 getCodeGenOpts().CoverageNotesFile.empty()) 6899 return; 6900 6901 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); 6902 if (!CUNode) 6903 return; 6904 6905 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); 6906 llvm::LLVMContext &Ctx = TheModule.getContext(); 6907 auto *CoverageDataFile = 6908 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); 6909 auto *CoverageNotesFile = 6910 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); 6911 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { 6912 llvm::MDNode *CU = CUNode->getOperand(i); 6913 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; 6914 GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); 6915 } 6916 } 6917 6918 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, 6919 bool ForEH) { 6920 // Return a bogus pointer if RTTI is disabled, unless it's for EH. 6921 // FIXME: should we even be calling this method if RTTI is disabled 6922 // and it's not for EH? 6923 if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice || 6924 (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && 6925 getTriple().isNVPTX())) 6926 return llvm::Constant::getNullValue(Int8PtrTy); 6927 6928 if (ForEH && Ty->isObjCObjectPointerType() && 6929 LangOpts.ObjCRuntime.isGNUFamily()) 6930 return ObjCRuntime->GetEHType(Ty); 6931 6932 return getCXXABI().getAddrOfRTTIDescriptor(Ty); 6933 } 6934 6935 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { 6936 // Do not emit threadprivates in simd-only mode. 6937 if (LangOpts.OpenMP && LangOpts.OpenMPSimd) 6938 return; 6939 for (auto RefExpr : D->varlists()) { 6940 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); 6941 bool PerformInit = 6942 VD->getAnyInitializer() && 6943 !VD->getAnyInitializer()->isConstantInitializer(getContext(), 6944 /*ForRef=*/false); 6945 6946 Address Addr(GetAddrOfGlobalVar(VD), 6947 getTypes().ConvertTypeForMem(VD->getType()), 6948 getContext().getDeclAlign(VD)); 6949 if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( 6950 VD, Addr, RefExpr->getBeginLoc(), PerformInit)) 6951 CXXGlobalInits.push_back(InitFunction); 6952 } 6953 } 6954 6955 llvm::Metadata * 6956 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, 6957 StringRef Suffix) { 6958 if (auto *FnType = T->getAs<FunctionProtoType>()) 6959 T = getContext().getFunctionType( 6960 FnType->getReturnType(), FnType->getParamTypes(), 6961 FnType->getExtProtoInfo().withExceptionSpec(EST_None)); 6962 6963 llvm::Metadata *&InternalId = Map[T.getCanonicalType()]; 6964 if (InternalId) 6965 return InternalId; 6966 6967 if (isExternallyVisible(T->getLinkage())) { 6968 std::string OutName; 6969 llvm::raw_string_ostream Out(OutName); 6970 getCXXABI().getMangleContext().mangleTypeName( 6971 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers); 6972 6973 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers) 6974 Out << ".normalized"; 6975 6976 Out << Suffix; 6977 6978 InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); 6979 } else { 6980 InternalId = llvm::MDNode::getDistinct(getLLVMContext(), 6981 llvm::ArrayRef<llvm::Metadata *>()); 6982 } 6983 6984 return InternalId; 6985 } 6986 6987 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { 6988 return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); 6989 } 6990 6991 llvm::Metadata * 6992 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { 6993 return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual"); 6994 } 6995 6996 // Generalize pointer types to a void pointer with the qualifiers of the 6997 // originally pointed-to type, e.g. 'const char *' and 'char * const *' 6998 // generalize to 'const void *' while 'char *' and 'const char **' generalize to 6999 // 'void *'. 7000 static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { 7001 if (!Ty->isPointerType()) 7002 return Ty; 7003 7004 return Ctx.getPointerType( 7005 QualType(Ctx.VoidTy).withCVRQualifiers( 7006 Ty->getPointeeType().getCVRQualifiers())); 7007 } 7008 7009 // Apply type generalization to a FunctionType's return and argument types 7010 static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { 7011 if (auto *FnType = Ty->getAs<FunctionProtoType>()) { 7012 SmallVector<QualType, 8> GeneralizedParams; 7013 for (auto &Param : FnType->param_types()) 7014 GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); 7015 7016 return Ctx.getFunctionType( 7017 GeneralizeType(Ctx, FnType->getReturnType()), 7018 GeneralizedParams, FnType->getExtProtoInfo()); 7019 } 7020 7021 if (auto *FnType = Ty->getAs<FunctionNoProtoType>()) 7022 return Ctx.getFunctionNoProtoType( 7023 GeneralizeType(Ctx, FnType->getReturnType())); 7024 7025 llvm_unreachable("Encountered unknown FunctionType"); 7026 } 7027 7028 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { 7029 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T), 7030 GeneralizedMetadataIdMap, ".generalized"); 7031 } 7032 7033 /// Returns whether this module needs the "all-vtables" type identifier. 7034 bool CodeGenModule::NeedAllVtablesTypeId() const { 7035 // Returns true if at least one of vtable-based CFI checkers is enabled and 7036 // is not in the trapping mode. 7037 return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && 7038 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || 7039 (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && 7040 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || 7041 (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && 7042 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || 7043 (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && 7044 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); 7045 } 7046 7047 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, 7048 CharUnits Offset, 7049 const CXXRecordDecl *RD) { 7050 llvm::Metadata *MD = 7051 CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); 7052 VTable->addTypeMetadata(Offset.getQuantity(), MD); 7053 7054 if (CodeGenOpts.SanitizeCfiCrossDso) 7055 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) 7056 VTable->addTypeMetadata(Offset.getQuantity(), 7057 llvm::ConstantAsMetadata::get(CrossDsoTypeId)); 7058 7059 if (NeedAllVtablesTypeId()) { 7060 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); 7061 VTable->addTypeMetadata(Offset.getQuantity(), MD); 7062 } 7063 } 7064 7065 llvm::SanitizerStatReport &CodeGenModule::getSanStats() { 7066 if (!SanStats) 7067 SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule()); 7068 7069 return *SanStats; 7070 } 7071 7072 llvm::Value * 7073 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, 7074 CodeGenFunction &CGF) { 7075 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType()); 7076 auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); 7077 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); 7078 auto *Call = CGF.EmitRuntimeCall( 7079 CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C}); 7080 return Call; 7081 } 7082 7083 CharUnits CodeGenModule::getNaturalPointeeTypeAlignment( 7084 QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) { 7085 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo, 7086 /* forPointeeType= */ true); 7087 } 7088 7089 CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T, 7090 LValueBaseInfo *BaseInfo, 7091 TBAAAccessInfo *TBAAInfo, 7092 bool forPointeeType) { 7093 if (TBAAInfo) 7094 *TBAAInfo = getTBAAAccessInfo(T); 7095 7096 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But 7097 // that doesn't return the information we need to compute BaseInfo. 7098 7099 // Honor alignment typedef attributes even on incomplete types. 7100 // We also honor them straight for C++ class types, even as pointees; 7101 // there's an expressivity gap here. 7102 if (auto TT = T->getAs<TypedefType>()) { 7103 if (auto Align = TT->getDecl()->getMaxAlignment()) { 7104 if (BaseInfo) 7105 *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType); 7106 return getContext().toCharUnitsFromBits(Align); 7107 } 7108 } 7109 7110 bool AlignForArray = T->isArrayType(); 7111 7112 // Analyze the base element type, so we don't get confused by incomplete 7113 // array types. 7114 T = getContext().getBaseElementType(T); 7115 7116 if (T->isIncompleteType()) { 7117 // We could try to replicate the logic from 7118 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the 7119 // type is incomplete, so it's impossible to test. We could try to reuse 7120 // getTypeAlignIfKnown, but that doesn't return the information we need 7121 // to set BaseInfo. So just ignore the possibility that the alignment is 7122 // greater than one. 7123 if (BaseInfo) 7124 *BaseInfo = LValueBaseInfo(AlignmentSource::Type); 7125 return CharUnits::One(); 7126 } 7127 7128 if (BaseInfo) 7129 *BaseInfo = LValueBaseInfo(AlignmentSource::Type); 7130 7131 CharUnits Alignment; 7132 const CXXRecordDecl *RD; 7133 if (T.getQualifiers().hasUnaligned()) { 7134 Alignment = CharUnits::One(); 7135 } else if (forPointeeType && !AlignForArray && 7136 (RD = T->getAsCXXRecordDecl())) { 7137 // For C++ class pointees, we don't know whether we're pointing at a 7138 // base or a complete object, so we generally need to use the 7139 // non-virtual alignment. 7140 Alignment = getClassPointerAlignment(RD); 7141 } else { 7142 Alignment = getContext().getTypeAlignInChars(T); 7143 } 7144 7145 // Cap to the global maximum type alignment unless the alignment 7146 // was somehow explicit on the type. 7147 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) { 7148 if (Alignment.getQuantity() > MaxAlign && 7149 !getContext().isAlignmentRequired(T)) 7150 Alignment = CharUnits::fromQuantity(MaxAlign); 7151 } 7152 return Alignment; 7153 } 7154 7155 bool CodeGenModule::stopAutoInit() { 7156 unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter; 7157 if (StopAfter) { 7158 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is 7159 // used 7160 if (NumAutoVarInit >= StopAfter) { 7161 return true; 7162 } 7163 if (!NumAutoVarInit) { 7164 unsigned DiagID = getDiags().getCustomDiagID( 7165 DiagnosticsEngine::Warning, 7166 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the " 7167 "number of times ftrivial-auto-var-init=%1 gets applied."); 7168 getDiags().Report(DiagID) 7169 << StopAfter 7170 << (getContext().getLangOpts().getTrivialAutoVarInit() == 7171 LangOptions::TrivialAutoVarInitKind::Zero 7172 ? "zero" 7173 : "pattern"); 7174 } 7175 ++NumAutoVarInit; 7176 } 7177 return false; 7178 } 7179 7180 void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &OS, 7181 const Decl *D) const { 7182 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers 7183 // postfix beginning with '.' since the symbol name can be demangled. 7184 if (LangOpts.HIP) 7185 OS << (isa<VarDecl>(D) ? ".static." : ".intern."); 7186 else 7187 OS << (isa<VarDecl>(D) ? "__static__" : "__intern__"); 7188 7189 // If the CUID is not specified we try to generate a unique postfix. 7190 if (getLangOpts().CUID.empty()) { 7191 SourceManager &SM = getContext().getSourceManager(); 7192 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); 7193 assert(PLoc.isValid() && "Source location is expected to be valid."); 7194 7195 // Get the hash of the user defined macros. 7196 llvm::MD5 Hash; 7197 llvm::MD5::MD5Result Result; 7198 for (const auto &Arg : PreprocessorOpts.Macros) 7199 Hash.update(Arg.first); 7200 Hash.final(Result); 7201 7202 // Get the UniqueID for the file containing the decl. 7203 llvm::sys::fs::UniqueID ID; 7204 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) { 7205 PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false); 7206 assert(PLoc.isValid() && "Source location is expected to be valid."); 7207 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) 7208 SM.getDiagnostics().Report(diag::err_cannot_open_file) 7209 << PLoc.getFilename() << EC.message(); 7210 } 7211 OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice()) 7212 << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8); 7213 } else { 7214 OS << getContext().getCUIDHash(); 7215 } 7216 } 7217 7218 void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) { 7219 assert(DeferredDeclsToEmit.empty() && 7220 "Should have emitted all decls deferred to emit."); 7221 assert(NewBuilder->DeferredDecls.empty() && 7222 "Newly created module should not have deferred decls"); 7223 NewBuilder->DeferredDecls = std::move(DeferredDecls); 7224 7225 assert(NewBuilder->DeferredVTables.empty() && 7226 "Newly created module should not have deferred vtables"); 7227 NewBuilder->DeferredVTables = std::move(DeferredVTables); 7228 7229 assert(NewBuilder->MangledDeclNames.empty() && 7230 "Newly created module should not have mangled decl names"); 7231 assert(NewBuilder->Manglings.empty() && 7232 "Newly created module should not have manglings"); 7233 NewBuilder->Manglings = std::move(Manglings); 7234 7235 assert(WeakRefReferences.empty() && "Not all WeakRefRefs have been applied"); 7236 NewBuilder->WeakRefReferences = std::move(WeakRefReferences); 7237 7238 NewBuilder->TBAA = std::move(TBAA); 7239 7240 assert(NewBuilder->EmittedDeferredDecls.empty() && 7241 "Still have (unmerged) EmittedDeferredDecls deferred decls"); 7242 7243 NewBuilder->EmittedDeferredDecls = std::move(EmittedDeferredDecls); 7244 7245 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx); 7246 } 7247