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