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