brintos

brintos / llvm-project-archived public Read only

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