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.ends_with("f")) 229 ABIFLen = 32; 230 else if (ABIStr.ends_with("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.ends_with("f")) 312 ABIFRLen = 32; 313 else if (ABIStr.ends_with("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 = getClangVendor() + "clang"; 999 getModule().addModuleFlag(llvm::Module::Error, "zos_product_id", 1000 llvm::MDString::get(VMContext, ProductId)); 1001 1002 // Record the language because we need it for the PPA2. 1003 StringRef lang_str = languageToString( 1004 LangStandard::getLangStandardForKind(LangOpts.LangStd).Language); 1005 getModule().addModuleFlag(llvm::Module::Error, "zos_cu_language", 1006 llvm::MDString::get(VMContext, lang_str)); 1007 1008 time_t TT = PreprocessorOpts.SourceDateEpoch 1009 ? *PreprocessorOpts.SourceDateEpoch 1010 : std::time(nullptr); 1011 getModule().addModuleFlag(llvm::Module::Max, "zos_translation_time", 1012 static_cast<uint64_t>(TT)); 1013 1014 // Multiple modes will be supported here. 1015 getModule().addModuleFlag(llvm::Module::Error, "zos_le_char_mode", 1016 llvm::MDString::get(VMContext, "ascii")); 1017 } 1018 1019 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch(); 1020 if ( Arch == llvm::Triple::arm 1021 || Arch == llvm::Triple::armeb 1022 || Arch == llvm::Triple::thumb 1023 || Arch == llvm::Triple::thumbeb) { 1024 // The minimum width of an enum in bytes 1025 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4; 1026 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth); 1027 } 1028 1029 if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) { 1030 StringRef ABIStr = Target.getABI(); 1031 llvm::LLVMContext &Ctx = TheModule.getContext(); 1032 getModule().addModuleFlag(llvm::Module::Error, "target-abi", 1033 llvm::MDString::get(Ctx, ABIStr)); 1034 } 1035 1036 if (CodeGenOpts.SanitizeCfiCrossDso) { 1037 // Indicate that we want cross-DSO control flow integrity checks. 1038 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1); 1039 } 1040 1041 if (CodeGenOpts.WholeProgramVTables) { 1042 // Indicate whether VFE was enabled for this module, so that the 1043 // vcall_visibility metadata added under whole program vtables is handled 1044 // appropriately in the optimizer. 1045 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim", 1046 CodeGenOpts.VirtualFunctionElimination); 1047 } 1048 1049 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) { 1050 getModule().addModuleFlag(llvm::Module::Override, 1051 "CFI Canonical Jump Tables", 1052 CodeGenOpts.SanitizeCfiCanonicalJumpTables); 1053 } 1054 1055 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) { 1056 getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1); 1057 // KCFI assumes patchable-function-prefix is the same for all indirectly 1058 // called functions. Store the expected offset for code generation. 1059 if (CodeGenOpts.PatchableFunctionEntryOffset) 1060 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset", 1061 CodeGenOpts.PatchableFunctionEntryOffset); 1062 } 1063 1064 if (CodeGenOpts.CFProtectionReturn && 1065 Target.checkCFProtectionReturnSupported(getDiags())) { 1066 // Indicate that we want to instrument return control flow protection. 1067 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return", 1068 1); 1069 } 1070 1071 if (CodeGenOpts.CFProtectionBranch && 1072 Target.checkCFProtectionBranchSupported(getDiags())) { 1073 // Indicate that we want to instrument branch control flow protection. 1074 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch", 1075 1); 1076 } 1077 1078 if (CodeGenOpts.FunctionReturnThunks) 1079 getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1); 1080 1081 if (CodeGenOpts.IndirectBranchCSPrefix) 1082 getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1); 1083 1084 // Add module metadata for return address signing (ignoring 1085 // non-leaf/all) and stack tagging. These are actually turned on by function 1086 // attributes, but we use module metadata to emit build attributes. This is 1087 // needed for LTO, where the function attributes are inside bitcode 1088 // serialised into a global variable by the time build attributes are 1089 // emitted, so we can't access them. LTO objects could be compiled with 1090 // different flags therefore module flags are set to "Min" behavior to achieve 1091 // the same end result of the normal build where e.g BTI is off if any object 1092 // doesn't support it. 1093 if (Context.getTargetInfo().hasFeature("ptrauth") && 1094 LangOpts.getSignReturnAddressScope() != 1095 LangOptions::SignReturnAddressScopeKind::None) 1096 getModule().addModuleFlag(llvm::Module::Override, 1097 "sign-return-address-buildattr", 1); 1098 if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack)) 1099 getModule().addModuleFlag(llvm::Module::Override, 1100 "tag-stack-memory-buildattr", 1); 1101 1102 if (Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb || 1103 Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb || 1104 Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 || 1105 Arch == llvm::Triple::aarch64_be) { 1106 if (LangOpts.BranchTargetEnforcement) 1107 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement", 1108 1); 1109 if (LangOpts.BranchProtectionPAuthLR) 1110 getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr", 1111 1); 1112 if (LangOpts.GuardedControlStack) 1113 getModule().addModuleFlag(llvm::Module::Min, "guarded-control-stack", 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.starts_with("+") && RHS.starts_with("+") && 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.starts_with("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.starts_with("__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.starts_with("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 // In Aarch64, default versions of multiversioned functions are mangled to 4184 // their 'normal' assembly name. This deviates from other targets which 4185 // append a '.default' string. As a result we need to continue appending 4186 // .ifunc in Aarch64. 4187 // FIXME: Should Aarch64 mangling for 'default' multiversion function and 4188 // in turn ifunc function match that of other targets? 4189 if (FD->isTargetClonesMultiVersion() && 4190 !getTarget().getTriple().isAArch64()) { 4191 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 4192 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI); 4193 std::string MangledName = getMangledNameImpl( 4194 *this, GD, FD, /*OmitMultiVersionMangling=*/true); 4195 // In prior versions of Clang, the mangling for ifuncs incorrectly 4196 // included an .ifunc suffix. This alias is generated for backward 4197 // compatibility. It is deprecated, and may be removed in the future. 4198 auto *Alias = llvm::GlobalAlias::create( 4199 DeclTy, 0, getMultiversionLinkage(*this, GD), 4200 MangledName + ".ifunc", IFunc, &getModule()); 4201 SetCommonAttributes(FD, Alias); 4202 } 4203 } 4204 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant); 4205 4206 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD)); 4207 4208 if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT()) 4209 ResolverFunc->setComdat( 4210 getModule().getOrInsertComdat(ResolverFunc->getName())); 4211 4212 const TargetInfo &TI = getTarget(); 4213 llvm::stable_sort( 4214 Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS, 4215 const CodeGenFunction::MultiVersionResolverOption &RHS) { 4216 return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS); 4217 }); 4218 CodeGenFunction CGF(*this); 4219 CGF.EmitMultiVersionResolver(ResolverFunc, Options); 4220 } 4221 4222 // Ensure that any additions to the deferred decls list caused by emitting a 4223 // variant are emitted. This can happen when the variant itself is inline and 4224 // calls a function without linkage. 4225 if (!MVFuncsToEmit.empty()) 4226 EmitDeferred(); 4227 4228 // Ensure that any additions to the multiversion funcs list from either the 4229 // deferred decls or the multiversion functions themselves are emitted. 4230 if (!MultiVersionFuncs.empty()) 4231 emitMultiVersionFunctions(); 4232 } 4233 4234 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) { 4235 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 4236 assert(FD && "Not a FunctionDecl?"); 4237 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?"); 4238 const auto *DD = FD->getAttr<CPUDispatchAttr>(); 4239 assert(DD && "Not a cpu_dispatch Function?"); 4240 4241 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 4242 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI); 4243 4244 StringRef ResolverName = getMangledName(GD); 4245 UpdateMultiVersionNames(GD, FD, ResolverName); 4246 4247 llvm::Type *ResolverType; 4248 GlobalDecl ResolverGD; 4249 if (getTarget().supportsIFunc()) { 4250 ResolverType = llvm::FunctionType::get( 4251 llvm::PointerType::get(DeclTy, 4252 getTypes().getTargetAddressSpace(FD->getType())), 4253 false); 4254 } 4255 else { 4256 ResolverType = DeclTy; 4257 ResolverGD = GD; 4258 } 4259 4260 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction( 4261 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false)); 4262 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD)); 4263 if (supportsCOMDAT()) 4264 ResolverFunc->setComdat( 4265 getModule().getOrInsertComdat(ResolverFunc->getName())); 4266 4267 SmallVector<CodeGenFunction::MultiVersionResolverOption, 10> Options; 4268 const TargetInfo &Target = getTarget(); 4269 unsigned Index = 0; 4270 for (const IdentifierInfo *II : DD->cpus()) { 4271 // Get the name of the target function so we can look it up/create it. 4272 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) + 4273 getCPUSpecificMangling(*this, II->getName()); 4274 4275 llvm::Constant *Func = GetGlobalValue(MangledName); 4276 4277 if (!Func) { 4278 GlobalDecl ExistingDecl = Manglings.lookup(MangledName); 4279 if (ExistingDecl.getDecl() && 4280 ExistingDecl.getDecl()->getAsFunction()->isDefined()) { 4281 EmitGlobalFunctionDefinition(ExistingDecl, nullptr); 4282 Func = GetGlobalValue(MangledName); 4283 } else { 4284 if (!ExistingDecl.getDecl()) 4285 ExistingDecl = GD.getWithMultiVersionIndex(Index); 4286 4287 Func = GetOrCreateLLVMFunction( 4288 MangledName, DeclTy, ExistingDecl, 4289 /*ForVTable=*/false, /*DontDefer=*/true, 4290 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition); 4291 } 4292 } 4293 4294 llvm::SmallVector<StringRef, 32> Features; 4295 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features); 4296 llvm::transform(Features, Features.begin(), 4297 [](StringRef Str) { return Str.substr(1); }); 4298 llvm::erase_if(Features, [&Target](StringRef Feat) { 4299 return !Target.validateCpuSupports(Feat); 4300 }); 4301 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features); 4302 ++Index; 4303 } 4304 4305 llvm::stable_sort( 4306 Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS, 4307 const CodeGenFunction::MultiVersionResolverOption &RHS) { 4308 return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) > 4309 llvm::X86::getCpuSupportsMask(RHS.Conditions.Features); 4310 }); 4311 4312 // If the list contains multiple 'default' versions, such as when it contains 4313 // 'pentium' and 'generic', don't emit the call to the generic one (since we 4314 // always run on at least a 'pentium'). We do this by deleting the 'least 4315 // advanced' (read, lowest mangling letter). 4316 while (Options.size() > 1 && 4317 llvm::all_of(llvm::X86::getCpuSupportsMask( 4318 (Options.end() - 2)->Conditions.Features), 4319 [](auto X) { return X == 0; })) { 4320 StringRef LHSName = (Options.end() - 2)->Function->getName(); 4321 StringRef RHSName = (Options.end() - 1)->Function->getName(); 4322 if (LHSName.compare(RHSName) < 0) 4323 Options.erase(Options.end() - 2); 4324 else 4325 Options.erase(Options.end() - 1); 4326 } 4327 4328 CodeGenFunction CGF(*this); 4329 CGF.EmitMultiVersionResolver(ResolverFunc, Options); 4330 4331 if (getTarget().supportsIFunc()) { 4332 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD); 4333 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD)); 4334 4335 // Fix up function declarations that were created for cpu_specific before 4336 // cpu_dispatch was known 4337 if (!isa<llvm::GlobalIFunc>(IFunc)) { 4338 assert(cast<llvm::Function>(IFunc)->isDeclaration()); 4339 auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc, 4340 &getModule()); 4341 GI->takeName(IFunc); 4342 IFunc->replaceAllUsesWith(GI); 4343 IFunc->eraseFromParent(); 4344 IFunc = GI; 4345 } 4346 4347 std::string AliasName = getMangledNameImpl( 4348 *this, GD, FD, /*OmitMultiVersionMangling=*/true); 4349 llvm::Constant *AliasFunc = GetGlobalValue(AliasName); 4350 if (!AliasFunc) { 4351 auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc, 4352 &getModule()); 4353 SetCommonAttributes(GD, GA); 4354 } 4355 } 4356 } 4357 4358 /// If a dispatcher for the specified mangled name is not in the module, create 4359 /// and return an llvm Function with the specified type. 4360 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) { 4361 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 4362 assert(FD && "Not a FunctionDecl?"); 4363 4364 std::string MangledName = 4365 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true); 4366 4367 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has 4368 // a separate resolver). 4369 std::string ResolverName = MangledName; 4370 if (getTarget().supportsIFunc()) { 4371 // In Aarch64, default versions of multiversioned functions are mangled to 4372 // their 'normal' assembly name. This deviates from other targets which 4373 // append a '.default' string. As a result we need to continue appending 4374 // .ifunc in Aarch64. 4375 // FIXME: Should Aarch64 mangling for 'default' multiversion function and 4376 // in turn ifunc function match that of other targets? 4377 if (!FD->isTargetClonesMultiVersion() || 4378 getTarget().getTriple().isAArch64()) 4379 ResolverName += ".ifunc"; 4380 } else if (FD->isTargetMultiVersion()) { 4381 ResolverName += ".resolver"; 4382 } 4383 4384 // If the resolver has already been created, just return it. 4385 if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName)) 4386 return ResolverGV; 4387 4388 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 4389 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI); 4390 4391 // The resolver needs to be created. For target and target_clones, defer 4392 // creation until the end of the TU. 4393 if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion()) 4394 MultiVersionFuncs.push_back(GD); 4395 4396 // For cpu_specific, don't create an ifunc yet because we don't know if the 4397 // cpu_dispatch will be emitted in this translation unit. 4398 if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) { 4399 llvm::Type *ResolverType = llvm::FunctionType::get( 4400 llvm::PointerType::get(DeclTy, 4401 getTypes().getTargetAddressSpace(FD->getType())), 4402 false); 4403 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 4404 MangledName + ".resolver", ResolverType, GlobalDecl{}, 4405 /*ForVTable=*/false); 4406 llvm::GlobalIFunc *GIF = 4407 llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD), 4408 "", Resolver, &getModule()); 4409 GIF->setName(ResolverName); 4410 SetCommonAttributes(FD, GIF); 4411 4412 return GIF; 4413 } 4414 4415 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 4416 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false); 4417 assert(isa<llvm::GlobalValue>(Resolver) && 4418 "Resolver should be created for the first time"); 4419 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver)); 4420 return Resolver; 4421 } 4422 4423 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the 4424 /// module, create and return an llvm Function with the specified type. If there 4425 /// is something in the module with the specified name, return it potentially 4426 /// bitcasted to the right type. 4427 /// 4428 /// If D is non-null, it specifies a decl that correspond to this. This is used 4429 /// to set the attributes on the function when it is first created. 4430 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction( 4431 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable, 4432 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs, 4433 ForDefinition_t IsForDefinition) { 4434 const Decl *D = GD.getDecl(); 4435 4436 // Any attempts to use a MultiVersion function should result in retrieving 4437 // the iFunc instead. Name Mangling will handle the rest of the changes. 4438 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) { 4439 // For the device mark the function as one that should be emitted. 4440 if (getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime && 4441 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() && 4442 !DontDefer && !IsForDefinition) { 4443 if (const FunctionDecl *FDDef = FD->getDefinition()) { 4444 GlobalDecl GDDef; 4445 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef)) 4446 GDDef = GlobalDecl(CD, GD.getCtorType()); 4447 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef)) 4448 GDDef = GlobalDecl(DD, GD.getDtorType()); 4449 else 4450 GDDef = GlobalDecl(FDDef); 4451 EmitGlobal(GDDef); 4452 } 4453 } 4454 4455 if (FD->isMultiVersion()) { 4456 UpdateMultiVersionNames(GD, FD, MangledName); 4457 if (!IsForDefinition) 4458 return GetOrCreateMultiVersionResolver(GD); 4459 } 4460 } 4461 4462 // Lookup the entry, lazily creating it if necessary. 4463 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 4464 if (Entry) { 4465 if (WeakRefReferences.erase(Entry)) { 4466 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D); 4467 if (FD && !FD->hasAttr<WeakAttr>()) 4468 Entry->setLinkage(llvm::Function::ExternalLinkage); 4469 } 4470 4471 // Handle dropped DLL attributes. 4472 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() && 4473 !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) { 4474 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 4475 setDSOLocal(Entry); 4476 } 4477 4478 // If there are two attempts to define the same mangled name, issue an 4479 // error. 4480 if (IsForDefinition && !Entry->isDeclaration()) { 4481 GlobalDecl OtherGD; 4482 // Check that GD is not yet in DiagnosedConflictingDefinitions is required 4483 // to make sure that we issue an error only once. 4484 if (lookupRepresentativeDecl(MangledName, OtherGD) && 4485 (GD.getCanonicalDecl().getDecl() != 4486 OtherGD.getCanonicalDecl().getDecl()) && 4487 DiagnosedConflictingDefinitions.insert(GD).second) { 4488 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) 4489 << MangledName; 4490 getDiags().Report(OtherGD.getDecl()->getLocation(), 4491 diag::note_previous_definition); 4492 } 4493 } 4494 4495 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) && 4496 (Entry->getValueType() == Ty)) { 4497 return Entry; 4498 } 4499 4500 // Make sure the result is of the correct type. 4501 // (If function is requested for a definition, we always need to create a new 4502 // function, not just return a bitcast.) 4503 if (!IsForDefinition) 4504 return Entry; 4505 } 4506 4507 // This function doesn't have a complete type (for example, the return 4508 // type is an incomplete struct). Use a fake type instead, and make 4509 // sure not to try to set attributes. 4510 bool IsIncompleteFunction = false; 4511 4512 llvm::FunctionType *FTy; 4513 if (isa<llvm::FunctionType>(Ty)) { 4514 FTy = cast<llvm::FunctionType>(Ty); 4515 } else { 4516 FTy = llvm::FunctionType::get(VoidTy, false); 4517 IsIncompleteFunction = true; 4518 } 4519 4520 llvm::Function *F = 4521 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage, 4522 Entry ? StringRef() : MangledName, &getModule()); 4523 4524 // Store the declaration associated with this function so it is potentially 4525 // updated by further declarations or definitions and emitted at the end. 4526 if (D && D->hasAttr<AnnotateAttr>()) 4527 DeferredAnnotations[MangledName] = cast<ValueDecl>(D); 4528 4529 // If we already created a function with the same mangled name (but different 4530 // type) before, take its name and add it to the list of functions to be 4531 // replaced with F at the end of CodeGen. 4532 // 4533 // This happens if there is a prototype for a function (e.g. "int f()") and 4534 // then a definition of a different type (e.g. "int f(int x)"). 4535 if (Entry) { 4536 F->takeName(Entry); 4537 4538 // This might be an implementation of a function without a prototype, in 4539 // which case, try to do special replacement of calls which match the new 4540 // prototype. The really key thing here is that we also potentially drop 4541 // arguments from the call site so as to make a direct call, which makes the 4542 // inliner happier and suppresses a number of optimizer warnings (!) about 4543 // dropping arguments. 4544 if (!Entry->use_empty()) { 4545 ReplaceUsesOfNonProtoTypeWithRealFunction(Entry, F); 4546 Entry->removeDeadConstantUsers(); 4547 } 4548 4549 addGlobalValReplacement(Entry, F); 4550 } 4551 4552 assert(F->getName() == MangledName && "name was uniqued!"); 4553 if (D) 4554 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk); 4555 if (ExtraAttrs.hasFnAttrs()) { 4556 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs()); 4557 F->addFnAttrs(B); 4558 } 4559 4560 if (!DontDefer) { 4561 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to 4562 // each other bottoming out with the base dtor. Therefore we emit non-base 4563 // dtors on usage, even if there is no dtor definition in the TU. 4564 if (isa_and_nonnull<CXXDestructorDecl>(D) && 4565 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D), 4566 GD.getDtorType())) 4567 addDeferredDeclToEmit(GD); 4568 4569 // This is the first use or definition of a mangled name. If there is a 4570 // deferred decl with this name, remember that we need to emit it at the end 4571 // of the file. 4572 auto DDI = DeferredDecls.find(MangledName); 4573 if (DDI != DeferredDecls.end()) { 4574 // Move the potentially referenced deferred decl to the 4575 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we 4576 // don't need it anymore). 4577 addDeferredDeclToEmit(DDI->second); 4578 DeferredDecls.erase(DDI); 4579 4580 // Otherwise, there are cases we have to worry about where we're 4581 // using a declaration for which we must emit a definition but where 4582 // we might not find a top-level definition: 4583 // - member functions defined inline in their classes 4584 // - friend functions defined inline in some class 4585 // - special member functions with implicit definitions 4586 // If we ever change our AST traversal to walk into class methods, 4587 // this will be unnecessary. 4588 // 4589 // We also don't emit a definition for a function if it's going to be an 4590 // entry in a vtable, unless it's already marked as used. 4591 } else if (getLangOpts().CPlusPlus && D) { 4592 // Look for a declaration that's lexically in a record. 4593 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD; 4594 FD = FD->getPreviousDecl()) { 4595 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { 4596 if (FD->doesThisDeclarationHaveABody()) { 4597 addDeferredDeclToEmit(GD.getWithDecl(FD)); 4598 break; 4599 } 4600 } 4601 } 4602 } 4603 } 4604 4605 // Make sure the result is of the requested type. 4606 if (!IsIncompleteFunction) { 4607 assert(F->getFunctionType() == Ty); 4608 return F; 4609 } 4610 4611 return F; 4612 } 4613 4614 /// GetAddrOfFunction - Return the address of the given function. If Ty is 4615 /// non-null, then this function will use the specified type if it has to 4616 /// create it (this occurs when we see a definition of the function). 4617 llvm::Constant * 4618 CodeGenModule::GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty, bool ForVTable, 4619 bool DontDefer, 4620 ForDefinition_t IsForDefinition) { 4621 // If there was no specific requested type, just convert it now. 4622 if (!Ty) { 4623 const auto *FD = cast<FunctionDecl>(GD.getDecl()); 4624 Ty = getTypes().ConvertType(FD->getType()); 4625 } 4626 4627 // Devirtualized destructor calls may come through here instead of via 4628 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead 4629 // of the complete destructor when necessary. 4630 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) { 4631 if (getTarget().getCXXABI().isMicrosoft() && 4632 GD.getDtorType() == Dtor_Complete && 4633 DD->getParent()->getNumVBases() == 0) 4634 GD = GlobalDecl(DD, Dtor_Base); 4635 } 4636 4637 StringRef MangledName = getMangledName(GD); 4638 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer, 4639 /*IsThunk=*/false, llvm::AttributeList(), 4640 IsForDefinition); 4641 // Returns kernel handle for HIP kernel stub function. 4642 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice && 4643 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) { 4644 auto *Handle = getCUDARuntime().getKernelHandle( 4645 cast<llvm::Function>(F->stripPointerCasts()), GD); 4646 if (IsForDefinition) 4647 return F; 4648 return Handle; 4649 } 4650 return F; 4651 } 4652 4653 llvm::Constant *CodeGenModule::GetFunctionStart(const ValueDecl *Decl) { 4654 llvm::GlobalValue *F = 4655 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts()); 4656 4657 return llvm::NoCFIValue::get(F); 4658 } 4659 4660 static const FunctionDecl * 4661 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) { 4662 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl(); 4663 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); 4664 4665 IdentifierInfo &CII = C.Idents.get(Name); 4666 for (const auto *Result : DC->lookup(&CII)) 4667 if (const auto *FD = dyn_cast<FunctionDecl>(Result)) 4668 return FD; 4669 4670 if (!C.getLangOpts().CPlusPlus) 4671 return nullptr; 4672 4673 // Demangle the premangled name from getTerminateFn() 4674 IdentifierInfo &CXXII = 4675 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ") 4676 ? C.Idents.get("terminate") 4677 : C.Idents.get(Name); 4678 4679 for (const auto &N : {"__cxxabiv1", "std"}) { 4680 IdentifierInfo &NS = C.Idents.get(N); 4681 for (const auto *Result : DC->lookup(&NS)) { 4682 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result); 4683 if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result)) 4684 for (const auto *Result : LSD->lookup(&NS)) 4685 if ((ND = dyn_cast<NamespaceDecl>(Result))) 4686 break; 4687 4688 if (ND) 4689 for (const auto *Result : ND->lookup(&CXXII)) 4690 if (const auto *FD = dyn_cast<FunctionDecl>(Result)) 4691 return FD; 4692 } 4693 } 4694 4695 return nullptr; 4696 } 4697 4698 /// CreateRuntimeFunction - Create a new runtime function with the specified 4699 /// type and name. 4700 llvm::FunctionCallee 4701 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, 4702 llvm::AttributeList ExtraAttrs, bool Local, 4703 bool AssumeConvergent) { 4704 if (AssumeConvergent) { 4705 ExtraAttrs = 4706 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent); 4707 } 4708 4709 llvm::Constant *C = 4710 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false, 4711 /*DontDefer=*/false, /*IsThunk=*/false, 4712 ExtraAttrs); 4713 4714 if (auto *F = dyn_cast<llvm::Function>(C)) { 4715 if (F->empty()) { 4716 F->setCallingConv(getRuntimeCC()); 4717 4718 // In Windows Itanium environments, try to mark runtime functions 4719 // dllimport. For Mingw and MSVC, don't. We don't really know if the user 4720 // will link their standard library statically or dynamically. Marking 4721 // functions imported when they are not imported can cause linker errors 4722 // and warnings. 4723 if (!Local && getTriple().isWindowsItaniumEnvironment() && 4724 !getCodeGenOpts().LTOVisibilityPublicStd) { 4725 const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); 4726 if (!FD || FD->hasAttr<DLLImportAttr>()) { 4727 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 4728 F->setLinkage(llvm::GlobalValue::ExternalLinkage); 4729 } 4730 } 4731 setDSOLocal(F); 4732 } 4733 } 4734 4735 return {FTy, C}; 4736 } 4737 4738 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, 4739 /// create and return an llvm GlobalVariable with the specified type and address 4740 /// space. If there is something in the module with the specified name, return 4741 /// it potentially bitcasted to the right type. 4742 /// 4743 /// If D is non-null, it specifies a decl that correspond to this. This is used 4744 /// to set the attributes on the global when it is first created. 4745 /// 4746 /// If IsForDefinition is true, it is guaranteed that an actual global with 4747 /// type Ty will be returned, not conversion of a variable with the same 4748 /// mangled name but some other type. 4749 llvm::Constant * 4750 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, 4751 LangAS AddrSpace, const VarDecl *D, 4752 ForDefinition_t IsForDefinition) { 4753 // Lookup the entry, lazily creating it if necessary. 4754 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 4755 unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace); 4756 if (Entry) { 4757 if (WeakRefReferences.erase(Entry)) { 4758 if (D && !D->hasAttr<WeakAttr>()) 4759 Entry->setLinkage(llvm::Function::ExternalLinkage); 4760 } 4761 4762 // Handle dropped DLL attributes. 4763 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() && 4764 !shouldMapVisibilityToDLLExport(D)) 4765 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); 4766 4767 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D) 4768 getOpenMPRuntime().registerTargetGlobalVariable(D, Entry); 4769 4770 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS) 4771 return Entry; 4772 4773 // If there are two attempts to define the same mangled name, issue an 4774 // error. 4775 if (IsForDefinition && !Entry->isDeclaration()) { 4776 GlobalDecl OtherGD; 4777 const VarDecl *OtherD; 4778 4779 // Check that D is not yet in DiagnosedConflictingDefinitions is required 4780 // to make sure that we issue an error only once. 4781 if (D && lookupRepresentativeDecl(MangledName, OtherGD) && 4782 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) && 4783 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) && 4784 OtherD->hasInit() && 4785 DiagnosedConflictingDefinitions.insert(D).second) { 4786 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name) 4787 << MangledName; 4788 getDiags().Report(OtherGD.getDecl()->getLocation(), 4789 diag::note_previous_definition); 4790 } 4791 } 4792 4793 // Make sure the result is of the correct type. 4794 if (Entry->getType()->getAddressSpace() != TargetAS) 4795 return llvm::ConstantExpr::getAddrSpaceCast( 4796 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS)); 4797 4798 // (If global is requested for a definition, we always need to create a new 4799 // global, not just return a bitcast.) 4800 if (!IsForDefinition) 4801 return Entry; 4802 } 4803 4804 auto DAddrSpace = GetGlobalVarAddressSpace(D); 4805 4806 auto *GV = new llvm::GlobalVariable( 4807 getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr, 4808 MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal, 4809 getContext().getTargetAddressSpace(DAddrSpace)); 4810 4811 // If we already created a global with the same mangled name (but different 4812 // type) before, take its name and remove it from its parent. 4813 if (Entry) { 4814 GV->takeName(Entry); 4815 4816 if (!Entry->use_empty()) { 4817 Entry->replaceAllUsesWith(GV); 4818 } 4819 4820 Entry->eraseFromParent(); 4821 } 4822 4823 // This is the first use or definition of a mangled name. If there is a 4824 // deferred decl with this name, remember that we need to emit it at the end 4825 // of the file. 4826 auto DDI = DeferredDecls.find(MangledName); 4827 if (DDI != DeferredDecls.end()) { 4828 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit 4829 // list, and remove it from DeferredDecls (since we don't need it anymore). 4830 addDeferredDeclToEmit(DDI->second); 4831 DeferredDecls.erase(DDI); 4832 } 4833 4834 // Handle things which are present even on external declarations. 4835 if (D) { 4836 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd) 4837 getOpenMPRuntime().registerTargetGlobalVariable(D, GV); 4838 4839 // FIXME: This code is overly simple and should be merged with other global 4840 // handling. 4841 GV->setConstant(D->getType().isConstantStorage(getContext(), false, false)); 4842 4843 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign()); 4844 4845 setLinkageForGV(GV, D); 4846 4847 if (D->getTLSKind()) { 4848 if (D->getTLSKind() == VarDecl::TLS_Dynamic) 4849 CXXThreadLocals.push_back(D); 4850 setTLSMode(GV, *D); 4851 } 4852 4853 setGVProperties(GV, D); 4854 4855 // If required by the ABI, treat declarations of static data members with 4856 // inline initializers as definitions. 4857 if (getContext().isMSStaticDataMemberInlineDefinition(D)) { 4858 EmitGlobalVarDefinition(D); 4859 } 4860 4861 // Emit section information for extern variables. 4862 if (D->hasExternalStorage()) { 4863 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) 4864 GV->setSection(SA->getName()); 4865 } 4866 4867 // Handle XCore specific ABI requirements. 4868 if (getTriple().getArch() == llvm::Triple::xcore && 4869 D->getLanguageLinkage() == CLanguageLinkage && 4870 D->getType().isConstant(Context) && 4871 isExternallyVisible(D->getLinkageAndVisibility().getLinkage())) 4872 GV->setSection(".cp.rodata"); 4873 4874 // Handle code model attribute 4875 if (const auto *CMA = D->getAttr<CodeModelAttr>()) 4876 GV->setCodeModel(CMA->getModel()); 4877 4878 // Check if we a have a const declaration with an initializer, we may be 4879 // able to emit it as available_externally to expose it's value to the 4880 // optimizer. 4881 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() && 4882 D->getType().isConstQualified() && !GV->hasInitializer() && 4883 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) { 4884 const auto *Record = 4885 Context.getBaseElementType(D->getType())->getAsCXXRecordDecl(); 4886 bool HasMutableFields = Record && Record->hasMutableFields(); 4887 if (!HasMutableFields) { 4888 const VarDecl *InitDecl; 4889 const Expr *InitExpr = D->getAnyInitializer(InitDecl); 4890 if (InitExpr) { 4891 ConstantEmitter emitter(*this); 4892 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl); 4893 if (Init) { 4894 auto *InitType = Init->getType(); 4895 if (GV->getValueType() != InitType) { 4896 // The type of the initializer does not match the definition. 4897 // This happens when an initializer has a different type from 4898 // the type of the global (because of padding at the end of a 4899 // structure for instance). 4900 GV->setName(StringRef()); 4901 // Make a new global with the correct type, this is now guaranteed 4902 // to work. 4903 auto *NewGV = cast<llvm::GlobalVariable>( 4904 GetAddrOfGlobalVar(D, InitType, IsForDefinition) 4905 ->stripPointerCasts()); 4906 4907 // Erase the old global, since it is no longer used. 4908 GV->eraseFromParent(); 4909 GV = NewGV; 4910 } else { 4911 GV->setInitializer(Init); 4912 GV->setConstant(true); 4913 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage); 4914 } 4915 emitter.finalize(GV); 4916 } 4917 } 4918 } 4919 } 4920 } 4921 4922 if (D && 4923 D->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly) { 4924 getTargetCodeGenInfo().setTargetAttributes(D, GV, *this); 4925 // External HIP managed variables needed to be recorded for transformation 4926 // in both device and host compilations. 4927 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() && 4928 D->hasExternalStorage()) 4929 getCUDARuntime().handleVarRegistration(D, *GV); 4930 } 4931 4932 if (D) 4933 SanitizerMD->reportGlobal(GV, *D); 4934 4935 LangAS ExpectedAS = 4936 D ? D->getType().getAddressSpace() 4937 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); 4938 assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS); 4939 if (DAddrSpace != ExpectedAS) { 4940 return getTargetCodeGenInfo().performAddrSpaceCast( 4941 *this, GV, DAddrSpace, ExpectedAS, 4942 llvm::PointerType::get(getLLVMContext(), TargetAS)); 4943 } 4944 4945 return GV; 4946 } 4947 4948 llvm::Constant * 4949 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition) { 4950 const Decl *D = GD.getDecl(); 4951 4952 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D)) 4953 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr, 4954 /*DontDefer=*/false, IsForDefinition); 4955 4956 if (isa<CXXMethodDecl>(D)) { 4957 auto FInfo = 4958 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D)); 4959 auto Ty = getTypes().GetFunctionType(*FInfo); 4960 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, 4961 IsForDefinition); 4962 } 4963 4964 if (isa<FunctionDecl>(D)) { 4965 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 4966 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 4967 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false, 4968 IsForDefinition); 4969 } 4970 4971 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition); 4972 } 4973 4974 llvm::GlobalVariable *CodeGenModule::CreateOrReplaceCXXRuntimeVariable( 4975 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, 4976 llvm::Align Alignment) { 4977 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name); 4978 llvm::GlobalVariable *OldGV = nullptr; 4979 4980 if (GV) { 4981 // Check if the variable has the right type. 4982 if (GV->getValueType() == Ty) 4983 return GV; 4984 4985 // Because C++ name mangling, the only way we can end up with an already 4986 // existing global with the same name is if it has been declared extern "C". 4987 assert(GV->isDeclaration() && "Declaration has wrong type!"); 4988 OldGV = GV; 4989 } 4990 4991 // Create a new variable. 4992 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true, 4993 Linkage, nullptr, Name); 4994 4995 if (OldGV) { 4996 // Replace occurrences of the old variable if needed. 4997 GV->takeName(OldGV); 4998 4999 if (!OldGV->use_empty()) { 5000 OldGV->replaceAllUsesWith(GV); 5001 } 5002 5003 OldGV->eraseFromParent(); 5004 } 5005 5006 if (supportsCOMDAT() && GV->isWeakForLinker() && 5007 !GV->hasAvailableExternallyLinkage()) 5008 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 5009 5010 GV->setAlignment(Alignment); 5011 5012 return GV; 5013 } 5014 5015 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the 5016 /// given global variable. If Ty is non-null and if the global doesn't exist, 5017 /// then it will be created with the specified type instead of whatever the 5018 /// normal requested type would be. If IsForDefinition is true, it is guaranteed 5019 /// that an actual global with type Ty will be returned, not conversion of a 5020 /// variable with the same mangled name but some other type. 5021 llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, 5022 llvm::Type *Ty, 5023 ForDefinition_t IsForDefinition) { 5024 assert(D->hasGlobalStorage() && "Not a global variable"); 5025 QualType ASTTy = D->getType(); 5026 if (!Ty) 5027 Ty = getTypes().ConvertTypeForMem(ASTTy); 5028 5029 StringRef MangledName = getMangledName(D); 5030 return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D, 5031 IsForDefinition); 5032 } 5033 5034 /// CreateRuntimeVariable - Create a new runtime global variable with the 5035 /// specified type and name. 5036 llvm::Constant * 5037 CodeGenModule::CreateRuntimeVariable(llvm::Type *Ty, 5038 StringRef Name) { 5039 LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global 5040 : LangAS::Default; 5041 auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr); 5042 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts())); 5043 return Ret; 5044 } 5045 5046 void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { 5047 assert(!D->getInit() && "Cannot emit definite definitions here!"); 5048 5049 StringRef MangledName = getMangledName(D); 5050 llvm::GlobalValue *GV = GetGlobalValue(MangledName); 5051 5052 // We already have a definition, not declaration, with the same mangled name. 5053 // Emitting of declaration is not required (and actually overwrites emitted 5054 // definition). 5055 if (GV && !GV->isDeclaration()) 5056 return; 5057 5058 // If we have not seen a reference to this variable yet, place it into the 5059 // deferred declarations table to be emitted if needed later. 5060 if (!MustBeEmitted(D) && !GV) { 5061 DeferredDecls[MangledName] = D; 5062 return; 5063 } 5064 5065 // The tentative definition is the only definition. 5066 EmitGlobalVarDefinition(D); 5067 } 5068 5069 void CodeGenModule::EmitExternalDeclaration(const VarDecl *D) { 5070 EmitExternalVarDeclaration(D); 5071 } 5072 5073 CharUnits CodeGenModule::GetTargetTypeStoreSize(llvm::Type *Ty) const { 5074 return Context.toCharUnitsFromBits( 5075 getDataLayout().getTypeStoreSizeInBits(Ty)); 5076 } 5077 5078 LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) { 5079 if (LangOpts.OpenCL) { 5080 LangAS AS = D ? D->getType().getAddressSpace() : LangAS::opencl_global; 5081 assert(AS == LangAS::opencl_global || 5082 AS == LangAS::opencl_global_device || 5083 AS == LangAS::opencl_global_host || 5084 AS == LangAS::opencl_constant || 5085 AS == LangAS::opencl_local || 5086 AS >= LangAS::FirstTargetAddressSpace); 5087 return AS; 5088 } 5089 5090 if (LangOpts.SYCLIsDevice && 5091 (!D || D->getType().getAddressSpace() == LangAS::Default)) 5092 return LangAS::sycl_global; 5093 5094 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) { 5095 if (D) { 5096 if (D->hasAttr<CUDAConstantAttr>()) 5097 return LangAS::cuda_constant; 5098 if (D->hasAttr<CUDASharedAttr>()) 5099 return LangAS::cuda_shared; 5100 if (D->hasAttr<CUDADeviceAttr>()) 5101 return LangAS::cuda_device; 5102 if (D->getType().isConstQualified()) 5103 return LangAS::cuda_constant; 5104 } 5105 return LangAS::cuda_device; 5106 } 5107 5108 if (LangOpts.OpenMP) { 5109 LangAS AS; 5110 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS)) 5111 return AS; 5112 } 5113 return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D); 5114 } 5115 5116 LangAS CodeGenModule::GetGlobalConstantAddressSpace() const { 5117 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. 5118 if (LangOpts.OpenCL) 5119 return LangAS::opencl_constant; 5120 if (LangOpts.SYCLIsDevice) 5121 return LangAS::sycl_global; 5122 if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV()) 5123 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V) 5124 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up 5125 // with OpVariable instructions with Generic storage class which is not 5126 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V 5127 // UniformConstant storage class is not viable as pointers to it may not be 5128 // casted to Generic pointers which are used to model HIP's "flat" pointers. 5129 return LangAS::cuda_device; 5130 if (auto AS = getTarget().getConstantAddressSpace()) 5131 return *AS; 5132 return LangAS::Default; 5133 } 5134 5135 // In address space agnostic languages, string literals are in default address 5136 // space in AST. However, certain targets (e.g. amdgcn) request them to be 5137 // emitted in constant address space in LLVM IR. To be consistent with other 5138 // parts of AST, string literal global variables in constant address space 5139 // need to be casted to default address space before being put into address 5140 // map and referenced by other part of CodeGen. 5141 // In OpenCL, string literals are in constant address space in AST, therefore 5142 // they should not be casted to default address space. 5143 static llvm::Constant * 5144 castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, 5145 llvm::GlobalVariable *GV) { 5146 llvm::Constant *Cast = GV; 5147 if (!CGM.getLangOpts().OpenCL) { 5148 auto AS = CGM.GetGlobalConstantAddressSpace(); 5149 if (AS != LangAS::Default) 5150 Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( 5151 CGM, GV, AS, LangAS::Default, 5152 llvm::PointerType::get( 5153 CGM.getLLVMContext(), 5154 CGM.getContext().getTargetAddressSpace(LangAS::Default))); 5155 } 5156 return Cast; 5157 } 5158 5159 template<typename SomeDecl> 5160 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl *D, 5161 llvm::GlobalValue *GV) { 5162 if (!getLangOpts().CPlusPlus) 5163 return; 5164 5165 // Must have 'used' attribute, or else inline assembly can't rely on 5166 // the name existing. 5167 if (!D->template hasAttr<UsedAttr>()) 5168 return; 5169 5170 // Must have internal linkage and an ordinary name. 5171 if (!D->getIdentifier() || D->getFormalLinkage() != Linkage::Internal) 5172 return; 5173 5174 // Must be in an extern "C" context. Entities declared directly within 5175 // a record are not extern "C" even if the record is in such a context. 5176 const SomeDecl *First = D->getFirstDecl(); 5177 if (First->getDeclContext()->isRecord() || !First->isInExternCContext()) 5178 return; 5179 5180 // OK, this is an internal linkage entity inside an extern "C" linkage 5181 // specification. Make a note of that so we can give it the "expected" 5182 // mangled name if nothing else is using that name. 5183 std::pair<StaticExternCMap::iterator, bool> R = 5184 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV)); 5185 5186 // If we have multiple internal linkage entities with the same name 5187 // in extern "C" regions, none of them gets that name. 5188 if (!R.second) 5189 R.first->second = nullptr; 5190 } 5191 5192 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) { 5193 if (!CGM.supportsCOMDAT()) 5194 return false; 5195 5196 if (D.hasAttr<SelectAnyAttr>()) 5197 return true; 5198 5199 GVALinkage Linkage; 5200 if (auto *VD = dyn_cast<VarDecl>(&D)) 5201 Linkage = CGM.getContext().GetGVALinkageForVariable(VD); 5202 else 5203 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D)); 5204 5205 switch (Linkage) { 5206 case GVA_Internal: 5207 case GVA_AvailableExternally: 5208 case GVA_StrongExternal: 5209 return false; 5210 case GVA_DiscardableODR: 5211 case GVA_StrongODR: 5212 return true; 5213 } 5214 llvm_unreachable("No such linkage"); 5215 } 5216 5217 bool CodeGenModule::supportsCOMDAT() const { 5218 return getTriple().supportsCOMDAT(); 5219 } 5220 5221 void CodeGenModule::maybeSetTrivialComdat(const Decl &D, 5222 llvm::GlobalObject &GO) { 5223 if (!shouldBeInCOMDAT(*this, D)) 5224 return; 5225 GO.setComdat(TheModule.getOrInsertComdat(GO.getName())); 5226 } 5227 5228 /// Pass IsTentative as true if you want to create a tentative definition. 5229 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D, 5230 bool IsTentative) { 5231 // OpenCL global variables of sampler type are translated to function calls, 5232 // therefore no need to be translated. 5233 QualType ASTTy = D->getType(); 5234 if (getLangOpts().OpenCL && ASTTy->isSamplerT()) 5235 return; 5236 5237 // If this is OpenMP device, check if it is legal to emit this global 5238 // normally. 5239 if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime && 5240 OpenMPRuntime->emitTargetGlobalVariable(D)) 5241 return; 5242 5243 llvm::TrackingVH<llvm::Constant> Init; 5244 bool NeedsGlobalCtor = false; 5245 // Whether the definition of the variable is available externally. 5246 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable 5247 // since this is the job for its original source. 5248 bool IsDefinitionAvailableExternally = 5249 getContext().GetGVALinkageForVariable(D) == GVA_AvailableExternally; 5250 bool NeedsGlobalDtor = 5251 !IsDefinitionAvailableExternally && 5252 D->needsDestruction(getContext()) == QualType::DK_cxx_destructor; 5253 5254 const VarDecl *InitDecl; 5255 const Expr *InitExpr = D->getAnyInitializer(InitDecl); 5256 5257 std::optional<ConstantEmitter> emitter; 5258 5259 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization 5260 // as part of their declaration." Sema has already checked for 5261 // error cases, so we just need to set Init to UndefValue. 5262 bool IsCUDASharedVar = 5263 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>(); 5264 // Shadows of initialized device-side global variables are also left 5265 // undefined. 5266 // Managed Variables should be initialized on both host side and device side. 5267 bool IsCUDAShadowVar = 5268 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() && 5269 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() || 5270 D->hasAttr<CUDASharedAttr>()); 5271 bool IsCUDADeviceShadowVar = 5272 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() && 5273 (D->getType()->isCUDADeviceBuiltinSurfaceType() || 5274 D->getType()->isCUDADeviceBuiltinTextureType()); 5275 if (getLangOpts().CUDA && 5276 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar)) 5277 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy)); 5278 else if (D->hasAttr<LoaderUninitializedAttr>()) 5279 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy)); 5280 else if (!InitExpr) { 5281 // This is a tentative definition; tentative definitions are 5282 // implicitly initialized with { 0 }. 5283 // 5284 // Note that tentative definitions are only emitted at the end of 5285 // a translation unit, so they should never have incomplete 5286 // type. In addition, EmitTentativeDefinition makes sure that we 5287 // never attempt to emit a tentative definition if a real one 5288 // exists. A use may still exists, however, so we still may need 5289 // to do a RAUW. 5290 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type"); 5291 Init = EmitNullConstant(D->getType()); 5292 } else { 5293 initializedGlobalDecl = GlobalDecl(D); 5294 emitter.emplace(*this); 5295 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl); 5296 if (!Initializer) { 5297 QualType T = InitExpr->getType(); 5298 if (D->getType()->isReferenceType()) 5299 T = D->getType(); 5300 5301 if (getLangOpts().CPlusPlus) { 5302 if (InitDecl->hasFlexibleArrayInit(getContext())) 5303 ErrorUnsupported(D, "flexible array initializer"); 5304 Init = EmitNullConstant(T); 5305 5306 if (!IsDefinitionAvailableExternally) 5307 NeedsGlobalCtor = true; 5308 } else { 5309 ErrorUnsupported(D, "static initializer"); 5310 Init = llvm::UndefValue::get(getTypes().ConvertType(T)); 5311 } 5312 } else { 5313 Init = Initializer; 5314 // We don't need an initializer, so remove the entry for the delayed 5315 // initializer position (just in case this entry was delayed) if we 5316 // also don't need to register a destructor. 5317 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor) 5318 DelayedCXXInitPosition.erase(D); 5319 5320 #ifndef NDEBUG 5321 CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) + 5322 InitDecl->getFlexibleArrayInitChars(getContext()); 5323 CharUnits CstSize = CharUnits::fromQuantity( 5324 getDataLayout().getTypeAllocSize(Init->getType())); 5325 assert(VarSize == CstSize && "Emitted constant has unexpected size"); 5326 #endif 5327 } 5328 } 5329 5330 llvm::Type* InitType = Init->getType(); 5331 llvm::Constant *Entry = 5332 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)); 5333 5334 // Strip off pointer casts if we got them. 5335 Entry = Entry->stripPointerCasts(); 5336 5337 // Entry is now either a Function or GlobalVariable. 5338 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry); 5339 5340 // We have a definition after a declaration with the wrong type. 5341 // We must make a new GlobalVariable* and update everything that used OldGV 5342 // (a declaration or tentative definition) with the new GlobalVariable* 5343 // (which will be a definition). 5344 // 5345 // This happens if there is a prototype for a global (e.g. 5346 // "extern int x[];") and then a definition of a different type (e.g. 5347 // "int x[10];"). This also happens when an initializer has a different type 5348 // from the type of the global (this happens with unions). 5349 if (!GV || GV->getValueType() != InitType || 5350 GV->getType()->getAddressSpace() != 5351 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) { 5352 5353 // Move the old entry aside so that we'll create a new one. 5354 Entry->setName(StringRef()); 5355 5356 // Make a new global with the correct type, this is now guaranteed to work. 5357 GV = cast<llvm::GlobalVariable>( 5358 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)) 5359 ->stripPointerCasts()); 5360 5361 // Replace all uses of the old global with the new global 5362 llvm::Constant *NewPtrForOldDecl = 5363 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, 5364 Entry->getType()); 5365 Entry->replaceAllUsesWith(NewPtrForOldDecl); 5366 5367 // Erase the old global, since it is no longer used. 5368 cast<llvm::GlobalValue>(Entry)->eraseFromParent(); 5369 } 5370 5371 MaybeHandleStaticInExternC(D, GV); 5372 5373 if (D->hasAttr<AnnotateAttr>()) 5374 AddGlobalAnnotations(D, GV); 5375 5376 // Set the llvm linkage type as appropriate. 5377 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(D); 5378 5379 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on 5380 // the device. [...]" 5381 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with 5382 // __device__, declares a variable that: [...] 5383 // Is accessible from all the threads within the grid and from the host 5384 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize() 5385 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())." 5386 if (LangOpts.CUDA) { 5387 if (LangOpts.CUDAIsDevice) { 5388 if (Linkage != llvm::GlobalValue::InternalLinkage && 5389 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || 5390 D->getType()->isCUDADeviceBuiltinSurfaceType() || 5391 D->getType()->isCUDADeviceBuiltinTextureType())) 5392 GV->setExternallyInitialized(true); 5393 } else { 5394 getCUDARuntime().internalizeDeviceSideVar(D, Linkage); 5395 } 5396 getCUDARuntime().handleVarRegistration(D, *GV); 5397 } 5398 5399 GV->setInitializer(Init); 5400 if (emitter) 5401 emitter->finalize(GV); 5402 5403 // If it is safe to mark the global 'constant', do so now. 5404 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor && 5405 D->getType().isConstantStorage(getContext(), true, true)); 5406 5407 // If it is in a read-only section, mark it 'constant'. 5408 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) { 5409 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()]; 5410 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0) 5411 GV->setConstant(true); 5412 } 5413 5414 CharUnits AlignVal = getContext().getDeclAlign(D); 5415 // Check for alignment specifed in an 'omp allocate' directive. 5416 if (std::optional<CharUnits> AlignValFromAllocate = 5417 getOMPAllocateAlignment(D)) 5418 AlignVal = *AlignValFromAllocate; 5419 GV->setAlignment(AlignVal.getAsAlign()); 5420 5421 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper 5422 // function is only defined alongside the variable, not also alongside 5423 // callers. Normally, all accesses to a thread_local go through the 5424 // thread-wrapper in order to ensure initialization has occurred, underlying 5425 // variable will never be used other than the thread-wrapper, so it can be 5426 // converted to internal linkage. 5427 // 5428 // However, if the variable has the 'constinit' attribute, it _can_ be 5429 // referenced directly, without calling the thread-wrapper, so the linkage 5430 // must not be changed. 5431 // 5432 // Additionally, if the variable isn't plain external linkage, e.g. if it's 5433 // weak or linkonce, the de-duplication semantics are important to preserve, 5434 // so we don't change the linkage. 5435 if (D->getTLSKind() == VarDecl::TLS_Dynamic && 5436 Linkage == llvm::GlobalValue::ExternalLinkage && 5437 Context.getTargetInfo().getTriple().isOSDarwin() && 5438 !D->hasAttr<ConstInitAttr>()) 5439 Linkage = llvm::GlobalValue::InternalLinkage; 5440 5441 GV->setLinkage(Linkage); 5442 if (D->hasAttr<DLLImportAttr>()) 5443 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass); 5444 else if (D->hasAttr<DLLExportAttr>()) 5445 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass); 5446 else 5447 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); 5448 5449 if (Linkage == llvm::GlobalVariable::CommonLinkage) { 5450 // common vars aren't constant even if declared const. 5451 GV->setConstant(false); 5452 // Tentative definition of global variables may be initialized with 5453 // non-zero null pointers. In this case they should have weak linkage 5454 // since common linkage must have zero initializer and must not have 5455 // explicit section therefore cannot have non-zero initial value. 5456 if (!GV->getInitializer()->isNullValue()) 5457 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage); 5458 } 5459 5460 setNonAliasAttributes(D, GV); 5461 5462 if (D->getTLSKind() && !GV->isThreadLocal()) { 5463 if (D->getTLSKind() == VarDecl::TLS_Dynamic) 5464 CXXThreadLocals.push_back(D); 5465 setTLSMode(GV, *D); 5466 } 5467 5468 maybeSetTrivialComdat(*D, *GV); 5469 5470 // Emit the initializer function if necessary. 5471 if (NeedsGlobalCtor || NeedsGlobalDtor) 5472 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor); 5473 5474 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor); 5475 5476 // Emit global variable debug information. 5477 if (CGDebugInfo *DI = getModuleDebugInfo()) 5478 if (getCodeGenOpts().hasReducedDebugInfo()) 5479 DI->EmitGlobalVariable(GV, D); 5480 } 5481 5482 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl *D) { 5483 if (CGDebugInfo *DI = getModuleDebugInfo()) 5484 if (getCodeGenOpts().hasReducedDebugInfo()) { 5485 QualType ASTTy = D->getType(); 5486 llvm::Type *Ty = getTypes().ConvertTypeForMem(D->getType()); 5487 llvm::Constant *GV = 5488 GetOrCreateLLVMGlobal(D->getName(), Ty, ASTTy.getAddressSpace(), D); 5489 DI->EmitExternalVariable( 5490 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D); 5491 } 5492 } 5493 5494 static bool isVarDeclStrongDefinition(const ASTContext &Context, 5495 CodeGenModule &CGM, const VarDecl *D, 5496 bool NoCommon) { 5497 // Don't give variables common linkage if -fno-common was specified unless it 5498 // was overridden by a NoCommon attribute. 5499 if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>()) 5500 return true; 5501 5502 // C11 6.9.2/2: 5503 // A declaration of an identifier for an object that has file scope without 5504 // an initializer, and without a storage-class specifier or with the 5505 // storage-class specifier static, constitutes a tentative definition. 5506 if (D->getInit() || D->hasExternalStorage()) 5507 return true; 5508 5509 // A variable cannot be both common and exist in a section. 5510 if (D->hasAttr<SectionAttr>()) 5511 return true; 5512 5513 // A variable cannot be both common and exist in a section. 5514 // We don't try to determine which is the right section in the front-end. 5515 // If no specialized section name is applicable, it will resort to default. 5516 if (D->hasAttr<PragmaClangBSSSectionAttr>() || 5517 D->hasAttr<PragmaClangDataSectionAttr>() || 5518 D->hasAttr<PragmaClangRelroSectionAttr>() || 5519 D->hasAttr<PragmaClangRodataSectionAttr>()) 5520 return true; 5521 5522 // Thread local vars aren't considered common linkage. 5523 if (D->getTLSKind()) 5524 return true; 5525 5526 // Tentative definitions marked with WeakImportAttr are true definitions. 5527 if (D->hasAttr<WeakImportAttr>()) 5528 return true; 5529 5530 // A variable cannot be both common and exist in a comdat. 5531 if (shouldBeInCOMDAT(CGM, *D)) 5532 return true; 5533 5534 // Declarations with a required alignment do not have common linkage in MSVC 5535 // mode. 5536 if (Context.getTargetInfo().getCXXABI().isMicrosoft()) { 5537 if (D->hasAttr<AlignedAttr>()) 5538 return true; 5539 QualType VarType = D->getType(); 5540 if (Context.isAlignmentRequired(VarType)) 5541 return true; 5542 5543 if (const auto *RT = VarType->getAs<RecordType>()) { 5544 const RecordDecl *RD = RT->getDecl(); 5545 for (const FieldDecl *FD : RD->fields()) { 5546 if (FD->isBitField()) 5547 continue; 5548 if (FD->hasAttr<AlignedAttr>()) 5549 return true; 5550 if (Context.isAlignmentRequired(FD->getType())) 5551 return true; 5552 } 5553 } 5554 } 5555 5556 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for 5557 // common symbols, so symbols with greater alignment requirements cannot be 5558 // common. 5559 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two 5560 // alignments for common symbols via the aligncomm directive, so this 5561 // restriction only applies to MSVC environments. 5562 if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() && 5563 Context.getTypeAlignIfKnown(D->getType()) > 5564 Context.toBits(CharUnits::fromQuantity(32))) 5565 return true; 5566 5567 return false; 5568 } 5569 5570 llvm::GlobalValue::LinkageTypes 5571 CodeGenModule::getLLVMLinkageForDeclarator(const DeclaratorDecl *D, 5572 GVALinkage Linkage) { 5573 if (Linkage == GVA_Internal) 5574 return llvm::Function::InternalLinkage; 5575 5576 if (D->hasAttr<WeakAttr>()) 5577 return llvm::GlobalVariable::WeakAnyLinkage; 5578 5579 if (const auto *FD = D->getAsFunction()) 5580 if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally) 5581 return llvm::GlobalVariable::LinkOnceAnyLinkage; 5582 5583 // We are guaranteed to have a strong definition somewhere else, 5584 // so we can use available_externally linkage. 5585 if (Linkage == GVA_AvailableExternally) 5586 return llvm::GlobalValue::AvailableExternallyLinkage; 5587 5588 // Note that Apple's kernel linker doesn't support symbol 5589 // coalescing, so we need to avoid linkonce and weak linkages there. 5590 // Normally, this means we just map to internal, but for explicit 5591 // instantiations we'll map to external. 5592 5593 // In C++, the compiler has to emit a definition in every translation unit 5594 // that references the function. We should use linkonce_odr because 5595 // a) if all references in this translation unit are optimized away, we 5596 // don't need to codegen it. b) if the function persists, it needs to be 5597 // merged with other definitions. c) C++ has the ODR, so we know the 5598 // definition is dependable. 5599 if (Linkage == GVA_DiscardableODR) 5600 return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage 5601 : llvm::Function::InternalLinkage; 5602 5603 // An explicit instantiation of a template has weak linkage, since 5604 // explicit instantiations can occur in multiple translation units 5605 // and must all be equivalent. However, we are not allowed to 5606 // throw away these explicit instantiations. 5607 // 5608 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU, 5609 // so say that CUDA templates are either external (for kernels) or internal. 5610 // This lets llvm perform aggressive inter-procedural optimizations. For 5611 // -fgpu-rdc case, device function calls across multiple TU's are allowed, 5612 // therefore we need to follow the normal linkage paradigm. 5613 if (Linkage == GVA_StrongODR) { 5614 if (getLangOpts().AppleKext) 5615 return llvm::Function::ExternalLinkage; 5616 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice && 5617 !getLangOpts().GPURelocatableDeviceCode) 5618 return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage 5619 : llvm::Function::InternalLinkage; 5620 return llvm::Function::WeakODRLinkage; 5621 } 5622 5623 // C++ doesn't have tentative definitions and thus cannot have common 5624 // linkage. 5625 if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) && 5626 !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D), 5627 CodeGenOpts.NoCommon)) 5628 return llvm::GlobalVariable::CommonLinkage; 5629 5630 // selectany symbols are externally visible, so use weak instead of 5631 // linkonce. MSVC optimizes away references to const selectany globals, so 5632 // all definitions should be the same and ODR linkage should be used. 5633 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx 5634 if (D->hasAttr<SelectAnyAttr>()) 5635 return llvm::GlobalVariable::WeakODRLinkage; 5636 5637 // Otherwise, we have strong external linkage. 5638 assert(Linkage == GVA_StrongExternal); 5639 return llvm::GlobalVariable::ExternalLinkage; 5640 } 5641 5642 llvm::GlobalValue::LinkageTypes 5643 CodeGenModule::getLLVMLinkageVarDefinition(const VarDecl *VD) { 5644 GVALinkage Linkage = getContext().GetGVALinkageForVariable(VD); 5645 return getLLVMLinkageForDeclarator(VD, Linkage); 5646 } 5647 5648 /// Replace the uses of a function that was declared with a non-proto type. 5649 /// We want to silently drop extra arguments from call sites 5650 static void replaceUsesOfNonProtoConstant(llvm::Constant *old, 5651 llvm::Function *newFn) { 5652 // Fast path. 5653 if (old->use_empty()) return; 5654 5655 llvm::Type *newRetTy = newFn->getReturnType(); 5656 SmallVector<llvm::Value*, 4> newArgs; 5657 5658 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end(); 5659 ui != ue; ) { 5660 llvm::Value::use_iterator use = ui++; // Increment before the use is erased. 5661 llvm::User *user = use->getUser(); 5662 5663 // Recognize and replace uses of bitcasts. Most calls to 5664 // unprototyped functions will use bitcasts. 5665 if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) { 5666 if (bitcast->getOpcode() == llvm::Instruction::BitCast) 5667 replaceUsesOfNonProtoConstant(bitcast, newFn); 5668 continue; 5669 } 5670 5671 // Recognize calls to the function. 5672 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user); 5673 if (!callSite) continue; 5674 if (!callSite->isCallee(&*use)) 5675 continue; 5676 5677 // If the return types don't match exactly, then we can't 5678 // transform this call unless it's dead. 5679 if (callSite->getType() != newRetTy && !callSite->use_empty()) 5680 continue; 5681 5682 // Get the call site's attribute list. 5683 SmallVector<llvm::AttributeSet, 8> newArgAttrs; 5684 llvm::AttributeList oldAttrs = callSite->getAttributes(); 5685 5686 // If the function was passed too few arguments, don't transform. 5687 unsigned newNumArgs = newFn->arg_size(); 5688 if (callSite->arg_size() < newNumArgs) 5689 continue; 5690 5691 // If extra arguments were passed, we silently drop them. 5692 // If any of the types mismatch, we don't transform. 5693 unsigned argNo = 0; 5694 bool dontTransform = false; 5695 for (llvm::Argument &A : newFn->args()) { 5696 if (callSite->getArgOperand(argNo)->getType() != A.getType()) { 5697 dontTransform = true; 5698 break; 5699 } 5700 5701 // Add any parameter attributes. 5702 newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo)); 5703 argNo++; 5704 } 5705 if (dontTransform) 5706 continue; 5707 5708 // Okay, we can transform this. Create the new call instruction and copy 5709 // over the required information. 5710 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo); 5711 5712 // Copy over any operand bundles. 5713 SmallVector<llvm::OperandBundleDef, 1> newBundles; 5714 callSite->getOperandBundlesAsDefs(newBundles); 5715 5716 llvm::CallBase *newCall; 5717 if (isa<llvm::CallInst>(callSite)) { 5718 newCall = 5719 llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite); 5720 } else { 5721 auto *oldInvoke = cast<llvm::InvokeInst>(callSite); 5722 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(), 5723 oldInvoke->getUnwindDest(), newArgs, 5724 newBundles, "", callSite); 5725 } 5726 newArgs.clear(); // for the next iteration 5727 5728 if (!newCall->getType()->isVoidTy()) 5729 newCall->takeName(callSite); 5730 newCall->setAttributes( 5731 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(), 5732 oldAttrs.getRetAttrs(), newArgAttrs)); 5733 newCall->setCallingConv(callSite->getCallingConv()); 5734 5735 // Finally, remove the old call, replacing any uses with the new one. 5736 if (!callSite->use_empty()) 5737 callSite->replaceAllUsesWith(newCall); 5738 5739 // Copy debug location attached to CI. 5740 if (callSite->getDebugLoc()) 5741 newCall->setDebugLoc(callSite->getDebugLoc()); 5742 5743 callSite->eraseFromParent(); 5744 } 5745 } 5746 5747 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we 5748 /// implement a function with no prototype, e.g. "int foo() {}". If there are 5749 /// existing call uses of the old function in the module, this adjusts them to 5750 /// call the new function directly. 5751 /// 5752 /// This is not just a cleanup: the always_inline pass requires direct calls to 5753 /// functions to be able to inline them. If there is a bitcast in the way, it 5754 /// won't inline them. Instcombine normally deletes these calls, but it isn't 5755 /// run at -O0. 5756 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, 5757 llvm::Function *NewFn) { 5758 // If we're redefining a global as a function, don't transform it. 5759 if (!isa<llvm::Function>(Old)) return; 5760 5761 replaceUsesOfNonProtoConstant(Old, NewFn); 5762 } 5763 5764 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl *VD) { 5765 auto DK = VD->isThisDeclarationADefinition(); 5766 if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>()) 5767 return; 5768 5769 TemplateSpecializationKind TSK = VD->getTemplateSpecializationKind(); 5770 // If we have a definition, this might be a deferred decl. If the 5771 // instantiation is explicit, make sure we emit it at the end. 5772 if (VD->getDefinition() && TSK == TSK_ExplicitInstantiationDefinition) 5773 GetAddrOfGlobalVar(VD); 5774 5775 EmitTopLevelDecl(VD); 5776 } 5777 5778 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD, 5779 llvm::GlobalValue *GV) { 5780 const auto *D = cast<FunctionDecl>(GD.getDecl()); 5781 5782 // Compute the function info and LLVM type. 5783 const CGFunctionInfo &FI = getTypes().arrangeGlobalDeclaration(GD); 5784 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI); 5785 5786 // Get or create the prototype for the function. 5787 if (!GV || (GV->getValueType() != Ty)) 5788 GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, 5789 /*DontDefer=*/true, 5790 ForDefinition)); 5791 5792 // Already emitted. 5793 if (!GV->isDeclaration()) 5794 return; 5795 5796 // We need to set linkage and visibility on the function before 5797 // generating code for it because various parts of IR generation 5798 // want to propagate this information down (e.g. to local static 5799 // declarations). 5800 auto *Fn = cast<llvm::Function>(GV); 5801 setFunctionLinkage(GD, Fn); 5802 5803 // FIXME: this is redundant with part of setFunctionDefinitionAttributes 5804 setGVProperties(Fn, GD); 5805 5806 MaybeHandleStaticInExternC(D, Fn); 5807 5808 maybeSetTrivialComdat(*D, *Fn); 5809 5810 CodeGenFunction(*this).GenerateCode(GD, Fn, FI); 5811 5812 setNonAliasAttributes(GD, Fn); 5813 SetLLVMFunctionAttributesForDefinition(D, Fn); 5814 5815 if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>()) 5816 AddGlobalCtor(Fn, CA->getPriority()); 5817 if (const DestructorAttr *DA = D->getAttr<DestructorAttr>()) 5818 AddGlobalDtor(Fn, DA->getPriority(), true); 5819 if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>()) 5820 getOpenMPRuntime().emitDeclareTargetFunction(D, GV); 5821 } 5822 5823 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) { 5824 const auto *D = cast<ValueDecl>(GD.getDecl()); 5825 const AliasAttr *AA = D->getAttr<AliasAttr>(); 5826 assert(AA && "Not an alias?"); 5827 5828 StringRef MangledName = getMangledName(GD); 5829 5830 if (AA->getAliasee() == MangledName) { 5831 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; 5832 return; 5833 } 5834 5835 // If there is a definition in the module, then it wins over the alias. 5836 // This is dubious, but allow it to be safe. Just ignore the alias. 5837 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 5838 if (Entry && !Entry->isDeclaration()) 5839 return; 5840 5841 Aliases.push_back(GD); 5842 5843 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 5844 5845 // Create a reference to the named value. This ensures that it is emitted 5846 // if a deferred decl. 5847 llvm::Constant *Aliasee; 5848 llvm::GlobalValue::LinkageTypes LT; 5849 if (isa<llvm::FunctionType>(DeclTy)) { 5850 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD, 5851 /*ForVTable=*/false); 5852 LT = getFunctionLinkage(GD); 5853 } else { 5854 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default, 5855 /*D=*/nullptr); 5856 if (const auto *VD = dyn_cast<VarDecl>(GD.getDecl())) 5857 LT = getLLVMLinkageVarDefinition(VD); 5858 else 5859 LT = getFunctionLinkage(GD); 5860 } 5861 5862 // Create the new alias itself, but don't set a name yet. 5863 unsigned AS = Aliasee->getType()->getPointerAddressSpace(); 5864 auto *GA = 5865 llvm::GlobalAlias::create(DeclTy, AS, LT, "", Aliasee, &getModule()); 5866 5867 if (Entry) { 5868 if (GA->getAliasee() == Entry) { 5869 Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0; 5870 return; 5871 } 5872 5873 assert(Entry->isDeclaration()); 5874 5875 // If there is a declaration in the module, then we had an extern followed 5876 // by the alias, as in: 5877 // extern int test6(); 5878 // ... 5879 // int test6() __attribute__((alias("test7"))); 5880 // 5881 // Remove it and replace uses of it with the alias. 5882 GA->takeName(Entry); 5883 5884 Entry->replaceAllUsesWith(GA); 5885 Entry->eraseFromParent(); 5886 } else { 5887 GA->setName(MangledName); 5888 } 5889 5890 // Set attributes which are particular to an alias; this is a 5891 // specialization of the attributes which may be set on a global 5892 // variable/function. 5893 if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() || 5894 D->isWeakImported()) { 5895 GA->setLinkage(llvm::Function::WeakAnyLinkage); 5896 } 5897 5898 if (const auto *VD = dyn_cast<VarDecl>(D)) 5899 if (VD->getTLSKind()) 5900 setTLSMode(GA, *VD); 5901 5902 SetCommonAttributes(GD, GA); 5903 5904 // Emit global alias debug information. 5905 if (isa<VarDecl>(D)) 5906 if (CGDebugInfo *DI = getModuleDebugInfo()) 5907 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD); 5908 } 5909 5910 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) { 5911 const auto *D = cast<ValueDecl>(GD.getDecl()); 5912 const IFuncAttr *IFA = D->getAttr<IFuncAttr>(); 5913 assert(IFA && "Not an ifunc?"); 5914 5915 StringRef MangledName = getMangledName(GD); 5916 5917 if (IFA->getResolver() == MangledName) { 5918 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; 5919 return; 5920 } 5921 5922 // Report an error if some definition overrides ifunc. 5923 llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 5924 if (Entry && !Entry->isDeclaration()) { 5925 GlobalDecl OtherGD; 5926 if (lookupRepresentativeDecl(MangledName, OtherGD) && 5927 DiagnosedConflictingDefinitions.insert(GD).second) { 5928 Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name) 5929 << MangledName; 5930 Diags.Report(OtherGD.getDecl()->getLocation(), 5931 diag::note_previous_definition); 5932 } 5933 return; 5934 } 5935 5936 Aliases.push_back(GD); 5937 5938 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType()); 5939 llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy); 5940 llvm::Constant *Resolver = 5941 GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {}, 5942 /*ForVTable=*/false); 5943 llvm::GlobalIFunc *GIF = 5944 llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage, 5945 "", Resolver, &getModule()); 5946 if (Entry) { 5947 if (GIF->getResolver() == Entry) { 5948 Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1; 5949 return; 5950 } 5951 assert(Entry->isDeclaration()); 5952 5953 // If there is a declaration in the module, then we had an extern followed 5954 // by the ifunc, as in: 5955 // extern int test(); 5956 // ... 5957 // int test() __attribute__((ifunc("resolver"))); 5958 // 5959 // Remove it and replace uses of it with the ifunc. 5960 GIF->takeName(Entry); 5961 5962 Entry->replaceAllUsesWith(GIF); 5963 Entry->eraseFromParent(); 5964 } else 5965 GIF->setName(MangledName); 5966 if (auto *F = dyn_cast<llvm::Function>(Resolver)) { 5967 F->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation); 5968 } 5969 SetCommonAttributes(GD, GIF); 5970 } 5971 5972 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID, 5973 ArrayRef<llvm::Type*> Tys) { 5974 return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID, 5975 Tys); 5976 } 5977 5978 static llvm::StringMapEntry<llvm::GlobalVariable *> & 5979 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map, 5980 const StringLiteral *Literal, bool TargetIsLSB, 5981 bool &IsUTF16, unsigned &StringLength) { 5982 StringRef String = Literal->getString(); 5983 unsigned NumBytes = String.size(); 5984 5985 // Check for simple case. 5986 if (!Literal->containsNonAsciiOrNull()) { 5987 StringLength = NumBytes; 5988 return *Map.insert(std::make_pair(String, nullptr)).first; 5989 } 5990 5991 // Otherwise, convert the UTF8 literals into a string of shorts. 5992 IsUTF16 = true; 5993 5994 SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls. 5995 const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data(); 5996 llvm::UTF16 *ToPtr = &ToBuf[0]; 5997 5998 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr, 5999 ToPtr + NumBytes, llvm::strictConversion); 6000 6001 // ConvertUTF8toUTF16 returns the length in ToPtr. 6002 StringLength = ToPtr - &ToBuf[0]; 6003 6004 // Add an explicit null. 6005 *ToPtr = 0; 6006 return *Map.insert(std::make_pair( 6007 StringRef(reinterpret_cast<const char *>(ToBuf.data()), 6008 (StringLength + 1) * 2), 6009 nullptr)).first; 6010 } 6011 6012 ConstantAddress 6013 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral *Literal) { 6014 unsigned StringLength = 0; 6015 bool isUTF16 = false; 6016 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry = 6017 GetConstantCFStringEntry(CFConstantStringMap, Literal, 6018 getDataLayout().isLittleEndian(), isUTF16, 6019 StringLength); 6020 6021 if (auto *C = Entry.second) 6022 return ConstantAddress( 6023 C, C->getValueType(), CharUnits::fromQuantity(C->getAlignment())); 6024 6025 llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty); 6026 llvm::Constant *Zeros[] = { Zero, Zero }; 6027 6028 const ASTContext &Context = getContext(); 6029 const llvm::Triple &Triple = getTriple(); 6030 6031 const auto CFRuntime = getLangOpts().CFRuntime; 6032 const bool IsSwiftABI = 6033 static_cast<unsigned>(CFRuntime) >= 6034 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift); 6035 const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1; 6036 6037 // If we don't already have it, get __CFConstantStringClassReference. 6038 if (!CFConstantStringClassRef) { 6039 const char *CFConstantStringClassName = "__CFConstantStringClassReference"; 6040 llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy); 6041 Ty = llvm::ArrayType::get(Ty, 0); 6042 6043 switch (CFRuntime) { 6044 default: break; 6045 case LangOptions::CoreFoundationABI::Swift: [[fallthrough]]; 6046 case LangOptions::CoreFoundationABI::Swift5_0: 6047 CFConstantStringClassName = 6048 Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN" 6049 : "$s10Foundation19_NSCFConstantStringCN"; 6050 Ty = IntPtrTy; 6051 break; 6052 case LangOptions::CoreFoundationABI::Swift4_2: 6053 CFConstantStringClassName = 6054 Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN" 6055 : "$S10Foundation19_NSCFConstantStringCN"; 6056 Ty = IntPtrTy; 6057 break; 6058 case LangOptions::CoreFoundationABI::Swift4_1: 6059 CFConstantStringClassName = 6060 Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN" 6061 : "__T010Foundation19_NSCFConstantStringCN"; 6062 Ty = IntPtrTy; 6063 break; 6064 } 6065 6066 llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName); 6067 6068 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) { 6069 llvm::GlobalValue *GV = nullptr; 6070 6071 if ((GV = dyn_cast<llvm::GlobalValue>(C))) { 6072 IdentifierInfo &II = Context.Idents.get(GV->getName()); 6073 TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl(); 6074 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); 6075 6076 const VarDecl *VD = nullptr; 6077 for (const auto *Result : DC->lookup(&II)) 6078 if ((VD = dyn_cast<VarDecl>(Result))) 6079 break; 6080 6081 if (Triple.isOSBinFormatELF()) { 6082 if (!VD) 6083 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 6084 } else { 6085 GV->setLinkage(llvm::GlobalValue::ExternalLinkage); 6086 if (!VD || !VD->hasAttr<DLLExportAttr>()) 6087 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); 6088 else 6089 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); 6090 } 6091 6092 setDSOLocal(GV); 6093 } 6094 } 6095 6096 // Decay array -> ptr 6097 CFConstantStringClassRef = 6098 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty) 6099 : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros); 6100 } 6101 6102 QualType CFTy = Context.getCFConstantStringType(); 6103 6104 auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy)); 6105 6106 ConstantInitBuilder Builder(*this); 6107 auto Fields = Builder.beginStruct(STy); 6108 6109 // Class pointer. 6110 Fields.add(cast<llvm::Constant>(CFConstantStringClassRef)); 6111 6112 // Flags. 6113 if (IsSwiftABI) { 6114 Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01); 6115 Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8); 6116 } else { 6117 Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8); 6118 } 6119 6120 // String pointer. 6121 llvm::Constant *C = nullptr; 6122 if (isUTF16) { 6123 auto Arr = llvm::ArrayRef( 6124 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())), 6125 Entry.first().size() / 2); 6126 C = llvm::ConstantDataArray::get(VMContext, Arr); 6127 } else { 6128 C = llvm::ConstantDataArray::getString(VMContext, Entry.first()); 6129 } 6130 6131 // Note: -fwritable-strings doesn't make the backing store strings of 6132 // CFStrings writable. 6133 auto *GV = 6134 new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true, 6135 llvm::GlobalValue::PrivateLinkage, C, ".str"); 6136 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 6137 // Don't enforce the target's minimum global alignment, since the only use 6138 // of the string is via this class initializer. 6139 CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy) 6140 : Context.getTypeAlignInChars(Context.CharTy); 6141 GV->setAlignment(Align.getAsAlign()); 6142 6143 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1. 6144 // Without it LLVM can merge the string with a non unnamed_addr one during 6145 // LTO. Doing that changes the section it ends in, which surprises ld64. 6146 if (Triple.isOSBinFormatMachO()) 6147 GV->setSection(isUTF16 ? "__TEXT,__ustring" 6148 : "__TEXT,__cstring,cstring_literals"); 6149 // Make sure the literal ends up in .rodata to allow for safe ICF and for 6150 // the static linker to adjust permissions to read-only later on. 6151 else if (Triple.isOSBinFormatELF()) 6152 GV->setSection(".rodata"); 6153 6154 // String. 6155 llvm::Constant *Str = 6156 llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros); 6157 6158 Fields.add(Str); 6159 6160 // String length. 6161 llvm::IntegerType *LengthTy = 6162 llvm::IntegerType::get(getModule().getContext(), 6163 Context.getTargetInfo().getLongWidth()); 6164 if (IsSwiftABI) { 6165 if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || 6166 CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) 6167 LengthTy = Int32Ty; 6168 else 6169 LengthTy = IntPtrTy; 6170 } 6171 Fields.addInt(LengthTy, StringLength); 6172 6173 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is 6174 // properly aligned on 32-bit platforms. 6175 CharUnits Alignment = 6176 IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign(); 6177 6178 // The struct. 6179 GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment, 6180 /*isConstant=*/false, 6181 llvm::GlobalVariable::PrivateLinkage); 6182 GV->addAttribute("objc_arc_inert"); 6183 switch (Triple.getObjectFormat()) { 6184 case llvm::Triple::UnknownObjectFormat: 6185 llvm_unreachable("unknown file format"); 6186 case llvm::Triple::DXContainer: 6187 case llvm::Triple::GOFF: 6188 case llvm::Triple::SPIRV: 6189 case llvm::Triple::XCOFF: 6190 llvm_unreachable("unimplemented"); 6191 case llvm::Triple::COFF: 6192 case llvm::Triple::ELF: 6193 case llvm::Triple::Wasm: 6194 GV->setSection("cfstring"); 6195 break; 6196 case llvm::Triple::MachO: 6197 GV->setSection("__DATA,__cfstring"); 6198 break; 6199 } 6200 Entry.second = GV; 6201 6202 return ConstantAddress(GV, GV->getValueType(), Alignment); 6203 } 6204 6205 bool CodeGenModule::getExpressionLocationsEnabled() const { 6206 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo; 6207 } 6208 6209 QualType CodeGenModule::getObjCFastEnumerationStateType() { 6210 if (ObjCFastEnumerationStateType.isNull()) { 6211 RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState"); 6212 D->startDefinition(); 6213 6214 QualType FieldTypes[] = { 6215 Context.UnsignedLongTy, Context.getPointerType(Context.getObjCIdType()), 6216 Context.getPointerType(Context.UnsignedLongTy), 6217 Context.getConstantArrayType(Context.UnsignedLongTy, llvm::APInt(32, 5), 6218 nullptr, ArraySizeModifier::Normal, 0)}; 6219 6220 for (size_t i = 0; i < 4; ++i) { 6221 FieldDecl *Field = FieldDecl::Create(Context, 6222 D, 6223 SourceLocation(), 6224 SourceLocation(), nullptr, 6225 FieldTypes[i], /*TInfo=*/nullptr, 6226 /*BitWidth=*/nullptr, 6227 /*Mutable=*/false, 6228 ICIS_NoInit); 6229 Field->setAccess(AS_public); 6230 D->addDecl(Field); 6231 } 6232 6233 D->completeDefinition(); 6234 ObjCFastEnumerationStateType = Context.getTagDeclType(D); 6235 } 6236 6237 return ObjCFastEnumerationStateType; 6238 } 6239 6240 llvm::Constant * 6241 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { 6242 assert(!E->getType()->isPointerType() && "Strings are always arrays"); 6243 6244 // Don't emit it as the address of the string, emit the string data itself 6245 // as an inline array. 6246 if (E->getCharByteWidth() == 1) { 6247 SmallString<64> Str(E->getString()); 6248 6249 // Resize the string to the right size, which is indicated by its type. 6250 const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType()); 6251 assert(CAT && "String literal not of constant array type!"); 6252 Str.resize(CAT->getSize().getZExtValue()); 6253 return llvm::ConstantDataArray::getString(VMContext, Str, false); 6254 } 6255 6256 auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType())); 6257 llvm::Type *ElemTy = AType->getElementType(); 6258 unsigned NumElements = AType->getNumElements(); 6259 6260 // Wide strings have either 2-byte or 4-byte elements. 6261 if (ElemTy->getPrimitiveSizeInBits() == 16) { 6262 SmallVector<uint16_t, 32> Elements; 6263 Elements.reserve(NumElements); 6264 6265 for(unsigned i = 0, e = E->getLength(); i != e; ++i) 6266 Elements.push_back(E->getCodeUnit(i)); 6267 Elements.resize(NumElements); 6268 return llvm::ConstantDataArray::get(VMContext, Elements); 6269 } 6270 6271 assert(ElemTy->getPrimitiveSizeInBits() == 32); 6272 SmallVector<uint32_t, 32> Elements; 6273 Elements.reserve(NumElements); 6274 6275 for(unsigned i = 0, e = E->getLength(); i != e; ++i) 6276 Elements.push_back(E->getCodeUnit(i)); 6277 Elements.resize(NumElements); 6278 return llvm::ConstantDataArray::get(VMContext, Elements); 6279 } 6280 6281 static llvm::GlobalVariable * 6282 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, 6283 CodeGenModule &CGM, StringRef GlobalName, 6284 CharUnits Alignment) { 6285 unsigned AddrSpace = CGM.getContext().getTargetAddressSpace( 6286 CGM.GetGlobalConstantAddressSpace()); 6287 6288 llvm::Module &M = CGM.getModule(); 6289 // Create a global variable for this string 6290 auto *GV = new llvm::GlobalVariable( 6291 M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName, 6292 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace); 6293 GV->setAlignment(Alignment.getAsAlign()); 6294 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); 6295 if (GV->isWeakForLinker()) { 6296 assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals"); 6297 GV->setComdat(M.getOrInsertComdat(GV->getName())); 6298 } 6299 CGM.setDSOLocal(GV); 6300 6301 return GV; 6302 } 6303 6304 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a 6305 /// constant array for the given string literal. 6306 ConstantAddress 6307 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S, 6308 StringRef Name) { 6309 CharUnits Alignment = getContext().getAlignOfGlobalVarInChars(S->getType()); 6310 6311 llvm::Constant *C = GetConstantArrayFromStringLiteral(S); 6312 llvm::GlobalVariable **Entry = nullptr; 6313 if (!LangOpts.WritableStrings) { 6314 Entry = &ConstantStringMap[C]; 6315 if (auto GV = *Entry) { 6316 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment()) 6317 GV->setAlignment(Alignment.getAsAlign()); 6318 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6319 GV->getValueType(), Alignment); 6320 } 6321 } 6322 6323 SmallString<256> MangledNameBuffer; 6324 StringRef GlobalVariableName; 6325 llvm::GlobalValue::LinkageTypes LT; 6326 6327 // Mangle the string literal if that's how the ABI merges duplicate strings. 6328 // Don't do it if they are writable, since we don't want writes in one TU to 6329 // affect strings in another. 6330 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) && 6331 !LangOpts.WritableStrings) { 6332 llvm::raw_svector_ostream Out(MangledNameBuffer); 6333 getCXXABI().getMangleContext().mangleStringLiteral(S, Out); 6334 LT = llvm::GlobalValue::LinkOnceODRLinkage; 6335 GlobalVariableName = MangledNameBuffer; 6336 } else { 6337 LT = llvm::GlobalValue::PrivateLinkage; 6338 GlobalVariableName = Name; 6339 } 6340 6341 auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment); 6342 6343 CGDebugInfo *DI = getModuleDebugInfo(); 6344 if (DI && getCodeGenOpts().hasReducedDebugInfo()) 6345 DI->AddStringLiteralDebugInfo(GV, S); 6346 6347 if (Entry) 6348 *Entry = GV; 6349 6350 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0), "<string literal>"); 6351 6352 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6353 GV->getValueType(), Alignment); 6354 } 6355 6356 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant 6357 /// array for the given ObjCEncodeExpr node. 6358 ConstantAddress 6359 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *E) { 6360 std::string Str; 6361 getContext().getObjCEncodingForType(E->getEncodedType(), Str); 6362 6363 return GetAddrOfConstantCString(Str); 6364 } 6365 6366 /// GetAddrOfConstantCString - Returns a pointer to a character array containing 6367 /// the literal and a terminating '\0' character. 6368 /// The result has pointer to array type. 6369 ConstantAddress CodeGenModule::GetAddrOfConstantCString( 6370 const std::string &Str, const char *GlobalName) { 6371 StringRef StrWithNull(Str.c_str(), Str.size() + 1); 6372 CharUnits Alignment = 6373 getContext().getAlignOfGlobalVarInChars(getContext().CharTy); 6374 6375 llvm::Constant *C = 6376 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false); 6377 6378 // Don't share any string literals if strings aren't constant. 6379 llvm::GlobalVariable **Entry = nullptr; 6380 if (!LangOpts.WritableStrings) { 6381 Entry = &ConstantStringMap[C]; 6382 if (auto GV = *Entry) { 6383 if (uint64_t(Alignment.getQuantity()) > GV->getAlignment()) 6384 GV->setAlignment(Alignment.getAsAlign()); 6385 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6386 GV->getValueType(), Alignment); 6387 } 6388 } 6389 6390 // Get the default prefix if a name wasn't specified. 6391 if (!GlobalName) 6392 GlobalName = ".str"; 6393 // Create a global variable for this. 6394 auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this, 6395 GlobalName, Alignment); 6396 if (Entry) 6397 *Entry = GV; 6398 6399 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV), 6400 GV->getValueType(), Alignment); 6401 } 6402 6403 ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( 6404 const MaterializeTemporaryExpr *E, const Expr *Init) { 6405 assert((E->getStorageDuration() == SD_Static || 6406 E->getStorageDuration() == SD_Thread) && "not a global temporary"); 6407 const auto *VD = cast<VarDecl>(E->getExtendingDecl()); 6408 6409 // If we're not materializing a subobject of the temporary, keep the 6410 // cv-qualifiers from the type of the MaterializeTemporaryExpr. 6411 QualType MaterializedType = Init->getType(); 6412 if (Init == E->getSubExpr()) 6413 MaterializedType = E->getType(); 6414 6415 CharUnits Align = getContext().getTypeAlignInChars(MaterializedType); 6416 6417 auto InsertResult = MaterializedGlobalTemporaryMap.insert({E, nullptr}); 6418 if (!InsertResult.second) { 6419 // We've seen this before: either we already created it or we're in the 6420 // process of doing so. 6421 if (!InsertResult.first->second) { 6422 // We recursively re-entered this function, probably during emission of 6423 // the initializer. Create a placeholder. We'll clean this up in the 6424 // outer call, at the end of this function. 6425 llvm::Type *Type = getTypes().ConvertTypeForMem(MaterializedType); 6426 InsertResult.first->second = new llvm::GlobalVariable( 6427 getModule(), Type, false, llvm::GlobalVariable::InternalLinkage, 6428 nullptr); 6429 } 6430 return ConstantAddress(InsertResult.first->second, 6431 llvm::cast<llvm::GlobalVariable>( 6432 InsertResult.first->second->stripPointerCasts()) 6433 ->getValueType(), 6434 Align); 6435 } 6436 6437 // FIXME: If an externally-visible declaration extends multiple temporaries, 6438 // we need to give each temporary the same name in every translation unit (and 6439 // we also need to make the temporaries externally-visible). 6440 SmallString<256> Name; 6441 llvm::raw_svector_ostream Out(Name); 6442 getCXXABI().getMangleContext().mangleReferenceTemporary( 6443 VD, E->getManglingNumber(), Out); 6444 6445 APValue *Value = nullptr; 6446 if (E->getStorageDuration() == SD_Static && VD->evaluateValue()) { 6447 // If the initializer of the extending declaration is a constant 6448 // initializer, we should have a cached constant initializer for this 6449 // temporary. Note that this might have a different value from the value 6450 // computed by evaluating the initializer if the surrounding constant 6451 // expression modifies the temporary. 6452 Value = E->getOrCreateValue(false); 6453 } 6454 6455 // Try evaluating it now, it might have a constant initializer. 6456 Expr::EvalResult EvalResult; 6457 if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) && 6458 !EvalResult.hasSideEffects()) 6459 Value = &EvalResult.Val; 6460 6461 LangAS AddrSpace = GetGlobalVarAddressSpace(VD); 6462 6463 std::optional<ConstantEmitter> emitter; 6464 llvm::Constant *InitialValue = nullptr; 6465 bool Constant = false; 6466 llvm::Type *Type; 6467 if (Value) { 6468 // The temporary has a constant initializer, use it. 6469 emitter.emplace(*this); 6470 InitialValue = emitter->emitForInitializer(*Value, AddrSpace, 6471 MaterializedType); 6472 Constant = 6473 MaterializedType.isConstantStorage(getContext(), /*ExcludeCtor*/ Value, 6474 /*ExcludeDtor*/ false); 6475 Type = InitialValue->getType(); 6476 } else { 6477 // No initializer, the initialization will be provided when we 6478 // initialize the declaration which performed lifetime extension. 6479 Type = getTypes().ConvertTypeForMem(MaterializedType); 6480 } 6481 6482 // Create a global variable for this lifetime-extended temporary. 6483 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(VD); 6484 if (Linkage == llvm::GlobalVariable::ExternalLinkage) { 6485 const VarDecl *InitVD; 6486 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) && 6487 isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) { 6488 // Temporaries defined inside a class get linkonce_odr linkage because the 6489 // class can be defined in multiple translation units. 6490 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage; 6491 } else { 6492 // There is no need for this temporary to have external linkage if the 6493 // VarDecl has external linkage. 6494 Linkage = llvm::GlobalVariable::InternalLinkage; 6495 } 6496 } 6497 auto TargetAS = getContext().getTargetAddressSpace(AddrSpace); 6498 auto *GV = new llvm::GlobalVariable( 6499 getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(), 6500 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS); 6501 if (emitter) emitter->finalize(GV); 6502 // Don't assign dllimport or dllexport to local linkage globals. 6503 if (!llvm::GlobalValue::isLocalLinkage(Linkage)) { 6504 setGVProperties(GV, VD); 6505 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass) 6506 // The reference temporary should never be dllexport. 6507 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass); 6508 } 6509 GV->setAlignment(Align.getAsAlign()); 6510 if (supportsCOMDAT() && GV->isWeakForLinker()) 6511 GV->setComdat(TheModule.getOrInsertComdat(GV->getName())); 6512 if (VD->getTLSKind()) 6513 setTLSMode(GV, *VD); 6514 llvm::Constant *CV = GV; 6515 if (AddrSpace != LangAS::Default) 6516 CV = getTargetCodeGenInfo().performAddrSpaceCast( 6517 *this, GV, AddrSpace, LangAS::Default, 6518 llvm::PointerType::get( 6519 getLLVMContext(), 6520 getContext().getTargetAddressSpace(LangAS::Default))); 6521 6522 // Update the map with the new temporary. If we created a placeholder above, 6523 // replace it with the new global now. 6524 llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E]; 6525 if (Entry) { 6526 Entry->replaceAllUsesWith(CV); 6527 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent(); 6528 } 6529 Entry = CV; 6530 6531 return ConstantAddress(CV, Type, Align); 6532 } 6533 6534 /// EmitObjCPropertyImplementations - Emit information for synthesized 6535 /// properties for an implementation. 6536 void CodeGenModule::EmitObjCPropertyImplementations(const 6537 ObjCImplementationDecl *D) { 6538 for (const auto *PID : D->property_impls()) { 6539 // Dynamic is just for type-checking. 6540 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 6541 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 6542 6543 // Determine which methods need to be implemented, some may have 6544 // been overridden. Note that ::isPropertyAccessor is not the method 6545 // we want, that just indicates if the decl came from a 6546 // property. What we want to know is if the method is defined in 6547 // this implementation. 6548 auto *Getter = PID->getGetterMethodDecl(); 6549 if (!Getter || Getter->isSynthesizedAccessorStub()) 6550 CodeGenFunction(*this).GenerateObjCGetter( 6551 const_cast<ObjCImplementationDecl *>(D), PID); 6552 auto *Setter = PID->getSetterMethodDecl(); 6553 if (!PD->isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub())) 6554 CodeGenFunction(*this).GenerateObjCSetter( 6555 const_cast<ObjCImplementationDecl *>(D), PID); 6556 } 6557 } 6558 } 6559 6560 static bool needsDestructMethod(ObjCImplementationDecl *impl) { 6561 const ObjCInterfaceDecl *iface = impl->getClassInterface(); 6562 for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); 6563 ivar; ivar = ivar->getNextIvar()) 6564 if (ivar->getType().isDestructedType()) 6565 return true; 6566 6567 return false; 6568 } 6569 6570 static bool AllTrivialInitializers(CodeGenModule &CGM, 6571 ObjCImplementationDecl *D) { 6572 CodeGenFunction CGF(CGM); 6573 for (ObjCImplementationDecl::init_iterator B = D->init_begin(), 6574 E = D->init_end(); B != E; ++B) { 6575 CXXCtorInitializer *CtorInitExp = *B; 6576 Expr *Init = CtorInitExp->getInit(); 6577 if (!CGF.isTrivialInitializer(Init)) 6578 return false; 6579 } 6580 return true; 6581 } 6582 6583 /// EmitObjCIvarInitializations - Emit information for ivar initialization 6584 /// for an implementation. 6585 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) { 6586 // We might need a .cxx_destruct even if we don't have any ivar initializers. 6587 if (needsDestructMethod(D)) { 6588 IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct"); 6589 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 6590 ObjCMethodDecl *DTORMethod = ObjCMethodDecl::Create( 6591 getContext(), D->getLocation(), D->getLocation(), cxxSelector, 6592 getContext().VoidTy, nullptr, D, 6593 /*isInstance=*/true, /*isVariadic=*/false, 6594 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, 6595 /*isImplicitlyDeclared=*/true, 6596 /*isDefined=*/false, ObjCImplementationControl::Required); 6597 D->addInstanceMethod(DTORMethod); 6598 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false); 6599 D->setHasDestructors(true); 6600 } 6601 6602 // If the implementation doesn't have any ivar initializers, we don't need 6603 // a .cxx_construct. 6604 if (D->getNumIvarInitializers() == 0 || 6605 AllTrivialInitializers(*this, D)) 6606 return; 6607 6608 IdentifierInfo *II = &getContext().Idents.get(".cxx_construct"); 6609 Selector cxxSelector = getContext().Selectors.getSelector(0, &II); 6610 // The constructor returns 'self'. 6611 ObjCMethodDecl *CTORMethod = ObjCMethodDecl::Create( 6612 getContext(), D->getLocation(), D->getLocation(), cxxSelector, 6613 getContext().getObjCIdType(), nullptr, D, /*isInstance=*/true, 6614 /*isVariadic=*/false, 6615 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, 6616 /*isImplicitlyDeclared=*/true, 6617 /*isDefined=*/false, ObjCImplementationControl::Required); 6618 D->addInstanceMethod(CTORMethod); 6619 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true); 6620 D->setHasNonZeroConstructors(true); 6621 } 6622 6623 // EmitLinkageSpec - Emit all declarations in a linkage spec. 6624 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) { 6625 if (LSD->getLanguage() != LinkageSpecLanguageIDs::C && 6626 LSD->getLanguage() != LinkageSpecLanguageIDs::CXX) { 6627 ErrorUnsupported(LSD, "linkage spec"); 6628 return; 6629 } 6630 6631 EmitDeclContext(LSD); 6632 } 6633 6634 void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl *D) { 6635 // Device code should not be at top level. 6636 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) 6637 return; 6638 6639 std::unique_ptr<CodeGenFunction> &CurCGF = 6640 GlobalTopLevelStmtBlockInFlight.first; 6641 6642 // We emitted a top-level stmt but after it there is initialization. 6643 // Stop squashing the top-level stmts into a single function. 6644 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) { 6645 CurCGF->FinishFunction(D->getEndLoc()); 6646 CurCGF = nullptr; 6647 } 6648 6649 if (!CurCGF) { 6650 // void __stmts__N(void) 6651 // FIXME: Ask the ABI name mangler to pick a name. 6652 std::string Name = "__stmts__" + llvm::utostr(CXXGlobalInits.size()); 6653 FunctionArgList Args; 6654 QualType RetTy = getContext().VoidTy; 6655 const CGFunctionInfo &FnInfo = 6656 getTypes().arrangeBuiltinFunctionDeclaration(RetTy, Args); 6657 llvm::FunctionType *FnTy = getTypes().GetFunctionType(FnInfo); 6658 llvm::Function *Fn = llvm::Function::Create( 6659 FnTy, llvm::GlobalValue::InternalLinkage, Name, &getModule()); 6660 6661 CurCGF.reset(new CodeGenFunction(*this)); 6662 GlobalTopLevelStmtBlockInFlight.second = D; 6663 CurCGF->StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args, 6664 D->getBeginLoc(), D->getBeginLoc()); 6665 CXXGlobalInits.push_back(Fn); 6666 } 6667 6668 CurCGF->EmitStmt(D->getStmt()); 6669 } 6670 6671 void CodeGenModule::EmitDeclContext(const DeclContext *DC) { 6672 for (auto *I : DC->decls()) { 6673 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope 6674 // are themselves considered "top-level", so EmitTopLevelDecl on an 6675 // ObjCImplDecl does not recursively visit them. We need to do that in 6676 // case they're nested inside another construct (LinkageSpecDecl / 6677 // ExportDecl) that does stop them from being considered "top-level". 6678 if (auto *OID = dyn_cast<ObjCImplDecl>(I)) { 6679 for (auto *M : OID->methods()) 6680 EmitTopLevelDecl(M); 6681 } 6682 6683 EmitTopLevelDecl(I); 6684 } 6685 } 6686 6687 /// EmitTopLevelDecl - Emit code for a single top level declaration. 6688 void CodeGenModule::EmitTopLevelDecl(Decl *D) { 6689 // Ignore dependent declarations. 6690 if (D->isTemplated()) 6691 return; 6692 6693 // Consteval function shouldn't be emitted. 6694 if (auto *FD = dyn_cast<FunctionDecl>(D); FD && FD->isImmediateFunction()) 6695 return; 6696 6697 switch (D->getKind()) { 6698 case Decl::CXXConversion: 6699 case Decl::CXXMethod: 6700 case Decl::Function: 6701 EmitGlobal(cast<FunctionDecl>(D)); 6702 // Always provide some coverage mapping 6703 // even for the functions that aren't emitted. 6704 AddDeferredUnusedCoverageMapping(D); 6705 break; 6706 6707 case Decl::CXXDeductionGuide: 6708 // Function-like, but does not result in code emission. 6709 break; 6710 6711 case Decl::Var: 6712 case Decl::Decomposition: 6713 case Decl::VarTemplateSpecialization: 6714 EmitGlobal(cast<VarDecl>(D)); 6715 if (auto *DD = dyn_cast<DecompositionDecl>(D)) 6716 for (auto *B : DD->bindings()) 6717 if (auto *HD = B->getHoldingVar()) 6718 EmitGlobal(HD); 6719 break; 6720 6721 // Indirect fields from global anonymous structs and unions can be 6722 // ignored; only the actual variable requires IR gen support. 6723 case Decl::IndirectField: 6724 break; 6725 6726 // C++ Decls 6727 case Decl::Namespace: 6728 EmitDeclContext(cast<NamespaceDecl>(D)); 6729 break; 6730 case Decl::ClassTemplateSpecialization: { 6731 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D); 6732 if (CGDebugInfo *DI = getModuleDebugInfo()) 6733 if (Spec->getSpecializationKind() == 6734 TSK_ExplicitInstantiationDefinition && 6735 Spec->hasDefinition()) 6736 DI->completeTemplateDefinition(*Spec); 6737 } [[fallthrough]]; 6738 case Decl::CXXRecord: { 6739 CXXRecordDecl *CRD = cast<CXXRecordDecl>(D); 6740 if (CGDebugInfo *DI = getModuleDebugInfo()) { 6741 if (CRD->hasDefinition()) 6742 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D))); 6743 if (auto *ES = D->getASTContext().getExternalSource()) 6744 if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never) 6745 DI->completeUnusedClass(*CRD); 6746 } 6747 // Emit any static data members, they may be definitions. 6748 for (auto *I : CRD->decls()) 6749 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I)) 6750 EmitTopLevelDecl(I); 6751 break; 6752 } 6753 // No code generation needed. 6754 case Decl::UsingShadow: 6755 case Decl::ClassTemplate: 6756 case Decl::VarTemplate: 6757 case Decl::Concept: 6758 case Decl::VarTemplatePartialSpecialization: 6759 case Decl::FunctionTemplate: 6760 case Decl::TypeAliasTemplate: 6761 case Decl::Block: 6762 case Decl::Empty: 6763 case Decl::Binding: 6764 break; 6765 case Decl::Using: // using X; [C++] 6766 if (CGDebugInfo *DI = getModuleDebugInfo()) 6767 DI->EmitUsingDecl(cast<UsingDecl>(*D)); 6768 break; 6769 case Decl::UsingEnum: // using enum X; [C++] 6770 if (CGDebugInfo *DI = getModuleDebugInfo()) 6771 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D)); 6772 break; 6773 case Decl::NamespaceAlias: 6774 if (CGDebugInfo *DI = getModuleDebugInfo()) 6775 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D)); 6776 break; 6777 case Decl::UsingDirective: // using namespace X; [C++] 6778 if (CGDebugInfo *DI = getModuleDebugInfo()) 6779 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D)); 6780 break; 6781 case Decl::CXXConstructor: 6782 getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D)); 6783 break; 6784 case Decl::CXXDestructor: 6785 getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D)); 6786 break; 6787 6788 case Decl::StaticAssert: 6789 // Nothing to do. 6790 break; 6791 6792 // Objective-C Decls 6793 6794 // Forward declarations, no (immediate) code generation. 6795 case Decl::ObjCInterface: 6796 case Decl::ObjCCategory: 6797 break; 6798 6799 case Decl::ObjCProtocol: { 6800 auto *Proto = cast<ObjCProtocolDecl>(D); 6801 if (Proto->isThisDeclarationADefinition()) 6802 ObjCRuntime->GenerateProtocol(Proto); 6803 break; 6804 } 6805 6806 case Decl::ObjCCategoryImpl: 6807 // Categories have properties but don't support synthesize so we 6808 // can ignore them here. 6809 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D)); 6810 break; 6811 6812 case Decl::ObjCImplementation: { 6813 auto *OMD = cast<ObjCImplementationDecl>(D); 6814 EmitObjCPropertyImplementations(OMD); 6815 EmitObjCIvarInitializations(OMD); 6816 ObjCRuntime->GenerateClass(OMD); 6817 // Emit global variable debug information. 6818 if (CGDebugInfo *DI = getModuleDebugInfo()) 6819 if (getCodeGenOpts().hasReducedDebugInfo()) 6820 DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType( 6821 OMD->getClassInterface()), OMD->getLocation()); 6822 break; 6823 } 6824 case Decl::ObjCMethod: { 6825 auto *OMD = cast<ObjCMethodDecl>(D); 6826 // If this is not a prototype, emit the body. 6827 if (OMD->getBody()) 6828 CodeGenFunction(*this).GenerateObjCMethod(OMD); 6829 break; 6830 } 6831 case Decl::ObjCCompatibleAlias: 6832 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D)); 6833 break; 6834 6835 case Decl::PragmaComment: { 6836 const auto *PCD = cast<PragmaCommentDecl>(D); 6837 switch (PCD->getCommentKind()) { 6838 case PCK_Unknown: 6839 llvm_unreachable("unexpected pragma comment kind"); 6840 case PCK_Linker: 6841 AppendLinkerOptions(PCD->getArg()); 6842 break; 6843 case PCK_Lib: 6844 AddDependentLib(PCD->getArg()); 6845 break; 6846 case PCK_Compiler: 6847 case PCK_ExeStr: 6848 case PCK_User: 6849 break; // We ignore all of these. 6850 } 6851 break; 6852 } 6853 6854 case Decl::PragmaDetectMismatch: { 6855 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D); 6856 AddDetectMismatch(PDMD->getName(), PDMD->getValue()); 6857 break; 6858 } 6859 6860 case Decl::LinkageSpec: 6861 EmitLinkageSpec(cast<LinkageSpecDecl>(D)); 6862 break; 6863 6864 case Decl::FileScopeAsm: { 6865 // File-scope asm is ignored during device-side CUDA compilation. 6866 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) 6867 break; 6868 // File-scope asm is ignored during device-side OpenMP compilation. 6869 if (LangOpts.OpenMPIsTargetDevice) 6870 break; 6871 // File-scope asm is ignored during device-side SYCL compilation. 6872 if (LangOpts.SYCLIsDevice) 6873 break; 6874 auto *AD = cast<FileScopeAsmDecl>(D); 6875 getModule().appendModuleInlineAsm(AD->getAsmString()->getString()); 6876 break; 6877 } 6878 6879 case Decl::TopLevelStmt: 6880 EmitTopLevelStmt(cast<TopLevelStmtDecl>(D)); 6881 break; 6882 6883 case Decl::Import: { 6884 auto *Import = cast<ImportDecl>(D); 6885 6886 // If we've already imported this module, we're done. 6887 if (!ImportedModules.insert(Import->getImportedModule())) 6888 break; 6889 6890 // Emit debug information for direct imports. 6891 if (!Import->getImportedOwningModule()) { 6892 if (CGDebugInfo *DI = getModuleDebugInfo()) 6893 DI->EmitImportDecl(*Import); 6894 } 6895 6896 // For C++ standard modules we are done - we will call the module 6897 // initializer for imported modules, and that will likewise call those for 6898 // any imports it has. 6899 if (CXX20ModuleInits && Import->getImportedOwningModule() && 6900 !Import->getImportedOwningModule()->isModuleMapModule()) 6901 break; 6902 6903 // For clang C++ module map modules the initializers for sub-modules are 6904 // emitted here. 6905 6906 // Find all of the submodules and emit the module initializers. 6907 llvm::SmallPtrSet<clang::Module *, 16> Visited; 6908 SmallVector<clang::Module *, 16> Stack; 6909 Visited.insert(Import->getImportedModule()); 6910 Stack.push_back(Import->getImportedModule()); 6911 6912 while (!Stack.empty()) { 6913 clang::Module *Mod = Stack.pop_back_val(); 6914 if (!EmittedModuleInitializers.insert(Mod).second) 6915 continue; 6916 6917 for (auto *D : Context.getModuleInitializers(Mod)) 6918 EmitTopLevelDecl(D); 6919 6920 // Visit the submodules of this module. 6921 for (auto *Submodule : Mod->submodules()) { 6922 // Skip explicit children; they need to be explicitly imported to emit 6923 // the initializers. 6924 if (Submodule->IsExplicit) 6925 continue; 6926 6927 if (Visited.insert(Submodule).second) 6928 Stack.push_back(Submodule); 6929 } 6930 } 6931 break; 6932 } 6933 6934 case Decl::Export: 6935 EmitDeclContext(cast<ExportDecl>(D)); 6936 break; 6937 6938 case Decl::OMPThreadPrivate: 6939 EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D)); 6940 break; 6941 6942 case Decl::OMPAllocate: 6943 EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D)); 6944 break; 6945 6946 case Decl::OMPDeclareReduction: 6947 EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D)); 6948 break; 6949 6950 case Decl::OMPDeclareMapper: 6951 EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D)); 6952 break; 6953 6954 case Decl::OMPRequires: 6955 EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D)); 6956 break; 6957 6958 case Decl::Typedef: 6959 case Decl::TypeAlias: // using foo = bar; [C++11] 6960 if (CGDebugInfo *DI = getModuleDebugInfo()) 6961 DI->EmitAndRetainType( 6962 getContext().getTypedefType(cast<TypedefNameDecl>(D))); 6963 break; 6964 6965 case Decl::Record: 6966 if (CGDebugInfo *DI = getModuleDebugInfo()) 6967 if (cast<RecordDecl>(D)->getDefinition()) 6968 DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(D))); 6969 break; 6970 6971 case Decl::Enum: 6972 if (CGDebugInfo *DI = getModuleDebugInfo()) 6973 if (cast<EnumDecl>(D)->getDefinition()) 6974 DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(D))); 6975 break; 6976 6977 case Decl::HLSLBuffer: 6978 getHLSLRuntime().addBuffer(cast<HLSLBufferDecl>(D)); 6979 break; 6980 6981 default: 6982 // Make sure we handled everything we should, every other kind is a 6983 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind 6984 // function. Need to recode Decl::Kind to do that easily. 6985 assert(isa<TypeDecl>(D) && "Unsupported decl kind"); 6986 break; 6987 } 6988 } 6989 6990 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl *D) { 6991 // Do we need to generate coverage mapping? 6992 if (!CodeGenOpts.CoverageMapping) 6993 return; 6994 switch (D->getKind()) { 6995 case Decl::CXXConversion: 6996 case Decl::CXXMethod: 6997 case Decl::Function: 6998 case Decl::ObjCMethod: 6999 case Decl::CXXConstructor: 7000 case Decl::CXXDestructor: { 7001 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody()) 7002 break; 7003 SourceManager &SM = getContext().getSourceManager(); 7004 if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc())) 7005 break; 7006 DeferredEmptyCoverageMappingDecls.try_emplace(D, true); 7007 break; 7008 } 7009 default: 7010 break; 7011 }; 7012 } 7013 7014 void CodeGenModule::ClearUnusedCoverageMapping(const Decl *D) { 7015 // Do we need to generate coverage mapping? 7016 if (!CodeGenOpts.CoverageMapping) 7017 return; 7018 if (const auto *Fn = dyn_cast<FunctionDecl>(D)) { 7019 if (Fn->isTemplateInstantiation()) 7020 ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern()); 7021 } 7022 DeferredEmptyCoverageMappingDecls.insert_or_assign(D, false); 7023 } 7024 7025 void CodeGenModule::EmitDeferredUnusedCoverageMappings() { 7026 // We call takeVector() here to avoid use-after-free. 7027 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because 7028 // we deserialize function bodies to emit coverage info for them, and that 7029 // deserializes more declarations. How should we handle that case? 7030 for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) { 7031 if (!Entry.second) 7032 continue; 7033 const Decl *D = Entry.first; 7034 switch (D->getKind()) { 7035 case Decl::CXXConversion: 7036 case Decl::CXXMethod: 7037 case Decl::Function: 7038 case Decl::ObjCMethod: { 7039 CodeGenPGO PGO(*this); 7040 GlobalDecl GD(cast<FunctionDecl>(D)); 7041 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 7042 getFunctionLinkage(GD)); 7043 break; 7044 } 7045 case Decl::CXXConstructor: { 7046 CodeGenPGO PGO(*this); 7047 GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base); 7048 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 7049 getFunctionLinkage(GD)); 7050 break; 7051 } 7052 case Decl::CXXDestructor: { 7053 CodeGenPGO PGO(*this); 7054 GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base); 7055 PGO.emitEmptyCounterMapping(D, getMangledName(GD), 7056 getFunctionLinkage(GD)); 7057 break; 7058 } 7059 default: 7060 break; 7061 }; 7062 } 7063 } 7064 7065 void CodeGenModule::EmitMainVoidAlias() { 7066 // In order to transition away from "__original_main" gracefully, emit an 7067 // alias for "main" in the no-argument case so that libc can detect when 7068 // new-style no-argument main is in used. 7069 if (llvm::Function *F = getModule().getFunction("main")) { 7070 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() && 7071 F->getReturnType()->isIntegerTy(Context.getTargetInfo().getIntWidth())) { 7072 auto *GA = llvm::GlobalAlias::create("__main_void", F); 7073 GA->setVisibility(llvm::GlobalValue::HiddenVisibility); 7074 } 7075 } 7076 } 7077 7078 /// Turns the given pointer into a constant. 7079 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context, 7080 const void *Ptr) { 7081 uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr); 7082 llvm::Type *i64 = llvm::Type::getInt64Ty(Context); 7083 return llvm::ConstantInt::get(i64, PtrInt); 7084 } 7085 7086 static void EmitGlobalDeclMetadata(CodeGenModule &CGM, 7087 llvm::NamedMDNode *&GlobalMetadata, 7088 GlobalDecl D, 7089 llvm::GlobalValue *Addr) { 7090 if (!GlobalMetadata) 7091 GlobalMetadata = 7092 CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs"); 7093 7094 // TODO: should we report variant information for ctors/dtors? 7095 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr), 7096 llvm::ConstantAsMetadata::get(GetPointerConstant( 7097 CGM.getLLVMContext(), D.getDecl()))}; 7098 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops)); 7099 } 7100 7101 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem, 7102 llvm::GlobalValue *CppFunc) { 7103 // Store the list of ifuncs we need to replace uses in. 7104 llvm::SmallVector<llvm::GlobalIFunc *> IFuncs; 7105 // List of ConstantExprs that we should be able to delete when we're done 7106 // here. 7107 llvm::SmallVector<llvm::ConstantExpr *> CEs; 7108 7109 // It isn't valid to replace the extern-C ifuncs if all we find is itself! 7110 if (Elem == CppFunc) 7111 return false; 7112 7113 // First make sure that all users of this are ifuncs (or ifuncs via a 7114 // bitcast), and collect the list of ifuncs and CEs so we can work on them 7115 // later. 7116 for (llvm::User *User : Elem->users()) { 7117 // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an 7118 // ifunc directly. In any other case, just give up, as we don't know what we 7119 // could break by changing those. 7120 if (auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) { 7121 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast) 7122 return false; 7123 7124 for (llvm::User *CEUser : ConstExpr->users()) { 7125 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) { 7126 IFuncs.push_back(IFunc); 7127 } else { 7128 return false; 7129 } 7130 } 7131 CEs.push_back(ConstExpr); 7132 } else if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) { 7133 IFuncs.push_back(IFunc); 7134 } else { 7135 // This user is one we don't know how to handle, so fail redirection. This 7136 // will result in an ifunc retaining a resolver name that will ultimately 7137 // fail to be resolved to a defined function. 7138 return false; 7139 } 7140 } 7141 7142 // Now we know this is a valid case where we can do this alias replacement, we 7143 // need to remove all of the references to Elem (and the bitcasts!) so we can 7144 // delete it. 7145 for (llvm::GlobalIFunc *IFunc : IFuncs) 7146 IFunc->setResolver(nullptr); 7147 for (llvm::ConstantExpr *ConstExpr : CEs) 7148 ConstExpr->destroyConstant(); 7149 7150 // We should now be out of uses for the 'old' version of this function, so we 7151 // can erase it as well. 7152 Elem->eraseFromParent(); 7153 7154 for (llvm::GlobalIFunc *IFunc : IFuncs) { 7155 // The type of the resolver is always just a function-type that returns the 7156 // type of the IFunc, so create that here. If the type of the actual 7157 // resolver doesn't match, it just gets bitcast to the right thing. 7158 auto *ResolverTy = 7159 llvm::FunctionType::get(IFunc->getType(), /*isVarArg*/ false); 7160 llvm::Constant *Resolver = GetOrCreateLLVMFunction( 7161 CppFunc->getName(), ResolverTy, {}, /*ForVTable*/ false); 7162 IFunc->setResolver(Resolver); 7163 } 7164 return true; 7165 } 7166 7167 /// For each function which is declared within an extern "C" region and marked 7168 /// as 'used', but has internal linkage, create an alias from the unmangled 7169 /// name to the mangled name if possible. People expect to be able to refer 7170 /// to such functions with an unmangled name from inline assembly within the 7171 /// same translation unit. 7172 void CodeGenModule::EmitStaticExternCAliases() { 7173 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases()) 7174 return; 7175 for (auto &I : StaticExternCValues) { 7176 IdentifierInfo *Name = I.first; 7177 llvm::GlobalValue *Val = I.second; 7178 7179 // If Val is null, that implies there were multiple declarations that each 7180 // had a claim to the unmangled name. In this case, generation of the alias 7181 // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC. 7182 if (!Val) 7183 break; 7184 7185 llvm::GlobalValue *ExistingElem = 7186 getModule().getNamedValue(Name->getName()); 7187 7188 // If there is either not something already by this name, or we were able to 7189 // replace all uses from IFuncs, create the alias. 7190 if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val)) 7191 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name->getName(), Val)); 7192 } 7193 } 7194 7195 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName, 7196 GlobalDecl &Result) const { 7197 auto Res = Manglings.find(MangledName); 7198 if (Res == Manglings.end()) 7199 return false; 7200 Result = Res->getValue(); 7201 return true; 7202 } 7203 7204 /// Emits metadata nodes associating all the global values in the 7205 /// current module with the Decls they came from. This is useful for 7206 /// projects using IR gen as a subroutine. 7207 /// 7208 /// Since there's currently no way to associate an MDNode directly 7209 /// with an llvm::GlobalValue, we create a global named metadata 7210 /// with the name 'clang.global.decl.ptrs'. 7211 void CodeGenModule::EmitDeclMetadata() { 7212 llvm::NamedMDNode *GlobalMetadata = nullptr; 7213 7214 for (auto &I : MangledDeclNames) { 7215 llvm::GlobalValue *Addr = getModule().getNamedValue(I.second); 7216 // Some mangled names don't necessarily have an associated GlobalValue 7217 // in this module, e.g. if we mangled it for DebugInfo. 7218 if (Addr) 7219 EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr); 7220 } 7221 } 7222 7223 /// Emits metadata nodes for all the local variables in the current 7224 /// function. 7225 void CodeGenFunction::EmitDeclMetadata() { 7226 if (LocalDeclMap.empty()) return; 7227 7228 llvm::LLVMContext &Context = getLLVMContext(); 7229 7230 // Find the unique metadata ID for this name. 7231 unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr"); 7232 7233 llvm::NamedMDNode *GlobalMetadata = nullptr; 7234 7235 for (auto &I : LocalDeclMap) { 7236 const Decl *D = I.first; 7237 llvm::Value *Addr = I.second.getPointer(); 7238 if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) { 7239 llvm::Value *DAddr = GetPointerConstant(getLLVMContext(), D); 7240 Alloca->setMetadata( 7241 DeclPtrKind, llvm::MDNode::get( 7242 Context, llvm::ValueAsMetadata::getConstant(DAddr))); 7243 } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) { 7244 GlobalDecl GD = GlobalDecl(cast<VarDecl>(D)); 7245 EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV); 7246 } 7247 } 7248 } 7249 7250 void CodeGenModule::EmitVersionIdentMetadata() { 7251 llvm::NamedMDNode *IdentMetadata = 7252 TheModule.getOrInsertNamedMetadata("llvm.ident"); 7253 std::string Version = getClangFullVersion(); 7254 llvm::LLVMContext &Ctx = TheModule.getContext(); 7255 7256 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)}; 7257 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode)); 7258 } 7259 7260 void CodeGenModule::EmitCommandLineMetadata() { 7261 llvm::NamedMDNode *CommandLineMetadata = 7262 TheModule.getOrInsertNamedMetadata("llvm.commandline"); 7263 std::string CommandLine = getCodeGenOpts().RecordCommandLine; 7264 llvm::LLVMContext &Ctx = TheModule.getContext(); 7265 7266 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)}; 7267 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode)); 7268 } 7269 7270 void CodeGenModule::EmitCoverageFile() { 7271 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu"); 7272 if (!CUNode) 7273 return; 7274 7275 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov"); 7276 llvm::LLVMContext &Ctx = TheModule.getContext(); 7277 auto *CoverageDataFile = 7278 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile); 7279 auto *CoverageNotesFile = 7280 llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile); 7281 for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) { 7282 llvm::MDNode *CU = CUNode->getOperand(i); 7283 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU}; 7284 GCov->addOperand(llvm::MDNode::get(Ctx, Elts)); 7285 } 7286 } 7287 7288 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, 7289 bool ForEH) { 7290 // Return a bogus pointer if RTTI is disabled, unless it's for EH. 7291 // FIXME: should we even be calling this method if RTTI is disabled 7292 // and it's not for EH? 7293 if (!shouldEmitRTTI(ForEH)) 7294 return llvm::Constant::getNullValue(GlobalsInt8PtrTy); 7295 7296 if (ForEH && Ty->isObjCObjectPointerType() && 7297 LangOpts.ObjCRuntime.isGNUFamily()) 7298 return ObjCRuntime->GetEHType(Ty); 7299 7300 return getCXXABI().getAddrOfRTTIDescriptor(Ty); 7301 } 7302 7303 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D) { 7304 // Do not emit threadprivates in simd-only mode. 7305 if (LangOpts.OpenMP && LangOpts.OpenMPSimd) 7306 return; 7307 for (auto RefExpr : D->varlists()) { 7308 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl()); 7309 bool PerformInit = 7310 VD->getAnyInitializer() && 7311 !VD->getAnyInitializer()->isConstantInitializer(getContext(), 7312 /*ForRef=*/false); 7313 7314 Address Addr(GetAddrOfGlobalVar(VD), 7315 getTypes().ConvertTypeForMem(VD->getType()), 7316 getContext().getDeclAlign(VD)); 7317 if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition( 7318 VD, Addr, RefExpr->getBeginLoc(), PerformInit)) 7319 CXXGlobalInits.push_back(InitFunction); 7320 } 7321 } 7322 7323 llvm::Metadata * 7324 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map, 7325 StringRef Suffix) { 7326 if (auto *FnType = T->getAs<FunctionProtoType>()) 7327 T = getContext().getFunctionType( 7328 FnType->getReturnType(), FnType->getParamTypes(), 7329 FnType->getExtProtoInfo().withExceptionSpec(EST_None)); 7330 7331 llvm::Metadata *&InternalId = Map[T.getCanonicalType()]; 7332 if (InternalId) 7333 return InternalId; 7334 7335 if (isExternallyVisible(T->getLinkage())) { 7336 std::string OutName; 7337 llvm::raw_string_ostream Out(OutName); 7338 getCXXABI().getMangleContext().mangleCanonicalTypeName( 7339 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers); 7340 7341 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers) 7342 Out << ".normalized"; 7343 7344 Out << Suffix; 7345 7346 InternalId = llvm::MDString::get(getLLVMContext(), Out.str()); 7347 } else { 7348 InternalId = llvm::MDNode::getDistinct(getLLVMContext(), 7349 llvm::ArrayRef<llvm::Metadata *>()); 7350 } 7351 7352 return InternalId; 7353 } 7354 7355 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierForType(QualType T) { 7356 return CreateMetadataIdentifierImpl(T, MetadataIdMap, ""); 7357 } 7358 7359 llvm::Metadata * 7360 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T) { 7361 return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual"); 7362 } 7363 7364 // Generalize pointer types to a void pointer with the qualifiers of the 7365 // originally pointed-to type, e.g. 'const char *' and 'char * const *' 7366 // generalize to 'const void *' while 'char *' and 'const char **' generalize to 7367 // 'void *'. 7368 static QualType GeneralizeType(ASTContext &Ctx, QualType Ty) { 7369 if (!Ty->isPointerType()) 7370 return Ty; 7371 7372 return Ctx.getPointerType( 7373 QualType(Ctx.VoidTy).withCVRQualifiers( 7374 Ty->getPointeeType().getCVRQualifiers())); 7375 } 7376 7377 // Apply type generalization to a FunctionType's return and argument types 7378 static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty) { 7379 if (auto *FnType = Ty->getAs<FunctionProtoType>()) { 7380 SmallVector<QualType, 8> GeneralizedParams; 7381 for (auto &Param : FnType->param_types()) 7382 GeneralizedParams.push_back(GeneralizeType(Ctx, Param)); 7383 7384 return Ctx.getFunctionType( 7385 GeneralizeType(Ctx, FnType->getReturnType()), 7386 GeneralizedParams, FnType->getExtProtoInfo()); 7387 } 7388 7389 if (auto *FnType = Ty->getAs<FunctionNoProtoType>()) 7390 return Ctx.getFunctionNoProtoType( 7391 GeneralizeType(Ctx, FnType->getReturnType())); 7392 7393 llvm_unreachable("Encountered unknown FunctionType"); 7394 } 7395 7396 llvm::Metadata *CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T) { 7397 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T), 7398 GeneralizedMetadataIdMap, ".generalized"); 7399 } 7400 7401 /// Returns whether this module needs the "all-vtables" type identifier. 7402 bool CodeGenModule::NeedAllVtablesTypeId() const { 7403 // Returns true if at least one of vtable-based CFI checkers is enabled and 7404 // is not in the trapping mode. 7405 return ((LangOpts.Sanitize.has(SanitizerKind::CFIVCall) && 7406 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIVCall)) || 7407 (LangOpts.Sanitize.has(SanitizerKind::CFINVCall) && 7408 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFINVCall)) || 7409 (LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast) && 7410 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIDerivedCast)) || 7411 (LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast) && 7412 !CodeGenOpts.SanitizeTrap.has(SanitizerKind::CFIUnrelatedCast))); 7413 } 7414 7415 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable *VTable, 7416 CharUnits Offset, 7417 const CXXRecordDecl *RD) { 7418 llvm::Metadata *MD = 7419 CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); 7420 VTable->addTypeMetadata(Offset.getQuantity(), MD); 7421 7422 if (CodeGenOpts.SanitizeCfiCrossDso) 7423 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD)) 7424 VTable->addTypeMetadata(Offset.getQuantity(), 7425 llvm::ConstantAsMetadata::get(CrossDsoTypeId)); 7426 7427 if (NeedAllVtablesTypeId()) { 7428 llvm::Metadata *MD = llvm::MDString::get(getLLVMContext(), "all-vtables"); 7429 VTable->addTypeMetadata(Offset.getQuantity(), MD); 7430 } 7431 } 7432 7433 llvm::SanitizerStatReport &CodeGenModule::getSanStats() { 7434 if (!SanStats) 7435 SanStats = std::make_unique<llvm::SanitizerStatReport>(&getModule()); 7436 7437 return *SanStats; 7438 } 7439 7440 llvm::Value * 7441 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr *E, 7442 CodeGenFunction &CGF) { 7443 llvm::Constant *C = ConstantEmitter(CGF).emitAbstract(E, E->getType()); 7444 auto *SamplerT = getOpenCLRuntime().getSamplerType(E->getType().getTypePtr()); 7445 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()}, false); 7446 auto *Call = CGF.EmitRuntimeCall( 7447 CreateRuntimeFunction(FTy, "__translate_sampler_initializer"), {C}); 7448 return Call; 7449 } 7450 7451 CharUnits CodeGenModule::getNaturalPointeeTypeAlignment( 7452 QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) { 7453 return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo, 7454 /* forPointeeType= */ true); 7455 } 7456 7457 CharUnits CodeGenModule::getNaturalTypeAlignment(QualType T, 7458 LValueBaseInfo *BaseInfo, 7459 TBAAAccessInfo *TBAAInfo, 7460 bool forPointeeType) { 7461 if (TBAAInfo) 7462 *TBAAInfo = getTBAAAccessInfo(T); 7463 7464 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But 7465 // that doesn't return the information we need to compute BaseInfo. 7466 7467 // Honor alignment typedef attributes even on incomplete types. 7468 // We also honor them straight for C++ class types, even as pointees; 7469 // there's an expressivity gap here. 7470 if (auto TT = T->getAs<TypedefType>()) { 7471 if (auto Align = TT->getDecl()->getMaxAlignment()) { 7472 if (BaseInfo) 7473 *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType); 7474 return getContext().toCharUnitsFromBits(Align); 7475 } 7476 } 7477 7478 bool AlignForArray = T->isArrayType(); 7479 7480 // Analyze the base element type, so we don't get confused by incomplete 7481 // array types. 7482 T = getContext().getBaseElementType(T); 7483 7484 if (T->isIncompleteType()) { 7485 // We could try to replicate the logic from 7486 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the 7487 // type is incomplete, so it's impossible to test. We could try to reuse 7488 // getTypeAlignIfKnown, but that doesn't return the information we need 7489 // to set BaseInfo. So just ignore the possibility that the alignment is 7490 // greater than one. 7491 if (BaseInfo) 7492 *BaseInfo = LValueBaseInfo(AlignmentSource::Type); 7493 return CharUnits::One(); 7494 } 7495 7496 if (BaseInfo) 7497 *BaseInfo = LValueBaseInfo(AlignmentSource::Type); 7498 7499 CharUnits Alignment; 7500 const CXXRecordDecl *RD; 7501 if (T.getQualifiers().hasUnaligned()) { 7502 Alignment = CharUnits::One(); 7503 } else if (forPointeeType && !AlignForArray && 7504 (RD = T->getAsCXXRecordDecl())) { 7505 // For C++ class pointees, we don't know whether we're pointing at a 7506 // base or a complete object, so we generally need to use the 7507 // non-virtual alignment. 7508 Alignment = getClassPointerAlignment(RD); 7509 } else { 7510 Alignment = getContext().getTypeAlignInChars(T); 7511 } 7512 7513 // Cap to the global maximum type alignment unless the alignment 7514 // was somehow explicit on the type. 7515 if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) { 7516 if (Alignment.getQuantity() > MaxAlign && 7517 !getContext().isAlignmentRequired(T)) 7518 Alignment = CharUnits::fromQuantity(MaxAlign); 7519 } 7520 return Alignment; 7521 } 7522 7523 bool CodeGenModule::stopAutoInit() { 7524 unsigned StopAfter = getContext().getLangOpts().TrivialAutoVarInitStopAfter; 7525 if (StopAfter) { 7526 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is 7527 // used 7528 if (NumAutoVarInit >= StopAfter) { 7529 return true; 7530 } 7531 if (!NumAutoVarInit) { 7532 unsigned DiagID = getDiags().getCustomDiagID( 7533 DiagnosticsEngine::Warning, 7534 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the " 7535 "number of times ftrivial-auto-var-init=%1 gets applied."); 7536 getDiags().Report(DiagID) 7537 << StopAfter 7538 << (getContext().getLangOpts().getTrivialAutoVarInit() == 7539 LangOptions::TrivialAutoVarInitKind::Zero 7540 ? "zero" 7541 : "pattern"); 7542 } 7543 ++NumAutoVarInit; 7544 } 7545 return false; 7546 } 7547 7548 void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream &OS, 7549 const Decl *D) const { 7550 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers 7551 // postfix beginning with '.' since the symbol name can be demangled. 7552 if (LangOpts.HIP) 7553 OS << (isa<VarDecl>(D) ? ".static." : ".intern."); 7554 else 7555 OS << (isa<VarDecl>(D) ? "__static__" : "__intern__"); 7556 7557 // If the CUID is not specified we try to generate a unique postfix. 7558 if (getLangOpts().CUID.empty()) { 7559 SourceManager &SM = getContext().getSourceManager(); 7560 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation()); 7561 assert(PLoc.isValid() && "Source location is expected to be valid."); 7562 7563 // Get the hash of the user defined macros. 7564 llvm::MD5 Hash; 7565 llvm::MD5::MD5Result Result; 7566 for (const auto &Arg : PreprocessorOpts.Macros) 7567 Hash.update(Arg.first); 7568 Hash.final(Result); 7569 7570 // Get the UniqueID for the file containing the decl. 7571 llvm::sys::fs::UniqueID ID; 7572 if (llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) { 7573 PLoc = SM.getPresumedLoc(D->getLocation(), /*UseLineDirectives=*/false); 7574 assert(PLoc.isValid() && "Source location is expected to be valid."); 7575 if (auto EC = llvm::sys::fs::getUniqueID(PLoc.getFilename(), ID)) 7576 SM.getDiagnostics().Report(diag::err_cannot_open_file) 7577 << PLoc.getFilename() << EC.message(); 7578 } 7579 OS << llvm::format("%x", ID.getFile()) << llvm::format("%x", ID.getDevice()) 7580 << "_" << llvm::utohexstr(Result.low(), /*LowerCase=*/true, /*Width=*/8); 7581 } else { 7582 OS << getContext().getCUIDHash(); 7583 } 7584 } 7585 7586 void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) { 7587 assert(DeferredDeclsToEmit.empty() && 7588 "Should have emitted all decls deferred to emit."); 7589 assert(NewBuilder->DeferredDecls.empty() && 7590 "Newly created module should not have deferred decls"); 7591 NewBuilder->DeferredDecls = std::move(DeferredDecls); 7592 assert(EmittedDeferredDecls.empty() && 7593 "Still have (unmerged) EmittedDeferredDecls deferred decls"); 7594 7595 assert(NewBuilder->DeferredVTables.empty() && 7596 "Newly created module should not have deferred vtables"); 7597 NewBuilder->DeferredVTables = std::move(DeferredVTables); 7598 7599 assert(NewBuilder->MangledDeclNames.empty() && 7600 "Newly created module should not have mangled decl names"); 7601 assert(NewBuilder->Manglings.empty() && 7602 "Newly created module should not have manglings"); 7603 NewBuilder->Manglings = std::move(Manglings); 7604 7605 NewBuilder->WeakRefReferences = std::move(WeakRefReferences); 7606 7607 NewBuilder->TBAA = std::move(TBAA); 7608 7609 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx); 7610 } 7611