55 #include "llvm/ADT/STLExtras.h"
56 #include "llvm/ADT/StringExtras.h"
57 #include "llvm/ADT/StringSwitch.h"
58 #include "llvm/Analysis/TargetLibraryInfo.h"
59 #include "llvm/BinaryFormat/ELF.h"
60 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
61 #include "llvm/IR/AttributeMask.h"
62 #include "llvm/IR/CallingConv.h"
63 #include "llvm/IR/DataLayout.h"
64 #include "llvm/IR/Intrinsics.h"
65 #include "llvm/IR/LLVMContext.h"
66 #include "llvm/IR/Module.h"
67 #include "llvm/IR/ProfileSummary.h"
68 #include "llvm/ProfileData/InstrProfReader.h"
69 #include "llvm/ProfileData/SampleProf.h"
70 #include "llvm/Support/CRC.h"
71 #include "llvm/Support/CodeGen.h"
72 #include "llvm/Support/CommandLine.h"
73 #include "llvm/Support/ConvertUTF.h"
74 #include "llvm/Support/ErrorHandling.h"
75 #include "llvm/Support/TimeProfiler.h"
76 #include "llvm/Support/xxhash.h"
77 #include "llvm/TargetParser/RISCVISAInfo.h"
78 #include "llvm/TargetParser/Triple.h"
79 #include "llvm/TargetParser/X86TargetParser.h"
80 #include "llvm/Transforms/Utils/BuildLibCalls.h"
83 using namespace clang;
84 using namespace CodeGen;
87 "limited-coverage-experimental", llvm::cl::Hidden,
88 llvm::cl::desc(
"Emit limited coverage mapping information (experimental)"));
94 case TargetCXXABI::AppleARM64:
95 case TargetCXXABI::Fuchsia:
96 case TargetCXXABI::GenericAArch64:
97 case TargetCXXABI::GenericARM:
98 case TargetCXXABI::iOS:
99 case TargetCXXABI::WatchOS:
100 case TargetCXXABI::GenericMIPS:
101 case TargetCXXABI::GenericItanium:
102 case TargetCXXABI::WebAssembly:
103 case TargetCXXABI::XL:
105 case TargetCXXABI::Microsoft:
109 llvm_unreachable(
"invalid C++ ABI kind");
114 return LangOpts.SYCLIsHost && LangOpts.CUDA && !LangOpts.CUDAIsDevice;
118 return LangOpts.SYCLIsDevice && LangOpts.CUDA && !LangOpts.CUDAIsDevice;
121 static std::unique_ptr<TargetCodeGenInfo>
127 switch (Triple.getArch()) {
131 case llvm::Triple::le32:
133 case llvm::Triple::m68k:
135 case llvm::Triple::mips:
136 case llvm::Triple::mipsel:
137 if (Triple.getOS() == llvm::Triple::NaCl)
141 case llvm::Triple::mips64:
142 case llvm::Triple::mips64el:
145 case llvm::Triple::avr: {
149 unsigned NPR =
Target.getABI() ==
"avrtiny" ? 6 : 18;
150 unsigned NRR =
Target.getABI() ==
"avrtiny" ? 4 : 8;
154 case llvm::Triple::aarch64:
155 case llvm::Triple::aarch64_32:
156 case llvm::Triple::aarch64_be: {
158 if (
Target.getABI() ==
"darwinpcs")
160 else if (Triple.isOSWindows())
162 else if (
Target.getABI() ==
"aapcs-soft")
168 case llvm::Triple::wasm32:
169 case llvm::Triple::wasm64: {
171 if (
Target.getABI() ==
"experimental-mv")
176 case llvm::Triple::arm:
177 case llvm::Triple::armeb:
178 case llvm::Triple::thumb:
179 case llvm::Triple::thumbeb: {
180 if (Triple.getOS() == llvm::Triple::Win32)
184 StringRef ABIStr =
Target.getABI();
185 if (ABIStr ==
"apcs-gnu")
187 else if (ABIStr ==
"aapcs16")
189 else if (CodeGenOpts.
FloatABI ==
"hard" ||
191 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
192 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
193 Triple.getEnvironment() == llvm::Triple::EABIHF)))
199 case llvm::Triple::ppc: {
200 if (Triple.isOSAIX())
207 case llvm::Triple::ppcle: {
208 bool IsSoftFloat = CodeGenOpts.
FloatABI ==
"soft";
211 case llvm::Triple::ppc64:
212 if (Triple.isOSAIX())
215 if (Triple.isOSBinFormatELF()) {
217 if (
Target.getABI() ==
"elfv2")
219 bool IsSoftFloat = CodeGenOpts.
FloatABI ==
"soft";
224 case llvm::Triple::ppc64le: {
225 assert(Triple.isOSBinFormatELF() &&
"PPC64 LE non-ELF not supported!");
227 if (
Target.getABI() ==
"elfv1")
229 bool IsSoftFloat = CodeGenOpts.
FloatABI ==
"soft";
234 case llvm::Triple::nvptx:
235 case llvm::Triple::nvptx64:
238 case llvm::Triple::msp430:
241 case llvm::Triple::riscv32:
242 case llvm::Triple::riscv64: {
243 StringRef ABIStr =
Target.getABI();
245 unsigned ABIFLen = 0;
246 if (ABIStr.ends_with(
"f"))
248 else if (ABIStr.ends_with(
"d"))
250 bool EABI = ABIStr.ends_with(
"e");
254 case llvm::Triple::systemz: {
255 bool SoftFloat = CodeGenOpts.
FloatABI ==
"soft";
256 bool HasVector = !SoftFloat &&
Target.getABI() ==
"vector";
260 case llvm::Triple::tce:
261 case llvm::Triple::tcele:
264 case llvm::Triple::x86: {
265 bool IsDarwinVectorABI = Triple.isOSDarwin();
266 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
268 if (Triple.getOS() == llvm::Triple::Win32) {
270 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
271 CodeGenOpts.NumRegisterParameters);
274 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
275 CodeGenOpts.NumRegisterParameters, CodeGenOpts.
FloatABI ==
"soft");
278 case llvm::Triple::x86_64: {
279 StringRef ABI =
Target.getABI();
284 switch (Triple.getOS()) {
285 case llvm::Triple::Win32:
291 case llvm::Triple::hexagon:
293 case llvm::Triple::lanai:
295 case llvm::Triple::r600:
297 case llvm::Triple::amdgcn:
299 case llvm::Triple::sparc:
301 case llvm::Triple::sparcv9:
303 case llvm::Triple::xcore:
305 case llvm::Triple::arc:
307 case llvm::Triple::spir:
308 case llvm::Triple::spir64:
310 case llvm::Triple::spirv32:
311 case llvm::Triple::spirv64:
313 case llvm::Triple::ve:
315 case llvm::Triple::csky: {
316 bool IsSoftFloat = !
Target.hasFeature(
"hard-float-abi");
318 Target.hasFeature(
"fpuv2_df") ||
Target.hasFeature(
"fpuv3_df");
323 case llvm::Triple::bpfeb:
324 case llvm::Triple::bpfel:
326 case llvm::Triple::loongarch32:
327 case llvm::Triple::loongarch64: {
328 StringRef ABIStr =
Target.getABI();
329 unsigned ABIFRLen = 0;
330 if (ABIStr.ends_with(
"f"))
332 else if (ABIStr.ends_with(
"d"))
341 if (!TheTargetCodeGenInfo)
343 return *TheTargetCodeGenInfo;
353 : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
354 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
356 VMContext(M.getContext()), Types(*this), VTables(*this),
360 llvm::LLVMContext &LLVMContext = M.getContext();
361 VoidTy = llvm::Type::getVoidTy(LLVMContext);
362 Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
363 Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
364 Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
365 Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
366 HalfTy = llvm::Type::getHalfTy(LLVMContext);
367 BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
368 FloatTy = llvm::Type::getFloatTy(LLVMContext);
369 DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
372 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(
LangAS::Default))
375 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
377 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
379 llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
380 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
381 IntPtrTy = llvm::IntegerType::get(LLVMContext,
382 C.getTargetInfo().getMaxPointerWidth());
383 Int8PtrTy = llvm::PointerType::get(LLVMContext,
385 const llvm::DataLayout &DL = M.getDataLayout();
387 llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
389 llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
418 if (LangOpts.OpenCL || LangOpts.SYCLIsDevice)
419 createOpenCLRuntime();
421 createOpenMPRuntime();
424 if (LangOpts.SYCLIsDevice)
430 if (LangOpts.
Sanitize.
has(SanitizerKind::Thread) ||
431 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
437 if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo ||
442 Block.GlobalUniqueCount = 0;
444 if (C.getLangOpts().ObjC)
455 PGOReader = std::move(ReaderOrErr.get());
460 if (CodeGenOpts.CoverageMapping)
464 if (CodeGenOpts.UniqueInternalLinkageNames &&
465 !
getModule().getSourceFileName().empty()) {
466 std::string Path =
getModule().getSourceFileName();
469 if (Path.rfind(Entry.first, 0) != std::string::npos) {
470 Path = Entry.second + Path.substr(Entry.first.size());
473 ModuleNameHash = llvm::getUniqueInternalLinkagePostfix(Path);
479 CodeGenOpts.NumRegisterParameters);
484 void CodeGenModule::createObjCRuntime() {
501 llvm_unreachable(
"bad runtime kind");
504 void CodeGenModule::createOpenCLRuntime() {
508 void CodeGenModule::createOpenMPRuntime() {
512 case llvm::Triple::nvptx:
513 case llvm::Triple::nvptx64:
514 case llvm::Triple::amdgcn:
516 "OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
520 if (LangOpts.OpenMPSimd)
528 void CodeGenModule::createCUDARuntime() {
532 void CodeGenModule::createSYCLRuntime() {
536 void CodeGenModule::createHLSLRuntime() {
541 Replacements[Name] = C;
544 void CodeGenModule::applyReplacements() {
545 for (
auto &I : Replacements) {
546 StringRef MangledName = I.first;
547 llvm::Constant *Replacement = I.second;
551 auto *OldF = cast<llvm::Function>(Entry);
552 auto *NewF = dyn_cast<llvm::Function>(Replacement);
554 if (
auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
555 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
557 auto *CE = cast<llvm::ConstantExpr>(Replacement);
558 assert(CE->getOpcode() == llvm::Instruction::BitCast ||
559 CE->getOpcode() == llvm::Instruction::GetElementPtr);
560 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
565 OldF->replaceAllUsesWith(Replacement);
567 NewF->removeFromParent();
568 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
571 OldF->eraseFromParent();
576 GlobalValReplacements.push_back(std::make_pair(GV, C));
579 void CodeGenModule::applyGlobalValReplacements() {
580 for (
auto &I : GlobalValReplacements) {
581 llvm::GlobalValue *GV = I.first;
582 llvm::Constant *C = I.second;
584 GV->replaceAllUsesWith(C);
585 GV->eraseFromParent();
592 const llvm::Constant *C;
593 if (
auto *GA = dyn_cast<llvm::GlobalAlias>(GV))
594 C = GA->getAliasee();
595 else if (
auto *GI = dyn_cast<llvm::GlobalIFunc>(GV))
596 C = GI->getResolver();
600 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
604 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
613 bool IsIFunc,
const llvm::GlobalValue *Alias,
const llvm::GlobalValue *&GV,
614 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames,
618 Diags.
Report(Location, diag::err_cyclic_alias) << IsIFunc;
622 if (GV->hasCommonLinkage()) {
624 if (Triple.getObjectFormat() == llvm::Triple::XCOFF) {
625 Diags.
Report(Location, diag::err_alias_to_common);
630 if (GV->isDeclaration()) {
631 Diags.
Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
632 Diags.
Report(Location, diag::note_alias_requires_mangled_name)
633 << IsIFunc << IsIFunc;
636 for (
const auto &[
Decl, Name] : MangledDeclNames) {
637 if (
const auto *ND = dyn_cast<NamedDecl>(
Decl.getDecl())) {
638 if (ND->getName() == GV->getName()) {
639 Diags.
Report(Location, diag::note_alias_mangled_name_alternative)
643 (Twine(IsIFunc ?
"ifunc" :
"alias") +
"(\"" + Name +
"\")")
653 const auto *F = dyn_cast<llvm::Function>(GV);
655 Diags.
Report(Location, diag::err_alias_to_undefined)
656 << IsIFunc << IsIFunc;
660 llvm::FunctionType *FTy = F->getFunctionType();
661 if (!FTy->getReturnType()->isPointerTy()) {
662 Diags.
Report(Location, diag::err_ifunc_resolver_return);
676 if (GVar->hasAttribute(
"toc-data")) {
677 auto GVId = GVar->getName();
680 Diags.
Report(Location, diag::warn_toc_unsupported_type)
681 << GVId <<
"the variable has an alias";
683 llvm::AttributeSet CurrAttributes = GVar->getAttributes();
684 llvm::AttributeSet NewAttributes =
685 CurrAttributes.removeAttribute(GVar->getContext(),
"toc-data");
686 GVar->setAttributes(NewAttributes);
690 void CodeGenModule::checkAliases() {
697 const auto *D = cast<ValueDecl>(GD.getDecl());
700 bool IsIFunc = D->hasAttr<IFuncAttr>();
701 if (
const Attr *A = D->getDefiningAttr()) {
702 Location = A->getLocation();
703 Range = A->getRange();
705 llvm_unreachable(
"Not an alias or ifunc?");
709 const llvm::GlobalValue *GV =
nullptr;
711 MangledDeclNames, Range)) {
717 if (
const llvm::GlobalVariable *GVar =
718 dyn_cast<const llvm::GlobalVariable>(GV))
722 llvm::Constant *Aliasee =
723 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver()
724 : cast<llvm::GlobalAlias>(Alias)->getAliasee();
726 llvm::GlobalValue *AliaseeGV;
727 if (
auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
728 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
730 AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
732 if (
const SectionAttr *SA = D->getAttr<SectionAttr>()) {
733 StringRef AliasSection = SA->getName();
734 if (AliasSection != AliaseeGV->getSection())
735 Diags.
Report(SA->getLocation(), diag::warn_alias_with_section)
736 << AliasSection << IsIFunc << IsIFunc;
744 if (
auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
745 if (GA->isInterposable()) {
746 Diags.
Report(Location, diag::warn_alias_to_weak_alias)
747 << GV->getName() << GA->getName() << IsIFunc;
748 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
749 GA->getAliasee(), Alias->getType());
752 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
754 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
764 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
765 Alias->eraseFromParent();
770 DeferredDeclsToEmit.clear();
771 EmittedDeferredDecls.clear();
772 DeferredAnnotations.clear();
774 OpenMPRuntime->clear();
778 StringRef MainFile) {
781 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
782 if (MainFile.empty())
783 MainFile =
"<stdin>";
784 Diags.
Report(diag::warn_profile_data_unprofiled) << MainFile;
787 Diags.
Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
790 Diags.
Report(diag::warn_profile_data_missing) << Visited << Missing;
794 static std::optional<llvm::GlobalValue::VisibilityTypes>
807 llvm_unreachable(
"unknown option value!");
811 std::optional<llvm::GlobalValue::VisibilityTypes>
V) {
820 GV.setDSOLocal(
false);
821 GV.setVisibility(*
V);
826 if (!LO.VisibilityFromDLLStorageClass)
829 std::optional<llvm::GlobalValue::VisibilityTypes> DLLExportVisibility =
832 std::optional<llvm::GlobalValue::VisibilityTypes>
833 NoDLLStorageClassVisibility =
836 std::optional<llvm::GlobalValue::VisibilityTypes>
837 ExternDeclDLLImportVisibility =
840 std::optional<llvm::GlobalValue::VisibilityTypes>
841 ExternDeclNoDLLStorageClassVisibility =
844 for (llvm::GlobalValue &GV : M.global_values()) {
845 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
848 if (GV.isDeclarationForLinker())
850 llvm::GlobalValue::DLLImportStorageClass
851 ? ExternDeclDLLImportVisibility
852 : ExternDeclNoDLLStorageClassVisibility);
855 llvm::GlobalValue::DLLExportStorageClass
856 ? DLLExportVisibility
857 : NoDLLStorageClassVisibility);
859 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
864 llvm::LLVMContext &Ctx, StringRef Name,
865 const SYCLUsesAspectsAttr *A) {
867 AspectsMD.push_back(llvm::MDString::get(Ctx, Name));
868 for (
auto *Aspect : A->aspects()) {
870 AspectsMD.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
871 llvm::Type::getInt32Ty(Ctx), AspectInt.getZExtValue())));
873 return llvm::MDNode::get(Ctx, AspectsMD);
877 llvm::LLVMContext &Ctx,
880 AspectEnumValMD.push_back(llvm::MDString::get(Ctx, ECD->
getName()));
881 AspectEnumValMD.push_back(
882 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
883 llvm::Type::getInt32Ty(Ctx), ECD->
getInitVal().getSExtValue())));
884 return llvm::MDNode::get(Ctx, AspectEnumValMD);
888 const llvm::Triple &Triple,
890 if (Triple.isAMDGPU() || Triple.isNVPTX())
892 return LangOpts.getStackProtector() == Mode;
898 EmitModuleInitializers(Primary);
900 DeferredDecls.insert(EmittedDeferredDecls.begin(),
901 EmittedDeferredDecls.end());
902 EmittedDeferredDecls.clear();
903 EmitVTablesOpportunistically();
904 applyGlobalValReplacements();
906 emitMultiVersionFunctions();
909 GlobalTopLevelStmtBlockInFlight.first) {
911 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->
getEndLoc());
912 GlobalTopLevelStmtBlockInFlight = {
nullptr,
nullptr};
918 EmitCXXModuleInitFunc(Primary);
920 EmitCXXGlobalInitFunc();
921 EmitCXXGlobalCleanUpFunc();
922 registerGlobalDtorsWithAtExit();
923 EmitCXXThreadLocalInitFunc();
925 if (llvm::Function *ObjCInitFunction =
ObjCRuntime->ModuleInitFunction())
928 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
932 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
933 OpenMPRuntime->clear();
937 PGOReader->getSummary(
false).getMD(VMContext),
938 llvm::ProfileSummary::PSK_Instr);
945 EmitCtorList(GlobalCtors,
"llvm.global_ctors");
946 EmitCtorList(GlobalDtors,
"llvm.global_dtors");
948 EmitStaticExternCAliases();
954 CoverageMapping->emit();
955 if (CodeGenOpts.SanitizeCfiCrossDso) {
961 emitAtAvailableLinkGuard();
968 if (
getTarget().getTargetOpts().CodeObjectVersion !=
969 llvm::CodeObjectVersionKind::COV_None) {
971 "amdhsa_code_object_version",
972 getTarget().getTargetOpts().CodeObjectVersion);
977 auto *MDStr = llvm::MDString::get(
995 if (
auto *FD = dyn_cast<FunctionDecl>(D))
999 UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1003 llvm::ArrayType *ATy = llvm::ArrayType::get(
Int8PtrTy, UsedArray.size());
1005 auto *GV =
new llvm::GlobalVariable(
1006 getModule(), ATy,
false, llvm::GlobalValue::InternalLinkage,
1007 llvm::ConstantArray::get(ATy, UsedArray),
"__clang_gpu_used_external");
1010 if (LangOpts.HIP && !
getLangOpts().OffloadingNewDriver) {
1013 auto *GV =
new llvm::GlobalVariable(
1015 llvm::Constant::getNullValue(
Int8Ty),
1023 if (CodeGenOpts.Autolink &&
1024 (Context.
getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
1025 EmitModuleLinkOptions();
1040 if (!ELFDependentLibraries.empty() && !Context.
getLangOpts().CUDAIsDevice) {
1041 auto *NMD =
getModule().getOrInsertNamedMetadata(
"llvm.dependent-libraries");
1042 for (
auto *MD : ELFDependentLibraries)
1043 NMD->addOperand(MD);
1046 if (CodeGenOpts.DwarfVersion) {
1047 getModule().addModuleFlag(llvm::Module::Max,
"Dwarf Version",
1048 CodeGenOpts.DwarfVersion);
1051 if (CodeGenOpts.Dwarf64)
1052 getModule().addModuleFlag(llvm::Module::Max,
"DWARF64", 1);
1056 getModule().setSemanticInterposition(
true);
1058 if (CodeGenOpts.EmitCodeView) {
1062 if (CodeGenOpts.CodeViewGHash) {
1065 if (CodeGenOpts.ControlFlowGuard) {
1068 }
else if (CodeGenOpts.ControlFlowGuardNoChecks) {
1072 if (CodeGenOpts.EHContGuard) {
1080 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
1087 llvm::Metadata *Ops[2] = {
1088 llvm::MDString::get(VMContext,
"StrictVTablePointers"),
1089 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1090 llvm::Type::getInt32Ty(VMContext), 1))};
1092 getModule().addModuleFlag(llvm::Module::Require,
1093 "StrictVTablePointersRequirement",
1094 llvm::MDNode::get(VMContext, Ops));
1101 llvm::DEBUG_METADATA_VERSION);
1112 "zos_product_major_version",
1113 uint32_t(CLANG_VERSION_MAJOR));
1115 "zos_product_minor_version",
1116 uint32_t(CLANG_VERSION_MINOR));
1118 uint32_t(CLANG_VERSION_PATCHLEVEL));
1121 llvm::MDString::get(VMContext, ProductId));
1127 llvm::MDString::get(VMContext, lang_str));
1131 : std::time(
nullptr);
1132 getModule().addModuleFlag(llvm::Module::Max,
"zos_translation_time",
1137 llvm::MDString::get(VMContext,
"ascii"));
1141 if (
T.isARM() ||
T.isThumb()) {
1148 StringRef ABIStr =
Target.getABI();
1149 llvm::LLVMContext &Ctx = TheModule.getContext();
1151 llvm::MDString::get(Ctx, ABIStr));
1156 const std::vector<std::string> &Features =
1159 llvm::RISCVISAInfo::parseFeatures(
T.isRISCV64() ? 64 : 32, Features);
1160 if (!errorToBool(ParseResult.takeError()))
1162 llvm::Module::AppendUnique,
"riscv-isa",
1164 Ctx, llvm::MDString::get(Ctx, (*ParseResult)->toString())));
1167 if (CodeGenOpts.SanitizeCfiCrossDso) {
1169 getModule().addModuleFlag(llvm::Module::Override,
"Cross-DSO CFI", 1);
1172 if (CodeGenOpts.WholeProgramVTables) {
1177 CodeGenOpts.VirtualFunctionElimination);
1180 if (LangOpts.
Sanitize.
has(SanitizerKind::CFIICall)) {
1181 getModule().addModuleFlag(llvm::Module::Override,
1182 "CFI Canonical Jump Tables",
1183 CodeGenOpts.SanitizeCfiCanonicalJumpTables);
1187 getModule().addModuleFlag(llvm::Module::Override,
"kcfi", 1);
1190 if (CodeGenOpts.PatchableFunctionEntryOffset)
1191 getModule().addModuleFlag(llvm::Module::Override,
"kcfi-offset",
1192 CodeGenOpts.PatchableFunctionEntryOffset);
1195 if (CodeGenOpts.CFProtectionReturn &&
1198 getModule().addModuleFlag(llvm::Module::Min,
"cf-protection-return",
1202 if (CodeGenOpts.CFProtectionBranch &&
1205 getModule().addModuleFlag(llvm::Module::Min,
"cf-protection-branch",
1209 if (CodeGenOpts.FunctionReturnThunks)
1210 getModule().addModuleFlag(llvm::Module::Override,
"function_return_thunk_extern", 1);
1212 if (CodeGenOpts.IndirectBranchCSPrefix)
1213 getModule().addModuleFlag(llvm::Module::Override,
"indirect_branch_cs_prefix", 1);
1225 LangOpts.getSignReturnAddressScope() !=
1227 getModule().addModuleFlag(llvm::Module::Override,
1228 "sign-return-address-buildattr", 1);
1229 if (LangOpts.
Sanitize.
has(SanitizerKind::MemtagStack))
1230 getModule().addModuleFlag(llvm::Module::Override,
1231 "tag-stack-memory-buildattr", 1);
1233 if (
T.isARM() ||
T.isThumb() ||
T.isAArch64()) {
1234 if (LangOpts.BranchTargetEnforcement)
1235 getModule().addModuleFlag(llvm::Module::Min,
"branch-target-enforcement",
1237 if (LangOpts.BranchProtectionPAuthLR)
1238 getModule().addModuleFlag(llvm::Module::Min,
"branch-protection-pauth-lr",
1240 if (LangOpts.GuardedControlStack)
1241 getModule().addModuleFlag(llvm::Module::Min,
"guarded-control-stack", 1);
1243 getModule().addModuleFlag(llvm::Module::Min,
"sign-return-address", 1);
1245 getModule().addModuleFlag(llvm::Module::Min,
"sign-return-address-all",
1248 getModule().addModuleFlag(llvm::Module::Min,
1249 "sign-return-address-with-bkey", 1);
1253 using namespace llvm::ELF;
1255 (LangOpts.PointerAuthIntrinsics
1256 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INTRINSICS) |
1257 (LangOpts.PointerAuthCalls
1258 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_CALLS) |
1259 (LangOpts.PointerAuthReturns
1260 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_RETURNS) |
1261 (LangOpts.PointerAuthAuthTraps
1262 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_AUTHTRAPS) |
1263 (LangOpts.PointerAuthVTPtrAddressDiscrimination
1264 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR) |
1265 (LangOpts.PointerAuthVTPtrTypeDiscrimination
1266 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR) |
1267 (LangOpts.PointerAuthInitFini
1268 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI);
1269 static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI ==
1270 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST,
1271 "Update when new enum items are defined");
1272 if (PAuthABIVersion != 0) {
1274 "aarch64-elf-pauthabi-platform",
1275 AARCH64_PAUTH_PLATFORM_LLVM_LINUX);
1277 "aarch64-elf-pauthabi-version",
1283 if (CodeGenOpts.StackClashProtector)
1285 llvm::Module::Override,
"probe-stack",
1286 llvm::MDString::get(TheModule.getContext(),
"inline-asm"));
1288 if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
1289 getModule().addModuleFlag(llvm::Module::Min,
"stack-probe-size",
1290 CodeGenOpts.StackProbeSize);
1293 llvm::LLVMContext &Ctx = TheModule.getContext();
1299 if ((LangOpts.CUDAIsDevice || LangOpts.SYCLIsDevice) &&
getTriple().isNVPTX()) {
1304 llvm::Module::Max,
"nvvm-reflect-ftz",
1306 (CodeGenOpts.
FPDenormalMode.Output != llvm::DenormalMode::IEEE));
1307 getModule().addModuleFlag(llvm::Module::Max,
"nvvm-reflect-prec-sqrt",
1308 getTarget().getTargetOpts().NVVMCudaPrecSqrt);
1311 if (LangOpts.SYCLIsDevice) {
1313 if (LangOpts.SYCLIsNativeCPU)
1317 if (LangOpts.EHAsynch)
1322 getModule().addModuleFlag(llvm::Module::Max,
"openmp", LangOpts.OpenMP);
1324 getModule().addModuleFlag(llvm::Module::Max,
"openmp-device",
1328 if (LangOpts.OpenCL || (LangOpts.CUDAIsDevice &&
getTriple().isSPIRV())) {
1329 EmitOpenCLMetadata();
1337 llvm::Metadata *SPIRVerElts[] = {
1338 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1340 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1341 Int32Ty, (Version / 100 > 1) ? 0 : 2))};
1342 llvm::NamedMDNode *SPIRVerMD =
1343 TheModule.getOrInsertNamedMetadata(
"opencl.spir.version");
1344 llvm::LLVMContext &Ctx = TheModule.getContext();
1345 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1351 if (LangOpts.SYCLIsDevice) {
1352 llvm::LLVMContext &Ctx = TheModule.getContext();
1353 llvm::Metadata *SPIRVerElts[] = {
1354 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
Int32Ty, 1)),
1355 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
Int32Ty, 2))};
1356 llvm::NamedMDNode *SPIRVerMD =
1357 TheModule.getOrInsertNamedMetadata(
"opencl.spir.version");
1358 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1362 int Lang = llvm::any_of(TheModule,
1364 return F.getMetadata(
"sycl_explicit_simd");
1369 llvm::Metadata *SPIRVSourceElts[] = {
1370 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
Int32Ty,
Lang)),
1371 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
Int32Ty, 100000))};
1372 llvm::NamedMDNode *SPIRVSourceMD =
1373 TheModule.getOrInsertNamedMetadata(
"spirv.Source");
1374 SPIRVSourceMD->addOperand(llvm::MDNode::get(Ctx, SPIRVSourceElts));
1377 if (TypesWithAspects.size() > 0) {
1378 llvm::NamedMDNode *AspectsMD =
1379 TheModule.getOrInsertNamedMetadata(
"sycl_types_that_use_aspects");
1380 for (
const auto &
Type : TypesWithAspects) {
1381 StringRef Name =
Type.first;
1383 AspectsMD->addOperand(
getAspectsMD(Context, TheModule.getContext(),
1385 RD->
getAttr<SYCLUsesAspectsAttr>()));
1390 if (AspectsEnumDecl) {
1391 llvm::NamedMDNode *AspectEnumValsMD =
1392 TheModule.getOrInsertNamedMetadata(
"sycl_aspects");
1394 AspectEnumValsMD->addOperand(
1403 if (uint32_t PLevel = Context.
getLangOpts().PICLevel) {
1404 assert(PLevel < 3 &&
"Invalid PIC Level");
1411 unsigned CM = llvm::StringSwitch<unsigned>(
getCodeGenOpts().CodeModel)
1412 .Case(
"tiny", llvm::CodeModel::Tiny)
1413 .Case(
"small", llvm::CodeModel::Small)
1414 .Case(
"kernel", llvm::CodeModel::Kernel)
1415 .Case(
"medium", llvm::CodeModel::Medium)
1416 .Case(
"large", llvm::CodeModel::Large)
1419 llvm::CodeModel::Model codeModel =
static_cast<llvm::CodeModel::Model
>(CM);
1422 if ((CM == llvm::CodeModel::Medium || CM == llvm::CodeModel::Large) &&
1424 llvm::Triple::x86_64) {
1430 if (CodeGenOpts.NoPLT)
1433 CodeGenOpts.DirectAccessExternalData !=
1434 getModule().getDirectAccessExternalData()) {
1435 getModule().setDirectAccessExternalData(
1436 CodeGenOpts.DirectAccessExternalData);
1438 if (CodeGenOpts.UnwindTables)
1439 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables));
1441 switch (CodeGenOpts.getFramePointer()) {
1446 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf);
1453 SimplifyPersonality();
1466 EmitVersionIdentMetadata();
1469 EmitCommandLineMetadata();
1477 getModule().setStackProtectorGuardSymbol(
1480 getModule().setStackProtectorGuardOffset(
1485 getModule().addModuleFlag(llvm::Module::Override,
"SkipRaxSetup", 1);
1487 getModule().addModuleFlag(llvm::Module::Override,
"RegCallv4", 1);
1489 if (
getContext().getTargetInfo().getMaxTLSAlign())
1491 getContext().getTargetInfo().getMaxTLSAlign());
1509 void CodeGenModule::EmitOpenCLMetadata() {
1514 llvm::Metadata *OCLVerElts[] = {
1515 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1517 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1518 Int32Ty, (Version % 100) / 10))};
1519 llvm::NamedMDNode *OCLVerMD =
1520 TheModule.getOrInsertNamedMetadata(
"opencl.ocl.version");
1521 llvm::LLVMContext &Ctx = TheModule.getContext();
1522 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
1525 void CodeGenModule::EmitBackendOptionsMetadata(
1528 getModule().addModuleFlag(llvm::Module::Min,
"SmallDataLimit",
1529 CodeGenOpts.SmallDataLimit);
1546 return TBAA->getTypeInfo(QTy);
1565 return TBAA->getAccessInfo(AccessType);
1572 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1578 return TBAA->getTBAAStructInfo(QTy);
1584 return TBAA->getBaseTypeInfo(QTy);
1590 return TBAA->getAccessTagInfo(Info);
1597 return TBAA->mergeTBAAInfoForCast(SourceInfo,
TargetInfo);
1605 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1613 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1619 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1624 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1637 "cannot compile this %0 yet");
1638 std::string Msg =
Type;
1640 << Msg << S->getSourceRange();
1647 "cannot compile this %0 yet");
1648 std::string Msg =
Type;
1659 if (GV->hasLocalLinkage()) {
1674 Context.
getLangOpts().OpenMPIsTargetDevice && isa<VarDecl>(D) &&
1675 D->
hasAttr<OMPDeclareTargetDeclAttr>() &&
1676 D->
getAttr<OMPDeclareTargetDeclAttr>()->getDevType() !=
1677 OMPDeclareTargetDeclAttr::DT_NoHost &&
1683 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
1687 if (GV->hasDLLExportStorageClass()) {
1690 diag::err_hidden_visibility_dllexport);
1693 diag::err_non_default_visibility_dllimport);
1699 !GV->isDeclarationForLinker())
1704 llvm::GlobalValue *GV) {
1705 if (GV->hasLocalLinkage())
1708 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
1712 if (GV->hasDLLImportStorageClass())
1715 const llvm::Triple &TT = CGM.
getTriple();
1717 if (TT.isWindowsGNUEnvironment()) {
1726 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
1735 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
1743 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
1747 if (!TT.isOSBinFormatELF())
1753 if (RM != llvm::Reloc::Static && !LOpts.PIE) {
1759 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias()))
1761 return !(CGM.
getLangOpts().SemanticInterposition ||
1766 if (!GV->isDeclarationForLinker())
1772 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
1779 if (CGOpts.DirectAccessExternalData) {
1785 if (
auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
1786 if (!Var->isThreadLocal())
1795 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
1811 const auto *D = dyn_cast<NamedDecl>(GD.
getDecl());
1813 if (
const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1823 if (D->
hasAttr<DLLImportAttr>())
1824 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1825 else if ((D->
hasAttr<DLLExportAttr>() ||
1827 !GV->isDeclarationForLinker())
1828 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1852 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1853 .Case(
"global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1854 .Case(
"local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1855 .Case(
"initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1856 .Case(
"local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1859 llvm::GlobalVariable::ThreadLocalMode
1861 switch (CodeGenOpts.getDefaultTLSModel()) {
1863 return llvm::GlobalVariable::GeneralDynamicTLSModel;
1865 return llvm::GlobalVariable::LocalDynamicTLSModel;
1867 return llvm::GlobalVariable::InitialExecTLSModel;
1869 return llvm::GlobalVariable::LocalExecTLSModel;
1871 llvm_unreachable(
"Invalid TLS model!");
1875 assert(D.
getTLSKind() &&
"setting TLS mode on non-TLS var!");
1877 llvm::GlobalValue::ThreadLocalMode TLM;
1881 if (
const TLSModelAttr *
Attr = D.
getAttr<TLSModelAttr>()) {
1885 GV->setThreadLocalMode(TLM);
1891 return (Twine(
'.') + Twine(
Target.CPUSpecificManglingCharacter(Name))).str();
1895 const CPUSpecificAttr *
Attr,
1917 bool OmitMultiVersionMangling =
false) {
1919 llvm::raw_svector_ostream Out(Buffer);
1928 assert(II &&
"Attempt to mangle unnamed decl.");
1929 const auto *FD = dyn_cast<FunctionDecl>(ND);
1934 Out <<
"__regcall4__" << II->
getName();
1936 Out <<
"__regcall3__" << II->
getName();
1937 }
else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1939 Out <<
"__device_stub__" << II->
getName();
1955 "Hash computed when not explicitly requested");
1959 if (
const auto *FD = dyn_cast<FunctionDecl>(ND))
1960 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1961 switch (FD->getMultiVersionKind()) {
1965 FD->getAttr<CPUSpecificAttr>(),
1969 auto *
Attr = FD->getAttr<TargetAttr>();
1971 Info.appendAttributeMangling(
Attr, Out);
1975 auto *
Attr = FD->getAttr<TargetVersionAttr>();
1977 Info.appendAttributeMangling(
Attr, Out);
1981 auto *
Attr = FD->getAttr<TargetClonesAttr>();
1988 llvm_unreachable(
"None multiversion type isn't valid here");
1998 return std::string(Out.str());
2001 void CodeGenModule::UpdateMultiVersionNames(
GlobalDecl GD,
2003 StringRef &CurName) {
2010 std::string NonTargetName =
2018 "Other GD should now be a multiversioned function");
2028 if (OtherName != NonTargetName) {
2031 const auto ExistingRecord = Manglings.find(NonTargetName);
2032 if (ExistingRecord != std::end(Manglings))
2033 Manglings.remove(&(*ExistingRecord));
2034 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
2036 Result.first->first();
2039 CurName = OtherNameRef;
2041 Entry->setName(OtherName);
2051 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.
getDecl())) {
2065 auto FoundName = MangledDeclNames.find(CanonicalGD);
2066 if (FoundName != MangledDeclNames.end())
2067 return FoundName->second;
2071 const auto *ND = cast<NamedDecl>(GD.
getDecl());
2083 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
2094 auto Result = Manglings.insert(std::make_pair(MangledName, GD));
2095 return MangledDeclNames[CanonicalGD] = Result.first->first();
2104 llvm::raw_svector_ostream Out(Buffer);
2107 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.
getDecl()), Out);
2108 else if (
const auto *CD = dyn_cast<CXXConstructorDecl>(D))
2110 else if (
const auto *DD = dyn_cast<CXXDestructorDecl>(D))
2113 MangleCtx.
mangleBlock(cast<DeclContext>(D), BD, Out);
2115 auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
2116 return Result.first->first();
2120 auto it = MangledDeclNames.begin();
2121 while (it != MangledDeclNames.end()) {
2122 if (it->second == Name)
2137 llvm::Constant *AssociatedData) {
2145 bool IsDtorAttrFunc) {
2146 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
2148 DtorsUsingAtExit[
Priority].push_back(Dtor);
2156 void CodeGenModule::EmitCtorList(
CtorList &Fns,
const char *GlobalName) {
2157 if (Fns.empty())
return;
2160 llvm::FunctionType* CtorFTy = llvm::FunctionType::get(
VoidTy,
false);
2161 llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
2162 TheModule.getDataLayout().getProgramAddressSpace());
2163 llvm::PointerType *TargetType =
VoidPtrTy;
2168 TargetType = llvm::PointerType::get(
2172 llvm::StructType *CtorStructTy =
2173 llvm::StructType::get(
Int32Ty, CtorPFTy, TargetType);
2177 auto ctors = builder.beginArray(CtorStructTy);
2178 for (
const auto &I : Fns) {
2179 auto ctor = ctors.beginStruct(CtorStructTy);
2180 ctor.addInt(
Int32Ty, I.Priority);
2181 ctor.add(I.Initializer);
2182 if (I.AssociatedData)
2183 ctor.add(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2184 I.AssociatedData, TargetType));
2186 ctor.addNullPointer(TargetType);
2187 ctor.finishAndAddTo(ctors);
2193 llvm::GlobalValue::AppendingLinkage);
2197 list->setAlignment(std::nullopt);
2202 llvm::GlobalValue::LinkageTypes
2204 const auto *D = cast<FunctionDecl>(GD.
getDecl());
2208 if (
const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
2215 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
2216 if (!MDS)
return nullptr;
2218 return llvm::ConstantInt::get(
Int64Ty, llvm::MD5Hash(MDS->getString()));
2224 FnType->getReturnType(), FnType->getParamTypes(),
2225 FnType->getExtProtoInfo().withExceptionSpec(
EST_None));
2227 std::string OutName;
2228 llvm::raw_string_ostream Out(OutName);
2233 Out <<
".normalized";
2235 return llvm::ConstantInt::get(
Int32Ty,
2236 static_cast<uint32_t
>(llvm::xxHash64(OutName)));
2241 llvm::Function *F,
bool IsThunk) {
2243 llvm::AttributeList PAL;
2246 if (
CallingConv == llvm::CallingConv::X86_VectorCall &&
2250 Loc = D->getLocation();
2252 Error(
Loc,
"__vectorcall calling convention is not currently supported");
2254 F->setAttributes(PAL);
2259 std::string ReadOnlyQual(
"__read_only");
2260 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
2261 if (ReadOnlyPos != std::string::npos)
2263 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
2265 std::string WriteOnlyQual(
"__write_only");
2266 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
2267 if (WriteOnlyPos != std::string::npos)
2268 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
2270 std::string ReadWriteQual(
"__read_write");
2271 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
2272 if (ReadWritePos != std::string::npos)
2273 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
2310 assert(((FD && CGF) || (!FD && !CGF)) &&
2311 "Incorrect use - FD and CGF should either be both null or not!");
2343 bool isKernelArgAnAccessor =
false;
2346 for (
unsigned i = 0, e = FD->
getNumParams(); i != e; ++i) {
2349 argNames.push_back(llvm::MDString::get(VMContext, parm->
getName()));
2354 std::string typeQuals;
2358 const Decl *PDecl = parm;
2360 PDecl = TD->getDecl();
2361 const OpenCLAccessAttr *A = PDecl->
getAttr<OpenCLAccessAttr>();
2362 if (A && A->isWriteOnly())
2363 accessQuals.push_back(llvm::MDString::get(VMContext,
"write_only"));
2364 else if (A && A->isReadWrite())
2365 accessQuals.push_back(llvm::MDString::get(VMContext,
"read_write"));
2367 accessQuals.push_back(llvm::MDString::get(VMContext,
"read_only"));
2369 accessQuals.push_back(llvm::MDString::get(VMContext,
"none"));
2371 auto getTypeSpelling = [&](
QualType Ty) {
2372 auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
2374 if (Ty.isCanonical()) {
2375 StringRef typeNameRef = typeName;
2377 if (typeNameRef.consume_front(
"unsigned "))
2378 return std::string(
"u") + typeNameRef.str();
2379 if (typeNameRef.consume_front(
"signed "))
2380 return typeNameRef.str();
2390 addressQuals.push_back(
2391 llvm::ConstantAsMetadata::get(CGF->
Builder.getInt32(
2395 std::string typeName = getTypeSpelling(pointeeTy) +
"*";
2396 std::string baseTypeName =
2398 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2399 argBaseTypeNames.push_back(
2400 llvm::MDString::get(VMContext, baseTypeName));
2404 typeQuals =
"restrict";
2407 typeQuals += typeQuals.empty() ?
"const" :
" const";
2409 typeQuals += typeQuals.empty() ?
"volatile" :
" volatile";
2411 uint32_t AddrSpc = 0;
2416 addressQuals.push_back(
2417 llvm::ConstantAsMetadata::get(CGF->
Builder.getInt32(AddrSpc)));
2421 std::string typeName = getTypeSpelling(ty);
2433 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2434 argBaseTypeNames.push_back(
2435 llvm::MDString::get(VMContext, baseTypeName));
2440 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
2442 auto *SYCLBufferLocationAttr =
2443 parm->
getAttr<SYCLIntelBufferLocationAttr>();
2444 argSYCLBufferLocationAttr.push_back(
2445 (SYCLBufferLocationAttr)
2446 ? llvm::ConstantAsMetadata::get(CGF->
Builder.getInt32(
2447 SYCLBufferLocationAttr->getLocationID()))
2448 : llvm::ConstantAsMetadata::get(CGF->
Builder.getInt32(-1)));
2463 if (parm->
hasAttr<SYCLAccessorPtrAttr>()) {
2464 isKernelArgAnAccessor =
true;
2465 argSYCLAccessorPtrs.push_back(
2466 llvm::ConstantAsMetadata::get(CGF->
Builder.getTrue()));
2468 argSYCLAccessorPtrs.push_back(
2469 llvm::ConstantAsMetadata::get(CGF->
Builder.getFalse()));
2473 bool IsEsimdFunction = FD && FD->
hasAttr<SYCLSimdAttr>();
2475 if (LangOpts.SYCLIsDevice && !IsEsimdFunction) {
2476 Fn->setMetadata(
"kernel_arg_buffer_location",
2477 llvm::MDNode::get(VMContext, argSYCLBufferLocationAttr));
2480 if (isKernelArgAnAccessor) {
2481 Fn->setMetadata(
"kernel_arg_runtime_aligned",
2482 llvm::MDNode::get(VMContext, argSYCLAccessorPtrs));
2483 Fn->setMetadata(
"kernel_arg_exclusive_ptr",
2484 llvm::MDNode::get(VMContext, argSYCLAccessorPtrs));
2488 Fn->setMetadata(
"kernel_arg_addr_space",
2489 llvm::MDNode::get(VMContext, addressQuals));
2490 Fn->setMetadata(
"kernel_arg_access_qual",
2491 llvm::MDNode::get(VMContext, accessQuals));
2492 Fn->setMetadata(
"kernel_arg_type",
2493 llvm::MDNode::get(VMContext, argTypeNames));
2494 Fn->setMetadata(
"kernel_arg_base_type",
2495 llvm::MDNode::get(VMContext, argBaseTypeNames));
2496 Fn->setMetadata(
"kernel_arg_type_qual",
2497 llvm::MDNode::get(VMContext, argTypeQuals));
2498 if (IsEsimdFunction)
2499 Fn->setMetadata(
"kernel_arg_accessor_ptr",
2500 llvm::MDNode::get(VMContext, argSYCLAccessorPtrs));
2504 Fn->setMetadata(
"kernel_arg_name",
2505 llvm::MDNode::get(VMContext, argNames));
2516 if (!LangOpts.Exceptions)
return false;
2519 if (LangOpts.CXXExceptions)
return true;
2522 if (LangOpts.ObjCExceptions) {
2539 !isa<CXXConstructorDecl, CXXDestructorDecl>(MD);
2544 llvm::SetVector<const CXXRecordDecl *> MostBases;
2546 std::function<void (
const CXXRecordDecl *)> CollectMostBases;
2549 MostBases.insert(RD);
2551 CollectMostBases(B.getType()->getAsCXXRecordDecl());
2553 CollectMostBases(RD);
2554 return MostBases.takeVector();
2561 const auto *ND = dyn_cast<NamespaceDecl>(DC);
2566 if (!isa<NamespaceDecl>(
Parent))
2568 ND = cast<NamespaceDecl>(
Parent);
2571 return ND && ND->getName() ==
"sycl";
2575 llvm::Function *F) {
2576 llvm::AttrBuilder B(F->getContext());
2578 if ((!D || !D->
hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables)
2579 B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
2581 if (CodeGenOpts.StackClashProtector)
2582 B.addAttribute(
"probe-stack",
"inline-asm");
2584 if (CodeGenOpts.StackProbeSize && CodeGenOpts.StackProbeSize != 4096)
2585 B.addAttribute(
"stack-probe-size",
2586 std::to_string(CodeGenOpts.StackProbeSize));
2589 B.addAttribute(llvm::Attribute::NoUnwind);
2591 if (D && D->
hasAttr<NoStackProtectorAttr>())
2593 else if (D && D->
hasAttr<StrictGuardStackCheckAttr>() &&
2595 B.addAttribute(llvm::Attribute::StackProtectStrong);
2597 B.addAttribute(llvm::Attribute::StackProtect);
2599 B.addAttribute(llvm::Attribute::StackProtectStrong);
2601 B.addAttribute(llvm::Attribute::StackProtectReq);
2607 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
2609 B.addAttribute(llvm::Attribute::NoInline);
2617 if (D->
hasAttr<ArmLocallyStreamingAttr>())
2618 B.addAttribute(
"aarch64_pstate_sm_body");
2621 if (
Attr->isNewZA())
2622 B.addAttribute(
"aarch64_new_za");
2623 if (
Attr->isNewZT0())
2624 B.addAttribute(
"aarch64_new_zt0");
2629 bool ShouldAddOptNone =
2630 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
2632 ShouldAddOptNone &= !D->
hasAttr<MinSizeAttr>();
2633 ShouldAddOptNone &= !D->
hasAttr<AlwaysInlineAttr>();
2636 if ((ShouldAddOptNone || D->
hasAttr<OptimizeNoneAttr>()) &&
2637 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2638 B.addAttribute(llvm::Attribute::OptimizeNone);
2641 B.addAttribute(llvm::Attribute::NoInline);
2646 B.addAttribute(llvm::Attribute::Naked);
2649 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2650 F->removeFnAttr(llvm::Attribute::MinSize);
2651 }
else if (D->
hasAttr<NakedAttr>()) {
2653 B.addAttribute(llvm::Attribute::Naked);
2654 B.addAttribute(llvm::Attribute::NoInline);
2655 }
else if (D->
hasAttr<NoDuplicateAttr>()) {
2656 B.addAttribute(llvm::Attribute::NoDuplicate);
2657 }
else if (D->
hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2659 B.addAttribute(llvm::Attribute::NoInline);
2660 }
else if (D->
hasAttr<AlwaysInlineAttr>() &&
2661 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2663 B.addAttribute(llvm::Attribute::AlwaysInline);
2667 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2668 B.addAttribute(llvm::Attribute::NoInline);
2672 if (
auto *FD = dyn_cast<FunctionDecl>(D)) {
2675 auto CheckRedeclForInline = [](
const FunctionDecl *Redecl) {
2676 return Redecl->isInlineSpecified();
2678 if (any_of(FD->
redecls(), CheckRedeclForInline))
2683 return any_of(Pattern->
redecls(), CheckRedeclForInline);
2685 if (CheckForInline(FD)) {
2686 B.addAttribute(llvm::Attribute::InlineHint);
2687 }
else if (CodeGenOpts.getInlining() ==
2690 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2691 B.addAttribute(llvm::Attribute::NoInline);
2698 if (!D->
hasAttr<OptimizeNoneAttr>()) {
2700 if (!ShouldAddOptNone)
2701 B.addAttribute(llvm::Attribute::OptimizeForSize);
2702 B.addAttribute(llvm::Attribute::Cold);
2705 B.addAttribute(llvm::Attribute::Hot);
2706 if (D->
hasAttr<MinSizeAttr>())
2707 B.addAttribute(llvm::Attribute::MinSize);
2714 F->removeFnAttr(llvm::Attribute::OptimizeNone);
2715 F->removeFnAttr(llvm::Attribute::NoInline);
2720 F->setAlignment(llvm::Align(alignment));
2722 if (!D->
hasAttr<AlignedAttr>())
2723 if (LangOpts.FunctionAlignment)
2724 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2731 if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(
getDataLayout()) < 2)
2732 F->setAlignment(
std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2737 if (CodeGenOpts.SanitizeCfiCrossDso &&
2738 CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
2739 if (
auto *FD = dyn_cast<FunctionDecl>(D)) {
2750 auto *MD = dyn_cast<CXXMethodDecl>(D);
2753 llvm::Metadata *
Id =
2756 F->addTypeMetadata(0,
Id);
2763 if (isa_and_nonnull<NamedDecl>(D))
2768 if (D && D->
hasAttr<UsedAttr>())
2771 if (
const auto *VD = dyn_cast_if_present<VarDecl>(D);
2773 ((CodeGenOpts.KeepPersistentStorageVariables &&
2774 (VD->getStorageDuration() ==
SD_Static ||
2775 VD->getStorageDuration() ==
SD_Thread)) ||
2776 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() ==
SD_Static &&
2777 VD->getType().isConstQualified())))
2784 if (D && isa<VarDecl>(D)) {
2785 const auto *VD = cast<VarDecl>(D);
2786 const RecordDecl *RD = VD->getType()->getAsRecordDecl();
2787 if (RD && RD->
hasAttr<SYCLDeviceGlobalAttr>() &&
2794 bool CodeGenModule::GetCPUAndFeaturesAttributes(
GlobalDecl GD,
2795 llvm::AttrBuilder &Attrs,
2796 bool SetTargetFeatures) {
2802 std::vector<std::string> Features;
2803 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.
getDecl());
2805 const auto *TD = FD ? FD->
getAttr<TargetAttr>() :
nullptr;
2806 const auto *TV = FD ? FD->
getAttr<TargetVersionAttr>() :
nullptr;
2807 assert((!TD || !TV) &&
"both target_version and target specified");
2808 const auto *SD = FD ? FD->
getAttr<CPUSpecificAttr>() :
nullptr;
2809 const auto *TC = FD ? FD->
getAttr<TargetClonesAttr>() :
nullptr;
2810 bool AddedAttr =
false;
2811 if (TD || TV || SD || TC) {
2812 llvm::StringMap<bool> FeatureMap;
2816 for (
const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2817 Features.push_back((Entry.getValue() ?
"+" :
"-") + Entry.getKey().str());
2825 Target.parseTargetAttr(TD->getFeaturesStr());
2847 if (!TargetCPU.empty()) {
2848 Attrs.addAttribute(
"target-cpu", TargetCPU);
2851 if (!TuneCPU.empty()) {
2852 Attrs.addAttribute(
"tune-cpu", TuneCPU);
2855 if (!Features.empty() && SetTargetFeatures) {
2856 llvm::erase_if(Features, [&](
const std::string& F) {
2859 llvm::sort(Features);
2860 Attrs.addAttribute(
"target-features", llvm::join(Features,
","));
2867 void CodeGenModule::setNonAliasAttributes(
GlobalDecl GD,
2868 llvm::GlobalObject *GO) {
2873 if (
auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2876 if (
auto *SA = D->
getAttr<PragmaClangBSSSectionAttr>())
2877 GV->addAttribute(
"bss-section", SA->getName());
2878 if (
auto *SA = D->
getAttr<PragmaClangDataSectionAttr>())
2879 GV->addAttribute(
"data-section", SA->getName());
2880 if (
auto *SA = D->
getAttr<PragmaClangRodataSectionAttr>())
2881 GV->addAttribute(
"rodata-section", SA->getName());
2882 if (
auto *SA = D->
getAttr<PragmaClangRelroSectionAttr>())
2883 GV->addAttribute(
"relro-section", SA->getName());
2886 if (
auto *F = dyn_cast<llvm::Function>(GO)) {
2889 if (
auto *SA = D->
getAttr<PragmaClangTextSectionAttr>())
2890 if (!D->
getAttr<SectionAttr>())
2891 F->setSection(SA->getName());
2893 llvm::AttrBuilder Attrs(F->getContext());
2894 if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2898 llvm::AttributeMask RemoveAttrs;
2899 RemoveAttrs.addAttribute(
"target-cpu");
2900 RemoveAttrs.addAttribute(
"target-features");
2901 RemoveAttrs.addAttribute(
"tune-cpu");
2902 F->removeFnAttrs(RemoveAttrs);
2903 F->addFnAttrs(Attrs);
2907 if (
const auto *CSA = D->
getAttr<CodeSegAttr>())
2908 GO->setSection(CSA->getName());
2909 else if (
const auto *SA = D->
getAttr<SectionAttr>())
2910 GO->setSection(SA->getName());
2923 F->setLinkage(llvm::Function::InternalLinkage);
2925 setNonAliasAttributes(GD, F);
2936 GV->
setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2940 llvm::Function *F) {
2942 if (!LangOpts.
Sanitize.
has(SanitizerKind::CFIICall))
2947 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2951 F->addTypeMetadata(0, MD);
2955 if (CodeGenOpts.SanitizeCfiCrossDso)
2957 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2961 llvm::LLVMContext &Ctx = F->getContext();
2962 llvm::MDBuilder MDB(Ctx);
2963 F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2972 return llvm::all_of(Name, [](
const char &C) {
2973 return llvm::isAlnum(C) || C ==
'_' || C ==
'.';
2979 for (
auto &F : M.functions()) {
2981 bool AddressTaken = F.hasAddressTaken();
2982 if (!AddressTaken && F.hasLocalLinkage())
2983 F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
2988 if (!AddressTaken || !F.isDeclaration())
2991 const llvm::ConstantInt *
Type;
2992 if (
const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
2993 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2997 StringRef Name = F.getName();
3001 std::string
Asm = (
".weak __kcfi_typeid_" + Name +
"\n.set __kcfi_typeid_" +
3002 Name +
", " + Twine(
Type->getZExtValue()) +
"\n")
3004 M.appendModuleInlineAsm(
Asm);
3008 template <
typename AttrT>
3010 llvm::Function *F, StringRef MDName) {
3012 for (
auto *Aspect : A->aspects()) {
3013 llvm::APSInt AspectInt = Aspect->EvaluateKnownConstInt(ACtx);
3014 auto *
T = llvm::Type::getInt32Ty(LLVMCtx);
3015 auto *C = llvm::Constant::getIntegerValue(
T, AspectInt);
3016 AspectsMD.push_back(llvm::ConstantAsMetadata::get(C));
3018 F->setMetadata(MDName, llvm::MDNode::get(LLVMCtx, AspectsMD));
3021 void CodeGenModule::SetFunctionAttributes(
GlobalDecl GD, llvm::Function *F,
3022 bool IsIncompleteFunction,
3028 F->setAttributes(llvm::Intrinsic::getAttributes(
getLLVMContext(), IID));
3032 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
3034 if (!IsIncompleteFunction)
3041 if (!IsThunk &&
getCXXABI().HasThisReturn(GD) &&
3043 assert(!F->arg_empty() &&
3044 F->arg_begin()->getType()
3045 ->canLosslesslyBitCastTo(F->getReturnType()) &&
3046 "unexpected this return");
3047 F->addParamAttr(0, llvm::Attribute::Returned);
3057 if (!IsIncompleteFunction && F->isDeclaration())
3060 if (
const auto *CSA = FD->
getAttr<CodeSegAttr>())
3061 F->setSection(CSA->getName());
3062 else if (
const auto *SA = FD->
getAttr<SectionAttr>())
3063 F->setSection(SA->getName());
3065 if (
const auto *EA = FD->
getAttr<ErrorAttr>()) {
3067 F->addFnAttr(
"dontcall-error", EA->getUserDiagnostic());
3068 else if (EA->isWarning())
3069 F->addFnAttr(
"dontcall-warn", EA->getUserDiagnostic());
3075 bool HasBody = FD->
hasBody(FDBody);
3077 assert(HasBody &&
"Inline builtin declarations should always have an "
3079 if (shouldEmitFunction(FDBody))
3080 F->addFnAttr(llvm::Attribute::NoBuiltin);
3086 F->addFnAttr(llvm::Attribute::NoBuiltin);
3089 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
3090 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3091 else if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD))
3092 if (MD->isVirtual())
3093 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3099 if (!CodeGenOpts.SanitizeCfiCrossDso ||
3100 !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
3109 if (CodeGenOpts.InlineMaxStackSize !=
UINT_MAX)
3110 F->addFnAttr(
"inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
3112 if (
const auto *CB = FD->
getAttr<CallbackAttr>()) {
3116 llvm::LLVMContext &Ctx = F->getContext();
3117 llvm::MDBuilder MDB(Ctx);
3121 int CalleeIdx = *CB->encoding_begin();
3122 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
3123 F->addMetadata(llvm::LLVMContext::MD_callback,
3124 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
3125 CalleeIdx, PayloadIndices,
3130 if (
const auto *A = FD->
getAttr<SYCLDeviceHasAttr>())
3132 "sycl_declared_aspects");
3134 if (
const auto *A = FD->
getAttr<SYCLUsesAspectsAttr>())
3136 "sycl_used_aspects");
3140 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
3141 "Only globals with definition can force usage.");
3142 LLVMUsed.emplace_back(GV);
3146 assert(!GV->isDeclaration() &&
3147 "Only globals with definition can force usage.");
3148 LLVMCompilerUsed.emplace_back(GV);
3152 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
3153 "Only globals with definition can force usage.");
3155 LLVMCompilerUsed.emplace_back(GV);
3157 LLVMUsed.emplace_back(GV);
3161 std::vector<llvm::WeakTrackingVH> &List) {
3168 llvm::PointerType *TargetType = CGM.
Int8PtrTy;
3170 TargetType = llvm::PointerType::get(
3176 UsedArray.resize(List.size());
3177 for (
unsigned i = 0, e = List.size(); i != e; ++i) {
3178 UsedArray[i] = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
3179 cast<llvm::Constant>(&*List[i]), TargetType);
3182 if (UsedArray.empty())
3184 llvm::ArrayType *ATy = llvm::ArrayType::get(TargetType, UsedArray.size());
3186 auto *GV =
new llvm::GlobalVariable(
3187 CGM.
getModule(), ATy,
false, llvm::GlobalValue::AppendingLinkage,
3188 llvm::ConstantArray::get(ATy, UsedArray), Name);
3190 GV->setSection(
"llvm.metadata");
3193 void CodeGenModule::emitLLVMUsed() {
3194 emitUsed(*
this,
"llvm.used", LLVMUsed);
3195 emitUsed(*
this,
"llvm.compiler.used", LLVMCompilerUsed);
3200 LinkerOptionsMetadata.push_back(llvm::MDNode::get(
getLLVMContext(), MDOpts));
3209 LinkerOptionsMetadata.push_back(llvm::MDNode::get(
getLLVMContext(), MDOpts));
3215 ELFDependentLibraries.push_back(
3216 llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
3223 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
3238 if (
Visited.insert(Import).second)
3252 for (
const Module::LinkLibrary &LL : llvm::reverse(Mod->
LinkLibraries)) {
3255 if (LL.IsFramework) {
3256 llvm::Metadata *Args[2] = {llvm::MDString::get(Context,
"-framework"),
3257 llvm::MDString::get(Context, LL.Library)};
3259 Metadata.push_back(llvm::MDNode::get(Context, Args));
3265 llvm::Metadata *Args[2] = {
3266 llvm::MDString::get(Context,
"lib"),
3267 llvm::MDString::get(Context, LL.Library),
3269 Metadata.push_back(llvm::MDNode::get(Context, Args));
3273 auto *OptString = llvm::MDString::get(Context, Opt);
3274 Metadata.push_back(llvm::MDNode::get(Context, OptString));
3279 void CodeGenModule::EmitModuleInitializers(
clang::Module *Primary) {
3281 "We should only emit module initializers for named modules.");
3287 if (isa<ImportDecl>(D))
3289 assert(isa<VarDecl>(D) &&
"GMF initializer decl is not a var?");
3296 if (isa<ImportDecl>(D))
3304 if (isa<ImportDecl>(D))
3306 assert(isa<VarDecl>(D) &&
"PMF initializer decl is not a var?");
3312 void CodeGenModule::EmitModuleLinkOptions() {
3316 llvm::SetVector<clang::Module *> LinkModules;
3321 for (
Module *M : ImportedModules) {
3324 if (M->getTopLevelModuleName() ==
getLangOpts().CurrentModule &&
3333 while (!Stack.empty()) {
3336 bool AnyChildren =
false;
3346 Stack.push_back(
SM);
3354 LinkModules.insert(Mod);
3363 for (
Module *M : LinkModules)
3366 std::reverse(MetadataArgs.begin(), MetadataArgs.end());
3367 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
3370 auto *NMD =
getModule().getOrInsertNamedMetadata(
"llvm.linker.options");
3371 for (
auto *MD : LinkerOptionsMetadata)
3372 NMD->addOperand(MD);
3375 void CodeGenModule::EmitDeferred() {
3384 if (!DeferredVTables.empty()) {
3385 EmitDeferredVTables();
3390 assert(DeferredVTables.empty());
3397 llvm::append_range(DeferredDeclsToEmit,
3401 if (DeferredDeclsToEmit.empty())
3406 std::vector<GlobalDecl> CurDeclsToEmit;
3407 CurDeclsToEmit.swap(DeferredDeclsToEmit);
3430 const ValueDecl *VD = cast<ValueDecl>(D.getDecl());
3433 if (LangOpts.SYCLIsDevice) {
3435 StringRef AliaseeName =
Attr->getAliasee();
3436 auto DDI = DeferredDecls.find(AliaseeName);
3438 if (DDI != DeferredDecls.end()) {
3440 llvm::GlobalValue *AliaseeGV =
3445 EmitGlobalDefinition(GD, AliaseeGV);
3448 DeferredDeclsToEmit.pop_back();
3451 EmitAliasDefinition(D);
3459 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
3477 if (!GV->isDeclaration())
3481 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3485 EmitGlobalDefinition(D, GV);
3487 if (LangOpts.SYCLIsDevice) {
3489 for (
auto It = DeferredAliases.begin(); It != DeferredAliases.end();
3493 EmitAliasDefinition(Global);
3494 It = DeferredAliases.erase(It);
3504 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
3506 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
3511 void CodeGenModule::EmitVTablesOpportunistically() {
3517 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
3518 &&
"Only emit opportunistic vtables with optimizations");
3522 "This queue should only contain external vtables");
3523 if (
getCXXABI().canSpeculativelyEmitVTable(RD))
3526 OpportunisticVTables.clear();
3530 for (
const auto& [MangledName, VD] : DeferredAnnotations) {
3535 DeferredAnnotations.clear();
3537 if (Annotations.empty())
3541 llvm::Constant *
Array = llvm::ConstantArray::get(llvm::ArrayType::get(
3542 Annotations[0]->getType(), Annotations.size()), Annotations);
3543 auto *gv =
new llvm::GlobalVariable(
getModule(),
Array->getType(),
false,
3544 llvm::GlobalValue::AppendingLinkage,
3545 Array,
"llvm.global.annotations");
3550 llvm::Constant *&AStr = AnnotationStrings[Str];
3555 llvm::Constant *
s = llvm::ConstantDataArray::getString(
getLLVMContext(), Str);
3556 auto *gv =
new llvm::GlobalVariable(
3557 getModule(),
s->getType(),
true, llvm::GlobalValue::PrivateLinkage,
s,
3558 ".str",
nullptr, llvm::GlobalValue::NotThreadLocal,
3561 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3578 SM.getExpansionLineNumber(L);
3579 return llvm::ConstantInt::get(
Int32Ty, LineNo);
3587 llvm::FoldingSetNodeID
ID;
3588 for (
Expr *E : Exprs) {
3589 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3591 llvm::Constant *&Lookup = AnnotationArgs[
ID.ComputeHash()];
3596 LLVMArgs.reserve(Exprs.size());
3598 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](
const Expr *E) {
3599 const auto *CE = cast<clang::ConstantExpr>(E);
3600 return ConstEmiter.
emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3603 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3604 auto *GV =
new llvm::GlobalVariable(
getModule(), Struct->getType(),
true,
3605 llvm::GlobalValue::PrivateLinkage, Struct,
3608 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3615 const AnnotateAttr *AA,
3623 llvm::Constant *GVInGlobalsAS = GV;
3624 if (GV->getAddressSpace() !=
3626 GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
3628 llvm::PointerType::get(
3629 GV->getContext(),
getDataLayout().getDefaultGlobalsAddressSpace()));
3633 llvm::Constant *Fields[] = {
3634 GVInGlobalsAS, AnnoGV, UnitGV, LineNoCst, Args,
3636 return llvm::ConstantStruct::getAnon(Fields);
3640 llvm::GlobalValue *GV) {
3641 assert(D->
hasAttr<AnnotateAttr>() &&
"no annotate attribute");
3649 &AnnotationNameValPairs) {
3651 if (AnnotationNameValPairs.empty())
3658 llvm::FoldingSetNodeID
ID;
3659 LLVMArgs.reserve(AnnotationNameValPairs.size() * 2);
3660 for (
const std::pair<std::string, std::string> &NVP :
3661 AnnotationNameValPairs) {
3663 llvm::Constant *ValueStrC =
3666 LLVMArgs.push_back(NameStrC);
3667 LLVMArgs.push_back(ValueStrC);
3674 llvm::Constant *&LookupRef = SYCLAnnotationArgs[
ID.ComputeHash()];
3681 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3682 auto *GV =
new llvm::GlobalVariable(
3683 getModule(), Struct->getType(),
true, llvm::GlobalValue::PrivateLinkage,
3684 Struct,
".args",
nullptr, llvm::GlobalValue::NotThreadLocal,
3687 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3692 LookupRef = Bitcasted;
3698 const auto *A = RD->
getAttr<SYCLAddIRAttributesGlobalVariableAttr>();
3699 assert(A &&
"no add_ir_attributes_global_variable attribute");
3701 A->getFilteredAttributeNameValuePairs(Context);
3702 for (
const std::pair<std::string, std::string> &NameValuePair :
3704 GV->addAttribute(NameValuePair.first, NameValuePair.second);
3713 GV->addAttribute(
"sycl-unique-id", builtinString);
3720 if (NoSanitizeL.containsFunction(
Kind, Fn->getName()))
3725 if (NoSanitizeL.containsMainFile(
Kind, MainFile.
getName()))
3730 return NoSanitizeL.containsLocation(
Kind,
Loc);
3733 return NoSanitizeL.containsFile(
Kind, MainFile.
getName());
3737 llvm::GlobalVariable *GV,
3739 StringRef Category)
const {
3741 if (NoSanitizeL.containsGlobal(
Kind, GV->getName(),
Category))
3744 if (NoSanitizeL.containsMainFile(
3745 Kind,
SM.getFileEntryRefForID(
SM.getMainFileID())->getName(),
3755 while (
auto AT = dyn_cast<ArrayType>(Ty.
getTypePtr()))
3756 Ty = AT->getElementType();
3769 StringRef Category)
const {
3772 auto Attr = ImbueAttr::NONE;
3775 if (
Attr == ImbueAttr::NONE)
3776 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3778 case ImbueAttr::NONE:
3780 case ImbueAttr::ALWAYS:
3781 Fn->addFnAttr(
"function-instrument",
"xray-always");
3783 case ImbueAttr::ALWAYS_ARG1:
3784 Fn->addFnAttr(
"function-instrument",
"xray-always");
3785 Fn->addFnAttr(
"xray-log-args",
"1");
3787 case ImbueAttr::NEVER:
3788 Fn->addFnAttr(
"function-instrument",
"xray-never");
3812 if (
auto MainFile =
SM.getFileEntryRefForID(
SM.getMainFileID()))
3826 if (NumGroups > 1) {
3827 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3834 bool CodeGenModule::MustBeEmitted(
const ValueDecl *Global) {
3836 if (LangOpts.EmitAllDecls)
3839 const auto *VD = dyn_cast<VarDecl>(Global);
3841 ((CodeGenOpts.KeepPersistentStorageVariables &&
3842 (VD->getStorageDuration() ==
SD_Static ||
3843 VD->getStorageDuration() ==
SD_Thread)) ||
3844 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() ==
SD_Static &&
3851 bool CodeGenModule::MayBeEmittedEagerly(
const ValueDecl *Global) {
3858 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) {
3859 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
3860 OMPDeclareTargetDeclAttr::getActiveAttr(Global);
3861 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (
unsigned)-1)
3865 if (
const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3874 if (
const auto *VD = dyn_cast<VarDecl>(Global)) {
3890 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
3891 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
3893 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
3906 if (llvm::GlobalVariable *GV =
getModule().getNamedGlobal(Name))
3910 llvm::Constant *Init;
3913 if (!
V.isAbsent()) {
3916 Init =
Emitter.emitForInitializer(
3924 llvm::Constant *Fields[4] = {
3928 llvm::ConstantDataArray::getRaw(
3929 StringRef(
reinterpret_cast<char *
>(Parts.
Part4And5), 8), 8,
3931 Init = llvm::ConstantStruct::getAnon(Fields);
3934 auto *GV =
new llvm::GlobalVariable(
3936 true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3938 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3941 if (!
V.isAbsent()) {
3954 llvm::GlobalVariable **Entry =
nullptr;
3955 Entry = &UnnamedGlobalConstantDeclMap[GCD];
3960 llvm::Constant *Init;
3964 assert(!
V.isAbsent());
3968 auto *GV =
new llvm::GlobalVariable(
getModule(), Init->getType(),
3970 llvm::GlobalValue::PrivateLinkage, Init,
3972 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3986 if (llvm::GlobalVariable *GV =
getModule().getNamedGlobal(Name))
3990 llvm::Constant *Init =
Emitter.emitForInitializer(
3998 llvm::GlobalValue::LinkageTypes
Linkage =
4000 ? llvm::GlobalValue::LinkOnceODRLinkage
4001 : llvm::GlobalValue::InternalLinkage;
4002 auto *GV =
new llvm::GlobalVariable(
getModule(), Init->getType(),
4006 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
4013 const AliasAttr *AA = VD->
getAttr<AliasAttr>();
4014 assert(AA &&
"No alias?");
4024 llvm::Constant *Aliasee;
4025 if (isa<llvm::FunctionType>(
DeclTy))
4026 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(),
DeclTy,
4033 auto *F = cast<llvm::GlobalValue>(Aliasee);
4034 F->setLinkage(llvm::Function::ExternalWeakLinkage);
4035 WeakRefReferences.insert(F);
4043 if (
auto *A = D->
getAttr<AttrT>())
4044 return A->isImplicit();
4052 if (
Global->hasAttr<WeakRefAttr>())
4057 if (AliasAttr *
Attr =
Global->getAttr<AliasAttr>()) {
4059 if (!LangOpts.SYCLIsDevice)
4060 return EmitAliasDefinition(GD);
4064 DeferredDecls[MangledName] = GD;
4065 StringRef AliaseeName =
Attr->getAliasee();
4066 DeferredAliases[AliaseeName] = GD;
4071 if (
Global->hasAttr<IFuncAttr>())
4072 return emitIFuncDefinition(GD);
4075 if (
Global->hasAttr<CPUDispatchAttr>())
4076 return emitCPUDispatchDefinition(GD);
4081 if (LangOpts.CUDA) {
4082 if (LangOpts.CUDAIsDevice) {
4083 const auto *FD = dyn_cast<FunctionDecl>(
Global);
4084 if ((!
Global->hasAttr<CUDADeviceAttr>() ||
4085 (LangOpts.OffloadImplicitHostDeviceTemplates && FD &&
4086 hasImplicitAttr<CUDAHostAttr>(FD) &&
4087 hasImplicitAttr<CUDADeviceAttr>(FD) && !FD->
isConstexpr() &&
4089 !
getContext().CUDAImplicitHostDeviceFunUsedByDevice.count(FD))) &&
4090 !
Global->hasAttr<CUDAGlobalAttr>() &&
4091 !
Global->hasAttr<CUDAConstantAttr>() &&
4092 !
Global->hasAttr<CUDASharedAttr>() &&
4093 !
Global->getType()->isCUDADeviceBuiltinSurfaceType() &&
4094 !
Global->getType()->isCUDADeviceBuiltinTextureType() &&
4095 !(LangOpts.HIPStdPar && isa<FunctionDecl>(
Global) &&
4096 !
Global->hasAttr<CUDAHostAttr>()))
4106 !
Global->hasAttr<CUDAHostAttr>() &&
Global->hasAttr<CUDADeviceAttr>())
4109 assert((isa<FunctionDecl>(
Global) || isa<VarDecl>(
Global)) &&
4110 "Expected Variable or Function");
4114 if (LangOpts.OpenMP) {
4116 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
4118 if (
auto *DRD = dyn_cast<OMPDeclareReductionDecl>(
Global)) {
4119 if (MustBeEmitted(
Global))
4123 if (
auto *DMD = dyn_cast<OMPDeclareMapperDecl>(
Global)) {
4124 if (MustBeEmitted(
Global))
4131 if (
const auto *FD = dyn_cast<FunctionDecl>(
Global)) {
4134 if (FD->
hasAttr<AnnotateAttr>()) {
4137 DeferredAnnotations[MangledName] = FD;
4153 GetOrCreateLLVMFunction(MangledName, Ty, GD,
false,
4158 const auto *VD = cast<VarDecl>(
Global);
4159 assert(VD->isFileVarDecl() &&
"Cannot emit local var decl as global.");
4162 if (LangOpts.OpenMP) {
4164 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
4165 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
4169 if (VD->hasExternalStorage() &&
4170 Res != OMPDeclareTargetDeclAttr::MT_Link)
4173 bool UnifiedMemoryEnabled =
4175 if ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
4176 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
4177 !UnifiedMemoryEnabled) {
4180 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
4181 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
4182 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
4183 UnifiedMemoryEnabled)) &&
4184 "Link clause or to clause with unified memory expected.");
4202 if (LangOpts.SYCLIsDevice && MustBeEmitted(
Global)) {
4203 addDeferredDeclToEmit(GD);
4210 if (MustBeEmitted(
Global) && MayBeEmittedEagerly(
Global)) {
4214 !
Global->hasAttr<CUDAHostAttr>() &&
Global->hasAttr<CUDADeviceAttr>()) {
4215 addDeferredDeclToEmit(GD);
4220 Global->hasAttr<CUDAHostAttr>() && !
Global->hasAttr<CUDADeviceAttr>()) {
4221 addDeferredDeclToEmit(GD);
4226 EmitGlobalDefinition(GD);
4227 addEmittedDeferredDecl(GD);
4234 cast<VarDecl>(
Global)->hasInit()) {
4235 DelayedCXXInitPosition[
Global] = CXXGlobalInits.size();
4236 CXXGlobalInits.push_back(
nullptr);
4242 addDeferredDeclToEmit(GD);
4243 }
else if (MustBeEmitted(
Global)) {
4245 assert(!MayBeEmittedEagerly(
Global));
4246 addDeferredDeclToEmit(GD);
4250 if (LangOpts.
isSYCL() && LangOpts.CUDA && !LangOpts.CUDAIsDevice) {
4252 if (LangOpts.SYCLIsHost) {
4253 if (
Global->hasAttr<CUDAHostAttr>()) {
4255 auto DDI = DeferredDecls.find(MangledName);
4256 if (DDI != DeferredDecls.end())
4257 DeferredDecls.erase(DDI);
4258 }
else if (
Global->hasAttr<CUDADeviceAttr>()) {
4260 auto DDI = DeferredDecls.find(MangledName);
4261 if (DDI != DeferredDecls.end())
4266 if (LangOpts.SYCLIsDevice) {
4267 if (
Global->hasAttr<CUDADeviceAttr>()) {
4269 auto DDI = DeferredDecls.find(MangledName);
4270 if (DDI != DeferredDecls.end())
4271 DeferredDecls.erase(DDI);
4272 }
else if (
Global->hasAttr<CUDAHostAttr>()) {
4274 auto DDI = DeferredDecls.find(MangledName);
4275 if (DDI != DeferredDecls.end())
4284 DeferredDecls[MangledName] = GD;
4291 if (
CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
4292 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
4299 struct FunctionIsDirectlyRecursive
4301 const StringRef Name;
4306 bool VisitCallExpr(
const CallExpr *E) {
4311 if (
Attr && Name ==
Attr->getLabel())
4316 StringRef BuiltinName = BI.
getName(BuiltinID);
4317 if (BuiltinName.starts_with(
"__builtin_") &&
4318 Name == BuiltinName.slice(strlen(
"__builtin_"), StringRef::npos)) {
4324 bool VisitStmt(
const Stmt *S) {
4325 for (
const Stmt *Child : S->children())
4326 if (Child && this->Visit(Child))
4333 struct DLLImportFunctionVisitor
4335 bool SafeToInline =
true;
4337 bool shouldVisitImplicitCode()
const {
return true; }
4339 bool VisitVarDecl(
VarDecl *VD) {
4342 SafeToInline =
false;
4343 return SafeToInline;
4350 return SafeToInline;
4355 SafeToInline = D->
hasAttr<DLLImportAttr>();
4356 return SafeToInline;
4361 if (isa<FunctionDecl>(VD))
4362 SafeToInline = VD->
hasAttr<DLLImportAttr>();
4363 else if (
VarDecl *
V = dyn_cast<VarDecl>(VD))
4364 SafeToInline = !
V->hasGlobalStorage() ||
V->hasAttr<DLLImportAttr>();
4365 return SafeToInline;
4370 return SafeToInline;
4377 SafeToInline =
true;
4379 SafeToInline = M->
hasAttr<DLLImportAttr>();
4381 return SafeToInline;
4386 return SafeToInline;
4391 return SafeToInline;
4400 CodeGenModule::isTriviallyRecursive(
const FunctionDecl *FD) {
4402 if (
getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
4407 Name =
Attr->getLabel();
4412 FunctionIsDirectlyRecursive Walker(Name, Context.
BuiltinInfo);
4414 return Body ? Walker.Visit(Body) :
false;
4417 bool CodeGenModule::shouldEmitFunction(
GlobalDecl GD) {
4421 const auto *F = cast<FunctionDecl>(GD.
getDecl());
4422 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
4427 if (
const Module *M = F->getOwningModule();
4428 M && M->getTopLevelModule()->isNamedModule() &&
4429 getContext().getCurrentNamedModule() != M->getTopLevelModule()) {
4439 if (!F->isTemplateInstantiation() || !F->hasAttr<AlwaysInlineAttr>()) {
4444 if (F->hasAttr<NoInlineAttr>())
4447 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
4449 DLLImportFunctionVisitor Visitor;
4450 Visitor.TraverseFunctionDecl(
const_cast<FunctionDecl*
>(F));
4451 if (!Visitor.SafeToInline)
4457 for (
const Decl *Member : Dtor->getParent()->decls())
4458 if (isa<FieldDecl>(Member))
4469 if (F->isInlineBuiltinDeclaration())
4477 return !isTriviallyRecursive(F);
4480 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
4481 return CodeGenOpts.OptimizationLevel > 0;
4484 void CodeGenModule::EmitMultiVersionFunctionDefinition(
GlobalDecl GD,
4485 llvm::GlobalValue *GV) {
4486 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
4489 auto *Spec = FD->
getAttr<CPUSpecificAttr>();
4490 for (
unsigned I = 0; I < Spec->cpus_size(); ++I)
4492 }
else if (
auto *TC = FD->
getAttr<TargetClonesAttr>()) {
4493 for (
unsigned I = 0; I < TC->featuresStrs_size(); ++I)
4496 TC->isFirstOfVersion(I))
4499 GetOrCreateMultiVersionResolver(GD);
4501 EmitGlobalFunctionDefinition(GD, GV);
4505 AddDeferredMultiVersionResolverToEmit(GD);
4508 void CodeGenModule::EmitGlobalDefinition(
GlobalDecl GD, llvm::GlobalValue *GV) {
4509 const auto *D = cast<ValueDecl>(GD.
getDecl());
4513 "Generating code for declaration");
4515 if (
const auto *FD = dyn_cast<FunctionDecl>(D)) {
4518 if (!shouldEmitFunction(GD))
4521 llvm::TimeTraceScope TimeScope(
"CodeGen Function", [&]() {
4523 llvm::raw_string_ostream OS(Name);
4529 if (
const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
4532 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
4533 ABI->emitCXXStructor(GD);
4535 EmitMultiVersionFunctionDefinition(GD, GV);
4537 EmitGlobalFunctionDefinition(GD, GV);
4539 if (Method->isVirtual())
4546 return EmitMultiVersionFunctionDefinition(GD, GV);
4547 return EmitGlobalFunctionDefinition(GD, GV);
4550 if (
const auto *VD = dyn_cast<VarDecl>(D))
4551 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
4553 llvm_unreachable(
"Invalid argument to EmitGlobalDefinition()");
4557 llvm::Function *NewFn);
4563 unsigned NumFeatures = 0;
4587 return llvm::GlobalValue::InternalLinkage;
4588 return llvm::GlobalValue::WeakODRLinkage;
4602 NewDecl->
addAttr(TargetVersionAttr::CreateImplicit(
4608 void CodeGenModule::emitMultiVersionFunctions() {
4609 std::vector<GlobalDecl> MVFuncsToEmit;
4610 MultiVersionFuncs.swap(MVFuncsToEmit);
4612 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
4613 assert(FD &&
"Expected a FunctionDecl");
4620 if (
Decl->isDefined()) {
4621 EmitGlobalFunctionDefinition(CurGD,
nullptr);
4629 assert(Func &&
"This should have just been created");
4631 return cast<llvm::Function>(Func);
4635 bool ShouldEmitResolver =
4643 if (
const auto *TA = CurFD->
getAttr<TargetAttr>()) {
4644 TA->getAddedFeatures(Feats);
4645 llvm::Function *Func = createFunction(CurFD);
4646 Options.emplace_back(Func, TA->getArchitecture(), Feats);
4647 }
else if (
const auto *TVA = CurFD->
getAttr<TargetVersionAttr>()) {
4648 bool HasDefaultDef = TVA->isDefaultVersion() &&
4649 CurFD->doesThisDeclarationHaveABody();
4650 HasDefaultDecl |= TVA->isDefaultVersion();
4651 ShouldEmitResolver |= (CurFD->isUsed() || HasDefaultDef);
4652 TVA->getFeatures(Feats);
4653 llvm::Function *Func = createFunction(CurFD);
4654 Options.emplace_back(Func,
"", Feats);
4655 }
else if (
const auto *TC = CurFD->
getAttr<TargetClonesAttr>()) {
4656 ShouldEmitResolver |= CurFD->doesThisDeclarationHaveABody();
4657 for (unsigned I = 0; I < TC->featuresStrs_size(); ++I) {
4658 if (!TC->isFirstOfVersion(I))
4661 llvm::Function *Func = createFunction(CurFD, I);
4662 StringRef Architecture;
4664 if (getTarget().getTriple().isAArch64())
4665 TC->getFeatures(Feats, I);
4667 StringRef Version = TC->getFeatureStr(I);
4668 if (Version.starts_with(
"arch="))
4669 Architecture = Version.drop_front(sizeof(
"arch=") - 1);
4670 else if (Version !=
"default")
4671 Feats.push_back(Version);
4673 Options.emplace_back(Func, Architecture, Feats);
4676 llvm_unreachable(
"unexpected MultiVersionKind");
4679 if (!ShouldEmitResolver)
4682 if (!HasDefaultDecl) {
4684 llvm::Function *
Func = createFunction(NewFD);
4686 Options.emplace_back(Func,
"", Feats);
4689 llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
4690 if (
auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant)) {
4691 ResolverConstant = IFunc->getResolver();
4697 *
this, GD, FD,
true);
4703 MangledName +
".ifunc", IFunc, &
getModule());
4707 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
4712 ResolverFunc->setComdat(
4713 getModule().getOrInsertComdat(ResolverFunc->getName()));
4722 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4728 if (!MVFuncsToEmit.empty())
4733 if (!MultiVersionFuncs.empty())
4734 emitMultiVersionFunctions();
4737 void CodeGenModule::emitCPUDispatchDefinition(
GlobalDecl GD) {
4738 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
4739 assert(FD &&
"Not a FunctionDecl?");
4741 const auto *DD = FD->
getAttr<CPUDispatchAttr>();
4742 assert(DD &&
"Not a cpu_dispatch Function?");
4748 UpdateMultiVersionNames(GD, FD, ResolverName);
4750 llvm::Type *ResolverType;
4753 ResolverType = llvm::FunctionType::get(
4754 llvm::PointerType::get(
DeclTy,
4763 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
4764 ResolverName, ResolverType, ResolverGD,
false));
4767 ResolverFunc->setComdat(
4768 getModule().getOrInsertComdat(ResolverFunc->getName()));
4781 GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
4784 EmitGlobalFunctionDefinition(ExistingDecl,
nullptr);
4790 Func = GetOrCreateLLVMFunction(
4791 MangledName,
DeclTy, ExistingDecl,
4798 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4799 llvm::transform(Features, Features.begin(),
4800 [](StringRef Str) { return Str.substr(1); });
4801 llvm::erase_if(Features, [&
Target](StringRef Feat) {
4802 return !
Target.validateCpuSupports(Feat);
4804 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
4819 while (Options.size() > 1 &&
4820 llvm::all_of(llvm::X86::getCpuSupportsMask(
4821 (Options.end() - 2)->Conditions.Features),
4822 [](
auto X) { return X == 0; })) {
4823 StringRef LHSName = (Options.end() - 2)->
Function->getName();
4824 StringRef RHSName = (Options.end() - 1)->
Function->getName();
4825 if (LHSName.compare(RHSName) < 0)
4826 Options.erase(Options.end() - 2);
4828 Options.erase(Options.end() - 1);
4832 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4836 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
4840 if (!isa<llvm::GlobalIFunc>(IFunc)) {
4841 assert(cast<llvm::Function>(IFunc)->isDeclaration());
4844 GI->takeName(IFunc);
4845 IFunc->replaceAllUsesWith(GI);
4846 IFunc->eraseFromParent();
4851 *
this, GD, FD,
true);
4862 void CodeGenModule::AddDeferredMultiVersionResolverToEmit(
GlobalDecl GD) {
4863 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
4864 assert(FD &&
"Not a FunctionDecl?");
4867 std::string MangledName =
4869 if (!DeferredResolversToEmit.insert(MangledName).second)
4872 MultiVersionFuncs.push_back(GD);
4877 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(
GlobalDecl GD) {
4878 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
4879 assert(FD &&
"Not a FunctionDecl?");
4881 std::string MangledName =
4886 std::string ResolverName = MangledName;
4890 llvm_unreachable(
"unexpected MultiVersionKind::None for resolver");
4894 ResolverName +=
".ifunc";
4901 ResolverName +=
".resolver";
4905 if (llvm::GlobalValue *ResolverGV =
GetGlobalValue(ResolverName))
4914 MultiVersionFuncs.push_back(GD);
4919 llvm::Type *ResolverType = llvm::FunctionType::get(
4920 llvm::PointerType::get(
DeclTy,
4923 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4924 MangledName +
".resolver", ResolverType,
GlobalDecl{},
4926 llvm::GlobalIFunc *GIF =
4929 GIF->setName(ResolverName);
4935 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4937 assert(isa<llvm::GlobalValue>(Resolver) &&
4938 "Resolver should be created for the first time");
4950 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
4951 StringRef MangledName, llvm::Type *Ty,
GlobalDecl GD,
bool ForVTable,
4952 bool DontDefer,
bool IsThunk, llvm::AttributeList ExtraAttrs,
4958 if (
const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
4960 if (
getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime &&
4961 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->
isDefined() &&
4962 !DontDefer && !IsForDefinition) {
4965 if (
const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
4967 else if (
const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
4976 UpdateMultiVersionNames(GD, FD, MangledName);
4979 AddDeferredMultiVersionResolverToEmit(GD);
4980 else if (!IsForDefinition)
4981 return GetOrCreateMultiVersionResolver(GD);
4988 if (WeakRefReferences.erase(Entry)) {
4989 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
4990 if (FD && !FD->
hasAttr<WeakAttr>())
4991 Entry->setLinkage(llvm::Function::ExternalLinkage);
4995 if (D && !D->
hasAttr<DLLImportAttr>() && !D->
hasAttr<DLLExportAttr>() &&
4997 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
5003 if (IsForDefinition && !Entry->isDeclaration()) {
5010 DiagnosedConflictingDefinitions.insert(GD).second) {
5014 diag::note_previous_definition);
5018 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
5019 (Entry->getValueType() == Ty)) {
5026 if (!IsForDefinition)
5033 bool IsIncompleteFunction =
false;
5035 llvm::FunctionType *FTy;
5036 if (isa<llvm::FunctionType>(Ty)) {
5037 FTy = cast<llvm::FunctionType>(Ty);
5039 FTy = llvm::FunctionType::get(
VoidTy,
false);
5040 IsIncompleteFunction =
true;
5044 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
5045 Entry ? StringRef() : MangledName, &
getModule());
5049 if (D && D->
hasAttr<AnnotateAttr>())
5050 DeferredAnnotations[MangledName] = cast<ValueDecl>(D);
5067 if (!Entry->use_empty()) {
5069 Entry->removeDeadConstantUsers();
5075 assert(F->getName() == MangledName &&
"name was uniqued!");
5077 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
5078 if (ExtraAttrs.hasFnAttrs()) {
5079 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
5087 if (isa_and_nonnull<CXXDestructorDecl>(D) &&
5088 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
5090 addDeferredDeclToEmit(GD);
5097 auto DDI = DeferredDecls.find(MangledName);
5098 if (DDI != DeferredDecls.end() &&
5101 ((DDI->second).getDecl()->hasAttr<CUDAHostAttr>() ==
5103 (DDI->second).getDecl()->hasAttr<CUDADeviceAttr>() ==
5104 D->
hasAttr<CUDADeviceAttr>()))) {
5108 addDeferredDeclToEmit(DDI->second);
5109 DeferredDecls.erase(DDI);
5124 for (
const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
5137 if (!IsIncompleteFunction) {
5138 assert(F->getFunctionType() == Ty);
5154 const auto *FD = cast<FunctionDecl>(GD.
getDecl());
5161 if (
const auto *DD = dyn_cast<CXXDestructorDecl>(GD.
getDecl())) {
5164 DD->getParent()->getNumVBases() == 0)
5169 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
5170 false, llvm::AttributeList(),
5173 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
5174 cast<FunctionDecl>(GD.
getDecl())->hasAttr<CUDAGlobalAttr>()) {
5176 cast<llvm::Function>(F->stripPointerCasts()), GD);
5177 if (IsForDefinition)
5185 llvm::GlobalValue *F =
5188 return llvm::NoCFIValue::get(F);
5197 for (
const auto *Result : DC->
lookup(&CII))
5198 if (
const auto *FD = dyn_cast<FunctionDecl>(Result))
5201 if (!C.getLangOpts().CPlusPlus)
5206 (Name ==
"_ZSt9terminatev" || Name ==
"?terminate@@YAXXZ")
5207 ? C.Idents.get(
"terminate")
5208 : C.Idents.get(Name);
5210 for (
const auto &N : {
"__cxxabiv1",
"std"}) {
5212 for (
const auto *Result : DC->
lookup(&NS)) {
5214 if (
auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
5215 for (
const auto *Result : LSD->lookup(&NS))
5216 if ((ND = dyn_cast<NamespaceDecl>(Result)))
5220 for (
const auto *Result : ND->
lookup(&CXXII))
5221 if (
const auto *FD = dyn_cast<FunctionDecl>(Result))
5231 llvm::FunctionCallee
5233 llvm::AttributeList ExtraAttrs,
bool Local,
5234 bool AssumeConvergent) {
5235 if (AssumeConvergent) {
5237 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
5241 GetOrCreateLLVMFunction(Name, FTy,
GlobalDecl(),
false,
5245 if (
auto *F = dyn_cast<llvm::Function>(C)) {
5254 if (!Local &&
getTriple().isWindowsItaniumEnvironment() &&
5257 if (!FD || FD->
hasAttr<DLLImportAttr>()) {
5258 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
5259 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
5266 markRegisterParameterAttributes(F);
5274 llvm::GlobalVariable *GV,
5283 if (
const auto *IOAttr = D->
getAttr<SYCLIntelPipeIOAttr>()) {
5284 const auto *CE = cast<ConstantExpr>(IOAttr->getID());
5285 std::optional<llvm::APSInt>
ID = CE->getResultAsAPSInt();
5288 llvm::Metadata *AttrMDArgs[] = {
5289 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
5290 llvm::Type::getInt32Ty(Context),
ID->getSExtValue()))};
5291 GV->setMetadata(IOAttr->getSpelling(),
5292 llvm::MDNode::get(Context, AttrMDArgs));
5315 if (WeakRefReferences.erase(Entry)) {
5316 if (D && !D->
hasAttr<WeakAttr>())
5317 Entry->setLinkage(llvm::Function::ExternalLinkage);
5321 if (D && !D->
hasAttr<DLLImportAttr>() && !D->
hasAttr<DLLExportAttr>() &&
5323 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
5325 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
5328 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
5333 if (IsForDefinition && !Entry->isDeclaration()) {
5341 (OtherD = dyn_cast<VarDecl>(OtherGD.
getDecl())) &&
5343 DiagnosedConflictingDefinitions.insert(D).second) {
5347 diag::note_previous_definition);
5352 if (Entry->getType()->getAddressSpace() != TargetAS)
5353 return llvm::ConstantExpr::getAddrSpaceCast(
5354 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
5358 if (!IsForDefinition)
5364 auto *GV =
new llvm::GlobalVariable(
5365 getModule(), Ty,
false, llvm::GlobalValue::ExternalLinkage,
nullptr,
5366 MangledName,
nullptr, llvm::GlobalVariable::NotThreadLocal,
5367 getContext().getTargetAddressSpace(DAddrSpace));
5372 GV->takeName(Entry);
5374 if (!Entry->use_empty()) {
5375 Entry->replaceAllUsesWith(GV);
5378 Entry->eraseFromParent();
5384 auto DDI = DeferredDecls.find(MangledName);
5385 if (DDI != DeferredDecls.end()) {
5388 addDeferredDeclToEmit(DDI->second);
5389 DeferredDecls.erase(DDI);
5394 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
5401 GV->setAlignment(
getContext().getDeclAlign(D).getAsAlign());
5407 CXXThreadLocals.push_back(D);
5415 if (
getContext().isMSStaticDataMemberInlineDefinition(D)) {
5416 EmitGlobalVarDefinition(D);
5421 if (
const SectionAttr *SA = D->
getAttr<SectionAttr>())
5422 GV->setSection(SA->getName());
5426 if (
getTriple().getArch() == llvm::Triple::xcore &&
5430 GV->setSection(
".cp.rodata");
5433 if (
const auto *CMA = D->
getAttr<CodeModelAttr>())
5434 GV->setCodeModel(CMA->getModel());
5439 if (Context.
getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
5445 if (!HasMutableFields) {
5452 auto *InitType = Init->getType();
5453 if (GV->getValueType() != InitType) {
5458 GV->setName(StringRef());
5461 auto *NewGV = cast<llvm::GlobalVariable>(
5463 ->stripPointerCasts());
5466 GV->eraseFromParent();
5469 GV->setInitializer(Init);
5470 GV->setConstant(
true);
5471 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
5479 if (LangOpts.SYCLIsDevice)
5494 SanitizerMD->reportGlobal(GV, *D);
5499 assert(
getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
5500 if (DAddrSpace != ExpectedAS) {
5502 *
this, GV, DAddrSpace, ExpectedAS,
5513 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
5515 false, IsForDefinition);
5517 if (isa<CXXMethodDecl>(D)) {
5525 if (isa<FunctionDecl>(D)) {
5536 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes
Linkage,
5537 llvm::Align Alignment) {
5538 llvm::GlobalVariable *GV =
getModule().getNamedGlobal(Name);
5539 llvm::GlobalVariable *OldGV =
nullptr;
5543 if (GV->getValueType() == Ty)
5548 assert(GV->isDeclaration() &&
"Declaration has wrong type!");
5554 nullptr, Name,
nullptr,
5555 llvm::GlobalValue::NotThreadLocal,
5560 GV->takeName(OldGV);
5562 if (!OldGV->use_empty()) {
5563 OldGV->replaceAllUsesWith(GV);
5566 OldGV->eraseFromParent();
5570 !GV->hasAvailableExternallyLinkage())
5571 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5573 GV->setAlignment(Alignment);
5610 assert(!D->
getInit() &&
"Cannot emit definite definitions here!");
5618 if (GV && !GV->isDeclaration())
5623 if (!MustBeEmitted(D) && !GV) {
5624 DeferredDecls[MangledName] = D;
5629 EmitGlobalVarDefinition(D);
5633 EmitExternalVarDeclaration(D);
5642 if (LangOpts.OpenCL) {
5653 if (LangOpts.SYCLIsDevice && D) {
5659 if (LangOpts.SYCLIsDevice &&
5663 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
5665 if (D->
hasAttr<CUDAConstantAttr>())
5667 if (D->
hasAttr<CUDASharedAttr>())
5669 if (D->
hasAttr<CUDADeviceAttr>())
5677 if (LangOpts.OpenMP) {
5679 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
5687 if (LangOpts.OpenCL)
5689 if (LangOpts.SYCLIsDevice)
5691 if (LangOpts.HIP && LangOpts.CUDAIsDevice &&
getTriple().isSPIRV())
5699 if (
auto AS =
getTarget().getConstantAddressSpace())
5712 static llvm::Constant *
5714 llvm::GlobalVariable *GV) {
5715 llvm::Constant *
Cast = GV;
5721 llvm::PointerType::get(
5728 template<
typename SomeDecl>
5730 llvm::GlobalValue *GV) {
5736 if (!D->template hasAttr<UsedAttr>())
5745 const SomeDecl *
First = D->getFirstDecl();
5746 if (
First->getDeclContext()->isRecord() || !
First->isInExternCContext())
5752 std::pair<StaticExternCMap::iterator, bool> R =
5753 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5758 R.first->second =
nullptr;
5765 if (D.
hasAttr<SelectAnyAttr>())
5769 if (
auto *VD = dyn_cast<VarDecl>(&D))
5783 llvm_unreachable(
"No such linkage");
5791 llvm::GlobalObject &GO) {
5794 GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
5798 if (AspectsEnumDecl && AspectsEnumDecl != ED) {
5802 diag::note_previous_definition);
5804 AspectsEnumDecl = ED;
5809 llvm::raw_svector_ostream Out(AnnotStr);
5810 if (D->
hasAttr<SYCLIntelRegisterAttr>())
5811 Out <<
"{register:1}";
5812 if (
auto const *MA = D->
getAttr<SYCLIntelMemoryAttr>()) {
5813 SYCLIntelMemoryAttr::MemoryKind
Kind = MA->getKind();
5816 case SYCLIntelMemoryAttr::MLAB:
5817 case SYCLIntelMemoryAttr::BlockRAM:
5818 Out << SYCLIntelMemoryAttr::ConvertMemoryKindToStr(
Kind);
5820 case SYCLIntelMemoryAttr::Default:
5825 if (
const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
5826 Out <<
"{sizeinfo:";
5829 QualType ElementTy = DD->getType();
5835 while (
const auto *AT =
getContext().getAsArrayType(ElementTy)) {
5837 const auto *CAT = cast<ConstantArrayType>(AT);
5838 Out <<
"," << CAT->getSize();
5839 ElementTy = CAT->getElementType();
5844 if (D->
hasAttr<SYCLIntelSinglePumpAttr>())
5846 if (D->
hasAttr<SYCLIntelDoublePumpAttr>())
5848 if (
const auto *BWA = D->
getAttr<SYCLIntelBankWidthAttr>()) {
5850 Out <<
'{' << BWA->getSpelling() <<
':' << BWAInt <<
'}';
5852 if (
const auto *PCA = D->
getAttr<SYCLIntelPrivateCopiesAttr>()) {
5854 Out <<
'{' << PCA->getSpelling() <<
':' << PCAInt <<
'}';
5856 if (
const auto *NBA = D->
getAttr<SYCLIntelNumBanksAttr>()) {
5858 Out <<
'{' << NBA->getSpelling() <<
':' << NBAInt <<
'}';
5860 if (
const auto *BBA = D->
getAttr<SYCLIntelBankBitsAttr>()) {
5861 Out <<
'{' << BBA->getSpelling() <<
':';
5862 for (SYCLIntelBankBitsAttr::args_iterator I = BBA->args_begin(),
5863 E = BBA->args_end();
5865 if (I != BBA->args_begin())
5872 if (
const auto *MRA = D->
getAttr<SYCLIntelMaxReplicatesAttr>()) {
5874 Out <<
'{' << MRA->getSpelling() <<
':' << MRAInt <<
'}';
5876 if (
const auto *MA = D->
getAttr<SYCLIntelMergeAttr>()) {
5877 Out <<
'{' << MA->getSpelling() <<
':' << MA->getName() <<
':'
5878 << MA->getDirection() <<
'}';
5880 if (D->
hasAttr<SYCLIntelSimpleDualPortAttr>())
5881 Out <<
"{simple_dual_port:1}";
5882 if (
const auto *FP2D = D->
getAttr<SYCLIntelForcePow2DepthAttr>()) {
5884 FP2D->getValue()->EvaluateKnownConstInt(
getContext());
5885 Out <<
'{' << FP2D->getSpelling() <<
':' << FP2DInt <<
'}';
5890 llvm::GlobalValue *GV) {
5893 if (!AnnotStr.empty()) {
5899 llvm::Constant *ASZeroGV = GV;
5900 if (GV->getAddressSpace() !=
5902 ASZeroGV = llvm::ConstantExpr::getAddrSpaceCast(
5903 GV, llvm::PointerType::get(
5908 llvm::Constant *Fields[5] = {
5912 Annotations.push_back(llvm::ConstantStruct::getAnon(Fields));
5917 void CodeGenModule::EmitGlobalVarDefinition(
const VarDecl *D,
5927 if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime &&
5928 OpenMPRuntime->emitTargetGlobalVariable(D))
5931 llvm::TrackingVH<llvm::Constant> Init;
5932 bool NeedsGlobalCtor =
false;
5936 bool IsDefinitionAvailableExternally =
5938 bool NeedsGlobalDtor =
5939 !IsDefinitionAvailableExternally &&
5945 std::optional<ConstantEmitter> emitter;
5950 bool IsCUDASharedVar =
5955 bool IsCUDAShadowVar =
5957 (D->
hasAttr<CUDAConstantAttr>() || D->
hasAttr<CUDADeviceAttr>() ||
5958 D->
hasAttr<CUDASharedAttr>());
5959 bool IsCUDADeviceShadowVar =
5964 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
5965 Init = llvm::UndefValue::get(
getTypes().ConvertTypeForMem(ASTTy));
5966 else if (D->
hasAttr<LoaderUninitializedAttr>())
5967 Init = llvm::UndefValue::get(
getTypes().ConvertTypeForMem(ASTTy));
5968 else if (!InitExpr) {
5982 emitter.emplace(*
this);
5983 llvm::Constant *
Initializer = emitter->tryEmitForInitializer(*InitDecl);
5994 if (!IsDefinitionAvailableExternally)
5995 NeedsGlobalCtor =
true;
5998 Init = llvm::UndefValue::get(
getTypes().ConvertType(
T));
6006 DelayedCXXInitPosition.erase(D);
6013 assert(VarSize == CstSize &&
"Emitted constant has unexpected size");
6018 llvm::Type* InitType =
Init->getType();
6019 llvm::Constant *Entry =
6023 Entry = Entry->stripPointerCasts();
6026 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
6037 if (!GV || GV->getValueType() != InitType ||
6038 GV->getType()->getAddressSpace() !=
6042 Entry->setName(StringRef());
6045 GV = cast<llvm::GlobalVariable>(
6047 ->stripPointerCasts());
6050 llvm::Constant *NewPtrForOldDecl =
6051 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
6053 Entry->replaceAllUsesWith(NewPtrForOldDecl);
6056 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
6061 if (D->
hasAttr<AnnotateAttr>())
6074 if (RD->
hasAttr<SYCLAddIRAttributesGlobalVariableAttr>())
6078 if (RD->
hasAttr<SYCLDeviceGlobalAttr>())
6082 if (
const auto *
Attr = RD->
getAttr<SYCLTypeAttr>())
6083 if (
Attr->getType() == SYCLTypeAttr::SYCLType::host_pipe)
6092 llvm::NamedMDNode *GlobalsRestrict =
6093 getModule().getOrInsertNamedMetadata(
"globals.restrict");
6094 llvm::Metadata *Args[] = {llvm::ValueAsMetadata::get(GV)};
6095 llvm::MDNode *
Node = llvm::MDNode::get(Context, Args);
6096 GlobalsRestrict->addOperand(
Node);
6109 if (LangOpts.CUDA) {
6110 if (LangOpts.CUDAIsDevice) {
6111 if (
Linkage != llvm::GlobalValue::InternalLinkage &&
6112 (D->
hasAttr<CUDADeviceAttr>() || D->
hasAttr<CUDAConstantAttr>() ||
6115 GV->setExternallyInitialized(
true);
6122 GV->setInitializer(Init);
6124 emitter->finalize(GV);
6127 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
6131 if (
const SectionAttr *SA = D->
getAttr<SectionAttr>()) {
6134 GV->setConstant(
true);
6139 if (std::optional<CharUnits> AlignValFromAllocate =
6141 AlignVal = *AlignValFromAllocate;
6159 Linkage == llvm::GlobalValue::ExternalLinkage &&
6162 Linkage = llvm::GlobalValue::InternalLinkage;
6165 if (D->
hasAttr<DLLImportAttr>())
6166 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
6167 else if (D->
hasAttr<DLLExportAttr>())
6168 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
6170 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
6172 if (
Linkage == llvm::GlobalVariable::CommonLinkage) {
6174 GV->setConstant(
false);
6179 if (!GV->getInitializer()->isNullValue())
6180 GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
6183 setNonAliasAttributes(D, GV);
6185 if (D->
getTLSKind() && !GV->isThreadLocal()) {
6187 CXXThreadLocals.push_back(D);
6194 if (NeedsGlobalCtor || NeedsGlobalDtor)
6195 EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
6197 SanitizerMD->reportGlobal(GV, *D, NeedsGlobalCtor);
6202 DI->EmitGlobalVariable(GV, D);
6204 if (LangOpts.SYCLIsDevice) {
6212 void CodeGenModule::EmitExternalVarDeclaration(
const VarDecl *D) {
6217 llvm::Constant *GV =
6219 DI->EmitExternalVariable(
6220 cast<llvm::GlobalVariable>(GV->stripPointerCasts()), D);
6229 if ((NoCommon || D->
hasAttr<NoCommonAttr>()) && !D->
hasAttr<CommonAttr>())
6240 if (D->
hasAttr<SectionAttr>())
6246 if (D->
hasAttr<PragmaClangBSSSectionAttr>() ||
6247 D->
hasAttr<PragmaClangDataSectionAttr>() ||
6248 D->
hasAttr<PragmaClangRelroSectionAttr>() ||
6249 D->
hasAttr<PragmaClangRodataSectionAttr>())
6257 if (D->
hasAttr<WeakImportAttr>())
6267 if (D->
hasAttr<AlignedAttr>())
6276 if (FD->isBitField())
6278 if (FD->
hasAttr<AlignedAttr>())
6300 llvm::GlobalValue::LinkageTypes
6304 return llvm::Function::InternalLinkage;
6307 return llvm::GlobalVariable::WeakAnyLinkage;
6311 return llvm::GlobalVariable::LinkOnceAnyLinkage;
6316 return llvm::GlobalValue::AvailableExternallyLinkage;
6324 !D->
hasAttr<SYCLDeviceAttr>() &&
6325 !SemaSYCL::isTypeDecoratedWithDeclAttribute<SYCLDeviceGlobalAttr>(
6328 ? llvm::Function::LinkOnceODRLinkage
6329 : llvm::Function::InternalLinkage;
6343 return !Context.
getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
6344 : llvm::Function::InternalLinkage;
6358 return llvm::Function::ExternalLinkage;
6361 return D->
hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
6362 : llvm::Function::InternalLinkage;
6363 return llvm::Function::WeakODRLinkage;
6368 if (!
getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
6370 CodeGenOpts.NoCommon))
6371 return llvm::GlobalVariable::CommonLinkage;
6377 if (D->
hasAttr<SelectAnyAttr>())
6378 return llvm::GlobalVariable::WeakODRLinkage;
6382 return llvm::GlobalVariable::ExternalLinkage;
6385 llvm::GlobalValue::LinkageTypes
6394 llvm::Function *newFn) {
6396 if (old->use_empty())
6399 llvm::Type *newRetTy = newFn->getReturnType();
6404 for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
6406 llvm::User *user = ui->getUser();
6410 if (
auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
6411 if (bitcast->getOpcode() == llvm::Instruction::BitCast)
6417 llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
6420 if (!callSite->isCallee(&*ui))
6425 if (callSite->getType() != newRetTy && !callSite->use_empty())
6430 llvm::AttributeList oldAttrs = callSite->getAttributes();
6433 unsigned newNumArgs = newFn->arg_size();
6434 if (callSite->arg_size() < newNumArgs)
6440 bool dontTransform =
false;
6441 for (llvm::Argument &A : newFn->args()) {
6442 if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
6443 dontTransform =
true;
6448 newArgAttrs.push_back(oldAttrs.getParamAttrs(argNo));
6456 newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
6460 callSite->getOperandBundlesAsDefs(newBundles);
6462 llvm::CallBase *newCall;
6463 if (isa<llvm::CallInst>(callSite)) {
6465 llvm::CallInst::Create(newFn, newArgs, newBundles,
"", callSite);
6467 auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
6468 newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
6469 oldInvoke->getUnwindDest(), newArgs,
6470 newBundles,
"", callSite);
6474 if (!newCall->getType()->isVoidTy())
6475 newCall->takeName(callSite);
6476 newCall->setAttributes(
6477 llvm::AttributeList::get(newFn->getContext(), oldAttrs.getFnAttrs(),
6478 oldAttrs.getRetAttrs(), newArgAttrs));
6479 newCall->setCallingConv(callSite->getCallingConv());
6482 if (!callSite->use_empty())
6483 callSite->replaceAllUsesWith(newCall);
6486 if (callSite->getDebugLoc())
6487 newCall->setDebugLoc(callSite->getDebugLoc());
6489 callSitesToBeRemovedFromParent.push_back(callSite);
6492 for (
auto *callSite : callSitesToBeRemovedFromParent) {
6493 callSite->eraseFromParent();
6507 llvm::Function *NewFn) {
6509 if (!isa<llvm::Function>(Old))
return;
6528 void CodeGenModule::EmitGlobalFunctionDefinition(
GlobalDecl GD,
6529 llvm::GlobalValue *GV) {
6530 const auto *D = cast<FunctionDecl>(GD.
getDecl());
6537 if (!GV || (GV->getValueType() != Ty))
6543 if (!GV->isDeclaration())
6550 auto *Fn = cast<llvm::Function>(GV);
6562 setNonAliasAttributes(GD, Fn);
6565 if (
const ConstructorAttr *CA = D->
getAttr<ConstructorAttr>())
6567 if (
const DestructorAttr *DA = D->
getAttr<DestructorAttr>())
6573 void CodeGenModule::EmitAliasDefinition(
GlobalDecl GD) {
6574 const auto *D = cast<ValueDecl>(GD.
getDecl());
6575 const AliasAttr *AA = D->
getAttr<AliasAttr>();
6576 assert(AA &&
"Not an alias?");
6580 if (AA->getAliasee() == MangledName) {
6581 Diags.
Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6588 if (Entry && !Entry->isDeclaration())
6591 Aliases.push_back(GD);
6597 llvm::Constant *Aliasee;
6598 llvm::GlobalValue::LinkageTypes
LT;
6600 if (isa<llvm::FunctionType>(
DeclTy)) {
6601 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(),
DeclTy, GD,
6604 AS = Aliasee->getType()->getPointerAddressSpace();
6610 if (
const auto *VD = dyn_cast<VarDecl>(GD.
getDecl()))
6621 if (GA->getAliasee() == Entry) {
6622 Diags.
Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
6626 assert(Entry->isDeclaration());
6635 GA->takeName(Entry);
6637 Entry->replaceAllUsesWith(GA);
6638 Entry->eraseFromParent();
6640 GA->setName(MangledName);
6648 GA->setLinkage(llvm::Function::WeakAnyLinkage);
6651 if (
const auto *VD = dyn_cast<VarDecl>(D))
6652 if (VD->getTLSKind())
6658 if (isa<VarDecl>(D))
6660 DI->EmitGlobalAlias(cast<llvm::GlobalValue>(GA->getAliasee()->stripPointerCasts()), GD);
6663 void CodeGenModule::emitIFuncDefinition(
GlobalDecl GD) {
6664 const auto *D = cast<ValueDecl>(GD.
getDecl());
6665 const IFuncAttr *IFA = D->
getAttr<IFuncAttr>();
6666 assert(IFA &&
"Not an ifunc?");
6670 if (IFA->getResolver() == MangledName) {
6671 Diags.
Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6677 if (Entry && !Entry->isDeclaration()) {
6680 DiagnosedConflictingDefinitions.insert(GD).second) {
6684 diag::note_previous_definition);
6689 Aliases.push_back(GD);
6692 llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(
DeclTy);
6693 llvm::Constant *Resolver =
6694 GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {},
6696 llvm::GlobalIFunc *GIF =
6700 if (GIF->getResolver() == Entry) {
6701 Diags.
Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
6704 assert(Entry->isDeclaration());
6713 GIF->takeName(Entry);
6715 Entry->replaceAllUsesWith(GIF);
6716 Entry->eraseFromParent();
6718 GIF->setName(MangledName);
6719 if (
auto *F = dyn_cast<llvm::Function>(Resolver)) {
6720 F->addFnAttr(llvm::Attribute::DisableSanitizerInstrumentation);
6731 static llvm::StringMapEntry<llvm::GlobalVariable *> &
6734 bool &IsUTF16,
unsigned &StringLength) {
6735 StringRef String =
Literal->getString();
6736 unsigned NumBytes = String.size();
6739 if (!
Literal->containsNonAsciiOrNull()) {
6740 StringLength = NumBytes;
6741 return *Map.insert(std::make_pair(String,
nullptr)).first;
6748 const llvm::UTF8 *FromPtr = (
const llvm::UTF8 *)String.data();
6749 llvm::UTF16 *ToPtr = &ToBuf[0];
6751 (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
6752 ToPtr + NumBytes, llvm::strictConversion);
6755 StringLength = ToPtr - &ToBuf[0];
6759 return *Map.insert(std::make_pair(
6760 StringRef(
reinterpret_cast<const char *
>(ToBuf.data()),
6761 (StringLength + 1) * 2),
6767 unsigned StringLength = 0;
6768 bool isUTF16 =
false;
6769 llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
6774 if (
auto *C = Entry.second)
6778 llvm::Constant *Zero = llvm::Constant::getNullValue(
Int32Ty);
6779 llvm::Constant *Zeros[] = { Zero, Zero };
6782 const llvm::Triple &Triple =
getTriple();
6785 const bool IsSwiftABI =
6786 static_cast<unsigned>(CFRuntime) >=
6791 if (!CFConstantStringClassRef) {
6792 const char *CFConstantStringClassName =
"__CFConstantStringClassReference";
6794 Ty = llvm::ArrayType::get(Ty, 0);
6796 switch (CFRuntime) {
6800 CFConstantStringClassName =
6801 Triple.isOSDarwin() ?
"$s15SwiftFoundation19_NSCFConstantStringCN"
6802 :
"$s10Foundation19_NSCFConstantStringCN";
6806 CFConstantStringClassName =
6807 Triple.isOSDarwin() ?
"$S15SwiftFoundation19_NSCFConstantStringCN"
6808 :
"$S10Foundation19_NSCFConstantStringCN";
6812 CFConstantStringClassName =
6813 Triple.isOSDarwin() ?
"__T015SwiftFoundation19_NSCFConstantStringCN"
6814 :
"__T010Foundation19_NSCFConstantStringCN";
6821 if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
6822 llvm::GlobalValue *GV =
nullptr;
6824 if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
6830 for (
const auto *Result : DC->
lookup(&II))
6831 if ((VD = dyn_cast<VarDecl>(Result)))
6834 if (Triple.isOSBinFormatELF()) {
6836 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6838 GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
6839 if (!VD || !VD->
hasAttr<DLLExportAttr>())
6840 GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
6842 GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
6850 CFConstantStringClassRef =
6851 IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
6852 : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
6857 auto *STy = cast<llvm::StructType>(
getTypes().ConvertType(CFTy));
6860 auto Fields = Builder.beginStruct(STy);
6863 Fields.add(cast<llvm::Constant>(CFConstantStringClassRef));
6867 Fields.addInt(
IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
6868 Fields.addInt(
Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
6870 Fields.addInt(
IntTy, isUTF16 ? 0x07d0 : 0x07C8);
6874 llvm::Constant *C =
nullptr;
6877 reinterpret_cast<uint16_t *
>(
const_cast<char *
>(Entry.first().data())),
6878 Entry.first().size() / 2);
6879 C = llvm::ConstantDataArray::get(VMContext, Arr);
6881 C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
6887 new llvm::GlobalVariable(
getModule(), C->getType(),
true,
6888 llvm::GlobalValue::PrivateLinkage, C,
".str");
6889 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
6899 if (Triple.isOSBinFormatMachO())
6900 GV->setSection(isUTF16 ?
"__TEXT,__ustring"
6901 :
"__TEXT,__cstring,cstring_literals");
6904 else if (Triple.isOSBinFormatELF())
6905 GV->setSection(
".rodata");
6908 llvm::Constant *Str =
6909 llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
6914 llvm::IntegerType *LengthTy =
6924 Fields.addInt(LengthTy, StringLength);
6932 GV = Fields.finishAndCreateGlobal(
"_unnamed_cfstring_", Alignment,
6934 llvm::GlobalVariable::PrivateLinkage);
6935 GV->addAttribute(
"objc_arc_inert");
6936 switch (Triple.getObjectFormat()) {
6937 case llvm::Triple::UnknownObjectFormat:
6938 llvm_unreachable(
"unknown file format");
6939 case llvm::Triple::DXContainer:
6940 case llvm::Triple::GOFF:
6941 case llvm::Triple::SPIRV:
6942 case llvm::Triple::XCOFF:
6943 llvm_unreachable(
"unimplemented");
6944 case llvm::Triple::COFF:
6945 case llvm::Triple::ELF:
6946 case llvm::Triple::Wasm:
6947 GV->setSection(
"cfstring");
6949 case llvm::Triple::MachO:
6950 GV->setSection(
"__DATA,__cfstring");
6959 return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
6963 if (ObjCFastEnumerationStateType.
isNull()) {
6973 for (
size_t i = 0; i < 4; ++i) {
6978 FieldTypes[i],
nullptr,
6990 return ObjCFastEnumerationStateType;
7004 assert(CAT &&
"String literal not of constant array type!");
7006 return llvm::ConstantDataArray::getString(VMContext, Str,
false);
7009 auto *AType = cast<llvm::ArrayType>(
getTypes().ConvertType(E->
getType()));
7010 llvm::Type *ElemTy = AType->getElementType();
7011 unsigned NumElements = AType->getNumElements();
7014 if (ElemTy->getPrimitiveSizeInBits() == 16) {
7016 Elements.reserve(NumElements);
7018 for(
unsigned i = 0, e = E->
getLength(); i != e; ++i)
7020 Elements.resize(NumElements);
7021 return llvm::ConstantDataArray::get(VMContext, Elements);
7024 assert(ElemTy->getPrimitiveSizeInBits() == 32);
7026 Elements.reserve(NumElements);
7028 for(
unsigned i = 0, e = E->
getLength(); i != e; ++i)
7030 Elements.resize(NumElements);
7031 return llvm::ConstantDataArray::get(VMContext, Elements);
7034 static llvm::GlobalVariable *
7043 auto *GV =
new llvm::GlobalVariable(
7044 M, C->getType(), !CGM.
getLangOpts().WritableStrings,
LT, C, GlobalName,
7045 nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
7047 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
7048 if (GV->isWeakForLinker()) {
7049 assert(CGM.
supportsCOMDAT() &&
"Only COFF uses weak string literals");
7050 GV->setComdat(M.getOrInsertComdat(GV->getName()));
7066 llvm::GlobalVariable **Entry =
nullptr;
7067 if (!LangOpts.WritableStrings) {
7068 Entry = &ConstantStringMap[C];
7069 if (
auto GV = *Entry) {
7073 GV->getValueType(), Alignment);
7078 StringRef GlobalVariableName;
7079 llvm::GlobalValue::LinkageTypes
LT;
7084 if (
getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
7085 !LangOpts.WritableStrings) {
7086 llvm::raw_svector_ostream Out(MangledNameBuffer);
7088 LT = llvm::GlobalValue::LinkOnceODRLinkage;
7089 GlobalVariableName = MangledNameBuffer;
7091 LT = llvm::GlobalValue::PrivateLinkage;
7092 GlobalVariableName = Name;
7104 SanitizerMD->reportGlobal(GV, S->getStrTokenLoc(0),
"<string literal>");
7107 GV->getValueType(), Alignment);
7124 const std::string &Str,
const char *GlobalName) {
7125 StringRef StrWithNull(Str.c_str(), Str.size() + 1);
7130 llvm::ConstantDataArray::getString(
getLLVMContext(), StrWithNull,
false);
7133 llvm::GlobalVariable **Entry =
nullptr;
7134 if (!LangOpts.WritableStrings) {
7135 Entry = &ConstantStringMap[C];
7136 if (
auto GV = *Entry) {
7140 GV->getValueType(), Alignment);
7146 GlobalName =
".str";
7149 GlobalName, Alignment);
7154 GV->getValueType(), Alignment);
7165 QualType MaterializedType = Init->getType();
7167 MaterializedType = E->
getType();
7171 auto InsertResult = MaterializedGlobalTemporaryMap.insert({E,
nullptr});
7172 if (!InsertResult.second) {
7175 if (!InsertResult.first->second) {
7180 InsertResult.first->second =
new llvm::GlobalVariable(
7181 getModule(),
Type,
false, llvm::GlobalVariable::InternalLinkage,
7185 llvm::cast<llvm::GlobalVariable>(
7186 InsertResult.first->second->stripPointerCasts())
7195 llvm::raw_svector_ostream Out(Name);
7217 std::optional<ConstantEmitter> emitter;
7218 llvm::Constant *InitialValue =
nullptr;
7219 bool Constant =
false;
7223 emitter.emplace(*
this);
7224 InitialValue = emitter->emitForInitializer(*
Value, AddrSpace,
7229 Type = InitialValue->getType();
7238 if (
Linkage == llvm::GlobalVariable::ExternalLinkage) {
7240 if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
7244 Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
7248 Linkage = llvm::GlobalVariable::InternalLinkage;
7252 auto *GV =
new llvm::GlobalVariable(
7254 nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
7255 if (emitter) emitter->finalize(GV);
7257 if (!llvm::GlobalValue::isLocalLinkage(
Linkage)) {
7259 if (GV->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass)
7261 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
7265 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
7266 if (VD->getTLSKind())
7268 llvm::Constant *CV = GV;
7272 llvm::PointerType::get(
7278 llvm::Constant *&Entry = MaterializedGlobalTemporaryMap[E];
7280 Entry->replaceAllUsesWith(CV);
7281 llvm::cast<llvm::GlobalVariable>(Entry)->eraseFromParent();
7290 void CodeGenModule::EmitObjCPropertyImplementations(
const
7303 if (!Getter || Getter->isSynthesizedAccessorStub())
7306 auto *Setter = PID->getSetterMethodDecl();
7307 if (!PD->
isReadOnly() && (!Setter || Setter->isSynthesizedAccessorStub()))
7318 if (ivar->getType().isDestructedType())
7367 getContext().getObjCIdType(),
nullptr, D,
true,
7385 EmitDeclContext(LSD);
7390 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
7393 std::unique_ptr<CodeGenFunction> &CurCGF =
7394 GlobalTopLevelStmtBlockInFlight.first;
7398 if (CurCGF && CXXGlobalInits.back() != CurCGF->CurFn) {
7406 std::string Name =
"__stmts__" + llvm::utostr(CXXGlobalInits.size());
7412 llvm::Function *Fn = llvm::Function::Create(
7413 FnTy, llvm::GlobalValue::InternalLinkage, Name, &
getModule());
7416 GlobalTopLevelStmtBlockInFlight.second = D;
7417 CurCGF->StartFunction(
GlobalDecl(), RetTy, Fn, FnInfo, Args,
7419 CXXGlobalInits.push_back(Fn);
7422 CurCGF->EmitStmt(D->
getStmt());
7425 void CodeGenModule::EmitDeclContext(
const DeclContext *DC) {
7426 for (
auto *I : DC->
decls()) {
7432 if (
auto *OID = dyn_cast<ObjCImplDecl>(I)) {
7433 for (
auto *M : OID->methods())
7452 case Decl::CXXConversion:
7453 case Decl::CXXMethod:
7454 case Decl::Function:
7461 case Decl::CXXDeductionGuide:
7466 case Decl::Decomposition:
7467 case Decl::VarTemplateSpecialization:
7469 if (
auto *DD = dyn_cast<DecompositionDecl>(D))
7470 for (
auto *B : DD->bindings())
7471 if (
auto *HD = B->getHoldingVar())
7477 case Decl::IndirectField:
7481 case Decl::Namespace:
7482 EmitDeclContext(cast<NamespaceDecl>(D));
7484 case Decl::ClassTemplateSpecialization: {
7485 const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
7487 if (Spec->getSpecializationKind() ==
7489 Spec->hasDefinition())
7490 DI->completeTemplateDefinition(*Spec);
7492 case Decl::CXXRecord: {
7496 DI->EmitAndRetainType(
getContext().getRecordType(cast<RecordDecl>(D)));
7499 DI->completeUnusedClass(*CRD);
7502 for (
auto *I : CRD->
decls())
7503 if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
7508 case Decl::UsingShadow:
7509 case Decl::ClassTemplate:
7510 case Decl::VarTemplate:
7512 case Decl::VarTemplatePartialSpecialization:
7513 case Decl::FunctionTemplate:
7514 case Decl::TypeAliasTemplate:
7521 DI->EmitUsingDecl(cast<UsingDecl>(*D));
7523 case Decl::UsingEnum:
7525 DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(*D));
7527 case Decl::NamespaceAlias:
7529 DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
7531 case Decl::UsingDirective:
7533 DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
7535 case Decl::CXXConstructor:
7538 case Decl::CXXDestructor:
7542 case Decl::StaticAssert:
7549 case Decl::ObjCInterface:
7550 case Decl::ObjCCategory:
7553 case Decl::ObjCProtocol: {
7554 auto *Proto = cast<ObjCProtocolDecl>(D);
7555 if (Proto->isThisDeclarationADefinition())
7560 case Decl::ObjCCategoryImpl:
7563 ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
7566 case Decl::ObjCImplementation: {
7567 auto *OMD = cast<ObjCImplementationDecl>(D);
7568 EmitObjCPropertyImplementations(OMD);
7569 EmitObjCIvarInitializations(OMD);
7574 DI->getOrCreateInterfaceType(
getContext().getObjCInterfaceType(
7575 OMD->getClassInterface()), OMD->getLocation());
7578 case Decl::ObjCMethod: {
7579 auto *OMD = cast<ObjCMethodDecl>(D);
7585 case Decl::ObjCCompatibleAlias:
7586 ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
7589 case Decl::PragmaComment: {
7590 const auto *PCD = cast<PragmaCommentDecl>(D);
7591 switch (PCD->getCommentKind()) {
7593 llvm_unreachable(
"unexpected pragma comment kind");
7608 case Decl::PragmaDetectMismatch: {
7609 const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
7614 case Decl::LinkageSpec:
7615 EmitLinkageSpec(cast<LinkageSpecDecl>(D));
7618 case Decl::FileScopeAsm: {
7620 if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
7623 if (LangOpts.OpenMPIsTargetDevice)
7626 if (LangOpts.SYCLIsDevice)
7628 auto *AD = cast<FileScopeAsmDecl>(D);
7629 getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
7633 case Decl::TopLevelStmt:
7634 EmitTopLevelStmt(cast<TopLevelStmtDecl>(D));
7637 case Decl::Import: {
7638 auto *Import = cast<ImportDecl>(D);
7641 if (!ImportedModules.insert(Import->getImportedModule()))
7645 if (!Import->getImportedOwningModule()) {
7647 DI->EmitImportDecl(*Import);
7653 if (CXX20ModuleInits && Import->getImportedOwningModule() &&
7654 !Import->getImportedOwningModule()->isModuleMapModule())
7663 Visited.insert(Import->getImportedModule());
7664 Stack.push_back(Import->getImportedModule());
7666 while (!Stack.empty()) {
7668 if (!EmittedModuleInitializers.insert(Mod).second)
7678 if (Submodule->IsExplicit)
7681 if (
Visited.insert(Submodule).second)
7682 Stack.push_back(Submodule);
7689 EmitDeclContext(cast<ExportDecl>(D));
7692 case Decl::OMPThreadPrivate:
7696 case Decl::OMPAllocate:
7700 case Decl::OMPDeclareReduction:
7704 case Decl::OMPDeclareMapper:
7708 case Decl::OMPRequires:
7713 case Decl::TypeAlias:
7715 DI->EmitAndRetainType(
7716 getContext().getTypedefType(cast<TypedefNameDecl>(D)));
7722 DI->EmitAndRetainType(
getContext().getRecordType(cast<RecordDecl>(D)));
7728 DI->EmitAndRetainType(
getContext().getEnumType(cast<EnumDecl>(D)));
7731 case Decl::HLSLBuffer:
7739 assert(isa<TypeDecl>(D) &&
"Unsupported decl kind");
7746 if (!CodeGenOpts.CoverageMapping)
7749 case Decl::CXXConversion:
7750 case Decl::CXXMethod:
7751 case Decl::Function:
7752 case Decl::ObjCMethod:
7753 case Decl::CXXConstructor:
7754 case Decl::CXXDestructor: {
7755 if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
7760 DeferredEmptyCoverageMappingDecls.try_emplace(D,
true);
7770 if (!CodeGenOpts.CoverageMapping)
7772 if (
const auto *Fn = dyn_cast<FunctionDecl>(D)) {
7773 if (Fn->isTemplateInstantiation())
7776 DeferredEmptyCoverageMappingDecls.insert_or_assign(D,
false);
7784 for (
const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
7787 const Decl *D = Entry.first;
7789 case Decl::CXXConversion:
7790 case Decl::CXXMethod:
7791 case Decl::Function:
7792 case Decl::ObjCMethod: {
7799 case Decl::CXXConstructor: {
7806 case Decl::CXXDestructor: {
7823 if (llvm::Function *F =
getModule().getFunction(
"main")) {
7824 if (!F->isDeclaration() && F->arg_size() == 0 && !F->isVarArg() &&
7836 llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
7837 return llvm::ConstantInt::get(i64, PtrInt);
7841 llvm::NamedMDNode *&GlobalMetadata,
7843 llvm::GlobalValue *Addr) {
7844 if (!GlobalMetadata)
7846 CGM.
getModule().getOrInsertNamedMetadata(
"clang.global.decl.ptrs");
7849 llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
7852 GlobalMetadata->addOperand(llvm::MDNode::get(CGM.
getLLVMContext(), Ops));
7855 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue *Elem,
7856 llvm::GlobalValue *CppFunc) {
7864 if (Elem == CppFunc)
7870 for (llvm::User *User : Elem->users()) {
7874 if (
auto *ConstExpr = dyn_cast<llvm::ConstantExpr>(User)) {
7875 if (ConstExpr->getOpcode() != llvm::Instruction::BitCast)
7878 for (llvm::User *CEUser : ConstExpr->users()) {
7879 if (
auto *IFunc = dyn_cast<llvm::GlobalIFunc>(CEUser)) {
7880 IFuncs.push_back(IFunc);
7885 CEs.push_back(ConstExpr);
7886 }
else if (
auto *IFunc = dyn_cast<llvm::GlobalIFunc>(User)) {
7887 IFuncs.push_back(IFunc);
7899 for (llvm::GlobalIFunc *IFunc : IFuncs)
7900 IFunc->setResolver(
nullptr);
7901 for (llvm::ConstantExpr *ConstExpr : CEs)
7902 ConstExpr->destroyConstant();
7906 Elem->eraseFromParent();
7908 for (llvm::GlobalIFunc *IFunc : IFuncs) {
7913 llvm::FunctionType::get(IFunc->getType(),
false);
7914 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
7915 CppFunc->getName(), ResolverTy, {},
false);
7916 IFunc->setResolver(Resolver);
7926 void CodeGenModule::EmitStaticExternCAliases() {
7929 for (
auto &I : StaticExternCValues) {
7931 llvm::GlobalValue *Val = I.second;
7939 llvm::GlobalValue *ExistingElem =
7940 getModule().getNamedValue(Name->getName());
7944 if (!ExistingElem || CheckAndReplaceExternCIFuncs(ExistingElem, Val))
7951 auto Res = Manglings.find(MangledName);
7952 if (Res == Manglings.end())
7954 Result = Res->getValue();
7965 void CodeGenModule::EmitDeclMetadata() {
7966 llvm::NamedMDNode *GlobalMetadata =
nullptr;
7968 for (
auto &I : MangledDeclNames) {
7969 llvm::GlobalValue *Addr =
getModule().getNamedValue(I.second);
7979 void CodeGenFunction::EmitDeclMetadata() {
7980 if (LocalDeclMap.empty())
return;
7985 unsigned DeclPtrKind = Context.getMDKindID(
"clang.decl.ptr");
7987 llvm::NamedMDNode *GlobalMetadata =
nullptr;
7989 for (
auto &I : LocalDeclMap) {
7990 const Decl *D = I.first;
7991 llvm::Value *Addr = I.second.emitRawPointer(*
this);
7992 if (
auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
7994 Alloca->setMetadata(
7995 DeclPtrKind, llvm::MDNode::get(
7996 Context, llvm::ValueAsMetadata::getConstant(DAddr)));
7997 }
else if (
auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
8004 void CodeGenModule::EmitVersionIdentMetadata() {
8005 llvm::NamedMDNode *IdentMetadata =
8006 TheModule.getOrInsertNamedMetadata(
"llvm.ident");
8008 llvm::LLVMContext &Ctx = TheModule.getContext();
8010 llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
8011 IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
8014 void CodeGenModule::EmitCommandLineMetadata() {
8015 llvm::NamedMDNode *CommandLineMetadata =
8016 TheModule.getOrInsertNamedMetadata(
"llvm.commandline");
8018 llvm::LLVMContext &Ctx = TheModule.getContext();
8020 llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
8021 CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
8024 void CodeGenModule::EmitCoverageFile() {
8025 llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata(
"llvm.dbg.cu");
8029 llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata(
"llvm.gcov");
8030 llvm::LLVMContext &Ctx = TheModule.getContext();
8031 auto *CoverageDataFile =
8033 auto *CoverageNotesFile =
8035 for (
int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
8036 llvm::MDNode *CU = CUNode->getOperand(i);
8037 llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
8038 GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
8059 if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
8061 for (
auto RefExpr : D->
varlists()) {
8062 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
8064 VD->getAnyInitializer() &&
8065 !VD->getAnyInitializer()->isConstantInitializer(
getContext(),
8072 VD, Addr, RefExpr->getBeginLoc(), PerformInit))
8073 CXXGlobalInits.push_back(InitFunction);
8078 CodeGenModule::CreateMetadataIdentifierImpl(
QualType T, MetadataTypeMap &Map,
8082 FnType->getReturnType(), FnType->getParamTypes(),
8083 FnType->getExtProtoInfo().withExceptionSpec(
EST_None));
8085 llvm::Metadata *&InternalId = Map[
T.getCanonicalType()];
8090 std::string OutName;
8091 llvm::raw_string_ostream Out(OutName);
8096 Out <<
".normalized";
8110 return CreateMetadataIdentifierImpl(
T, MetadataIdMap,
"");
8115 return CreateMetadataIdentifierImpl(
T, VirtualMetadataIdMap,
".virtual");
8135 for (
auto &Param : FnType->param_types())
8140 GeneralizedParams, FnType->getExtProtoInfo());
8147 llvm_unreachable(
"Encountered unknown FunctionType");
8152 GeneralizedMetadataIdMap,
".generalized");
8159 return ((LangOpts.
Sanitize.
has(SanitizerKind::CFIVCall) &&
8161 (LangOpts.
Sanitize.
has(SanitizerKind::CFINVCall) &&
8163 (LangOpts.
Sanitize.
has(SanitizerKind::CFIDerivedCast) &&
8165 (LangOpts.
Sanitize.
has(SanitizerKind::CFIUnrelatedCast) &&
8172 llvm::Metadata *MD =
8174 VTable->addTypeMetadata(
Offset.getQuantity(), MD);
8176 if (CodeGenOpts.SanitizeCfiCrossDso)
8178 VTable->addTypeMetadata(
Offset.getQuantity(),
8179 llvm::ConstantAsMetadata::get(CrossDsoTypeId));
8182 llvm::Metadata *MD = llvm::MDString::get(
getLLVMContext(),
"all-vtables");
8183 VTable->addTypeMetadata(
Offset.getQuantity(), MD);
8189 SanStats = std::make_unique<llvm::SanitizerStatReport>(&
getModule());
8199 auto *FTy = llvm::FunctionType::get(SamplerT, {C->getType()},
false);
8214 bool forPointeeType) {
8225 if (
auto Align = TT->getDecl()->getMaxAlignment()) {
8255 if (
T.getQualifiers().hasUnaligned()) {
8257 }
else if (forPointeeType && !AlignForArray &&
8269 if (
unsigned MaxAlign =
getLangOpts().MaxTypeAlign) {
8282 if (NumAutoVarInit >= StopAfter) {
8285 if (!NumAutoVarInit) {
8288 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
8289 "number of times ftrivial-auto-var-init=%1 gets applied.");
8303 const Decl *D)
const {
8307 OS << (isa<VarDecl>(D) ?
".static." :
".intern.");
8309 OS << (isa<VarDecl>(D) ?
"__static__" :
"__intern__");
8315 assert(PLoc.
isValid() &&
"Source location is expected to be valid.");
8319 llvm::MD5::MD5Result Result;
8320 for (
const auto &Arg : PreprocessorOpts.
Macros)
8321 Hash.update(Arg.first);
8325 llvm::sys::fs::UniqueID
ID;
8328 assert(PLoc.
isValid() &&
"Source location is expected to be valid.");
8329 if (
auto EC = llvm::sys::fs::getUniqueID(PLoc.
getFilename(),
ID))
8330 SM.getDiagnostics().Report(diag::err_cannot_open_file)
8333 OS << llvm::format(
"%x",
ID.getFile()) << llvm::format(
"%x",
ID.getDevice())
8334 <<
"_" << llvm::utohexstr(Result.low(),
true, 8);
8341 assert(DeferredDeclsToEmit.empty() &&
8342 "Should have emitted all decls deferred to emit.");
8343 assert(NewBuilder->DeferredDecls.empty() &&
8344 "Newly created module should not have deferred decls");
8345 NewBuilder->DeferredDecls = std::move(DeferredDecls);
8346 assert(EmittedDeferredDecls.empty() &&
8347 "Still have (unmerged) EmittedDeferredDecls deferred decls");
8349 assert(NewBuilder->DeferredVTables.empty() &&
8350 "Newly created module should not have deferred vtables");
8351 NewBuilder->DeferredVTables = std::move(DeferredVTables);
8353 assert(NewBuilder->MangledDeclNames.empty() &&
8354 "Newly created module should not have mangled decl names");
8355 assert(NewBuilder->Manglings.empty() &&
8356 "Newly created module should not have manglings");
8357 NewBuilder->Manglings = std::move(Manglings);
8359 NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
8361 NewBuilder->TBAA = std::move(TBAA);
8363 NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
8367 llvm::AttributeList &AttrList,
8368 llvm::Metadata *&MD,
8370 const llvm::Type *FuncType) {
8372 getDefaultFunctionFPAccuracyAttributes(Name, FuncAttrs, MD,
ID, FuncType);
8373 AttrList = llvm::AttributeList::get(
8374 getLLVMContext(), llvm::AttributeList::FunctionIndex, FuncAttrs);
Defines the clang::ASTContext interface.
ASTImporterLookupTable & LT
This file provides some common utility functions for processing Lambda related AST Constructs.
Defines the Diagnostic-related interfaces.
Defines enum values for all the target-independent builtin functions.
static llvm::Constant * GetPointerConstant(llvm::LLVMContext &Context, const void *Ptr)
Turns the given pointer into a constant.
static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule &CGM, const CPUSpecificAttr *Attr, unsigned CPUIndex, raw_ostream &Out)
static bool AllTrivialInitializers(CodeGenModule &CGM, ObjCImplementationDecl *D)
static llvm::MDNode * getAspectsMD(ASTContext &ASTContext, llvm::LLVMContext &Ctx, StringRef Name, const SYCLUsesAspectsAttr *A)
static void checkAliasForTocData(llvm::GlobalVariable *GVar, const CodeGenOptions &CodeGenOpts, DiagnosticsEngine &Diags, SourceLocation Location)
static bool SYCLCUDAIsSYCLDevice(const clang::LangOptions &LangOpts)
llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM, GlobalDecl GD)
static bool hasImplicitAttr(const ValueDecl *D)
static void emitUsed(CodeGenModule &CGM, StringRef Name, std::vector< llvm::WeakTrackingVH > &List)
static llvm::StringMapEntry< llvm::GlobalVariable * > & GetConstantCFStringEntry(llvm::StringMap< llvm::GlobalVariable * > &Map, const StringLiteral *Literal, bool TargetIsLSB, bool &IsUTF16, unsigned &StringLength)
static bool HasNonDllImportDtor(QualType T)
static FunctionDecl * createDefaultTargetVersionFrom(const FunctionDecl *FD)
static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S)
static bool SYCLCUDAIsHost(const clang::LangOptions &LangOpts)
static unsigned TargetMVPriority(const TargetInfo &TI, const CodeGenFunction::MultiVersionResolverOption &RO)
static llvm::MDNode * getAspectEnumValueMD(ASTContext &ASTContext, llvm::LLVMContext &Ctx, const EnumConstantDecl *ECD)
static void setVisibilityFromDLLStorageClass(const clang::LangOptions &LO, llvm::Module &M)
static std::string getCPUSpecificMangling(const CodeGenModule &CGM, StringRef Name)
static llvm::Constant * castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, llvm::GlobalVariable *GV)
static const char AnnotationSection[]
static bool isUniqueInternalLinkageDecl(GlobalDecl GD, CodeGenModule &CGM)
static QualType GeneralizeFunctionType(ASTContext &Ctx, QualType Ty)
static bool allowKCFIIdentifier(StringRef Name)
static bool shouldAssumeDSOLocal(const CodeGenModule &CGM, llvm::GlobalValue *GV)
static void replaceUsesOfNonProtoConstant(llvm::Constant *old, llvm::Function *newFn)
Replace the uses of a function that was declared with a non-proto type.
void applySYCLAspectsMD(AttrT *A, ASTContext &ACtx, llvm::LLVMContext &LLVMCtx, llvm::Function *F, StringRef MDName)
static CGCXXABI * createCXXABI(CodeGenModule &CGM)
static const llvm::GlobalValue * getAliasedGlobal(const llvm::GlobalValue *GV)
static void addSYCLUniqueID(llvm::GlobalVariable *GV, const VarDecl *VD, ASTContext &Context)
static bool needsDestructMethod(ObjCImplementationDecl *impl)
static bool isStackProtectorOn(const LangOptions &LangOpts, const llvm::Triple &Triple, clang::LangOptions::StackProtectorMode Mode)
static void removeImageAccessQualifier(std::string &TyName)
static bool hasUnwindExceptions(const LangOptions &LangOpts)
Determines whether the language options require us to model unwind exceptions.
static void addLinkOptionsPostorder(CodeGenModule &CGM, Module *Mod, SmallVectorImpl< llvm::MDNode * > &Metadata, llvm::SmallPtrSet< Module *, 16 > &Visited)
Add link options implied by the given module, including modules it depends on, using a postorder walk...
static std::unique_ptr< TargetCodeGenInfo > createTargetCodeGenInfo(CodeGenModule &CGM)
void setLLVMVisibility(llvm::GlobalValue &GV, std::optional< llvm::GlobalValue::VisibilityTypes > V)
static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D)
static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD, const NamedDecl *ND, bool OmitMultiVersionMangling=false)
static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND)
static const FunctionDecl * GetRuntimeFunctionDecl(ASTContext &C, StringRef Name)
static unsigned ArgInfoAddressSpace(LangAS AS)
static QualType GeneralizeType(ASTContext &Ctx, QualType Ty)
static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old, llvm::Function *NewFn)
ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we implement a function with...
static void maybeEmitPipeStorageMetadata(const VarDecl *D, llvm::GlobalVariable *GV, CodeGenModule &CGM)
static bool isVarDeclStrongDefinition(const ASTContext &Context, CodeGenModule &CGM, const VarDecl *D, bool NoCommon)
static llvm::GlobalVariable * GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT, CodeGenModule &CGM, StringRef GlobalName, CharUnits Alignment)
static llvm::cl::opt< bool > LimitedCoverage("limited-coverage-experimental", llvm::cl::Hidden, llvm::cl::desc("Emit limited coverage mapping information (experimental)"))
static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule &CGM, const CXXMethodDecl *MD)
static bool checkAliasedGlobal(const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location, bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV, const llvm::MapVector< GlobalDecl, StringRef > &MangledDeclNames, SourceRange AliasRange)
static bool checkIfDeclaredInSYCLNamespace(const Decl *D)
Function checks whether given DeclContext contains a topmost namespace with name "sycl".
static std::optional< llvm::GlobalValue::VisibilityTypes > getLLVMVisibility(clang::LangOptions::VisibilityFromDLLStorageClassKinds K)
static void EmitGlobalDeclMetadata(CodeGenModule &CGM, llvm::NamedMDNode *&GlobalMetadata, GlobalDecl D, llvm::GlobalValue *Addr)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Defines the clang::FileManager interface and associated types.
llvm::DenseSet< const void * > Visited
llvm::MachO::Target Target
llvm::MachO::Record Record
Defines the clang::Module class, which describes a module in the source code.
static const NamedDecl * getDefinition(const Decl *D)
This file declares semantic analysis for SYCL constructs.
Defines the SourceManager interface.
static CharUnits getTypeAllocSize(CodeGenModule &CGM, llvm::Type *type)
Defines version macros and version-related utility functions for Clang.
__DEVICE__ int max(int __a, int __b)
__device__ __2f16 float __ockl_bool s
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const clang::PrintingPolicy & getPrintingPolicy() const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
SourceManager & getSourceManager()
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
@ Strong
Strong definition.
@ WeakUnknown
Weak for now, might become strong later in this TU.
const XRayFunctionFilter & getXRayFilter() const
llvm::StringMap< SectionInfo > SectionInfos
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl.
QualType getMemberPointerType(QualType T, const Type *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
QualType getRecordType(const RecordDecl *Decl) const
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
const NoSanitizeList & getNoSanitizeList() const
QualType getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info) const
Return a K&R style C function type like 'int()'.
const ProfileList & getProfileList() const
bool shouldExternalize(const Decl *D) const
Whether a C++ static variable or CUDA/HIP kernel should be externalized.
FullSourceLoc getFullLoc(SourceLocation Loc) const
ArrayRef< Decl * > getModuleInitializers(Module *M)
Get the initializations to perform when importing a module, if any.
bool DeclMustBeEmitted(const Decl *D)
Determines if the decl can be CodeGen'ed or deserialized from PCH lazily, only when used; this is onl...
RecordDecl * buildImplicitRecord(StringRef Name, RecordDecl::TagKind TK=RecordDecl::TagKind::Struct) const
Create a new implicit TU-level CXXRecordDecl or RecordDecl declaration.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
StringRef getCUIDHash() const
bool isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const
Returns true if this is an inline-initialized static data member which is treated as a definition for...
Builtin::Context & BuiltinInfo
QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, const Expr *SizeExpr, ArraySizeModifier ASM, unsigned IndexTypeQuals) const
Return the unique reference to the type for a constant array of the specified element type.
SelectorTable & Selectors
const ConstantArrayType * getAsConstantArrayType(QualType T) const
const LangOptions & getLangOpts() const
void forEachMultiversionedFunctionVersion(const FunctionDecl *FD, llvm::function_ref< void(FunctionDecl *)> Pred) const
Visits all versions of a multiversioned function with the passed predicate.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
QualType getCFConstantStringType() const
Return the C structure type used to represent constant CFStrings.
GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD) const
CanQualType UnsignedLongTy
bool isAlignmentRequired(const Type *T) const
Determine if the alignment the type has was required using an alignment attribute.
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CharUnits getAlignOfGlobalVarInChars(QualType T, const VarDecl *VD) const
Return the alignment in characters that should be given to a global variable with type T.
GVALinkage GetGVALinkageForVariable(const VarDecl *VD) const
QualType getObjCIdType() const
Represents the Objective-CC id type.
llvm::DenseSet< const ValueDecl * > CUDAExternalDeviceDeclODRUsedByHost
Keep track of CUDA/HIP external kernels or device variables ODR-used by host code.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
unsigned getTypeAlignIfKnown(QualType T, bool NeedsPreferredAlignment=false) const
Return the alignment of a type, in bits, or 0 if the type is incomplete and we cannot determine the a...
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
const TargetInfo & getTargetInfo() const
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
void getFunctionFeatureMap(llvm::StringMap< bool > &FeatureMap, const FunctionDecl *) const
TargetCXXABI::Kind getCXXABIKind() const
Return the C++ ABI kind that should be used.
unsigned getTargetAddressSpace(LangAS AS) const
QualType getWideCharType() const
Return the type of wide characters.
InlineVariableDefinitionKind getInlineVariableDefinitionKind(const VarDecl *VD) const
Determine whether a definition of this inline variable should be treated as a weak or strong definiti...
TranslationUnitDecl * getTranslationUnitDecl() const
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Attr - This represents one attribute.
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Holds information about both target-independent and target-specific builtins, allowing easy queries b...
bool isLibFunction(unsigned ID) const
Return true if this is a builtin for a libc/libm function, with a "__builtin_" prefix (e....
llvm::StringRef getName(unsigned ID) const
Return the identifier name for the specified builtin, e.g.
Represents a base class of a C++ class.
Represents binding an expression to a temporary.
CXXTemporary * getTemporary()
Represents a call to a C++ constructor.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Represents a C++ base or member initializer.
Expr * getInit() const
Get the initializer.
Represents a delete expression for memory deallocation and destructor calls, e.g.
FunctionDecl * getOperatorDelete() const
Represents a C++ destructor within a class.
Represents a call to a member function that may be written either with member call syntax (e....
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Represents a static or instance method of a struct/union/class.
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
FunctionDecl * getOperatorNew() const
Represents a C++ struct/union/class.
bool hasMutableFields() const
Determine whether this class, or any of its class subobjects, contains a mutable field.
unsigned getNumBases() const
Retrieves the number of base classes of this class.
bool hasDefinition() const
const CXXDestructorDecl * getDestructor() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
CharUnits - This is an opaque type for sizes expressed in character units.
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static CharUnits One()
One - Construct a CharUnits quantity of one.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::string RecordCommandLine
The string containing the commandline for the llvm.commandline metadata, if non-empty.
std::string FloatABI
The ABI to use for passing floating point arguments.
llvm::Reloc::Model RelocationModel
The name of the relocation model to use.
llvm::DenormalMode FPDenormalMode
The floating-point denormal mode to use.
std::string CoverageNotesFile
The filename with path we use for coverage notes files.
std::string ProfileInstrumentUsePath
Name of the profile file to use as input for -fprofile-instr-use.
std::string MemoryProfileOutput
Name of the profile file to use as output for with -fmemory-profile.
std::vector< std::string > TocDataVarsUserSpecified
List of global variables explicitly specified by the user as toc-data.
std::string CoverageDataFile
The filename with path we use for coverage data files.
llvm::DenormalMode FP32DenormalMode
The floating-point denormal mode to use, for float.
SanitizerSet SanitizeTrap
Set of sanitizer checks that trap rather than diagnose.
std::string ProfileRemappingFile
Name of the profile remapping file to apply to the profile data supplied by -fprofile-sample-use or -...
bool hasProfileClangUse() const
Check if Clang profile use is on.
std::string SymbolPartition
The name of the partition that symbols are assigned to, specified with -fsymbol-partition (see https:...
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions.
virtual void appendAttributeMangling(TargetAttr *Attr, raw_ostream &Out) const
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
virtual llvm::GlobalValue * getKernelHandle(llvm::Function *Stub, GlobalDecl GD)=0
Get kernel handle by stub function.
virtual void handleVarRegistration(const VarDecl *VD, llvm::GlobalVariable &Var)=0
Check whether a variable is a device variable and register it if true.
virtual void internalizeDeviceSideVar(const VarDecl *D, llvm::GlobalValue::LinkageTypes &Linkage)=0
Adjust linkage of shadow variables in host compilation.
Implements C++ ABI-specific code generation functions.
virtual void EmitCXXConstructors(const CXXConstructorDecl *D)=0
Emit constructor variants required by this ABI.
virtual void EmitCXXDestructors(const CXXDestructorDecl *D)=0
Emit destructor variants required by this ABI.
virtual void setCXXDestructorDLLStorage(llvm::GlobalValue *GV, const CXXDestructorDecl *Dtor, CXXDtorType DT) const
MangleContext & getMangleContext()
Gets the mangle context.
virtual llvm::Constant * getAddrOfRTTIDescriptor(QualType Ty)=0
virtual llvm::GlobalValue::LinkageTypes getCXXDestructorLinkage(GVALinkage Linkage, const CXXDestructorDecl *Dtor, CXXDtorType DT) const
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
void AddStringLiteralDebugInfo(llvm::GlobalVariable *GV, const StringLiteral *S)
DebugInfo isn't attached to string literals by default.
CGFunctionInfo - Class to encapsulate the information about a function definition.
void addBuffer(const HLSLBufferDecl *D)
llvm::Type * getSamplerType(const Type *T)
void emitDeferredTargetDecls() const
Emit deferred declare target variables marked for deferred emission.
virtual void emitDeclareTargetFunction(const FunctionDecl *FD, llvm::GlobalValue *GV)
Emit code for handling declare target functions in the runtime.
virtual ConstantAddress getAddrOfDeclareTargetVar(const VarDecl *VD)
Returns the address of the variable marked as declare target with link clause OR as declare target wi...
bool hasRequiresUnifiedSharedMemory() const
Return whether the unified_shared_memory has been specified.
virtual void emitDeclareSimdFunction(const FunctionDecl *FD, llvm::Function *Fn)
Marks function Fn with properly mangled versions of vector functions.
virtual void registerTargetGlobalVariable(const VarDecl *VD, llvm::Constant *Addr)
Checks if the provided global decl GD is a declare target variable and registers it when emitting cod...
Class supports emissionof SIMD-only code.
bool actOnGlobalVarEmit(CodeGenModule &CGM, const VarDecl &D, llvm::Value *Addr)
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
void GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo)
llvm::LLVMContext & getLLVMContext()
void EmitCfiCheckFail()
Emit a cross-DSO CFI failure handling function.
void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, ObjCMethodDecl *MD, bool ctor)
void GenerateObjCGetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCGetter - Synthesize an Objective-C property getter function.
void EmitCfiCheckStub()
Emit a stub for the cross-DSO CFI check function.
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
void GenerateObjCMethod(const ObjCMethodDecl *OMD)
Generate an Objective-C method.
void GenerateObjCSetter(ObjCImplementationDecl *IMP, const ObjCPropertyImplDecl *PID)
GenerateObjCSetter - Synthesize an Objective-C property setter function for the given property.
bool isTrivialInitializer(const Expr *Init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
This class organizes the cross-function state that is used while generating LLVM code.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD)
Get the address of a GUID.
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
void AddVTableTypeMetadata(llvm::GlobalVariable *VTable, CharUnits Offset, const CXXRecordDecl *RD)
Create and attach type metadata for the given vtable.
void UpdateCompletedType(const TagDecl *TD)
llvm::MDNode * getTBAAAccessTagInfo(TBAAAccessInfo Info)
getTBAAAccessTagInfo - Get TBAA tag for a given memory access.
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
const TargetInfo & getTarget() const
llvm::GlobalVariable::ThreadLocalMode GetDefaultLLVMTLSModel() const
Get LLVM TLS mode from CodeGenOptions.
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
void setDSOLocal(llvm::GlobalValue *GV) const
llvm::MDNode * getTBAAStructInfo(QualType QTy)
llvm::Constant * EmitAnnotationArgs(const AnnotateAttr *Attr)
Emit additional args of the annotation.
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
CodeGenTypes & getTypes()
bool NeedAllVtablesTypeId() const
Returns whether this module needs the "all-vtables" type identifier.
void addCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
llvm::ConstantInt * CreateCrossDsoCfiTypeId(llvm::Metadata *MD)
Generate a cross-DSO type identifier for MD.
const llvm::DataLayout & getDataLayout() const
CharUnits GetTargetTypeStoreSize(llvm::Type *Ty) const
Return the store size, in character units, of the given LLVM type.
bool getExpressionLocationsEnabled() const
Return true if we should emit location information for expressions.
void addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C)
void getFPAccuracyFuncAttributes(StringRef Name, llvm::AttributeList &AttrList, llvm::Metadata *&MDs, unsigned ID, const llvm::Type *FuncType)
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
void setGVPropertiesAux(llvm::GlobalValue *GV, const NamedDecl *D) const
void EmitMainVoidAlias()
Emit an alias for "main" if it has no arguments (needed for wasm).
void addGlobalIntelFPGAAnnotation(const VarDecl *VD, llvm::GlobalValue *GV)
void DecorateInstructionWithInvariantGroup(llvm::Instruction *I, const CXXRecordDecl *RD)
Adds !invariant.barrier !tag to instruction.
bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
llvm::Constant * EmitAnnotateAttr(llvm::GlobalValue *GV, const AnnotateAttr *AA, SourceLocation L)
Generate the llvm::ConstantStruct which contains the annotation information for a given GlobalValue.
llvm::GlobalValue::LinkageTypes getLLVMLinkageForDeclarator(const DeclaratorDecl *D, GVALinkage Linkage)
Returns LLVM linkage for a declarator.
llvm::Module & getModule() const
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, TBAAAccessInfo SrcInfo)
mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the purposes of memory transfer call...
void setAspectsEnumDecl(const EnumDecl *ED)
CodeGenVTables & getVTables()
llvm::Constant * EmitAnnotationLineNo(SourceLocation L)
Emit the annotation line number.
QualType getObjCFastEnumerationStateType()
Retrieve the record type that describes the state of an Objective-C fast enumeration loop (for....
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const
bool shouldEmitRTTI(bool ForEH=false)
void EmitGlobal(GlobalDecl D)
Emit code for a single global function or var decl.
const LangOptions & getLangOpts() const
llvm::Metadata * CreateMetadataIdentifierForType(QualType T)
Create a metadata identifier for the given type.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
void AppendLinkerOptions(StringRef Opts)
Appends Opts to the "llvm.linker.options" metadata value.
const llvm::Triple & getTriple() const
void AddDependentLib(StringRef Lib)
Appends a dependent lib to the appropriate metadata value.
CGSYCLRuntime & getSYCLRuntime()
Return a reference to the configured SYCL runtime.
void Release()
Finalize LLVM code generation.
ProfileList::ExclusionType isFunctionBlockedByProfileList(llvm::Function *Fn, SourceLocation Loc) const
llvm::MDNode * getTBAABaseTypeInfo(QualType QTy)
getTBAABaseTypeInfo - Get metadata that describes the given base access type.
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
bool lookupRepresentativeDecl(StringRef MangledName, GlobalDecl &Result) const
void EmitOMPAllocateDecl(const OMPAllocateDecl *D)
Emit a code for the allocate directive.
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
llvm::LLVMContext & getLLVMContext()
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD)
Returns LLVM linkage for a declarator.
bool HasHiddenLTOVisibility(const CXXRecordDecl *RD)
Returns whether the given record has hidden LTO visibility and therefore may participate in (single-m...
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
TBAAAccessInfo getTBAAVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getTBAAVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table poi...
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
llvm::Constant * GetFunctionStart(const ValueDecl *Decl)
static llvm::GlobalValue::VisibilityTypes GetLLVMVisibility(Visibility V)
void EmitTentativeDefinition(const VarDecl *D)
void EmitDeferredUnusedCoverageMappings()
Emit all the deferred coverage mappings for the uninstrumented functions.
void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
bool imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc, StringRef Category=StringRef()) const
Imbue XRay attributes to a function, applying the always/never attribute lists in the process.
llvm::Metadata * CreateMetadataIdentifierGeneralized(QualType T)
Create a metadata identifier for the generalization of the given type.
void EmitGlobalAnnotations()
Emit all the global annotations.
llvm::Constant * getAddrOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the constructor/destructor of the given type.
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
llvm::Constant * EmitSYCLAnnotationArgs(SmallVectorImpl< std::pair< std::string, std::string >> &Pairs)
Emit additional args of the annotation.
CGDebugInfo * getModuleDebugInfo()
CGCXXABI & getCXXABI() const
SmallVector< const CXXRecordDecl *, 0 > getMostBaseClasses(const CXXRecordDecl *RD)
Return a vector of most-base classes for RD.
void AddDeferredUnusedCoverageMapping(Decl *D)
Stored a deferred empty coverage mapping for an unused and thus uninstrumented top level declaration.
void MaybeHandleStaticInExternC(const SomeDecl *D, llvm::GlobalValue *GV)
If the declaration has internal linkage but is inside an extern "C" linkage specification,...
void DecorateInstructionWithTBAA(llvm::Instruction *Inst, TBAAAccessInfo TBAAInfo)
DecorateInstructionWithTBAA - Decorate the instruction with a TBAA tag.
CGHLSLRuntime & getHLSLRuntime()
Return a reference to the configured HLSL runtime.
llvm::GlobalVariable::LinkageTypes getFunctionLinkage(GlobalDecl GD)
void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535, bool IsDtorAttrFunc=false)
AddGlobalDtor - Add a function to the list that will be called when the module is unloaded.
DiagnosticsEngine & getDiags() const
llvm::Constant * CreateRuntimeVariable(llvm::Type *Ty, StringRef Name)
Create a new runtime global variable with the specified type and name.
void ConstructAttributeList(StringRef Name, const CGFunctionInfo &Info, CGCalleeInfo CalleeInfo, llvm::AttributeList &Attrs, unsigned &CallingConv, bool AttrOnCallSite, bool IsThunk)
Get the LLVM attributes and calling convention to use for a particular function type.
llvm::Constant * GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, LangAS AddrSpace, const VarDecl *D, ForDefinition_t IsForDefinition=NotForDefinition)
GetOrCreateLLVMGlobal - If the specified mangled name is not in the module, create and return an llvm...
TBAAAccessInfo getTBAAAccessInfo(QualType AccessType)
getTBAAAccessInfo - Get TBAA information that describes an access to an object of the given type.
void setFunctionLinkage(GlobalDecl GD, llvm::Function *F)
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal)
Return a pointer to a constant CFString object for the given string.
ProfileList::ExclusionType isFunctionBlockedFromProfileInstr(llvm::Function *Fn, SourceLocation Loc) const
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
ConstantAddress GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
ConstantAddress GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO)
Get the address of a template parameter object.
void EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl *D)
Emit a code for threadprivate directive.
CGCUDARuntime & getCUDARuntime()
Return a reference to the configured CUDA runtime.
ConstantAddress GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD)
Get the address of a UnnamedGlobalConstant.
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purposes of type casts.
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
llvm::SanitizerStatReport & getSanStats()
const std::string & getModuleNameHash() const
llvm::Constant * EmitAnnotationString(StringRef Str)
Emit an annotation string.
void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare mapper construct.
void EmitExternalDeclaration(const VarDecl *D)
bool supportsCOMDAT() const
void RefreshTypeCacheForClass(const CXXRecordDecl *Class)
llvm::MDNode * getTBAATypeInfo(QualType QTy)
getTBAATypeInfo - Get metadata used to describe accesses to objects of the given type.
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD)
Tell the consumer that this variable has been instantiated.
const TargetCodeGenInfo & getTargetCodeGenInfo()
StringRef getMangledName(GlobalDecl GD)
ASTContext & getContext() const
llvm::Constant * GetConstantArrayFromStringLiteral(const StringLiteral *E)
Return a constant array for the given string.
void SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV)
Set attributes which are common to any form of a global definition (alias, Objective-C method,...
std::optional< CharUnits > getOMPAllocateAlignment(const VarDecl *VD)
Return the alignment specified in an allocate directive, if present.
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage, llvm::Align Alignment)
Will return a global variable of the given type.
CharUnits getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purposes of conditional ope...
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
void GenKernelArgMetadata(llvm::Function *FN, const FunctionDecl *FD=nullptr, CodeGenFunction *CGF=nullptr)
OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument information in the program executab...
void CreateFunctionTypeMetadataForIcall(const FunctionDecl *FD, llvm::Function *F)
Create and attach type metadata to the given function.
void setKCFIType(const FunctionDecl *FD, llvm::Function *F)
Set type metadata to the given function.
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO)
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
void AddDetectMismatch(StringRef Name, StringRef Value)
Appends a detect mismatch command to the linker options.
void AddGlobalSYCLIRAttributes(llvm::GlobalVariable *GV, const RecordDecl *RD)
Add attributes from add_ir_attributes_global_variable on TND to GV.
void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const
const CodeGenOptions & getCodeGenOpts() const
llvm::Value * createOpenCLIntToSamplerConversion(const Expr *E, CodeGenFunction &CGF)
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
LangAS GetGlobalVarAddressSpace(const VarDecl *D)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
void SetLLVMFunctionAttributes(GlobalDecl GD, const CGFunctionInfo &Info, llvm::Function *F, bool IsThunk)
Set the LLVM function attributes (sext, zext, etc).
void generateIntelFPGAAnnotation(const Decl *D, llvm::SmallString< 256 > &AnnotStr)
void addReplacement(StringRef Name, llvm::Constant *C)
llvm::ConstantInt * CreateKCFITypeId(QualType T)
Generate a KCFI type identifier for T.
void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535, unsigned LexOrder=~0U, llvm::Constant *AssociatedData=nullptr)
AddGlobalCtor - Add a function to the list that will be called before main() runs.
void SetLLVMFunctionAttributesForDefinition(const Decl *D, llvm::Function *F)
Set the LLVM function attributes which only apply to a function definition.
llvm::Metadata * CreateMetadataIdentifierForVirtualMemPtrType(QualType T)
Create a metadata identifier that is intended to be used to check virtual calls via a member function...
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=std::nullopt)
ConstantAddress GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
const GlobalDecl getMangledNameDecl(StringRef)
void ClearUnusedCoverageMapping(const Decl *D)
Remove the deferred empty coverage mapping as this declaration is actually instrumented.
void EmitTopLevelDecl(Decl *D)
Emit code for a single top level declaration.
llvm::Constant * EmitAnnotationUnit(SourceLocation Loc)
Emit the annotation's translation unit.
ConstantAddress GetAddrOfConstantCString(const std::string &Str, const char *GlobalName=nullptr)
Returns a pointer to a character array containing the literal and a terminating '\0' character.
std::vector< Structor > CtorList
void printPostfixForExternalizedDecl(llvm::raw_ostream &OS, const Decl *D) const
Print the postfix for externalized static variable or kernels for single source offloading languages ...
void moveLazyEmissionStates(CodeGenModule *NewBuilder)
Move some lazily-emitted states to the NewBuilder.
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
void finalizeKCFITypes()
Emit KCFI type identifier constants and remove unused identifiers.
void setValueProfilingFlag(llvm::Module &M)
void setProfileVersion(llvm::Module &M)
void emitEmptyCounterMapping(const Decl *D, StringRef FuncName, llvm::GlobalValue::LinkageTypes Linkage)
Emit a coverage mapping range with a counter zero for an unused declaration.
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
C++ methods have some special rules and also have implicit parameters.
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
void RefreshTypeCacheForClass(const CXXRecordDecl *RD)
Remove stale types from the type cache when an inheritance model gets assigned to a class.
void UpdateCompletedType(const TagDecl *TD)
UpdateCompletedType - When we find the full definition for a TagDecl, replace the 'opaque' type we pr...
const CGFunctionInfo & arrangeGlobalDeclaration(GlobalDecl GD)
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
void GenerateClassData(const CXXRecordDecl *RD)
GenerateClassData - Generate all the class data required to be generated upon definition of a KeyFunc...
void EmitThunks(GlobalDecl GD)
EmitThunks - Emit the associated thunks for the given global decl.
A specialization of Address that requires the address to be an LLVM Constant.
static ConstantAddress invalid()
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
void finalize(llvm::GlobalVariable *global)
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
The standard implementation of ConstantInitBuilder used in Clang.
Organizes the cross-function state that is used while generating code coverage mapping data.
FunctionArgList - Type for representing both the decl and type of parameters to a function.
bool hasDiagnostics()
Whether or not the stats we've gathered indicate any potential problems.
void reportDiagnostics(DiagnosticsEngine &Diags, StringRef MainFile)
Report potential problems we've found to Diags.
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
const T & getABIInfo() const
virtual void getDependentLibraryOption(llvm::StringRef Lib, llvm::SmallString< 24 > &Opt) const
Gets the linker options necessary to link a dependent library on this platform.
Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, LangAS SrcAddr, LangAS DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
virtual LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, const VarDecl *D) const
Get target favored AST address space of a global variable for languages other than OpenCL and CUDA.
virtual void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const
setTargetAttributes - Provides a convenient hook to handle extra target-specific attributes for the g...
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
virtual void emitTargetMetadata(CodeGen::CodeGenModule &CGM, const llvm::MapVector< GlobalDecl, StringRef > &MangledDeclNames) const
emitTargetMetadata - Provides a convenient hook to handle extra target-specific metadata for the give...
virtual void emitTargetGlobals(CodeGen::CodeGenModule &CGM) const
Provides a convenient hook to handle extra target-specific globals.
virtual void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, llvm::SmallString< 32 > &Opt) const
Gets the linker options necessary to detect object file mismatches on this platform.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Represents the canonical version of C arrays with a specified constant size.
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Stores additional source code information like skipped ranges which is required by the coverage mappi...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
void addDecl(Decl *D)
Add the declaration D into this context.
DeclContext * getEnclosingNamespaceContext()
Retrieve the nearest enclosing namespace context.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
A reference to a declared variable, function, enum, etc.
Decl - This represents one declaration (or definition), e.g.
SourceLocation getEndLoc() const LLVM_READONLY
ASTContext & getASTContext() const LLVM_READONLY
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
bool isWeakImported() const
Determine whether this is a weak-imported symbol.
unsigned getMaxAlignment() const
getMaxAlignment - return the maximum alignment specified by attributes on this decl,...
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
SourceLocation getLocation() const
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
SourceLocation getBeginLoc() const LLVM_READONLY
Module * getOwningModule() const
Get the module that owns this declaration (for visibility purposes).
DeclContext * getDeclContext()
The name of a declaration.
Represents a ValueDecl that came out of a declarator.
SourceLocation getBeginLoc() const LLVM_READONLY
TypeSourceInfo * getTypeSourceInfo() const
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
An instance of this object exists for each enum constant that is defined.
llvm::APSInt getInitVal() const
enumerator_range enumerators() const
This represents one expression.
Represents a member of a struct/union/class.
static FieldDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, InClassInitStyle InitStyle)
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
StringRef getName() const
The name of this FileEntry.
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Represents a function declaration or definition.
bool isTargetClonesMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target-clones functional...
bool isMultiVersion() const
True if this function is considered a multiversioned function.
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
bool isImmediateFunction() const
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
bool isInlined() const
Determine whether this function should be inlined, because it is either marked "inline" or "constexpr...
void setIsMultiVersion(bool V=true)
Sets the multiversion state for this declaration and all of its redeclarations.
bool isCPUSpecificMultiVersion() const
True if this function is a multiversioned processor specific function as a part of the cpu_specific/c...
FunctionDecl * getTemplateInstantiationPattern(bool ForDefinition=true) const
Retrieve the function declaration from which this function could be instantiated, if it is an instant...
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
StorageClass getStorageClass() const
Returns the storage class as written in the source.
bool isInlineBuiltinDeclaration() const
Determine if this function provides an inline implementation of a builtin.
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin=false, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=ConstexprSpecKind::Unspecified, Expr *TrailingRequiresClause=nullptr)
bool isTargetVersionMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target-version functiona...
bool isReplaceableGlobalAllocationFunction(std::optional< unsigned > *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions: void *operato...
bool isCPUDispatchMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the cpu_specific/cpu_dispatc...
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
TemplateSpecializationKind getTemplateSpecializationKind() const
Determine what kind of template instantiation this function represents.
bool doesDeclarationForceExternallyVisibleDefinition() const
For a function declaration in C or C++, determine whether this declaration causes the definition to b...
bool isTargetMultiVersion() const
True if this function is a multiversioned dispatch function as a part of the target functionality.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
DeclarationNameInfo getNameInfo() const
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
FunctionDecl * getDefinition()
Get the definition for this declaration.
bool isDefined(const FunctionDecl *&Definition, bool CheckForPendingFriendDefinition=false) const
Returns true if the function has a definition that does not need to be instantiated.
MultiVersionKind getMultiVersionKind() const
Gets the kind of multiversioning attribute this declaration has.
void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const override
Appends a human-readable name for this declaration into the given stream.
const ParmVarDecl * getParamDecl(unsigned i) const
Represents a K&R-style 'int foo()' function, which has no information available about its arguments.
Represents a prototype with parameter type info, e.g.
FunctionType - C99 6.7.5.3 - Function Declarators.
CallingConv getCallConv() const
GlobalDecl - represents a global declaration.
GlobalDecl getWithMultiVersionIndex(unsigned Index)
CXXCtorType getCtorType() const
GlobalDecl getWithKernelReferenceKind(KernelReferenceKind Kind)
GlobalDecl getCanonicalDecl() const
KernelReferenceKind getKernelReferenceKind() const
GlobalDecl getWithDecl(const Decl *D)
unsigned getMultiVersionIndex() const
const Decl * getDecl() const
CXXDtorType getDtorType() const
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
@ None
No signing for any function.
@ Swift5_0
Interoperability with the Swift 5.0 runtime.
@ Swift
Interoperability with the latest known version of the Swift runtime.
@ Swift4_2
Interoperability with the Swift 4.2 runtime.
@ Swift4_1
Interoperability with the Swift 4.1 runtime.
VisibilityFromDLLStorageClassKinds
@ Keep
Keep the IR-gen assigned visibility.
@ Protected
Override the IR-gen assigned visibility with protected visibility.
@ Default
Override the IR-gen assigned visibility with default visibility.
@ Hidden
Override the IR-gen assigned visibility with hidden visibility.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool isSignReturnAddressWithAKey() const
Check if return address signing uses AKey.
clang::ObjCRuntime ObjCRuntime
CoreFoundationABI CFRuntime
SanitizerSet Sanitize
Set of enabled sanitizers.
bool hasSignReturnAddress() const
Check if return address signing is enabled.
std::map< std::string, std::string, std::greater< std::string > > MacroPrefixMap
A prefix map for FILE, BASE_FILE and __builtin_FILE().
LangStandard::Kind LangStd
The used language standard.
bool isSignReturnAddressScopeAll() const
Check if leaf functions are also signed.
std::string CUID
The user provided compilation unit ID, if non-empty.
unsigned getOpenCLCompatibleVersion() const
Return the OpenCL version that kernel language is compatible with.
Visibility getVisibility() const
void setLinkage(Linkage L)
Linkage getLinkage() const
bool isVisibilityExplicit() const
Represents a linkage specification.
LinkageSpecLanguageIDs getLanguage() const
Return the language specified by this linkage specification.
Parts getParts() const
Get the decomposed parts of this declaration.
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
void mangleBlock(const DeclContext *DC, const BlockDecl *BD, raw_ostream &Out)
void mangleCtorBlock(const CXXConstructorDecl *CD, CXXCtorType CT, const BlockDecl *BD, raw_ostream &Out)
void mangleGlobalBlock(const BlockDecl *BD, const NamedDecl *ID, raw_ostream &Out)
bool shouldMangleDeclName(const NamedDecl *D)
void mangleName(GlobalDecl GD, raw_ostream &)
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing.
virtual void mangleStringLiteral(const StringLiteral *SL, raw_ostream &)=0
ManglerKind getKind() const
virtual void needsUniqueInternalLinkageNames()
virtual void mangleReferenceTemporary(const VarDecl *D, unsigned ManglingNumber, raw_ostream &)=0
void mangleDtorBlock(const CXXDestructorDecl *CD, CXXDtorType DT, const BlockDecl *BD, raw_ostream &Out)
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
APValue * getOrCreateValue(bool MayCreate) const
Get the storage for the constant value of a materialized temporary of static storage duration.
unsigned getManglingNumber() const
Describes a module or submodule.
bool isInterfaceOrPartition() const
bool isNamedModuleUnit() const
Is this a C++20 named module unit.
Module * Parent
The parent of this module.
Module * getPrivateModuleFragment() const
Get the Private Module Fragment (sub-module) for this module, it there is one.
llvm::SmallSetVector< Module *, 2 > Imports
The set of modules imported by this module, and on which this module depends.
Module * getGlobalModuleFragment() const
Get the Global Module Fragment (sub-module) for this module, it there is one.
bool isModuleMapModule() const
llvm::SmallVector< LinkLibrary, 2 > LinkLibraries
The set of libraries or frameworks to link against when an entity from this module is used.
llvm::iterator_range< submodule_iterator > submodules()
bool isHeaderLikeModule() const
Is this module have similar semantics as headers.
bool UseExportAsModuleLinkName
Autolinking uses the framework name for linking purposes when this is false and the export_as name ot...
This represents a decl that may have a name.
LinkageInfo getLinkageAndVisibility() const
Determines the linkage and visibility of this entity.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Linkage getFormalLinkage() const
Get the linkage from a semantic point of view.
bool isExternallyVisible() const
Represent a C++ namespace.
This represents '#pragma omp threadprivate ...' directive.
ObjCEncodeExpr, used for @encode in Objective-C.
QualType getEncodedType() const
propimpl_range property_impls() const
const ObjCInterfaceDecl * getClassInterface() const
void addInstanceMethod(ObjCMethodDecl *method)
ObjCImplementationDecl - Represents a class definition - this is where method definitions are specifi...
init_iterator init_end()
init_end() - Retrieve an iterator past the last initializer.
init_iterator init_begin()
init_begin() - Retrieve an iterator to the first initializer.
unsigned getNumIvarInitializers() const
getNumArgs - Number of ivars which must be initialized.
void setHasDestructors(bool val)
void setHasNonZeroConstructors(bool val)
Represents an ObjC class declaration.
ObjCIvarDecl * all_declared_ivar_begin()
all_declared_ivar_begin - return first ivar declared in this class, its extensions and its implementa...
ObjCIvarDecl - Represents an ObjC instance variable.
ObjCIvarDecl * getNextIvar()
ObjCMethodDecl - Represents an instance or class method declaration.
static ObjCMethodDecl * Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl, bool isInstance=true, bool isVariadic=false, bool isPropertyAccessor=false, bool isSynthesizedAccessorStub=false, bool isImplicitlyDeclared=false, bool isDefined=false, ObjCImplementationControl impControl=ObjCImplementationControl::None, bool HasRelatedResultType=false)
Represents one property declaration in an Objective-C interface.
ObjCMethodDecl * getGetterMethodDecl() const
bool isReadOnly() const
isReadOnly - Return true iff the property has a setter.
The basic abstraction for the target Objective-C runtime.
bool hasUnwindExceptions() const
Does this runtime use zero-cost exceptions?
bool isGNUFamily() const
Is this runtime basically of the GNU family of runtimes?
@ MacOSX
'macosx' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
@ FragileMacOSX
'macosx-fragile' is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
@ GNUstep
'gnustep' is the modern non-fragile GNUstep runtime.
@ ObjFW
'objfw' is the Objective-C runtime included in ObjFW
@ iOS
'ios' is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
@ GCC
'gcc' is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI
@ WatchOS
'watchos' is a variant of iOS for Apple's watchOS.
Represents a parameter to a function.
ParsedAttr - Represents a syntactic attribute.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::optional< uint64_t > SourceDateEpoch
If set, the UNIX timestamp specified by SOURCE_DATE_EPOCH.
std::vector< std::pair< std::string, bool > > Macros
Represents an unpacked "presumed" location which can be presented to the user.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
std::optional< ExclusionType > isFunctionExcluded(StringRef FunctionName, CodeGenOptions::ProfileInstrKind Kind) const
std::optional< ExclusionType > isLocationExcluded(SourceLocation Loc, CodeGenOptions::ProfileInstrKind Kind) const
ExclusionType
Represents if an how something should be excluded from profiling.
@ Skip
Profiling is skipped using the skipprofile attribute.
@ Allow
Profiling is allowed.
ExclusionType getDefault(CodeGenOptions::ProfileInstrKind Kind) const
std::optional< ExclusionType > isFileExcluded(StringRef FileName, CodeGenOptions::ProfileInstrKind Kind) const
A (possibly-)qualified type.
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
bool isRestrictQualified() const
Determine whether this type is restrict-qualified.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
LangAS getAddressSpace() const
Return the address space of this type.
bool isConstant(const ASTContext &Ctx) const
QualType getCanonicalType() const
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
QualType withCVRQualifiers(unsigned CVR) const
bool isConstQualified() const
Determine whether this type is const-qualified.
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
unsigned getCVRQualifiers() const
Retrieve the set of CVR (const-volatile-restrict) qualifiers applied to this type.
static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)
Represents a struct/union/class.
field_range fields() const
virtual void completeDefinition()
Note that the definition of this type is now complete.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
decl_type * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
decl_type * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
std::string ComputeName(ASTContext &Context) const
Scope - A scope is a transient data structure that is used while parsing the program.
Selector getSelector(unsigned NumArgs, const IdentifierInfo **IIV)
Can create any sort of selector.
Smart pointer class that efficiently represents Objective-C method names.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
Stmt - This represents one statement.
StringLiteral - This represents a string literal expression, e.g.
unsigned getLength() const
uint32_t getCodeUnit(size_t i) const
StringRef getString() const
unsigned getCharByteWidth() const
Represents the declaration of a struct/union/class/enum.
void startDefinition()
Starts the definition of this tag declaration.
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Exposes information about the current target.
bool isReadOnlyFeature(StringRef Feature) const
Determine whether the given target feature is read only.
virtual unsigned multiVersionSortPriority(StringRef Name) const
unsigned getIntWidth() const
getIntWidth/Align - Return the size of 'signed int' and 'unsigned int' for this target,...
virtual unsigned multiVersionFeatureCost() const
bool supportsIFunc() const
Identify whether this target supports IFuncs.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
unsigned getLongWidth() const
getLongWidth/Align - Return the size of 'signed long' and 'unsigned long' for this target,...
virtual bool hasFeature(StringRef Feature) const
Determine whether the given target has the given feature.
TargetOptions & getTargetOpts() const
Retrieve the target options.
std::vector< std::string > Features
The list of target specific features to enable or disable – this should be a list of strings starting...
std::string TuneCPU
If given, the name of the target CPU to tune code for.
std::string CPU
If given, the name of the target CPU to generate code for.
@ Hostcall
printf lowering scheme involving hostcalls, currently used by HIP programs by default
A template parameter object.
const APValue & getValue() const
A declaration that models statements at global scope.
The top declaration context.
static DeclContext * castToDeclContext(const TranslationUnitDecl *D)
const Type * getTypeForDecl() const
A container of type source information.
QualType getType() const
Return the type wrapped by this type source info.
The base class of the type hierarchy.
bool isStructureType() const
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isPointerType() const
const T * castAs() const
Member-template castAs<specific type>.
bool isReferenceType() const
bool isCUDADeviceBuiltinSurfaceType() const
Check if the type is the CUDA device builtin surface type.
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
bool isCUDADeviceBuiltinTextureType() const
Check if the type is the CUDA device builtin texture type.
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
bool isObjCObjectPointerType() const
Linkage getLinkage() const
Determine the linkage of this type.
const T * getAs() const
Member-template getAs<specific type>'.
bool isRecordType() const
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
const APValue & getValue() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
TLSKind getTLSKind() const
DefinitionKind isThisDeclarationADefinition(ASTContext &) const
Check whether this declaration is a definition.
VarDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
CharUnits getFlexibleArrayInitChars(const ASTContext &Ctx) const
If hasFlexibleArrayInit is true, compute the number of additional bytes necessary to store those elem...
VarDecl * getDefinition(ASTContext &)
Get the real (not just tentative) definition for this declaration.
LanguageLinkage getLanguageLinkage() const
Compute the language linkage.
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
const Expr * getInit() const
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
@ TLS_Dynamic
TLS with a dynamic initializer.
@ DeclarationOnly
This declaration is only a declaration.
@ Definition
This declaration is definitely a definition.
DefinitionKind hasDefinition(ASTContext &) const
Check whether this variable is defined in this translation unit.
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Defines the clang::TargetInfo interface.
std::unique_ptr< TargetCodeGenInfo > createARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind Kind)
std::unique_ptr< TargetCodeGenInfo > createM68kTargetCodeGenInfo(CodeGenModule &CGM)
@ AttributedType
The l-value was considered opaque, so the alignment was determined from a type, but that type was an ...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
std::unique_ptr< TargetCodeGenInfo > createBPFTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createMSP430TargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel)
std::unique_ptr< TargetCodeGenInfo > createWebAssemblyTargetCodeGenInfo(CodeGenModule &CGM, WebAssemblyABIKind K)
std::unique_ptr< TargetCodeGenInfo > createPPC64_SVR4_TargetCodeGenInfo(CodeGenModule &CGM, PPC64_SVR4_ABIKind Kind, bool SoftFloatABI)
std::unique_ptr< TargetCodeGenInfo > createMIPSTargetCodeGenInfo(CodeGenModule &CGM, bool IsOS32)
std::unique_ptr< TargetCodeGenInfo > createHexagonTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createNVPTXTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createSystemZTargetCodeGenInfo(CodeGenModule &CGM, bool HasVector, bool SoftFloatABI)
std::unique_ptr< TargetCodeGenInfo > createWinX86_32TargetCodeGenInfo(CodeGenModule &CGM, bool DarwinVectorABI, bool Win32StructABI, unsigned NumRegisterParameters)
std::unique_ptr< TargetCodeGenInfo > createAIXTargetCodeGenInfo(CodeGenModule &CGM, bool Is64Bit)
std::unique_ptr< TargetCodeGenInfo > createAMDGPUTargetCodeGenInfo(CodeGenModule &CGM)
X86AVXABILevel
The AVX ABI level for X86 targets.
std::unique_ptr< TargetCodeGenInfo > createTCETargetCodeGenInfo(CodeGenModule &CGM)
CGObjCRuntime * CreateGNUObjCRuntime(CodeGenModule &CGM)
Creates an instance of an Objective-C runtime class.
std::unique_ptr< TargetCodeGenInfo > createWindowsARMTargetCodeGenInfo(CodeGenModule &CGM, ARMABIKind K)
std::unique_ptr< TargetCodeGenInfo > createAVRTargetCodeGenInfo(CodeGenModule &CGM, unsigned NPR, unsigned NRR)
std::unique_ptr< TargetCodeGenInfo > createARCTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createDefaultTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind Kind)
CGObjCRuntime * CreateMacObjCRuntime(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createSparcV8TargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createVETargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createRISCVTargetCodeGenInfo(CodeGenModule &CGM, unsigned XLen, unsigned FLen, bool EABI)
std::unique_ptr< TargetCodeGenInfo > createWindowsAArch64TargetCodeGenInfo(CodeGenModule &CGM, AArch64ABIKind K)
std::unique_ptr< TargetCodeGenInfo > createSparcV9TargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createPNaClTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createX86_32TargetCodeGenInfo(CodeGenModule &CGM, bool DarwinVectorABI, bool Win32StructABI, unsigned NumRegisterParameters, bool SoftFloatABI)
std::unique_ptr< TargetCodeGenInfo > createLanaiTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createPPC32TargetCodeGenInfo(CodeGenModule &CGM, bool SoftFloatABI)
CGCUDARuntime * CreateNVCUDARuntime(CodeGenModule &CGM)
Creates an instance of a CUDA runtime class.
std::unique_ptr< TargetCodeGenInfo > createLoongArchTargetCodeGenInfo(CodeGenModule &CGM, unsigned GRLen, unsigned FLen)
std::unique_ptr< TargetCodeGenInfo > createPPC64TargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createWinX86_64TargetCodeGenInfo(CodeGenModule &CGM, X86AVXABILevel AVXLevel)
CGCXXABI * CreateMicrosoftCXXABI(CodeGenModule &CGM)
Creates a Microsoft-family ABI.
std::unique_ptr< TargetCodeGenInfo > createXCoreTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createCSKYTargetCodeGenInfo(CodeGenModule &CGM, unsigned FLen)
CGCXXABI * CreateItaniumCXXABI(CodeGenModule &CGM)
Creates an Itanium-family ABI.
constexpr XRayInstrMask All
uint32_t Literal
Literals are represented as positive integers.
unsigned llvm::PointerUnion< const Decl *, const Expr * > DeclTy
bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
bool Init(InterpState &S, CodePtr OpPC)
bool Cast(InterpState &S, CodePtr OpPC)
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
The JSON file list parser is used to communicate input to InstallAPI.
CXXCtorType
C++ constructor types.
@ Ctor_Base
Base object ctor.
@ Ctor_Complete
Complete object ctor.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, DiagnosticsEngine &Diags)
GVALinkage
A more specific kind of linkage than enum Linkage.
@ GVA_AvailableExternally
std::string getClangVendor()
Retrieves the Clang vendor tag.
@ ICIS_NoInit
No in-class initializer.
StorageClass
Storage classes.
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
@ Internal
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
@ Asm
Assembly: we accept this only so that we can preprocess it.
@ SD_Thread
Thread storage duration.
@ SD_Static
Static storage duration.
bool isLambdaCallOperator(const CXXMethodDecl *MD)
StringRef languageToString(Language L)
@ Dtor_Base
Base object dtor.
@ Dtor_Complete
Complete object dtor.
LangAS
Defines the address space values used by the address space qualifier of QualType.
@ FirstTargetAddressSpace
const FunctionProtoType * T
TemplateSpecializationKind
Describes the kind of template specialization that a particular template specialization declaration r...
@ TSK_ExplicitInstantiationDefinition
This template specialization was instantiated from a template due to an explicit instantiation defini...
@ TSK_ImplicitInstantiation
This template specialization was implicitly instantiated from a template.
CallingConv
CallingConv - Specifies the calling convention that a function uses.
bool isExternallyVisible(Linkage L)
@ EST_None
no exception specification
std::string getClangFullVersion()
Retrieves a string representing the complete clang version, which includes the clang version number,...
@ HiddenVisibility
Objects with "hidden" visibility are not seen by the dynamic linker.
@ ProtectedVisibility
Objects with "protected" visibility are seen by the dynamic linker but always dynamically resolve to ...
@ DefaultVisibility
Objects with "default" visibility are seen by the dynamic linker and act like normal objects.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
llvm::SmallVector< StringRef, 8 > Features
struct clang::CodeGen::CodeGenFunction::MultiVersionResolverOption::Conds Conditions
llvm::CallingConv::ID RuntimeCC
llvm::PointerType * VoidPtrTy
llvm::IntegerType * Int64Ty
llvm::PointerType * ConstGlobalsPtrTy
void* in the address space for constant globals
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
llvm::IntegerType * CharTy
char
LangAS ASTAllocaAddressSpace
unsigned char PointerWidthInBits
The width of a pointer into the generic address space.
unsigned char IntAlignInBytes
llvm::Type * HalfTy
half, bfloat, float, double
llvm::PointerType * RuntimeGlobalsInt8PtrTy
void* used for virtual table globals.
unsigned char SizeSizeInBytes
llvm::CallingConv::ID getRuntimeCC() const
llvm::IntegerType * SizeTy
llvm::PointerType * GlobalsInt8PtrTy
llvm::IntegerType * Int32Ty
llvm::IntegerType * IntPtrTy
llvm::IntegerType * IntTy
int
llvm::IntegerType * Int16Ty
unsigned char PointerAlignInBytes
llvm::PointerType * Int8PtrTy
llvm::PointerType * DefaultInt8PtrTy
void* in target address space
CharUnits getPointerAlign() const
llvm::PointerType * AllocaInt8PtrTy
DeclarationName getName() const
getName - Returns the embedded declaration name.
EvalResult is a struct with detailed info about an evaluated expression.
APValue Val
Val - This is the value the expression can be folded to.
bool hasSideEffects() const
static const LangStandard & getLangStandardForKind(Kind K)
Parts of a decomposed MSGuidDecl.
uint16_t Part2
...-89ab-...
uint32_t Part1
{01234567-...
uint16_t Part3
...-cdef-...
uint8_t Part4And5[8]
...-0123-456789abcdef}
Contains information gathered from parsing the contents of TargetAttr.
Describes how types, statements, expressions, and declarations should be printed.
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.