1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This coordinates the per-module state used while generating code.
11 //===----------------------------------------------------------------------===//
13 #include "CodeGenModule.h"
16 #include "CGCUDARuntime.h"
19 #include "CGDebugInfo.h"
20 #include "CGHLSLRuntime.h"
21 #include "CGObjCRuntime.h"
22 #include "CGOpenCLRuntime.h"
23 #include "CGOpenMPRuntime.h"
24 #include "CGOpenMPRuntimeGPU.h"
25 #include "CodeGenFunction.h"
26 #include "CodeGenPGO.h"
27 #include "ConstantEmitter.h"
28 #include "CoverageMappingGen.h"
29 #include "TargetInfo.h"
30 #include "clang/AST/ASTContext.h"
31 #include "clang/AST/ASTLambda.h"
32 #include "clang/AST/CharUnits.h"
33 #include "clang/AST/Decl.h"
34 #include "clang/AST/DeclCXX.h"
35 #include "clang/AST/DeclObjC.h"
36 #include "clang/AST/DeclTemplate.h"
37 #include "clang/AST/Mangle.h"
38 #include "clang/AST/RecursiveASTVisitor.h"
39 #include "clang/AST/StmtVisitor.h"
40 #include "clang/Basic/Builtins.h"
41 #include "clang/Basic/CharInfo.h"
42 #include "clang/Basic/CodeGenOptions.h"
43 #include "clang/Basic/Diagnostic.h"
44 #include "clang/Basic/FileManager.h"
45 #include "clang/Basic/Module.h"
46 #include "clang/Basic/SourceManager.h"
47 #include "clang/Basic/TargetInfo.h"
48 #include "clang/Basic/Version.h"
49 #include "clang/CodeGen/BackendUtil.h"
50 #include "clang/CodeGen/ConstantInitBuilder.h"
51 #include "clang/Frontend/FrontendDiagnostic.h"
52 #include "llvm/ADT/STLExtras.h"
53 #include "llvm/ADT/StringExtras.h"
54 #include "llvm/ADT/StringSwitch.h"
55 #include "llvm/Analysis/TargetLibraryInfo.h"
56 #include "llvm/BinaryFormat/ELF.h"
57 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
58 #include "llvm/IR/AttributeMask.h"
59 #include "llvm/IR/CallingConv.h"
60 #include "llvm/IR/DataLayout.h"
61 #include "llvm/IR/Intrinsics.h"
62 #include "llvm/IR/LLVMContext.h"
63 #include "llvm/IR/Module.h"
64 #include "llvm/IR/ProfileSummary.h"
65 #include "llvm/ProfileData/InstrProfReader.h"
66 #include "llvm/ProfileData/SampleProf.h"
67 #include "llvm/Support/CRC.h"
68 #include "llvm/Support/CodeGen.h"
69 #include "llvm/Support/CommandLine.h"
70 #include "llvm/Support/ConvertUTF.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/TimeProfiler.h"
73 #include "llvm/Support/xxhash.h"
74 #include "llvm/TargetParser/RISCVISAInfo.h"
75 #include "llvm/TargetParser/Triple.h"
76 #include "llvm/TargetParser/X86TargetParser.h"
77 #include "llvm/Transforms/Utils/BuildLibCalls.h"
80 using namespace clang
;
81 using namespace CodeGen
;
83 static llvm::cl::opt
<bool> LimitedCoverage(
84 "limited-coverage-experimental", llvm::cl::Hidden
,
85 llvm::cl::desc("Emit limited coverage mapping information (experimental)"));
87 static const char AnnotationSection
[] = "llvm.metadata";
89 static CGCXXABI
*createCXXABI(CodeGenModule
&CGM
) {
90 switch (CGM
.getContext().getCXXABIKind()) {
91 case TargetCXXABI::AppleARM64
:
92 case TargetCXXABI::Fuchsia
:
93 case TargetCXXABI::GenericAArch64
:
94 case TargetCXXABI::GenericARM
:
95 case TargetCXXABI::iOS
:
96 case TargetCXXABI::WatchOS
:
97 case TargetCXXABI::GenericMIPS
:
98 case TargetCXXABI::GenericItanium
:
99 case TargetCXXABI::WebAssembly
:
100 case TargetCXXABI::XL
:
101 return CreateItaniumCXXABI(CGM
);
102 case TargetCXXABI::Microsoft
:
103 return CreateMicrosoftCXXABI(CGM
);
106 llvm_unreachable("invalid C++ ABI kind");
109 static std::unique_ptr
<TargetCodeGenInfo
>
110 createTargetCodeGenInfo(CodeGenModule
&CGM
) {
111 const TargetInfo
&Target
= CGM
.getTarget();
112 const llvm::Triple
&Triple
= Target
.getTriple();
113 const CodeGenOptions
&CodeGenOpts
= CGM
.getCodeGenOpts();
115 switch (Triple
.getArch()) {
117 return createDefaultTargetCodeGenInfo(CGM
);
119 case llvm::Triple::le32
:
120 return createPNaClTargetCodeGenInfo(CGM
);
121 case llvm::Triple::m68k
:
122 return createM68kTargetCodeGenInfo(CGM
);
123 case llvm::Triple::mips
:
124 case llvm::Triple::mipsel
:
125 if (Triple
.getOS() == llvm::Triple::NaCl
)
126 return createPNaClTargetCodeGenInfo(CGM
);
127 return createMIPSTargetCodeGenInfo(CGM
, /*IsOS32=*/true);
129 case llvm::Triple::mips64
:
130 case llvm::Triple::mips64el
:
131 return createMIPSTargetCodeGenInfo(CGM
, /*IsOS32=*/false);
133 case llvm::Triple::avr
: {
134 // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
135 // on avrtiny. For passing return value, R18~R25 are used on avr, and
136 // R22~R25 are used on avrtiny.
137 unsigned NPR
= Target
.getABI() == "avrtiny" ? 6 : 18;
138 unsigned NRR
= Target
.getABI() == "avrtiny" ? 4 : 8;
139 return createAVRTargetCodeGenInfo(CGM
, NPR
, NRR
);
142 case llvm::Triple::aarch64
:
143 case llvm::Triple::aarch64_32
:
144 case llvm::Triple::aarch64_be
: {
145 AArch64ABIKind Kind
= AArch64ABIKind::AAPCS
;
146 if (Target
.getABI() == "darwinpcs")
147 Kind
= AArch64ABIKind::DarwinPCS
;
148 else if (Triple
.isOSWindows())
149 return createWindowsAArch64TargetCodeGenInfo(CGM
, AArch64ABIKind::Win64
);
150 else if (Target
.getABI() == "aapcs-soft")
151 Kind
= AArch64ABIKind::AAPCSSoft
;
152 else if (Target
.getABI() == "pauthtest")
153 Kind
= AArch64ABIKind::PAuthTest
;
155 return createAArch64TargetCodeGenInfo(CGM
, Kind
);
158 case llvm::Triple::wasm32
:
159 case llvm::Triple::wasm64
: {
160 WebAssemblyABIKind Kind
= WebAssemblyABIKind::MVP
;
161 if (Target
.getABI() == "experimental-mv")
162 Kind
= WebAssemblyABIKind::ExperimentalMV
;
163 return createWebAssemblyTargetCodeGenInfo(CGM
, Kind
);
166 case llvm::Triple::arm
:
167 case llvm::Triple::armeb
:
168 case llvm::Triple::thumb
:
169 case llvm::Triple::thumbeb
: {
170 if (Triple
.getOS() == llvm::Triple::Win32
)
171 return createWindowsARMTargetCodeGenInfo(CGM
, ARMABIKind::AAPCS_VFP
);
173 ARMABIKind Kind
= ARMABIKind::AAPCS
;
174 StringRef ABIStr
= Target
.getABI();
175 if (ABIStr
== "apcs-gnu")
176 Kind
= ARMABIKind::APCS
;
177 else if (ABIStr
== "aapcs16")
178 Kind
= ARMABIKind::AAPCS16_VFP
;
179 else if (CodeGenOpts
.FloatABI
== "hard" ||
180 (CodeGenOpts
.FloatABI
!= "soft" &&
181 (Triple
.getEnvironment() == llvm::Triple::GNUEABIHF
||
182 Triple
.getEnvironment() == llvm::Triple::MuslEABIHF
||
183 Triple
.getEnvironment() == llvm::Triple::EABIHF
)))
184 Kind
= ARMABIKind::AAPCS_VFP
;
186 return createARMTargetCodeGenInfo(CGM
, Kind
);
189 case llvm::Triple::ppc
: {
190 if (Triple
.isOSAIX())
191 return createAIXTargetCodeGenInfo(CGM
, /*Is64Bit=*/false);
194 CodeGenOpts
.FloatABI
== "soft" || Target
.hasFeature("spe");
195 return createPPC32TargetCodeGenInfo(CGM
, IsSoftFloat
);
197 case llvm::Triple::ppcle
: {
198 bool IsSoftFloat
= CodeGenOpts
.FloatABI
== "soft";
199 return createPPC32TargetCodeGenInfo(CGM
, IsSoftFloat
);
201 case llvm::Triple::ppc64
:
202 if (Triple
.isOSAIX())
203 return createAIXTargetCodeGenInfo(CGM
, /*Is64Bit=*/true);
205 if (Triple
.isOSBinFormatELF()) {
206 PPC64_SVR4_ABIKind Kind
= PPC64_SVR4_ABIKind::ELFv1
;
207 if (Target
.getABI() == "elfv2")
208 Kind
= PPC64_SVR4_ABIKind::ELFv2
;
209 bool IsSoftFloat
= CodeGenOpts
.FloatABI
== "soft";
211 return createPPC64_SVR4_TargetCodeGenInfo(CGM
, Kind
, IsSoftFloat
);
213 return createPPC64TargetCodeGenInfo(CGM
);
214 case llvm::Triple::ppc64le
: {
215 assert(Triple
.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
216 PPC64_SVR4_ABIKind Kind
= PPC64_SVR4_ABIKind::ELFv2
;
217 if (Target
.getABI() == "elfv1")
218 Kind
= PPC64_SVR4_ABIKind::ELFv1
;
219 bool IsSoftFloat
= CodeGenOpts
.FloatABI
== "soft";
221 return createPPC64_SVR4_TargetCodeGenInfo(CGM
, Kind
, IsSoftFloat
);
224 case llvm::Triple::nvptx
:
225 case llvm::Triple::nvptx64
:
226 return createNVPTXTargetCodeGenInfo(CGM
);
228 case llvm::Triple::msp430
:
229 return createMSP430TargetCodeGenInfo(CGM
);
231 case llvm::Triple::riscv32
:
232 case llvm::Triple::riscv64
: {
233 StringRef ABIStr
= Target
.getABI();
234 unsigned XLen
= Target
.getPointerWidth(LangAS::Default
);
235 unsigned ABIFLen
= 0;
236 if (ABIStr
.ends_with("f"))
238 else if (ABIStr
.ends_with("d"))
240 bool EABI
= ABIStr
.ends_with("e");
241 return createRISCVTargetCodeGenInfo(CGM
, XLen
, ABIFLen
, EABI
);
244 case llvm::Triple::systemz
: {
245 bool SoftFloat
= CodeGenOpts
.FloatABI
== "soft";
246 bool HasVector
= !SoftFloat
&& Target
.getABI() == "vector";
247 return createSystemZTargetCodeGenInfo(CGM
, HasVector
, SoftFloat
);
250 case llvm::Triple::tce
:
251 case llvm::Triple::tcele
:
252 return createTCETargetCodeGenInfo(CGM
);
254 case llvm::Triple::x86
: {
255 bool IsDarwinVectorABI
= Triple
.isOSDarwin();
256 bool IsWin32FloatStructABI
= Triple
.isOSWindows() && !Triple
.isOSCygMing();
258 if (Triple
.getOS() == llvm::Triple::Win32
) {
259 return createWinX86_32TargetCodeGenInfo(
260 CGM
, IsDarwinVectorABI
, IsWin32FloatStructABI
,
261 CodeGenOpts
.NumRegisterParameters
);
263 return createX86_32TargetCodeGenInfo(
264 CGM
, IsDarwinVectorABI
, IsWin32FloatStructABI
,
265 CodeGenOpts
.NumRegisterParameters
, CodeGenOpts
.FloatABI
== "soft");
268 case llvm::Triple::x86_64
: {
269 StringRef ABI
= Target
.getABI();
270 X86AVXABILevel AVXLevel
= (ABI
== "avx512" ? X86AVXABILevel::AVX512
271 : ABI
== "avx" ? X86AVXABILevel::AVX
272 : X86AVXABILevel::None
);
274 switch (Triple
.getOS()) {
275 case llvm::Triple::Win32
:
276 return createWinX86_64TargetCodeGenInfo(CGM
, AVXLevel
);
278 return createX86_64TargetCodeGenInfo(CGM
, AVXLevel
);
281 case llvm::Triple::hexagon
:
282 return createHexagonTargetCodeGenInfo(CGM
);
283 case llvm::Triple::lanai
:
284 return createLanaiTargetCodeGenInfo(CGM
);
285 case llvm::Triple::r600
:
286 return createAMDGPUTargetCodeGenInfo(CGM
);
287 case llvm::Triple::amdgcn
:
288 return createAMDGPUTargetCodeGenInfo(CGM
);
289 case llvm::Triple::sparc
:
290 return createSparcV8TargetCodeGenInfo(CGM
);
291 case llvm::Triple::sparcv9
:
292 return createSparcV9TargetCodeGenInfo(CGM
);
293 case llvm::Triple::xcore
:
294 return createXCoreTargetCodeGenInfo(CGM
);
295 case llvm::Triple::arc
:
296 return createARCTargetCodeGenInfo(CGM
);
297 case llvm::Triple::spir
:
298 case llvm::Triple::spir64
:
299 return createCommonSPIRTargetCodeGenInfo(CGM
);
300 case llvm::Triple::spirv32
:
301 case llvm::Triple::spirv64
:
302 return createSPIRVTargetCodeGenInfo(CGM
);
303 case llvm::Triple::ve
:
304 return createVETargetCodeGenInfo(CGM
);
305 case llvm::Triple::csky
: {
306 bool IsSoftFloat
= !Target
.hasFeature("hard-float-abi");
308 Target
.hasFeature("fpuv2_df") || Target
.hasFeature("fpuv3_df");
309 return createCSKYTargetCodeGenInfo(CGM
, IsSoftFloat
? 0
313 case llvm::Triple::bpfeb
:
314 case llvm::Triple::bpfel
:
315 return createBPFTargetCodeGenInfo(CGM
);
316 case llvm::Triple::loongarch32
:
317 case llvm::Triple::loongarch64
: {
318 StringRef ABIStr
= Target
.getABI();
319 unsigned ABIFRLen
= 0;
320 if (ABIStr
.ends_with("f"))
322 else if (ABIStr
.ends_with("d"))
324 return createLoongArchTargetCodeGenInfo(
325 CGM
, Target
.getPointerWidth(LangAS::Default
), ABIFRLen
);
330 const TargetCodeGenInfo
&CodeGenModule::getTargetCodeGenInfo() {
331 if (!TheTargetCodeGenInfo
)
332 TheTargetCodeGenInfo
= createTargetCodeGenInfo(*this);
333 return *TheTargetCodeGenInfo
;
336 CodeGenModule::CodeGenModule(ASTContext
&C
,
337 IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> FS
,
338 const HeaderSearchOptions
&HSO
,
339 const PreprocessorOptions
&PPO
,
340 const CodeGenOptions
&CGO
, llvm::Module
&M
,
341 DiagnosticsEngine
&diags
,
342 CoverageSourceInfo
*CoverageInfo
)
343 : Context(C
), LangOpts(C
.getLangOpts()), FS(FS
), HeaderSearchOpts(HSO
),
344 PreprocessorOpts(PPO
), CodeGenOpts(CGO
), TheModule(M
), Diags(diags
),
345 Target(C
.getTargetInfo()), ABI(createCXXABI(*this)),
346 VMContext(M
.getContext()), Types(*this), VTables(*this),
347 SanitizerMD(new SanitizerMetadata(*this)) {
349 // Initialize the type cache.
350 llvm::LLVMContext
&LLVMContext
= M
.getContext();
351 VoidTy
= llvm::Type::getVoidTy(LLVMContext
);
352 Int8Ty
= llvm::Type::getInt8Ty(LLVMContext
);
353 Int16Ty
= llvm::Type::getInt16Ty(LLVMContext
);
354 Int32Ty
= llvm::Type::getInt32Ty(LLVMContext
);
355 Int64Ty
= llvm::Type::getInt64Ty(LLVMContext
);
356 HalfTy
= llvm::Type::getHalfTy(LLVMContext
);
357 BFloatTy
= llvm::Type::getBFloatTy(LLVMContext
);
358 FloatTy
= llvm::Type::getFloatTy(LLVMContext
);
359 DoubleTy
= llvm::Type::getDoubleTy(LLVMContext
);
360 PointerWidthInBits
= C
.getTargetInfo().getPointerWidth(LangAS::Default
);
361 PointerAlignInBytes
=
362 C
.toCharUnitsFromBits(C
.getTargetInfo().getPointerAlign(LangAS::Default
))
365 C
.toCharUnitsFromBits(C
.getTargetInfo().getMaxPointerWidth()).getQuantity();
367 C
.toCharUnitsFromBits(C
.getTargetInfo().getIntAlign()).getQuantity();
369 llvm::IntegerType::get(LLVMContext
, C
.getTargetInfo().getCharWidth());
370 IntTy
= llvm::IntegerType::get(LLVMContext
, C
.getTargetInfo().getIntWidth());
371 IntPtrTy
= llvm::IntegerType::get(LLVMContext
,
372 C
.getTargetInfo().getMaxPointerWidth());
373 Int8PtrTy
= llvm::PointerType::get(LLVMContext
,
374 C
.getTargetAddressSpace(LangAS::Default
));
375 const llvm::DataLayout
&DL
= M
.getDataLayout();
377 llvm::PointerType::get(LLVMContext
, DL
.getAllocaAddrSpace());
379 llvm::PointerType::get(LLVMContext
, DL
.getDefaultGlobalsAddressSpace());
380 ConstGlobalsPtrTy
= llvm::PointerType::get(
381 LLVMContext
, C
.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
382 ASTAllocaAddressSpace
= getTargetCodeGenInfo().getASTAllocaAddressSpace();
384 // Build C++20 Module initializers.
385 // TODO: Add Microsoft here once we know the mangling required for the
388 LangOpts
.CPlusPlusModules
&& getCXXABI().getMangleContext().getKind() ==
389 ItaniumMangleContext::MK_Itanium
;
391 RuntimeCC
= getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
396 createOpenCLRuntime();
398 createOpenMPRuntime();
404 // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
405 if (LangOpts
.Sanitize
.has(SanitizerKind::Thread
) ||
406 (!CodeGenOpts
.RelaxedAliasing
&& CodeGenOpts
.OptimizationLevel
> 0))
407 TBAA
.reset(new CodeGenTBAA(Context
, getTypes(), TheModule
, CodeGenOpts
,
408 getLangOpts(), getCXXABI().getMangleContext()));
410 // If debug info or coverage generation is enabled, create the CGDebugInfo
412 if (CodeGenOpts
.getDebugInfo() != llvm::codegenoptions::NoDebugInfo
||
413 CodeGenOpts
.CoverageNotesFile
.size() ||
414 CodeGenOpts
.CoverageDataFile
.size())
415 DebugInfo
.reset(new CGDebugInfo(*this));
417 Block
.GlobalUniqueCount
= 0;
419 if (C
.getLangOpts().ObjC
)
420 ObjCData
.reset(new ObjCEntrypoints());
422 if (CodeGenOpts
.hasProfileClangUse()) {
423 auto ReaderOrErr
= llvm::IndexedInstrProfReader::create(
424 CodeGenOpts
.ProfileInstrumentUsePath
, *FS
,
425 CodeGenOpts
.ProfileRemappingFile
);
426 // We're checking for profile read errors in CompilerInvocation, so if
427 // there was an error it should've already been caught. If it hasn't been
428 // somehow, trip an assertion.
430 PGOReader
= std::move(ReaderOrErr
.get());
433 // If coverage mapping generation is enabled, create the
434 // CoverageMappingModuleGen object.
435 if (CodeGenOpts
.CoverageMapping
)
436 CoverageMapping
.reset(new CoverageMappingModuleGen(*this, *CoverageInfo
));
438 // Generate the module name hash here if needed.
439 if (CodeGenOpts
.UniqueInternalLinkageNames
&&
440 !getModule().getSourceFileName().empty()) {
441 std::string Path
= getModule().getSourceFileName();
442 // Check if a path substitution is needed from the MacroPrefixMap.
443 for (const auto &Entry
: LangOpts
.MacroPrefixMap
)
444 if (Path
.rfind(Entry
.first
, 0) != std::string::npos
) {
445 Path
= Entry
.second
+ Path
.substr(Entry
.first
.size());
448 ModuleNameHash
= llvm::getUniqueInternalLinkagePostfix(Path
);
451 // Record mregparm value now so it is visible through all of codegen.
452 if (Context
.getTargetInfo().getTriple().getArch() == llvm::Triple::x86
)
453 getModule().addModuleFlag(llvm::Module::Error
, "NumRegisterParameters",
454 CodeGenOpts
.NumRegisterParameters
);
457 CodeGenModule::~CodeGenModule() {}
459 void CodeGenModule::createObjCRuntime() {
460 // This is just isGNUFamily(), but we want to force implementors of
461 // new ABIs to decide how best to do this.
462 switch (LangOpts
.ObjCRuntime
.getKind()) {
463 case ObjCRuntime::GNUstep
:
464 case ObjCRuntime::GCC
:
465 case ObjCRuntime::ObjFW
:
466 ObjCRuntime
.reset(CreateGNUObjCRuntime(*this));
469 case ObjCRuntime::FragileMacOSX
:
470 case ObjCRuntime::MacOSX
:
471 case ObjCRuntime::iOS
:
472 case ObjCRuntime::WatchOS
:
473 ObjCRuntime
.reset(CreateMacObjCRuntime(*this));
476 llvm_unreachable("bad runtime kind");
479 void CodeGenModule::createOpenCLRuntime() {
480 OpenCLRuntime
.reset(new CGOpenCLRuntime(*this));
483 void CodeGenModule::createOpenMPRuntime() {
484 // Select a specialized code generation class based on the target, if any.
485 // If it does not exist use the default implementation.
486 switch (getTriple().getArch()) {
487 case llvm::Triple::nvptx
:
488 case llvm::Triple::nvptx64
:
489 case llvm::Triple::amdgcn
:
490 assert(getLangOpts().OpenMPIsTargetDevice
&&
491 "OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
492 OpenMPRuntime
.reset(new CGOpenMPRuntimeGPU(*this));
495 if (LangOpts
.OpenMPSimd
)
496 OpenMPRuntime
.reset(new CGOpenMPSIMDRuntime(*this));
498 OpenMPRuntime
.reset(new CGOpenMPRuntime(*this));
503 void CodeGenModule::createCUDARuntime() {
504 CUDARuntime
.reset(CreateNVCUDARuntime(*this));
507 void CodeGenModule::createHLSLRuntime() {
508 HLSLRuntime
.reset(new CGHLSLRuntime(*this));
511 void CodeGenModule::addReplacement(StringRef Name
, llvm::Constant
*C
) {
512 Replacements
[Name
] = C
;
515 void CodeGenModule::applyReplacements() {
516 for (auto &I
: Replacements
) {
517 StringRef MangledName
= I
.first
;
518 llvm::Constant
*Replacement
= I
.second
;
519 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
522 auto *OldF
= cast
<llvm::Function
>(Entry
);
523 auto *NewF
= dyn_cast
<llvm::Function
>(Replacement
);
525 if (auto *Alias
= dyn_cast
<llvm::GlobalAlias
>(Replacement
)) {
526 NewF
= dyn_cast
<llvm::Function
>(Alias
->getAliasee());
528 auto *CE
= cast
<llvm::ConstantExpr
>(Replacement
);
529 assert(CE
->getOpcode() == llvm::Instruction::BitCast
||
530 CE
->getOpcode() == llvm::Instruction::GetElementPtr
);
531 NewF
= dyn_cast
<llvm::Function
>(CE
->getOperand(0));
535 // Replace old with new, but keep the old order.
536 OldF
->replaceAllUsesWith(Replacement
);
538 NewF
->removeFromParent();
539 OldF
->getParent()->getFunctionList().insertAfter(OldF
->getIterator(),
542 OldF
->eraseFromParent();
546 void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue
*GV
, llvm::Constant
*C
) {
547 GlobalValReplacements
.push_back(std::make_pair(GV
, C
));
550 void CodeGenModule::applyGlobalValReplacements() {
551 for (auto &I
: GlobalValReplacements
) {
552 llvm::GlobalValue
*GV
= I
.first
;
553 llvm::Constant
*C
= I
.second
;
555 GV
->replaceAllUsesWith(C
);
556 GV
->eraseFromParent();
560 // This is only used in aliases that we created and we know they have a
562 static const llvm::GlobalValue
*getAliasedGlobal(const llvm::GlobalValue
*GV
) {
563 const llvm::Constant
*C
;
564 if (auto *GA
= dyn_cast
<llvm::GlobalAlias
>(GV
))
565 C
= GA
->getAliasee();
566 else if (auto *GI
= dyn_cast
<llvm::GlobalIFunc
>(GV
))
567 C
= GI
->getResolver();
571 const auto *AliaseeGV
= dyn_cast
<llvm::GlobalValue
>(C
->stripPointerCasts());
575 const llvm::GlobalValue
*FinalGV
= AliaseeGV
->getAliaseeObject();
582 static bool checkAliasedGlobal(
583 const ASTContext
&Context
, DiagnosticsEngine
&Diags
, SourceLocation Location
,
584 bool IsIFunc
, const llvm::GlobalValue
*Alias
, const llvm::GlobalValue
*&GV
,
585 const llvm::MapVector
<GlobalDecl
, StringRef
> &MangledDeclNames
,
586 SourceRange AliasRange
) {
587 GV
= getAliasedGlobal(Alias
);
589 Diags
.Report(Location
, diag::err_cyclic_alias
) << IsIFunc
;
593 if (GV
->hasCommonLinkage()) {
594 const llvm::Triple
&Triple
= Context
.getTargetInfo().getTriple();
595 if (Triple
.getObjectFormat() == llvm::Triple::XCOFF
) {
596 Diags
.Report(Location
, diag::err_alias_to_common
);
601 if (GV
->isDeclaration()) {
602 Diags
.Report(Location
, diag::err_alias_to_undefined
) << IsIFunc
<< IsIFunc
;
603 Diags
.Report(Location
, diag::note_alias_requires_mangled_name
)
604 << IsIFunc
<< IsIFunc
;
605 // Provide a note if the given function is not found and exists as a
607 for (const auto &[Decl
, Name
] : MangledDeclNames
) {
608 if (const auto *ND
= dyn_cast
<NamedDecl
>(Decl
.getDecl())) {
609 if (ND
->getName() == GV
->getName()) {
610 Diags
.Report(Location
, diag::note_alias_mangled_name_alternative
)
612 << FixItHint::CreateReplacement(
614 (Twine(IsIFunc
? "ifunc" : "alias") + "(\"" + Name
+ "\")")
623 // Check resolver function type.
624 const auto *F
= dyn_cast
<llvm::Function
>(GV
);
626 Diags
.Report(Location
, diag::err_alias_to_undefined
)
627 << IsIFunc
<< IsIFunc
;
631 llvm::FunctionType
*FTy
= F
->getFunctionType();
632 if (!FTy
->getReturnType()->isPointerTy()) {
633 Diags
.Report(Location
, diag::err_ifunc_resolver_return
);
641 // Emit a warning if toc-data attribute is requested for global variables that
642 // have aliases and remove the toc-data attribute.
643 static void checkAliasForTocData(llvm::GlobalVariable
*GVar
,
644 const CodeGenOptions
&CodeGenOpts
,
645 DiagnosticsEngine
&Diags
,
646 SourceLocation Location
) {
647 if (GVar
->hasAttribute("toc-data")) {
648 auto GVId
= GVar
->getName();
649 // Is this a global variable specified by the user as local?
650 if ((llvm::binary_search(CodeGenOpts
.TocDataVarsUserSpecified
, GVId
))) {
651 Diags
.Report(Location
, diag::warn_toc_unsupported_type
)
652 << GVId
<< "the variable has an alias";
654 llvm::AttributeSet CurrAttributes
= GVar
->getAttributes();
655 llvm::AttributeSet NewAttributes
=
656 CurrAttributes
.removeAttribute(GVar
->getContext(), "toc-data");
657 GVar
->setAttributes(NewAttributes
);
661 void CodeGenModule::checkAliases() {
662 // Check if the constructed aliases are well formed. It is really unfortunate
663 // that we have to do this in CodeGen, but we only construct mangled names
664 // and aliases during codegen.
666 DiagnosticsEngine
&Diags
= getDiags();
667 for (const GlobalDecl
&GD
: Aliases
) {
668 const auto *D
= cast
<ValueDecl
>(GD
.getDecl());
669 SourceLocation Location
;
671 bool IsIFunc
= D
->hasAttr
<IFuncAttr
>();
672 if (const Attr
*A
= D
->getDefiningAttr()) {
673 Location
= A
->getLocation();
674 Range
= A
->getRange();
676 llvm_unreachable("Not an alias or ifunc?");
678 StringRef MangledName
= getMangledName(GD
);
679 llvm::GlobalValue
*Alias
= GetGlobalValue(MangledName
);
680 const llvm::GlobalValue
*GV
= nullptr;
681 if (!checkAliasedGlobal(getContext(), Diags
, Location
, IsIFunc
, Alias
, GV
,
682 MangledDeclNames
, Range
)) {
687 if (getContext().getTargetInfo().getTriple().isOSAIX())
688 if (const llvm::GlobalVariable
*GVar
=
689 dyn_cast
<const llvm::GlobalVariable
>(GV
))
690 checkAliasForTocData(const_cast<llvm::GlobalVariable
*>(GVar
),
691 getCodeGenOpts(), Diags
, Location
);
693 llvm::Constant
*Aliasee
=
694 IsIFunc
? cast
<llvm::GlobalIFunc
>(Alias
)->getResolver()
695 : cast
<llvm::GlobalAlias
>(Alias
)->getAliasee();
697 llvm::GlobalValue
*AliaseeGV
;
698 if (auto CE
= dyn_cast
<llvm::ConstantExpr
>(Aliasee
))
699 AliaseeGV
= cast
<llvm::GlobalValue
>(CE
->getOperand(0));
701 AliaseeGV
= cast
<llvm::GlobalValue
>(Aliasee
);
703 if (const SectionAttr
*SA
= D
->getAttr
<SectionAttr
>()) {
704 StringRef AliasSection
= SA
->getName();
705 if (AliasSection
!= AliaseeGV
->getSection())
706 Diags
.Report(SA
->getLocation(), diag::warn_alias_with_section
)
707 << AliasSection
<< IsIFunc
<< IsIFunc
;
710 // We have to handle alias to weak aliases in here. LLVM itself disallows
711 // this since the object semantics would not match the IL one. For
712 // compatibility with gcc we implement it by just pointing the alias
713 // to its aliasee's aliasee. We also warn, since the user is probably
714 // expecting the link to be weak.
715 if (auto *GA
= dyn_cast
<llvm::GlobalAlias
>(AliaseeGV
)) {
716 if (GA
->isInterposable()) {
717 Diags
.Report(Location
, diag::warn_alias_to_weak_alias
)
718 << GV
->getName() << GA
->getName() << IsIFunc
;
719 Aliasee
= llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
720 GA
->getAliasee(), Alias
->getType());
723 cast
<llvm::GlobalIFunc
>(Alias
)->setResolver(Aliasee
);
725 cast
<llvm::GlobalAlias
>(Alias
)->setAliasee(Aliasee
);
728 // ifunc resolvers are usually implemented to run before sanitizer
729 // initialization. Disable instrumentation to prevent the ordering issue.
731 cast
<llvm::Function
>(Aliasee
)->addFnAttr(
732 llvm::Attribute::DisableSanitizerInstrumentation
);
737 for (const GlobalDecl
&GD
: Aliases
) {
738 StringRef MangledName
= getMangledName(GD
);
739 llvm::GlobalValue
*Alias
= GetGlobalValue(MangledName
);
740 Alias
->replaceAllUsesWith(llvm::UndefValue::get(Alias
->getType()));
741 Alias
->eraseFromParent();
745 void CodeGenModule::clear() {
746 DeferredDeclsToEmit
.clear();
747 EmittedDeferredDecls
.clear();
748 DeferredAnnotations
.clear();
750 OpenMPRuntime
->clear();
753 void InstrProfStats::reportDiagnostics(DiagnosticsEngine
&Diags
,
754 StringRef MainFile
) {
755 if (!hasDiagnostics())
757 if (VisitedInMainFile
> 0 && VisitedInMainFile
== MissingInMainFile
) {
758 if (MainFile
.empty())
759 MainFile
= "<stdin>";
760 Diags
.Report(diag::warn_profile_data_unprofiled
) << MainFile
;
763 Diags
.Report(diag::warn_profile_data_out_of_date
) << Visited
<< Mismatched
;
766 Diags
.Report(diag::warn_profile_data_missing
) << Visited
<< Missing
;
770 static std::optional
<llvm::GlobalValue::VisibilityTypes
>
771 getLLVMVisibility(clang::LangOptions::VisibilityFromDLLStorageClassKinds K
) {
772 // Map to LLVM visibility.
774 case clang::LangOptions::VisibilityFromDLLStorageClassKinds::Keep
:
776 case clang::LangOptions::VisibilityFromDLLStorageClassKinds::Default
:
777 return llvm::GlobalValue::DefaultVisibility
;
778 case clang::LangOptions::VisibilityFromDLLStorageClassKinds::Hidden
:
779 return llvm::GlobalValue::HiddenVisibility
;
780 case clang::LangOptions::VisibilityFromDLLStorageClassKinds::Protected
:
781 return llvm::GlobalValue::ProtectedVisibility
;
783 llvm_unreachable("unknown option value!");
786 void setLLVMVisibility(llvm::GlobalValue
&GV
,
787 std::optional
<llvm::GlobalValue::VisibilityTypes
> V
) {
791 // Reset DSO locality before setting the visibility. This removes
792 // any effects that visibility options and annotations may have
793 // had on the DSO locality. Setting the visibility will implicitly set
794 // appropriate globals to DSO Local; however, this will be pessimistic
795 // w.r.t. to the normal compiler IRGen.
796 GV
.setDSOLocal(false);
797 GV
.setVisibility(*V
);
800 static void setVisibilityFromDLLStorageClass(const clang::LangOptions
&LO
,
802 if (!LO
.VisibilityFromDLLStorageClass
)
805 std::optional
<llvm::GlobalValue::VisibilityTypes
> DLLExportVisibility
=
806 getLLVMVisibility(LO
.getDLLExportVisibility());
808 std::optional
<llvm::GlobalValue::VisibilityTypes
>
809 NoDLLStorageClassVisibility
=
810 getLLVMVisibility(LO
.getNoDLLStorageClassVisibility());
812 std::optional
<llvm::GlobalValue::VisibilityTypes
>
813 ExternDeclDLLImportVisibility
=
814 getLLVMVisibility(LO
.getExternDeclDLLImportVisibility());
816 std::optional
<llvm::GlobalValue::VisibilityTypes
>
817 ExternDeclNoDLLStorageClassVisibility
=
818 getLLVMVisibility(LO
.getExternDeclNoDLLStorageClassVisibility());
820 for (llvm::GlobalValue
&GV
: M
.global_values()) {
821 if (GV
.hasAppendingLinkage() || GV
.hasLocalLinkage())
824 if (GV
.isDeclarationForLinker())
825 setLLVMVisibility(GV
, GV
.getDLLStorageClass() ==
826 llvm::GlobalValue::DLLImportStorageClass
827 ? ExternDeclDLLImportVisibility
828 : ExternDeclNoDLLStorageClassVisibility
);
830 setLLVMVisibility(GV
, GV
.getDLLStorageClass() ==
831 llvm::GlobalValue::DLLExportStorageClass
832 ? DLLExportVisibility
833 : NoDLLStorageClassVisibility
);
835 GV
.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass
);
839 static bool isStackProtectorOn(const LangOptions
&LangOpts
,
840 const llvm::Triple
&Triple
,
841 clang::LangOptions::StackProtectorMode Mode
) {
842 if (Triple
.isAMDGPU() || Triple
.isNVPTX())
844 return LangOpts
.getStackProtector() == Mode
;
847 void CodeGenModule::Release() {
848 Module
*Primary
= getContext().getCurrentNamedModule();
849 if (CXX20ModuleInits
&& Primary
&& !Primary
->isHeaderLikeModule())
850 EmitModuleInitializers(Primary
);
852 DeferredDecls
.insert(EmittedDeferredDecls
.begin(),
853 EmittedDeferredDecls
.end());
854 EmittedDeferredDecls
.clear();
855 EmitVTablesOpportunistically();
856 applyGlobalValReplacements();
858 emitMultiVersionFunctions();
860 if (Context
.getLangOpts().IncrementalExtensions
&&
861 GlobalTopLevelStmtBlockInFlight
.first
) {
862 const TopLevelStmtDecl
*TLSD
= GlobalTopLevelStmtBlockInFlight
.second
;
863 GlobalTopLevelStmtBlockInFlight
.first
->FinishFunction(TLSD
->getEndLoc());
864 GlobalTopLevelStmtBlockInFlight
= {nullptr, nullptr};
867 // Module implementations are initialized the same way as a regular TU that
868 // imports one or more modules.
869 if (CXX20ModuleInits
&& Primary
&& Primary
->isInterfaceOrPartition())
870 EmitCXXModuleInitFunc(Primary
);
872 EmitCXXGlobalInitFunc();
873 EmitCXXGlobalCleanUpFunc();
874 registerGlobalDtorsWithAtExit();
875 EmitCXXThreadLocalInitFunc();
877 if (llvm::Function
*ObjCInitFunction
= ObjCRuntime
->ModuleInitFunction())
878 AddGlobalCtor(ObjCInitFunction
);
879 if (Context
.getLangOpts().CUDA
&& CUDARuntime
) {
880 if (llvm::Function
*CudaCtorFunction
= CUDARuntime
->finalizeModule())
881 AddGlobalCtor(CudaCtorFunction
);
884 OpenMPRuntime
->createOffloadEntriesAndInfoMetadata();
885 OpenMPRuntime
->clear();
888 getModule().setProfileSummary(
889 PGOReader
->getSummary(/* UseCS */ false).getMD(VMContext
),
890 llvm::ProfileSummary::PSK_Instr
);
891 if (PGOStats
.hasDiagnostics())
892 PGOStats
.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName
);
894 llvm::stable_sort(GlobalCtors
, [](const Structor
&L
, const Structor
&R
) {
895 return L
.LexOrder
< R
.LexOrder
;
897 EmitCtorList(GlobalCtors
, "llvm.global_ctors");
898 EmitCtorList(GlobalDtors
, "llvm.global_dtors");
899 EmitGlobalAnnotations();
900 EmitStaticExternCAliases();
902 EmitDeferredUnusedCoverageMappings();
903 CodeGenPGO(*this).setValueProfilingFlag(getModule());
904 CodeGenPGO(*this).setProfileVersion(getModule());
906 CoverageMapping
->emit();
907 if (CodeGenOpts
.SanitizeCfiCrossDso
) {
908 CodeGenFunction(*this).EmitCfiCheckFail();
909 CodeGenFunction(*this).EmitCfiCheckStub();
911 if (LangOpts
.Sanitize
.has(SanitizerKind::KCFI
))
913 emitAtAvailableLinkGuard();
914 if (Context
.getTargetInfo().getTriple().isWasm())
917 if (getTriple().isAMDGPU() ||
918 (getTriple().isSPIRV() && getTriple().getVendor() == llvm::Triple::AMD
)) {
919 // Emit amdhsa_code_object_version module flag, which is code object version
921 if (getTarget().getTargetOpts().CodeObjectVersion
!=
922 llvm::CodeObjectVersionKind::COV_None
) {
923 getModule().addModuleFlag(llvm::Module::Error
,
924 "amdhsa_code_object_version",
925 getTarget().getTargetOpts().CodeObjectVersion
);
928 // Currently, "-mprintf-kind" option is only supported for HIP
930 auto *MDStr
= llvm::MDString::get(
931 getLLVMContext(), (getTarget().getTargetOpts().AMDGPUPrintfKindVal
==
932 TargetOptions::AMDGPUPrintfKind::Hostcall
)
935 getModule().addModuleFlag(llvm::Module::Error
, "amdgpu_printf_kind",
940 // Emit a global array containing all external kernels or device variables
941 // used by host functions and mark it as used for CUDA/HIP. This is necessary
942 // to get kernels or device variables in archives linked in even if these
943 // kernels or device variables are only used in host functions.
944 if (!Context
.CUDAExternalDeviceDeclODRUsedByHost
.empty()) {
945 SmallVector
<llvm::Constant
*, 8> UsedArray
;
946 for (auto D
: Context
.CUDAExternalDeviceDeclODRUsedByHost
) {
948 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
))
949 GD
= GlobalDecl(FD
, KernelReferenceKind::Kernel
);
952 UsedArray
.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
953 GetAddrOfGlobal(GD
), Int8PtrTy
));
956 llvm::ArrayType
*ATy
= llvm::ArrayType::get(Int8PtrTy
, UsedArray
.size());
958 auto *GV
= new llvm::GlobalVariable(
959 getModule(), ATy
, false, llvm::GlobalValue::InternalLinkage
,
960 llvm::ConstantArray::get(ATy
, UsedArray
), "__clang_gpu_used_external");
961 addCompilerUsedGlobal(GV
);
963 if (LangOpts
.HIP
&& !getLangOpts().OffloadingNewDriver
) {
964 // Emit a unique ID so that host and device binaries from the same
965 // compilation unit can be associated.
966 auto *GV
= new llvm::GlobalVariable(
967 getModule(), Int8Ty
, false, llvm::GlobalValue::ExternalLinkage
,
968 llvm::Constant::getNullValue(Int8Ty
),
969 "__hip_cuid_" + getContext().getCUIDHash());
970 addCompilerUsedGlobal(GV
);
976 if (CodeGenOpts
.Autolink
&&
977 (Context
.getLangOpts().Modules
|| !LinkerOptionsMetadata
.empty())) {
978 EmitModuleLinkOptions();
981 // On ELF we pass the dependent library specifiers directly to the linker
982 // without manipulating them. This is in contrast to other platforms where
983 // they are mapped to a specific linker option by the compiler. This
984 // difference is a result of the greater variety of ELF linkers and the fact
985 // that ELF linkers tend to handle libraries in a more complicated fashion
986 // than on other platforms. This forces us to defer handling the dependent
987 // libs to the linker.
989 // CUDA/HIP device and host libraries are different. Currently there is no
990 // way to differentiate dependent libraries for host or device. Existing
991 // usage of #pragma comment(lib, *) is intended for host libraries on
992 // Windows. Therefore emit llvm.dependent-libraries only for host.
993 if (!ELFDependentLibraries
.empty() && !Context
.getLangOpts().CUDAIsDevice
) {
994 auto *NMD
= getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
995 for (auto *MD
: ELFDependentLibraries
)
999 if (CodeGenOpts
.DwarfVersion
) {
1000 getModule().addModuleFlag(llvm::Module::Max
, "Dwarf Version",
1001 CodeGenOpts
.DwarfVersion
);
1004 if (CodeGenOpts
.Dwarf64
)
1005 getModule().addModuleFlag(llvm::Module::Max
, "DWARF64", 1);
1007 if (Context
.getLangOpts().SemanticInterposition
)
1008 // Require various optimization to respect semantic interposition.
1009 getModule().setSemanticInterposition(true);
1011 if (CodeGenOpts
.EmitCodeView
) {
1012 // Indicate that we want CodeView in the metadata.
1013 getModule().addModuleFlag(llvm::Module::Warning
, "CodeView", 1);
1015 if (CodeGenOpts
.CodeViewGHash
) {
1016 getModule().addModuleFlag(llvm::Module::Warning
, "CodeViewGHash", 1);
1018 if (CodeGenOpts
.ControlFlowGuard
) {
1019 // Function ID tables and checks for Control Flow Guard (cfguard=2).
1020 getModule().addModuleFlag(llvm::Module::Warning
, "cfguard", 2);
1021 } else if (CodeGenOpts
.ControlFlowGuardNoChecks
) {
1022 // Function ID tables for Control Flow Guard (cfguard=1).
1023 getModule().addModuleFlag(llvm::Module::Warning
, "cfguard", 1);
1025 if (CodeGenOpts
.EHContGuard
) {
1026 // Function ID tables for EH Continuation Guard.
1027 getModule().addModuleFlag(llvm::Module::Warning
, "ehcontguard", 1);
1029 if (Context
.getLangOpts().Kernel
) {
1030 // Note if we are compiling with /kernel.
1031 getModule().addModuleFlag(llvm::Module::Warning
, "ms-kernel", 1);
1033 if (CodeGenOpts
.OptimizationLevel
> 0 && CodeGenOpts
.StrictVTablePointers
) {
1034 // We don't support LTO with 2 with different StrictVTablePointers
1035 // FIXME: we could support it by stripping all the information introduced
1036 // by StrictVTablePointers.
1038 getModule().addModuleFlag(llvm::Module::Error
, "StrictVTablePointers",1);
1040 llvm::Metadata
*Ops
[2] = {
1041 llvm::MDString::get(VMContext
, "StrictVTablePointers"),
1042 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1043 llvm::Type::getInt32Ty(VMContext
), 1))};
1045 getModule().addModuleFlag(llvm::Module::Require
,
1046 "StrictVTablePointersRequirement",
1047 llvm::MDNode::get(VMContext
, Ops
));
1049 if (getModuleDebugInfo())
1050 // We support a single version in the linked module. The LLVM
1051 // parser will drop debug info with a different version number
1052 // (and warn about it, too).
1053 getModule().addModuleFlag(llvm::Module::Warning
, "Debug Info Version",
1054 llvm::DEBUG_METADATA_VERSION
);
1056 // We need to record the widths of enums and wchar_t, so that we can generate
1057 // the correct build attributes in the ARM backend. wchar_size is also used by
1058 // TargetLibraryInfo.
1059 uint64_t WCharWidth
=
1060 Context
.getTypeSizeInChars(Context
.getWideCharType()).getQuantity();
1061 getModule().addModuleFlag(llvm::Module::Error
, "wchar_size", WCharWidth
);
1063 if (getTriple().isOSzOS()) {
1064 getModule().addModuleFlag(llvm::Module::Warning
,
1065 "zos_product_major_version",
1066 uint32_t(CLANG_VERSION_MAJOR
));
1067 getModule().addModuleFlag(llvm::Module::Warning
,
1068 "zos_product_minor_version",
1069 uint32_t(CLANG_VERSION_MINOR
));
1070 getModule().addModuleFlag(llvm::Module::Warning
, "zos_product_patchlevel",
1071 uint32_t(CLANG_VERSION_PATCHLEVEL
));
1072 std::string ProductId
= getClangVendor() + "clang";
1073 getModule().addModuleFlag(llvm::Module::Error
, "zos_product_id",
1074 llvm::MDString::get(VMContext
, ProductId
));
1076 // Record the language because we need it for the PPA2.
1077 StringRef lang_str
= languageToString(
1078 LangStandard::getLangStandardForKind(LangOpts
.LangStd
).Language
);
1079 getModule().addModuleFlag(llvm::Module::Error
, "zos_cu_language",
1080 llvm::MDString::get(VMContext
, lang_str
));
1082 time_t TT
= PreprocessorOpts
.SourceDateEpoch
1083 ? *PreprocessorOpts
.SourceDateEpoch
1084 : std::time(nullptr);
1085 getModule().addModuleFlag(llvm::Module::Max
, "zos_translation_time",
1086 static_cast<uint64_t>(TT
));
1088 // Multiple modes will be supported here.
1089 getModule().addModuleFlag(llvm::Module::Error
, "zos_le_char_mode",
1090 llvm::MDString::get(VMContext
, "ascii"));
1093 llvm::Triple T
= Context
.getTargetInfo().getTriple();
1094 if (T
.isARM() || T
.isThumb()) {
1095 // The minimum width of an enum in bytes
1096 uint64_t EnumWidth
= Context
.getLangOpts().ShortEnums
? 1 : 4;
1097 getModule().addModuleFlag(llvm::Module::Error
, "min_enum_size", EnumWidth
);
1101 StringRef ABIStr
= Target
.getABI();
1102 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
1103 getModule().addModuleFlag(llvm::Module::Error
, "target-abi",
1104 llvm::MDString::get(Ctx
, ABIStr
));
1106 // Add the canonical ISA string as metadata so the backend can set the ELF
1107 // attributes correctly. We use AppendUnique so LTO will keep all of the
1108 // unique ISA strings that were linked together.
1109 const std::vector
<std::string
> &Features
=
1110 getTarget().getTargetOpts().Features
;
1112 llvm::RISCVISAInfo::parseFeatures(T
.isRISCV64() ? 64 : 32, Features
);
1113 if (!errorToBool(ParseResult
.takeError()))
1114 getModule().addModuleFlag(
1115 llvm::Module::AppendUnique
, "riscv-isa",
1117 Ctx
, llvm::MDString::get(Ctx
, (*ParseResult
)->toString())));
1120 if (CodeGenOpts
.SanitizeCfiCrossDso
) {
1121 // Indicate that we want cross-DSO control flow integrity checks.
1122 getModule().addModuleFlag(llvm::Module::Override
, "Cross-DSO CFI", 1);
1125 if (CodeGenOpts
.WholeProgramVTables
) {
1126 // Indicate whether VFE was enabled for this module, so that the
1127 // vcall_visibility metadata added under whole program vtables is handled
1128 // appropriately in the optimizer.
1129 getModule().addModuleFlag(llvm::Module::Error
, "Virtual Function Elim",
1130 CodeGenOpts
.VirtualFunctionElimination
);
1133 if (LangOpts
.Sanitize
.has(SanitizerKind::CFIICall
)) {
1134 getModule().addModuleFlag(llvm::Module::Override
,
1135 "CFI Canonical Jump Tables",
1136 CodeGenOpts
.SanitizeCfiCanonicalJumpTables
);
1139 if (LangOpts
.Sanitize
.has(SanitizerKind::KCFI
)) {
1140 getModule().addModuleFlag(llvm::Module::Override
, "kcfi", 1);
1141 // KCFI assumes patchable-function-prefix is the same for all indirectly
1142 // called functions. Store the expected offset for code generation.
1143 if (CodeGenOpts
.PatchableFunctionEntryOffset
)
1144 getModule().addModuleFlag(llvm::Module::Override
, "kcfi-offset",
1145 CodeGenOpts
.PatchableFunctionEntryOffset
);
1148 if (CodeGenOpts
.CFProtectionReturn
&&
1149 Target
.checkCFProtectionReturnSupported(getDiags())) {
1150 // Indicate that we want to instrument return control flow protection.
1151 getModule().addModuleFlag(llvm::Module::Min
, "cf-protection-return",
1155 if (CodeGenOpts
.CFProtectionBranch
&&
1156 Target
.checkCFProtectionBranchSupported(getDiags())) {
1157 // Indicate that we want to instrument branch control flow protection.
1158 getModule().addModuleFlag(llvm::Module::Min
, "cf-protection-branch",
1162 if (CodeGenOpts
.FunctionReturnThunks
)
1163 getModule().addModuleFlag(llvm::Module::Override
, "function_return_thunk_extern", 1);
1165 if (CodeGenOpts
.IndirectBranchCSPrefix
)
1166 getModule().addModuleFlag(llvm::Module::Override
, "indirect_branch_cs_prefix", 1);
1168 // Add module metadata for return address signing (ignoring
1169 // non-leaf/all) and stack tagging. These are actually turned on by function
1170 // attributes, but we use module metadata to emit build attributes. This is
1171 // needed for LTO, where the function attributes are inside bitcode
1172 // serialised into a global variable by the time build attributes are
1173 // emitted, so we can't access them. LTO objects could be compiled with
1174 // different flags therefore module flags are set to "Min" behavior to achieve
1175 // the same end result of the normal build where e.g BTI is off if any object
1176 // doesn't support it.
1177 if (Context
.getTargetInfo().hasFeature("ptrauth") &&
1178 LangOpts
.getSignReturnAddressScope() !=
1179 LangOptions::SignReturnAddressScopeKind::None
)
1180 getModule().addModuleFlag(llvm::Module::Override
,
1181 "sign-return-address-buildattr", 1);
1182 if (LangOpts
.Sanitize
.has(SanitizerKind::MemtagStack
))
1183 getModule().addModuleFlag(llvm::Module::Override
,
1184 "tag-stack-memory-buildattr", 1);
1186 if (T
.isARM() || T
.isThumb() || T
.isAArch64()) {
1187 if (LangOpts
.BranchTargetEnforcement
)
1188 getModule().addModuleFlag(llvm::Module::Min
, "branch-target-enforcement",
1190 if (LangOpts
.BranchProtectionPAuthLR
)
1191 getModule().addModuleFlag(llvm::Module::Min
, "branch-protection-pauth-lr",
1193 if (LangOpts
.GuardedControlStack
)
1194 getModule().addModuleFlag(llvm::Module::Min
, "guarded-control-stack", 1);
1195 if (LangOpts
.hasSignReturnAddress())
1196 getModule().addModuleFlag(llvm::Module::Min
, "sign-return-address", 1);
1197 if (LangOpts
.isSignReturnAddressScopeAll())
1198 getModule().addModuleFlag(llvm::Module::Min
, "sign-return-address-all",
1200 if (!LangOpts
.isSignReturnAddressWithAKey())
1201 getModule().addModuleFlag(llvm::Module::Min
,
1202 "sign-return-address-with-bkey", 1);
1204 if (getTriple().isOSLinux()) {
1205 assert(getTriple().isOSBinFormatELF());
1206 using namespace llvm::ELF
;
1207 uint64_t PAuthABIVersion
=
1208 (LangOpts
.PointerAuthIntrinsics
1209 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INTRINSICS
) |
1210 (LangOpts
.PointerAuthCalls
1211 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_CALLS
) |
1212 (LangOpts
.PointerAuthReturns
1213 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_RETURNS
) |
1214 (LangOpts
.PointerAuthAuthTraps
1215 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_AUTHTRAPS
) |
1216 (LangOpts
.PointerAuthVTPtrAddressDiscrimination
1217 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRADDRDISCR
) |
1218 (LangOpts
.PointerAuthVTPtrTypeDiscrimination
1219 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_VPTRTYPEDISCR
) |
1220 (LangOpts
.PointerAuthInitFini
1221 << AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI
);
1222 static_assert(AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_INITFINI
==
1223 AARCH64_PAUTH_PLATFORM_LLVM_LINUX_VERSION_LAST
,
1224 "Update when new enum items are defined");
1225 if (PAuthABIVersion
!= 0) {
1226 getModule().addModuleFlag(llvm::Module::Error
,
1227 "aarch64-elf-pauthabi-platform",
1228 AARCH64_PAUTH_PLATFORM_LLVM_LINUX
);
1229 getModule().addModuleFlag(llvm::Module::Error
,
1230 "aarch64-elf-pauthabi-version",
1236 if (CodeGenOpts
.StackClashProtector
)
1237 getModule().addModuleFlag(
1238 llvm::Module::Override
, "probe-stack",
1239 llvm::MDString::get(TheModule
.getContext(), "inline-asm"));
1241 if (CodeGenOpts
.StackProbeSize
&& CodeGenOpts
.StackProbeSize
!= 4096)
1242 getModule().addModuleFlag(llvm::Module::Min
, "stack-probe-size",
1243 CodeGenOpts
.StackProbeSize
);
1245 if (!CodeGenOpts
.MemoryProfileOutput
.empty()) {
1246 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
1247 getModule().addModuleFlag(
1248 llvm::Module::Error
, "MemProfProfileFilename",
1249 llvm::MDString::get(Ctx
, CodeGenOpts
.MemoryProfileOutput
));
1252 if (LangOpts
.CUDAIsDevice
&& getTriple().isNVPTX()) {
1253 // Indicate whether __nvvm_reflect should be configured to flush denormal
1254 // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
1256 getModule().addModuleFlag(llvm::Module::Override
, "nvvm-reflect-ftz",
1257 CodeGenOpts
.FP32DenormalMode
.Output
!=
1258 llvm::DenormalMode::IEEE
);
1261 if (LangOpts
.EHAsynch
)
1262 getModule().addModuleFlag(llvm::Module::Warning
, "eh-asynch", 1);
1264 // Indicate whether this Module was compiled with -fopenmp
1265 if (getLangOpts().OpenMP
&& !getLangOpts().OpenMPSimd
)
1266 getModule().addModuleFlag(llvm::Module::Max
, "openmp", LangOpts
.OpenMP
);
1267 if (getLangOpts().OpenMPIsTargetDevice
)
1268 getModule().addModuleFlag(llvm::Module::Max
, "openmp-device",
1271 // Emit OpenCL specific module metadata: OpenCL/SPIR version.
1272 if (LangOpts
.OpenCL
|| (LangOpts
.CUDAIsDevice
&& getTriple().isSPIRV())) {
1273 EmitOpenCLMetadata();
1274 // Emit SPIR version.
1275 if (getTriple().isSPIR()) {
1276 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
1277 // opencl.spir.version named metadata.
1278 // C++ for OpenCL has a distinct mapping for version compatibility with
1280 auto Version
= LangOpts
.getOpenCLCompatibleVersion();
1281 llvm::Metadata
*SPIRVerElts
[] = {
1282 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1283 Int32Ty
, Version
/ 100)),
1284 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1285 Int32Ty
, (Version
/ 100 > 1) ? 0 : 2))};
1286 llvm::NamedMDNode
*SPIRVerMD
=
1287 TheModule
.getOrInsertNamedMetadata("opencl.spir.version");
1288 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
1289 SPIRVerMD
->addOperand(llvm::MDNode::get(Ctx
, SPIRVerElts
));
1293 // HLSL related end of code gen work items.
1295 getHLSLRuntime().finishCodeGen();
1297 if (uint32_t PLevel
= Context
.getLangOpts().PICLevel
) {
1298 assert(PLevel
< 3 && "Invalid PIC Level");
1299 getModule().setPICLevel(static_cast<llvm::PICLevel::Level
>(PLevel
));
1300 if (Context
.getLangOpts().PIE
)
1301 getModule().setPIELevel(static_cast<llvm::PIELevel::Level
>(PLevel
));
1304 if (getCodeGenOpts().CodeModel
.size() > 0) {
1305 unsigned CM
= llvm::StringSwitch
<unsigned>(getCodeGenOpts().CodeModel
)
1306 .Case("tiny", llvm::CodeModel::Tiny
)
1307 .Case("small", llvm::CodeModel::Small
)
1308 .Case("kernel", llvm::CodeModel::Kernel
)
1309 .Case("medium", llvm::CodeModel::Medium
)
1310 .Case("large", llvm::CodeModel::Large
)
1313 llvm::CodeModel::Model codeModel
= static_cast<llvm::CodeModel::Model
>(CM
);
1314 getModule().setCodeModel(codeModel
);
1316 if ((CM
== llvm::CodeModel::Medium
|| CM
== llvm::CodeModel::Large
) &&
1317 Context
.getTargetInfo().getTriple().getArch() ==
1318 llvm::Triple::x86_64
) {
1319 getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold
);
1324 if (CodeGenOpts
.NoPLT
)
1325 getModule().setRtLibUseGOT();
1326 if (getTriple().isOSBinFormatELF() &&
1327 CodeGenOpts
.DirectAccessExternalData
!=
1328 getModule().getDirectAccessExternalData()) {
1329 getModule().setDirectAccessExternalData(
1330 CodeGenOpts
.DirectAccessExternalData
);
1332 if (CodeGenOpts
.UnwindTables
)
1333 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts
.UnwindTables
));
1335 switch (CodeGenOpts
.getFramePointer()) {
1336 case CodeGenOptions::FramePointerKind::None
:
1337 // 0 ("none") is the default.
1339 case CodeGenOptions::FramePointerKind::Reserved
:
1340 getModule().setFramePointer(llvm::FramePointerKind::Reserved
);
1342 case CodeGenOptions::FramePointerKind::NonLeaf
:
1343 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf
);
1345 case CodeGenOptions::FramePointerKind::All
:
1346 getModule().setFramePointer(llvm::FramePointerKind::All
);
1350 SimplifyPersonality();
1352 if (getCodeGenOpts().EmitDeclMetadata
)
1355 if (getCodeGenOpts().CoverageNotesFile
.size() ||
1356 getCodeGenOpts().CoverageDataFile
.size())
1359 if (CGDebugInfo
*DI
= getModuleDebugInfo())
1362 if (getCodeGenOpts().EmitVersionIdentMetadata
)
1363 EmitVersionIdentMetadata();
1365 if (!getCodeGenOpts().RecordCommandLine
.empty())
1366 EmitCommandLineMetadata();
1368 if (!getCodeGenOpts().StackProtectorGuard
.empty())
1369 getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard
);
1370 if (!getCodeGenOpts().StackProtectorGuardReg
.empty())
1371 getModule().setStackProtectorGuardReg(
1372 getCodeGenOpts().StackProtectorGuardReg
);
1373 if (!getCodeGenOpts().StackProtectorGuardSymbol
.empty())
1374 getModule().setStackProtectorGuardSymbol(
1375 getCodeGenOpts().StackProtectorGuardSymbol
);
1376 if (getCodeGenOpts().StackProtectorGuardOffset
!= INT_MAX
)
1377 getModule().setStackProtectorGuardOffset(
1378 getCodeGenOpts().StackProtectorGuardOffset
);
1379 if (getCodeGenOpts().StackAlignment
)
1380 getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment
);
1381 if (getCodeGenOpts().SkipRaxSetup
)
1382 getModule().addModuleFlag(llvm::Module::Override
, "SkipRaxSetup", 1);
1383 if (getLangOpts().RegCall4
)
1384 getModule().addModuleFlag(llvm::Module::Override
, "RegCallv4", 1);
1386 if (getContext().getTargetInfo().getMaxTLSAlign())
1387 getModule().addModuleFlag(llvm::Module::Error
, "MaxTLSAlign",
1388 getContext().getTargetInfo().getMaxTLSAlign());
1390 getTargetCodeGenInfo().emitTargetGlobals(*this);
1392 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames
);
1394 EmitBackendOptionsMetadata(getCodeGenOpts());
1396 // If there is device offloading code embed it in the host now.
1397 EmbedObject(&getModule(), CodeGenOpts
, getDiags());
1399 // Set visibility from DLL storage class
1400 // We do this at the end of LLVM IR generation; after any operation
1401 // that might affect the DLL storage class or the visibility, and
1402 // before anything that might act on these.
1403 setVisibilityFromDLLStorageClass(LangOpts
, getModule());
1405 // Check the tail call symbols are truly undefined.
1406 if (getTriple().isPPC() && !MustTailCallUndefinedGlobals
.empty()) {
1407 for (auto &I
: MustTailCallUndefinedGlobals
) {
1408 if (!I
.first
->isDefined())
1409 getDiags().Report(I
.second
, diag::err_ppc_impossible_musttail
) << 2;
1411 StringRef MangledName
= getMangledName(GlobalDecl(I
.first
));
1412 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
1413 if (!Entry
|| Entry
->isWeakForLinker() ||
1414 Entry
->isDeclarationForLinker())
1415 getDiags().Report(I
.second
, diag::err_ppc_impossible_musttail
) << 2;
1421 void CodeGenModule::EmitOpenCLMetadata() {
1422 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
1423 // opencl.ocl.version named metadata node.
1424 // C++ for OpenCL has a distinct mapping for versions compatible with OpenCL.
1425 auto CLVersion
= LangOpts
.getOpenCLCompatibleVersion();
1427 auto EmitVersion
= [this](StringRef MDName
, int Version
) {
1428 llvm::Metadata
*OCLVerElts
[] = {
1429 llvm::ConstantAsMetadata::get(
1430 llvm::ConstantInt::get(Int32Ty
, Version
/ 100)),
1431 llvm::ConstantAsMetadata::get(
1432 llvm::ConstantInt::get(Int32Ty
, (Version
% 100) / 10))};
1433 llvm::NamedMDNode
*OCLVerMD
= TheModule
.getOrInsertNamedMetadata(MDName
);
1434 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
1435 OCLVerMD
->addOperand(llvm::MDNode::get(Ctx
, OCLVerElts
));
1438 EmitVersion("opencl.ocl.version", CLVersion
);
1439 if (LangOpts
.OpenCLCPlusPlus
) {
1440 // In addition to the OpenCL compatible version, emit the C++ version.
1441 EmitVersion("opencl.cxx.version", LangOpts
.OpenCLCPlusPlusVersion
);
1445 void CodeGenModule::EmitBackendOptionsMetadata(
1446 const CodeGenOptions
&CodeGenOpts
) {
1447 if (getTriple().isRISCV()) {
1448 getModule().addModuleFlag(llvm::Module::Min
, "SmallDataLimit",
1449 CodeGenOpts
.SmallDataLimit
);
1453 void CodeGenModule::UpdateCompletedType(const TagDecl
*TD
) {
1454 // Make sure that this type is translated.
1455 Types
.UpdateCompletedType(TD
);
1458 void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl
*RD
) {
1459 // Make sure that this type is translated.
1460 Types
.RefreshTypeCacheForClass(RD
);
1463 llvm::MDNode
*CodeGenModule::getTBAATypeInfo(QualType QTy
) {
1466 return TBAA
->getTypeInfo(QTy
);
1469 TBAAAccessInfo
CodeGenModule::getTBAAAccessInfo(QualType AccessType
) {
1471 return TBAAAccessInfo();
1472 if (getLangOpts().CUDAIsDevice
) {
1473 // As CUDA builtin surface/texture types are replaced, skip generating TBAA
1475 if (AccessType
->isCUDADeviceBuiltinSurfaceType()) {
1476 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
1478 return TBAAAccessInfo();
1479 } else if (AccessType
->isCUDADeviceBuiltinTextureType()) {
1480 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
1482 return TBAAAccessInfo();
1485 return TBAA
->getAccessInfo(AccessType
);
1489 CodeGenModule::getTBAAVTablePtrAccessInfo(llvm::Type
*VTablePtrType
) {
1491 return TBAAAccessInfo();
1492 return TBAA
->getVTablePtrAccessInfo(VTablePtrType
);
1495 llvm::MDNode
*CodeGenModule::getTBAAStructInfo(QualType QTy
) {
1498 return TBAA
->getTBAAStructInfo(QTy
);
1501 llvm::MDNode
*CodeGenModule::getTBAABaseTypeInfo(QualType QTy
) {
1504 return TBAA
->getBaseTypeInfo(QTy
);
1507 llvm::MDNode
*CodeGenModule::getTBAAAccessTagInfo(TBAAAccessInfo Info
) {
1510 return TBAA
->getAccessTagInfo(Info
);
1513 TBAAAccessInfo
CodeGenModule::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo
,
1514 TBAAAccessInfo TargetInfo
) {
1516 return TBAAAccessInfo();
1517 return TBAA
->mergeTBAAInfoForCast(SourceInfo
, TargetInfo
);
1521 CodeGenModule::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA
,
1522 TBAAAccessInfo InfoB
) {
1524 return TBAAAccessInfo();
1525 return TBAA
->mergeTBAAInfoForConditionalOperator(InfoA
, InfoB
);
1529 CodeGenModule::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo
,
1530 TBAAAccessInfo SrcInfo
) {
1532 return TBAAAccessInfo();
1533 return TBAA
->mergeTBAAInfoForConditionalOperator(DestInfo
, SrcInfo
);
1536 void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction
*Inst
,
1537 TBAAAccessInfo TBAAInfo
) {
1538 if (llvm::MDNode
*Tag
= getTBAAAccessTagInfo(TBAAInfo
))
1539 Inst
->setMetadata(llvm::LLVMContext::MD_tbaa
, Tag
);
1542 void CodeGenModule::DecorateInstructionWithInvariantGroup(
1543 llvm::Instruction
*I
, const CXXRecordDecl
*RD
) {
1544 I
->setMetadata(llvm::LLVMContext::MD_invariant_group
,
1545 llvm::MDNode::get(getLLVMContext(), {}));
1548 void CodeGenModule::Error(SourceLocation loc
, StringRef message
) {
1549 unsigned diagID
= getDiags().getCustomDiagID(DiagnosticsEngine::Error
, "%0");
1550 getDiags().Report(Context
.getFullLoc(loc
), diagID
) << message
;
1553 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1554 /// specified stmt yet.
1555 void CodeGenModule::ErrorUnsupported(const Stmt
*S
, const char *Type
) {
1556 unsigned DiagID
= getDiags().getCustomDiagID(DiagnosticsEngine::Error
,
1557 "cannot compile this %0 yet");
1558 std::string Msg
= Type
;
1559 getDiags().Report(Context
.getFullLoc(S
->getBeginLoc()), DiagID
)
1560 << Msg
<< S
->getSourceRange();
1563 /// ErrorUnsupported - Print out an error that codegen doesn't support the
1564 /// specified decl yet.
1565 void CodeGenModule::ErrorUnsupported(const Decl
*D
, const char *Type
) {
1566 unsigned DiagID
= getDiags().getCustomDiagID(DiagnosticsEngine::Error
,
1567 "cannot compile this %0 yet");
1568 std::string Msg
= Type
;
1569 getDiags().Report(Context
.getFullLoc(D
->getLocation()), DiagID
) << Msg
;
1572 llvm::ConstantInt
*CodeGenModule::getSize(CharUnits size
) {
1573 return llvm::ConstantInt::get(SizeTy
, size
.getQuantity());
1576 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue
*GV
,
1577 const NamedDecl
*D
) const {
1578 // Internal definitions always have default visibility.
1579 if (GV
->hasLocalLinkage()) {
1580 GV
->setVisibility(llvm::GlobalValue::DefaultVisibility
);
1586 // Set visibility for definitions, and for declarations if requested globally
1587 // or set explicitly.
1588 LinkageInfo LV
= D
->getLinkageAndVisibility();
1590 // OpenMP declare target variables must be visible to the host so they can
1591 // be registered. We require protected visibility unless the variable has
1592 // the DT_nohost modifier and does not need to be registered.
1593 if (Context
.getLangOpts().OpenMP
&&
1594 Context
.getLangOpts().OpenMPIsTargetDevice
&& isa
<VarDecl
>(D
) &&
1595 D
->hasAttr
<OMPDeclareTargetDeclAttr
>() &&
1596 D
->getAttr
<OMPDeclareTargetDeclAttr
>()->getDevType() !=
1597 OMPDeclareTargetDeclAttr::DT_NoHost
&&
1598 LV
.getVisibility() == HiddenVisibility
) {
1599 GV
->setVisibility(llvm::GlobalValue::ProtectedVisibility
);
1603 if (GV
->hasDLLExportStorageClass() || GV
->hasDLLImportStorageClass()) {
1604 // Reject incompatible dlllstorage and visibility annotations.
1605 if (!LV
.isVisibilityExplicit())
1607 if (GV
->hasDLLExportStorageClass()) {
1608 if (LV
.getVisibility() == HiddenVisibility
)
1609 getDiags().Report(D
->getLocation(),
1610 diag::err_hidden_visibility_dllexport
);
1611 } else if (LV
.getVisibility() != DefaultVisibility
) {
1612 getDiags().Report(D
->getLocation(),
1613 diag::err_non_default_visibility_dllimport
);
1618 if (LV
.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls
||
1619 !GV
->isDeclarationForLinker())
1620 GV
->setVisibility(GetLLVMVisibility(LV
.getVisibility()));
1623 static bool shouldAssumeDSOLocal(const CodeGenModule
&CGM
,
1624 llvm::GlobalValue
*GV
) {
1625 if (GV
->hasLocalLinkage())
1628 if (!GV
->hasDefaultVisibility() && !GV
->hasExternalWeakLinkage())
1631 // DLLImport explicitly marks the GV as external.
1632 if (GV
->hasDLLImportStorageClass())
1635 const llvm::Triple
&TT
= CGM
.getTriple();
1636 const auto &CGOpts
= CGM
.getCodeGenOpts();
1637 if (TT
.isWindowsGNUEnvironment()) {
1638 // In MinGW, variables without DLLImport can still be automatically
1639 // imported from a DLL by the linker; don't mark variables that
1640 // potentially could come from another DLL as DSO local.
1642 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1643 // (and this actually happens in the public interface of libstdc++), so
1644 // such variables can't be marked as DSO local. (Native TLS variables
1645 // can't be dllimported at all, though.)
1646 if (GV
->isDeclarationForLinker() && isa
<llvm::GlobalVariable
>(GV
) &&
1647 (!GV
->isThreadLocal() || CGM
.getCodeGenOpts().EmulatedTLS
) &&
1652 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1653 // remain unresolved in the link, they can be resolved to zero, which is
1654 // outside the current DSO.
1655 if (TT
.isOSBinFormatCOFF() && GV
->hasExternalWeakLinkage())
1658 // Every other GV is local on COFF.
1659 // Make an exception for windows OS in the triple: Some firmware builds use
1660 // *-win32-macho triples. This (accidentally?) produced windows relocations
1661 // without GOT tables in older clang versions; Keep this behaviour.
1662 // FIXME: even thread local variables?
1663 if (TT
.isOSBinFormatCOFF() || (TT
.isOSWindows() && TT
.isOSBinFormatMachO()))
1666 // Only handle COFF and ELF for now.
1667 if (!TT
.isOSBinFormatELF())
1670 // If this is not an executable, don't assume anything is local.
1671 llvm::Reloc::Model RM
= CGOpts
.RelocationModel
;
1672 const auto &LOpts
= CGM
.getLangOpts();
1673 if (RM
!= llvm::Reloc::Static
&& !LOpts
.PIE
) {
1674 // On ELF, if -fno-semantic-interposition is specified and the target
1675 // supports local aliases, there will be neither CC1
1676 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1677 // dso_local on the function if using a local alias is preferable (can avoid
1678 // PLT indirection).
1679 if (!(isa
<llvm::Function
>(GV
) && GV
->canBenefitFromLocalAlias()))
1681 return !(CGM
.getLangOpts().SemanticInterposition
||
1682 CGM
.getLangOpts().HalfNoSemanticInterposition
);
1685 // A definition cannot be preempted from an executable.
1686 if (!GV
->isDeclarationForLinker())
1689 // Most PIC code sequences that assume that a symbol is local cannot produce a
1690 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
1691 // depended, it seems worth it to handle it here.
1692 if (RM
== llvm::Reloc::PIC_
&& GV
->hasExternalWeakLinkage())
1695 // PowerPC64 prefers TOC indirection to avoid copy relocations.
1699 if (CGOpts
.DirectAccessExternalData
) {
1700 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1701 // for non-thread-local variables. If the symbol is not defined in the
1702 // executable, a copy relocation will be needed at link time. dso_local is
1703 // excluded for thread-local variables because they generally don't support
1704 // copy relocations.
1705 if (auto *Var
= dyn_cast
<llvm::GlobalVariable
>(GV
))
1706 if (!Var
->isThreadLocal())
1709 // -fno-pic sets dso_local on a function declaration to allow direct
1710 // accesses when taking its address (similar to a data symbol). If the
1711 // function is not defined in the executable, a canonical PLT entry will be
1712 // needed at link time. -fno-direct-access-external-data can avoid the
1713 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1714 // it could just cause trouble without providing perceptible benefits.
1715 if (isa
<llvm::Function
>(GV
) && !CGOpts
.NoPLT
&& RM
== llvm::Reloc::Static
)
1719 // If we can use copy relocations we can assume it is local.
1721 // Otherwise don't assume it is local.
1725 void CodeGenModule::setDSOLocal(llvm::GlobalValue
*GV
) const {
1726 GV
->setDSOLocal(shouldAssumeDSOLocal(*this, GV
));
1729 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue
*GV
,
1730 GlobalDecl GD
) const {
1731 const auto *D
= dyn_cast
<NamedDecl
>(GD
.getDecl());
1732 // C++ destructors have a few C++ ABI specific special cases.
1733 if (const auto *Dtor
= dyn_cast_or_null
<CXXDestructorDecl
>(D
)) {
1734 getCXXABI().setCXXDestructorDLLStorage(GV
, Dtor
, GD
.getDtorType());
1737 setDLLImportDLLExport(GV
, D
);
1740 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue
*GV
,
1741 const NamedDecl
*D
) const {
1742 if (D
&& D
->isExternallyVisible()) {
1743 if (D
->hasAttr
<DLLImportAttr
>())
1744 GV
->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass
);
1745 else if ((D
->hasAttr
<DLLExportAttr
>() ||
1746 shouldMapVisibilityToDLLExport(D
)) &&
1747 !GV
->isDeclarationForLinker())
1748 GV
->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass
);
1752 void CodeGenModule::setGVProperties(llvm::GlobalValue
*GV
,
1753 GlobalDecl GD
) const {
1754 setDLLImportDLLExport(GV
, GD
);
1755 setGVPropertiesAux(GV
, dyn_cast
<NamedDecl
>(GD
.getDecl()));
1758 void CodeGenModule::setGVProperties(llvm::GlobalValue
*GV
,
1759 const NamedDecl
*D
) const {
1760 setDLLImportDLLExport(GV
, D
);
1761 setGVPropertiesAux(GV
, D
);
1764 void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue
*GV
,
1765 const NamedDecl
*D
) const {
1766 setGlobalVisibility(GV
, D
);
1768 GV
->setPartition(CodeGenOpts
.SymbolPartition
);
1771 static llvm::GlobalVariable::ThreadLocalMode
GetLLVMTLSModel(StringRef S
) {
1772 return llvm::StringSwitch
<llvm::GlobalVariable::ThreadLocalMode
>(S
)
1773 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel
)
1774 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel
)
1775 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel
)
1776 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel
);
1779 llvm::GlobalVariable::ThreadLocalMode
1780 CodeGenModule::GetDefaultLLVMTLSModel() const {
1781 switch (CodeGenOpts
.getDefaultTLSModel()) {
1782 case CodeGenOptions::GeneralDynamicTLSModel
:
1783 return llvm::GlobalVariable::GeneralDynamicTLSModel
;
1784 case CodeGenOptions::LocalDynamicTLSModel
:
1785 return llvm::GlobalVariable::LocalDynamicTLSModel
;
1786 case CodeGenOptions::InitialExecTLSModel
:
1787 return llvm::GlobalVariable::InitialExecTLSModel
;
1788 case CodeGenOptions::LocalExecTLSModel
:
1789 return llvm::GlobalVariable::LocalExecTLSModel
;
1791 llvm_unreachable("Invalid TLS model!");
1794 void CodeGenModule::setTLSMode(llvm::GlobalValue
*GV
, const VarDecl
&D
) const {
1795 assert(D
.getTLSKind() && "setting TLS mode on non-TLS var!");
1797 llvm::GlobalValue::ThreadLocalMode TLM
;
1798 TLM
= GetDefaultLLVMTLSModel();
1800 // Override the TLS model if it is explicitly specified.
1801 if (const TLSModelAttr
*Attr
= D
.getAttr
<TLSModelAttr
>()) {
1802 TLM
= GetLLVMTLSModel(Attr
->getModel());
1805 GV
->setThreadLocalMode(TLM
);
1808 static std::string
getCPUSpecificMangling(const CodeGenModule
&CGM
,
1810 const TargetInfo
&Target
= CGM
.getTarget();
1811 return (Twine('.') + Twine(Target
.CPUSpecificManglingCharacter(Name
))).str();
1814 static void AppendCPUSpecificCPUDispatchMangling(const CodeGenModule
&CGM
,
1815 const CPUSpecificAttr
*Attr
,
1818 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1821 Out
<< getCPUSpecificMangling(CGM
, Attr
->getCPUName(CPUIndex
)->getName());
1822 else if (CGM
.getTarget().supportsIFunc())
1826 // Returns true if GD is a function decl with internal linkage and
1827 // needs a unique suffix after the mangled name.
1828 static bool isUniqueInternalLinkageDecl(GlobalDecl GD
,
1829 CodeGenModule
&CGM
) {
1830 const Decl
*D
= GD
.getDecl();
1831 return !CGM
.getModuleNameHash().empty() && isa
<FunctionDecl
>(D
) &&
1832 (CGM
.getFunctionLinkage(GD
) == llvm::GlobalValue::InternalLinkage
);
1835 static std::string
getMangledNameImpl(CodeGenModule
&CGM
, GlobalDecl GD
,
1836 const NamedDecl
*ND
,
1837 bool OmitMultiVersionMangling
= false) {
1838 SmallString
<256> Buffer
;
1839 llvm::raw_svector_ostream
Out(Buffer
);
1840 MangleContext
&MC
= CGM
.getCXXABI().getMangleContext();
1841 if (!CGM
.getModuleNameHash().empty())
1842 MC
.needsUniqueInternalLinkageNames();
1843 bool ShouldMangle
= MC
.shouldMangleDeclName(ND
);
1845 MC
.mangleName(GD
.getWithDecl(ND
), Out
);
1847 IdentifierInfo
*II
= ND
->getIdentifier();
1848 assert(II
&& "Attempt to mangle unnamed decl.");
1849 const auto *FD
= dyn_cast
<FunctionDecl
>(ND
);
1852 FD
->getType()->castAs
<FunctionType
>()->getCallConv() == CC_X86RegCall
) {
1853 if (CGM
.getLangOpts().RegCall4
)
1854 Out
<< "__regcall4__" << II
->getName();
1856 Out
<< "__regcall3__" << II
->getName();
1857 } else if (FD
&& FD
->hasAttr
<CUDAGlobalAttr
>() &&
1858 GD
.getKernelReferenceKind() == KernelReferenceKind::Stub
) {
1859 Out
<< "__device_stub__" << II
->getName();
1861 Out
<< II
->getName();
1865 // Check if the module name hash should be appended for internal linkage
1866 // symbols. This should come before multi-version target suffixes are
1867 // appended. This is to keep the name and module hash suffix of the
1868 // internal linkage function together. The unique suffix should only be
1869 // added when name mangling is done to make sure that the final name can
1870 // be properly demangled. For example, for C functions without prototypes,
1871 // name mangling is not done and the unique suffix should not be appeneded
1873 if (ShouldMangle
&& isUniqueInternalLinkageDecl(GD
, CGM
)) {
1874 assert(CGM
.getCodeGenOpts().UniqueInternalLinkageNames
&&
1875 "Hash computed when not explicitly requested");
1876 Out
<< CGM
.getModuleNameHash();
1879 if (const auto *FD
= dyn_cast
<FunctionDecl
>(ND
))
1880 if (FD
->isMultiVersion() && !OmitMultiVersionMangling
) {
1881 switch (FD
->getMultiVersionKind()) {
1882 case MultiVersionKind::CPUDispatch
:
1883 case MultiVersionKind::CPUSpecific
:
1884 AppendCPUSpecificCPUDispatchMangling(CGM
,
1885 FD
->getAttr
<CPUSpecificAttr
>(),
1886 GD
.getMultiVersionIndex(), Out
);
1888 case MultiVersionKind::Target
: {
1889 auto *Attr
= FD
->getAttr
<TargetAttr
>();
1890 assert(Attr
&& "Expected TargetAttr to be present "
1891 "for attribute mangling");
1892 const ABIInfo
&Info
= CGM
.getTargetCodeGenInfo().getABIInfo();
1893 Info
.appendAttributeMangling(Attr
, Out
);
1896 case MultiVersionKind::TargetVersion
: {
1897 auto *Attr
= FD
->getAttr
<TargetVersionAttr
>();
1898 assert(Attr
&& "Expected TargetVersionAttr to be present "
1899 "for attribute mangling");
1900 const ABIInfo
&Info
= CGM
.getTargetCodeGenInfo().getABIInfo();
1901 Info
.appendAttributeMangling(Attr
, Out
);
1904 case MultiVersionKind::TargetClones
: {
1905 auto *Attr
= FD
->getAttr
<TargetClonesAttr
>();
1906 assert(Attr
&& "Expected TargetClonesAttr to be present "
1907 "for attribute mangling");
1908 unsigned Index
= GD
.getMultiVersionIndex();
1909 const ABIInfo
&Info
= CGM
.getTargetCodeGenInfo().getABIInfo();
1910 Info
.appendAttributeMangling(Attr
, Index
, Out
);
1913 case MultiVersionKind::None
:
1914 llvm_unreachable("None multiversion type isn't valid here");
1918 // Make unique name for device side static file-scope variable for HIP.
1919 if (CGM
.getContext().shouldExternalize(ND
) &&
1920 CGM
.getLangOpts().GPURelocatableDeviceCode
&&
1921 CGM
.getLangOpts().CUDAIsDevice
)
1922 CGM
.printPostfixForExternalizedDecl(Out
, ND
);
1924 return std::string(Out
.str());
1927 void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD
,
1928 const FunctionDecl
*FD
,
1929 StringRef
&CurName
) {
1930 if (!FD
->isMultiVersion())
1933 // Get the name of what this would be without the 'target' attribute. This
1934 // allows us to lookup the version that was emitted when this wasn't a
1935 // multiversion function.
1936 std::string NonTargetName
=
1937 getMangledNameImpl(*this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
1939 if (lookupRepresentativeDecl(NonTargetName
, OtherGD
)) {
1940 assert(OtherGD
.getCanonicalDecl()
1943 ->isMultiVersion() &&
1944 "Other GD should now be a multiversioned function");
1945 // OtherFD is the version of this function that was mangled BEFORE
1946 // becoming a MultiVersion function. It potentially needs to be updated.
1947 const FunctionDecl
*OtherFD
= OtherGD
.getCanonicalDecl()
1950 ->getMostRecentDecl();
1951 std::string OtherName
= getMangledNameImpl(*this, OtherGD
, OtherFD
);
1952 // This is so that if the initial version was already the 'default'
1953 // version, we don't try to update it.
1954 if (OtherName
!= NonTargetName
) {
1955 // Remove instead of erase, since others may have stored the StringRef
1957 const auto ExistingRecord
= Manglings
.find(NonTargetName
);
1958 if (ExistingRecord
!= std::end(Manglings
))
1959 Manglings
.remove(&(*ExistingRecord
));
1960 auto Result
= Manglings
.insert(std::make_pair(OtherName
, OtherGD
));
1961 StringRef OtherNameRef
= MangledDeclNames
[OtherGD
.getCanonicalDecl()] =
1962 Result
.first
->first();
1963 // If this is the current decl is being created, make sure we update the name.
1964 if (GD
.getCanonicalDecl() == OtherGD
.getCanonicalDecl())
1965 CurName
= OtherNameRef
;
1966 if (llvm::GlobalValue
*Entry
= GetGlobalValue(NonTargetName
))
1967 Entry
->setName(OtherName
);
1972 StringRef
CodeGenModule::getMangledName(GlobalDecl GD
) {
1973 GlobalDecl CanonicalGD
= GD
.getCanonicalDecl();
1975 // Some ABIs don't have constructor variants. Make sure that base and
1976 // complete constructors get mangled the same.
1977 if (const auto *CD
= dyn_cast
<CXXConstructorDecl
>(CanonicalGD
.getDecl())) {
1978 if (!getTarget().getCXXABI().hasConstructorVariants()) {
1979 CXXCtorType OrigCtorType
= GD
.getCtorType();
1980 assert(OrigCtorType
== Ctor_Base
|| OrigCtorType
== Ctor_Complete
);
1981 if (OrigCtorType
== Ctor_Base
)
1982 CanonicalGD
= GlobalDecl(CD
, Ctor_Complete
);
1986 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
1987 // static device variable depends on whether the variable is referenced by
1988 // a host or device host function. Therefore the mangled name cannot be
1990 if (!LangOpts
.CUDAIsDevice
|| !getContext().mayExternalize(GD
.getDecl())) {
1991 auto FoundName
= MangledDeclNames
.find(CanonicalGD
);
1992 if (FoundName
!= MangledDeclNames
.end())
1993 return FoundName
->second
;
1996 // Keep the first result in the case of a mangling collision.
1997 const auto *ND
= cast
<NamedDecl
>(GD
.getDecl());
1998 std::string MangledName
= getMangledNameImpl(*this, GD
, ND
);
2000 // Ensure either we have different ABIs between host and device compilations,
2001 // says host compilation following MSVC ABI but device compilation follows
2002 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
2003 // mangling should be the same after name stubbing. The later checking is
2004 // very important as the device kernel name being mangled in host-compilation
2005 // is used to resolve the device binaries to be executed. Inconsistent naming
2006 // result in undefined behavior. Even though we cannot check that naming
2007 // directly between host- and device-compilations, the host- and
2008 // device-mangling in host compilation could help catching certain ones.
2009 assert(!isa
<FunctionDecl
>(ND
) || !ND
->hasAttr
<CUDAGlobalAttr
>() ||
2010 getContext().shouldExternalize(ND
) || getLangOpts().CUDAIsDevice
||
2011 (getContext().getAuxTargetInfo() &&
2012 (getContext().getAuxTargetInfo()->getCXXABI() !=
2013 getContext().getTargetInfo().getCXXABI())) ||
2014 getCUDARuntime().getDeviceSideName(ND
) ==
2017 GD
.getWithKernelReferenceKind(KernelReferenceKind::Kernel
),
2020 auto Result
= Manglings
.insert(std::make_pair(MangledName
, GD
));
2021 return MangledDeclNames
[CanonicalGD
] = Result
.first
->first();
2024 StringRef
CodeGenModule::getBlockMangledName(GlobalDecl GD
,
2025 const BlockDecl
*BD
) {
2026 MangleContext
&MangleCtx
= getCXXABI().getMangleContext();
2027 const Decl
*D
= GD
.getDecl();
2029 SmallString
<256> Buffer
;
2030 llvm::raw_svector_ostream
Out(Buffer
);
2032 MangleCtx
.mangleGlobalBlock(BD
,
2033 dyn_cast_or_null
<VarDecl
>(initializedGlobalDecl
.getDecl()), Out
);
2034 else if (const auto *CD
= dyn_cast
<CXXConstructorDecl
>(D
))
2035 MangleCtx
.mangleCtorBlock(CD
, GD
.getCtorType(), BD
, Out
);
2036 else if (const auto *DD
= dyn_cast
<CXXDestructorDecl
>(D
))
2037 MangleCtx
.mangleDtorBlock(DD
, GD
.getDtorType(), BD
, Out
);
2039 MangleCtx
.mangleBlock(cast
<DeclContext
>(D
), BD
, Out
);
2041 auto Result
= Manglings
.insert(std::make_pair(Out
.str(), BD
));
2042 return Result
.first
->first();
2045 const GlobalDecl
CodeGenModule::getMangledNameDecl(StringRef Name
) {
2046 auto it
= MangledDeclNames
.begin();
2047 while (it
!= MangledDeclNames
.end()) {
2048 if (it
->second
== Name
)
2052 return GlobalDecl();
2055 llvm::GlobalValue
*CodeGenModule::GetGlobalValue(StringRef Name
) {
2056 return getModule().getNamedValue(Name
);
2059 /// AddGlobalCtor - Add a function to the list that will be called before
2061 void CodeGenModule::AddGlobalCtor(llvm::Function
*Ctor
, int Priority
,
2063 llvm::Constant
*AssociatedData
) {
2064 // FIXME: Type coercion of void()* types.
2065 GlobalCtors
.push_back(Structor(Priority
, LexOrder
, Ctor
, AssociatedData
));
2068 /// AddGlobalDtor - Add a function to the list that will be called
2069 /// when the module is unloaded.
2070 void CodeGenModule::AddGlobalDtor(llvm::Function
*Dtor
, int Priority
,
2071 bool IsDtorAttrFunc
) {
2072 if (CodeGenOpts
.RegisterGlobalDtorsWithAtExit
&&
2073 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc
)) {
2074 DtorsUsingAtExit
[Priority
].push_back(Dtor
);
2078 // FIXME: Type coercion of void()* types.
2079 GlobalDtors
.push_back(Structor(Priority
, ~0U, Dtor
, nullptr));
2082 void CodeGenModule::EmitCtorList(CtorList
&Fns
, const char *GlobalName
) {
2083 if (Fns
.empty()) return;
2085 // Ctor function type is void()*.
2086 llvm::FunctionType
* CtorFTy
= llvm::FunctionType::get(VoidTy
, false);
2087 llvm::Type
*CtorPFTy
= llvm::PointerType::get(CtorFTy
,
2088 TheModule
.getDataLayout().getProgramAddressSpace());
2090 // Get the type of a ctor entry, { i32, void ()*, i8* }.
2091 llvm::StructType
*CtorStructTy
= llvm::StructType::get(
2092 Int32Ty
, CtorPFTy
, VoidPtrTy
);
2094 // Construct the constructor and destructor arrays.
2095 ConstantInitBuilder
builder(*this);
2096 auto ctors
= builder
.beginArray(CtorStructTy
);
2097 for (const auto &I
: Fns
) {
2098 auto ctor
= ctors
.beginStruct(CtorStructTy
);
2099 ctor
.addInt(Int32Ty
, I
.Priority
);
2100 ctor
.add(I
.Initializer
);
2101 if (I
.AssociatedData
)
2102 ctor
.add(I
.AssociatedData
);
2104 ctor
.addNullPointer(VoidPtrTy
);
2105 ctor
.finishAndAddTo(ctors
);
2109 ctors
.finishAndCreateGlobal(GlobalName
, getPointerAlign(),
2111 llvm::GlobalValue::AppendingLinkage
);
2113 // The LTO linker doesn't seem to like it when we set an alignment
2114 // on appending variables. Take it off as a workaround.
2115 list
->setAlignment(std::nullopt
);
2120 llvm::GlobalValue::LinkageTypes
2121 CodeGenModule::getFunctionLinkage(GlobalDecl GD
) {
2122 const auto *D
= cast
<FunctionDecl
>(GD
.getDecl());
2124 GVALinkage Linkage
= getContext().GetGVALinkageForFunction(D
);
2126 if (const auto *Dtor
= dyn_cast
<CXXDestructorDecl
>(D
))
2127 return getCXXABI().getCXXDestructorLinkage(Linkage
, Dtor
, GD
.getDtorType());
2129 return getLLVMLinkageForDeclarator(D
, Linkage
);
2132 llvm::ConstantInt
*CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata
*MD
) {
2133 llvm::MDString
*MDS
= dyn_cast
<llvm::MDString
>(MD
);
2134 if (!MDS
) return nullptr;
2136 return llvm::ConstantInt::get(Int64Ty
, llvm::MD5Hash(MDS
->getString()));
2139 llvm::ConstantInt
*CodeGenModule::CreateKCFITypeId(QualType T
) {
2140 if (auto *FnType
= T
->getAs
<FunctionProtoType
>())
2141 T
= getContext().getFunctionType(
2142 FnType
->getReturnType(), FnType
->getParamTypes(),
2143 FnType
->getExtProtoInfo().withExceptionSpec(EST_None
));
2145 std::string OutName
;
2146 llvm::raw_string_ostream
Out(OutName
);
2147 getCXXABI().getMangleContext().mangleCanonicalTypeName(
2148 T
, Out
, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers
);
2150 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers
)
2151 Out
<< ".normalized";
2153 return llvm::ConstantInt::get(Int32Ty
,
2154 static_cast<uint32_t>(llvm::xxHash64(OutName
)));
2157 void CodeGenModule::SetLLVMFunctionAttributes(GlobalDecl GD
,
2158 const CGFunctionInfo
&Info
,
2159 llvm::Function
*F
, bool IsThunk
) {
2160 unsigned CallingConv
;
2161 llvm::AttributeList PAL
;
2162 ConstructAttributeList(F
->getName(), Info
, GD
, PAL
, CallingConv
,
2163 /*AttrOnCallSite=*/false, IsThunk
);
2164 if (CallingConv
== llvm::CallingConv::X86_VectorCall
&&
2165 getTarget().getTriple().isWindowsArm64EC()) {
2167 if (const Decl
*D
= GD
.getDecl())
2168 Loc
= D
->getLocation();
2170 Error(Loc
, "__vectorcall calling convention is not currently supported");
2172 F
->setAttributes(PAL
);
2173 F
->setCallingConv(static_cast<llvm::CallingConv::ID
>(CallingConv
));
2176 static void removeImageAccessQualifier(std::string
& TyName
) {
2177 std::string
ReadOnlyQual("__read_only");
2178 std::string::size_type ReadOnlyPos
= TyName
.find(ReadOnlyQual
);
2179 if (ReadOnlyPos
!= std::string::npos
)
2180 // "+ 1" for the space after access qualifier.
2181 TyName
.erase(ReadOnlyPos
, ReadOnlyQual
.size() + 1);
2183 std::string
WriteOnlyQual("__write_only");
2184 std::string::size_type WriteOnlyPos
= TyName
.find(WriteOnlyQual
);
2185 if (WriteOnlyPos
!= std::string::npos
)
2186 TyName
.erase(WriteOnlyPos
, WriteOnlyQual
.size() + 1);
2188 std::string
ReadWriteQual("__read_write");
2189 std::string::size_type ReadWritePos
= TyName
.find(ReadWriteQual
);
2190 if (ReadWritePos
!= std::string::npos
)
2191 TyName
.erase(ReadWritePos
, ReadWriteQual
.size() + 1);
2196 // Returns the address space id that should be produced to the
2197 // kernel_arg_addr_space metadata. This is always fixed to the ids
2198 // as specified in the SPIR 2.0 specification in order to differentiate
2199 // for example in clGetKernelArgInfo() implementation between the address
2200 // spaces with targets without unique mapping to the OpenCL address spaces
2201 // (basically all single AS CPUs).
2202 static unsigned ArgInfoAddressSpace(LangAS AS
) {
2204 case LangAS::opencl_global
:
2206 case LangAS::opencl_constant
:
2208 case LangAS::opencl_local
:
2210 case LangAS::opencl_generic
:
2211 return 4; // Not in SPIR 2.0 specs.
2212 case LangAS::opencl_global_device
:
2214 case LangAS::opencl_global_host
:
2217 return 0; // Assume private.
2221 void CodeGenModule::GenKernelArgMetadata(llvm::Function
*Fn
,
2222 const FunctionDecl
*FD
,
2223 CodeGenFunction
*CGF
) {
2224 assert(((FD
&& CGF
) || (!FD
&& !CGF
)) &&
2225 "Incorrect use - FD and CGF should either be both null or not!");
2226 // Create MDNodes that represent the kernel arg metadata.
2227 // Each MDNode is a list in the form of "key", N number of values which is
2228 // the same number of values as their are kernel arguments.
2230 const PrintingPolicy
&Policy
= Context
.getPrintingPolicy();
2232 // MDNode for the kernel argument address space qualifiers.
2233 SmallVector
<llvm::Metadata
*, 8> addressQuals
;
2235 // MDNode for the kernel argument access qualifiers (images only).
2236 SmallVector
<llvm::Metadata
*, 8> accessQuals
;
2238 // MDNode for the kernel argument type names.
2239 SmallVector
<llvm::Metadata
*, 8> argTypeNames
;
2241 // MDNode for the kernel argument base type names.
2242 SmallVector
<llvm::Metadata
*, 8> argBaseTypeNames
;
2244 // MDNode for the kernel argument type qualifiers.
2245 SmallVector
<llvm::Metadata
*, 8> argTypeQuals
;
2247 // MDNode for the kernel argument names.
2248 SmallVector
<llvm::Metadata
*, 8> argNames
;
2251 for (unsigned i
= 0, e
= FD
->getNumParams(); i
!= e
; ++i
) {
2252 const ParmVarDecl
*parm
= FD
->getParamDecl(i
);
2253 // Get argument name.
2254 argNames
.push_back(llvm::MDString::get(VMContext
, parm
->getName()));
2256 if (!getLangOpts().OpenCL
)
2258 QualType ty
= parm
->getType();
2259 std::string typeQuals
;
2261 // Get image and pipe access qualifier:
2262 if (ty
->isImageType() || ty
->isPipeType()) {
2263 const Decl
*PDecl
= parm
;
2264 if (const auto *TD
= ty
->getAs
<TypedefType
>())
2265 PDecl
= TD
->getDecl();
2266 const OpenCLAccessAttr
*A
= PDecl
->getAttr
<OpenCLAccessAttr
>();
2267 if (A
&& A
->isWriteOnly())
2268 accessQuals
.push_back(llvm::MDString::get(VMContext
, "write_only"));
2269 else if (A
&& A
->isReadWrite())
2270 accessQuals
.push_back(llvm::MDString::get(VMContext
, "read_write"));
2272 accessQuals
.push_back(llvm::MDString::get(VMContext
, "read_only"));
2274 accessQuals
.push_back(llvm::MDString::get(VMContext
, "none"));
2276 auto getTypeSpelling
= [&](QualType Ty
) {
2277 auto typeName
= Ty
.getUnqualifiedType().getAsString(Policy
);
2279 if (Ty
.isCanonical()) {
2280 StringRef typeNameRef
= typeName
;
2281 // Turn "unsigned type" to "utype"
2282 if (typeNameRef
.consume_front("unsigned "))
2283 return std::string("u") + typeNameRef
.str();
2284 if (typeNameRef
.consume_front("signed "))
2285 return typeNameRef
.str();
2291 if (ty
->isPointerType()) {
2292 QualType pointeeTy
= ty
->getPointeeType();
2294 // Get address qualifier.
2295 addressQuals
.push_back(
2296 llvm::ConstantAsMetadata::get(CGF
->Builder
.getInt32(
2297 ArgInfoAddressSpace(pointeeTy
.getAddressSpace()))));
2299 // Get argument type name.
2300 std::string typeName
= getTypeSpelling(pointeeTy
) + "*";
2301 std::string baseTypeName
=
2302 getTypeSpelling(pointeeTy
.getCanonicalType()) + "*";
2303 argTypeNames
.push_back(llvm::MDString::get(VMContext
, typeName
));
2304 argBaseTypeNames
.push_back(
2305 llvm::MDString::get(VMContext
, baseTypeName
));
2307 // Get argument type qualifiers:
2308 if (ty
.isRestrictQualified())
2309 typeQuals
= "restrict";
2310 if (pointeeTy
.isConstQualified() ||
2311 (pointeeTy
.getAddressSpace() == LangAS::opencl_constant
))
2312 typeQuals
+= typeQuals
.empty() ? "const" : " const";
2313 if (pointeeTy
.isVolatileQualified())
2314 typeQuals
+= typeQuals
.empty() ? "volatile" : " volatile";
2316 uint32_t AddrSpc
= 0;
2317 bool isPipe
= ty
->isPipeType();
2318 if (ty
->isImageType() || isPipe
)
2319 AddrSpc
= ArgInfoAddressSpace(LangAS::opencl_global
);
2321 addressQuals
.push_back(
2322 llvm::ConstantAsMetadata::get(CGF
->Builder
.getInt32(AddrSpc
)));
2324 // Get argument type name.
2325 ty
= isPipe
? ty
->castAs
<PipeType
>()->getElementType() : ty
;
2326 std::string typeName
= getTypeSpelling(ty
);
2327 std::string baseTypeName
= getTypeSpelling(ty
.getCanonicalType());
2329 // Remove access qualifiers on images
2330 // (as they are inseparable from type in clang implementation,
2331 // but OpenCL spec provides a special query to get access qualifier
2332 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
2333 if (ty
->isImageType()) {
2334 removeImageAccessQualifier(typeName
);
2335 removeImageAccessQualifier(baseTypeName
);
2338 argTypeNames
.push_back(llvm::MDString::get(VMContext
, typeName
));
2339 argBaseTypeNames
.push_back(
2340 llvm::MDString::get(VMContext
, baseTypeName
));
2345 argTypeQuals
.push_back(llvm::MDString::get(VMContext
, typeQuals
));
2348 if (getLangOpts().OpenCL
) {
2349 Fn
->setMetadata("kernel_arg_addr_space",
2350 llvm::MDNode::get(VMContext
, addressQuals
));
2351 Fn
->setMetadata("kernel_arg_access_qual",
2352 llvm::MDNode::get(VMContext
, accessQuals
));
2353 Fn
->setMetadata("kernel_arg_type",
2354 llvm::MDNode::get(VMContext
, argTypeNames
));
2355 Fn
->setMetadata("kernel_arg_base_type",
2356 llvm::MDNode::get(VMContext
, argBaseTypeNames
));
2357 Fn
->setMetadata("kernel_arg_type_qual",
2358 llvm::MDNode::get(VMContext
, argTypeQuals
));
2360 if (getCodeGenOpts().EmitOpenCLArgMetadata
||
2361 getCodeGenOpts().HIPSaveKernelArgName
)
2362 Fn
->setMetadata("kernel_arg_name",
2363 llvm::MDNode::get(VMContext
, argNames
));
2366 /// Determines whether the language options require us to model
2367 /// unwind exceptions. We treat -fexceptions as mandating this
2368 /// except under the fragile ObjC ABI with only ObjC exceptions
2369 /// enabled. This means, for example, that C with -fexceptions
2371 static bool hasUnwindExceptions(const LangOptions
&LangOpts
) {
2372 // If exceptions are completely disabled, obviously this is false.
2373 if (!LangOpts
.Exceptions
) return false;
2375 // If C++ exceptions are enabled, this is true.
2376 if (LangOpts
.CXXExceptions
) return true;
2378 // If ObjC exceptions are enabled, this depends on the ABI.
2379 if (LangOpts
.ObjCExceptions
) {
2380 return LangOpts
.ObjCRuntime
.hasUnwindExceptions();
2386 static bool requiresMemberFunctionPointerTypeMetadata(CodeGenModule
&CGM
,
2387 const CXXMethodDecl
*MD
) {
2388 // Check that the type metadata can ever actually be used by a call.
2389 if (!CGM
.getCodeGenOpts().LTOUnit
||
2390 !CGM
.HasHiddenLTOVisibility(MD
->getParent()))
2393 // Only functions whose address can be taken with a member function pointer
2394 // need this sort of type metadata.
2395 return MD
->isImplicitObjectMemberFunction() && !MD
->isVirtual() &&
2396 !isa
<CXXConstructorDecl
, CXXDestructorDecl
>(MD
);
2399 SmallVector
<const CXXRecordDecl
*, 0>
2400 CodeGenModule::getMostBaseClasses(const CXXRecordDecl
*RD
) {
2401 llvm::SetVector
<const CXXRecordDecl
*> MostBases
;
2403 std::function
<void (const CXXRecordDecl
*)> CollectMostBases
;
2404 CollectMostBases
= [&](const CXXRecordDecl
*RD
) {
2405 if (RD
->getNumBases() == 0)
2406 MostBases
.insert(RD
);
2407 for (const CXXBaseSpecifier
&B
: RD
->bases())
2408 CollectMostBases(B
.getType()->getAsCXXRecordDecl());
2410 CollectMostBases(RD
);
2411 return MostBases
.takeVector();
2414 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl
*D
,
2415 llvm::Function
*F
) {
2416 llvm::AttrBuilder
B(F
->getContext());
2418 if ((!D
|| !D
->hasAttr
<NoUwtableAttr
>()) && CodeGenOpts
.UnwindTables
)
2419 B
.addUWTableAttr(llvm::UWTableKind(CodeGenOpts
.UnwindTables
));
2421 if (CodeGenOpts
.StackClashProtector
)
2422 B
.addAttribute("probe-stack", "inline-asm");
2424 if (CodeGenOpts
.StackProbeSize
&& CodeGenOpts
.StackProbeSize
!= 4096)
2425 B
.addAttribute("stack-probe-size",
2426 std::to_string(CodeGenOpts
.StackProbeSize
));
2428 if (!hasUnwindExceptions(LangOpts
))
2429 B
.addAttribute(llvm::Attribute::NoUnwind
);
2431 if (D
&& D
->hasAttr
<NoStackProtectorAttr
>())
2433 else if (D
&& D
->hasAttr
<StrictGuardStackCheckAttr
>() &&
2434 isStackProtectorOn(LangOpts
, getTriple(), LangOptions::SSPOn
))
2435 B
.addAttribute(llvm::Attribute::StackProtectStrong
);
2436 else if (isStackProtectorOn(LangOpts
, getTriple(), LangOptions::SSPOn
))
2437 B
.addAttribute(llvm::Attribute::StackProtect
);
2438 else if (isStackProtectorOn(LangOpts
, getTriple(), LangOptions::SSPStrong
))
2439 B
.addAttribute(llvm::Attribute::StackProtectStrong
);
2440 else if (isStackProtectorOn(LangOpts
, getTriple(), LangOptions::SSPReq
))
2441 B
.addAttribute(llvm::Attribute::StackProtectReq
);
2444 // If we don't have a declaration to control inlining, the function isn't
2445 // explicitly marked as alwaysinline for semantic reasons, and inlining is
2446 // disabled, mark the function as noinline.
2447 if (!F
->hasFnAttribute(llvm::Attribute::AlwaysInline
) &&
2448 CodeGenOpts
.getInlining() == CodeGenOptions::OnlyAlwaysInlining
)
2449 B
.addAttribute(llvm::Attribute::NoInline
);
2455 // Handle SME attributes that apply to function definitions,
2456 // rather than to function prototypes.
2457 if (D
->hasAttr
<ArmLocallyStreamingAttr
>())
2458 B
.addAttribute("aarch64_pstate_sm_body");
2460 if (auto *Attr
= D
->getAttr
<ArmNewAttr
>()) {
2461 if (Attr
->isNewZA())
2462 B
.addAttribute("aarch64_new_za");
2463 if (Attr
->isNewZT0())
2464 B
.addAttribute("aarch64_new_zt0");
2467 // Track whether we need to add the optnone LLVM attribute,
2468 // starting with the default for this optimization level.
2469 bool ShouldAddOptNone
=
2470 !CodeGenOpts
.DisableO0ImplyOptNone
&& CodeGenOpts
.OptimizationLevel
== 0;
2471 // We can't add optnone in the following cases, it won't pass the verifier.
2472 ShouldAddOptNone
&= !D
->hasAttr
<MinSizeAttr
>();
2473 ShouldAddOptNone
&= !D
->hasAttr
<AlwaysInlineAttr
>();
2475 // Add optnone, but do so only if the function isn't always_inline.
2476 if ((ShouldAddOptNone
|| D
->hasAttr
<OptimizeNoneAttr
>()) &&
2477 !F
->hasFnAttribute(llvm::Attribute::AlwaysInline
)) {
2478 B
.addAttribute(llvm::Attribute::OptimizeNone
);
2480 // OptimizeNone implies noinline; we should not be inlining such functions.
2481 B
.addAttribute(llvm::Attribute::NoInline
);
2483 // We still need to handle naked functions even though optnone subsumes
2484 // much of their semantics.
2485 if (D
->hasAttr
<NakedAttr
>())
2486 B
.addAttribute(llvm::Attribute::Naked
);
2488 // OptimizeNone wins over OptimizeForSize and MinSize.
2489 F
->removeFnAttr(llvm::Attribute::OptimizeForSize
);
2490 F
->removeFnAttr(llvm::Attribute::MinSize
);
2491 } else if (D
->hasAttr
<NakedAttr
>()) {
2492 // Naked implies noinline: we should not be inlining such functions.
2493 B
.addAttribute(llvm::Attribute::Naked
);
2494 B
.addAttribute(llvm::Attribute::NoInline
);
2495 } else if (D
->hasAttr
<NoDuplicateAttr
>()) {
2496 B
.addAttribute(llvm::Attribute::NoDuplicate
);
2497 } else if (D
->hasAttr
<NoInlineAttr
>() && !F
->hasFnAttribute(llvm::Attribute::AlwaysInline
)) {
2498 // Add noinline if the function isn't always_inline.
2499 B
.addAttribute(llvm::Attribute::NoInline
);
2500 } else if (D
->hasAttr
<AlwaysInlineAttr
>() &&
2501 !F
->hasFnAttribute(llvm::Attribute::NoInline
)) {
2502 // (noinline wins over always_inline, and we can't specify both in IR)
2503 B
.addAttribute(llvm::Attribute::AlwaysInline
);
2504 } else if (CodeGenOpts
.getInlining() == CodeGenOptions::OnlyAlwaysInlining
) {
2505 // If we're not inlining, then force everything that isn't always_inline to
2506 // carry an explicit noinline attribute.
2507 if (!F
->hasFnAttribute(llvm::Attribute::AlwaysInline
))
2508 B
.addAttribute(llvm::Attribute::NoInline
);
2510 // Otherwise, propagate the inline hint attribute and potentially use its
2511 // absence to mark things as noinline.
2512 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
2513 // Search function and template pattern redeclarations for inline.
2514 auto CheckForInline
= [](const FunctionDecl
*FD
) {
2515 auto CheckRedeclForInline
= [](const FunctionDecl
*Redecl
) {
2516 return Redecl
->isInlineSpecified();
2518 if (any_of(FD
->redecls(), CheckRedeclForInline
))
2520 const FunctionDecl
*Pattern
= FD
->getTemplateInstantiationPattern();
2523 return any_of(Pattern
->redecls(), CheckRedeclForInline
);
2525 if (CheckForInline(FD
)) {
2526 B
.addAttribute(llvm::Attribute::InlineHint
);
2527 } else if (CodeGenOpts
.getInlining() ==
2528 CodeGenOptions::OnlyHintInlining
&&
2530 !F
->hasFnAttribute(llvm::Attribute::AlwaysInline
)) {
2531 B
.addAttribute(llvm::Attribute::NoInline
);
2536 // Add other optimization related attributes if we are optimizing this
2538 if (!D
->hasAttr
<OptimizeNoneAttr
>()) {
2539 if (D
->hasAttr
<ColdAttr
>()) {
2540 if (!ShouldAddOptNone
)
2541 B
.addAttribute(llvm::Attribute::OptimizeForSize
);
2542 B
.addAttribute(llvm::Attribute::Cold
);
2544 if (D
->hasAttr
<HotAttr
>())
2545 B
.addAttribute(llvm::Attribute::Hot
);
2546 if (D
->hasAttr
<MinSizeAttr
>())
2547 B
.addAttribute(llvm::Attribute::MinSize
);
2552 unsigned alignment
= D
->getMaxAlignment() / Context
.getCharWidth();
2554 F
->setAlignment(llvm::Align(alignment
));
2556 if (!D
->hasAttr
<AlignedAttr
>())
2557 if (LangOpts
.FunctionAlignment
)
2558 F
->setAlignment(llvm::Align(1ull << LangOpts
.FunctionAlignment
));
2560 // Some C++ ABIs require 2-byte alignment for member functions, in order to
2561 // reserve a bit for differentiating between virtual and non-virtual member
2562 // functions. If the current target's C++ ABI requires this and this is a
2563 // member function, set its alignment accordingly.
2564 if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2565 if (isa
<CXXMethodDecl
>(D
) && F
->getPointerAlignment(getDataLayout()) < 2)
2566 F
->setAlignment(std::max(llvm::Align(2), F
->getAlign().valueOrOne()));
2569 // In the cross-dso CFI mode with canonical jump tables, we want !type
2570 // attributes on definitions only.
2571 if (CodeGenOpts
.SanitizeCfiCrossDso
&&
2572 CodeGenOpts
.SanitizeCfiCanonicalJumpTables
) {
2573 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
2574 // Skip available_externally functions. They won't be codegen'ed in the
2575 // current module anyway.
2576 if (getContext().GetGVALinkageForFunction(FD
) != GVA_AvailableExternally
)
2577 CreateFunctionTypeMetadataForIcall(FD
, F
);
2581 // Emit type metadata on member functions for member function pointer checks.
2582 // These are only ever necessary on definitions; we're guaranteed that the
2583 // definition will be present in the LTO unit as a result of LTO visibility.
2584 auto *MD
= dyn_cast
<CXXMethodDecl
>(D
);
2585 if (MD
&& requiresMemberFunctionPointerTypeMetadata(*this, MD
)) {
2586 for (const CXXRecordDecl
*Base
: getMostBaseClasses(MD
->getParent())) {
2587 llvm::Metadata
*Id
=
2588 CreateMetadataIdentifierForType(Context
.getMemberPointerType(
2589 MD
->getType(), Context
.getRecordType(Base
).getTypePtr()));
2590 F
->addTypeMetadata(0, Id
);
2595 void CodeGenModule::SetCommonAttributes(GlobalDecl GD
, llvm::GlobalValue
*GV
) {
2596 const Decl
*D
= GD
.getDecl();
2597 if (isa_and_nonnull
<NamedDecl
>(D
))
2598 setGVProperties(GV
, GD
);
2600 GV
->setVisibility(llvm::GlobalValue::DefaultVisibility
);
2602 if (D
&& D
->hasAttr
<UsedAttr
>())
2603 addUsedOrCompilerUsedGlobal(GV
);
2605 if (const auto *VD
= dyn_cast_if_present
<VarDecl
>(D
);
2607 ((CodeGenOpts
.KeepPersistentStorageVariables
&&
2608 (VD
->getStorageDuration() == SD_Static
||
2609 VD
->getStorageDuration() == SD_Thread
)) ||
2610 (CodeGenOpts
.KeepStaticConsts
&& VD
->getStorageDuration() == SD_Static
&&
2611 VD
->getType().isConstQualified())))
2612 addUsedOrCompilerUsedGlobal(GV
);
2615 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD
,
2616 llvm::AttrBuilder
&Attrs
,
2617 bool SetTargetFeatures
) {
2618 // Add target-cpu and target-features attributes to functions. If
2619 // we have a decl for the function and it has a target attribute then
2620 // parse that and add it to the feature set.
2621 StringRef TargetCPU
= getTarget().getTargetOpts().CPU
;
2622 StringRef TuneCPU
= getTarget().getTargetOpts().TuneCPU
;
2623 std::vector
<std::string
> Features
;
2624 const auto *FD
= dyn_cast_or_null
<FunctionDecl
>(GD
.getDecl());
2625 FD
= FD
? FD
->getMostRecentDecl() : FD
;
2626 const auto *TD
= FD
? FD
->getAttr
<TargetAttr
>() : nullptr;
2627 const auto *TV
= FD
? FD
->getAttr
<TargetVersionAttr
>() : nullptr;
2628 assert((!TD
|| !TV
) && "both target_version and target specified");
2629 const auto *SD
= FD
? FD
->getAttr
<CPUSpecificAttr
>() : nullptr;
2630 const auto *TC
= FD
? FD
->getAttr
<TargetClonesAttr
>() : nullptr;
2631 bool AddedAttr
= false;
2632 if (TD
|| TV
|| SD
|| TC
) {
2633 llvm::StringMap
<bool> FeatureMap
;
2634 getContext().getFunctionFeatureMap(FeatureMap
, GD
);
2636 // Produce the canonical string for this set of features.
2637 for (const llvm::StringMap
<bool>::value_type
&Entry
: FeatureMap
)
2638 Features
.push_back((Entry
.getValue() ? "+" : "-") + Entry
.getKey().str());
2640 // Now add the target-cpu and target-features to the function.
2641 // While we populated the feature map above, we still need to
2642 // get and parse the target attribute so we can get the cpu for
2645 ParsedTargetAttr ParsedAttr
=
2646 Target
.parseTargetAttr(TD
->getFeaturesStr());
2647 if (!ParsedAttr
.CPU
.empty() &&
2648 getTarget().isValidCPUName(ParsedAttr
.CPU
)) {
2649 TargetCPU
= ParsedAttr
.CPU
;
2650 TuneCPU
= ""; // Clear the tune CPU.
2652 if (!ParsedAttr
.Tune
.empty() &&
2653 getTarget().isValidCPUName(ParsedAttr
.Tune
))
2654 TuneCPU
= ParsedAttr
.Tune
;
2658 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2659 // favor this processor.
2660 TuneCPU
= SD
->getCPUName(GD
.getMultiVersionIndex())->getName();
2663 // Otherwise just add the existing target cpu and target features to the
2665 Features
= getTarget().getTargetOpts().Features
;
2668 if (!TargetCPU
.empty()) {
2669 Attrs
.addAttribute("target-cpu", TargetCPU
);
2672 if (!TuneCPU
.empty()) {
2673 Attrs
.addAttribute("tune-cpu", TuneCPU
);
2676 if (!Features
.empty() && SetTargetFeatures
) {
2677 llvm::erase_if(Features
, [&](const std::string
& F
) {
2678 return getTarget().isReadOnlyFeature(F
.substr(1));
2680 llvm::sort(Features
);
2681 Attrs
.addAttribute("target-features", llvm::join(Features
, ","));
2688 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD
,
2689 llvm::GlobalObject
*GO
) {
2690 const Decl
*D
= GD
.getDecl();
2691 SetCommonAttributes(GD
, GO
);
2694 if (auto *GV
= dyn_cast
<llvm::GlobalVariable
>(GO
)) {
2695 if (D
->hasAttr
<RetainAttr
>())
2697 if (auto *SA
= D
->getAttr
<PragmaClangBSSSectionAttr
>())
2698 GV
->addAttribute("bss-section", SA
->getName());
2699 if (auto *SA
= D
->getAttr
<PragmaClangDataSectionAttr
>())
2700 GV
->addAttribute("data-section", SA
->getName());
2701 if (auto *SA
= D
->getAttr
<PragmaClangRodataSectionAttr
>())
2702 GV
->addAttribute("rodata-section", SA
->getName());
2703 if (auto *SA
= D
->getAttr
<PragmaClangRelroSectionAttr
>())
2704 GV
->addAttribute("relro-section", SA
->getName());
2707 if (auto *F
= dyn_cast
<llvm::Function
>(GO
)) {
2708 if (D
->hasAttr
<RetainAttr
>())
2710 if (auto *SA
= D
->getAttr
<PragmaClangTextSectionAttr
>())
2711 if (!D
->getAttr
<SectionAttr
>())
2712 F
->setSection(SA
->getName());
2714 llvm::AttrBuilder
Attrs(F
->getContext());
2715 if (GetCPUAndFeaturesAttributes(GD
, Attrs
)) {
2716 // We know that GetCPUAndFeaturesAttributes will always have the
2717 // newest set, since it has the newest possible FunctionDecl, so the
2718 // new ones should replace the old.
2719 llvm::AttributeMask RemoveAttrs
;
2720 RemoveAttrs
.addAttribute("target-cpu");
2721 RemoveAttrs
.addAttribute("target-features");
2722 RemoveAttrs
.addAttribute("tune-cpu");
2723 F
->removeFnAttrs(RemoveAttrs
);
2724 F
->addFnAttrs(Attrs
);
2728 if (const auto *CSA
= D
->getAttr
<CodeSegAttr
>())
2729 GO
->setSection(CSA
->getName());
2730 else if (const auto *SA
= D
->getAttr
<SectionAttr
>())
2731 GO
->setSection(SA
->getName());
2734 getTargetCodeGenInfo().setTargetAttributes(D
, GO
, *this);
2737 void CodeGenModule::SetInternalFunctionAttributes(GlobalDecl GD
,
2739 const CGFunctionInfo
&FI
) {
2740 const Decl
*D
= GD
.getDecl();
2741 SetLLVMFunctionAttributes(GD
, FI
, F
, /*IsThunk=*/false);
2742 SetLLVMFunctionAttributesForDefinition(D
, F
);
2744 F
->setLinkage(llvm::Function::InternalLinkage
);
2746 setNonAliasAttributes(GD
, F
);
2749 static void setLinkageForGV(llvm::GlobalValue
*GV
, const NamedDecl
*ND
) {
2750 // Set linkage and visibility in case we never see a definition.
2751 LinkageInfo LV
= ND
->getLinkageAndVisibility();
2752 // Don't set internal linkage on declarations.
2753 // "extern_weak" is overloaded in LLVM; we probably should have
2754 // separate linkage types for this.
2755 if (isExternallyVisible(LV
.getLinkage()) &&
2756 (ND
->hasAttr
<WeakAttr
>() || ND
->isWeakImported()))
2757 GV
->setLinkage(llvm::GlobalValue::ExternalWeakLinkage
);
2760 void CodeGenModule::CreateFunctionTypeMetadataForIcall(const FunctionDecl
*FD
,
2761 llvm::Function
*F
) {
2762 // Only if we are checking indirect calls.
2763 if (!LangOpts
.Sanitize
.has(SanitizerKind::CFIICall
))
2766 // Non-static class methods are handled via vtable or member function pointer
2767 // checks elsewhere.
2768 if (isa
<CXXMethodDecl
>(FD
) && !cast
<CXXMethodDecl
>(FD
)->isStatic())
2771 llvm::Metadata
*MD
= CreateMetadataIdentifierForType(FD
->getType());
2772 F
->addTypeMetadata(0, MD
);
2773 F
->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD
->getType()));
2775 // Emit a hash-based bit set entry for cross-DSO calls.
2776 if (CodeGenOpts
.SanitizeCfiCrossDso
)
2777 if (auto CrossDsoTypeId
= CreateCrossDsoCfiTypeId(MD
))
2778 F
->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId
));
2781 void CodeGenModule::setKCFIType(const FunctionDecl
*FD
, llvm::Function
*F
) {
2782 llvm::LLVMContext
&Ctx
= F
->getContext();
2783 llvm::MDBuilder
MDB(Ctx
);
2784 F
->setMetadata(llvm::LLVMContext::MD_kcfi_type
,
2786 Ctx
, MDB
.createConstant(CreateKCFITypeId(FD
->getType()))));
2789 static bool allowKCFIIdentifier(StringRef Name
) {
2790 // KCFI type identifier constants are only necessary for external assembly
2791 // functions, which means it's safe to skip unusual names. Subset of
2792 // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
2793 return llvm::all_of(Name
, [](const char &C
) {
2794 return llvm::isAlnum(C
) || C
== '_' || C
== '.';
2798 void CodeGenModule::finalizeKCFITypes() {
2799 llvm::Module
&M
= getModule();
2800 for (auto &F
: M
.functions()) {
2801 // Remove KCFI type metadata from non-address-taken local functions.
2802 bool AddressTaken
= F
.hasAddressTaken();
2803 if (!AddressTaken
&& F
.hasLocalLinkage())
2804 F
.eraseMetadata(llvm::LLVMContext::MD_kcfi_type
);
2806 // Generate a constant with the expected KCFI type identifier for all
2807 // address-taken function declarations to support annotating indirectly
2808 // called assembly functions.
2809 if (!AddressTaken
|| !F
.isDeclaration())
2812 const llvm::ConstantInt
*Type
;
2813 if (const llvm::MDNode
*MD
= F
.getMetadata(llvm::LLVMContext::MD_kcfi_type
))
2814 Type
= llvm::mdconst::extract
<llvm::ConstantInt
>(MD
->getOperand(0));
2818 StringRef Name
= F
.getName();
2819 if (!allowKCFIIdentifier(Name
))
2822 std::string Asm
= (".weak __kcfi_typeid_" + Name
+ "\n.set __kcfi_typeid_" +
2823 Name
+ ", " + Twine(Type
->getZExtValue()) + "\n")
2825 M
.appendModuleInlineAsm(Asm
);
2829 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD
, llvm::Function
*F
,
2830 bool IsIncompleteFunction
,
2833 if (llvm::Intrinsic::ID IID
= F
->getIntrinsicID()) {
2834 // If this is an intrinsic function, set the function's attributes
2835 // to the intrinsic's attributes.
2836 F
->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID
));
2840 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
2842 if (!IsIncompleteFunction
)
2843 SetLLVMFunctionAttributes(GD
, getTypes().arrangeGlobalDeclaration(GD
), F
,
2846 // Add the Returned attribute for "this", except for iOS 5 and earlier
2847 // where substantial code, including the libstdc++ dylib, was compiled with
2848 // GCC and does not actually return "this".
2849 if (!IsThunk
&& getCXXABI().HasThisReturn(GD
) &&
2850 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
2851 assert(!F
->arg_empty() &&
2852 F
->arg_begin()->getType()
2853 ->canLosslesslyBitCastTo(F
->getReturnType()) &&
2854 "unexpected this return");
2855 F
->addParamAttr(0, llvm::Attribute::Returned
);
2858 // Only a few attributes are set on declarations; these may later be
2859 // overridden by a definition.
2861 setLinkageForGV(F
, FD
);
2862 setGVProperties(F
, FD
);
2864 // Setup target-specific attributes.
2865 if (!IsIncompleteFunction
&& F
->isDeclaration())
2866 getTargetCodeGenInfo().setTargetAttributes(FD
, F
, *this);
2868 if (const auto *CSA
= FD
->getAttr
<CodeSegAttr
>())
2869 F
->setSection(CSA
->getName());
2870 else if (const auto *SA
= FD
->getAttr
<SectionAttr
>())
2871 F
->setSection(SA
->getName());
2873 if (const auto *EA
= FD
->getAttr
<ErrorAttr
>()) {
2875 F
->addFnAttr("dontcall-error", EA
->getUserDiagnostic());
2876 else if (EA
->isWarning())
2877 F
->addFnAttr("dontcall-warn", EA
->getUserDiagnostic());
2880 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2881 if (FD
->isInlineBuiltinDeclaration()) {
2882 const FunctionDecl
*FDBody
;
2883 bool HasBody
= FD
->hasBody(FDBody
);
2885 assert(HasBody
&& "Inline builtin declarations should always have an "
2887 if (shouldEmitFunction(FDBody
))
2888 F
->addFnAttr(llvm::Attribute::NoBuiltin
);
2891 if (FD
->isReplaceableGlobalAllocationFunction()) {
2892 // A replaceable global allocation function does not act like a builtin by
2893 // default, only if it is invoked by a new-expression or delete-expression.
2894 F
->addFnAttr(llvm::Attribute::NoBuiltin
);
2897 if (isa
<CXXConstructorDecl
>(FD
) || isa
<CXXDestructorDecl
>(FD
))
2898 F
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
2899 else if (const auto *MD
= dyn_cast
<CXXMethodDecl
>(FD
))
2900 if (MD
->isVirtual())
2901 F
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
2903 // Don't emit entries for function declarations in the cross-DSO mode. This
2904 // is handled with better precision by the receiving DSO. But if jump tables
2905 // are non-canonical then we need type metadata in order to produce the local
2907 if (!CodeGenOpts
.SanitizeCfiCrossDso
||
2908 !CodeGenOpts
.SanitizeCfiCanonicalJumpTables
)
2909 CreateFunctionTypeMetadataForIcall(FD
, F
);
2911 if (LangOpts
.Sanitize
.has(SanitizerKind::KCFI
))
2914 if (getLangOpts().OpenMP
&& FD
->hasAttr
<OMPDeclareSimdDeclAttr
>())
2915 getOpenMPRuntime().emitDeclareSimdFunction(FD
, F
);
2917 if (CodeGenOpts
.InlineMaxStackSize
!= UINT_MAX
)
2918 F
->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts
.InlineMaxStackSize
));
2920 if (const auto *CB
= FD
->getAttr
<CallbackAttr
>()) {
2921 // Annotate the callback behavior as metadata:
2922 // - The callback callee (as argument number).
2923 // - The callback payloads (as argument numbers).
2924 llvm::LLVMContext
&Ctx
= F
->getContext();
2925 llvm::MDBuilder
MDB(Ctx
);
2927 // The payload indices are all but the first one in the encoding. The first
2928 // identifies the callback callee.
2929 int CalleeIdx
= *CB
->encoding_begin();
2930 ArrayRef
<int> PayloadIndices(CB
->encoding_begin() + 1, CB
->encoding_end());
2931 F
->addMetadata(llvm::LLVMContext::MD_callback
,
2932 *llvm::MDNode::get(Ctx
, {MDB
.createCallbackEncoding(
2933 CalleeIdx
, PayloadIndices
,
2934 /* VarArgsArePassed */ false)}));
2938 void CodeGenModule::addUsedGlobal(llvm::GlobalValue
*GV
) {
2939 assert((isa
<llvm::Function
>(GV
) || !GV
->isDeclaration()) &&
2940 "Only globals with definition can force usage.");
2941 LLVMUsed
.emplace_back(GV
);
2944 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue
*GV
) {
2945 assert(!GV
->isDeclaration() &&
2946 "Only globals with definition can force usage.");
2947 LLVMCompilerUsed
.emplace_back(GV
);
2950 void CodeGenModule::addUsedOrCompilerUsedGlobal(llvm::GlobalValue
*GV
) {
2951 assert((isa
<llvm::Function
>(GV
) || !GV
->isDeclaration()) &&
2952 "Only globals with definition can force usage.");
2953 if (getTriple().isOSBinFormatELF())
2954 LLVMCompilerUsed
.emplace_back(GV
);
2956 LLVMUsed
.emplace_back(GV
);
2959 static void emitUsed(CodeGenModule
&CGM
, StringRef Name
,
2960 std::vector
<llvm::WeakTrackingVH
> &List
) {
2961 // Don't create llvm.used if there is no need.
2965 // Convert List to what ConstantArray needs.
2966 SmallVector
<llvm::Constant
*, 8> UsedArray
;
2967 UsedArray
.resize(List
.size());
2968 for (unsigned i
= 0, e
= List
.size(); i
!= e
; ++i
) {
2970 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2971 cast
<llvm::Constant
>(&*List
[i
]), CGM
.Int8PtrTy
);
2974 if (UsedArray
.empty())
2976 llvm::ArrayType
*ATy
= llvm::ArrayType::get(CGM
.Int8PtrTy
, UsedArray
.size());
2978 auto *GV
= new llvm::GlobalVariable(
2979 CGM
.getModule(), ATy
, false, llvm::GlobalValue::AppendingLinkage
,
2980 llvm::ConstantArray::get(ATy
, UsedArray
), Name
);
2982 GV
->setSection("llvm.metadata");
2985 void CodeGenModule::emitLLVMUsed() {
2986 emitUsed(*this, "llvm.used", LLVMUsed
);
2987 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed
);
2990 void CodeGenModule::AppendLinkerOptions(StringRef Opts
) {
2991 auto *MDOpts
= llvm::MDString::get(getLLVMContext(), Opts
);
2992 LinkerOptionsMetadata
.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts
));
2995 void CodeGenModule::AddDetectMismatch(StringRef Name
, StringRef Value
) {
2996 llvm::SmallString
<32> Opt
;
2997 getTargetCodeGenInfo().getDetectMismatchOption(Name
, Value
, Opt
);
3000 auto *MDOpts
= llvm::MDString::get(getLLVMContext(), Opt
);
3001 LinkerOptionsMetadata
.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts
));
3004 void CodeGenModule::AddDependentLib(StringRef Lib
) {
3005 auto &C
= getLLVMContext();
3006 if (getTarget().getTriple().isOSBinFormatELF()) {
3007 ELFDependentLibraries
.push_back(
3008 llvm::MDNode::get(C
, llvm::MDString::get(C
, Lib
)));
3012 llvm::SmallString
<24> Opt
;
3013 getTargetCodeGenInfo().getDependentLibraryOption(Lib
, Opt
);
3014 auto *MDOpts
= llvm::MDString::get(getLLVMContext(), Opt
);
3015 LinkerOptionsMetadata
.push_back(llvm::MDNode::get(C
, MDOpts
));
3018 /// Add link options implied by the given module, including modules
3019 /// it depends on, using a postorder walk.
3020 static void addLinkOptionsPostorder(CodeGenModule
&CGM
, Module
*Mod
,
3021 SmallVectorImpl
<llvm::MDNode
*> &Metadata
,
3022 llvm::SmallPtrSet
<Module
*, 16> &Visited
) {
3023 // Import this module's parent.
3024 if (Mod
->Parent
&& Visited
.insert(Mod
->Parent
).second
) {
3025 addLinkOptionsPostorder(CGM
, Mod
->Parent
, Metadata
, Visited
);
3028 // Import this module's dependencies.
3029 for (Module
*Import
: llvm::reverse(Mod
->Imports
)) {
3030 if (Visited
.insert(Import
).second
)
3031 addLinkOptionsPostorder(CGM
, Import
, Metadata
, Visited
);
3034 // Add linker options to link against the libraries/frameworks
3035 // described by this module.
3036 llvm::LLVMContext
&Context
= CGM
.getLLVMContext();
3037 bool IsELF
= CGM
.getTarget().getTriple().isOSBinFormatELF();
3039 // For modules that use export_as for linking, use that module
3041 if (Mod
->UseExportAsModuleLinkName
)
3044 for (const Module::LinkLibrary
&LL
: llvm::reverse(Mod
->LinkLibraries
)) {
3045 // Link against a framework. Frameworks are currently Darwin only, so we
3046 // don't to ask TargetCodeGenInfo for the spelling of the linker option.
3047 if (LL
.IsFramework
) {
3048 llvm::Metadata
*Args
[2] = {llvm::MDString::get(Context
, "-framework"),
3049 llvm::MDString::get(Context
, LL
.Library
)};
3051 Metadata
.push_back(llvm::MDNode::get(Context
, Args
));
3055 // Link against a library.
3057 llvm::Metadata
*Args
[2] = {
3058 llvm::MDString::get(Context
, "lib"),
3059 llvm::MDString::get(Context
, LL
.Library
),
3061 Metadata
.push_back(llvm::MDNode::get(Context
, Args
));
3063 llvm::SmallString
<24> Opt
;
3064 CGM
.getTargetCodeGenInfo().getDependentLibraryOption(LL
.Library
, Opt
);
3065 auto *OptString
= llvm::MDString::get(Context
, Opt
);
3066 Metadata
.push_back(llvm::MDNode::get(Context
, OptString
));
3071 void CodeGenModule::EmitModuleInitializers(clang::Module
*Primary
) {
3072 assert(Primary
->isNamedModuleUnit() &&
3073 "We should only emit module initializers for named modules.");
3075 // Emit the initializers in the order that sub-modules appear in the
3076 // source, first Global Module Fragments, if present.
3077 if (auto GMF
= Primary
->getGlobalModuleFragment()) {
3078 for (Decl
*D
: getContext().getModuleInitializers(GMF
)) {
3079 if (isa
<ImportDecl
>(D
))
3081 assert(isa
<VarDecl
>(D
) && "GMF initializer decl is not a var?");
3082 EmitTopLevelDecl(D
);
3085 // Second any associated with the module, itself.
3086 for (Decl
*D
: getContext().getModuleInitializers(Primary
)) {
3087 // Skip import decls, the inits for those are called explicitly.
3088 if (isa
<ImportDecl
>(D
))
3090 EmitTopLevelDecl(D
);
3092 // Third any associated with the Privat eMOdule Fragment, if present.
3093 if (auto PMF
= Primary
->getPrivateModuleFragment()) {
3094 for (Decl
*D
: getContext().getModuleInitializers(PMF
)) {
3095 // Skip import decls, the inits for those are called explicitly.
3096 if (isa
<ImportDecl
>(D
))
3098 assert(isa
<VarDecl
>(D
) && "PMF initializer decl is not a var?");
3099 EmitTopLevelDecl(D
);
3104 void CodeGenModule::EmitModuleLinkOptions() {
3105 // Collect the set of all of the modules we want to visit to emit link
3106 // options, which is essentially the imported modules and all of their
3107 // non-explicit child modules.
3108 llvm::SetVector
<clang::Module
*> LinkModules
;
3109 llvm::SmallPtrSet
<clang::Module
*, 16> Visited
;
3110 SmallVector
<clang::Module
*, 16> Stack
;
3112 // Seed the stack with imported modules.
3113 for (Module
*M
: ImportedModules
) {
3114 // Do not add any link flags when an implementation TU of a module imports
3115 // a header of that same module.
3116 if (M
->getTopLevelModuleName() == getLangOpts().CurrentModule
&&
3117 !getLangOpts().isCompilingModule())
3119 if (Visited
.insert(M
).second
)
3123 // Find all of the modules to import, making a little effort to prune
3124 // non-leaf modules.
3125 while (!Stack
.empty()) {
3126 clang::Module
*Mod
= Stack
.pop_back_val();
3128 bool AnyChildren
= false;
3130 // Visit the submodules of this module.
3131 for (const auto &SM
: Mod
->submodules()) {
3132 // Skip explicit children; they need to be explicitly imported to be
3137 if (Visited
.insert(SM
).second
) {
3138 Stack
.push_back(SM
);
3143 // We didn't find any children, so add this module to the list of
3144 // modules to link against.
3146 LinkModules
.insert(Mod
);
3150 // Add link options for all of the imported modules in reverse topological
3151 // order. We don't do anything to try to order import link flags with respect
3152 // to linker options inserted by things like #pragma comment().
3153 SmallVector
<llvm::MDNode
*, 16> MetadataArgs
;
3155 for (Module
*M
: LinkModules
)
3156 if (Visited
.insert(M
).second
)
3157 addLinkOptionsPostorder(*this, M
, MetadataArgs
, Visited
);
3158 std::reverse(MetadataArgs
.begin(), MetadataArgs
.end());
3159 LinkerOptionsMetadata
.append(MetadataArgs
.begin(), MetadataArgs
.end());
3161 // Add the linker options metadata flag.
3162 auto *NMD
= getModule().getOrInsertNamedMetadata("llvm.linker.options");
3163 for (auto *MD
: LinkerOptionsMetadata
)
3164 NMD
->addOperand(MD
);
3167 void CodeGenModule::EmitDeferred() {
3168 // Emit deferred declare target declarations.
3169 if (getLangOpts().OpenMP
&& !getLangOpts().OpenMPSimd
)
3170 getOpenMPRuntime().emitDeferredTargetDecls();
3172 // Emit code for any potentially referenced deferred decls. Since a
3173 // previously unused static decl may become used during the generation of code
3174 // for a static function, iterate until no changes are made.
3176 if (!DeferredVTables
.empty()) {
3177 EmitDeferredVTables();
3179 // Emitting a vtable doesn't directly cause more vtables to
3180 // become deferred, although it can cause functions to be
3181 // emitted that then need those vtables.
3182 assert(DeferredVTables
.empty());
3185 // Emit CUDA/HIP static device variables referenced by host code only.
3186 // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
3187 // needed for further handling.
3188 if (getLangOpts().CUDA
&& getLangOpts().CUDAIsDevice
)
3189 llvm::append_range(DeferredDeclsToEmit
,
3190 getContext().CUDADeviceVarODRUsedByHost
);
3192 // Stop if we're out of both deferred vtables and deferred declarations.
3193 if (DeferredDeclsToEmit
.empty())
3196 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
3197 // work, it will not interfere with this.
3198 std::vector
<GlobalDecl
> CurDeclsToEmit
;
3199 CurDeclsToEmit
.swap(DeferredDeclsToEmit
);
3201 for (GlobalDecl
&D
: CurDeclsToEmit
) {
3202 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
3203 // to get GlobalValue with exactly the type we need, not something that
3204 // might had been created for another decl with the same mangled name but
3206 llvm::GlobalValue
*GV
= dyn_cast
<llvm::GlobalValue
>(
3207 GetAddrOfGlobal(D
, ForDefinition
));
3209 // In case of different address spaces, we may still get a cast, even with
3210 // IsForDefinition equal to true. Query mangled names table to get
3213 GV
= GetGlobalValue(getMangledName(D
));
3215 // Make sure GetGlobalValue returned non-null.
3218 // Check to see if we've already emitted this. This is necessary
3219 // for a couple of reasons: first, decls can end up in the
3220 // deferred-decls queue multiple times, and second, decls can end
3221 // up with definitions in unusual ways (e.g. by an extern inline
3222 // function acquiring a strong function redefinition). Just
3223 // ignore these cases.
3224 if (!GV
->isDeclaration())
3227 // If this is OpenMP, check if it is legal to emit this global normally.
3228 if (LangOpts
.OpenMP
&& OpenMPRuntime
&& OpenMPRuntime
->emitTargetGlobal(D
))
3231 // Otherwise, emit the definition and move on to the next one.
3232 EmitGlobalDefinition(D
, GV
);
3234 // If we found out that we need to emit more decls, do that recursively.
3235 // This has the advantage that the decls are emitted in a DFS and related
3236 // ones are close together, which is convenient for testing.
3237 if (!DeferredVTables
.empty() || !DeferredDeclsToEmit
.empty()) {
3239 assert(DeferredVTables
.empty() && DeferredDeclsToEmit
.empty());
3244 void CodeGenModule::EmitVTablesOpportunistically() {
3245 // Try to emit external vtables as available_externally if they have emitted
3246 // all inlined virtual functions. It runs after EmitDeferred() and therefore
3247 // is not allowed to create new references to things that need to be emitted
3248 // lazily. Note that it also uses fact that we eagerly emitting RTTI.
3250 assert((OpportunisticVTables
.empty() || shouldOpportunisticallyEmitVTables())
3251 && "Only emit opportunistic vtables with optimizations");
3253 for (const CXXRecordDecl
*RD
: OpportunisticVTables
) {
3254 assert(getVTables().isVTableExternal(RD
) &&
3255 "This queue should only contain external vtables");
3256 if (getCXXABI().canSpeculativelyEmitVTable(RD
))
3257 VTables
.GenerateClassData(RD
);
3259 OpportunisticVTables
.clear();
3262 void CodeGenModule::EmitGlobalAnnotations() {
3263 for (const auto& [MangledName
, VD
] : DeferredAnnotations
) {
3264 llvm::GlobalValue
*GV
= GetGlobalValue(MangledName
);
3266 AddGlobalAnnotations(VD
, GV
);
3268 DeferredAnnotations
.clear();
3270 if (Annotations
.empty())
3273 // Create a new global variable for the ConstantStruct in the Module.
3274 llvm::Constant
*Array
= llvm::ConstantArray::get(llvm::ArrayType::get(
3275 Annotations
[0]->getType(), Annotations
.size()), Annotations
);
3276 auto *gv
= new llvm::GlobalVariable(getModule(), Array
->getType(), false,
3277 llvm::GlobalValue::AppendingLinkage
,
3278 Array
, "llvm.global.annotations");
3279 gv
->setSection(AnnotationSection
);
3282 llvm::Constant
*CodeGenModule::EmitAnnotationString(StringRef Str
) {
3283 llvm::Constant
*&AStr
= AnnotationStrings
[Str
];
3287 // Not found yet, create a new global.
3288 llvm::Constant
*s
= llvm::ConstantDataArray::getString(getLLVMContext(), Str
);
3289 auto *gv
= new llvm::GlobalVariable(
3290 getModule(), s
->getType(), true, llvm::GlobalValue::PrivateLinkage
, s
,
3291 ".str", nullptr, llvm::GlobalValue::NotThreadLocal
,
3292 ConstGlobalsPtrTy
->getAddressSpace());
3293 gv
->setSection(AnnotationSection
);
3294 gv
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
3299 llvm::Constant
*CodeGenModule::EmitAnnotationUnit(SourceLocation Loc
) {
3300 SourceManager
&SM
= getContext().getSourceManager();
3301 PresumedLoc PLoc
= SM
.getPresumedLoc(Loc
);
3303 return EmitAnnotationString(PLoc
.getFilename());
3304 return EmitAnnotationString(SM
.getBufferName(Loc
));
3307 llvm::Constant
*CodeGenModule::EmitAnnotationLineNo(SourceLocation L
) {
3308 SourceManager
&SM
= getContext().getSourceManager();
3309 PresumedLoc PLoc
= SM
.getPresumedLoc(L
);
3310 unsigned LineNo
= PLoc
.isValid() ? PLoc
.getLine() :
3311 SM
.getExpansionLineNumber(L
);
3312 return llvm::ConstantInt::get(Int32Ty
, LineNo
);
3315 llvm::Constant
*CodeGenModule::EmitAnnotationArgs(const AnnotateAttr
*Attr
) {
3316 ArrayRef
<Expr
*> Exprs
= {Attr
->args_begin(), Attr
->args_size()};
3318 return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy
);
3320 llvm::FoldingSetNodeID ID
;
3321 for (Expr
*E
: Exprs
) {
3322 ID
.Add(cast
<clang::ConstantExpr
>(E
)->getAPValueResult());
3324 llvm::Constant
*&Lookup
= AnnotationArgs
[ID
.ComputeHash()];
3328 llvm::SmallVector
<llvm::Constant
*, 4> LLVMArgs
;
3329 LLVMArgs
.reserve(Exprs
.size());
3330 ConstantEmitter
ConstEmiter(*this);
3331 llvm::transform(Exprs
, std::back_inserter(LLVMArgs
), [&](const Expr
*E
) {
3332 const auto *CE
= cast
<clang::ConstantExpr
>(E
);
3333 return ConstEmiter
.emitAbstract(CE
->getBeginLoc(), CE
->getAPValueResult(),
3336 auto *Struct
= llvm::ConstantStruct::getAnon(LLVMArgs
);
3337 auto *GV
= new llvm::GlobalVariable(getModule(), Struct
->getType(), true,
3338 llvm::GlobalValue::PrivateLinkage
, Struct
,
3340 GV
->setSection(AnnotationSection
);
3341 GV
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
3347 llvm::Constant
*CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue
*GV
,
3348 const AnnotateAttr
*AA
,
3350 // Get the globals for file name, annotation, and the line number.
3351 llvm::Constant
*AnnoGV
= EmitAnnotationString(AA
->getAnnotation()),
3352 *UnitGV
= EmitAnnotationUnit(L
),
3353 *LineNoCst
= EmitAnnotationLineNo(L
),
3354 *Args
= EmitAnnotationArgs(AA
);
3356 llvm::Constant
*GVInGlobalsAS
= GV
;
3357 if (GV
->getAddressSpace() !=
3358 getDataLayout().getDefaultGlobalsAddressSpace()) {
3359 GVInGlobalsAS
= llvm::ConstantExpr::getAddrSpaceCast(
3361 llvm::PointerType::get(
3362 GV
->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3365 // Create the ConstantStruct for the global annotation.
3366 llvm::Constant
*Fields
[] = {
3367 GVInGlobalsAS
, AnnoGV
, UnitGV
, LineNoCst
, Args
,
3369 return llvm::ConstantStruct::getAnon(Fields
);
3372 void CodeGenModule::AddGlobalAnnotations(const ValueDecl
*D
,
3373 llvm::GlobalValue
*GV
) {
3374 assert(D
->hasAttr
<AnnotateAttr
>() && "no annotate attribute");
3375 // Get the struct elements for these annotations.
3376 for (const auto *I
: D
->specific_attrs
<AnnotateAttr
>())
3377 Annotations
.push_back(EmitAnnotateAttr(GV
, I
, D
->getLocation()));
3380 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind
, llvm::Function
*Fn
,
3381 SourceLocation Loc
) const {
3382 const auto &NoSanitizeL
= getContext().getNoSanitizeList();
3383 // NoSanitize by function name.
3384 if (NoSanitizeL
.containsFunction(Kind
, Fn
->getName()))
3386 // NoSanitize by location. Check "mainfile" prefix.
3387 auto &SM
= Context
.getSourceManager();
3388 FileEntryRef MainFile
= *SM
.getFileEntryRefForID(SM
.getMainFileID());
3389 if (NoSanitizeL
.containsMainFile(Kind
, MainFile
.getName()))
3392 // Check "src" prefix.
3394 return NoSanitizeL
.containsLocation(Kind
, Loc
);
3395 // If location is unknown, this may be a compiler-generated function. Assume
3396 // it's located in the main file.
3397 return NoSanitizeL
.containsFile(Kind
, MainFile
.getName());
3400 bool CodeGenModule::isInNoSanitizeList(SanitizerMask Kind
,
3401 llvm::GlobalVariable
*GV
,
3402 SourceLocation Loc
, QualType Ty
,
3403 StringRef Category
) const {
3404 const auto &NoSanitizeL
= getContext().getNoSanitizeList();
3405 if (NoSanitizeL
.containsGlobal(Kind
, GV
->getName(), Category
))
3407 auto &SM
= Context
.getSourceManager();
3408 if (NoSanitizeL
.containsMainFile(
3409 Kind
, SM
.getFileEntryRefForID(SM
.getMainFileID())->getName(),
3412 if (NoSanitizeL
.containsLocation(Kind
, Loc
, Category
))
3415 // Check global type.
3417 // Drill down the array types: if global variable of a fixed type is
3418 // not sanitized, we also don't instrument arrays of them.
3419 while (auto AT
= dyn_cast
<ArrayType
>(Ty
.getTypePtr()))
3420 Ty
= AT
->getElementType();
3421 Ty
= Ty
.getCanonicalType().getUnqualifiedType();
3422 // Only record types (classes, structs etc.) are ignored.
3423 if (Ty
->isRecordType()) {
3424 std::string TypeStr
= Ty
.getAsString(getContext().getPrintingPolicy());
3425 if (NoSanitizeL
.containsType(Kind
, TypeStr
, Category
))
3432 bool CodeGenModule::imbueXRayAttrs(llvm::Function
*Fn
, SourceLocation Loc
,
3433 StringRef Category
) const {
3434 const auto &XRayFilter
= getContext().getXRayFilter();
3435 using ImbueAttr
= XRayFunctionFilter::ImbueAttribute
;
3436 auto Attr
= ImbueAttr::NONE
;
3438 Attr
= XRayFilter
.shouldImbueLocation(Loc
, Category
);
3439 if (Attr
== ImbueAttr::NONE
)
3440 Attr
= XRayFilter
.shouldImbueFunction(Fn
->getName());
3442 case ImbueAttr::NONE
:
3444 case ImbueAttr::ALWAYS
:
3445 Fn
->addFnAttr("function-instrument", "xray-always");
3447 case ImbueAttr::ALWAYS_ARG1
:
3448 Fn
->addFnAttr("function-instrument", "xray-always");
3449 Fn
->addFnAttr("xray-log-args", "1");
3451 case ImbueAttr::NEVER
:
3452 Fn
->addFnAttr("function-instrument", "xray-never");
3458 ProfileList::ExclusionType
3459 CodeGenModule::isFunctionBlockedByProfileList(llvm::Function
*Fn
,
3460 SourceLocation Loc
) const {
3461 const auto &ProfileList
= getContext().getProfileList();
3462 // If the profile list is empty, then instrument everything.
3463 if (ProfileList
.isEmpty())
3464 return ProfileList::Allow
;
3465 CodeGenOptions::ProfileInstrKind Kind
= getCodeGenOpts().getProfileInstr();
3466 // First, check the function name.
3467 if (auto V
= ProfileList
.isFunctionExcluded(Fn
->getName(), Kind
))
3469 // Next, check the source location.
3471 if (auto V
= ProfileList
.isLocationExcluded(Loc
, Kind
))
3473 // If location is unknown, this may be a compiler-generated function. Assume
3474 // it's located in the main file.
3475 auto &SM
= Context
.getSourceManager();
3476 if (auto MainFile
= SM
.getFileEntryRefForID(SM
.getMainFileID()))
3477 if (auto V
= ProfileList
.isFileExcluded(MainFile
->getName(), Kind
))
3479 return ProfileList
.getDefault(Kind
);
3482 ProfileList::ExclusionType
3483 CodeGenModule::isFunctionBlockedFromProfileInstr(llvm::Function
*Fn
,
3484 SourceLocation Loc
) const {
3485 auto V
= isFunctionBlockedByProfileList(Fn
, Loc
);
3486 if (V
!= ProfileList::Allow
)
3489 auto NumGroups
= getCodeGenOpts().ProfileTotalFunctionGroups
;
3490 if (NumGroups
> 1) {
3491 auto Group
= llvm::crc32(arrayRefFromStringRef(Fn
->getName())) % NumGroups
;
3492 if (Group
!= getCodeGenOpts().ProfileSelectedFunctionGroup
)
3493 return ProfileList::Skip
;
3495 return ProfileList::Allow
;
3498 bool CodeGenModule::MustBeEmitted(const ValueDecl
*Global
) {
3499 // Never defer when EmitAllDecls is specified.
3500 if (LangOpts
.EmitAllDecls
)
3503 const auto *VD
= dyn_cast
<VarDecl
>(Global
);
3505 ((CodeGenOpts
.KeepPersistentStorageVariables
&&
3506 (VD
->getStorageDuration() == SD_Static
||
3507 VD
->getStorageDuration() == SD_Thread
)) ||
3508 (CodeGenOpts
.KeepStaticConsts
&& VD
->getStorageDuration() == SD_Static
&&
3509 VD
->getType().isConstQualified())))
3512 return getContext().DeclMustBeEmitted(Global
);
3515 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl
*Global
) {
3516 // In OpenMP 5.0 variables and function may be marked as
3517 // device_type(host/nohost) and we should not emit them eagerly unless we sure
3518 // that they must be emitted on the host/device. To be sure we need to have
3519 // seen a declare target with an explicit mentioning of the function, we know
3520 // we have if the level of the declare target attribute is -1. Note that we
3521 // check somewhere else if we should emit this at all.
3522 if (LangOpts
.OpenMP
>= 50 && !LangOpts
.OpenMPSimd
) {
3523 std::optional
<OMPDeclareTargetDeclAttr
*> ActiveAttr
=
3524 OMPDeclareTargetDeclAttr::getActiveAttr(Global
);
3525 if (!ActiveAttr
|| (*ActiveAttr
)->getLevel() != (unsigned)-1)
3529 if (const auto *FD
= dyn_cast
<FunctionDecl
>(Global
)) {
3530 if (FD
->getTemplateSpecializationKind() == TSK_ImplicitInstantiation
)
3531 // Implicit template instantiations may change linkage if they are later
3532 // explicitly instantiated, so they should not be emitted eagerly.
3534 // Defer until all versions have been semantically checked.
3535 if (FD
->hasAttr
<TargetVersionAttr
>() && !FD
->isMultiVersion())
3538 if (const auto *VD
= dyn_cast
<VarDecl
>(Global
)) {
3539 if (Context
.getInlineVariableDefinitionKind(VD
) ==
3540 ASTContext::InlineVariableDefinitionKind::WeakUnknown
)
3541 // A definition of an inline constexpr static data member may change
3542 // linkage later if it's redeclared outside the class.
3544 if (CXX20ModuleInits
&& VD
->getOwningModule() &&
3545 !VD
->getOwningModule()->isModuleMapModule()) {
3546 // For CXX20, module-owned initializers need to be deferred, since it is
3547 // not known at this point if they will be run for the current module or
3548 // as part of the initializer for an imported one.
3552 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
3553 // codegen for global variables, because they may be marked as threadprivate.
3554 if (LangOpts
.OpenMP
&& LangOpts
.OpenMPUseTLS
&&
3555 getContext().getTargetInfo().isTLSSupported() && isa
<VarDecl
>(Global
) &&
3556 !Global
->getType().isConstantStorage(getContext(), false, false) &&
3557 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global
))
3563 ConstantAddress
CodeGenModule::GetAddrOfMSGuidDecl(const MSGuidDecl
*GD
) {
3564 StringRef Name
= getMangledName(GD
);
3566 // The UUID descriptor should be pointer aligned.
3567 CharUnits Alignment
= CharUnits::fromQuantity(PointerAlignInBytes
);
3569 // Look for an existing global.
3570 if (llvm::GlobalVariable
*GV
= getModule().getNamedGlobal(Name
))
3571 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
3573 ConstantEmitter
Emitter(*this);
3574 llvm::Constant
*Init
;
3576 APValue
&V
= GD
->getAsAPValue();
3577 if (!V
.isAbsent()) {
3578 // If possible, emit the APValue version of the initializer. In particular,
3579 // this gets the type of the constant right.
3580 Init
= Emitter
.emitForInitializer(
3581 GD
->getAsAPValue(), GD
->getType().getAddressSpace(), GD
->getType());
3583 // As a fallback, directly construct the constant.
3584 // FIXME: This may get padding wrong under esoteric struct layout rules.
3585 // MSVC appears to create a complete type 'struct __s_GUID' that it
3586 // presumably uses to represent these constants.
3587 MSGuidDecl::Parts Parts
= GD
->getParts();
3588 llvm::Constant
*Fields
[4] = {
3589 llvm::ConstantInt::get(Int32Ty
, Parts
.Part1
),
3590 llvm::ConstantInt::get(Int16Ty
, Parts
.Part2
),
3591 llvm::ConstantInt::get(Int16Ty
, Parts
.Part3
),
3592 llvm::ConstantDataArray::getRaw(
3593 StringRef(reinterpret_cast<char *>(Parts
.Part4And5
), 8), 8,
3595 Init
= llvm::ConstantStruct::getAnon(Fields
);
3598 auto *GV
= new llvm::GlobalVariable(
3599 getModule(), Init
->getType(),
3600 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage
, Init
, Name
);
3601 if (supportsCOMDAT())
3602 GV
->setComdat(TheModule
.getOrInsertComdat(GV
->getName()));
3605 if (!V
.isAbsent()) {
3606 Emitter
.finalize(GV
);
3607 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
3610 llvm::Type
*Ty
= getTypes().ConvertTypeForMem(GD
->getType());
3611 return ConstantAddress(GV
, Ty
, Alignment
);
3614 ConstantAddress
CodeGenModule::GetAddrOfUnnamedGlobalConstantDecl(
3615 const UnnamedGlobalConstantDecl
*GCD
) {
3616 CharUnits Alignment
= getContext().getTypeAlignInChars(GCD
->getType());
3618 llvm::GlobalVariable
**Entry
= nullptr;
3619 Entry
= &UnnamedGlobalConstantDeclMap
[GCD
];
3621 return ConstantAddress(*Entry
, (*Entry
)->getValueType(), Alignment
);
3623 ConstantEmitter
Emitter(*this);
3624 llvm::Constant
*Init
;
3626 const APValue
&V
= GCD
->getValue();
3628 assert(!V
.isAbsent());
3629 Init
= Emitter
.emitForInitializer(V
, GCD
->getType().getAddressSpace(),
3632 auto *GV
= new llvm::GlobalVariable(getModule(), Init
->getType(),
3633 /*isConstant=*/true,
3634 llvm::GlobalValue::PrivateLinkage
, Init
,
3636 GV
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
3637 GV
->setAlignment(Alignment
.getAsAlign());
3639 Emitter
.finalize(GV
);
3642 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
3645 ConstantAddress
CodeGenModule::GetAddrOfTemplateParamObject(
3646 const TemplateParamObjectDecl
*TPO
) {
3647 StringRef Name
= getMangledName(TPO
);
3648 CharUnits Alignment
= getNaturalTypeAlignment(TPO
->getType());
3650 if (llvm::GlobalVariable
*GV
= getModule().getNamedGlobal(Name
))
3651 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
3653 ConstantEmitter
Emitter(*this);
3654 llvm::Constant
*Init
= Emitter
.emitForInitializer(
3655 TPO
->getValue(), TPO
->getType().getAddressSpace(), TPO
->getType());
3658 ErrorUnsupported(TPO
, "template parameter object");
3659 return ConstantAddress::invalid();
3662 llvm::GlobalValue::LinkageTypes Linkage
=
3663 isExternallyVisible(TPO
->getLinkageAndVisibility().getLinkage())
3664 ? llvm::GlobalValue::LinkOnceODRLinkage
3665 : llvm::GlobalValue::InternalLinkage
;
3666 auto *GV
= new llvm::GlobalVariable(getModule(), Init
->getType(),
3667 /*isConstant=*/true, Linkage
, Init
, Name
);
3668 setGVProperties(GV
, TPO
);
3669 if (supportsCOMDAT())
3670 GV
->setComdat(TheModule
.getOrInsertComdat(GV
->getName()));
3671 Emitter
.finalize(GV
);
3673 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
3676 ConstantAddress
CodeGenModule::GetWeakRefReference(const ValueDecl
*VD
) {
3677 const AliasAttr
*AA
= VD
->getAttr
<AliasAttr
>();
3678 assert(AA
&& "No alias?");
3680 CharUnits Alignment
= getContext().getDeclAlign(VD
);
3681 llvm::Type
*DeclTy
= getTypes().ConvertTypeForMem(VD
->getType());
3683 // See if there is already something with the target's name in the module.
3684 llvm::GlobalValue
*Entry
= GetGlobalValue(AA
->getAliasee());
3686 return ConstantAddress(Entry
, DeclTy
, Alignment
);
3688 llvm::Constant
*Aliasee
;
3689 if (isa
<llvm::FunctionType
>(DeclTy
))
3690 Aliasee
= GetOrCreateLLVMFunction(AA
->getAliasee(), DeclTy
,
3691 GlobalDecl(cast
<FunctionDecl
>(VD
)),
3692 /*ForVTable=*/false);
3694 Aliasee
= GetOrCreateLLVMGlobal(AA
->getAliasee(), DeclTy
, LangAS::Default
,
3697 auto *F
= cast
<llvm::GlobalValue
>(Aliasee
);
3698 F
->setLinkage(llvm::Function::ExternalWeakLinkage
);
3699 WeakRefReferences
.insert(F
);
3701 return ConstantAddress(Aliasee
, DeclTy
, Alignment
);
3704 template <typename AttrT
> static bool hasImplicitAttr(const ValueDecl
*D
) {
3707 if (auto *A
= D
->getAttr
<AttrT
>())
3708 return A
->isImplicit();
3709 return D
->isImplicit();
3712 bool CodeGenModule::shouldEmitCUDAGlobalVar(const VarDecl
*Global
) const {
3713 assert(LangOpts
.CUDA
&& "Should not be called by non-CUDA languages");
3714 // We need to emit host-side 'shadows' for all global
3715 // device-side variables because the CUDA runtime needs their
3716 // size and host-side address in order to provide access to
3717 // their device-side incarnations.
3718 return !LangOpts
.CUDAIsDevice
|| Global
->hasAttr
<CUDADeviceAttr
>() ||
3719 Global
->hasAttr
<CUDAConstantAttr
>() ||
3720 Global
->hasAttr
<CUDASharedAttr
>() ||
3721 Global
->getType()->isCUDADeviceBuiltinSurfaceType() ||
3722 Global
->getType()->isCUDADeviceBuiltinTextureType();
3725 void CodeGenModule::EmitGlobal(GlobalDecl GD
) {
3726 const auto *Global
= cast
<ValueDecl
>(GD
.getDecl());
3728 // Weak references don't produce any output by themselves.
3729 if (Global
->hasAttr
<WeakRefAttr
>())
3732 // If this is an alias definition (which otherwise looks like a declaration)
3734 if (Global
->hasAttr
<AliasAttr
>())
3735 return EmitAliasDefinition(GD
);
3737 // IFunc like an alias whose value is resolved at runtime by calling resolver.
3738 if (Global
->hasAttr
<IFuncAttr
>())
3739 return emitIFuncDefinition(GD
);
3741 // If this is a cpu_dispatch multiversion function, emit the resolver.
3742 if (Global
->hasAttr
<CPUDispatchAttr
>())
3743 return emitCPUDispatchDefinition(GD
);
3745 // If this is CUDA, be selective about which declarations we emit.
3746 // Non-constexpr non-lambda implicit host device functions are not emitted
3747 // unless they are used on device side.
3748 if (LangOpts
.CUDA
) {
3749 assert((isa
<FunctionDecl
>(Global
) || isa
<VarDecl
>(Global
)) &&
3750 "Expected Variable or Function");
3751 if (const auto *VD
= dyn_cast
<VarDecl
>(Global
)) {
3752 if (!shouldEmitCUDAGlobalVar(VD
))
3754 } else if (LangOpts
.CUDAIsDevice
) {
3755 const auto *FD
= dyn_cast
<FunctionDecl
>(Global
);
3756 if ((!Global
->hasAttr
<CUDADeviceAttr
>() ||
3757 (LangOpts
.OffloadImplicitHostDeviceTemplates
&&
3758 hasImplicitAttr
<CUDAHostAttr
>(FD
) &&
3759 hasImplicitAttr
<CUDADeviceAttr
>(FD
) && !FD
->isConstexpr() &&
3760 !isLambdaCallOperator(FD
) &&
3761 !getContext().CUDAImplicitHostDeviceFunUsedByDevice
.count(FD
))) &&
3762 !Global
->hasAttr
<CUDAGlobalAttr
>() &&
3763 !(LangOpts
.HIPStdPar
&& isa
<FunctionDecl
>(Global
) &&
3764 !Global
->hasAttr
<CUDAHostAttr
>()))
3766 // Device-only functions are the only things we skip.
3767 } else if (!Global
->hasAttr
<CUDAHostAttr
>() &&
3768 Global
->hasAttr
<CUDADeviceAttr
>())
3772 if (LangOpts
.OpenMP
) {
3773 // If this is OpenMP, check if it is legal to emit this global normally.
3774 if (OpenMPRuntime
&& OpenMPRuntime
->emitTargetGlobal(GD
))
3776 if (auto *DRD
= dyn_cast
<OMPDeclareReductionDecl
>(Global
)) {
3777 if (MustBeEmitted(Global
))
3778 EmitOMPDeclareReduction(DRD
);
3781 if (auto *DMD
= dyn_cast
<OMPDeclareMapperDecl
>(Global
)) {
3782 if (MustBeEmitted(Global
))
3783 EmitOMPDeclareMapper(DMD
);
3788 // Ignore declarations, they will be emitted on their first use.
3789 if (const auto *FD
= dyn_cast
<FunctionDecl
>(Global
)) {
3790 // Update deferred annotations with the latest declaration if the function
3791 // function was already used or defined.
3792 if (FD
->hasAttr
<AnnotateAttr
>()) {
3793 StringRef MangledName
= getMangledName(GD
);
3794 if (GetGlobalValue(MangledName
))
3795 DeferredAnnotations
[MangledName
] = FD
;
3798 // Forward declarations are emitted lazily on first use.
3799 if (!FD
->doesThisDeclarationHaveABody()) {
3800 if (!FD
->doesDeclarationForceExternallyVisibleDefinition() &&
3801 (!FD
->isMultiVersion() || !getTarget().getTriple().isAArch64()))
3804 StringRef MangledName
= getMangledName(GD
);
3806 // Compute the function info and LLVM type.
3807 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
3808 llvm::Type
*Ty
= getTypes().GetFunctionType(FI
);
3810 GetOrCreateLLVMFunction(MangledName
, Ty
, GD
, /*ForVTable=*/false,
3811 /*DontDefer=*/false);
3815 const auto *VD
= cast
<VarDecl
>(Global
);
3816 assert(VD
->isFileVarDecl() && "Cannot emit local var decl as global.");
3817 if (VD
->isThisDeclarationADefinition() != VarDecl::Definition
&&
3818 !Context
.isMSStaticDataMemberInlineDefinition(VD
)) {
3819 if (LangOpts
.OpenMP
) {
3820 // Emit declaration of the must-be-emitted declare target variable.
3821 if (std::optional
<OMPDeclareTargetDeclAttr::MapTypeTy
> Res
=
3822 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD
)) {
3824 // If this variable has external storage and doesn't require special
3825 // link handling we defer to its canonical definition.
3826 if (VD
->hasExternalStorage() &&
3827 Res
!= OMPDeclareTargetDeclAttr::MT_Link
)
3830 bool UnifiedMemoryEnabled
=
3831 getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
3832 if ((*Res
== OMPDeclareTargetDeclAttr::MT_To
||
3833 *Res
== OMPDeclareTargetDeclAttr::MT_Enter
) &&
3834 !UnifiedMemoryEnabled
) {
3835 (void)GetAddrOfGlobalVar(VD
);
3837 assert(((*Res
== OMPDeclareTargetDeclAttr::MT_Link
) ||
3838 ((*Res
== OMPDeclareTargetDeclAttr::MT_To
||
3839 *Res
== OMPDeclareTargetDeclAttr::MT_Enter
) &&
3840 UnifiedMemoryEnabled
)) &&
3841 "Link clause or to clause with unified memory expected.");
3842 (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD
);
3848 // If this declaration may have caused an inline variable definition to
3849 // change linkage, make sure that it's emitted.
3850 if (Context
.getInlineVariableDefinitionKind(VD
) ==
3851 ASTContext::InlineVariableDefinitionKind::Strong
)
3852 GetAddrOfGlobalVar(VD
);
3857 // Defer code generation to first use when possible, e.g. if this is an inline
3858 // function. If the global must always be emitted, do it eagerly if possible
3859 // to benefit from cache locality.
3860 if (MustBeEmitted(Global
) && MayBeEmittedEagerly(Global
)) {
3861 // Emit the definition if it can't be deferred.
3862 EmitGlobalDefinition(GD
);
3863 addEmittedDeferredDecl(GD
);
3867 // If we're deferring emission of a C++ variable with an
3868 // initializer, remember the order in which it appeared in the file.
3869 if (getLangOpts().CPlusPlus
&& isa
<VarDecl
>(Global
) &&
3870 cast
<VarDecl
>(Global
)->hasInit()) {
3871 DelayedCXXInitPosition
[Global
] = CXXGlobalInits
.size();
3872 CXXGlobalInits
.push_back(nullptr);
3875 StringRef MangledName
= getMangledName(GD
);
3876 if (GetGlobalValue(MangledName
) != nullptr) {
3877 // The value has already been used and should therefore be emitted.
3878 addDeferredDeclToEmit(GD
);
3879 } else if (MustBeEmitted(Global
)) {
3880 // The value must be emitted, but cannot be emitted eagerly.
3881 assert(!MayBeEmittedEagerly(Global
));
3882 addDeferredDeclToEmit(GD
);
3884 // Otherwise, remember that we saw a deferred decl with this name. The
3885 // first use of the mangled name will cause it to move into
3886 // DeferredDeclsToEmit.
3887 DeferredDecls
[MangledName
] = GD
;
3891 // Check if T is a class type with a destructor that's not dllimport.
3892 static bool HasNonDllImportDtor(QualType T
) {
3893 if (const auto *RT
= T
->getBaseElementTypeUnsafe()->getAs
<RecordType
>())
3894 if (CXXRecordDecl
*RD
= dyn_cast
<CXXRecordDecl
>(RT
->getDecl()))
3895 if (RD
->getDestructor() && !RD
->getDestructor()->hasAttr
<DLLImportAttr
>())
3902 struct FunctionIsDirectlyRecursive
3903 : public ConstStmtVisitor
<FunctionIsDirectlyRecursive
, bool> {
3904 const StringRef Name
;
3905 const Builtin::Context
&BI
;
3906 FunctionIsDirectlyRecursive(StringRef N
, const Builtin::Context
&C
)
3909 bool VisitCallExpr(const CallExpr
*E
) {
3910 const FunctionDecl
*FD
= E
->getDirectCallee();
3913 AsmLabelAttr
*Attr
= FD
->getAttr
<AsmLabelAttr
>();
3914 if (Attr
&& Name
== Attr
->getLabel())
3916 unsigned BuiltinID
= FD
->getBuiltinID();
3917 if (!BuiltinID
|| !BI
.isLibFunction(BuiltinID
))
3919 StringRef BuiltinName
= BI
.getName(BuiltinID
);
3920 if (BuiltinName
.starts_with("__builtin_") &&
3921 Name
== BuiltinName
.slice(strlen("__builtin_"), StringRef::npos
)) {
3927 bool VisitStmt(const Stmt
*S
) {
3928 for (const Stmt
*Child
: S
->children())
3929 if (Child
&& this->Visit(Child
))
3935 // Make sure we're not referencing non-imported vars or functions.
3936 struct DLLImportFunctionVisitor
3937 : public RecursiveASTVisitor
<DLLImportFunctionVisitor
> {
3938 bool SafeToInline
= true;
3940 bool shouldVisitImplicitCode() const { return true; }
3942 bool VisitVarDecl(VarDecl
*VD
) {
3943 if (VD
->getTLSKind()) {
3944 // A thread-local variable cannot be imported.
3945 SafeToInline
= false;
3946 return SafeToInline
;
3949 // A variable definition might imply a destructor call.
3950 if (VD
->isThisDeclarationADefinition())
3951 SafeToInline
= !HasNonDllImportDtor(VD
->getType());
3953 return SafeToInline
;
3956 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr
*E
) {
3957 if (const auto *D
= E
->getTemporary()->getDestructor())
3958 SafeToInline
= D
->hasAttr
<DLLImportAttr
>();
3959 return SafeToInline
;
3962 bool VisitDeclRefExpr(DeclRefExpr
*E
) {
3963 ValueDecl
*VD
= E
->getDecl();
3964 if (isa
<FunctionDecl
>(VD
))
3965 SafeToInline
= VD
->hasAttr
<DLLImportAttr
>();
3966 else if (VarDecl
*V
= dyn_cast
<VarDecl
>(VD
))
3967 SafeToInline
= !V
->hasGlobalStorage() || V
->hasAttr
<DLLImportAttr
>();
3968 return SafeToInline
;
3971 bool VisitCXXConstructExpr(CXXConstructExpr
*E
) {
3972 SafeToInline
= E
->getConstructor()->hasAttr
<DLLImportAttr
>();
3973 return SafeToInline
;
3976 bool VisitCXXMemberCallExpr(CXXMemberCallExpr
*E
) {
3977 CXXMethodDecl
*M
= E
->getMethodDecl();
3979 // Call through a pointer to member function. This is safe to inline.
3980 SafeToInline
= true;
3982 SafeToInline
= M
->hasAttr
<DLLImportAttr
>();
3984 return SafeToInline
;
3987 bool VisitCXXDeleteExpr(CXXDeleteExpr
*E
) {
3988 SafeToInline
= E
->getOperatorDelete()->hasAttr
<DLLImportAttr
>();
3989 return SafeToInline
;
3992 bool VisitCXXNewExpr(CXXNewExpr
*E
) {
3993 SafeToInline
= E
->getOperatorNew()->hasAttr
<DLLImportAttr
>();
3994 return SafeToInline
;
3999 // isTriviallyRecursive - Check if this function calls another
4000 // decl that, because of the asm attribute or the other decl being a builtin,
4001 // ends up pointing to itself.
4003 CodeGenModule::isTriviallyRecursive(const FunctionDecl
*FD
) {
4005 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD
)) {
4006 // asm labels are a special kind of mangling we have to support.
4007 AsmLabelAttr
*Attr
= FD
->getAttr
<AsmLabelAttr
>();
4010 Name
= Attr
->getLabel();
4012 Name
= FD
->getName();
4015 FunctionIsDirectlyRecursive
Walker(Name
, Context
.BuiltinInfo
);
4016 const Stmt
*Body
= FD
->getBody();
4017 return Body
? Walker
.Visit(Body
) : false;
4020 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD
) {
4021 if (getFunctionLinkage(GD
) != llvm::Function::AvailableExternallyLinkage
)
4024 const auto *F
= cast
<FunctionDecl
>(GD
.getDecl());
4025 if (CodeGenOpts
.OptimizationLevel
== 0 && !F
->hasAttr
<AlwaysInlineAttr
>())
4028 // We don't import function bodies from other named module units since that
4029 // behavior may break ABI compatibility of the current unit.
4030 if (const Module
*M
= F
->getOwningModule();
4031 M
&& M
->getTopLevelModule()->isNamedModule() &&
4032 getContext().getCurrentNamedModule() != M
->getTopLevelModule()) {
4033 // There are practices to mark template member function as always-inline
4034 // and mark the template as extern explicit instantiation but not give
4035 // the definition for member function. So we have to emit the function
4036 // from explicitly instantiation with always-inline.
4038 // See https://github.com/llvm/llvm-project/issues/86893 for details.
4040 // TODO: Maybe it is better to give it a warning if we call a non-inline
4041 // function from other module units which is marked as always-inline.
4042 if (!F
->isTemplateInstantiation() || !F
->hasAttr
<AlwaysInlineAttr
>()) {
4047 if (F
->hasAttr
<NoInlineAttr
>())
4050 if (F
->hasAttr
<DLLImportAttr
>() && !F
->hasAttr
<AlwaysInlineAttr
>()) {
4051 // Check whether it would be safe to inline this dllimport function.
4052 DLLImportFunctionVisitor Visitor
;
4053 Visitor
.TraverseFunctionDecl(const_cast<FunctionDecl
*>(F
));
4054 if (!Visitor
.SafeToInline
)
4057 if (const CXXDestructorDecl
*Dtor
= dyn_cast
<CXXDestructorDecl
>(F
)) {
4058 // Implicit destructor invocations aren't captured in the AST, so the
4059 // check above can't see them. Check for them manually here.
4060 for (const Decl
*Member
: Dtor
->getParent()->decls())
4061 if (isa
<FieldDecl
>(Member
))
4062 if (HasNonDllImportDtor(cast
<FieldDecl
>(Member
)->getType()))
4064 for (const CXXBaseSpecifier
&B
: Dtor
->getParent()->bases())
4065 if (HasNonDllImportDtor(B
.getType()))
4070 // Inline builtins declaration must be emitted. They often are fortified
4072 if (F
->isInlineBuiltinDeclaration())
4075 // PR9614. Avoid cases where the source code is lying to us. An available
4076 // externally function should have an equivalent function somewhere else,
4077 // but a function that calls itself through asm label/`__builtin_` trickery is
4078 // clearly not equivalent to the real implementation.
4079 // This happens in glibc's btowc and in some configure checks.
4080 return !isTriviallyRecursive(F
);
4083 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
4084 return CodeGenOpts
.OptimizationLevel
> 0;
4087 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD
,
4088 llvm::GlobalValue
*GV
) {
4089 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4091 if (FD
->isCPUSpecificMultiVersion()) {
4092 auto *Spec
= FD
->getAttr
<CPUSpecificAttr
>();
4093 for (unsigned I
= 0; I
< Spec
->cpus_size(); ++I
)
4094 EmitGlobalFunctionDefinition(GD
.getWithMultiVersionIndex(I
), nullptr);
4095 } else if (auto *TC
= FD
->getAttr
<TargetClonesAttr
>()) {
4096 for (unsigned I
= 0; I
< TC
->featuresStrs_size(); ++I
)
4097 // AArch64 favors the default target version over the clone if any.
4098 if ((!TC
->isDefaultVersion(I
) || !getTarget().getTriple().isAArch64()) &&
4099 TC
->isFirstOfVersion(I
))
4100 EmitGlobalFunctionDefinition(GD
.getWithMultiVersionIndex(I
), nullptr);
4101 // Ensure that the resolver function is also emitted.
4102 GetOrCreateMultiVersionResolver(GD
);
4104 EmitGlobalFunctionDefinition(GD
, GV
);
4106 // Defer the resolver emission until we can reason whether the TU
4107 // contains a default target version implementation.
4108 if (FD
->isTargetVersionMultiVersion())
4109 AddDeferredMultiVersionResolverToEmit(GD
);
4112 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD
, llvm::GlobalValue
*GV
) {
4113 const auto *D
= cast
<ValueDecl
>(GD
.getDecl());
4115 PrettyStackTraceDecl
CrashInfo(const_cast<ValueDecl
*>(D
), D
->getLocation(),
4116 Context
.getSourceManager(),
4117 "Generating code for declaration");
4119 if (const auto *FD
= dyn_cast
<FunctionDecl
>(D
)) {
4120 // At -O0, don't generate IR for functions with available_externally
4122 if (!shouldEmitFunction(GD
))
4125 llvm::TimeTraceScope
TimeScope("CodeGen Function", [&]() {
4127 llvm::raw_string_ostream
OS(Name
);
4128 FD
->getNameForDiagnostic(OS
, getContext().getPrintingPolicy(),
4129 /*Qualified=*/true);
4133 if (const auto *Method
= dyn_cast
<CXXMethodDecl
>(D
)) {
4134 // Make sure to emit the definition(s) before we emit the thunks.
4135 // This is necessary for the generation of certain thunks.
4136 if (isa
<CXXConstructorDecl
>(Method
) || isa
<CXXDestructorDecl
>(Method
))
4137 ABI
->emitCXXStructor(GD
);
4138 else if (FD
->isMultiVersion())
4139 EmitMultiVersionFunctionDefinition(GD
, GV
);
4141 EmitGlobalFunctionDefinition(GD
, GV
);
4143 if (Method
->isVirtual())
4144 getVTables().EmitThunks(GD
);
4149 if (FD
->isMultiVersion())
4150 return EmitMultiVersionFunctionDefinition(GD
, GV
);
4151 return EmitGlobalFunctionDefinition(GD
, GV
);
4154 if (const auto *VD
= dyn_cast
<VarDecl
>(D
))
4155 return EmitGlobalVarDefinition(VD
, !VD
->hasDefinition());
4157 llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
4160 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue
*Old
,
4161 llvm::Function
*NewFn
);
4164 TargetMVPriority(const TargetInfo
&TI
,
4165 const CodeGenFunction::MultiVersionResolverOption
&RO
) {
4166 unsigned Priority
= 0;
4167 unsigned NumFeatures
= 0;
4168 for (StringRef Feat
: RO
.Conditions
.Features
) {
4169 Priority
= std::max(Priority
, TI
.multiVersionSortPriority(Feat
));
4173 if (!RO
.Conditions
.Architecture
.empty())
4174 Priority
= std::max(
4175 Priority
, TI
.multiVersionSortPriority(RO
.Conditions
.Architecture
));
4177 Priority
+= TI
.multiVersionFeatureCost() * NumFeatures
;
4182 // Multiversion functions should be at most 'WeakODRLinkage' so that a different
4183 // TU can forward declare the function without causing problems. Particularly
4184 // in the cases of CPUDispatch, this causes issues. This also makes sure we
4185 // work with internal linkage functions, so that the same function name can be
4186 // used with internal linkage in multiple TUs.
4187 llvm::GlobalValue::LinkageTypes
getMultiversionLinkage(CodeGenModule
&CGM
,
4189 const FunctionDecl
*FD
= cast
<FunctionDecl
>(GD
.getDecl());
4190 if (FD
->getFormalLinkage() == Linkage::Internal
)
4191 return llvm::GlobalValue::InternalLinkage
;
4192 return llvm::GlobalValue::WeakODRLinkage
;
4195 void CodeGenModule::emitMultiVersionFunctions() {
4196 std::vector
<GlobalDecl
> MVFuncsToEmit
;
4197 MultiVersionFuncs
.swap(MVFuncsToEmit
);
4198 for (GlobalDecl GD
: MVFuncsToEmit
) {
4199 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4200 assert(FD
&& "Expected a FunctionDecl");
4202 auto createFunction
= [&](const FunctionDecl
*Decl
, unsigned MVIdx
= 0) {
4203 GlobalDecl CurGD
{Decl
->isDefined() ? Decl
->getDefinition() : Decl
, MVIdx
};
4204 StringRef MangledName
= getMangledName(CurGD
);
4205 llvm::Constant
*Func
= GetGlobalValue(MangledName
);
4207 if (Decl
->isDefined()) {
4208 EmitGlobalFunctionDefinition(CurGD
, nullptr);
4209 Func
= GetGlobalValue(MangledName
);
4211 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(CurGD
);
4212 llvm::FunctionType
*Ty
= getTypes().GetFunctionType(FI
);
4213 Func
= GetAddrOfFunction(CurGD
, Ty
, /*ForVTable=*/false,
4214 /*DontDefer=*/false, ForDefinition
);
4216 assert(Func
&& "This should have just been created");
4218 return cast
<llvm::Function
>(Func
);
4221 // For AArch64, a resolver is only emitted if a function marked with
4222 // target_version("default")) or target_clones() is present and defined
4223 // in this TU. For other architectures it is always emitted.
4224 bool ShouldEmitResolver
= !getTarget().getTriple().isAArch64();
4225 SmallVector
<CodeGenFunction::MultiVersionResolverOption
, 10> Options
;
4227 getContext().forEachMultiversionedFunctionVersion(
4228 FD
, [&](const FunctionDecl
*CurFD
) {
4229 llvm::SmallVector
<StringRef
, 8> Feats
;
4230 bool IsDefined
= CurFD
->doesThisDeclarationHaveABody();
4232 if (const auto *TA
= CurFD
->getAttr
<TargetAttr
>()) {
4233 TA
->getAddedFeatures(Feats
);
4234 llvm::Function
*Func
= createFunction(CurFD
);
4235 Options
.emplace_back(Func
, TA
->getArchitecture(), Feats
);
4236 } else if (const auto *TVA
= CurFD
->getAttr
<TargetVersionAttr
>()) {
4237 if (TVA
->isDefaultVersion() && IsDefined
)
4238 ShouldEmitResolver
= true;
4239 TVA
->getFeatures(Feats
);
4240 llvm::Function
*Func
= createFunction(CurFD
);
4241 Options
.emplace_back(Func
, /*Architecture*/ "", Feats
);
4242 } else if (const auto *TC
= CurFD
->getAttr
<TargetClonesAttr
>()) {
4244 ShouldEmitResolver
= true;
4245 for (unsigned I
= 0; I
< TC
->featuresStrs_size(); ++I
) {
4246 if (!TC
->isFirstOfVersion(I
))
4249 llvm::Function
*Func
= createFunction(CurFD
, I
);
4250 StringRef Architecture
;
4252 if (getTarget().getTriple().isAArch64())
4253 TC
->getFeatures(Feats
, I
);
4255 StringRef Version
= TC
->getFeatureStr(I
);
4256 if (Version
.starts_with("arch="))
4257 Architecture
= Version
.drop_front(sizeof("arch=") - 1);
4258 else if (Version
!= "default")
4259 Feats
.push_back(Version
);
4261 Options
.emplace_back(Func
, Architecture
, Feats
);
4264 llvm_unreachable("unexpected MultiVersionKind");
4267 if (!ShouldEmitResolver
)
4270 llvm::Constant
*ResolverConstant
= GetOrCreateMultiVersionResolver(GD
);
4271 if (auto *IFunc
= dyn_cast
<llvm::GlobalIFunc
>(ResolverConstant
)) {
4272 ResolverConstant
= IFunc
->getResolver();
4273 if (FD
->isTargetClonesMultiVersion() &&
4274 !getTarget().getTriple().isAArch64()) {
4275 std::string MangledName
= getMangledNameImpl(
4276 *this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
4277 if (!GetGlobalValue(MangledName
+ ".ifunc")) {
4278 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
4279 llvm::FunctionType
*DeclTy
= getTypes().GetFunctionType(FI
);
4280 // In prior versions of Clang, the mangling for ifuncs incorrectly
4281 // included an .ifunc suffix. This alias is generated for backward
4282 // compatibility. It is deprecated, and may be removed in the future.
4283 auto *Alias
= llvm::GlobalAlias::create(
4284 DeclTy
, 0, getMultiversionLinkage(*this, GD
),
4285 MangledName
+ ".ifunc", IFunc
, &getModule());
4286 SetCommonAttributes(FD
, Alias
);
4290 llvm::Function
*ResolverFunc
= cast
<llvm::Function
>(ResolverConstant
);
4292 ResolverFunc
->setLinkage(getMultiversionLinkage(*this, GD
));
4294 if (!ResolverFunc
->hasLocalLinkage() && supportsCOMDAT())
4295 ResolverFunc
->setComdat(
4296 getModule().getOrInsertComdat(ResolverFunc
->getName()));
4298 const TargetInfo
&TI
= getTarget();
4300 Options
, [&TI
](const CodeGenFunction::MultiVersionResolverOption
&LHS
,
4301 const CodeGenFunction::MultiVersionResolverOption
&RHS
) {
4302 return TargetMVPriority(TI
, LHS
) > TargetMVPriority(TI
, RHS
);
4304 CodeGenFunction
CGF(*this);
4305 CGF
.EmitMultiVersionResolver(ResolverFunc
, Options
);
4308 // Ensure that any additions to the deferred decls list caused by emitting a
4309 // variant are emitted. This can happen when the variant itself is inline and
4310 // calls a function without linkage.
4311 if (!MVFuncsToEmit
.empty())
4314 // Ensure that any additions to the multiversion funcs list from either the
4315 // deferred decls or the multiversion functions themselves are emitted.
4316 if (!MultiVersionFuncs
.empty())
4317 emitMultiVersionFunctions();
4320 static void replaceDeclarationWith(llvm::GlobalValue
*Old
,
4321 llvm::Constant
*New
) {
4322 assert(cast
<llvm::Function
>(Old
)->isDeclaration() && "Not a declaration");
4324 Old
->replaceAllUsesWith(New
);
4325 Old
->eraseFromParent();
4328 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD
) {
4329 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4330 assert(FD
&& "Not a FunctionDecl?");
4331 assert(FD
->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4332 const auto *DD
= FD
->getAttr
<CPUDispatchAttr
>();
4333 assert(DD
&& "Not a cpu_dispatch Function?");
4335 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
4336 llvm::FunctionType
*DeclTy
= getTypes().GetFunctionType(FI
);
4338 StringRef ResolverName
= getMangledName(GD
);
4339 UpdateMultiVersionNames(GD
, FD
, ResolverName
);
4341 llvm::Type
*ResolverType
;
4342 GlobalDecl ResolverGD
;
4343 if (getTarget().supportsIFunc()) {
4344 ResolverType
= llvm::FunctionType::get(
4345 llvm::PointerType::get(DeclTy
,
4346 getTypes().getTargetAddressSpace(FD
->getType())),
4350 ResolverType
= DeclTy
;
4354 auto *ResolverFunc
= cast
<llvm::Function
>(GetOrCreateLLVMFunction(
4355 ResolverName
, ResolverType
, ResolverGD
, /*ForVTable=*/false));
4356 ResolverFunc
->setLinkage(getMultiversionLinkage(*this, GD
));
4357 if (supportsCOMDAT())
4358 ResolverFunc
->setComdat(
4359 getModule().getOrInsertComdat(ResolverFunc
->getName()));
4361 SmallVector
<CodeGenFunction::MultiVersionResolverOption
, 10> Options
;
4362 const TargetInfo
&Target
= getTarget();
4364 for (const IdentifierInfo
*II
: DD
->cpus()) {
4365 // Get the name of the target function so we can look it up/create it.
4366 std::string MangledName
= getMangledNameImpl(*this, GD
, FD
, true) +
4367 getCPUSpecificMangling(*this, II
->getName());
4369 llvm::Constant
*Func
= GetGlobalValue(MangledName
);
4372 GlobalDecl ExistingDecl
= Manglings
.lookup(MangledName
);
4373 if (ExistingDecl
.getDecl() &&
4374 ExistingDecl
.getDecl()->getAsFunction()->isDefined()) {
4375 EmitGlobalFunctionDefinition(ExistingDecl
, nullptr);
4376 Func
= GetGlobalValue(MangledName
);
4378 if (!ExistingDecl
.getDecl())
4379 ExistingDecl
= GD
.getWithMultiVersionIndex(Index
);
4381 Func
= GetOrCreateLLVMFunction(
4382 MangledName
, DeclTy
, ExistingDecl
,
4383 /*ForVTable=*/false, /*DontDefer=*/true,
4384 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition
);
4388 llvm::SmallVector
<StringRef
, 32> Features
;
4389 Target
.getCPUSpecificCPUDispatchFeatures(II
->getName(), Features
);
4390 llvm::transform(Features
, Features
.begin(),
4391 [](StringRef Str
) { return Str
.substr(1); });
4392 llvm::erase_if(Features
, [&Target
](StringRef Feat
) {
4393 return !Target
.validateCpuSupports(Feat
);
4395 Options
.emplace_back(cast
<llvm::Function
>(Func
), StringRef
{}, Features
);
4400 Options
, [](const CodeGenFunction::MultiVersionResolverOption
&LHS
,
4401 const CodeGenFunction::MultiVersionResolverOption
&RHS
) {
4402 return llvm::X86::getCpuSupportsMask(LHS
.Conditions
.Features
) >
4403 llvm::X86::getCpuSupportsMask(RHS
.Conditions
.Features
);
4406 // If the list contains multiple 'default' versions, such as when it contains
4407 // 'pentium' and 'generic', don't emit the call to the generic one (since we
4408 // always run on at least a 'pentium'). We do this by deleting the 'least
4409 // advanced' (read, lowest mangling letter).
4410 while (Options
.size() > 1 &&
4411 llvm::all_of(llvm::X86::getCpuSupportsMask(
4412 (Options
.end() - 2)->Conditions
.Features
),
4413 [](auto X
) { return X
== 0; })) {
4414 StringRef LHSName
= (Options
.end() - 2)->Function
->getName();
4415 StringRef RHSName
= (Options
.end() - 1)->Function
->getName();
4416 if (LHSName
.compare(RHSName
) < 0)
4417 Options
.erase(Options
.end() - 2);
4419 Options
.erase(Options
.end() - 1);
4422 CodeGenFunction
CGF(*this);
4423 CGF
.EmitMultiVersionResolver(ResolverFunc
, Options
);
4425 if (getTarget().supportsIFunc()) {
4426 llvm::GlobalValue::LinkageTypes Linkage
= getMultiversionLinkage(*this, GD
);
4427 auto *IFunc
= cast
<llvm::GlobalValue
>(GetOrCreateMultiVersionResolver(GD
));
4429 // Fix up function declarations that were created for cpu_specific before
4430 // cpu_dispatch was known
4431 if (!isa
<llvm::GlobalIFunc
>(IFunc
)) {
4432 auto *GI
= llvm::GlobalIFunc::create(DeclTy
, 0, Linkage
, "", ResolverFunc
,
4434 replaceDeclarationWith(IFunc
, GI
);
4438 std::string AliasName
= getMangledNameImpl(
4439 *this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
4440 llvm::Constant
*AliasFunc
= GetGlobalValue(AliasName
);
4442 auto *GA
= llvm::GlobalAlias::create(DeclTy
, 0, Linkage
, AliasName
, IFunc
,
4444 SetCommonAttributes(GD
, GA
);
4449 /// Adds a declaration to the list of multi version functions if not present.
4450 void CodeGenModule::AddDeferredMultiVersionResolverToEmit(GlobalDecl GD
) {
4451 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4452 assert(FD
&& "Not a FunctionDecl?");
4454 if (FD
->isTargetVersionMultiVersion() || FD
->isTargetClonesMultiVersion()) {
4455 std::string MangledName
=
4456 getMangledNameImpl(*this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
4457 if (!DeferredResolversToEmit
.insert(MangledName
).second
)
4460 MultiVersionFuncs
.push_back(GD
);
4463 /// If a dispatcher for the specified mangled name is not in the module, create
4464 /// and return it. The dispatcher is either an llvm Function with the specified
4465 /// type, or a global ifunc.
4466 llvm::Constant
*CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD
) {
4467 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4468 assert(FD
&& "Not a FunctionDecl?");
4470 std::string MangledName
=
4471 getMangledNameImpl(*this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
4473 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
4474 // a separate resolver).
4475 std::string ResolverName
= MangledName
;
4476 if (getTarget().supportsIFunc()) {
4477 switch (FD
->getMultiVersionKind()) {
4478 case MultiVersionKind::None
:
4479 llvm_unreachable("unexpected MultiVersionKind::None for resolver");
4480 case MultiVersionKind::Target
:
4481 case MultiVersionKind::CPUSpecific
:
4482 case MultiVersionKind::CPUDispatch
:
4483 ResolverName
+= ".ifunc";
4485 case MultiVersionKind::TargetClones
:
4486 case MultiVersionKind::TargetVersion
:
4489 } else if (FD
->isTargetMultiVersion()) {
4490 ResolverName
+= ".resolver";
4493 // If the resolver has already been created, just return it. This lookup may
4494 // yield a function declaration instead of a resolver on AArch64. That is
4495 // because we didn't know whether a resolver will be generated when we first
4496 // encountered a use of the symbol named after this resolver. Therefore,
4497 // targets which support ifuncs should not return here unless we actually
4499 llvm::GlobalValue
*ResolverGV
= GetGlobalValue(ResolverName
);
4501 (isa
<llvm::GlobalIFunc
>(ResolverGV
) || !getTarget().supportsIFunc()))
4504 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
4505 llvm::FunctionType
*DeclTy
= getTypes().GetFunctionType(FI
);
4507 // The resolver needs to be created. For target and target_clones, defer
4508 // creation until the end of the TU.
4509 if (FD
->isTargetMultiVersion() || FD
->isTargetClonesMultiVersion())
4510 AddDeferredMultiVersionResolverToEmit(GD
);
4512 // For cpu_specific, don't create an ifunc yet because we don't know if the
4513 // cpu_dispatch will be emitted in this translation unit.
4514 if (getTarget().supportsIFunc() && !FD
->isCPUSpecificMultiVersion()) {
4515 llvm::Type
*ResolverType
= llvm::FunctionType::get(
4516 llvm::PointerType::get(DeclTy
,
4517 getTypes().getTargetAddressSpace(FD
->getType())),
4519 llvm::Constant
*Resolver
= GetOrCreateLLVMFunction(
4520 MangledName
+ ".resolver", ResolverType
, GlobalDecl
{},
4521 /*ForVTable=*/false);
4522 llvm::GlobalIFunc
*GIF
=
4523 llvm::GlobalIFunc::create(DeclTy
, 0, getMultiversionLinkage(*this, GD
),
4524 "", Resolver
, &getModule());
4525 GIF
->setName(ResolverName
);
4526 SetCommonAttributes(FD
, GIF
);
4528 replaceDeclarationWith(ResolverGV
, GIF
);
4532 llvm::Constant
*Resolver
= GetOrCreateLLVMFunction(
4533 ResolverName
, DeclTy
, GlobalDecl
{}, /*ForVTable=*/false);
4534 assert(isa
<llvm::GlobalValue
>(Resolver
) &&
4535 "Resolver should be created for the first time");
4536 SetCommonAttributes(FD
, cast
<llvm::GlobalValue
>(Resolver
));
4538 replaceDeclarationWith(ResolverGV
, Resolver
);
4542 bool CodeGenModule::shouldDropDLLAttribute(const Decl
*D
,
4543 const llvm::GlobalValue
*GV
) const {
4544 auto SC
= GV
->getDLLStorageClass();
4545 if (SC
== llvm::GlobalValue::DefaultStorageClass
)
4547 const Decl
*MRD
= D
->getMostRecentDecl();
4548 return (((SC
== llvm::GlobalValue::DLLImportStorageClass
&&
4549 !MRD
->hasAttr
<DLLImportAttr
>()) ||
4550 (SC
== llvm::GlobalValue::DLLExportStorageClass
&&
4551 !MRD
->hasAttr
<DLLExportAttr
>())) &&
4552 !shouldMapVisibilityToDLLExport(cast
<NamedDecl
>(MRD
)));
4555 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4556 /// module, create and return an llvm Function with the specified type. If there
4557 /// is something in the module with the specified name, return it potentially
4558 /// bitcasted to the right type.
4560 /// If D is non-null, it specifies a decl that correspond to this. This is used
4561 /// to set the attributes on the function when it is first created.
4562 llvm::Constant
*CodeGenModule::GetOrCreateLLVMFunction(
4563 StringRef MangledName
, llvm::Type
*Ty
, GlobalDecl GD
, bool ForVTable
,
4564 bool DontDefer
, bool IsThunk
, llvm::AttributeList ExtraAttrs
,
4565 ForDefinition_t IsForDefinition
) {
4566 const Decl
*D
= GD
.getDecl();
4568 std::string NameWithoutMultiVersionMangling
;
4569 // Any attempts to use a MultiVersion function should result in retrieving
4570 // the iFunc instead. Name Mangling will handle the rest of the changes.
4571 if (const FunctionDecl
*FD
= cast_or_null
<FunctionDecl
>(D
)) {
4572 // For the device mark the function as one that should be emitted.
4573 if (getLangOpts().OpenMPIsTargetDevice
&& OpenMPRuntime
&&
4574 !OpenMPRuntime
->markAsGlobalTarget(GD
) && FD
->isDefined() &&
4575 !DontDefer
&& !IsForDefinition
) {
4576 if (const FunctionDecl
*FDDef
= FD
->getDefinition()) {
4578 if (const auto *CD
= dyn_cast
<CXXConstructorDecl
>(FDDef
))
4579 GDDef
= GlobalDecl(CD
, GD
.getCtorType());
4580 else if (const auto *DD
= dyn_cast
<CXXDestructorDecl
>(FDDef
))
4581 GDDef
= GlobalDecl(DD
, GD
.getDtorType());
4583 GDDef
= GlobalDecl(FDDef
);
4588 if (FD
->isMultiVersion()) {
4589 UpdateMultiVersionNames(GD
, FD
, MangledName
);
4590 if (!IsForDefinition
) {
4591 // On AArch64 we do not immediatelly emit an ifunc resolver when a
4592 // function is used. Instead we defer the emission until we see a
4593 // default definition. In the meantime we just reference the symbol
4594 // without FMV mangling (it may or may not be replaced later).
4595 if (getTarget().getTriple().isAArch64()) {
4596 AddDeferredMultiVersionResolverToEmit(GD
);
4597 NameWithoutMultiVersionMangling
= getMangledNameImpl(
4598 *this, GD
, FD
, /*OmitMultiVersionMangling=*/true);
4600 return GetOrCreateMultiVersionResolver(GD
);
4605 if (!NameWithoutMultiVersionMangling
.empty())
4606 MangledName
= NameWithoutMultiVersionMangling
;
4608 // Lookup the entry, lazily creating it if necessary.
4609 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
4611 if (WeakRefReferences
.erase(Entry
)) {
4612 const FunctionDecl
*FD
= cast_or_null
<FunctionDecl
>(D
);
4613 if (FD
&& !FD
->hasAttr
<WeakAttr
>())
4614 Entry
->setLinkage(llvm::Function::ExternalLinkage
);
4617 // Handle dropped DLL attributes.
4618 if (D
&& shouldDropDLLAttribute(D
, Entry
)) {
4619 Entry
->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass
);
4623 // If there are two attempts to define the same mangled name, issue an
4625 if (IsForDefinition
&& !Entry
->isDeclaration()) {
4627 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
4628 // to make sure that we issue an error only once.
4629 if (lookupRepresentativeDecl(MangledName
, OtherGD
) &&
4630 (GD
.getCanonicalDecl().getDecl() !=
4631 OtherGD
.getCanonicalDecl().getDecl()) &&
4632 DiagnosedConflictingDefinitions
.insert(GD
).second
) {
4633 getDiags().Report(D
->getLocation(), diag::err_duplicate_mangled_name
)
4635 getDiags().Report(OtherGD
.getDecl()->getLocation(),
4636 diag::note_previous_definition
);
4640 if ((isa
<llvm::Function
>(Entry
) || isa
<llvm::GlobalAlias
>(Entry
)) &&
4641 (Entry
->getValueType() == Ty
)) {
4645 // Make sure the result is of the correct type.
4646 // (If function is requested for a definition, we always need to create a new
4647 // function, not just return a bitcast.)
4648 if (!IsForDefinition
)
4652 // This function doesn't have a complete type (for example, the return
4653 // type is an incomplete struct). Use a fake type instead, and make
4654 // sure not to try to set attributes.
4655 bool IsIncompleteFunction
= false;
4657 llvm::FunctionType
*FTy
;
4658 if (isa
<llvm::FunctionType
>(Ty
)) {
4659 FTy
= cast
<llvm::FunctionType
>(Ty
);
4661 FTy
= llvm::FunctionType::get(VoidTy
, false);
4662 IsIncompleteFunction
= true;
4666 llvm::Function::Create(FTy
, llvm::Function::ExternalLinkage
,
4667 Entry
? StringRef() : MangledName
, &getModule());
4669 // Store the declaration associated with this function so it is potentially
4670 // updated by further declarations or definitions and emitted at the end.
4671 if (D
&& D
->hasAttr
<AnnotateAttr
>())
4672 DeferredAnnotations
[MangledName
] = cast
<ValueDecl
>(D
);
4674 // If we already created a function with the same mangled name (but different
4675 // type) before, take its name and add it to the list of functions to be
4676 // replaced with F at the end of CodeGen.
4678 // This happens if there is a prototype for a function (e.g. "int f()") and
4679 // then a definition of a different type (e.g. "int f(int x)").
4683 // This might be an implementation of a function without a prototype, in
4684 // which case, try to do special replacement of calls which match the new
4685 // prototype. The really key thing here is that we also potentially drop
4686 // arguments from the call site so as to make a direct call, which makes the
4687 // inliner happier and suppresses a number of optimizer warnings (!) about
4688 // dropping arguments.
4689 if (!Entry
->use_empty()) {
4690 ReplaceUsesOfNonProtoTypeWithRealFunction(Entry
, F
);
4691 Entry
->removeDeadConstantUsers();
4694 addGlobalValReplacement(Entry
, F
);
4697 assert(F
->getName() == MangledName
&& "name was uniqued!");
4699 SetFunctionAttributes(GD
, F
, IsIncompleteFunction
, IsThunk
);
4700 if (ExtraAttrs
.hasFnAttrs()) {
4701 llvm::AttrBuilder
B(F
->getContext(), ExtraAttrs
.getFnAttrs());
4706 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
4707 // each other bottoming out with the base dtor. Therefore we emit non-base
4708 // dtors on usage, even if there is no dtor definition in the TU.
4709 if (isa_and_nonnull
<CXXDestructorDecl
>(D
) &&
4710 getCXXABI().useThunkForDtorVariant(cast
<CXXDestructorDecl
>(D
),
4712 addDeferredDeclToEmit(GD
);
4714 // This is the first use or definition of a mangled name. If there is a
4715 // deferred decl with this name, remember that we need to emit it at the end
4717 auto DDI
= DeferredDecls
.find(MangledName
);
4718 if (DDI
!= DeferredDecls
.end()) {
4719 // Move the potentially referenced deferred decl to the
4720 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
4721 // don't need it anymore).
4722 addDeferredDeclToEmit(DDI
->second
);
4723 DeferredDecls
.erase(DDI
);
4725 // Otherwise, there are cases we have to worry about where we're
4726 // using a declaration for which we must emit a definition but where
4727 // we might not find a top-level definition:
4728 // - member functions defined inline in their classes
4729 // - friend functions defined inline in some class
4730 // - special member functions with implicit definitions
4731 // If we ever change our AST traversal to walk into class methods,
4732 // this will be unnecessary.
4734 // We also don't emit a definition for a function if it's going to be an
4735 // entry in a vtable, unless it's already marked as used.
4736 } else if (getLangOpts().CPlusPlus
&& D
) {
4737 // Look for a declaration that's lexically in a record.
4738 for (const auto *FD
= cast
<FunctionDecl
>(D
)->getMostRecentDecl(); FD
;
4739 FD
= FD
->getPreviousDecl()) {
4740 if (isa
<CXXRecordDecl
>(FD
->getLexicalDeclContext())) {
4741 if (FD
->doesThisDeclarationHaveABody()) {
4742 addDeferredDeclToEmit(GD
.getWithDecl(FD
));
4750 // Make sure the result is of the requested type.
4751 if (!IsIncompleteFunction
) {
4752 assert(F
->getFunctionType() == Ty
);
4759 /// GetAddrOfFunction - Return the address of the given function. If Ty is
4760 /// non-null, then this function will use the specified type if it has to
4761 /// create it (this occurs when we see a definition of the function).
4763 CodeGenModule::GetAddrOfFunction(GlobalDecl GD
, llvm::Type
*Ty
, bool ForVTable
,
4765 ForDefinition_t IsForDefinition
) {
4766 // If there was no specific requested type, just convert it now.
4768 const auto *FD
= cast
<FunctionDecl
>(GD
.getDecl());
4769 Ty
= getTypes().ConvertType(FD
->getType());
4772 // Devirtualized destructor calls may come through here instead of via
4773 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
4774 // of the complete destructor when necessary.
4775 if (const auto *DD
= dyn_cast
<CXXDestructorDecl
>(GD
.getDecl())) {
4776 if (getTarget().getCXXABI().isMicrosoft() &&
4777 GD
.getDtorType() == Dtor_Complete
&&
4778 DD
->getParent()->getNumVBases() == 0)
4779 GD
= GlobalDecl(DD
, Dtor_Base
);
4782 StringRef MangledName
= getMangledName(GD
);
4783 auto *F
= GetOrCreateLLVMFunction(MangledName
, Ty
, GD
, ForVTable
, DontDefer
,
4784 /*IsThunk=*/false, llvm::AttributeList(),
4786 // Returns kernel handle for HIP kernel stub function.
4787 if (LangOpts
.CUDA
&& !LangOpts
.CUDAIsDevice
&&
4788 cast
<FunctionDecl
>(GD
.getDecl())->hasAttr
<CUDAGlobalAttr
>()) {
4789 auto *Handle
= getCUDARuntime().getKernelHandle(
4790 cast
<llvm::Function
>(F
->stripPointerCasts()), GD
);
4791 if (IsForDefinition
)
4798 llvm::Constant
*CodeGenModule::GetFunctionStart(const ValueDecl
*Decl
) {
4799 llvm::GlobalValue
*F
=
4800 cast
<llvm::GlobalValue
>(GetAddrOfFunction(Decl
)->stripPointerCasts());
4802 return llvm::NoCFIValue::get(F
);
4805 static const FunctionDecl
*
4806 GetRuntimeFunctionDecl(ASTContext
&C
, StringRef Name
) {
4807 TranslationUnitDecl
*TUDecl
= C
.getTranslationUnitDecl();
4808 DeclContext
*DC
= TranslationUnitDecl::castToDeclContext(TUDecl
);
4810 IdentifierInfo
&CII
= C
.Idents
.get(Name
);
4811 for (const auto *Result
: DC
->lookup(&CII
))
4812 if (const auto *FD
= dyn_cast
<FunctionDecl
>(Result
))
4815 if (!C
.getLangOpts().CPlusPlus
)
4818 // Demangle the premangled name from getTerminateFn()
4819 IdentifierInfo
&CXXII
=
4820 (Name
== "_ZSt9terminatev" || Name
== "?terminate@@YAXXZ")
4821 ? C
.Idents
.get("terminate")
4822 : C
.Idents
.get(Name
);
4824 for (const auto &N
: {"__cxxabiv1", "std"}) {
4825 IdentifierInfo
&NS
= C
.Idents
.get(N
);
4826 for (const auto *Result
: DC
->lookup(&NS
)) {
4827 const NamespaceDecl
*ND
= dyn_cast
<NamespaceDecl
>(Result
);
4828 if (auto *LSD
= dyn_cast
<LinkageSpecDecl
>(Result
))
4829 for (const auto *Result
: LSD
->lookup(&NS
))
4830 if ((ND
= dyn_cast
<NamespaceDecl
>(Result
)))
4834 for (const auto *Result
: ND
->lookup(&CXXII
))
4835 if (const auto *FD
= dyn_cast
<FunctionDecl
>(Result
))
4843 /// CreateRuntimeFunction - Create a new runtime function with the specified
4845 llvm::FunctionCallee
4846 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType
*FTy
, StringRef Name
,
4847 llvm::AttributeList ExtraAttrs
, bool Local
,
4848 bool AssumeConvergent
) {
4849 if (AssumeConvergent
) {
4851 ExtraAttrs
.addFnAttribute(VMContext
, llvm::Attribute::Convergent
);
4855 GetOrCreateLLVMFunction(Name
, FTy
, GlobalDecl(), /*ForVTable=*/false,
4856 /*DontDefer=*/false, /*IsThunk=*/false,
4859 if (auto *F
= dyn_cast
<llvm::Function
>(C
)) {
4861 F
->setCallingConv(getRuntimeCC());
4863 // In Windows Itanium environments, try to mark runtime functions
4864 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
4865 // will link their standard library statically or dynamically. Marking
4866 // functions imported when they are not imported can cause linker errors
4868 if (!Local
&& getTriple().isWindowsItaniumEnvironment() &&
4869 !getCodeGenOpts().LTOVisibilityPublicStd
) {
4870 const FunctionDecl
*FD
= GetRuntimeFunctionDecl(Context
, Name
);
4871 if (!FD
|| FD
->hasAttr
<DLLImportAttr
>()) {
4872 F
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass
);
4873 F
->setLinkage(llvm::GlobalValue::ExternalLinkage
);
4877 // FIXME: We should use CodeGenModule::SetLLVMFunctionAttributes() instead
4878 // of trying to approximate the attributes using the LLVM function
4879 // signature. This requires revising the API of CreateRuntimeFunction().
4880 markRegisterParameterAttributes(F
);
4887 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4888 /// create and return an llvm GlobalVariable with the specified type and address
4889 /// space. If there is something in the module with the specified name, return
4890 /// it potentially bitcasted to the right type.
4892 /// If D is non-null, it specifies a decl that correspond to this. This is used
4893 /// to set the attributes on the global when it is first created.
4895 /// If IsForDefinition is true, it is guaranteed that an actual global with
4896 /// type Ty will be returned, not conversion of a variable with the same
4897 /// mangled name but some other type.
4899 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName
, llvm::Type
*Ty
,
4900 LangAS AddrSpace
, const VarDecl
*D
,
4901 ForDefinition_t IsForDefinition
) {
4902 // Lookup the entry, lazily creating it if necessary.
4903 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
4904 unsigned TargetAS
= getContext().getTargetAddressSpace(AddrSpace
);
4906 if (WeakRefReferences
.erase(Entry
)) {
4907 if (D
&& !D
->hasAttr
<WeakAttr
>())
4908 Entry
->setLinkage(llvm::Function::ExternalLinkage
);
4911 // Handle dropped DLL attributes.
4912 if (D
&& shouldDropDLLAttribute(D
, Entry
))
4913 Entry
->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass
);
4915 if (LangOpts
.OpenMP
&& !LangOpts
.OpenMPSimd
&& D
)
4916 getOpenMPRuntime().registerTargetGlobalVariable(D
, Entry
);
4918 if (Entry
->getValueType() == Ty
&& Entry
->getAddressSpace() == TargetAS
)
4921 // If there are two attempts to define the same mangled name, issue an
4923 if (IsForDefinition
&& !Entry
->isDeclaration()) {
4925 const VarDecl
*OtherD
;
4927 // Check that D is not yet in DiagnosedConflictingDefinitions is required
4928 // to make sure that we issue an error only once.
4929 if (D
&& lookupRepresentativeDecl(MangledName
, OtherGD
) &&
4930 (D
->getCanonicalDecl() != OtherGD
.getCanonicalDecl().getDecl()) &&
4931 (OtherD
= dyn_cast
<VarDecl
>(OtherGD
.getDecl())) &&
4932 OtherD
->hasInit() &&
4933 DiagnosedConflictingDefinitions
.insert(D
).second
) {
4934 getDiags().Report(D
->getLocation(), diag::err_duplicate_mangled_name
)
4936 getDiags().Report(OtherGD
.getDecl()->getLocation(),
4937 diag::note_previous_definition
);
4941 // Make sure the result is of the correct type.
4942 if (Entry
->getType()->getAddressSpace() != TargetAS
)
4943 return llvm::ConstantExpr::getAddrSpaceCast(
4944 Entry
, llvm::PointerType::get(Ty
->getContext(), TargetAS
));
4946 // (If global is requested for a definition, we always need to create a new
4947 // global, not just return a bitcast.)
4948 if (!IsForDefinition
)
4952 auto DAddrSpace
= GetGlobalVarAddressSpace(D
);
4954 auto *GV
= new llvm::GlobalVariable(
4955 getModule(), Ty
, false, llvm::GlobalValue::ExternalLinkage
, nullptr,
4956 MangledName
, nullptr, llvm::GlobalVariable::NotThreadLocal
,
4957 getContext().getTargetAddressSpace(DAddrSpace
));
4959 // If we already created a global with the same mangled name (but different
4960 // type) before, take its name and remove it from its parent.
4962 GV
->takeName(Entry
);
4964 if (!Entry
->use_empty()) {
4965 Entry
->replaceAllUsesWith(GV
);
4968 Entry
->eraseFromParent();
4971 // This is the first use or definition of a mangled name. If there is a
4972 // deferred decl with this name, remember that we need to emit it at the end
4974 auto DDI
= DeferredDecls
.find(MangledName
);
4975 if (DDI
!= DeferredDecls
.end()) {
4976 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
4977 // list, and remove it from DeferredDecls (since we don't need it anymore).
4978 addDeferredDeclToEmit(DDI
->second
);
4979 DeferredDecls
.erase(DDI
);
4982 // Handle things which are present even on external declarations.
4984 if (LangOpts
.OpenMP
&& !LangOpts
.OpenMPSimd
)
4985 getOpenMPRuntime().registerTargetGlobalVariable(D
, GV
);
4987 // FIXME: This code is overly simple and should be merged with other global
4989 GV
->setConstant(D
->getType().isConstantStorage(getContext(), false, false));
4991 GV
->setAlignment(getContext().getDeclAlign(D
).getAsAlign());
4993 setLinkageForGV(GV
, D
);
4995 if (D
->getTLSKind()) {
4996 if (D
->getTLSKind() == VarDecl::TLS_Dynamic
)
4997 CXXThreadLocals
.push_back(D
);
5001 setGVProperties(GV
, D
);
5003 // If required by the ABI, treat declarations of static data members with
5004 // inline initializers as definitions.
5005 if (getContext().isMSStaticDataMemberInlineDefinition(D
)) {
5006 EmitGlobalVarDefinition(D
);
5009 // Emit section information for extern variables.
5010 if (D
->hasExternalStorage()) {
5011 if (const SectionAttr
*SA
= D
->getAttr
<SectionAttr
>())
5012 GV
->setSection(SA
->getName());
5015 // Handle XCore specific ABI requirements.
5016 if (getTriple().getArch() == llvm::Triple::xcore
&&
5017 D
->getLanguageLinkage() == CLanguageLinkage
&&
5018 D
->getType().isConstant(Context
) &&
5019 isExternallyVisible(D
->getLinkageAndVisibility().getLinkage()))
5020 GV
->setSection(".cp.rodata");
5022 // Handle code model attribute
5023 if (const auto *CMA
= D
->getAttr
<CodeModelAttr
>())
5024 GV
->setCodeModel(CMA
->getModel());
5026 // Check if we a have a const declaration with an initializer, we may be
5027 // able to emit it as available_externally to expose it's value to the
5029 if (Context
.getLangOpts().CPlusPlus
&& GV
->hasExternalLinkage() &&
5030 D
->getType().isConstQualified() && !GV
->hasInitializer() &&
5031 !D
->hasDefinition() && D
->hasInit() && !D
->hasAttr
<DLLImportAttr
>()) {
5032 const auto *Record
=
5033 Context
.getBaseElementType(D
->getType())->getAsCXXRecordDecl();
5034 bool HasMutableFields
= Record
&& Record
->hasMutableFields();
5035 if (!HasMutableFields
) {
5036 const VarDecl
*InitDecl
;
5037 const Expr
*InitExpr
= D
->getAnyInitializer(InitDecl
);
5039 ConstantEmitter
emitter(*this);
5040 llvm::Constant
*Init
= emitter
.tryEmitForInitializer(*InitDecl
);
5042 auto *InitType
= Init
->getType();
5043 if (GV
->getValueType() != InitType
) {
5044 // The type of the initializer does not match the definition.
5045 // This happens when an initializer has a different type from
5046 // the type of the global (because of padding at the end of a
5047 // structure for instance).
5048 GV
->setName(StringRef());
5049 // Make a new global with the correct type, this is now guaranteed
5051 auto *NewGV
= cast
<llvm::GlobalVariable
>(
5052 GetAddrOfGlobalVar(D
, InitType
, IsForDefinition
)
5053 ->stripPointerCasts());
5055 // Erase the old global, since it is no longer used.
5056 GV
->eraseFromParent();
5059 GV
->setInitializer(Init
);
5060 GV
->setConstant(true);
5061 GV
->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage
);
5063 emitter
.finalize(GV
);
5071 D
->isThisDeclarationADefinition(Context
) == VarDecl::DeclarationOnly
) {
5072 getTargetCodeGenInfo().setTargetAttributes(D
, GV
, *this);
5073 // External HIP managed variables needed to be recorded for transformation
5074 // in both device and host compilations.
5075 if (getLangOpts().CUDA
&& D
&& D
->hasAttr
<HIPManagedAttr
>() &&
5076 D
->hasExternalStorage())
5077 getCUDARuntime().handleVarRegistration(D
, *GV
);
5081 SanitizerMD
->reportGlobal(GV
, *D
);
5084 D
? D
->getType().getAddressSpace()
5085 : (LangOpts
.OpenCL
? LangAS::opencl_global
: LangAS::Default
);
5086 assert(getContext().getTargetAddressSpace(ExpectedAS
) == TargetAS
);
5087 if (DAddrSpace
!= ExpectedAS
) {
5088 return getTargetCodeGenInfo().performAddrSpaceCast(
5089 *this, GV
, DAddrSpace
, ExpectedAS
,
5090 llvm::PointerType::get(getLLVMContext(), TargetAS
));
5097 CodeGenModule::GetAddrOfGlobal(GlobalDecl GD
, ForDefinition_t IsForDefinition
) {
5098 const Decl
*D
= GD
.getDecl();
5100 if (isa
<CXXConstructorDecl
>(D
) || isa
<CXXDestructorDecl
>(D
))
5101 return getAddrOfCXXStructor(GD
, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
5102 /*DontDefer=*/false, IsForDefinition
);
5104 if (isa
<CXXMethodDecl
>(D
)) {
5106 &getTypes().arrangeCXXMethodDeclaration(cast
<CXXMethodDecl
>(D
));
5107 auto Ty
= getTypes().GetFunctionType(*FInfo
);
5108 return GetAddrOfFunction(GD
, Ty
, /*ForVTable=*/false, /*DontDefer=*/false,
5112 if (isa
<FunctionDecl
>(D
)) {
5113 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
5114 llvm::FunctionType
*Ty
= getTypes().GetFunctionType(FI
);
5115 return GetAddrOfFunction(GD
, Ty
, /*ForVTable=*/false, /*DontDefer=*/false,
5119 return GetAddrOfGlobalVar(cast
<VarDecl
>(D
), /*Ty=*/nullptr, IsForDefinition
);
5122 llvm::GlobalVariable
*CodeGenModule::CreateOrReplaceCXXRuntimeVariable(
5123 StringRef Name
, llvm::Type
*Ty
, llvm::GlobalValue::LinkageTypes Linkage
,
5124 llvm::Align Alignment
) {
5125 llvm::GlobalVariable
*GV
= getModule().getNamedGlobal(Name
);
5126 llvm::GlobalVariable
*OldGV
= nullptr;
5129 // Check if the variable has the right type.
5130 if (GV
->getValueType() == Ty
)
5133 // Because C++ name mangling, the only way we can end up with an already
5134 // existing global with the same name is if it has been declared extern "C".
5135 assert(GV
->isDeclaration() && "Declaration has wrong type!");
5139 // Create a new variable.
5140 GV
= new llvm::GlobalVariable(getModule(), Ty
, /*isConstant=*/true,
5141 Linkage
, nullptr, Name
);
5144 // Replace occurrences of the old variable if needed.
5145 GV
->takeName(OldGV
);
5147 if (!OldGV
->use_empty()) {
5148 OldGV
->replaceAllUsesWith(GV
);
5151 OldGV
->eraseFromParent();
5154 if (supportsCOMDAT() && GV
->isWeakForLinker() &&
5155 !GV
->hasAvailableExternallyLinkage())
5156 GV
->setComdat(TheModule
.getOrInsertComdat(GV
->getName()));
5158 GV
->setAlignment(Alignment
);
5163 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
5164 /// given global variable. If Ty is non-null and if the global doesn't exist,
5165 /// then it will be created with the specified type instead of whatever the
5166 /// normal requested type would be. If IsForDefinition is true, it is guaranteed
5167 /// that an actual global with type Ty will be returned, not conversion of a
5168 /// variable with the same mangled name but some other type.
5169 llvm::Constant
*CodeGenModule::GetAddrOfGlobalVar(const VarDecl
*D
,
5171 ForDefinition_t IsForDefinition
) {
5172 assert(D
->hasGlobalStorage() && "Not a global variable");
5173 QualType ASTTy
= D
->getType();
5175 Ty
= getTypes().ConvertTypeForMem(ASTTy
);
5177 StringRef MangledName
= getMangledName(D
);
5178 return GetOrCreateLLVMGlobal(MangledName
, Ty
, ASTTy
.getAddressSpace(), D
,
5182 /// CreateRuntimeVariable - Create a new runtime global variable with the
5183 /// specified type and name.
5185 CodeGenModule::CreateRuntimeVariable(llvm::Type
*Ty
,
5187 LangAS AddrSpace
= getContext().getLangOpts().OpenCL
? LangAS::opencl_global
5189 auto *Ret
= GetOrCreateLLVMGlobal(Name
, Ty
, AddrSpace
, nullptr);
5190 setDSOLocal(cast
<llvm::GlobalValue
>(Ret
->stripPointerCasts()));
5194 void CodeGenModule::EmitTentativeDefinition(const VarDecl
*D
) {
5195 assert(!D
->getInit() && "Cannot emit definite definitions here!");
5197 StringRef MangledName
= getMangledName(D
);
5198 llvm::GlobalValue
*GV
= GetGlobalValue(MangledName
);
5200 // We already have a definition, not declaration, with the same mangled name.
5201 // Emitting of declaration is not required (and actually overwrites emitted
5203 if (GV
&& !GV
->isDeclaration())
5206 // If we have not seen a reference to this variable yet, place it into the
5207 // deferred declarations table to be emitted if needed later.
5208 if (!MustBeEmitted(D
) && !GV
) {
5209 DeferredDecls
[MangledName
] = D
;
5213 // The tentative definition is the only definition.
5214 EmitGlobalVarDefinition(D
);
5217 void CodeGenModule::EmitExternalDeclaration(const DeclaratorDecl
*D
) {
5218 if (auto const *V
= dyn_cast
<const VarDecl
>(D
))
5219 EmitExternalVarDeclaration(V
);
5220 if (auto const *FD
= dyn_cast
<const FunctionDecl
>(D
))
5221 EmitExternalFunctionDeclaration(FD
);
5224 CharUnits
CodeGenModule::GetTargetTypeStoreSize(llvm::Type
*Ty
) const {
5225 return Context
.toCharUnitsFromBits(
5226 getDataLayout().getTypeStoreSizeInBits(Ty
));
5229 LangAS
CodeGenModule::GetGlobalVarAddressSpace(const VarDecl
*D
) {
5230 if (LangOpts
.OpenCL
) {
5231 LangAS AS
= D
? D
->getType().getAddressSpace() : LangAS::opencl_global
;
5232 assert(AS
== LangAS::opencl_global
||
5233 AS
== LangAS::opencl_global_device
||
5234 AS
== LangAS::opencl_global_host
||
5235 AS
== LangAS::opencl_constant
||
5236 AS
== LangAS::opencl_local
||
5237 AS
>= LangAS::FirstTargetAddressSpace
);
5241 if (LangOpts
.SYCLIsDevice
&&
5242 (!D
|| D
->getType().getAddressSpace() == LangAS::Default
))
5243 return LangAS::sycl_global
;
5245 if (LangOpts
.CUDA
&& LangOpts
.CUDAIsDevice
) {
5247 if (D
->hasAttr
<CUDAConstantAttr
>())
5248 return LangAS::cuda_constant
;
5249 if (D
->hasAttr
<CUDASharedAttr
>())
5250 return LangAS::cuda_shared
;
5251 if (D
->hasAttr
<CUDADeviceAttr
>())
5252 return LangAS::cuda_device
;
5253 if (D
->getType().isConstQualified())
5254 return LangAS::cuda_constant
;
5256 return LangAS::cuda_device
;
5259 if (LangOpts
.OpenMP
) {
5261 if (OpenMPRuntime
->hasAllocateAttributeForGlobalVar(D
, AS
))
5264 return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D
);
5267 LangAS
CodeGenModule::GetGlobalConstantAddressSpace() const {
5268 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
5269 if (LangOpts
.OpenCL
)
5270 return LangAS::opencl_constant
;
5271 if (LangOpts
.SYCLIsDevice
)
5272 return LangAS::sycl_global
;
5273 if (LangOpts
.HIP
&& LangOpts
.CUDAIsDevice
&& getTriple().isSPIRV())
5274 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
5275 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
5276 // with OpVariable instructions with Generic storage class which is not
5277 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
5278 // UniformConstant storage class is not viable as pointers to it may not be
5279 // casted to Generic pointers which are used to model HIP's "flat" pointers.
5280 return LangAS::cuda_device
;
5281 if (auto AS
= getTarget().getConstantAddressSpace())
5283 return LangAS::Default
;
5286 // In address space agnostic languages, string literals are in default address
5287 // space in AST. However, certain targets (e.g. amdgcn) request them to be
5288 // emitted in constant address space in LLVM IR. To be consistent with other
5289 // parts of AST, string literal global variables in constant address space
5290 // need to be casted to default address space before being put into address
5291 // map and referenced by other part of CodeGen.
5292 // In OpenCL, string literals are in constant address space in AST, therefore
5293 // they should not be casted to default address space.
5294 static llvm::Constant
*
5295 castStringLiteralToDefaultAddressSpace(CodeGenModule
&CGM
,
5296 llvm::GlobalVariable
*GV
) {
5297 llvm::Constant
*Cast
= GV
;
5298 if (!CGM
.getLangOpts().OpenCL
) {
5299 auto AS
= CGM
.GetGlobalConstantAddressSpace();
5300 if (AS
!= LangAS::Default
)
5301 Cast
= CGM
.getTargetCodeGenInfo().performAddrSpaceCast(
5302 CGM
, GV
, AS
, LangAS::Default
,
5303 llvm::PointerType::get(
5304 CGM
.getLLVMContext(),
5305 CGM
.getContext().getTargetAddressSpace(LangAS::Default
)));
5310 template<typename SomeDecl
>
5311 void CodeGenModule::MaybeHandleStaticInExternC(const SomeDecl
*D
,
5312 llvm::GlobalValue
*GV
) {
5313 if (!getLangOpts().CPlusPlus
)
5316 // Must have 'used' attribute, or else inline assembly can't rely on
5317 // the name existing.
5318 if (!D
->template hasAttr
<UsedAttr
>())
5321 // Must have internal linkage and an ordinary name.
5322 if (!D
->getIdentifier() || D
->getFormalLinkage() != Linkage::Internal
)
5325 // Must be in an extern "C" context. Entities declared directly within
5326 // a record are not extern "C" even if the record is in such a context.
5327 const SomeDecl
*First
= D
->getFirstDecl();
5328 if (First
->getDeclContext()->isRecord() || !First
->isInExternCContext())
5331 // OK, this is an internal linkage entity inside an extern "C" linkage
5332 // specification. Make a note of that so we can give it the "expected"
5333 // mangled name if nothing else is using that name.
5334 std::pair
<StaticExternCMap::iterator
, bool> R
=
5335 StaticExternCValues
.insert(std::make_pair(D
->getIdentifier(), GV
));
5337 // If we have multiple internal linkage entities with the same name
5338 // in extern "C" regions, none of them gets that name.
5340 R
.first
->second
= nullptr;
5343 static bool shouldBeInCOMDAT(CodeGenModule
&CGM
, const Decl
&D
) {
5344 if (!CGM
.supportsCOMDAT())
5347 if (D
.hasAttr
<SelectAnyAttr
>())
5351 if (auto *VD
= dyn_cast
<VarDecl
>(&D
))
5352 Linkage
= CGM
.getContext().GetGVALinkageForVariable(VD
);
5354 Linkage
= CGM
.getContext().GetGVALinkageForFunction(cast
<FunctionDecl
>(&D
));
5358 case GVA_AvailableExternally
:
5359 case GVA_StrongExternal
:
5361 case GVA_DiscardableODR
:
5365 llvm_unreachable("No such linkage");
5368 bool CodeGenModule::supportsCOMDAT() const {
5369 return getTriple().supportsCOMDAT();
5372 void CodeGenModule::maybeSetTrivialComdat(const Decl
&D
,
5373 llvm::GlobalObject
&GO
) {
5374 if (!shouldBeInCOMDAT(*this, D
))
5376 GO
.setComdat(TheModule
.getOrInsertComdat(GO
.getName()));
5379 /// Pass IsTentative as true if you want to create a tentative definition.
5380 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl
*D
,
5382 // OpenCL global variables of sampler type are translated to function calls,
5383 // therefore no need to be translated.
5384 QualType ASTTy
= D
->getType();
5385 if (getLangOpts().OpenCL
&& ASTTy
->isSamplerT())
5388 // If this is OpenMP device, check if it is legal to emit this global
5390 if (LangOpts
.OpenMPIsTargetDevice
&& OpenMPRuntime
&&
5391 OpenMPRuntime
->emitTargetGlobalVariable(D
))
5394 llvm::TrackingVH
<llvm::Constant
> Init
;
5395 bool NeedsGlobalCtor
= false;
5396 // Whether the definition of the variable is available externally.
5397 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
5398 // since this is the job for its original source.
5399 bool IsDefinitionAvailableExternally
=
5400 getContext().GetGVALinkageForVariable(D
) == GVA_AvailableExternally
;
5401 bool NeedsGlobalDtor
=
5402 !IsDefinitionAvailableExternally
&&
5403 D
->needsDestruction(getContext()) == QualType::DK_cxx_destructor
;
5405 // It is helpless to emit the definition for an available_externally variable
5406 // which can't be marked as const.
5407 // We don't need to check if it needs global ctor or dtor. See the above
5408 // comment for ideas.
5409 if (IsDefinitionAvailableExternally
&&
5410 (!D
->hasConstantInitialization() ||
5411 // TODO: Update this when we have interface to check constexpr
5413 D
->needsDestruction(getContext()) ||
5414 !D
->getType().isConstantStorage(getContext(), true, true)))
5417 const VarDecl
*InitDecl
;
5418 const Expr
*InitExpr
= D
->getAnyInitializer(InitDecl
);
5420 std::optional
<ConstantEmitter
> emitter
;
5422 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
5423 // as part of their declaration." Sema has already checked for
5424 // error cases, so we just need to set Init to UndefValue.
5425 bool IsCUDASharedVar
=
5426 getLangOpts().CUDAIsDevice
&& D
->hasAttr
<CUDASharedAttr
>();
5427 // Shadows of initialized device-side global variables are also left
5429 // Managed Variables should be initialized on both host side and device side.
5430 bool IsCUDAShadowVar
=
5431 !getLangOpts().CUDAIsDevice
&& !D
->hasAttr
<HIPManagedAttr
>() &&
5432 (D
->hasAttr
<CUDAConstantAttr
>() || D
->hasAttr
<CUDADeviceAttr
>() ||
5433 D
->hasAttr
<CUDASharedAttr
>());
5434 bool IsCUDADeviceShadowVar
=
5435 getLangOpts().CUDAIsDevice
&& !D
->hasAttr
<HIPManagedAttr
>() &&
5436 (D
->getType()->isCUDADeviceBuiltinSurfaceType() ||
5437 D
->getType()->isCUDADeviceBuiltinTextureType());
5438 if (getLangOpts().CUDA
&&
5439 (IsCUDASharedVar
|| IsCUDAShadowVar
|| IsCUDADeviceShadowVar
))
5440 Init
= llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy
));
5441 else if (D
->hasAttr
<LoaderUninitializedAttr
>())
5442 Init
= llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy
));
5443 else if (!InitExpr
) {
5444 // This is a tentative definition; tentative definitions are
5445 // implicitly initialized with { 0 }.
5447 // Note that tentative definitions are only emitted at the end of
5448 // a translation unit, so they should never have incomplete
5449 // type. In addition, EmitTentativeDefinition makes sure that we
5450 // never attempt to emit a tentative definition if a real one
5451 // exists. A use may still exists, however, so we still may need
5453 assert(!ASTTy
->isIncompleteType() && "Unexpected incomplete type");
5454 Init
= EmitNullConstant(D
->getType());
5456 initializedGlobalDecl
= GlobalDecl(D
);
5457 emitter
.emplace(*this);
5458 llvm::Constant
*Initializer
= emitter
->tryEmitForInitializer(*InitDecl
);
5460 QualType T
= InitExpr
->getType();
5461 if (D
->getType()->isReferenceType())
5464 if (getLangOpts().CPlusPlus
) {
5465 if (InitDecl
->hasFlexibleArrayInit(getContext()))
5466 ErrorUnsupported(D
, "flexible array initializer");
5467 Init
= EmitNullConstant(T
);
5469 if (!IsDefinitionAvailableExternally
)
5470 NeedsGlobalCtor
= true;
5472 ErrorUnsupported(D
, "static initializer");
5473 Init
= llvm::UndefValue::get(getTypes().ConvertType(T
));
5477 // We don't need an initializer, so remove the entry for the delayed
5478 // initializer position (just in case this entry was delayed) if we
5479 // also don't need to register a destructor.
5480 if (getLangOpts().CPlusPlus
&& !NeedsGlobalDtor
)
5481 DelayedCXXInitPosition
.erase(D
);
5484 CharUnits VarSize
= getContext().getTypeSizeInChars(ASTTy
) +
5485 InitDecl
->getFlexibleArrayInitChars(getContext());
5486 CharUnits CstSize
= CharUnits::fromQuantity(
5487 getDataLayout().getTypeAllocSize(Init
->getType()));
5488 assert(VarSize
== CstSize
&& "Emitted constant has unexpected size");
5493 llvm::Type
* InitType
= Init
->getType();
5494 llvm::Constant
*Entry
=
5495 GetAddrOfGlobalVar(D
, InitType
, ForDefinition_t(!IsTentative
));
5497 // Strip off pointer casts if we got them.
5498 Entry
= Entry
->stripPointerCasts();
5500 // Entry is now either a Function or GlobalVariable.
5501 auto *GV
= dyn_cast
<llvm::GlobalVariable
>(Entry
);
5503 // We have a definition after a declaration with the wrong type.
5504 // We must make a new GlobalVariable* and update everything that used OldGV
5505 // (a declaration or tentative definition) with the new GlobalVariable*
5506 // (which will be a definition).
5508 // This happens if there is a prototype for a global (e.g.
5509 // "extern int x[];") and then a definition of a different type (e.g.
5510 // "int x[10];"). This also happens when an initializer has a different type
5511 // from the type of the global (this happens with unions).
5512 if (!GV
|| GV
->getValueType() != InitType
||
5513 GV
->getType()->getAddressSpace() !=
5514 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D
))) {
5516 // Move the old entry aside so that we'll create a new one.
5517 Entry
->setName(StringRef());
5519 // Make a new global with the correct type, this is now guaranteed to work.
5520 GV
= cast
<llvm::GlobalVariable
>(
5521 GetAddrOfGlobalVar(D
, InitType
, ForDefinition_t(!IsTentative
))
5522 ->stripPointerCasts());
5524 // Replace all uses of the old global with the new global
5525 llvm::Constant
*NewPtrForOldDecl
=
5526 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV
,
5528 Entry
->replaceAllUsesWith(NewPtrForOldDecl
);
5530 // Erase the old global, since it is no longer used.
5531 cast
<llvm::GlobalValue
>(Entry
)->eraseFromParent();
5534 MaybeHandleStaticInExternC(D
, GV
);
5536 if (D
->hasAttr
<AnnotateAttr
>())
5537 AddGlobalAnnotations(D
, GV
);
5539 // Set the llvm linkage type as appropriate.
5540 llvm::GlobalValue::LinkageTypes Linkage
= getLLVMLinkageVarDefinition(D
);
5542 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
5543 // the device. [...]"
5544 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
5545 // __device__, declares a variable that: [...]
5546 // Is accessible from all the threads within the grid and from the host
5547 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
5548 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
5549 if (LangOpts
.CUDA
) {
5550 if (LangOpts
.CUDAIsDevice
) {
5551 if (Linkage
!= llvm::GlobalValue::InternalLinkage
&&
5552 (D
->hasAttr
<CUDADeviceAttr
>() || D
->hasAttr
<CUDAConstantAttr
>() ||
5553 D
->getType()->isCUDADeviceBuiltinSurfaceType() ||
5554 D
->getType()->isCUDADeviceBuiltinTextureType()))
5555 GV
->setExternallyInitialized(true);
5557 getCUDARuntime().internalizeDeviceSideVar(D
, Linkage
);
5559 getCUDARuntime().handleVarRegistration(D
, *GV
);
5562 GV
->setInitializer(Init
);
5564 emitter
->finalize(GV
);
5566 // If it is safe to mark the global 'constant', do so now.
5567 GV
->setConstant(!NeedsGlobalCtor
&& !NeedsGlobalDtor
&&
5568 D
->getType().isConstantStorage(getContext(), true, true));
5570 // If it is in a read-only section, mark it 'constant'.
5571 if (const SectionAttr
*SA
= D
->getAttr
<SectionAttr
>()) {
5572 const ASTContext::SectionInfo
&SI
= Context
.SectionInfos
[SA
->getName()];
5573 if ((SI
.SectionFlags
& ASTContext::PSF_Write
) == 0)
5574 GV
->setConstant(true);
5577 CharUnits AlignVal
= getContext().getDeclAlign(D
);
5578 // Check for alignment specifed in an 'omp allocate' directive.
5579 if (std::optional
<CharUnits
> AlignValFromAllocate
=
5580 getOMPAllocateAlignment(D
))
5581 AlignVal
= *AlignValFromAllocate
;
5582 GV
->setAlignment(AlignVal
.getAsAlign());
5584 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5585 // function is only defined alongside the variable, not also alongside
5586 // callers. Normally, all accesses to a thread_local go through the
5587 // thread-wrapper in order to ensure initialization has occurred, underlying
5588 // variable will never be used other than the thread-wrapper, so it can be
5589 // converted to internal linkage.
5591 // However, if the variable has the 'constinit' attribute, it _can_ be
5592 // referenced directly, without calling the thread-wrapper, so the linkage
5593 // must not be changed.
5595 // Additionally, if the variable isn't plain external linkage, e.g. if it's
5596 // weak or linkonce, the de-duplication semantics are important to preserve,
5597 // so we don't change the linkage.
5598 if (D
->getTLSKind() == VarDecl::TLS_Dynamic
&&
5599 Linkage
== llvm::GlobalValue::ExternalLinkage
&&
5600 Context
.getTargetInfo().getTriple().isOSDarwin() &&
5601 !D
->hasAttr
<ConstInitAttr
>())
5602 Linkage
= llvm::GlobalValue::InternalLinkage
;
5604 GV
->setLinkage(Linkage
);
5605 if (D
->hasAttr
<DLLImportAttr
>())
5606 GV
->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass
);
5607 else if (D
->hasAttr
<DLLExportAttr
>())
5608 GV
->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass
);
5610 GV
->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass
);
5612 if (Linkage
== llvm::GlobalVariable::CommonLinkage
) {
5613 // common vars aren't constant even if declared const.
5614 GV
->setConstant(false);
5615 // Tentative definition of global variables may be initialized with
5616 // non-zero null pointers. In this case they should have weak linkage
5617 // since common linkage must have zero initializer and must not have
5618 // explicit section therefore cannot have non-zero initial value.
5619 if (!GV
->getInitializer()->isNullValue())
5620 GV
->setLinkage(llvm::GlobalVariable::WeakAnyLinkage
);
5623 setNonAliasAttributes(D
, GV
);
5625 if (D
->getTLSKind() && !GV
->isThreadLocal()) {
5626 if (D
->getTLSKind() == VarDecl::TLS_Dynamic
)
5627 CXXThreadLocals
.push_back(D
);
5631 maybeSetTrivialComdat(*D
, *GV
);
5633 // Emit the initializer function if necessary.
5634 if (NeedsGlobalCtor
|| NeedsGlobalDtor
)
5635 EmitCXXGlobalVarDeclInitFunc(D
, GV
, NeedsGlobalCtor
);
5637 SanitizerMD
->reportGlobal(GV
, *D
, NeedsGlobalCtor
);
5639 // Emit global variable debug information.
5640 if (CGDebugInfo
*DI
= getModuleDebugInfo())
5641 if (getCodeGenOpts().hasReducedDebugInfo())
5642 DI
->EmitGlobalVariable(GV
, D
);
5645 void CodeGenModule::EmitExternalVarDeclaration(const VarDecl
*D
) {
5646 if (CGDebugInfo
*DI
= getModuleDebugInfo())
5647 if (getCodeGenOpts().hasReducedDebugInfo()) {
5648 QualType ASTTy
= D
->getType();
5649 llvm::Type
*Ty
= getTypes().ConvertTypeForMem(D
->getType());
5650 llvm::Constant
*GV
=
5651 GetOrCreateLLVMGlobal(D
->getName(), Ty
, ASTTy
.getAddressSpace(), D
);
5652 DI
->EmitExternalVariable(
5653 cast
<llvm::GlobalVariable
>(GV
->stripPointerCasts()), D
);
5657 void CodeGenModule::EmitExternalFunctionDeclaration(const FunctionDecl
*FD
) {
5658 if (CGDebugInfo
*DI
= getModuleDebugInfo())
5659 if (getCodeGenOpts().hasReducedDebugInfo()) {
5660 auto *Ty
= getTypes().ConvertType(FD
->getType());
5661 StringRef MangledName
= getMangledName(FD
);
5662 auto *Fn
= dyn_cast
<llvm::Function
>(
5663 GetOrCreateLLVMFunction(MangledName
, Ty
, FD
, /* ForVTable */ false));
5664 if (!Fn
->getSubprogram())
5665 DI
->EmitFunctionDecl(FD
, FD
->getLocation(), FD
->getType(), Fn
);
5669 static bool isVarDeclStrongDefinition(const ASTContext
&Context
,
5670 CodeGenModule
&CGM
, const VarDecl
*D
,
5672 // Don't give variables common linkage if -fno-common was specified unless it
5673 // was overridden by a NoCommon attribute.
5674 if ((NoCommon
|| D
->hasAttr
<NoCommonAttr
>()) && !D
->hasAttr
<CommonAttr
>())
5678 // A declaration of an identifier for an object that has file scope without
5679 // an initializer, and without a storage-class specifier or with the
5680 // storage-class specifier static, constitutes a tentative definition.
5681 if (D
->getInit() || D
->hasExternalStorage())
5684 // A variable cannot be both common and exist in a section.
5685 if (D
->hasAttr
<SectionAttr
>())
5688 // A variable cannot be both common and exist in a section.
5689 // We don't try to determine which is the right section in the front-end.
5690 // If no specialized section name is applicable, it will resort to default.
5691 if (D
->hasAttr
<PragmaClangBSSSectionAttr
>() ||
5692 D
->hasAttr
<PragmaClangDataSectionAttr
>() ||
5693 D
->hasAttr
<PragmaClangRelroSectionAttr
>() ||
5694 D
->hasAttr
<PragmaClangRodataSectionAttr
>())
5697 // Thread local vars aren't considered common linkage.
5698 if (D
->getTLSKind())
5701 // Tentative definitions marked with WeakImportAttr are true definitions.
5702 if (D
->hasAttr
<WeakImportAttr
>())
5705 // A variable cannot be both common and exist in a comdat.
5706 if (shouldBeInCOMDAT(CGM
, *D
))
5709 // Declarations with a required alignment do not have common linkage in MSVC
5711 if (Context
.getTargetInfo().getCXXABI().isMicrosoft()) {
5712 if (D
->hasAttr
<AlignedAttr
>())
5714 QualType VarType
= D
->getType();
5715 if (Context
.isAlignmentRequired(VarType
))
5718 if (const auto *RT
= VarType
->getAs
<RecordType
>()) {
5719 const RecordDecl
*RD
= RT
->getDecl();
5720 for (const FieldDecl
*FD
: RD
->fields()) {
5721 if (FD
->isBitField())
5723 if (FD
->hasAttr
<AlignedAttr
>())
5725 if (Context
.isAlignmentRequired(FD
->getType()))
5731 // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
5732 // common symbols, so symbols with greater alignment requirements cannot be
5734 // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
5735 // alignments for common symbols via the aligncomm directive, so this
5736 // restriction only applies to MSVC environments.
5737 if (Context
.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
5738 Context
.getTypeAlignIfKnown(D
->getType()) >
5739 Context
.toBits(CharUnits::fromQuantity(32)))
5745 llvm::GlobalValue::LinkageTypes
5746 CodeGenModule::getLLVMLinkageForDeclarator(const DeclaratorDecl
*D
,
5747 GVALinkage Linkage
) {
5748 if (Linkage
== GVA_Internal
)
5749 return llvm::Function::InternalLinkage
;
5751 if (D
->hasAttr
<WeakAttr
>())
5752 return llvm::GlobalVariable::WeakAnyLinkage
;
5754 if (const auto *FD
= D
->getAsFunction())
5755 if (FD
->isMultiVersion() && Linkage
== GVA_AvailableExternally
)
5756 return llvm::GlobalVariable::LinkOnceAnyLinkage
;
5758 // We are guaranteed to have a strong definition somewhere else,
5759 // so we can use available_externally linkage.
5760 if (Linkage
== GVA_AvailableExternally
)
5761 return llvm::GlobalValue::AvailableExternallyLinkage
;
5763 // Note that Apple's kernel linker doesn't support symbol
5764 // coalescing, so we need to avoid linkonce and weak linkages there.
5765 // Normally, this means we just map to internal, but for explicit
5766 // instantiations we'll map to external.
5768 // In C++, the compiler has to emit a definition in every translation unit
5769 // that references the function. We should use linkonce_odr because
5770 // a) if all references in this translation unit are optimized away, we
5771 // don't need to codegen it. b) if the function persists, it needs to be
5772 // merged with other definitions. c) C++ has the ODR, so we know the
5773 // definition is dependable.
5774 if (Linkage
== GVA_DiscardableODR
)
5775 return !Context
.getLangOpts().AppleKext
? llvm::Function::LinkOnceODRLinkage
5776 : llvm::Function::InternalLinkage
;
5778 // An explicit instantiation of a template has weak linkage, since
5779 // explicit instantiations can occur in multiple translation units
5780 // and must all be equivalent. However, we are not allowed to
5781 // throw away these explicit instantiations.
5783 // CUDA/HIP: For -fno-gpu-rdc case, device code is limited to one TU,
5784 // so say that CUDA templates are either external (for kernels) or internal.
5785 // This lets llvm perform aggressive inter-procedural optimizations. For
5786 // -fgpu-rdc case, device function calls across multiple TU's are allowed,
5787 // therefore we need to follow the normal linkage paradigm.
5788 if (Linkage
== GVA_StrongODR
) {
5789 if (getLangOpts().AppleKext
)
5790 return llvm::Function::ExternalLinkage
;
5791 if (getLangOpts().CUDA
&& getLangOpts().CUDAIsDevice
&&
5792 !getLangOpts().GPURelocatableDeviceCode
)
5793 return D
->hasAttr
<CUDAGlobalAttr
>() ? llvm::Function::ExternalLinkage
5794 : llvm::Function::InternalLinkage
;
5795 return llvm::Function::WeakODRLinkage
;
5798 // C++ doesn't have tentative definitions and thus cannot have common
5800 if (!getLangOpts().CPlusPlus
&& isa
<VarDecl
>(D
) &&
5801 !isVarDeclStrongDefinition(Context
, *this, cast
<VarDecl
>(D
),
5802 CodeGenOpts
.NoCommon
))
5803 return llvm::GlobalVariable::CommonLinkage
;
5805 // selectany symbols are externally visible, so use weak instead of
5806 // linkonce. MSVC optimizes away references to const selectany globals, so
5807 // all definitions should be the same and ODR linkage should be used.
5808 // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
5809 if (D
->hasAttr
<SelectAnyAttr
>())
5810 return llvm::GlobalVariable::WeakODRLinkage
;
5812 // Otherwise, we have strong external linkage.
5813 assert(Linkage
== GVA_StrongExternal
);
5814 return llvm::GlobalVariable::ExternalLinkage
;
5817 llvm::GlobalValue::LinkageTypes
5818 CodeGenModule::getLLVMLinkageVarDefinition(const VarDecl
*VD
) {
5819 GVALinkage Linkage
= getContext().GetGVALinkageForVariable(VD
);
5820 return getLLVMLinkageForDeclarator(VD
, Linkage
);
5823 /// Replace the uses of a function that was declared with a non-proto type.
5824 /// We want to silently drop extra arguments from call sites
5825 static void replaceUsesOfNonProtoConstant(llvm::Constant
*old
,
5826 llvm::Function
*newFn
) {
5828 if (old
->use_empty())
5831 llvm::Type
*newRetTy
= newFn
->getReturnType();
5832 SmallVector
<llvm::Value
*, 4> newArgs
;
5834 SmallVector
<llvm::CallBase
*> callSitesToBeRemovedFromParent
;
5836 for (llvm::Value::use_iterator ui
= old
->use_begin(), ue
= old
->use_end();
5838 llvm::User
*user
= ui
->getUser();
5840 // Recognize and replace uses of bitcasts. Most calls to
5841 // unprototyped functions will use bitcasts.
5842 if (auto *bitcast
= dyn_cast
<llvm::ConstantExpr
>(user
)) {
5843 if (bitcast
->getOpcode() == llvm::Instruction::BitCast
)
5844 replaceUsesOfNonProtoConstant(bitcast
, newFn
);
5848 // Recognize calls to the function.
5849 llvm::CallBase
*callSite
= dyn_cast
<llvm::CallBase
>(user
);
5852 if (!callSite
->isCallee(&*ui
))
5855 // If the return types don't match exactly, then we can't
5856 // transform this call unless it's dead.
5857 if (callSite
->getType() != newRetTy
&& !callSite
->use_empty())
5860 // Get the call site's attribute list.
5861 SmallVector
<llvm::AttributeSet
, 8> newArgAttrs
;
5862 llvm::AttributeList oldAttrs
= callSite
->getAttributes();
5864 // If the function was passed too few arguments, don't transform.
5865 unsigned newNumArgs
= newFn
->arg_size();
5866 if (callSite
->arg_size() < newNumArgs
)
5869 // If extra arguments were passed, we silently drop them.
5870 // If any of the types mismatch, we don't transform.
5872 bool dontTransform
= false;
5873 for (llvm::Argument
&A
: newFn
->args()) {
5874 if (callSite
->getArgOperand(argNo
)->getType() != A
.getType()) {
5875 dontTransform
= true;
5879 // Add any parameter attributes.
5880 newArgAttrs
.push_back(oldAttrs
.getParamAttrs(argNo
));
5886 // Okay, we can transform this. Create the new call instruction and copy
5887 // over the required information.
5888 newArgs
.append(callSite
->arg_begin(), callSite
->arg_begin() + argNo
);
5890 // Copy over any operand bundles.
5891 SmallVector
<llvm::OperandBundleDef
, 1> newBundles
;
5892 callSite
->getOperandBundlesAsDefs(newBundles
);
5894 llvm::CallBase
*newCall
;
5895 if (isa
<llvm::CallInst
>(callSite
)) {
5897 llvm::CallInst::Create(newFn
, newArgs
, newBundles
, "", callSite
);
5899 auto *oldInvoke
= cast
<llvm::InvokeInst
>(callSite
);
5900 newCall
= llvm::InvokeInst::Create(newFn
, oldInvoke
->getNormalDest(),
5901 oldInvoke
->getUnwindDest(), newArgs
,
5902 newBundles
, "", callSite
);
5904 newArgs
.clear(); // for the next iteration
5906 if (!newCall
->getType()->isVoidTy())
5907 newCall
->takeName(callSite
);
5908 newCall
->setAttributes(
5909 llvm::AttributeList::get(newFn
->getContext(), oldAttrs
.getFnAttrs(),
5910 oldAttrs
.getRetAttrs(), newArgAttrs
));
5911 newCall
->setCallingConv(callSite
->getCallingConv());
5913 // Finally, remove the old call, replacing any uses with the new one.
5914 if (!callSite
->use_empty())
5915 callSite
->replaceAllUsesWith(newCall
);
5917 // Copy debug location attached to CI.
5918 if (callSite
->getDebugLoc())
5919 newCall
->setDebugLoc(callSite
->getDebugLoc());
5921 callSitesToBeRemovedFromParent
.push_back(callSite
);
5924 for (auto *callSite
: callSitesToBeRemovedFromParent
) {
5925 callSite
->eraseFromParent();
5929 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
5930 /// implement a function with no prototype, e.g. "int foo() {}". If there are
5931 /// existing call uses of the old function in the module, this adjusts them to
5932 /// call the new function directly.
5934 /// This is not just a cleanup: the always_inline pass requires direct calls to
5935 /// functions to be able to inline them. If there is a bitcast in the way, it
5936 /// won't inline them. Instcombine normally deletes these calls, but it isn't
5938 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue
*Old
,
5939 llvm::Function
*NewFn
) {
5940 // If we're redefining a global as a function, don't transform it.
5941 if (!isa
<llvm::Function
>(Old
)) return;
5943 replaceUsesOfNonProtoConstant(Old
, NewFn
);
5946 void CodeGenModule::HandleCXXStaticMemberVarInstantiation(VarDecl
*VD
) {
5947 auto DK
= VD
->isThisDeclarationADefinition();
5948 if ((DK
== VarDecl::Definition
&& VD
->hasAttr
<DLLImportAttr
>()) ||
5949 (LangOpts
.CUDA
&& !shouldEmitCUDAGlobalVar(VD
)))
5952 TemplateSpecializationKind TSK
= VD
->getTemplateSpecializationKind();
5953 // If we have a definition, this might be a deferred decl. If the
5954 // instantiation is explicit, make sure we emit it at the end.
5955 if (VD
->getDefinition() && TSK
== TSK_ExplicitInstantiationDefinition
)
5956 GetAddrOfGlobalVar(VD
);
5958 EmitTopLevelDecl(VD
);
5961 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD
,
5962 llvm::GlobalValue
*GV
) {
5963 const auto *D
= cast
<FunctionDecl
>(GD
.getDecl());
5965 // Compute the function info and LLVM type.
5966 const CGFunctionInfo
&FI
= getTypes().arrangeGlobalDeclaration(GD
);
5967 llvm::FunctionType
*Ty
= getTypes().GetFunctionType(FI
);
5969 // Get or create the prototype for the function.
5970 if (!GV
|| (GV
->getValueType() != Ty
))
5971 GV
= cast
<llvm::GlobalValue
>(GetAddrOfFunction(GD
, Ty
, /*ForVTable=*/false,
5976 if (!GV
->isDeclaration())
5979 // We need to set linkage and visibility on the function before
5980 // generating code for it because various parts of IR generation
5981 // want to propagate this information down (e.g. to local static
5983 auto *Fn
= cast
<llvm::Function
>(GV
);
5984 setFunctionLinkage(GD
, Fn
);
5986 // FIXME: this is redundant with part of setFunctionDefinitionAttributes
5987 setGVProperties(Fn
, GD
);
5989 MaybeHandleStaticInExternC(D
, Fn
);
5991 maybeSetTrivialComdat(*D
, *Fn
);
5993 CodeGenFunction(*this).GenerateCode(GD
, Fn
, FI
);
5995 setNonAliasAttributes(GD
, Fn
);
5996 SetLLVMFunctionAttributesForDefinition(D
, Fn
);
5998 if (const ConstructorAttr
*CA
= D
->getAttr
<ConstructorAttr
>())
5999 AddGlobalCtor(Fn
, CA
->getPriority());
6000 if (const DestructorAttr
*DA
= D
->getAttr
<DestructorAttr
>())
6001 AddGlobalDtor(Fn
, DA
->getPriority(), true);
6002 if (getLangOpts().OpenMP
&& D
->hasAttr
<OMPDeclareTargetDeclAttr
>())
6003 getOpenMPRuntime().emitDeclareTargetFunction(D
, GV
);
6006 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD
) {
6007 const auto *D
= cast
<ValueDecl
>(GD
.getDecl());
6008 const AliasAttr
*AA
= D
->getAttr
<AliasAttr
>();
6009 assert(AA
&& "Not an alias?");
6011 StringRef MangledName
= getMangledName(GD
);
6013 if (AA
->getAliasee() == MangledName
) {
6014 Diags
.Report(AA
->getLocation(), diag::err_cyclic_alias
) << 0;
6018 // If there is a definition in the module, then it wins over the alias.
6019 // This is dubious, but allow it to be safe. Just ignore the alias.
6020 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
6021 if (Entry
&& !Entry
->isDeclaration())
6024 Aliases
.push_back(GD
);
6026 llvm::Type
*DeclTy
= getTypes().ConvertTypeForMem(D
->getType());
6028 // Create a reference to the named value. This ensures that it is emitted
6029 // if a deferred decl.
6030 llvm::Constant
*Aliasee
;
6031 llvm::GlobalValue::LinkageTypes LT
;
6032 if (isa
<llvm::FunctionType
>(DeclTy
)) {
6033 Aliasee
= GetOrCreateLLVMFunction(AA
->getAliasee(), DeclTy
, GD
,
6034 /*ForVTable=*/false);
6035 LT
= getFunctionLinkage(GD
);
6037 Aliasee
= GetOrCreateLLVMGlobal(AA
->getAliasee(), DeclTy
, LangAS::Default
,
6039 if (const auto *VD
= dyn_cast
<VarDecl
>(GD
.getDecl()))
6040 LT
= getLLVMLinkageVarDefinition(VD
);
6042 LT
= getFunctionLinkage(GD
);
6045 // Create the new alias itself, but don't set a name yet.
6046 unsigned AS
= Aliasee
->getType()->getPointerAddressSpace();
6048 llvm::GlobalAlias::create(DeclTy
, AS
, LT
, "", Aliasee
, &getModule());
6051 if (GA
->getAliasee() == Entry
) {
6052 Diags
.Report(AA
->getLocation(), diag::err_cyclic_alias
) << 0;
6056 assert(Entry
->isDeclaration());
6058 // If there is a declaration in the module, then we had an extern followed
6059 // by the alias, as in:
6060 // extern int test6();
6062 // int test6() __attribute__((alias("test7")));
6064 // Remove it and replace uses of it with the alias.
6065 GA
->takeName(Entry
);
6067 Entry
->replaceAllUsesWith(GA
);
6068 Entry
->eraseFromParent();
6070 GA
->setName(MangledName
);
6073 // Set attributes which are particular to an alias; this is a
6074 // specialization of the attributes which may be set on a global
6075 // variable/function.
6076 if (D
->hasAttr
<WeakAttr
>() || D
->hasAttr
<WeakRefAttr
>() ||
6077 D
->isWeakImported()) {
6078 GA
->setLinkage(llvm::Function::WeakAnyLinkage
);
6081 if (const auto *VD
= dyn_cast
<VarDecl
>(D
))
6082 if (VD
->getTLSKind())
6083 setTLSMode(GA
, *VD
);
6085 SetCommonAttributes(GD
, GA
);
6087 // Emit global alias debug information.
6088 if (isa
<VarDecl
>(D
))
6089 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6090 DI
->EmitGlobalAlias(cast
<llvm::GlobalValue
>(GA
->getAliasee()->stripPointerCasts()), GD
);
6093 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD
) {
6094 const auto *D
= cast
<ValueDecl
>(GD
.getDecl());
6095 const IFuncAttr
*IFA
= D
->getAttr
<IFuncAttr
>();
6096 assert(IFA
&& "Not an ifunc?");
6098 StringRef MangledName
= getMangledName(GD
);
6100 if (IFA
->getResolver() == MangledName
) {
6101 Diags
.Report(IFA
->getLocation(), diag::err_cyclic_alias
) << 1;
6105 // Report an error if some definition overrides ifunc.
6106 llvm::GlobalValue
*Entry
= GetGlobalValue(MangledName
);
6107 if (Entry
&& !Entry
->isDeclaration()) {
6109 if (lookupRepresentativeDecl(MangledName
, OtherGD
) &&
6110 DiagnosedConflictingDefinitions
.insert(GD
).second
) {
6111 Diags
.Report(D
->getLocation(), diag::err_duplicate_mangled_name
)
6113 Diags
.Report(OtherGD
.getDecl()->getLocation(),
6114 diag::note_previous_definition
);
6119 Aliases
.push_back(GD
);
6121 // The resolver might not be visited yet. Specify a dummy non-function type to
6122 // indicate IsIncompleteFunction. Either the type is ignored (if the resolver
6123 // was emitted) or the whole function will be replaced (if the resolver has
6124 // not been emitted).
6125 llvm::Constant
*Resolver
=
6126 GetOrCreateLLVMFunction(IFA
->getResolver(), VoidTy
, {},
6127 /*ForVTable=*/false);
6128 llvm::Type
*DeclTy
= getTypes().ConvertTypeForMem(D
->getType());
6129 llvm::GlobalIFunc
*GIF
=
6130 llvm::GlobalIFunc::create(DeclTy
, 0, llvm::Function::ExternalLinkage
,
6131 "", Resolver
, &getModule());
6133 if (GIF
->getResolver() == Entry
) {
6134 Diags
.Report(IFA
->getLocation(), diag::err_cyclic_alias
) << 1;
6137 assert(Entry
->isDeclaration());
6139 // If there is a declaration in the module, then we had an extern followed
6140 // by the ifunc, as in:
6141 // extern int test();
6143 // int test() __attribute__((ifunc("resolver")));
6145 // Remove it and replace uses of it with the ifunc.
6146 GIF
->takeName(Entry
);
6148 Entry
->replaceAllUsesWith(GIF
);
6149 Entry
->eraseFromParent();
6151 GIF
->setName(MangledName
);
6152 SetCommonAttributes(GD
, GIF
);
6155 llvm::Function
*CodeGenModule::getIntrinsic(unsigned IID
,
6156 ArrayRef
<llvm::Type
*> Tys
) {
6157 return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID
)IID
,
6161 static llvm::StringMapEntry
<llvm::GlobalVariable
*> &
6162 GetConstantCFStringEntry(llvm::StringMap
<llvm::GlobalVariable
*> &Map
,
6163 const StringLiteral
*Literal
, bool TargetIsLSB
,
6164 bool &IsUTF16
, unsigned &StringLength
) {
6165 StringRef String
= Literal
->getString();
6166 unsigned NumBytes
= String
.size();
6168 // Check for simple case.
6169 if (!Literal
->containsNonAsciiOrNull()) {
6170 StringLength
= NumBytes
;
6171 return *Map
.insert(std::make_pair(String
, nullptr)).first
;
6174 // Otherwise, convert the UTF8 literals into a string of shorts.
6177 SmallVector
<llvm::UTF16
, 128> ToBuf(NumBytes
+ 1); // +1 for ending nulls.
6178 const llvm::UTF8
*FromPtr
= (const llvm::UTF8
*)String
.data();
6179 llvm::UTF16
*ToPtr
= &ToBuf
[0];
6181 (void)llvm::ConvertUTF8toUTF16(&FromPtr
, FromPtr
+ NumBytes
, &ToPtr
,
6182 ToPtr
+ NumBytes
, llvm::strictConversion
);
6184 // ConvertUTF8toUTF16 returns the length in ToPtr.
6185 StringLength
= ToPtr
- &ToBuf
[0];
6187 // Add an explicit null.
6189 return *Map
.insert(std::make_pair(
6190 StringRef(reinterpret_cast<const char *>(ToBuf
.data()),
6191 (StringLength
+ 1) * 2),
6196 CodeGenModule::GetAddrOfConstantCFString(const StringLiteral
*Literal
) {
6197 unsigned StringLength
= 0;
6198 bool isUTF16
= false;
6199 llvm::StringMapEntry
<llvm::GlobalVariable
*> &Entry
=
6200 GetConstantCFStringEntry(CFConstantStringMap
, Literal
,
6201 getDataLayout().isLittleEndian(), isUTF16
,
6204 if (auto *C
= Entry
.second
)
6205 return ConstantAddress(
6206 C
, C
->getValueType(), CharUnits::fromQuantity(C
->getAlignment()));
6208 const ASTContext
&Context
= getContext();
6209 const llvm::Triple
&Triple
= getTriple();
6211 const auto CFRuntime
= getLangOpts().CFRuntime
;
6212 const bool IsSwiftABI
=
6213 static_cast<unsigned>(CFRuntime
) >=
6214 static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift
);
6215 const bool IsSwift4_1
= CFRuntime
== LangOptions::CoreFoundationABI::Swift4_1
;
6217 // If we don't already have it, get __CFConstantStringClassReference.
6218 if (!CFConstantStringClassRef
) {
6219 const char *CFConstantStringClassName
= "__CFConstantStringClassReference";
6220 llvm::Type
*Ty
= getTypes().ConvertType(getContext().IntTy
);
6221 Ty
= llvm::ArrayType::get(Ty
, 0);
6223 switch (CFRuntime
) {
6225 case LangOptions::CoreFoundationABI::Swift
: [[fallthrough
]];
6226 case LangOptions::CoreFoundationABI::Swift5_0
:
6227 CFConstantStringClassName
=
6228 Triple
.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
6229 : "$s10Foundation19_NSCFConstantStringCN";
6232 case LangOptions::CoreFoundationABI::Swift4_2
:
6233 CFConstantStringClassName
=
6234 Triple
.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
6235 : "$S10Foundation19_NSCFConstantStringCN";
6238 case LangOptions::CoreFoundationABI::Swift4_1
:
6239 CFConstantStringClassName
=
6240 Triple
.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
6241 : "__T010Foundation19_NSCFConstantStringCN";
6246 llvm::Constant
*C
= CreateRuntimeVariable(Ty
, CFConstantStringClassName
);
6248 if (Triple
.isOSBinFormatELF() || Triple
.isOSBinFormatCOFF()) {
6249 llvm::GlobalValue
*GV
= nullptr;
6251 if ((GV
= dyn_cast
<llvm::GlobalValue
>(C
))) {
6252 IdentifierInfo
&II
= Context
.Idents
.get(GV
->getName());
6253 TranslationUnitDecl
*TUDecl
= Context
.getTranslationUnitDecl();
6254 DeclContext
*DC
= TranslationUnitDecl::castToDeclContext(TUDecl
);
6256 const VarDecl
*VD
= nullptr;
6257 for (const auto *Result
: DC
->lookup(&II
))
6258 if ((VD
= dyn_cast
<VarDecl
>(Result
)))
6261 if (Triple
.isOSBinFormatELF()) {
6263 GV
->setLinkage(llvm::GlobalValue::ExternalLinkage
);
6265 GV
->setLinkage(llvm::GlobalValue::ExternalLinkage
);
6266 if (!VD
|| !VD
->hasAttr
<DLLExportAttr
>())
6267 GV
->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass
);
6269 GV
->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass
);
6276 // Decay array -> ptr
6277 CFConstantStringClassRef
=
6278 IsSwiftABI
? llvm::ConstantExpr::getPtrToInt(C
, Ty
) : C
;
6281 QualType CFTy
= Context
.getCFConstantStringType();
6283 auto *STy
= cast
<llvm::StructType
>(getTypes().ConvertType(CFTy
));
6285 ConstantInitBuilder
Builder(*this);
6286 auto Fields
= Builder
.beginStruct(STy
);
6289 Fields
.add(cast
<llvm::Constant
>(CFConstantStringClassRef
));
6293 Fields
.addInt(IntPtrTy
, IsSwift4_1
? 0x05 : 0x01);
6294 Fields
.addInt(Int64Ty
, isUTF16
? 0x07d0 : 0x07c8);
6296 Fields
.addInt(IntTy
, isUTF16
? 0x07d0 : 0x07C8);
6300 llvm::Constant
*C
= nullptr;
6302 auto Arr
= llvm::ArrayRef(
6303 reinterpret_cast<uint16_t *>(const_cast<char *>(Entry
.first().data())),
6304 Entry
.first().size() / 2);
6305 C
= llvm::ConstantDataArray::get(VMContext
, Arr
);
6307 C
= llvm::ConstantDataArray::getString(VMContext
, Entry
.first());
6310 // Note: -fwritable-strings doesn't make the backing store strings of
6311 // CFStrings writable.
6313 new llvm::GlobalVariable(getModule(), C
->getType(), /*isConstant=*/true,
6314 llvm::GlobalValue::PrivateLinkage
, C
, ".str");
6315 GV
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
6316 // Don't enforce the target's minimum global alignment, since the only use
6317 // of the string is via this class initializer.
6318 CharUnits Align
= isUTF16
? Context
.getTypeAlignInChars(Context
.ShortTy
)
6319 : Context
.getTypeAlignInChars(Context
.CharTy
);
6320 GV
->setAlignment(Align
.getAsAlign());
6322 // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
6323 // Without it LLVM can merge the string with a non unnamed_addr one during
6324 // LTO. Doing that changes the section it ends in, which surprises ld64.
6325 if (Triple
.isOSBinFormatMachO())
6326 GV
->setSection(isUTF16
? "__TEXT,__ustring"
6327 : "__TEXT,__cstring,cstring_literals");
6328 // Make sure the literal ends up in .rodata to allow for safe ICF and for
6329 // the static linker to adjust permissions to read-only later on.
6330 else if (Triple
.isOSBinFormatELF())
6331 GV
->setSection(".rodata");
6337 llvm::IntegerType
*LengthTy
=
6338 llvm::IntegerType::get(getModule().getContext(),
6339 Context
.getTargetInfo().getLongWidth());
6341 if (CFRuntime
== LangOptions::CoreFoundationABI::Swift4_1
||
6342 CFRuntime
== LangOptions::CoreFoundationABI::Swift4_2
)
6345 LengthTy
= IntPtrTy
;
6347 Fields
.addInt(LengthTy
, StringLength
);
6349 // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
6350 // properly aligned on 32-bit platforms.
6351 CharUnits Alignment
=
6352 IsSwiftABI
? Context
.toCharUnitsFromBits(64) : getPointerAlign();
6355 GV
= Fields
.finishAndCreateGlobal("_unnamed_cfstring_", Alignment
,
6356 /*isConstant=*/false,
6357 llvm::GlobalVariable::PrivateLinkage
);
6358 GV
->addAttribute("objc_arc_inert");
6359 switch (Triple
.getObjectFormat()) {
6360 case llvm::Triple::UnknownObjectFormat
:
6361 llvm_unreachable("unknown file format");
6362 case llvm::Triple::DXContainer
:
6363 case llvm::Triple::GOFF
:
6364 case llvm::Triple::SPIRV
:
6365 case llvm::Triple::XCOFF
:
6366 llvm_unreachable("unimplemented");
6367 case llvm::Triple::COFF
:
6368 case llvm::Triple::ELF
:
6369 case llvm::Triple::Wasm
:
6370 GV
->setSection("cfstring");
6372 case llvm::Triple::MachO
:
6373 GV
->setSection("__DATA,__cfstring");
6378 return ConstantAddress(GV
, GV
->getValueType(), Alignment
);
6381 bool CodeGenModule::getExpressionLocationsEnabled() const {
6382 return !CodeGenOpts
.EmitCodeView
|| CodeGenOpts
.DebugColumnInfo
;
6385 QualType
CodeGenModule::getObjCFastEnumerationStateType() {
6386 if (ObjCFastEnumerationStateType
.isNull()) {
6387 RecordDecl
*D
= Context
.buildImplicitRecord("__objcFastEnumerationState");
6388 D
->startDefinition();
6390 QualType FieldTypes
[] = {
6391 Context
.UnsignedLongTy
, Context
.getPointerType(Context
.getObjCIdType()),
6392 Context
.getPointerType(Context
.UnsignedLongTy
),
6393 Context
.getConstantArrayType(Context
.UnsignedLongTy
, llvm::APInt(32, 5),
6394 nullptr, ArraySizeModifier::Normal
, 0)};
6396 for (size_t i
= 0; i
< 4; ++i
) {
6397 FieldDecl
*Field
= FieldDecl::Create(Context
,
6400 SourceLocation(), nullptr,
6401 FieldTypes
[i
], /*TInfo=*/nullptr,
6402 /*BitWidth=*/nullptr,
6405 Field
->setAccess(AS_public
);
6409 D
->completeDefinition();
6410 ObjCFastEnumerationStateType
= Context
.getTagDeclType(D
);
6413 return ObjCFastEnumerationStateType
;
6417 CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral
*E
) {
6418 assert(!E
->getType()->isPointerType() && "Strings are always arrays");
6420 // Don't emit it as the address of the string, emit the string data itself
6421 // as an inline array.
6422 if (E
->getCharByteWidth() == 1) {
6423 SmallString
<64> Str(E
->getString());
6425 // Resize the string to the right size, which is indicated by its type.
6426 const ConstantArrayType
*CAT
= Context
.getAsConstantArrayType(E
->getType());
6427 assert(CAT
&& "String literal not of constant array type!");
6428 Str
.resize(CAT
->getZExtSize());
6429 return llvm::ConstantDataArray::getString(VMContext
, Str
, false);
6432 auto *AType
= cast
<llvm::ArrayType
>(getTypes().ConvertType(E
->getType()));
6433 llvm::Type
*ElemTy
= AType
->getElementType();
6434 unsigned NumElements
= AType
->getNumElements();
6436 // Wide strings have either 2-byte or 4-byte elements.
6437 if (ElemTy
->getPrimitiveSizeInBits() == 16) {
6438 SmallVector
<uint16_t, 32> Elements
;
6439 Elements
.reserve(NumElements
);
6441 for(unsigned i
= 0, e
= E
->getLength(); i
!= e
; ++i
)
6442 Elements
.push_back(E
->getCodeUnit(i
));
6443 Elements
.resize(NumElements
);
6444 return llvm::ConstantDataArray::get(VMContext
, Elements
);
6447 assert(ElemTy
->getPrimitiveSizeInBits() == 32);
6448 SmallVector
<uint32_t, 32> Elements
;
6449 Elements
.reserve(NumElements
);
6451 for(unsigned i
= 0, e
= E
->getLength(); i
!= e
; ++i
)
6452 Elements
.push_back(E
->getCodeUnit(i
));
6453 Elements
.resize(NumElements
);
6454 return llvm::ConstantDataArray::get(VMContext
, Elements
);
6457 static llvm::GlobalVariable
*
6458 GenerateStringLiteral(llvm::Constant
*C
, llvm::GlobalValue::LinkageTypes LT
,
6459 CodeGenModule
&CGM
, StringRef GlobalName
,
6460 CharUnits Alignment
) {
6461 unsigned AddrSpace
= CGM
.getContext().getTargetAddressSpace(
6462 CGM
.GetGlobalConstantAddressSpace());
6464 llvm::Module
&M
= CGM
.getModule();
6465 // Create a global variable for this string
6466 auto *GV
= new llvm::GlobalVariable(
6467 M
, C
->getType(), !CGM
.getLangOpts().WritableStrings
, LT
, C
, GlobalName
,
6468 nullptr, llvm::GlobalVariable::NotThreadLocal
, AddrSpace
);
6469 GV
->setAlignment(Alignment
.getAsAlign());
6470 GV
->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global
);
6471 if (GV
->isWeakForLinker()) {
6472 assert(CGM
.supportsCOMDAT() && "Only COFF uses weak string literals");
6473 GV
->setComdat(M
.getOrInsertComdat(GV
->getName()));
6475 CGM
.setDSOLocal(GV
);
6480 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
6481 /// constant array for the given string literal.
6483 CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral
*S
,
6485 CharUnits Alignment
=
6486 getContext().getAlignOfGlobalVarInChars(S
->getType(), /*VD=*/nullptr);
6488 llvm::Constant
*C
= GetConstantArrayFromStringLiteral(S
);
6489 llvm::GlobalVariable
**Entry
= nullptr;
6490 if (!LangOpts
.WritableStrings
) {
6491 Entry
= &ConstantStringMap
[C
];
6492 if (auto GV
= *Entry
) {
6493 if (uint64_t(Alignment
.getQuantity()) > GV
->getAlignment())
6494 GV
->setAlignment(Alignment
.getAsAlign());
6495 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV
),
6496 GV
->getValueType(), Alignment
);
6500 SmallString
<256> MangledNameBuffer
;
6501 StringRef GlobalVariableName
;
6502 llvm::GlobalValue::LinkageTypes LT
;
6504 // Mangle the string literal if that's how the ABI merges duplicate strings.
6505 // Don't do it if they are writable, since we don't want writes in one TU to
6506 // affect strings in another.
6507 if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S
) &&
6508 !LangOpts
.WritableStrings
) {
6509 llvm::raw_svector_ostream
Out(MangledNameBuffer
);
6510 getCXXABI().getMangleContext().mangleStringLiteral(S
, Out
);
6511 LT
= llvm::GlobalValue::LinkOnceODRLinkage
;
6512 GlobalVariableName
= MangledNameBuffer
;
6514 LT
= llvm::GlobalValue::PrivateLinkage
;
6515 GlobalVariableName
= Name
;
6518 auto GV
= GenerateStringLiteral(C
, LT
, *this, GlobalVariableName
, Alignment
);
6520 CGDebugInfo
*DI
= getModuleDebugInfo();
6521 if (DI
&& getCodeGenOpts().hasReducedDebugInfo())
6522 DI
->AddStringLiteralDebugInfo(GV
, S
);
6527 SanitizerMD
->reportGlobal(GV
, S
->getStrTokenLoc(0), "<string literal>");
6529 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV
),
6530 GV
->getValueType(), Alignment
);
6533 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
6534 /// array for the given ObjCEncodeExpr node.
6536 CodeGenModule::GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr
*E
) {
6538 getContext().getObjCEncodingForType(E
->getEncodedType(), Str
);
6540 return GetAddrOfConstantCString(Str
);
6543 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
6544 /// the literal and a terminating '\0' character.
6545 /// The result has pointer to array type.
6546 ConstantAddress
CodeGenModule::GetAddrOfConstantCString(
6547 const std::string
&Str
, const char *GlobalName
) {
6548 StringRef
StrWithNull(Str
.c_str(), Str
.size() + 1);
6549 CharUnits Alignment
= getContext().getAlignOfGlobalVarInChars(
6550 getContext().CharTy
, /*VD=*/nullptr);
6553 llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull
, false);
6555 // Don't share any string literals if strings aren't constant.
6556 llvm::GlobalVariable
**Entry
= nullptr;
6557 if (!LangOpts
.WritableStrings
) {
6558 Entry
= &ConstantStringMap
[C
];
6559 if (auto GV
= *Entry
) {
6560 if (uint64_t(Alignment
.getQuantity()) > GV
->getAlignment())
6561 GV
->setAlignment(Alignment
.getAsAlign());
6562 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV
),
6563 GV
->getValueType(), Alignment
);
6567 // Get the default prefix if a name wasn't specified.
6569 GlobalName
= ".str";
6570 // Create a global variable for this.
6571 auto GV
= GenerateStringLiteral(C
, llvm::GlobalValue::PrivateLinkage
, *this,
6572 GlobalName
, Alignment
);
6576 return ConstantAddress(castStringLiteralToDefaultAddressSpace(*this, GV
),
6577 GV
->getValueType(), Alignment
);
6580 ConstantAddress
CodeGenModule::GetAddrOfGlobalTemporary(
6581 const MaterializeTemporaryExpr
*E
, const Expr
*Init
) {
6582 assert((E
->getStorageDuration() == SD_Static
||
6583 E
->getStorageDuration() == SD_Thread
) && "not a global temporary");
6584 const auto *VD
= cast
<VarDecl
>(E
->getExtendingDecl());
6586 // If we're not materializing a subobject of the temporary, keep the
6587 // cv-qualifiers from the type of the MaterializeTemporaryExpr.
6588 QualType MaterializedType
= Init
->getType();
6589 if (Init
== E
->getSubExpr())
6590 MaterializedType
= E
->getType();
6592 CharUnits Align
= getContext().getTypeAlignInChars(MaterializedType
);
6594 auto InsertResult
= MaterializedGlobalTemporaryMap
.insert({E
, nullptr});
6595 if (!InsertResult
.second
) {
6596 // We've seen this before: either we already created it or we're in the
6597 // process of doing so.
6598 if (!InsertResult
.first
->second
) {
6599 // We recursively re-entered this function, probably during emission of
6600 // the initializer. Create a placeholder. We'll clean this up in the
6601 // outer call, at the end of this function.
6602 llvm::Type
*Type
= getTypes().ConvertTypeForMem(MaterializedType
);
6603 InsertResult
.first
->second
= new llvm::GlobalVariable(
6604 getModule(), Type
, false, llvm::GlobalVariable::InternalLinkage
,
6607 return ConstantAddress(InsertResult
.first
->second
,
6608 llvm::cast
<llvm::GlobalVariable
>(
6609 InsertResult
.first
->second
->stripPointerCasts())
6614 // FIXME: If an externally-visible declaration extends multiple temporaries,
6615 // we need to give each temporary the same name in every translation unit (and
6616 // we also need to make the temporaries externally-visible).
6617 SmallString
<256> Name
;
6618 llvm::raw_svector_ostream
Out(Name
);
6619 getCXXABI().getMangleContext().mangleReferenceTemporary(
6620 VD
, E
->getManglingNumber(), Out
);
6622 APValue
*Value
= nullptr;
6623 if (E
->getStorageDuration() == SD_Static
&& VD
->evaluateValue()) {
6624 // If the initializer of the extending declaration is a constant
6625 // initializer, we should have a cached constant initializer for this
6626 // temporary. Note that this might have a different value from the value
6627 // computed by evaluating the initializer if the surrounding constant
6628 // expression modifies the temporary.
6629 Value
= E
->getOrCreateValue(false);
6632 // Try evaluating it now, it might have a constant initializer.
6633 Expr::EvalResult EvalResult
;
6634 if (!Value
&& Init
->EvaluateAsRValue(EvalResult
, getContext()) &&
6635 !EvalResult
.hasSideEffects())
6636 Value
= &EvalResult
.Val
;
6638 LangAS AddrSpace
= GetGlobalVarAddressSpace(VD
);
6640 std::optional
<ConstantEmitter
> emitter
;
6641 llvm::Constant
*InitialValue
= nullptr;
6642 bool Constant
= false;
6645 // The temporary has a constant initializer, use it.
6646 emitter
.emplace(*this);
6647 InitialValue
= emitter
->emitForInitializer(*Value
, AddrSpace
,
6650 MaterializedType
.isConstantStorage(getContext(), /*ExcludeCtor*/ Value
,
6651 /*ExcludeDtor*/ false);
6652 Type
= InitialValue
->getType();
6654 // No initializer, the initialization will be provided when we
6655 // initialize the declaration which performed lifetime extension.
6656 Type
= getTypes().ConvertTypeForMem(MaterializedType
);
6659 // Create a global variable for this lifetime-extended temporary.
6660 llvm::GlobalValue::LinkageTypes Linkage
= getLLVMLinkageVarDefinition(VD
);
6661 if (Linkage
== llvm::GlobalVariable::ExternalLinkage
) {
6662 const VarDecl
*InitVD
;
6663 if (VD
->isStaticDataMember() && VD
->getAnyInitializer(InitVD
) &&
6664 isa
<CXXRecordDecl
>(InitVD
->getLexicalDeclContext())) {
6665 // Temporaries defined inside a class get linkonce_odr linkage because the
6666 // class can be defined in multiple translation units.
6667 Linkage
= llvm::GlobalVariable::LinkOnceODRLinkage
;
6669 // There is no need for this temporary to have external linkage if the
6670 // VarDecl has external linkage.
6671 Linkage
= llvm::GlobalVariable::InternalLinkage
;
6674 auto TargetAS
= getContext().getTargetAddressSpace(AddrSpace
);
6675 auto *GV
= new llvm::GlobalVariable(
6676 getModule(), Type
, Constant
, Linkage
, InitialValue
, Name
.c_str(),
6677 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal
, TargetAS
);
6678 if (emitter
) emitter
->finalize(GV
);
6679 // Don't assign dllimport or dllexport to local linkage globals.
6680 if (!llvm::GlobalValue::isLocalLinkage(Linkage
)) {
6681 setGVProperties(GV
, VD
);
6682 if (GV
->getDLLStorageClass() == llvm::GlobalVariable::DLLExportStorageClass
)
6683 // The reference temporary should never be dllexport.
6684 GV
->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass
);
6686 GV
->setAlignment(Align
.getAsAlign());
6687 if (supportsCOMDAT() && GV
->isWeakForLinker())
6688 GV
->setComdat(TheModule
.getOrInsertComdat(GV
->getName()));
6689 if (VD
->getTLSKind())
6690 setTLSMode(GV
, *VD
);
6691 llvm::Constant
*CV
= GV
;
6692 if (AddrSpace
!= LangAS::Default
)
6693 CV
= getTargetCodeGenInfo().performAddrSpaceCast(
6694 *this, GV
, AddrSpace
, LangAS::Default
,
6695 llvm::PointerType::get(
6697 getContext().getTargetAddressSpace(LangAS::Default
)));
6699 // Update the map with the new temporary. If we created a placeholder above,
6700 // replace it with the new global now.
6701 llvm::Constant
*&Entry
= MaterializedGlobalTemporaryMap
[E
];
6703 Entry
->replaceAllUsesWith(CV
);
6704 llvm::cast
<llvm::GlobalVariable
>(Entry
)->eraseFromParent();
6708 return ConstantAddress(CV
, Type
, Align
);
6711 /// EmitObjCPropertyImplementations - Emit information for synthesized
6712 /// properties for an implementation.
6713 void CodeGenModule::EmitObjCPropertyImplementations(const
6714 ObjCImplementationDecl
*D
) {
6715 for (const auto *PID
: D
->property_impls()) {
6716 // Dynamic is just for type-checking.
6717 if (PID
->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize
) {
6718 ObjCPropertyDecl
*PD
= PID
->getPropertyDecl();
6720 // Determine which methods need to be implemented, some may have
6721 // been overridden. Note that ::isPropertyAccessor is not the method
6722 // we want, that just indicates if the decl came from a
6723 // property. What we want to know is if the method is defined in
6724 // this implementation.
6725 auto *Getter
= PID
->getGetterMethodDecl();
6726 if (!Getter
|| Getter
->isSynthesizedAccessorStub())
6727 CodeGenFunction(*this).GenerateObjCGetter(
6728 const_cast<ObjCImplementationDecl
*>(D
), PID
);
6729 auto *Setter
= PID
->getSetterMethodDecl();
6730 if (!PD
->isReadOnly() && (!Setter
|| Setter
->isSynthesizedAccessorStub()))
6731 CodeGenFunction(*this).GenerateObjCSetter(
6732 const_cast<ObjCImplementationDecl
*>(D
), PID
);
6737 static bool needsDestructMethod(ObjCImplementationDecl
*impl
) {
6738 const ObjCInterfaceDecl
*iface
= impl
->getClassInterface();
6739 for (const ObjCIvarDecl
*ivar
= iface
->all_declared_ivar_begin();
6740 ivar
; ivar
= ivar
->getNextIvar())
6741 if (ivar
->getType().isDestructedType())
6747 static bool AllTrivialInitializers(CodeGenModule
&CGM
,
6748 ObjCImplementationDecl
*D
) {
6749 CodeGenFunction
CGF(CGM
);
6750 for (ObjCImplementationDecl::init_iterator B
= D
->init_begin(),
6751 E
= D
->init_end(); B
!= E
; ++B
) {
6752 CXXCtorInitializer
*CtorInitExp
= *B
;
6753 Expr
*Init
= CtorInitExp
->getInit();
6754 if (!CGF
.isTrivialInitializer(Init
))
6760 /// EmitObjCIvarInitializations - Emit information for ivar initialization
6761 /// for an implementation.
6762 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl
*D
) {
6763 // We might need a .cxx_destruct even if we don't have any ivar initializers.
6764 if (needsDestructMethod(D
)) {
6765 const IdentifierInfo
*II
= &getContext().Idents
.get(".cxx_destruct");
6766 Selector cxxSelector
= getContext().Selectors
.getSelector(0, &II
);
6767 ObjCMethodDecl
*DTORMethod
= ObjCMethodDecl::Create(
6768 getContext(), D
->getLocation(), D
->getLocation(), cxxSelector
,
6769 getContext().VoidTy
, nullptr, D
,
6770 /*isInstance=*/true, /*isVariadic=*/false,
6771 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6772 /*isImplicitlyDeclared=*/true,
6773 /*isDefined=*/false, ObjCImplementationControl::Required
);
6774 D
->addInstanceMethod(DTORMethod
);
6775 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D
, DTORMethod
, false);
6776 D
->setHasDestructors(true);
6779 // If the implementation doesn't have any ivar initializers, we don't need
6780 // a .cxx_construct.
6781 if (D
->getNumIvarInitializers() == 0 ||
6782 AllTrivialInitializers(*this, D
))
6785 const IdentifierInfo
*II
= &getContext().Idents
.get(".cxx_construct");
6786 Selector cxxSelector
= getContext().Selectors
.getSelector(0, &II
);
6787 // The constructor returns 'self'.
6788 ObjCMethodDecl
*CTORMethod
= ObjCMethodDecl::Create(
6789 getContext(), D
->getLocation(), D
->getLocation(), cxxSelector
,
6790 getContext().getObjCIdType(), nullptr, D
, /*isInstance=*/true,
6791 /*isVariadic=*/false,
6792 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
6793 /*isImplicitlyDeclared=*/true,
6794 /*isDefined=*/false, ObjCImplementationControl::Required
);
6795 D
->addInstanceMethod(CTORMethod
);
6796 CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D
, CTORMethod
, true);
6797 D
->setHasNonZeroConstructors(true);
6800 // EmitLinkageSpec - Emit all declarations in a linkage spec.
6801 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl
*LSD
) {
6802 if (LSD
->getLanguage() != LinkageSpecLanguageIDs::C
&&
6803 LSD
->getLanguage() != LinkageSpecLanguageIDs::CXX
) {
6804 ErrorUnsupported(LSD
, "linkage spec");
6808 EmitDeclContext(LSD
);
6811 void CodeGenModule::EmitTopLevelStmt(const TopLevelStmtDecl
*D
) {
6812 // Device code should not be at top level.
6813 if (LangOpts
.CUDA
&& LangOpts
.CUDAIsDevice
)
6816 std::unique_ptr
<CodeGenFunction
> &CurCGF
=
6817 GlobalTopLevelStmtBlockInFlight
.first
;
6819 // We emitted a top-level stmt but after it there is initialization.
6820 // Stop squashing the top-level stmts into a single function.
6821 if (CurCGF
&& CXXGlobalInits
.back() != CurCGF
->CurFn
) {
6822 CurCGF
->FinishFunction(D
->getEndLoc());
6827 // void __stmts__N(void)
6828 // FIXME: Ask the ABI name mangler to pick a name.
6829 std::string Name
= "__stmts__" + llvm::utostr(CXXGlobalInits
.size());
6830 FunctionArgList Args
;
6831 QualType RetTy
= getContext().VoidTy
;
6832 const CGFunctionInfo
&FnInfo
=
6833 getTypes().arrangeBuiltinFunctionDeclaration(RetTy
, Args
);
6834 llvm::FunctionType
*FnTy
= getTypes().GetFunctionType(FnInfo
);
6835 llvm::Function
*Fn
= llvm::Function::Create(
6836 FnTy
, llvm::GlobalValue::InternalLinkage
, Name
, &getModule());
6838 CurCGF
.reset(new CodeGenFunction(*this));
6839 GlobalTopLevelStmtBlockInFlight
.second
= D
;
6840 CurCGF
->StartFunction(GlobalDecl(), RetTy
, Fn
, FnInfo
, Args
,
6841 D
->getBeginLoc(), D
->getBeginLoc());
6842 CXXGlobalInits
.push_back(Fn
);
6845 CurCGF
->EmitStmt(D
->getStmt());
6848 void CodeGenModule::EmitDeclContext(const DeclContext
*DC
) {
6849 for (auto *I
: DC
->decls()) {
6850 // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
6851 // are themselves considered "top-level", so EmitTopLevelDecl on an
6852 // ObjCImplDecl does not recursively visit them. We need to do that in
6853 // case they're nested inside another construct (LinkageSpecDecl /
6854 // ExportDecl) that does stop them from being considered "top-level".
6855 if (auto *OID
= dyn_cast
<ObjCImplDecl
>(I
)) {
6856 for (auto *M
: OID
->methods())
6857 EmitTopLevelDecl(M
);
6860 EmitTopLevelDecl(I
);
6864 /// EmitTopLevelDecl - Emit code for a single top level declaration.
6865 void CodeGenModule::EmitTopLevelDecl(Decl
*D
) {
6866 // Ignore dependent declarations.
6867 if (D
->isTemplated())
6870 // Consteval function shouldn't be emitted.
6871 if (auto *FD
= dyn_cast
<FunctionDecl
>(D
); FD
&& FD
->isImmediateFunction())
6874 switch (D
->getKind()) {
6875 case Decl::CXXConversion
:
6876 case Decl::CXXMethod
:
6877 case Decl::Function
:
6878 EmitGlobal(cast
<FunctionDecl
>(D
));
6879 // Always provide some coverage mapping
6880 // even for the functions that aren't emitted.
6881 AddDeferredUnusedCoverageMapping(D
);
6884 case Decl::CXXDeductionGuide
:
6885 // Function-like, but does not result in code emission.
6889 case Decl::Decomposition
:
6890 case Decl::VarTemplateSpecialization
:
6891 EmitGlobal(cast
<VarDecl
>(D
));
6892 if (auto *DD
= dyn_cast
<DecompositionDecl
>(D
))
6893 for (auto *B
: DD
->bindings())
6894 if (auto *HD
= B
->getHoldingVar())
6898 // Indirect fields from global anonymous structs and unions can be
6899 // ignored; only the actual variable requires IR gen support.
6900 case Decl::IndirectField
:
6904 case Decl::Namespace
:
6905 EmitDeclContext(cast
<NamespaceDecl
>(D
));
6907 case Decl::ClassTemplateSpecialization
: {
6908 const auto *Spec
= cast
<ClassTemplateSpecializationDecl
>(D
);
6909 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6910 if (Spec
->getSpecializationKind() ==
6911 TSK_ExplicitInstantiationDefinition
&&
6912 Spec
->hasDefinition())
6913 DI
->completeTemplateDefinition(*Spec
);
6915 case Decl::CXXRecord
: {
6916 CXXRecordDecl
*CRD
= cast
<CXXRecordDecl
>(D
);
6917 if (CGDebugInfo
*DI
= getModuleDebugInfo()) {
6918 if (CRD
->hasDefinition())
6919 DI
->EmitAndRetainType(getContext().getRecordType(cast
<RecordDecl
>(D
)));
6920 if (auto *ES
= D
->getASTContext().getExternalSource())
6921 if (ES
->hasExternalDefinitions(D
) == ExternalASTSource::EK_Never
)
6922 DI
->completeUnusedClass(*CRD
);
6924 // Emit any static data members, they may be definitions.
6925 for (auto *I
: CRD
->decls())
6926 if (isa
<VarDecl
>(I
) || isa
<CXXRecordDecl
>(I
))
6927 EmitTopLevelDecl(I
);
6930 // No code generation needed.
6931 case Decl::UsingShadow
:
6932 case Decl::ClassTemplate
:
6933 case Decl::VarTemplate
:
6935 case Decl::VarTemplatePartialSpecialization
:
6936 case Decl::FunctionTemplate
:
6937 case Decl::TypeAliasTemplate
:
6942 case Decl::Using
: // using X; [C++]
6943 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6944 DI
->EmitUsingDecl(cast
<UsingDecl
>(*D
));
6946 case Decl::UsingEnum
: // using enum X; [C++]
6947 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6948 DI
->EmitUsingEnumDecl(cast
<UsingEnumDecl
>(*D
));
6950 case Decl::NamespaceAlias
:
6951 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6952 DI
->EmitNamespaceAlias(cast
<NamespaceAliasDecl
>(*D
));
6954 case Decl::UsingDirective
: // using namespace X; [C++]
6955 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6956 DI
->EmitUsingDirective(cast
<UsingDirectiveDecl
>(*D
));
6958 case Decl::CXXConstructor
:
6959 getCXXABI().EmitCXXConstructors(cast
<CXXConstructorDecl
>(D
));
6961 case Decl::CXXDestructor
:
6962 getCXXABI().EmitCXXDestructors(cast
<CXXDestructorDecl
>(D
));
6965 case Decl::StaticAssert
:
6969 // Objective-C Decls
6971 // Forward declarations, no (immediate) code generation.
6972 case Decl::ObjCInterface
:
6973 case Decl::ObjCCategory
:
6976 case Decl::ObjCProtocol
: {
6977 auto *Proto
= cast
<ObjCProtocolDecl
>(D
);
6978 if (Proto
->isThisDeclarationADefinition())
6979 ObjCRuntime
->GenerateProtocol(Proto
);
6983 case Decl::ObjCCategoryImpl
:
6984 // Categories have properties but don't support synthesize so we
6985 // can ignore them here.
6986 ObjCRuntime
->GenerateCategory(cast
<ObjCCategoryImplDecl
>(D
));
6989 case Decl::ObjCImplementation
: {
6990 auto *OMD
= cast
<ObjCImplementationDecl
>(D
);
6991 EmitObjCPropertyImplementations(OMD
);
6992 EmitObjCIvarInitializations(OMD
);
6993 ObjCRuntime
->GenerateClass(OMD
);
6994 // Emit global variable debug information.
6995 if (CGDebugInfo
*DI
= getModuleDebugInfo())
6996 if (getCodeGenOpts().hasReducedDebugInfo())
6997 DI
->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
6998 OMD
->getClassInterface()), OMD
->getLocation());
7001 case Decl::ObjCMethod
: {
7002 auto *OMD
= cast
<ObjCMethodDecl
>(D
);
7003 // If this is not a prototype, emit the body.
7005 CodeGenFunction(*this).GenerateObjCMethod(OMD
);
7008 case Decl::ObjCCompatibleAlias
:
7009 ObjCRuntime
->RegisterAlias(cast
<ObjCCompatibleAliasDecl
>(D
));
7012 case Decl::PragmaComment
: {
7013 const auto *PCD
= cast
<PragmaCommentDecl
>(D
);
7014 switch (PCD
->getCommentKind()) {
7016 llvm_unreachable("unexpected pragma comment kind");
7018 AppendLinkerOptions(PCD
->getArg());
7021 AddDependentLib(PCD
->getArg());
7026 break; // We ignore all of these.
7031 case Decl::PragmaDetectMismatch
: {
7032 const auto *PDMD
= cast
<PragmaDetectMismatchDecl
>(D
);
7033 AddDetectMismatch(PDMD
->getName(), PDMD
->getValue());
7037 case Decl::LinkageSpec
:
7038 EmitLinkageSpec(cast
<LinkageSpecDecl
>(D
));
7041 case Decl::FileScopeAsm
: {
7042 // File-scope asm is ignored during device-side CUDA compilation.
7043 if (LangOpts
.CUDA
&& LangOpts
.CUDAIsDevice
)
7045 // File-scope asm is ignored during device-side OpenMP compilation.
7046 if (LangOpts
.OpenMPIsTargetDevice
)
7048 // File-scope asm is ignored during device-side SYCL compilation.
7049 if (LangOpts
.SYCLIsDevice
)
7051 auto *AD
= cast
<FileScopeAsmDecl
>(D
);
7052 getModule().appendModuleInlineAsm(AD
->getAsmString()->getString());
7056 case Decl::TopLevelStmt
:
7057 EmitTopLevelStmt(cast
<TopLevelStmtDecl
>(D
));
7060 case Decl::Import
: {
7061 auto *Import
= cast
<ImportDecl
>(D
);
7063 // If we've already imported this module, we're done.
7064 if (!ImportedModules
.insert(Import
->getImportedModule()))
7067 // Emit debug information for direct imports.
7068 if (!Import
->getImportedOwningModule()) {
7069 if (CGDebugInfo
*DI
= getModuleDebugInfo())
7070 DI
->EmitImportDecl(*Import
);
7073 // For C++ standard modules we are done - we will call the module
7074 // initializer for imported modules, and that will likewise call those for
7075 // any imports it has.
7076 if (CXX20ModuleInits
&& Import
->getImportedOwningModule() &&
7077 !Import
->getImportedOwningModule()->isModuleMapModule())
7080 // For clang C++ module map modules the initializers for sub-modules are
7083 // Find all of the submodules and emit the module initializers.
7084 llvm::SmallPtrSet
<clang::Module
*, 16> Visited
;
7085 SmallVector
<clang::Module
*, 16> Stack
;
7086 Visited
.insert(Import
->getImportedModule());
7087 Stack
.push_back(Import
->getImportedModule());
7089 while (!Stack
.empty()) {
7090 clang::Module
*Mod
= Stack
.pop_back_val();
7091 if (!EmittedModuleInitializers
.insert(Mod
).second
)
7094 for (auto *D
: Context
.getModuleInitializers(Mod
))
7095 EmitTopLevelDecl(D
);
7097 // Visit the submodules of this module.
7098 for (auto *Submodule
: Mod
->submodules()) {
7099 // Skip explicit children; they need to be explicitly imported to emit
7100 // the initializers.
7101 if (Submodule
->IsExplicit
)
7104 if (Visited
.insert(Submodule
).second
)
7105 Stack
.push_back(Submodule
);
7112 EmitDeclContext(cast
<ExportDecl
>(D
));
7115 case Decl::OMPThreadPrivate
:
7116 EmitOMPThreadPrivateDecl(cast
<OMPThreadPrivateDecl
>(D
));
7119 case Decl::OMPAllocate
:
7120 EmitOMPAllocateDecl(cast
<OMPAllocateDecl
>(D
));
7123 case Decl::OMPDeclareReduction
:
7124 EmitOMPDeclareReduction(cast
<OMPDeclareReductionDecl
>(D
));
7127 case Decl::OMPDeclareMapper
:
7128 EmitOMPDeclareMapper(cast
<OMPDeclareMapperDecl
>(D
));
7131 case Decl::OMPRequires
:
7132 EmitOMPRequiresDecl(cast
<OMPRequiresDecl
>(D
));
7136 case Decl::TypeAlias
: // using foo = bar; [C++11]
7137 if (CGDebugInfo
*DI
= getModuleDebugInfo())
7138 DI
->EmitAndRetainType(
7139 getContext().getTypedefType(cast
<TypedefNameDecl
>(D
)));
7143 if (CGDebugInfo
*DI
= getModuleDebugInfo())
7144 if (cast
<RecordDecl
>(D
)->getDefinition())
7145 DI
->EmitAndRetainType(getContext().getRecordType(cast
<RecordDecl
>(D
)));
7149 if (CGDebugInfo
*DI
= getModuleDebugInfo())
7150 if (cast
<EnumDecl
>(D
)->getDefinition())
7151 DI
->EmitAndRetainType(getContext().getEnumType(cast
<EnumDecl
>(D
)));
7154 case Decl::HLSLBuffer
:
7155 getHLSLRuntime().addBuffer(cast
<HLSLBufferDecl
>(D
));
7159 // Make sure we handled everything we should, every other kind is a
7160 // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
7161 // function. Need to recode Decl::Kind to do that easily.
7162 assert(isa
<TypeDecl
>(D
) && "Unsupported decl kind");
7167 void CodeGenModule::AddDeferredUnusedCoverageMapping(Decl
*D
) {
7168 // Do we need to generate coverage mapping?
7169 if (!CodeGenOpts
.CoverageMapping
)
7171 switch (D
->getKind()) {
7172 case Decl::CXXConversion
:
7173 case Decl::CXXMethod
:
7174 case Decl::Function
:
7175 case Decl::ObjCMethod
:
7176 case Decl::CXXConstructor
:
7177 case Decl::CXXDestructor
: {
7178 if (!cast
<FunctionDecl
>(D
)->doesThisDeclarationHaveABody())
7180 SourceManager
&SM
= getContext().getSourceManager();
7181 if (LimitedCoverage
&& SM
.getMainFileID() != SM
.getFileID(D
->getBeginLoc()))
7183 if (!llvm::coverage::SystemHeadersCoverage
&&
7184 SM
.isInSystemHeader(D
->getBeginLoc()))
7186 DeferredEmptyCoverageMappingDecls
.try_emplace(D
, true);
7194 void CodeGenModule::ClearUnusedCoverageMapping(const Decl
*D
) {
7195 // Do we need to generate coverage mapping?
7196 if (!CodeGenOpts
.CoverageMapping
)
7198 if (const auto *Fn
= dyn_cast
<FunctionDecl
>(D
)) {
7199 if (Fn
->isTemplateInstantiation())
7200 ClearUnusedCoverageMapping(Fn
->getTemplateInstantiationPattern());
7202 DeferredEmptyCoverageMappingDecls
.insert_or_assign(D
, false);
7205 void CodeGenModule::EmitDeferredUnusedCoverageMappings() {
7206 // We call takeVector() here to avoid use-after-free.
7207 // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
7208 // we deserialize function bodies to emit coverage info for them, and that
7209 // deserializes more declarations. How should we handle that case?
7210 for (const auto &Entry
: DeferredEmptyCoverageMappingDecls
.takeVector()) {
7213 const Decl
*D
= Entry
.first
;
7214 switch (D
->getKind()) {
7215 case Decl::CXXConversion
:
7216 case Decl::CXXMethod
:
7217 case Decl::Function
:
7218 case Decl::ObjCMethod
: {
7219 CodeGenPGO
PGO(*this);
7220 GlobalDecl
GD(cast
<FunctionDecl
>(D
));
7221 PGO
.emitEmptyCounterMapping(D
, getMangledName(GD
),
7222 getFunctionLinkage(GD
));
7225 case Decl::CXXConstructor
: {
7226 CodeGenPGO
PGO(*this);
7227 GlobalDecl
GD(cast
<CXXConstructorDecl
>(D
), Ctor_Base
);
7228 PGO
.emitEmptyCounterMapping(D
, getMangledName(GD
),
7229 getFunctionLinkage(GD
));
7232 case Decl::CXXDestructor
: {
7233 CodeGenPGO
PGO(*this);
7234 GlobalDecl
GD(cast
<CXXDestructorDecl
>(D
), Dtor_Base
);
7235 PGO
.emitEmptyCounterMapping(D
, getMangledName(GD
),
7236 getFunctionLinkage(GD
));
7245 void CodeGenModule::EmitMainVoidAlias() {
7246 // In order to transition away from "__original_main" gracefully, emit an
7247 // alias for "main" in the no-argument case so that libc can detect when
7248 // new-style no-argument main is in used.
7249 if (llvm::Function
*F
= getModule().getFunction("main")) {
7250 if (!F
->isDeclaration() && F
->arg_size() == 0 && !F
->isVarArg() &&
7251 F
->getReturnType()->isIntegerTy(Context
.getTargetInfo().getIntWidth())) {
7252 auto *GA
= llvm::GlobalAlias::create("__main_void", F
);
7253 GA
->setVisibility(llvm::GlobalValue::HiddenVisibility
);
7258 /// Turns the given pointer into a constant.
7259 static llvm::Constant
*GetPointerConstant(llvm::LLVMContext
&Context
,
7261 uintptr_t PtrInt
= reinterpret_cast<uintptr_t>(Ptr
);
7262 llvm::Type
*i64
= llvm::Type::getInt64Ty(Context
);
7263 return llvm::ConstantInt::get(i64
, PtrInt
);
7266 static void EmitGlobalDeclMetadata(CodeGenModule
&CGM
,
7267 llvm::NamedMDNode
*&GlobalMetadata
,
7269 llvm::GlobalValue
*Addr
) {
7270 if (!GlobalMetadata
)
7272 CGM
.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
7274 // TODO: should we report variant information for ctors/dtors?
7275 llvm::Metadata
*Ops
[] = {llvm::ConstantAsMetadata::get(Addr
),
7276 llvm::ConstantAsMetadata::get(GetPointerConstant(
7277 CGM
.getLLVMContext(), D
.getDecl()))};
7278 GlobalMetadata
->addOperand(llvm::MDNode::get(CGM
.getLLVMContext(), Ops
));
7281 bool CodeGenModule::CheckAndReplaceExternCIFuncs(llvm::GlobalValue
*Elem
,
7282 llvm::GlobalValue
*CppFunc
) {
7283 // Store the list of ifuncs we need to replace uses in.
7284 llvm::SmallVector
<llvm::GlobalIFunc
*> IFuncs
;
7285 // List of ConstantExprs that we should be able to delete when we're done
7287 llvm::SmallVector
<llvm::ConstantExpr
*> CEs
;
7289 // It isn't valid to replace the extern-C ifuncs if all we find is itself!
7290 if (Elem
== CppFunc
)
7293 // First make sure that all users of this are ifuncs (or ifuncs via a
7294 // bitcast), and collect the list of ifuncs and CEs so we can work on them
7296 for (llvm::User
*User
: Elem
->users()) {
7297 // Users can either be a bitcast ConstExpr that is used by the ifuncs, OR an
7298 // ifunc directly. In any other case, just give up, as we don't know what we
7299 // could break by changing those.
7300 if (auto *ConstExpr
= dyn_cast
<llvm::ConstantExpr
>(User
)) {
7301 if (ConstExpr
->getOpcode() != llvm::Instruction::BitCast
)
7304 for (llvm::User
*CEUser
: ConstExpr
->users()) {
7305 if (auto *IFunc
= dyn_cast
<llvm::GlobalIFunc
>(CEUser
)) {
7306 IFuncs
.push_back(IFunc
);
7311 CEs
.push_back(ConstExpr
);
7312 } else if (auto *IFunc
= dyn_cast
<llvm::GlobalIFunc
>(User
)) {
7313 IFuncs
.push_back(IFunc
);
7315 // This user is one we don't know how to handle, so fail redirection. This
7316 // will result in an ifunc retaining a resolver name that will ultimately
7317 // fail to be resolved to a defined function.
7322 // Now we know this is a valid case where we can do this alias replacement, we
7323 // need to remove all of the references to Elem (and the bitcasts!) so we can
7325 for (llvm::GlobalIFunc
*IFunc
: IFuncs
)
7326 IFunc
->setResolver(nullptr);
7327 for (llvm::ConstantExpr
*ConstExpr
: CEs
)
7328 ConstExpr
->destroyConstant();
7330 // We should now be out of uses for the 'old' version of this function, so we
7331 // can erase it as well.
7332 Elem
->eraseFromParent();
7334 for (llvm::GlobalIFunc
*IFunc
: IFuncs
) {
7335 // The type of the resolver is always just a function-type that returns the
7336 // type of the IFunc, so create that here. If the type of the actual
7337 // resolver doesn't match, it just gets bitcast to the right thing.
7339 llvm::FunctionType::get(IFunc
->getType(), /*isVarArg*/ false);
7340 llvm::Constant
*Resolver
= GetOrCreateLLVMFunction(
7341 CppFunc
->getName(), ResolverTy
, {}, /*ForVTable*/ false);
7342 IFunc
->setResolver(Resolver
);
7347 /// For each function which is declared within an extern "C" region and marked
7348 /// as 'used', but has internal linkage, create an alias from the unmangled
7349 /// name to the mangled name if possible. People expect to be able to refer
7350 /// to such functions with an unmangled name from inline assembly within the
7351 /// same translation unit.
7352 void CodeGenModule::EmitStaticExternCAliases() {
7353 if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
7355 for (auto &I
: StaticExternCValues
) {
7356 const IdentifierInfo
*Name
= I
.first
;
7357 llvm::GlobalValue
*Val
= I
.second
;
7359 // If Val is null, that implies there were multiple declarations that each
7360 // had a claim to the unmangled name. In this case, generation of the alias
7361 // is suppressed. See CodeGenModule::MaybeHandleStaticInExternC.
7365 llvm::GlobalValue
*ExistingElem
=
7366 getModule().getNamedValue(Name
->getName());
7368 // If there is either not something already by this name, or we were able to
7369 // replace all uses from IFuncs, create the alias.
7370 if (!ExistingElem
|| CheckAndReplaceExternCIFuncs(ExistingElem
, Val
))
7371 addCompilerUsedGlobal(llvm::GlobalAlias::create(Name
->getName(), Val
));
7375 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName
,
7376 GlobalDecl
&Result
) const {
7377 auto Res
= Manglings
.find(MangledName
);
7378 if (Res
== Manglings
.end())
7380 Result
= Res
->getValue();
7384 /// Emits metadata nodes associating all the global values in the
7385 /// current module with the Decls they came from. This is useful for
7386 /// projects using IR gen as a subroutine.
7388 /// Since there's currently no way to associate an MDNode directly
7389 /// with an llvm::GlobalValue, we create a global named metadata
7390 /// with the name 'clang.global.decl.ptrs'.
7391 void CodeGenModule::EmitDeclMetadata() {
7392 llvm::NamedMDNode
*GlobalMetadata
= nullptr;
7394 for (auto &I
: MangledDeclNames
) {
7395 llvm::GlobalValue
*Addr
= getModule().getNamedValue(I
.second
);
7396 // Some mangled names don't necessarily have an associated GlobalValue
7397 // in this module, e.g. if we mangled it for DebugInfo.
7399 EmitGlobalDeclMetadata(*this, GlobalMetadata
, I
.first
, Addr
);
7403 /// Emits metadata nodes for all the local variables in the current
7405 void CodeGenFunction::EmitDeclMetadata() {
7406 if (LocalDeclMap
.empty()) return;
7408 llvm::LLVMContext
&Context
= getLLVMContext();
7410 // Find the unique metadata ID for this name.
7411 unsigned DeclPtrKind
= Context
.getMDKindID("clang.decl.ptr");
7413 llvm::NamedMDNode
*GlobalMetadata
= nullptr;
7415 for (auto &I
: LocalDeclMap
) {
7416 const Decl
*D
= I
.first
;
7417 llvm::Value
*Addr
= I
.second
.emitRawPointer(*this);
7418 if (auto *Alloca
= dyn_cast
<llvm::AllocaInst
>(Addr
)) {
7419 llvm::Value
*DAddr
= GetPointerConstant(getLLVMContext(), D
);
7420 Alloca
->setMetadata(
7421 DeclPtrKind
, llvm::MDNode::get(
7422 Context
, llvm::ValueAsMetadata::getConstant(DAddr
)));
7423 } else if (auto *GV
= dyn_cast
<llvm::GlobalValue
>(Addr
)) {
7424 GlobalDecl GD
= GlobalDecl(cast
<VarDecl
>(D
));
7425 EmitGlobalDeclMetadata(CGM
, GlobalMetadata
, GD
, GV
);
7430 void CodeGenModule::EmitVersionIdentMetadata() {
7431 llvm::NamedMDNode
*IdentMetadata
=
7432 TheModule
.getOrInsertNamedMetadata("llvm.ident");
7433 std::string Version
= getClangFullVersion();
7434 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
7436 llvm::Metadata
*IdentNode
[] = {llvm::MDString::get(Ctx
, Version
)};
7437 IdentMetadata
->addOperand(llvm::MDNode::get(Ctx
, IdentNode
));
7440 void CodeGenModule::EmitCommandLineMetadata() {
7441 llvm::NamedMDNode
*CommandLineMetadata
=
7442 TheModule
.getOrInsertNamedMetadata("llvm.commandline");
7443 std::string CommandLine
= getCodeGenOpts().RecordCommandLine
;
7444 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
7446 llvm::Metadata
*CommandLineNode
[] = {llvm::MDString::get(Ctx
, CommandLine
)};
7447 CommandLineMetadata
->addOperand(llvm::MDNode::get(Ctx
, CommandLineNode
));
7450 void CodeGenModule::EmitCoverageFile() {
7451 llvm::NamedMDNode
*CUNode
= TheModule
.getNamedMetadata("llvm.dbg.cu");
7455 llvm::NamedMDNode
*GCov
= TheModule
.getOrInsertNamedMetadata("llvm.gcov");
7456 llvm::LLVMContext
&Ctx
= TheModule
.getContext();
7457 auto *CoverageDataFile
=
7458 llvm::MDString::get(Ctx
, getCodeGenOpts().CoverageDataFile
);
7459 auto *CoverageNotesFile
=
7460 llvm::MDString::get(Ctx
, getCodeGenOpts().CoverageNotesFile
);
7461 for (int i
= 0, e
= CUNode
->getNumOperands(); i
!= e
; ++i
) {
7462 llvm::MDNode
*CU
= CUNode
->getOperand(i
);
7463 llvm::Metadata
*Elts
[] = {CoverageNotesFile
, CoverageDataFile
, CU
};
7464 GCov
->addOperand(llvm::MDNode::get(Ctx
, Elts
));
7468 llvm::Constant
*CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty
,
7470 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
7471 // FIXME: should we even be calling this method if RTTI is disabled
7472 // and it's not for EH?
7473 if (!shouldEmitRTTI(ForEH
))
7474 return llvm::Constant::getNullValue(GlobalsInt8PtrTy
);
7476 if (ForEH
&& Ty
->isObjCObjectPointerType() &&
7477 LangOpts
.ObjCRuntime
.isGNUFamily())
7478 return ObjCRuntime
->GetEHType(Ty
);
7480 return getCXXABI().getAddrOfRTTIDescriptor(Ty
);
7483 void CodeGenModule::EmitOMPThreadPrivateDecl(const OMPThreadPrivateDecl
*D
) {
7484 // Do not emit threadprivates in simd-only mode.
7485 if (LangOpts
.OpenMP
&& LangOpts
.OpenMPSimd
)
7487 for (auto RefExpr
: D
->varlists()) {
7488 auto *VD
= cast
<VarDecl
>(cast
<DeclRefExpr
>(RefExpr
)->getDecl());
7490 VD
->getAnyInitializer() &&
7491 !VD
->getAnyInitializer()->isConstantInitializer(getContext(),
7494 Address
Addr(GetAddrOfGlobalVar(VD
),
7495 getTypes().ConvertTypeForMem(VD
->getType()),
7496 getContext().getDeclAlign(VD
));
7497 if (auto InitFunction
= getOpenMPRuntime().emitThreadPrivateVarDefinition(
7498 VD
, Addr
, RefExpr
->getBeginLoc(), PerformInit
))
7499 CXXGlobalInits
.push_back(InitFunction
);
7504 CodeGenModule::CreateMetadataIdentifierImpl(QualType T
, MetadataTypeMap
&Map
,
7506 if (auto *FnType
= T
->getAs
<FunctionProtoType
>())
7507 T
= getContext().getFunctionType(
7508 FnType
->getReturnType(), FnType
->getParamTypes(),
7509 FnType
->getExtProtoInfo().withExceptionSpec(EST_None
));
7511 llvm::Metadata
*&InternalId
= Map
[T
.getCanonicalType()];
7515 if (isExternallyVisible(T
->getLinkage())) {
7516 std::string OutName
;
7517 llvm::raw_string_ostream
Out(OutName
);
7518 getCXXABI().getMangleContext().mangleCanonicalTypeName(
7519 T
, Out
, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers
);
7521 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers
)
7522 Out
<< ".normalized";
7526 InternalId
= llvm::MDString::get(getLLVMContext(), Out
.str());
7528 InternalId
= llvm::MDNode::getDistinct(getLLVMContext(),
7529 llvm::ArrayRef
<llvm::Metadata
*>());
7535 llvm::Metadata
*CodeGenModule::CreateMetadataIdentifierForType(QualType T
) {
7536 return CreateMetadataIdentifierImpl(T
, MetadataIdMap
, "");
7540 CodeGenModule::CreateMetadataIdentifierForVirtualMemPtrType(QualType T
) {
7541 return CreateMetadataIdentifierImpl(T
, VirtualMetadataIdMap
, ".virtual");
7544 // Generalize pointer types to a void pointer with the qualifiers of the
7545 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
7546 // generalize to 'const void *' while 'char *' and 'const char **' generalize to
7548 static QualType
GeneralizeType(ASTContext
&Ctx
, QualType Ty
) {
7549 if (!Ty
->isPointerType())
7552 return Ctx
.getPointerType(
7553 QualType(Ctx
.VoidTy
).withCVRQualifiers(
7554 Ty
->getPointeeType().getCVRQualifiers()));
7557 // Apply type generalization to a FunctionType's return and argument types
7558 static QualType
GeneralizeFunctionType(ASTContext
&Ctx
, QualType Ty
) {
7559 if (auto *FnType
= Ty
->getAs
<FunctionProtoType
>()) {
7560 SmallVector
<QualType
, 8> GeneralizedParams
;
7561 for (auto &Param
: FnType
->param_types())
7562 GeneralizedParams
.push_back(GeneralizeType(Ctx
, Param
));
7564 return Ctx
.getFunctionType(
7565 GeneralizeType(Ctx
, FnType
->getReturnType()),
7566 GeneralizedParams
, FnType
->getExtProtoInfo());
7569 if (auto *FnType
= Ty
->getAs
<FunctionNoProtoType
>())
7570 return Ctx
.getFunctionNoProtoType(
7571 GeneralizeType(Ctx
, FnType
->getReturnType()));
7573 llvm_unreachable("Encountered unknown FunctionType");
7576 llvm::Metadata
*CodeGenModule::CreateMetadataIdentifierGeneralized(QualType T
) {
7577 return CreateMetadataIdentifierImpl(GeneralizeFunctionType(getContext(), T
),
7578 GeneralizedMetadataIdMap
, ".generalized");
7581 /// Returns whether this module needs the "all-vtables" type identifier.
7582 bool CodeGenModule::NeedAllVtablesTypeId() const {
7583 // Returns true if at least one of vtable-based CFI checkers is enabled and
7584 // is not in the trapping mode.
7585 return ((LangOpts
.Sanitize
.has(SanitizerKind::CFIVCall
) &&
7586 !CodeGenOpts
.SanitizeTrap
.has(SanitizerKind::CFIVCall
)) ||
7587 (LangOpts
.Sanitize
.has(SanitizerKind::CFINVCall
) &&
7588 !CodeGenOpts
.SanitizeTrap
.has(SanitizerKind::CFINVCall
)) ||
7589 (LangOpts
.Sanitize
.has(SanitizerKind::CFIDerivedCast
) &&
7590 !CodeGenOpts
.SanitizeTrap
.has(SanitizerKind::CFIDerivedCast
)) ||
7591 (LangOpts
.Sanitize
.has(SanitizerKind::CFIUnrelatedCast
) &&
7592 !CodeGenOpts
.SanitizeTrap
.has(SanitizerKind::CFIUnrelatedCast
)));
7595 void CodeGenModule::AddVTableTypeMetadata(llvm::GlobalVariable
*VTable
,
7597 const CXXRecordDecl
*RD
) {
7598 llvm::Metadata
*MD
=
7599 CreateMetadataIdentifierForType(QualType(RD
->getTypeForDecl(), 0));
7600 VTable
->addTypeMetadata(Offset
.getQuantity(), MD
);
7602 if (CodeGenOpts
.SanitizeCfiCrossDso
)
7603 if (auto CrossDsoTypeId
= CreateCrossDsoCfiTypeId(MD
))
7604 VTable
->addTypeMetadata(Offset
.getQuantity(),
7605 llvm::ConstantAsMetadata::get(CrossDsoTypeId
));
7607 if (NeedAllVtablesTypeId()) {
7608 llvm::Metadata
*MD
= llvm::MDString::get(getLLVMContext(), "all-vtables");
7609 VTable
->addTypeMetadata(Offset
.getQuantity(), MD
);
7613 llvm::SanitizerStatReport
&CodeGenModule::getSanStats() {
7615 SanStats
= std::make_unique
<llvm::SanitizerStatReport
>(&getModule());
7621 CodeGenModule::createOpenCLIntToSamplerConversion(const Expr
*E
,
7622 CodeGenFunction
&CGF
) {
7623 llvm::Constant
*C
= ConstantEmitter(CGF
).emitAbstract(E
, E
->getType());
7624 auto *SamplerT
= getOpenCLRuntime().getSamplerType(E
->getType().getTypePtr());
7625 auto *FTy
= llvm::FunctionType::get(SamplerT
, {C
->getType()}, false);
7626 auto *Call
= CGF
.EmitRuntimeCall(
7627 CreateRuntimeFunction(FTy
, "__translate_sampler_initializer"), {C
});
7631 CharUnits
CodeGenModule::getNaturalPointeeTypeAlignment(
7632 QualType T
, LValueBaseInfo
*BaseInfo
, TBAAAccessInfo
*TBAAInfo
) {
7633 return getNaturalTypeAlignment(T
->getPointeeType(), BaseInfo
, TBAAInfo
,
7634 /* forPointeeType= */ true);
7637 CharUnits
CodeGenModule::getNaturalTypeAlignment(QualType T
,
7638 LValueBaseInfo
*BaseInfo
,
7639 TBAAAccessInfo
*TBAAInfo
,
7640 bool forPointeeType
) {
7642 *TBAAInfo
= getTBAAAccessInfo(T
);
7644 // FIXME: This duplicates logic in ASTContext::getTypeAlignIfKnown. But
7645 // that doesn't return the information we need to compute BaseInfo.
7647 // Honor alignment typedef attributes even on incomplete types.
7648 // We also honor them straight for C++ class types, even as pointees;
7649 // there's an expressivity gap here.
7650 if (auto TT
= T
->getAs
<TypedefType
>()) {
7651 if (auto Align
= TT
->getDecl()->getMaxAlignment()) {
7653 *BaseInfo
= LValueBaseInfo(AlignmentSource::AttributedType
);
7654 return getContext().toCharUnitsFromBits(Align
);
7658 bool AlignForArray
= T
->isArrayType();
7660 // Analyze the base element type, so we don't get confused by incomplete
7662 T
= getContext().getBaseElementType(T
);
7664 if (T
->isIncompleteType()) {
7665 // We could try to replicate the logic from
7666 // ASTContext::getTypeAlignIfKnown, but nothing uses the alignment if the
7667 // type is incomplete, so it's impossible to test. We could try to reuse
7668 // getTypeAlignIfKnown, but that doesn't return the information we need
7669 // to set BaseInfo. So just ignore the possibility that the alignment is
7670 // greater than one.
7672 *BaseInfo
= LValueBaseInfo(AlignmentSource::Type
);
7673 return CharUnits::One();
7677 *BaseInfo
= LValueBaseInfo(AlignmentSource::Type
);
7679 CharUnits Alignment
;
7680 const CXXRecordDecl
*RD
;
7681 if (T
.getQualifiers().hasUnaligned()) {
7682 Alignment
= CharUnits::One();
7683 } else if (forPointeeType
&& !AlignForArray
&&
7684 (RD
= T
->getAsCXXRecordDecl())) {
7685 // For C++ class pointees, we don't know whether we're pointing at a
7686 // base or a complete object, so we generally need to use the
7687 // non-virtual alignment.
7688 Alignment
= getClassPointerAlignment(RD
);
7690 Alignment
= getContext().getTypeAlignInChars(T
);
7693 // Cap to the global maximum type alignment unless the alignment
7694 // was somehow explicit on the type.
7695 if (unsigned MaxAlign
= getLangOpts().MaxTypeAlign
) {
7696 if (Alignment
.getQuantity() > MaxAlign
&&
7697 !getContext().isAlignmentRequired(T
))
7698 Alignment
= CharUnits::fromQuantity(MaxAlign
);
7703 bool CodeGenModule::stopAutoInit() {
7704 unsigned StopAfter
= getContext().getLangOpts().TrivialAutoVarInitStopAfter
;
7706 // This number is positive only when -ftrivial-auto-var-init-stop-after=* is
7708 if (NumAutoVarInit
>= StopAfter
) {
7711 if (!NumAutoVarInit
) {
7712 unsigned DiagID
= getDiags().getCustomDiagID(
7713 DiagnosticsEngine::Warning
,
7714 "-ftrivial-auto-var-init-stop-after=%0 has been enabled to limit the "
7715 "number of times ftrivial-auto-var-init=%1 gets applied.");
7716 getDiags().Report(DiagID
)
7718 << (getContext().getLangOpts().getTrivialAutoVarInit() ==
7719 LangOptions::TrivialAutoVarInitKind::Zero
7728 void CodeGenModule::printPostfixForExternalizedDecl(llvm::raw_ostream
&OS
,
7729 const Decl
*D
) const {
7730 // ptxas does not allow '.' in symbol names. On the other hand, HIP prefers
7731 // postfix beginning with '.' since the symbol name can be demangled.
7733 OS
<< (isa
<VarDecl
>(D
) ? ".static." : ".intern.");
7735 OS
<< (isa
<VarDecl
>(D
) ? "__static__" : "__intern__");
7737 // If the CUID is not specified we try to generate a unique postfix.
7738 if (getLangOpts().CUID
.empty()) {
7739 SourceManager
&SM
= getContext().getSourceManager();
7740 PresumedLoc PLoc
= SM
.getPresumedLoc(D
->getLocation());
7741 assert(PLoc
.isValid() && "Source location is expected to be valid.");
7743 // Get the hash of the user defined macros.
7745 llvm::MD5::MD5Result Result
;
7746 for (const auto &Arg
: PreprocessorOpts
.Macros
)
7747 Hash
.update(Arg
.first
);
7750 // Get the UniqueID for the file containing the decl.
7751 llvm::sys::fs::UniqueID ID
;
7752 if (llvm::sys::fs::getUniqueID(PLoc
.getFilename(), ID
)) {
7753 PLoc
= SM
.getPresumedLoc(D
->getLocation(), /*UseLineDirectives=*/false);
7754 assert(PLoc
.isValid() && "Source location is expected to be valid.");
7755 if (auto EC
= llvm::sys::fs::getUniqueID(PLoc
.getFilename(), ID
))
7756 SM
.getDiagnostics().Report(diag::err_cannot_open_file
)
7757 << PLoc
.getFilename() << EC
.message();
7759 OS
<< llvm::format("%x", ID
.getFile()) << llvm::format("%x", ID
.getDevice())
7760 << "_" << llvm::utohexstr(Result
.low(), /*LowerCase=*/true, /*Width=*/8);
7762 OS
<< getContext().getCUIDHash();
7766 void CodeGenModule::moveLazyEmissionStates(CodeGenModule
*NewBuilder
) {
7767 assert(DeferredDeclsToEmit
.empty() &&
7768 "Should have emitted all decls deferred to emit.");
7769 assert(NewBuilder
->DeferredDecls
.empty() &&
7770 "Newly created module should not have deferred decls");
7771 NewBuilder
->DeferredDecls
= std::move(DeferredDecls
);
7772 assert(EmittedDeferredDecls
.empty() &&
7773 "Still have (unmerged) EmittedDeferredDecls deferred decls");
7775 assert(NewBuilder
->DeferredVTables
.empty() &&
7776 "Newly created module should not have deferred vtables");
7777 NewBuilder
->DeferredVTables
= std::move(DeferredVTables
);
7779 assert(NewBuilder
->MangledDeclNames
.empty() &&
7780 "Newly created module should not have mangled decl names");
7781 assert(NewBuilder
->Manglings
.empty() &&
7782 "Newly created module should not have manglings");
7783 NewBuilder
->Manglings
= std::move(Manglings
);
7785 NewBuilder
->WeakRefReferences
= std::move(WeakRefReferences
);
7787 NewBuilder
->TBAA
= std::move(TBAA
);
7789 NewBuilder
->ABI
->MangleCtx
= std::move(ABI
->MangleCtx
);