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