clang 18.0.0git
CodeGenModule.cpp
Go to the documentation of this file.
1//===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This coordinates the per-module state used while generating code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenModule.h"
14#include "ABIInfo.h"
15#include "CGBlocks.h"
16#include "CGCUDARuntime.h"
17#include "CGCXXABI.h"
18#include "CGCall.h"
19#include "CGDebugInfo.h"
20#include "CGHLSLRuntime.h"
21#include "CGObjCRuntime.h"
22#include "CGOpenCLRuntime.h"
23#include "CGOpenMPRuntime.h"
24#include "CGOpenMPRuntimeGPU.h"
25#include "CodeGenFunction.h"
26#include "CodeGenPGO.h"
27#include "ConstantEmitter.h"
28#include "CoverageMappingGen.h"
29#include "TargetInfo.h"
31#include "clang/AST/CharUnits.h"
32#include "clang/AST/DeclCXX.h"
33#include "clang/AST/DeclObjC.h"
35#include "clang/AST/Mangle.h"
43#include "clang/Basic/Module.h"
46#include "clang/Basic/Version.h"
50#include "llvm/ADT/STLExtras.h"
51#include "llvm/ADT/StringExtras.h"
52#include "llvm/ADT/StringSwitch.h"
53#include "llvm/Analysis/TargetLibraryInfo.h"
54#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
55#include "llvm/IR/AttributeMask.h"
56#include "llvm/IR/CallingConv.h"
57#include "llvm/IR/DataLayout.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/LLVMContext.h"
60#include "llvm/IR/Module.h"
61#include "llvm/IR/ProfileSummary.h"
62#include "llvm/ProfileData/InstrProfReader.h"
63#include "llvm/ProfileData/SampleProf.h"
64#include "llvm/Support/CRC.h"
65#include "llvm/Support/CodeGen.h"
66#include "llvm/Support/CommandLine.h"
67#include "llvm/Support/ConvertUTF.h"
68#include "llvm/Support/ErrorHandling.h"
69#include "llvm/Support/TimeProfiler.h"
70#include "llvm/Support/xxhash.h"
71#include "llvm/TargetParser/Triple.h"
72#include "llvm/TargetParser/X86TargetParser.h"
73#include <optional>
74
75using namespace clang;
76using namespace CodeGen;
77
78static llvm::cl::opt<bool> LimitedCoverage(
79 "limited-coverage-experimental", llvm::cl::Hidden,
80 llvm::cl::desc("Emit limited coverage mapping information (experimental)"));
81
82static const char AnnotationSection[] = "llvm.metadata";
83
85 switch (CGM.getContext().getCXXABIKind()) {
86 case TargetCXXABI::AppleARM64:
87 case TargetCXXABI::Fuchsia:
88 case TargetCXXABI::GenericAArch64:
89 case TargetCXXABI::GenericARM:
90 case TargetCXXABI::iOS:
91 case TargetCXXABI::WatchOS:
92 case TargetCXXABI::GenericMIPS:
93 case TargetCXXABI::GenericItanium:
94 case TargetCXXABI::WebAssembly:
95 case TargetCXXABI::XL:
96 return CreateItaniumCXXABI(CGM);
97 case TargetCXXABI::Microsoft:
98 return CreateMicrosoftCXXABI(CGM);
99 }
100
101 llvm_unreachable("invalid C++ ABI kind");
102}
103
104static std::unique_ptr<TargetCodeGenInfo>
106 const TargetInfo &Target = CGM.getTarget();
107 const llvm::Triple &Triple = Target.getTriple();
108 const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
109
110 switch (Triple.getArch()) {
111 default:
113
114 case llvm::Triple::le32:
116 case llvm::Triple::m68k:
117 return createM68kTargetCodeGenInfo(CGM);
118 case llvm::Triple::mips:
119 case llvm::Triple::mipsel:
120 if (Triple.getOS() == llvm::Triple::NaCl)
122 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/true);
123
124 case llvm::Triple::mips64:
125 case llvm::Triple::mips64el:
126 return createMIPSTargetCodeGenInfo(CGM, /*IsOS32=*/false);
127
128 case llvm::Triple::avr: {
129 // For passing parameters, R8~R25 are used on avr, and R18~R25 are used
130 // on avrtiny. For passing return value, R18~R25 are used on avr, and
131 // R22~R25 are used on avrtiny.
132 unsigned NPR = Target.getABI() == "avrtiny" ? 6 : 18;
133 unsigned NRR = Target.getABI() == "avrtiny" ? 4 : 8;
134 return createAVRTargetCodeGenInfo(CGM, NPR, NRR);
135 }
136
137 case llvm::Triple::aarch64:
138 case llvm::Triple::aarch64_32:
139 case llvm::Triple::aarch64_be: {
140 AArch64ABIKind Kind = AArch64ABIKind::AAPCS;
141 if (Target.getABI() == "darwinpcs")
142 Kind = AArch64ABIKind::DarwinPCS;
143 else if (Triple.isOSWindows())
144 return createWindowsAArch64TargetCodeGenInfo(CGM, AArch64ABIKind::Win64);
145
146 return createAArch64TargetCodeGenInfo(CGM, Kind);
147 }
148
149 case llvm::Triple::wasm32:
150 case llvm::Triple::wasm64: {
151 WebAssemblyABIKind Kind = WebAssemblyABIKind::MVP;
152 if (Target.getABI() == "experimental-mv")
153 Kind = WebAssemblyABIKind::ExperimentalMV;
154 return createWebAssemblyTargetCodeGenInfo(CGM, Kind);
155 }
156
157 case llvm::Triple::arm:
158 case llvm::Triple::armeb:
159 case llvm::Triple::thumb:
160 case llvm::Triple::thumbeb: {
161 if (Triple.getOS() == llvm::Triple::Win32)
162 return createWindowsARMTargetCodeGenInfo(CGM, ARMABIKind::AAPCS_VFP);
163
164 ARMABIKind Kind = ARMABIKind::AAPCS;
165 StringRef ABIStr = Target.getABI();
166 if (ABIStr == "apcs-gnu")
167 Kind = ARMABIKind::APCS;
168 else if (ABIStr == "aapcs16")
169 Kind = ARMABIKind::AAPCS16_VFP;
170 else if (CodeGenOpts.FloatABI == "hard" ||
171 (CodeGenOpts.FloatABI != "soft" &&
172 (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
173 Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
174 Triple.getEnvironment() == llvm::Triple::EABIHF)))
175 Kind = ARMABIKind::AAPCS_VFP;
176
177 return createARMTargetCodeGenInfo(CGM, Kind);
178 }
179
180 case llvm::Triple::ppc: {
181 if (Triple.isOSAIX())
182 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/false);
183
184 bool IsSoftFloat =
185 CodeGenOpts.FloatABI == "soft" || Target.hasFeature("spe");
186 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
187 }
188 case llvm::Triple::ppcle: {
189 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
190 return createPPC32TargetCodeGenInfo(CGM, IsSoftFloat);
191 }
192 case llvm::Triple::ppc64:
193 if (Triple.isOSAIX())
194 return createAIXTargetCodeGenInfo(CGM, /*Is64Bit=*/true);
195
196 if (Triple.isOSBinFormatELF()) {
197 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv1;
198 if (Target.getABI() == "elfv2")
199 Kind = PPC64_SVR4_ABIKind::ELFv2;
200 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
201
202 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
203 }
205 case llvm::Triple::ppc64le: {
206 assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
207 PPC64_SVR4_ABIKind Kind = PPC64_SVR4_ABIKind::ELFv2;
208 if (Target.getABI() == "elfv1")
209 Kind = PPC64_SVR4_ABIKind::ELFv1;
210 bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
211
212 return createPPC64_SVR4_TargetCodeGenInfo(CGM, Kind, IsSoftFloat);
213 }
214
215 case llvm::Triple::nvptx:
216 case llvm::Triple::nvptx64:
218
219 case llvm::Triple::msp430:
221
222 case llvm::Triple::riscv32:
223 case llvm::Triple::riscv64: {
224 StringRef ABIStr = Target.getABI();
225 unsigned XLen = Target.getPointerWidth(LangAS::Default);
226 unsigned ABIFLen = 0;
227 if (ABIStr.endswith("f"))
228 ABIFLen = 32;
229 else if (ABIStr.endswith("d"))
230 ABIFLen = 64;
231 return createRISCVTargetCodeGenInfo(CGM, XLen, ABIFLen);
232 }
233
234 case llvm::Triple::systemz: {
235 bool SoftFloat = CodeGenOpts.FloatABI == "soft";
236 bool HasVector = !SoftFloat && Target.getABI() == "vector";
237 return createSystemZTargetCodeGenInfo(CGM, HasVector, SoftFloat);
238 }
239
240 case llvm::Triple::tce:
241 case llvm::Triple::tcele:
242 return createTCETargetCodeGenInfo(CGM);
243
244 case llvm::Triple::x86: {
245 bool IsDarwinVectorABI = Triple.isOSDarwin();
246 bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
247
248 if (Triple.getOS() == llvm::Triple::Win32) {
250 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
251 CodeGenOpts.NumRegisterParameters);
252 }
254 CGM, IsDarwinVectorABI, IsWin32FloatStructABI,
255 CodeGenOpts.NumRegisterParameters, CodeGenOpts.FloatABI == "soft");
256 }
257
258 case llvm::Triple::x86_64: {
259 StringRef ABI = Target.getABI();
260 X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512
261 : ABI == "avx" ? X86AVXABILevel::AVX
262 : X86AVXABILevel::None);
263
264 switch (Triple.getOS()) {
265 case llvm::Triple::Win32:
266 return createWinX86_64TargetCodeGenInfo(CGM, AVXLevel);
267 default:
268 return createX86_64TargetCodeGenInfo(CGM, AVXLevel);
269 }
270 }
271 case llvm::Triple::hexagon:
273 case llvm::Triple::lanai:
275 case llvm::Triple::r600:
277 case llvm::Triple::amdgcn:
279 case llvm::Triple::sparc:
281 case llvm::Triple::sparcv9:
283 case llvm::Triple::xcore:
285 case llvm::Triple::arc:
286 return createARCTargetCodeGenInfo(CGM);
287 case llvm::Triple::spir:
288 case llvm::Triple::spir64:
290 case llvm::Triple::spirv32:
291 case llvm::Triple::spirv64:
293 case llvm::Triple::ve:
294 return createVETargetCodeGenInfo(CGM);
295 case llvm::Triple::csky: {
296 bool IsSoftFloat = !Target.hasFeature("hard-float-abi");
297 bool hasFP64 =
298 Target.hasFeature("fpuv2_df") || Target.hasFeature("fpuv3_df");
299 return createCSKYTargetCodeGenInfo(CGM, IsSoftFloat ? 0
300 : hasFP64 ? 64
301 : 32);
302 }
303 case llvm::Triple::bpfeb:
304 case llvm::Triple::bpfel:
305 return createBPFTargetCodeGenInfo(CGM);
306 case llvm::Triple::loongarch32:
307 case llvm::Triple::loongarch64: {
308 StringRef ABIStr = Target.getABI();
309 unsigned ABIFRLen = 0;
310 if (ABIStr.endswith("f"))
311 ABIFRLen = 32;
312 else if (ABIStr.endswith("d"))
313 ABIFRLen = 64;
315 CGM, Target.getPointerWidth(LangAS::Default), ABIFRLen);
316 }
317 }
318}
319
321 if (!TheTargetCodeGenInfo)
322 TheTargetCodeGenInfo = createTargetCodeGenInfo(*this);
323 return *TheTargetCodeGenInfo;
324}
325
326CodeGenModule::CodeGenModule(ASTContext &C,
328 const HeaderSearchOptions &HSO,
329 const PreprocessorOptions &PPO,
330 const CodeGenOptions &CGO, llvm::Module &M,
331 DiagnosticsEngine &diags,
332 CoverageSourceInfo *CoverageInfo)
333 : Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
334 PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
335 Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
336 VMContext(M.getContext()), Types(*this), VTables(*this),
337 SanitizerMD(new SanitizerMetadata(*this)) {
338
339 // Initialize the type cache.
340 llvm::LLVMContext &LLVMContext = M.getContext();
341 VoidTy = llvm::Type::getVoidTy(LLVMContext);
342 Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
343 Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
344 Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
345 Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
346 HalfTy = llvm::Type::getHalfTy(LLVMContext);
347 BFloatTy = llvm::Type::getBFloatTy(LLVMContext);
348 FloatTy = llvm::Type::getFloatTy(LLVMContext);
349 DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
350 PointerWidthInBits = C.getTargetInfo().getPointerWidth(LangAS::Default);
352 C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(LangAS::Default))
353 .getQuantity();
355 C.toCharUnitsFromBits(C.getTargetInfo().getMaxPointerWidth()).getQuantity();
357 C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
358 CharTy =
359 llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getCharWidth());
360 IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
361 IntPtrTy = llvm::IntegerType::get(LLVMContext,
362 C.getTargetInfo().getMaxPointerWidth());
363 Int8PtrTy = llvm::PointerType::get(LLVMContext, 0);
364 const llvm::DataLayout &DL = M.getDataLayout();
366 llvm::PointerType::get(LLVMContext, DL.getAllocaAddrSpace());
368 llvm::PointerType::get(LLVMContext, DL.getDefaultGlobalsAddressSpace());
369 ConstGlobalsPtrTy = llvm::PointerType::get(
370 LLVMContext, C.getTargetAddressSpace(GetGlobalConstantAddressSpace()));
372
373 // Build C++20 Module initializers.
374 // TODO: Add Microsoft here once we know the mangling required for the
375 // initializers.
376 CXX20ModuleInits =
377 LangOpts.CPlusPlusModules && getCXXABI().getMangleContext().getKind() ==
379
380 RuntimeCC = getTargetCodeGenInfo().getABIInfo().getRuntimeCC();
381
382 if (LangOpts.ObjC)
383 createObjCRuntime();
384 if (LangOpts.OpenCL)
385 createOpenCLRuntime();
386 if (LangOpts.OpenMP)
387 createOpenMPRuntime();
388 if (LangOpts.CUDA)
389 createCUDARuntime();
390 if (LangOpts.HLSL)
391 createHLSLRuntime();
392
393 // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
394 if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
395 (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
396 TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
397 getCXXABI().getMangleContext()));
398
399 // If debug info or coverage generation is enabled, create the CGDebugInfo
400 // object.
401 if (CodeGenOpts.getDebugInfo() != llvm::codegenoptions::NoDebugInfo ||
402 CodeGenOpts.CoverageNotesFile.size() ||
403 CodeGenOpts.CoverageDataFile.size())
404 DebugInfo.reset(new CGDebugInfo(*this));
405
406 Block.GlobalUniqueCount = 0;
407
408 if (C.getLangOpts().ObjC)
409 ObjCData.reset(new ObjCEntrypoints());
410
411 if (CodeGenOpts.hasProfileClangUse()) {
412 auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
413 CodeGenOpts.ProfileInstrumentUsePath, *FS,
414 CodeGenOpts.ProfileRemappingFile);
415 // We're checking for profile read errors in CompilerInvocation, so if
416 // there was an error it should've already been caught. If it hasn't been
417 // somehow, trip an assertion.
418 assert(ReaderOrErr);
419 PGOReader = std::move(ReaderOrErr.get());
420 }
421
422 // If coverage mapping generation is enabled, create the
423 // CoverageMappingModuleGen object.
424 if (CodeGenOpts.CoverageMapping)
425 CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
426
427 // Generate the module name hash here if needed.
428 if (CodeGenOpts.UniqueInternalLinkageNames &&
429 !getModule().getSourceFileName().empty()) {
430 std::string Path = getModule().getSourceFileName();
431 // Check if a path substitution is needed from the MacroPrefixMap.
432 for (const auto &Entry : LangOpts.MacroPrefixMap)
433 if (Path.rfind(Entry.first, 0) != std::string::npos) {
434 Path = Entry.second + Path.substr(Entry.first.size());
435 break;
436 }
437 ModuleNameHash = llvm::getUniqueInternalLinkagePostfix(Path);
438 }
439}
440
442
443void CodeGenModule::createObjCRuntime() {
444 // This is just isGNUFamily(), but we want to force implementors of
445 // new ABIs to decide how best to do this.
446 switch (LangOpts.ObjCRuntime.getKind()) {
448 case ObjCRuntime::GCC:
450 ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
451 return;
452
455 case ObjCRuntime::iOS:
457 ObjCRuntime.reset(CreateMacObjCRuntime(*this));
458 return;
459 }
460 llvm_unreachable("bad runtime kind");
461}
462
463void CodeGenModule::createOpenCLRuntime() {
464 OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
465}
466
467void CodeGenModule::createOpenMPRuntime() {
468 // Select a specialized code generation class based on the target, if any.
469 // If it does not exist use the default implementation.
470 switch (getTriple().getArch()) {
471 case llvm::Triple::nvptx:
472 case llvm::Triple::nvptx64:
473 case llvm::Triple::amdgcn:
474 assert(getLangOpts().OpenMPIsTargetDevice &&
475 "OpenMP AMDGPU/NVPTX is only prepared to deal with device code.");
476 OpenMPRuntime.reset(new CGOpenMPRuntimeGPU(*this));
477 break;
478 default:
479 if (LangOpts.OpenMPSimd)
480 OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
481 else
482 OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
483 break;
484 }
485}
486
487void CodeGenModule::createCUDARuntime() {
488 CUDARuntime.reset(CreateNVCUDARuntime(*this));
489}
490
491void CodeGenModule::createHLSLRuntime() {
492 HLSLRuntime.reset(new CGHLSLRuntime(*this));
493}
494
495void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
496 Replacements[Name] = C;
497}
498
499void CodeGenModule::applyReplacements() {
500 for (auto &I : Replacements) {
501 StringRef MangledName = I.first;
502 llvm::Constant *Replacement = I.second;
503 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
504 if (!Entry)
505 continue;
506 auto *OldF = cast<llvm::Function>(Entry);
507 auto *NewF = dyn_cast<llvm::Function>(Replacement);
508 if (!NewF) {
509 if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
510 NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
511 } else {
512 auto *CE = cast<llvm::ConstantExpr>(Replacement);
513 assert(CE->getOpcode() == llvm::Instruction::BitCast ||
514 CE->getOpcode() == llvm::Instruction::GetElementPtr);
515 NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
516 }
517 }
518
519 // Replace old with new, but keep the old order.
520 OldF->replaceAllUsesWith(Replacement);
521 if (NewF) {
522 NewF->removeFromParent();
523 OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
524 NewF);
525 }
526 OldF->eraseFromParent();
527 }
528}
529
530void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
531 GlobalValReplacements.push_back(std::make_pair(GV, C));
532}
533
534void CodeGenModule::applyGlobalValReplacements() {
535 for (auto &I : GlobalValReplacements) {
536 llvm::GlobalValue *GV = I.first;
537 llvm::Constant *C = I.second;
538
539 GV->replaceAllUsesWith(C);
540 GV->eraseFromParent();
541 }
542}
543
544// This is only used in aliases that we created and we know they have a
545// linear structure.
546static const llvm::GlobalValue *getAliasedGlobal(const llvm::GlobalValue *GV) {
547 const llvm::Constant *C;
548 if (auto *GA = dyn_cast<llvm::GlobalAlias>(GV))
549 C = GA->getAliasee();
550 else if (auto *GI = dyn_cast<llvm::GlobalIFunc>(GV))
551 C = GI->getResolver();
552 else
553 return GV;
554
555 const auto *AliaseeGV = dyn_cast<llvm::GlobalValue>(C->stripPointerCasts());
556 if (!AliaseeGV)
557 return nullptr;
558
559 const llvm::GlobalValue *FinalGV = AliaseeGV->getAliaseeObject();
560 if (FinalGV == GV)
561 return nullptr;
562
563 return FinalGV;
564}
565
567 const ASTContext &Context, DiagnosticsEngine &Diags, SourceLocation Location,
568 bool IsIFunc, const llvm::GlobalValue *Alias, const llvm::GlobalValue *&GV,
569 const llvm::MapVector<GlobalDecl, StringRef> &MangledDeclNames,
570 SourceRange AliasRange) {
571 GV = getAliasedGlobal(Alias);
572 if (!GV) {
573 Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
574 return false;
575 }
576
577 if (GV->hasCommonLinkage()) {
578 const llvm::Triple &Triple = Context.getTargetInfo().getTriple();
579 if (Triple.getObjectFormat() == llvm::Triple::XCOFF) {
580 Diags.Report(Location, diag::err_alias_to_common);
581 return false;
582 }
583 }
584
585 if (GV->isDeclaration()) {
586 Diags.Report(Location, diag::err_alias_to_undefined) << IsIFunc << IsIFunc;
587 Diags.Report(Location, diag::note_alias_requires_mangled_name)
588 << IsIFunc << IsIFunc;
589 // Provide a note if the given function is not found and exists as a
590 // mangled name.
591 for (const auto &[Decl, Name] : MangledDeclNames) {
592 if (const auto *ND = dyn_cast<NamedDecl>(Decl.getDecl())) {
593 if (ND->getName() == GV->getName()) {
594 Diags.Report(Location, diag::note_alias_mangled_name_alternative)
595 << Name
597 AliasRange,
598 (Twine(IsIFunc ? "ifunc" : "alias") + "(\"" + Name + "\")")
599 .str());
600 }
601 }
602 }
603 return false;
604 }
605
606 if (IsIFunc) {
607 // Check resolver function type.
608 const auto *F = dyn_cast<llvm::Function>(GV);
609 if (!F) {
610 Diags.Report(Location, diag::err_alias_to_undefined)
611 << IsIFunc << IsIFunc;
612 return false;
613 }
614
615 llvm::FunctionType *FTy = F->getFunctionType();
616 if (!FTy->getReturnType()->isPointerTy()) {
617 Diags.Report(Location, diag::err_ifunc_resolver_return);
618 return false;
619 }
620 }
621
622 return true;
623}
624
625void CodeGenModule::checkAliases() {
626 // Check if the constructed aliases are well formed. It is really unfortunate
627 // that we have to do this in CodeGen, but we only construct mangled names
628 // and aliases during codegen.
629 bool Error = false;
630 DiagnosticsEngine &Diags = getDiags();
631 for (const GlobalDecl &GD : Aliases) {
632 const auto *D = cast<ValueDecl>(GD.getDecl());
633 SourceLocation Location;
635 bool IsIFunc = D->hasAttr<IFuncAttr>();
636 if (const Attr *A = D->getDefiningAttr()) {
637 Location = A->getLocation();
638 Range = A->getRange();
639 } else
640 llvm_unreachable("Not an alias or ifunc?");
641
642 StringRef MangledName = getMangledName(GD);
643 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
644 const llvm::GlobalValue *GV = nullptr;
645 if (!checkAliasedGlobal(getContext(), Diags, Location, IsIFunc, Alias, GV,
646 MangledDeclNames, Range)) {
647 Error = true;
648 continue;
649 }
650
651 llvm::Constant *Aliasee =
652 IsIFunc ? cast<llvm::GlobalIFunc>(Alias)->getResolver()
653 : cast<llvm::GlobalAlias>(Alias)->getAliasee();
654
655 llvm::GlobalValue *AliaseeGV;
656 if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
657 AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
658 else
659 AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
660
661 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
662 StringRef AliasSection = SA->getName();
663 if (AliasSection != AliaseeGV->getSection())
664 Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
665 << AliasSection << IsIFunc << IsIFunc;
666 }
667
668 // We have to handle alias to weak aliases in here. LLVM itself disallows
669 // this since the object semantics would not match the IL one. For
670 // compatibility with gcc we implement it by just pointing the alias
671 // to its aliasee's aliasee. We also warn, since the user is probably
672 // expecting the link to be weak.
673 if (auto *GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
674 if (GA->isInterposable()) {
675 Diags.Report(Location, diag::warn_alias_to_weak_alias)
676 << GV->getName() << GA->getName() << IsIFunc;
677 Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
678 GA->getAliasee(), Alias->getType());
679
680 if (IsIFunc)
681 cast<llvm::GlobalIFunc>(Alias)->setResolver(Aliasee);
682 else
683 cast<llvm::GlobalAlias>(Alias)->setAliasee(Aliasee);
684 }
685 }
686 }
687 if (!Error)
688 return;
689
690 for (const GlobalDecl &GD : Aliases) {
691 StringRef MangledName = getMangledName(GD);
692 llvm::GlobalValue *Alias = GetGlobalValue(MangledName);
693 Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
694 Alias->eraseFromParent();
695 }
696}
697
699 DeferredDeclsToEmit.clear();
700 EmittedDeferredDecls.clear();
701 if (OpenMPRuntime)
702 OpenMPRuntime->clear();
703}
704
706 StringRef MainFile) {
707 if (!hasDiagnostics())
708 return;
709 if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
710 if (MainFile.empty())
711 MainFile = "<stdin>";
712 Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
713 } else {
714 if (Mismatched > 0)
715 Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
716
717 if (Missing > 0)
718 Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
719 }
720}
721
723 llvm::Module &M) {
724 if (!LO.VisibilityFromDLLStorageClass)
725 return;
726
727 llvm::GlobalValue::VisibilityTypes DLLExportVisibility =
728 CodeGenModule::GetLLVMVisibility(LO.getDLLExportVisibility());
729 llvm::GlobalValue::VisibilityTypes NoDLLStorageClassVisibility =
730 CodeGenModule::GetLLVMVisibility(LO.getNoDLLStorageClassVisibility());
731 llvm::GlobalValue::VisibilityTypes ExternDeclDLLImportVisibility =
732 CodeGenModule::GetLLVMVisibility(LO.getExternDeclDLLImportVisibility());
733 llvm::GlobalValue::VisibilityTypes ExternDeclNoDLLStorageClassVisibility =
735 LO.getExternDeclNoDLLStorageClassVisibility());
736
737 for (llvm::GlobalValue &GV : M.global_values()) {
738 if (GV.hasAppendingLinkage() || GV.hasLocalLinkage())
739 continue;
740
741 // Reset DSO locality before setting the visibility. This removes
742 // any effects that visibility options and annotations may have
743 // had on the DSO locality. Setting the visibility will implicitly set
744 // appropriate globals to DSO Local; however, this will be pessimistic
745 // w.r.t. to the normal compiler IRGen.
746 GV.setDSOLocal(false);
747
748 if (GV.isDeclarationForLinker()) {
749 GV.setVisibility(GV.getDLLStorageClass() ==
750 llvm::GlobalValue::DLLImportStorageClass
751 ? ExternDeclDLLImportVisibility
752 : ExternDeclNoDLLStorageClassVisibility);
753 } else {
754 GV.setVisibility(GV.getDLLStorageClass() ==
755 llvm::GlobalValue::DLLExportStorageClass
756 ? DLLExportVisibility
757 : NoDLLStorageClassVisibility);
758 }
759
760 GV.setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
761 }
762}
763
766 if (CXX20ModuleInits && Primary && !Primary->isHeaderLikeModule())
767 EmitModuleInitializers(Primary);
768 EmitDeferred();
769 DeferredDecls.insert(EmittedDeferredDecls.begin(),
770 EmittedDeferredDecls.end());
771 EmittedDeferredDecls.clear();
772 EmitVTablesOpportunistically();
773 applyGlobalValReplacements();
774 applyReplacements();
775 emitMultiVersionFunctions();
776
777 if (Context.getLangOpts().IncrementalExtensions &&
778 GlobalTopLevelStmtBlockInFlight.first) {
779 const TopLevelStmtDecl *TLSD = GlobalTopLevelStmtBlockInFlight.second;
780 GlobalTopLevelStmtBlockInFlight.first->FinishFunction(TLSD->getEndLoc());
781 GlobalTopLevelStmtBlockInFlight = {nullptr, nullptr};
782 }
783
784 // Module implementations are initialized the same way as a regular TU that
785 // imports one or more modules.
786 if (CXX20ModuleInits && Primary && Primary->isInterfaceOrPartition())
787 EmitCXXModuleInitFunc(Primary);
788 else
789 EmitCXXGlobalInitFunc();
790 EmitCXXGlobalCleanUpFunc();
791 registerGlobalDtorsWithAtExit();
792 EmitCXXThreadLocalInitFunc();
793 if (ObjCRuntime)
794 if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
795 AddGlobalCtor(ObjCInitFunction);
796 if (Context.getLangOpts().CUDA && CUDARuntime) {
797 if (llvm::Function *CudaCtorFunction = CUDARuntime->finalizeModule())
798 AddGlobalCtor(CudaCtorFunction);
799 }
800 if (OpenMPRuntime) {
801 if (llvm::Function *OpenMPRequiresDirectiveRegFun =
802 OpenMPRuntime->emitRequiresDirectiveRegFun()) {
803 AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
804 }
805 OpenMPRuntime->createOffloadEntriesAndInfoMetadata();
806 OpenMPRuntime->clear();
807 }
808 if (PGOReader) {
809 getModule().setProfileSummary(
810 PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
811 llvm::ProfileSummary::PSK_Instr);
812 if (PGOStats.hasDiagnostics())
813 PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
814 }
815 llvm::stable_sort(GlobalCtors, [](const Structor &L, const Structor &R) {
816 return L.LexOrder < R.LexOrder;
817 });
818 EmitCtorList(GlobalCtors, "llvm.global_ctors");
819 EmitCtorList(GlobalDtors, "llvm.global_dtors");
821 EmitStaticExternCAliases();
822 checkAliases();
825 if (CoverageMapping)
826 CoverageMapping->emit();
827 if (CodeGenOpts.SanitizeCfiCrossDso) {
830 }
831 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
833 emitAtAvailableLinkGuard();
834 if (Context.getTargetInfo().getTriple().isWasm())
836
837 if (getTriple().isAMDGPU()) {
838 // Emit amdgpu_code_object_version module flag, which is code object version
839 // times 100.
840 if (getTarget().getTargetOpts().CodeObjectVersion !=
842 getModule().addModuleFlag(llvm::Module::Error,
843 "amdgpu_code_object_version",
844 getTarget().getTargetOpts().CodeObjectVersion);
845 }
846
847 // Currently, "-mprintf-kind" option is only supported for HIP
848 if (LangOpts.HIP) {
849 auto *MDStr = llvm::MDString::get(
850 getLLVMContext(), (getTarget().getTargetOpts().AMDGPUPrintfKindVal ==
852 ? "hostcall"
853 : "buffered");
854 getModule().addModuleFlag(llvm::Module::Error, "amdgpu_printf_kind",
855 MDStr);
856 }
857 }
858
859 // Emit a global array containing all external kernels or device variables
860 // used by host functions and mark it as used for CUDA/HIP. This is necessary
861 // to get kernels or device variables in archives linked in even if these
862 // kernels or device variables are only used in host functions.
863 if (!Context.CUDAExternalDeviceDeclODRUsedByHost.empty()) {
865 for (auto D : Context.CUDAExternalDeviceDeclODRUsedByHost) {
866 GlobalDecl GD;
867 if (auto *FD = dyn_cast<FunctionDecl>(D))
869 else
870 GD = GlobalDecl(D);
871 UsedArray.push_back(llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
873 }
874
875 llvm::ArrayType *ATy = llvm::ArrayType::get(Int8PtrTy, UsedArray.size());
876
877 auto *GV = new llvm::GlobalVariable(
878 getModule(), ATy, false, llvm::GlobalValue::InternalLinkage,
879 llvm::ConstantArray::get(ATy, UsedArray), "__clang_gpu_used_external");
881 }
882
883 emitLLVMUsed();
884 if (SanStats)
885 SanStats->finish();
886
887 if (CodeGenOpts.Autolink &&
888 (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
889 EmitModuleLinkOptions();
890 }
891
892 // On ELF we pass the dependent library specifiers directly to the linker
893 // without manipulating them. This is in contrast to other platforms where
894 // they are mapped to a specific linker option by the compiler. This
895 // difference is a result of the greater variety of ELF linkers and the fact
896 // that ELF linkers tend to handle libraries in a more complicated fashion
897 // than on other platforms. This forces us to defer handling the dependent
898 // libs to the linker.
899 //
900 // CUDA/HIP device and host libraries are different. Currently there is no
901 // way to differentiate dependent libraries for host or device. Existing
902 // usage of #pragma comment(lib, *) is intended for host libraries on
903 // Windows. Therefore emit llvm.dependent-libraries only for host.
904 if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
905 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
906 for (auto *MD : ELFDependentLibraries)
907 NMD->addOperand(MD);
908 }
909
910 // Record mregparm value now so it is visible through rest of codegen.
911 if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
912 getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
913 CodeGenOpts.NumRegisterParameters);
914
915 if (CodeGenOpts.DwarfVersion) {
916 getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version",
917 CodeGenOpts.DwarfVersion);
918 }
919
920 if (CodeGenOpts.Dwarf64)
921 getModule().addModuleFlag(llvm::Module::Max, "DWARF64", 1);
922
923 if (Context.getLangOpts().SemanticInterposition)
924 // Require various optimization to respect semantic interposition.
925 getModule().setSemanticInterposition(true);
926
927 if (CodeGenOpts.EmitCodeView) {
928 // Indicate that we want CodeView in the metadata.
929 getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
930 }
931 if (CodeGenOpts.CodeViewGHash) {
932 getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
933 }
934 if (CodeGenOpts.ControlFlowGuard) {
935 // Function ID tables and checks for Control Flow Guard (cfguard=2).
936 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 2);
937 } else if (CodeGenOpts.ControlFlowGuardNoChecks) {
938 // Function ID tables for Control Flow Guard (cfguard=1).
939 getModule().addModuleFlag(llvm::Module::Warning, "cfguard", 1);
940 }
941 if (CodeGenOpts.EHContGuard) {
942 // Function ID tables for EH Continuation Guard.
943 getModule().addModuleFlag(llvm::Module::Warning, "ehcontguard", 1);
944 }
945 if (Context.getLangOpts().Kernel) {
946 // Note if we are compiling with /kernel.
947 getModule().addModuleFlag(llvm::Module::Warning, "ms-kernel", 1);
948 }
949 if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
950 // We don't support LTO with 2 with different StrictVTablePointers
951 // FIXME: we could support it by stripping all the information introduced
952 // by StrictVTablePointers.
953
954 getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
955
956 llvm::Metadata *Ops[2] = {
957 llvm::MDString::get(VMContext, "StrictVTablePointers"),
958 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
959 llvm::Type::getInt32Ty(VMContext), 1))};
960
961 getModule().addModuleFlag(llvm::Module::Require,
962 "StrictVTablePointersRequirement",
963 llvm::MDNode::get(VMContext, Ops));
964 }
965 if (getModuleDebugInfo())
966 // We support a single version in the linked module. The LLVM
967 // parser will drop debug info with a different version number
968 // (and warn about it, too).
969 getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
970 llvm::DEBUG_METADATA_VERSION);
971
972 // We need to record the widths of enums and wchar_t, so that we can generate
973 // the correct build attributes in the ARM backend. wchar_size is also used by
974 // TargetLibraryInfo.
975 uint64_t WCharWidth =
977 getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
978
979 llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
980 if ( Arch == llvm::Triple::arm
981 || Arch == llvm::Triple::armeb
982 || Arch == llvm::Triple::thumb
983 || Arch == llvm::Triple::thumbeb) {
984 // The minimum width of an enum in bytes
985 uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
986 getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
987 }
988
989 if (Arch == llvm::Triple::riscv32 || Arch == llvm::Triple::riscv64) {
990 StringRef ABIStr = Target.getABI();
991 llvm::LLVMContext &Ctx = TheModule.getContext();
992 getModule().addModuleFlag(llvm::Module::Error, "target-abi",
993 llvm::MDString::get(Ctx, ABIStr));
994 }
995
996 if (CodeGenOpts.SanitizeCfiCrossDso) {
997 // Indicate that we want cross-DSO control flow integrity checks.
998 getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
999 }
1000
1001 if (CodeGenOpts.WholeProgramVTables) {
1002 // Indicate whether VFE was enabled for this module, so that the
1003 // vcall_visibility metadata added under whole program vtables is handled
1004 // appropriately in the optimizer.
1005 getModule().addModuleFlag(llvm::Module::Error, "Virtual Function Elim",
1006 CodeGenOpts.VirtualFunctionElimination);
1007 }
1008
1009 if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
1010 getModule().addModuleFlag(llvm::Module::Override,
1011 "CFI Canonical Jump Tables",
1012 CodeGenOpts.SanitizeCfiCanonicalJumpTables);
1013 }
1014
1015 if (LangOpts.Sanitize.has(SanitizerKind::KCFI)) {
1016 getModule().addModuleFlag(llvm::Module::Override, "kcfi", 1);
1017 // KCFI assumes patchable-function-prefix is the same for all indirectly
1018 // called functions. Store the expected offset for code generation.
1019 if (CodeGenOpts.PatchableFunctionEntryOffset)
1020 getModule().addModuleFlag(llvm::Module::Override, "kcfi-offset",
1021 CodeGenOpts.PatchableFunctionEntryOffset);
1022 }
1023
1024 if (CodeGenOpts.CFProtectionReturn &&
1026 // Indicate that we want to instrument return control flow protection.
1027 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-return",
1028 1);
1029 }
1030
1031 if (CodeGenOpts.CFProtectionBranch &&
1033 // Indicate that we want to instrument branch control flow protection.
1034 getModule().addModuleFlag(llvm::Module::Min, "cf-protection-branch",
1035 1);
1036 }
1037
1038 if (CodeGenOpts.FunctionReturnThunks)
1039 getModule().addModuleFlag(llvm::Module::Override, "function_return_thunk_extern", 1);
1040
1041 if (CodeGenOpts.IndirectBranchCSPrefix)
1042 getModule().addModuleFlag(llvm::Module::Override, "indirect_branch_cs_prefix", 1);
1043
1044 // Add module metadata for return address signing (ignoring
1045 // non-leaf/all) and stack tagging. These are actually turned on by function
1046 // attributes, but we use module metadata to emit build attributes. This is
1047 // needed for LTO, where the function attributes are inside bitcode
1048 // serialised into a global variable by the time build attributes are
1049 // emitted, so we can't access them. LTO objects could be compiled with
1050 // different flags therefore module flags are set to "Min" behavior to achieve
1051 // the same end result of the normal build where e.g BTI is off if any object
1052 // doesn't support it.
1053 if (Context.getTargetInfo().hasFeature("ptrauth") &&
1054 LangOpts.getSignReturnAddressScope() !=
1056 getModule().addModuleFlag(llvm::Module::Override,
1057 "sign-return-address-buildattr", 1);
1058 if (LangOpts.Sanitize.has(SanitizerKind::MemtagStack))
1059 getModule().addModuleFlag(llvm::Module::Override,
1060 "tag-stack-memory-buildattr", 1);
1061
1062 if (Arch == llvm::Triple::thumb || Arch == llvm::Triple::thumbeb ||
1063 Arch == llvm::Triple::arm || Arch == llvm::Triple::armeb ||
1064 Arch == llvm::Triple::aarch64 || Arch == llvm::Triple::aarch64_32 ||
1065 Arch == llvm::Triple::aarch64_be) {
1066 if (LangOpts.BranchTargetEnforcement)
1067 getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
1068 1);
1069 if (LangOpts.hasSignReturnAddress())
1070 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
1071 if (LangOpts.isSignReturnAddressScopeAll())
1072 getModule().addModuleFlag(llvm::Module::Min, "sign-return-address-all",
1073 1);
1074 if (!LangOpts.isSignReturnAddressWithAKey())
1075 getModule().addModuleFlag(llvm::Module::Min,
1076 "sign-return-address-with-bkey", 1);
1077 }
1078
1079 if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1080 llvm::LLVMContext &Ctx = TheModule.getContext();
1081 getModule().addModuleFlag(
1082 llvm::Module::Error, "MemProfProfileFilename",
1083 llvm::MDString::get(Ctx, CodeGenOpts.MemoryProfileOutput));
1084 }
1085
1086 if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
1087 // Indicate whether __nvvm_reflect should be configured to flush denormal
1088 // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
1089 // property.)
1090 getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
1091 CodeGenOpts.FP32DenormalMode.Output !=
1092 llvm::DenormalMode::IEEE);
1093 }
1094
1095 if (LangOpts.EHAsynch)
1096 getModule().addModuleFlag(llvm::Module::Warning, "eh-asynch", 1);
1097
1098 // Indicate whether this Module was compiled with -fopenmp
1099 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
1100 getModule().addModuleFlag(llvm::Module::Max, "openmp", LangOpts.OpenMP);
1101 if (getLangOpts().OpenMPIsTargetDevice)
1102 getModule().addModuleFlag(llvm::Module::Max, "openmp-device",
1103 LangOpts.OpenMP);
1104
1105 // Emit OpenCL specific module metadata: OpenCL/SPIR version.
1106 if (LangOpts.OpenCL || (LangOpts.CUDAIsDevice && getTriple().isSPIRV())) {
1107 EmitOpenCLMetadata();
1108 // Emit SPIR version.
1109 if (getTriple().isSPIR()) {
1110 // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
1111 // opencl.spir.version named metadata.
1112 // C++ for OpenCL has a distinct mapping for version compatibility with
1113 // OpenCL.
1114 auto Version = LangOpts.getOpenCLCompatibleVersion();
1115 llvm::Metadata *SPIRVerElts[] = {
1116 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1117 Int32Ty, Version / 100)),
1118 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1119 Int32Ty, (Version / 100 > 1) ? 0 : 2))};
1120 llvm::NamedMDNode *SPIRVerMD =
1121 TheModule.getOrInsertNamedMetadata("opencl.spir.version");
1122 llvm::LLVMContext &Ctx = TheModule.getContext();
1123 SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
1124 }
1125 }
1126
1127 // HLSL related end of code gen work items.
1128 if (LangOpts.HLSL)
1130
1131 if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
1132 assert(PLevel < 3 && "Invalid PIC Level");
1133 getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
1134 if (Context.getLangOpts().PIE)
1135 getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
1136 }
1137
1138 if (getCodeGenOpts().CodeModel.size() > 0) {
1139 unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
1140 .Case("tiny", llvm::CodeModel::Tiny)
1141 .Case("small", llvm::CodeModel::Small)
1142 .Case("kernel", llvm::CodeModel::Kernel)
1143 .Case("medium", llvm::CodeModel::Medium)
1144 .Case("large", llvm::CodeModel::Large)
1145 .Default(~0u);
1146 if (CM != ~0u) {
1147 llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
1148 getModule().setCodeModel(codeModel);
1149
1150 if (CM == llvm::CodeModel::Medium &&
1151 Context.getTargetInfo().getTriple().getArch() ==
1152 llvm::Triple::x86_64) {
1153 getModule().setLargeDataThreshold(getCodeGenOpts().LargeDataThreshold);
1154 }
1155 }
1156 }
1157
1158 if (CodeGenOpts.NoPLT)
1159 getModule().setRtLibUseGOT();
1160 if (getTriple().isOSBinFormatELF() &&
1161 CodeGenOpts.DirectAccessExternalData !=
1162 getModule().getDirectAccessExternalData()) {
1163 getModule().setDirectAccessExternalData(
1164 CodeGenOpts.DirectAccessExternalData);
1165 }
1166 if (CodeGenOpts.UnwindTables)
1167 getModule().setUwtable(llvm::UWTableKind(CodeGenOpts.UnwindTables));
1168
1169 switch (CodeGenOpts.getFramePointer()) {
1171 // 0 ("none") is the default.
1172 break;
1174 getModule().setFramePointer(llvm::FramePointerKind::NonLeaf);
1175 break;
1177 getModule().setFramePointer(llvm::FramePointerKind::All);
1178 break;
1179 }
1180
1181 SimplifyPersonality();
1182
1183 if (getCodeGenOpts().EmitDeclMetadata)
1184 EmitDeclMetadata();
1185
1186 if (getCodeGenOpts().CoverageNotesFile.size() ||
1187 getCodeGenOpts().CoverageDataFile.size())
1188 EmitCoverageFile();
1189
1190 if (CGDebugInfo *DI = getModuleDebugInfo())
1191 DI->finalize();
1192
1193 if (getCodeGenOpts().EmitVersionIdentMetadata)
1194 EmitVersionIdentMetadata();
1195
1196 if (!getCodeGenOpts().RecordCommandLine.empty())
1197 EmitCommandLineMetadata();
1198
1199 if (!getCodeGenOpts().StackProtectorGuard.empty())
1200 getModule().setStackProtectorGuard(getCodeGenOpts().StackProtectorGuard);
1201 if (!getCodeGenOpts().StackProtectorGuardReg.empty())
1202 getModule().setStackProtectorGuardReg(
1203 getCodeGenOpts().StackProtectorGuardReg);
1204 if (!getCodeGenOpts().StackProtectorGuardSymbol.empty())
1205 getModule().setStackProtectorGuardSymbol(
1206 getCodeGenOpts().StackProtectorGuardSymbol);
1207 if (getCodeGenOpts().StackProtectorGuardOffset != INT_MAX)
1208 getModule().setStackProtectorGuardOffset(
1209 getCodeGenOpts().StackProtectorGuardOffset);
1210 if (getCodeGenOpts().StackAlignment)
1211 getModule().setOverrideStackAlignment(getCodeGenOpts().StackAlignment);
1212 if (getCodeGenOpts().SkipRaxSetup)
1213 getModule().addModuleFlag(llvm::Module::Override, "SkipRaxSetup", 1);
1214 if (getLangOpts().RegCall4)
1215 getModule().addModuleFlag(llvm::Module::Override, "RegCallv4", 1);
1216
1217 if (getContext().getTargetInfo().getMaxTLSAlign())
1218 getModule().addModuleFlag(llvm::Module::Error, "MaxTLSAlign",
1219 getContext().getTargetInfo().getMaxTLSAlign());
1220
1222
1223 getTargetCodeGenInfo().emitTargetMetadata(*this, MangledDeclNames);
1224
1225 EmitBackendOptionsMetadata(getCodeGenOpts());
1226
1227 // If there is device offloading code embed it in the host now.
1228 EmbedObject(&getModule(), CodeGenOpts, getDiags());
1229
1230 // Set visibility from DLL storage class
1231 // We do this at the end of LLVM IR generation; after any operation
1232 // that might affect the DLL storage class or the visibility, and
1233 // before anything that might act on these.
1235}
1236
1237void CodeGenModule::EmitOpenCLMetadata() {
1238 // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
1239 // opencl.ocl.version named metadata node.
1240 // C++ for OpenCL has a distinct mapping for versions compatibile with OpenCL.
1241 auto Version = LangOpts.getOpenCLCompatibleVersion();
1242 llvm::Metadata *OCLVerElts[] = {
1243 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1244 Int32Ty, Version / 100)),
1245 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1246 Int32Ty, (Version % 100) / 10))};
1247 llvm::NamedMDNode *OCLVerMD =
1248 TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
1249 llvm::LLVMContext &Ctx = TheModule.getContext();
1250 OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
1251}
1252
1253void CodeGenModule::EmitBackendOptionsMetadata(
1254 const CodeGenOptions &CodeGenOpts) {
1255 if (getTriple().isRISCV()) {
1256 getModule().addModuleFlag(llvm::Module::Min, "SmallDataLimit",
1257 CodeGenOpts.SmallDataLimit);
1258 }
1259}
1260
1262 // Make sure that this type is translated.
1263 Types.UpdateCompletedType(TD);
1264}
1265
1267 // Make sure that this type is translated.
1268 Types.RefreshTypeCacheForClass(RD);
1269}
1270
1272 if (!TBAA)
1273 return nullptr;
1274 return TBAA->getTypeInfo(QTy);
1275}
1276
1278 if (!TBAA)
1279 return TBAAAccessInfo();
1280 if (getLangOpts().CUDAIsDevice) {
1281 // As CUDA builtin surface/texture types are replaced, skip generating TBAA
1282 // access info.
1283 if (AccessType->isCUDADeviceBuiltinSurfaceType()) {
1284 if (getTargetCodeGenInfo().getCUDADeviceBuiltinSurfaceDeviceType() !=
1285 nullptr)
1286 return TBAAAccessInfo();
1287 } else if (AccessType->isCUDADeviceBuiltinTextureType()) {
1288 if (getTargetCodeGenInfo().getCUDADeviceBuiltinTextureDeviceType() !=
1289 nullptr)
1290 return TBAAAccessInfo();
1291 }
1292 }
1293 return TBAA->getAccessInfo(AccessType);
1294}
1295
1298 if (!TBAA)
1299 return TBAAAccessInfo();
1300 return TBAA->getVTablePtrAccessInfo(VTablePtrType);
1301}
1302
1304 if (!TBAA)
1305 return nullptr;
1306 return TBAA->getTBAAStructInfo(QTy);
1307}
1308
1310 if (!TBAA)
1311 return nullptr;
1312 return TBAA->getBaseTypeInfo(QTy);
1313}
1314
1316 if (!TBAA)
1317 return nullptr;
1318 return TBAA->getAccessTagInfo(Info);
1319}
1320
1323 if (!TBAA)
1324 return TBAAAccessInfo();
1325 return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
1326}
1327
1330 TBAAAccessInfo InfoB) {
1331 if (!TBAA)
1332 return TBAAAccessInfo();
1333 return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
1334}
1335
1338 TBAAAccessInfo SrcInfo) {
1339 if (!TBAA)
1340 return TBAAAccessInfo();
1341 return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
1342}
1343
1345 TBAAAccessInfo TBAAInfo) {
1346 if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
1347 Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
1348}
1349
1351 llvm::Instruction *I, const CXXRecordDecl *RD) {
1352 I->setMetadata(llvm::LLVMContext::MD_invariant_group,
1353 llvm::MDNode::get(getLLVMContext(), {}));
1354}
1355
1356void CodeGenModule::Error(SourceLocation loc, StringRef message) {
1357 unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
1358 getDiags().Report(Context.getFullLoc(loc), diagID) << message;
1359}
1360
1361/// ErrorUnsupported - Print out an error that codegen doesn't support the
1362/// specified stmt yet.
1363void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
1365 "cannot compile this %0 yet");
1366 std::string Msg = Type;
1367 getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
1368 << Msg << S->getSourceRange();
1369}
1370
1371/// ErrorUnsupported - Print out an error that codegen doesn't support the
1372/// specified decl yet.
1373void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
1375 "cannot compile this %0 yet");
1376 std::string Msg = Type;
1377 getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
1378}
1379
1380llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
1381 return llvm::ConstantInt::get(SizeTy, size.getQuantity());
1382}
1383
1384void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
1385 const NamedDecl *D) const {
1386 // Internal definitions always have default visibility.
1387 if (GV->hasLocalLinkage()) {
1388 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1389 return;
1390 }
1391 if (!D)
1392 return;
1393 // Set visibility for definitions, and for declarations if requested globally
1394 // or set explicitly.
1396 if (GV->hasDLLExportStorageClass() || GV->hasDLLImportStorageClass()) {
1397 // Reject incompatible dlllstorage and visibility annotations.
1398 if (!LV.isVisibilityExplicit())
1399 return;
1400 if (GV->hasDLLExportStorageClass()) {
1401 if (LV.getVisibility() == HiddenVisibility)
1403 diag::err_hidden_visibility_dllexport);
1404 } else if (LV.getVisibility() != DefaultVisibility) {
1406 diag::err_non_default_visibility_dllimport);
1407 }
1408 return;
1409 }
1410
1411 if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
1412 !GV->isDeclarationForLinker())
1413 GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
1414}
1415
1417 llvm::GlobalValue *GV) {
1418 if (GV->hasLocalLinkage())
1419 return true;
1420
1421 if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
1422 return true;
1423
1424 // DLLImport explicitly marks the GV as external.
1425 if (GV->hasDLLImportStorageClass())
1426 return false;
1427
1428 const llvm::Triple &TT = CGM.getTriple();
1429 const auto &CGOpts = CGM.getCodeGenOpts();
1430 if (TT.isWindowsGNUEnvironment()) {
1431 // In MinGW, variables without DLLImport can still be automatically
1432 // imported from a DLL by the linker; don't mark variables that
1433 // potentially could come from another DLL as DSO local.
1434
1435 // With EmulatedTLS, TLS variables can be autoimported from other DLLs
1436 // (and this actually happens in the public interface of libstdc++), so
1437 // such variables can't be marked as DSO local. (Native TLS variables
1438 // can't be dllimported at all, though.)
1439 if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
1440 (!GV->isThreadLocal() || CGM.getCodeGenOpts().EmulatedTLS) &&
1441 CGOpts.AutoImport)
1442 return false;
1443 }
1444
1445 // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
1446 // remain unresolved in the link, they can be resolved to zero, which is
1447 // outside the current DSO.
1448 if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
1449 return false;
1450
1451 // Every other GV is local on COFF.
1452 // Make an exception for windows OS in the triple: Some firmware builds use
1453 // *-win32-macho triples. This (accidentally?) produced windows relocations
1454 // without GOT tables in older clang versions; Keep this behaviour.
1455 // FIXME: even thread local variables?
1456 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
1457 return true;
1458
1459 // Only handle COFF and ELF for now.
1460 if (!TT.isOSBinFormatELF())
1461 return false;
1462
1463 // If this is not an executable, don't assume anything is local.
1464 llvm::Reloc::Model RM = CGOpts.RelocationModel;
1465 const auto &LOpts = CGM.getLangOpts();
1466 if (RM != llvm::Reloc::Static && !LOpts.PIE) {
1467 // On ELF, if -fno-semantic-interposition is specified and the target
1468 // supports local aliases, there will be neither CC1
1469 // -fsemantic-interposition nor -fhalf-no-semantic-interposition. Set
1470 // dso_local on the function if using a local alias is preferable (can avoid
1471 // PLT indirection).
1472 if (!(isa<llvm::Function>(GV) && GV->canBenefitFromLocalAlias()))
1473 return false;
1474 return !(CGM.getLangOpts().SemanticInterposition ||
1475 CGM.getLangOpts().HalfNoSemanticInterposition);
1476 }
1477
1478 // A definition cannot be preempted from an executable.
1479 if (!GV->isDeclarationForLinker())
1480 return true;
1481
1482 // Most PIC code sequences that assume that a symbol is local cannot produce a
1483 // 0 if it turns out the symbol is undefined. While this is ABI and relocation
1484 // depended, it seems worth it to handle it here.
1485 if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
1486 return false;
1487
1488 // PowerPC64 prefers TOC indirection to avoid copy relocations.
1489 if (TT.isPPC64())
1490 return false;
1491
1492 if (CGOpts.DirectAccessExternalData) {
1493 // If -fdirect-access-external-data (default for -fno-pic), set dso_local
1494 // for non-thread-local variables. If the symbol is not defined in the
1495 // executable, a copy relocation will be needed at link time. dso_local is
1496 // excluded for thread-local variables because they generally don't support
1497 // copy relocations.
1498 if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
1499 if (!Var->isThreadLocal())
1500 return true;
1501
1502 // -fno-pic sets dso_local on a function declaration to allow direct
1503 // accesses when taking its address (similar to a data symbol). If the
1504 // function is not defined in the executable, a canonical PLT entry will be
1505 // needed at link time. -fno-direct-access-external-data can avoid the
1506 // canonical PLT entry. We don't generalize this condition to -fpie/-fpic as
1507 // it could just cause trouble without providing perceptible benefits.
1508 if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
1509 return true;
1510 }
1511
1512 // If we can use copy relocations we can assume it is local.
1513
1514 // Otherwise don't assume it is local.
1515 return false;
1516}
1517
1518void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
1519 GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
1520}
1521
1522void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1523 GlobalDecl GD) const {
1524 const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
1525 // C++ destructors have a few C++ ABI specific special cases.
1526 if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
1528 return;
1529 }
1530 setDLLImportDLLExport(GV, D);
1531}
1532
1533void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
1534 const NamedDecl *D) const {
1535 if (D && D->isExternallyVisible()) {
1536 if (D->hasAttr<DLLImportAttr>())
1537 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
1538 else if ((D->hasAttr<DLLExportAttr>() ||
1540 !GV->isDeclarationForLinker())
1541 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
1542 }
1543}
1544
1545void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1546 GlobalDecl GD) const {
1547 setDLLImportDLLExport(GV, GD);
1548 setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
1549}
1550
1551void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
1552 const NamedDecl *D) const {
1553 setDLLImportDLLExport(GV, D);
1554 setGVPropertiesAux(GV, D);
1555}
1556
1557void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
1558 const NamedDecl *D) const {
1559 setGlobalVisibility(GV, D);
1560 setDSOLocal(GV);
1561 GV->setPartition(CodeGenOpts.SymbolPartition);
1562}
1563
1564static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
1565 return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
1566 .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
1567 .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
1568 .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
1569 .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
1570}
1571
1572llvm::GlobalVariable::ThreadLocalMode
1574 switch (CodeGenOpts.getDefaultTLSModel()) {
1576 return llvm::GlobalVariable::GeneralDynamicTLSModel;
1578 return llvm::GlobalVariable::LocalDynamicTLSModel;
1580 return llvm::GlobalVariable::InitialExecTLSModel;
1582 return llvm::GlobalVariable::LocalExecTLSModel;
1583 }
1584 llvm_unreachable("Invalid TLS model!");
1585}
1586
1587void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
1588 assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
1589
1590 llvm::GlobalValue::ThreadLocalMode TLM;
1591 TLM = GetDefaultLLVMTLSModel();
1592
1593 // Override the TLS model if it is explicitly specified.
1594 if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
1595 TLM = GetLLVMTLSModel(Attr->getModel());
1596 }
1597
1598 GV->setThreadLocalMode(TLM);
1599}
1600
1601static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
1602 StringRef Name) {
1603 const TargetInfo &Target = CGM.getTarget();
1604 return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
1605}
1606
1608 const CPUSpecificAttr *Attr,
1609 unsigned CPUIndex,
1610 raw_ostream &Out) {
1611 // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
1612 // supported.
1613 if (Attr)
1614 Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
1615 else if (CGM.getTarget().supportsIFunc())
1616 Out << ".resolver";
1617}
1618
1620 const TargetVersionAttr *Attr,
1621 raw_ostream &Out) {
1622 if (Attr->isDefaultVersion())
1623 return;
1624 Out << "._";
1625 const TargetInfo &TI = CGM.getTarget();
1627 Attr->getFeatures(Feats);
1628 llvm::stable_sort(Feats, [&TI](const StringRef FeatL, const StringRef FeatR) {
1629 return TI.multiVersionSortPriority(FeatL) <
1630 TI.multiVersionSortPriority(FeatR);
1631 });
1632 for (const auto &Feat : Feats) {
1633 Out << 'M';
1634 Out << Feat;
1635 }
1636}
1637
1639 const TargetAttr *Attr, raw_ostream &Out) {
1640 if (Attr->isDefaultVersion())
1641 return;
1642
1643 Out << '.';
1644 const TargetInfo &Target = CGM.getTarget();
1645 ParsedTargetAttr Info = Target.parseTargetAttr(Attr->getFeaturesStr());
1646 llvm::sort(Info.Features, [&Target](StringRef LHS, StringRef RHS) {
1647 // Multiversioning doesn't allow "no-${feature}", so we can
1648 // only have "+" prefixes here.
1649 assert(LHS.startswith("+") && RHS.startswith("+") &&
1650 "Features should always have a prefix.");
1651 return Target.multiVersionSortPriority(LHS.substr(1)) >
1652 Target.multiVersionSortPriority(RHS.substr(1));
1653 });
1654
1655 bool IsFirst = true;
1656
1657 if (!Info.CPU.empty()) {
1658 IsFirst = false;
1659 Out << "arch_" << Info.CPU;
1660 }
1661
1662 for (StringRef Feat : Info.Features) {
1663 if (!IsFirst)
1664 Out << '_';
1665 IsFirst = false;
1666 Out << Feat.substr(1);
1667 }
1668}
1669
1670// Returns true if GD is a function decl with internal linkage and
1671// needs a unique suffix after the mangled name.
1673 CodeGenModule &CGM) {
1674 const Decl *D = GD.getDecl();
1675 return !CGM.getModuleNameHash().empty() && isa<FunctionDecl>(D) &&
1676 (CGM.getFunctionLinkage(GD) == llvm::GlobalValue::InternalLinkage);
1677}
1678
1680 const TargetClonesAttr *Attr,
1681 unsigned VersionIndex,
1682 raw_ostream &Out) {
1683 const TargetInfo &TI = CGM.getTarget();
1684 if (TI.getTriple().isAArch64()) {
1685 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex);
1686 if (FeatureStr == "default")
1687 return;
1688 Out << "._";
1690 FeatureStr.split(Features, "+");
1691 llvm::stable_sort(Features,
1692 [&TI](const StringRef FeatL, const StringRef FeatR) {
1693 return TI.multiVersionSortPriority(FeatL) <
1694 TI.multiVersionSortPriority(FeatR);
1695 });
1696 for (auto &Feat : Features) {
1697 Out << 'M';
1698 Out << Feat;
1699 }
1700 } else {
1701 Out << '.';
1702 StringRef FeatureStr = Attr->getFeatureStr(VersionIndex);
1703 if (FeatureStr.startswith("arch="))
1704 Out << "arch_" << FeatureStr.substr(sizeof("arch=") - 1);
1705 else
1706 Out << FeatureStr;
1707
1708 Out << '.' << Attr->getMangledIndex(VersionIndex);
1709 }
1710}
1711
1712static std::string getMangledNameImpl(CodeGenModule &CGM, GlobalDecl GD,
1713 const NamedDecl *ND,
1714 bool OmitMultiVersionMangling = false) {
1715 SmallString<256> Buffer;
1716 llvm::raw_svector_ostream Out(Buffer);
1718 if (!CGM.getModuleNameHash().empty())
1720 bool ShouldMangle = MC.shouldMangleDeclName(ND);
1721 if (ShouldMangle)
1722 MC.mangleName(GD.getWithDecl(ND), Out);
1723 else {
1724 IdentifierInfo *II = ND->getIdentifier();
1725 assert(II && "Attempt to mangle unnamed decl.");
1726 const auto *FD = dyn_cast<FunctionDecl>(ND);
1727
1728 if (FD &&
1729 FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1730 if (CGM.getLangOpts().RegCall4)
1731 Out << "__regcall4__" << II->getName();
1732 else
1733 Out << "__regcall3__" << II->getName();
1734 } else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
1736 Out << "__device_stub__" << II->getName();
1737 } else {
1738 Out << II->getName();
1739 }
1740 }
1741
1742 // Check if the module name hash should be appended for internal linkage
1743 // symbols. This should come before multi-version target suffixes are
1744 // appended. This is to keep the name and module hash suffix of the
1745 // internal linkage function together. The unique suffix should only be
1746 // added when name mangling is done to make sure that the final name can
1747 // be properly demangled. For example, for C functions without prototypes,
1748 // name mangling is not done and the unique suffix should not be appeneded
1749 // then.
1750 if (ShouldMangle && isUniqueInternalLinkageDecl(GD, CGM)) {
1751 assert(CGM.getCodeGenOpts().UniqueInternalLinkageNames &&
1752 "Hash computed when not explicitly requested");
1753 Out << CGM.getModuleNameHash();
1754 }
1755
1756 if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1757 if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1758 switch (FD->getMultiVersionKind()) {
1762 FD->getAttr<CPUSpecificAttr>(),
1763 GD.getMultiVersionIndex(), Out);
1764 break;
1766 AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1767 break;
1769 AppendTargetVersionMangling(CGM, FD->getAttr<TargetVersionAttr>(), Out);
1770 break;
1772 AppendTargetClonesMangling(CGM, FD->getAttr<TargetClonesAttr>(),
1773 GD.getMultiVersionIndex(), Out);
1774 break;
1776 llvm_unreachable("None multiversion type isn't valid here");
1777 }
1778 }
1779
1780 // Make unique name for device side static file-scope variable for HIP.
1781 if (CGM.getContext().shouldExternalize(ND) &&
1782 CGM.getLangOpts().GPURelocatableDeviceCode &&
1783 CGM.getLangOpts().CUDAIsDevice)
1785
1786 return std::string(Out.str());
1787}
1788
1789void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1790 const FunctionDecl *FD,
1791 StringRef &CurName) {
1792 if (!FD->isMultiVersion())
1793 return;
1794
1795 // Get the name of what this would be without the 'target' attribute. This
1796 // allows us to lookup the version that was emitted when this wasn't a
1797 // multiversion function.
1798 std::string NonTargetName =
1799 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1800 GlobalDecl OtherGD;
1801 if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1802 assert(OtherGD.getCanonicalDecl()
1803 .getDecl()
1804 ->getAsFunction()
1805 ->isMultiVersion() &&
1806 "Other GD should now be a multiversioned function");
1807 // OtherFD is the version of this function that was mangled BEFORE
1808 // becoming a MultiVersion function. It potentially needs to be updated.
1809 const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1810 .getDecl()
1811 ->getAsFunction()
1813 std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1814 // This is so that if the initial version was already the 'default'
1815 // version, we don't try to update it.
1816 if (OtherName != NonTargetName) {
1817 // Remove instead of erase, since others may have stored the StringRef
1818 // to this.
1819 const auto ExistingRecord = Manglings.find(NonTargetName);
1820 if (ExistingRecord != std::end(Manglings))
1821 Manglings.remove(&(*ExistingRecord));
1822 auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1823 StringRef OtherNameRef = MangledDeclNames[OtherGD.getCanonicalDecl()] =
1824 Result.first->first();
1825 // If this is the current decl is being created, make sure we update the name.
1826 if (GD.getCanonicalDecl() == OtherGD.getCanonicalDecl())
1827 CurName = OtherNameRef;
1828 if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1829 Entry->setName(OtherName);
1830 }
1831 }
1832}
1833
1835 GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1836
1837 // Some ABIs don't have constructor variants. Make sure that base and
1838 // complete constructors get mangled the same.
1839 if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1840 if (!getTarget().getCXXABI().hasConstructorVariants()) {
1841 CXXCtorType OrigCtorType = GD.getCtorType();
1842 assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
1843 if (OrigCtorType == Ctor_Base)
1844 CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1845 }
1846 }
1847
1848 // In CUDA/HIP device compilation with -fgpu-rdc, the mangled name of a
1849 // static device variable depends on whether the variable is referenced by
1850 // a host or device host function. Therefore the mangled name cannot be
1851 // cached.
1852 if (!LangOpts.CUDAIsDevice || !getContext().mayExternalize(GD.getDecl())) {
1853 auto FoundName = MangledDeclNames.find(CanonicalGD);
1854 if (FoundName != MangledDeclNames.end())
1855 return FoundName->second;
1856 }
1857
1858 // Keep the first result in the case of a mangling collision.
1859 const auto *ND = cast<NamedDecl>(GD.getDecl());
1860 std::string MangledName = getMangledNameImpl(*this, GD, ND);
1861
1862 // Ensure either we have different ABIs between host and device compilations,
1863 // says host compilation following MSVC ABI but device compilation follows
1864 // Itanium C++ ABI or, if they follow the same ABI, kernel names after
1865 // mangling should be the same after name stubbing. The later checking is
1866 // very important as the device kernel name being mangled in host-compilation
1867 // is used to resolve the device binaries to be executed. Inconsistent naming
1868 // result in undefined behavior. Even though we cannot check that naming
1869 // directly between host- and device-compilations, the host- and
1870 // device-mangling in host compilation could help catching certain ones.
1871 assert(!isa<FunctionDecl>(ND) || !ND->hasAttr<CUDAGlobalAttr>() ||
1872 getContext().shouldExternalize(ND) || getLangOpts().CUDAIsDevice ||
1873 (getContext().getAuxTargetInfo() &&
1874 (getContext().getAuxTargetInfo()->getCXXABI() !=
1875 getContext().getTargetInfo().getCXXABI())) ||
1876 getCUDARuntime().getDeviceSideName(ND) ==
1878 *this,
1880 ND));
1881
1882 auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1883 return MangledDeclNames[CanonicalGD] = Result.first->first();
1884}
1885
1887 const BlockDecl *BD) {
1888 MangleContext &MangleCtx = getCXXABI().getMangleContext();
1889 const Decl *D = GD.getDecl();
1890
1891 SmallString<256> Buffer;
1892 llvm::raw_svector_ostream Out(Buffer);
1893 if (!D)
1894 MangleCtx.mangleGlobalBlock(BD,
1895 dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1896 else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1897 MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1898 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1899 MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1900 else
1901 MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1902
1903 auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1904 return Result.first->first();
1905}
1906
1908 auto it = MangledDeclNames.begin();
1909 while (it != MangledDeclNames.end()) {
1910 if (it->second == Name)
1911 return it->first;
1912 it++;
1913 }
1914 return GlobalDecl();
1915}
1916
1917llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1918 return getModule().getNamedValue(Name);
1919}
1920
1921/// AddGlobalCtor - Add a function to the list that will be called before
1922/// main() runs.
1923void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1924 unsigned LexOrder,
1925 llvm::Constant *AssociatedData) {
1926 // FIXME: Type coercion of void()* types.
1927 GlobalCtors.push_back(Structor(Priority, LexOrder, Ctor, AssociatedData));
1928}
1929
1930/// AddGlobalDtor - Add a function to the list that will be called
1931/// when the module is unloaded.
1932void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority,
1933 bool IsDtorAttrFunc) {
1934 if (CodeGenOpts.RegisterGlobalDtorsWithAtExit &&
1935 (!getContext().getTargetInfo().getTriple().isOSAIX() || IsDtorAttrFunc)) {
1936 DtorsUsingAtExit[Priority].push_back(Dtor);
1937 return;
1938 }
1939
1940 // FIXME: Type coercion of void()* types.
1941 GlobalDtors.push_back(Structor(Priority, ~0U, Dtor, nullptr));
1942}
1943
1944void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1945 if (Fns.empty()) return;
1946
1947 // Ctor function type is void()*.
1948 llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1949 llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1950 TheModule.getDataLayout().getProgramAddressSpace());
1951
1952 // Get the type of a ctor entry, { i32, void ()*, i8* }.
1953 llvm::StructType *CtorStructTy = llvm::StructType::get(
1954 Int32Ty, CtorPFTy, VoidPtrTy);
1955
1956 // Construct the constructor and destructor arrays.
1957 ConstantInitBuilder builder(*this);
1958 auto ctors = builder.beginArray(CtorStructTy);
1959 for (const auto &I : Fns) {
1960 auto ctor = ctors.beginStruct(CtorStructTy);
1961 ctor.addInt(Int32Ty, I.Priority);
1962 ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1963 if (I.AssociatedData)
1964 ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1965 else
1966 ctor.addNullPointer(VoidPtrTy);
1967 ctor.finishAndAddTo(ctors);
1968 }
1969
1970 auto list =
1971 ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1972 /*constant*/ false,
1973 llvm::GlobalValue::AppendingLinkage);
1974
1975 // The LTO linker doesn't seem to like it when we set an alignment
1976 // on appending variables. Take it off as a workaround.
1977 list->setAlignment(std::nullopt);
1978
1979 Fns.clear();
1980}
1981
1982llvm::GlobalValue::LinkageTypes
1984 const auto *D = cast<FunctionDecl>(GD.getDecl());
1985
1987
1988 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
1990
1992}
1993
1994llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
1995 llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
1996 if (!MDS) return nullptr;
1997
1998 return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
1999}
2000
2002 if (auto *FnType = T->getAs<FunctionProtoType>())
2004 FnType->getReturnType(), FnType->getParamTypes(),
2005 FnType->getExtProtoInfo().withExceptionSpec(EST_None));
2006
2007 std::string OutName;
2008 llvm::raw_string_ostream Out(OutName);
2010 T, Out, getCodeGenOpts().SanitizeCfiICallNormalizeIntegers);
2011
2012 if (getCodeGenOpts().SanitizeCfiICallNormalizeIntegers)
2013 Out << ".normalized";
2014
2015 return llvm::ConstantInt::get(Int32Ty,
2016 static_cast<uint32_t>(llvm::xxHash64(OutName)));
2017}
2018
2020 const CGFunctionInfo &Info,
2021 llvm::Function *F, bool IsThunk) {
2022 unsigned CallingConv;
2023 llvm::AttributeList PAL;
2024 ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv,
2025 /*AttrOnCallSite=*/false, IsThunk);
2026 F->setAttributes(PAL);
2027 F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2028}
2029
2030static void removeImageAccessQualifier(std::string& TyName) {
2031 std::string ReadOnlyQual("__read_only");
2032 std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
2033 if (ReadOnlyPos != std::string::npos)
2034 // "+ 1" for the space after access qualifier.
2035 TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
2036 else {
2037 std::string WriteOnlyQual("__write_only");
2038 std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
2039 if (WriteOnlyPos != std::string::npos)
2040 TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
2041 else {
2042 std::string ReadWriteQual("__read_write");
2043 std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
2044 if (ReadWritePos != std::string::npos)
2045 TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
2046 }
2047 }
2048}
2049
2050// Returns the address space id that should be produced to the
2051// kernel_arg_addr_space metadata. This is always fixed to the ids
2052// as specified in the SPIR 2.0 specification in order to differentiate
2053// for example in clGetKernelArgInfo() implementation between the address
2054// spaces with targets without unique mapping to the OpenCL address spaces
2055// (basically all single AS CPUs).
2056static unsigned ArgInfoAddressSpace(LangAS AS) {
2057 switch (AS) {
2059 return 1;
2061 return 2;
2063 return 3;
2065 return 4; // Not in SPIR 2.0 specs.
2067 return 5;
2069 return 6;
2070 default:
2071 return 0; // Assume private.
2072 }
2073}
2074
2076 const FunctionDecl *FD,
2077 CodeGenFunction *CGF) {
2078 assert(((FD && CGF) || (!FD && !CGF)) &&
2079 "Incorrect use - FD and CGF should either be both null or not!");
2080 // Create MDNodes that represent the kernel arg metadata.
2081 // Each MDNode is a list in the form of "key", N number of values which is
2082 // the same number of values as their are kernel arguments.
2083
2084 const PrintingPolicy &Policy = Context.getPrintingPolicy();
2085
2086 // MDNode for the kernel argument address space qualifiers.
2088
2089 // MDNode for the kernel argument access qualifiers (images only).
2091
2092 // MDNode for the kernel argument type names.
2094
2095 // MDNode for the kernel argument base type names.
2096 SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
2097
2098 // MDNode for the kernel argument type qualifiers.
2100
2101 // MDNode for the kernel argument names.
2103
2104 if (FD && CGF)
2105 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
2106 const ParmVarDecl *parm = FD->getParamDecl(i);
2107 // Get argument name.
2108 argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
2109
2110 if (!getLangOpts().OpenCL)
2111 continue;
2112 QualType ty = parm->getType();
2113 std::string typeQuals;
2114
2115 // Get image and pipe access qualifier:
2116 if (ty->isImageType() || ty->isPipeType()) {
2117 const Decl *PDecl = parm;
2118 if (const auto *TD = ty->getAs<TypedefType>())
2119 PDecl = TD->getDecl();
2120 const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
2121 if (A && A->isWriteOnly())
2122 accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
2123 else if (A && A->isReadWrite())
2124 accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
2125 else
2126 accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
2127 } else
2128 accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
2129
2130 auto getTypeSpelling = [&](QualType Ty) {
2131 auto typeName = Ty.getUnqualifiedType().getAsString(Policy);
2132
2133 if (Ty.isCanonical()) {
2134 StringRef typeNameRef = typeName;
2135 // Turn "unsigned type" to "utype"
2136 if (typeNameRef.consume_front("unsigned "))
2137 return std::string("u") + typeNameRef.str();
2138 if (typeNameRef.consume_front("signed "))
2139 return typeNameRef.str();
2140 }
2141
2142 return typeName;
2143 };
2144
2145 if (ty->isPointerType()) {
2146 QualType pointeeTy = ty->getPointeeType();
2147
2148 // Get address qualifier.
2149 addressQuals.push_back(
2150 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
2151 ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
2152
2153 // Get argument type name.
2154 std::string typeName = getTypeSpelling(pointeeTy) + "*";
2155 std::string baseTypeName =
2156 getTypeSpelling(pointeeTy.getCanonicalType()) + "*";
2157 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2158 argBaseTypeNames.push_back(
2159 llvm::MDString::get(VMContext, baseTypeName));
2160
2161 // Get argument type qualifiers:
2162 if (ty.isRestrictQualified())
2163 typeQuals = "restrict";
2164 if (pointeeTy.isConstQualified() ||
2166 typeQuals += typeQuals.empty() ? "const" : " const";
2167 if (pointeeTy.isVolatileQualified())
2168 typeQuals += typeQuals.empty() ? "volatile" : " volatile";
2169 } else {
2170 uint32_t AddrSpc = 0;
2171 bool isPipe = ty->isPipeType();
2172 if (ty->isImageType() || isPipe)
2174
2175 addressQuals.push_back(
2176 llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
2177
2178 // Get argument type name.
2179 ty = isPipe ? ty->castAs<PipeType>()->getElementType() : ty;
2180 std::string typeName = getTypeSpelling(ty);
2181 std::string baseTypeName = getTypeSpelling(ty.getCanonicalType());
2182
2183 // Remove access qualifiers on images
2184 // (as they are inseparable from type in clang implementation,
2185 // but OpenCL spec provides a special query to get access qualifier
2186 // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
2187 if (ty->isImageType()) {
2189 removeImageAccessQualifier(baseTypeName);
2190 }
2191
2192 argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
2193 argBaseTypeNames.push_back(
2194 llvm::MDString::get(VMContext, baseTypeName));
2195
2196 if (isPipe)
2197 typeQuals = "pipe";
2198 }
2199 argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
2200 }
2201
2202 if (getLangOpts().OpenCL) {
2203 Fn->setMetadata("kernel_arg_addr_space",
2204 llvm::MDNode::get(VMContext, addressQuals));
2205 Fn->setMetadata("kernel_arg_access_qual",
2206 llvm::MDNode::get(VMContext, accessQuals));
2207 Fn->setMetadata("kernel_arg_type",
2208 llvm::MDNode::get(VMContext, argTypeNames));
2209 Fn->setMetadata("kernel_arg_base_type",
2210 llvm::MDNode::get(VMContext, argBaseTypeNames));
2211 Fn->setMetadata("kernel_arg_type_qual",
2212 llvm::MDNode::get(VMContext, argTypeQuals));
2213 }
2214 if (getCodeGenOpts().EmitOpenCLArgMetadata ||
2215 getCodeGenOpts().HIPSaveKernelArgName)
2216 Fn->setMetadata("kernel_arg_name",
2217 llvm::MDNode::get(VMContext, argNames));
2218}
2219
2220/// Determines whether the language options require us to model
2221/// unwind exceptions. We treat -fexceptions as mandating this
2222/// except under the fragile ObjC ABI with only ObjC exceptions
2223/// enabled. This means, for example, that C with -fexceptions
2224/// enables this.
2225static bool hasUnwindExceptions(const LangOptions &LangOpts) {
2226 // If exceptions are completely disabled, obviously this is false.
2227 if (!LangOpts.Exceptions) return false;
2228
2229 // If C++ exceptions are enabled, this is true.
2230 if (LangOpts.CXXExceptions) return true;
2231
2232 // If ObjC exceptions are enabled, this depends on the ABI.
2233 if (LangOpts.ObjCExceptions) {
2234 return LangOpts.ObjCRuntime.hasUnwindExceptions();
2235 }
2236
2237 return true;
2238}
2239
2241 const CXXMethodDecl *MD) {
2242 // Check that the type metadata can ever actually be used by a call.
2243 if (!CGM.getCodeGenOpts().LTOUnit ||
2245 return false;
2246
2247 // Only functions whose address can be taken with a member function pointer
2248 // need this sort of type metadata.
2249 return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) &&
2250 !isa<CXXDestructorDecl>(MD);
2251}
2252
2255 llvm::SetVector<const CXXRecordDecl *> MostBases;
2256
2257 std::function<void (const CXXRecordDecl *)> CollectMostBases;
2258 CollectMostBases = [&](const CXXRecordDecl *RD) {
2259 if (RD->getNumBases() == 0)
2260 MostBases.insert(RD);
2261 for (const CXXBaseSpecifier &B : RD->bases())
2262 CollectMostBases(B.getType()->getAsCXXRecordDecl());
2263 };
2264 CollectMostBases(RD);
2265 return MostBases.takeVector();
2266}
2267
2269 llvm::Function *F) {
2270 llvm::AttrBuilder B(F->getContext());
2271
2272 if ((!D || !D->hasAttr<NoUwtableAttr>()) && CodeGenOpts.UnwindTables)
2273 B.addUWTableAttr(llvm::UWTableKind(CodeGenOpts.UnwindTables));
2274
2275 if (CodeGenOpts.StackClashProtector)
2276 B.addAttribute("probe-stack", "inline-asm");
2277
2278 if (!hasUnwindExceptions(LangOpts))
2279 B.addAttribute(llvm::Attribute::NoUnwind);
2280
2281 if (D && D->hasAttr<NoStackProtectorAttr>())
2282 ; // Do nothing.
2283 else if (D && D->hasAttr<StrictGuardStackCheckAttr>() &&
2284 LangOpts.getStackProtector() == LangOptions::SSPOn)
2285 B.addAttribute(llvm::Attribute::StackProtectStrong);
2286 else if (LangOpts.getStackProtector() == LangOptions::SSPOn)
2287 B.addAttribute(llvm::Attribute::StackProtect);
2288 else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
2289 B.addAttribute(llvm::Attribute::StackProtectStrong);
2290 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
2291 B.addAttribute(llvm::Attribute::StackProtectReq);
2292
2293 if (!D) {
2294 // If we don't have a declaration to control inlining, the function isn't
2295 // explicitly marked as alwaysinline for semantic reasons, and inlining is
2296 // disabled, mark the function as noinline.
2297 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
2298 CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
2299 B.addAttribute(llvm::Attribute::NoInline);
2300
2301 F->addFnAttrs(B);
2302 return;
2303 }
2304
2305 // Handle SME attributes that apply to function definitions,
2306 // rather than to function prototypes.
2307 if (D->hasAttr<ArmLocallyStreamingAttr>())
2308 B.addAttribute("aarch64_pstate_sm_body");
2309
2310 if (D->hasAttr<ArmNewZAAttr>())
2311 B.addAttribute("aarch64_pstate_za_new");
2312
2313 // Track whether we need to add the optnone LLVM attribute,
2314 // starting with the default for this optimization level.
2315 bool ShouldAddOptNone =
2316 !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
2317 // We can't add optnone in the following cases, it won't pass the verifier.
2318 ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
2319 ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
2320
2321 // Add optnone, but do so only if the function isn't always_inline.
2322 if ((ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) &&
2323 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2324 B.addAttribute(llvm::Attribute::OptimizeNone);
2325
2326 // OptimizeNone implies noinline; we should not be inlining such functions.
2327 B.addAttribute(llvm::Attribute::NoInline);
2328
2329 // We still need to handle naked functions even though optnone subsumes
2330 // much of their semantics.
2331 if (D->hasAttr<NakedAttr>())
2332 B.addAttribute(llvm::Attribute::Naked);
2333
2334 // OptimizeNone wins over OptimizeForSize and MinSize.
2335 F->removeFnAttr(llvm::Attribute::OptimizeForSize);
2336 F->removeFnAttr(llvm::Attribute::MinSize);
2337 } else if (D->hasAttr<NakedAttr>()) {
2338 // Naked implies noinline: we should not be inlining such functions.
2339 B.addAttribute(llvm::Attribute::Naked);
2340 B.addAttribute(llvm::Attribute::NoInline);
2341 } else if (D->hasAttr<NoDuplicateAttr>()) {
2342 B.addAttribute(llvm::Attribute::NoDuplicate);
2343 } else if (D->hasAttr<NoInlineAttr>() && !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2344 // Add noinline if the function isn't always_inline.
2345 B.addAttribute(llvm::Attribute::NoInline);
2346 } else if (D->hasAttr<AlwaysInlineAttr>() &&
2347 !F->hasFnAttribute(llvm::Attribute::NoInline)) {
2348 // (noinline wins over always_inline, and we can't specify both in IR)
2349 B.addAttribute(llvm::Attribute::AlwaysInline);
2350 } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
2351 // If we're not inlining, then force everything that isn't always_inline to
2352 // carry an explicit noinline attribute.
2353 if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
2354 B.addAttribute(llvm::Attribute::NoInline);
2355 } else {
2356 // Otherwise, propagate the inline hint attribute and potentially use its
2357 // absence to mark things as noinline.
2358 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2359 // Search function and template pattern redeclarations for inline.
2360 auto CheckForInline = [](const FunctionDecl *FD) {
2361 auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
2362 return Redecl->isInlineSpecified();
2363 };
2364 if (any_of(FD->redecls(), CheckRedeclForInline))
2365 return true;
2366 const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2367 if (!Pattern)
2368 return false;
2369 return any_of(Pattern->redecls(), CheckRedeclForInline);
2370 };
2371 if (CheckForInline(FD)) {
2372 B.addAttribute(llvm::Attribute::InlineHint);
2373 } else if (CodeGenOpts.getInlining() ==
2375 !FD->isInlined() &&
2376 !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
2377 B.addAttribute(llvm::Attribute::NoInline);
2378 }
2379 }
2380 }
2381
2382 // Add other optimization related attributes if we are optimizing this
2383 // function.
2384 if (!D->hasAttr<OptimizeNoneAttr>()) {
2385 if (D->hasAttr<ColdAttr>()) {
2386 if (!ShouldAddOptNone)
2387 B.addAttribute(llvm::Attribute::OptimizeForSize);
2388 B.addAttribute(llvm::Attribute::Cold);
2389 }
2390 if (D->hasAttr<HotAttr>())
2391 B.addAttribute(llvm::Attribute::Hot);
2392 if (D->hasAttr<MinSizeAttr>())
2393 B.addAttribute(llvm::Attribute::MinSize);
2394 }
2395
2396 F->addFnAttrs(B);
2397
2398 unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
2399 if (alignment)
2400 F->setAlignment(llvm::Align(alignment));
2401
2402 if (!D->hasAttr<AlignedAttr>())
2403 if (LangOpts.FunctionAlignment)
2404 F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
2405
2406 // Some C++ ABIs require 2-byte alignment for member functions, in order to
2407 // reserve a bit for differentiating between virtual and non-virtual member
2408 // functions. If the current target's C++ ABI requires this and this is a
2409 // member function, set its alignment accordingly.
2410 if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
2411 if (isa<CXXMethodDecl>(D) && F->getPointerAlignment(getDataLayout()) < 2)
2412 F->setAlignment(std::max(llvm::Align(2), F->getAlign().valueOrOne()));
2413 }
2414
2415 // In the cross-dso CFI mode with canonical jump tables, we want !type
2416 // attributes on definitions only.
2417 if (CodeGenOpts.SanitizeCfiCrossDso &&
2418 CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
2419 if (auto *FD = dyn_cast<FunctionDecl>(D)) {
2420 // Skip available_externally functions. They won't be codegen'ed in the
2421 // current module anyway.
2422 if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
2424 }
2425 }
2426
2427 // Emit type metadata on member functions for member function pointer checks.
2428 // These are only ever necessary on definitions; we're guaranteed that the
2429 // definition will be present in the LTO unit as a result of LTO visibility.
2430 auto *MD = dyn_cast<CXXMethodDecl>(D);
2431 if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
2432 for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
2433 llvm::Metadata *Id =
2435 MD->getType(), Context.getRecordType(Base).getTypePtr()));
2436 F->addTypeMetadata(0, Id);
2437 }
2438 }
2439}
2440
2441void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
2442 const Decl *D = GD.getDecl();
2443 if (isa_and_nonnull<NamedDecl>(D))
2444 setGVProperties(GV, GD);
2445 else
2446 GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
2447
2448 if (D && D->hasAttr<UsedAttr>())
2450
2451 if (const auto *VD = dyn_cast_if_present<VarDecl>(D);
2452 VD &&
2453 ((CodeGenOpts.KeepPersistentStorageVariables &&
2454 (VD->getStorageDuration() == SD_Static ||
2455 VD->getStorageDuration() == SD_Thread)) ||
2456 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
2457 VD->getType().isConstQualified())))
2459}
2460
2461bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
2462 llvm::AttrBuilder &Attrs,
2463 bool SetTargetFeatures) {
2464 // Add target-cpu and target-features attributes to functions. If
2465 // we have a decl for the function and it has a target attribute then
2466 // parse that and add it to the feature set.
2467 StringRef TargetCPU = getTarget().getTargetOpts().CPU;
2468 StringRef TuneCPU = getTarget().getTargetOpts().TuneCPU;
2469 std::vector<std::string> Features;
2470 const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
2471 FD = FD ? FD->getMostRecentDecl() : FD;
2472 const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
2473 const auto *TV = FD ? FD->getAttr<TargetVersionAttr>() : nullptr;
2474 assert((!TD || !TV) && "both target_version and target specified");
2475 const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
2476 const auto *TC = FD ? FD->getAttr<TargetClonesAttr>() : nullptr;
2477 bool AddedAttr = false;
2478 if (TD || TV || SD || TC) {
2479 llvm::StringMap<bool> FeatureMap;
2480 getContext().getFunctionFeatureMap(FeatureMap, GD);
2481
2482 // Produce the canonical string for this set of features.
2483 for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
2484 Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
2485
2486 // Now add the target-cpu and target-features to the function.
2487 // While we populated the feature map above, we still need to
2488 // get and parse the target attribute so we can get the cpu for
2489 // the function.
2490 if (TD) {
2492 Target.parseTargetAttr(TD->getFeaturesStr());
2493 if (!ParsedAttr.CPU.empty() &&
2494 getTarget().isValidCPUName(ParsedAttr.CPU)) {
2495 TargetCPU = ParsedAttr.CPU;
2496 TuneCPU = ""; // Clear the tune CPU.
2497 }
2498 if (!ParsedAttr.Tune.empty() &&
2499 getTarget().isValidCPUName(ParsedAttr.Tune))
2500 TuneCPU = ParsedAttr.Tune;
2501 }
2502
2503 if (SD) {
2504 // Apply the given CPU name as the 'tune-cpu' so that the optimizer can
2505 // favor this processor.
2506 TuneCPU = SD->getCPUName(GD.getMultiVersionIndex())->getName();
2507 }
2508 } else {
2509 // Otherwise just add the existing target cpu and target features to the
2510 // function.
2511 Features = getTarget().getTargetOpts().Features;
2512 }
2513
2514 if (!TargetCPU.empty()) {
2515 Attrs.addAttribute("target-cpu", TargetCPU);
2516 AddedAttr = true;
2517 }
2518 if (!TuneCPU.empty()) {
2519 Attrs.addAttribute("tune-cpu", TuneCPU);
2520 AddedAttr = true;
2521 }
2522 if (!Features.empty() && SetTargetFeatures) {
2523 llvm::erase_if(Features, [&](const std::string& F) {
2524 return getTarget().isReadOnlyFeature(F.substr(1));
2525 });
2526 llvm::sort(Features);
2527 Attrs.addAttribute("target-features", llvm::join(Features, ","));
2528 AddedAttr = true;
2529 }
2530
2531 return AddedAttr;
2532}
2533
2534void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
2535 llvm::GlobalObject *GO) {
2536 const Decl *D = GD.getDecl();
2537 SetCommonAttributes(GD, GO);
2538
2539 if (D) {
2540 if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
2541 if (D->hasAttr<RetainAttr>())
2542 addUsedGlobal(GV);
2543 if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
2544 GV->addAttribute("bss-section", SA->getName());
2545 if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
2546 GV->addAttribute("data-section", SA->getName());
2547 if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
2548 GV->addAttribute("rodata-section", SA->getName());
2549 if (auto *SA = D->getAttr<PragmaClangRelroSectionAttr>())
2550 GV->addAttribute("relro-section", SA->getName());
2551 }
2552
2553 if (auto *F = dyn_cast<llvm::Function>(GO)) {
2554 if (D->hasAttr<RetainAttr>())
2555 addUsedGlobal(F);
2556 if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
2557 if (!D->getAttr<SectionAttr>())
2558 F->addFnAttr("implicit-section-name", SA->getName());
2559
2560 llvm::AttrBuilder Attrs(F->getContext());
2561 if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
2562 // We know that GetCPUAndFeaturesAttributes will always have the
2563 // newest set, since it has the newest possible FunctionDecl, so the
2564 // new ones should replace the old.
2565 llvm::AttributeMask RemoveAttrs;
2566 RemoveAttrs.addAttribute("target-cpu");
2567 RemoveAttrs.addAttribute("target-features");
2568 RemoveAttrs.addAttribute("tune-cpu");
2569 F->removeFnAttrs(RemoveAttrs);
2570 F->addFnAttrs(Attrs);
2571 }
2572 }
2573
2574 if (const auto *CSA = D->getAttr<CodeSegAttr>())
2575 GO->setSection(CSA->getName());
2576 else if (const auto *SA = D->getAttr<SectionAttr>())
2577 GO->setSection(SA->getName());
2578 }
2579
2581}
2582
2584 llvm::Function *F,
2585 const CGFunctionInfo &FI) {
2586 const Decl *D = GD.getDecl();
2587 SetLLVMFunctionAttributes(GD, FI, F, /*IsThunk=*/false);
2589
2590 F->setLinkage(llvm::Function::InternalLinkage);
2591
2592 setNonAliasAttributes(GD, F);
2593}
2594
2595static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
2596 // Set linkage and visibility in case we never see a definition.
2598 // Don't set internal linkage on declarations.
2599 // "extern_weak" is overloaded in LLVM; we probably should have
2600 // separate linkage types for this.
2601 if (isExternallyVisible(LV.getLinkage()) &&
2602 (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
2603 GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
2604}
2605
2607 llvm::Function *F) {
2608 // Only if we are checking indirect calls.
2609 if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
2610 return;
2611
2612 // Non-static class methods are handled via vtable or member function pointer
2613 // checks elsewhere.
2614 if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
2615 return;
2616
2617 llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
2618 F->addTypeMetadata(0, MD);
2619 F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
2620
2621 // Emit a hash-based bit set entry for cross-DSO calls.
2622 if (CodeGenOpts.SanitizeCfiCrossDso)
2623 if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
2624 F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
2625}
2626
2627void CodeGenModule::setKCFIType(const FunctionDecl *FD, llvm::Function *F) {
2628 llvm::LLVMContext &Ctx = F->getContext();
2629 llvm::MDBuilder MDB(Ctx);
2630 F->setMetadata(llvm::LLVMContext::MD_kcfi_type,
2631 llvm::MDNode::get(
2632 Ctx, MDB.createConstant(CreateKCFITypeId(FD->getType()))));
2633}
2634
2635static bool allowKCFIIdentifier(StringRef Name) {
2636 // KCFI type identifier constants are only necessary for external assembly
2637 // functions, which means it's safe to skip unusual names. Subset of
2638 // MCAsmInfo::isAcceptableChar() and MCAsmInfoXCOFF::isAcceptableChar().
2639 return llvm::all_of(Name, [](const char &C) {
2640 return llvm::isAlnum(C) || C == '_' || C == '.';
2641 });
2642}
2643
2645 llvm::Module &M = getModule();
2646 for (auto &F : M.functions()) {
2647 // Remove KCFI type metadata from non-address-taken local functions.
2648 bool AddressTaken = F.hasAddressTaken();
2649 if (!AddressTaken && F.hasLocalLinkage())
2650 F.eraseMetadata(llvm::LLVMContext::MD_kcfi_type);
2651
2652 // Generate a constant with the expected KCFI type identifier for all
2653 // address-taken function declarations to support annotating indirectly
2654 // called assembly functions.
2655 if (!AddressTaken || !F.isDeclaration())
2656 continue;
2657
2658 const llvm::ConstantInt *Type;
2659 if (const llvm::MDNode *MD = F.getMetadata(llvm::LLVMContext::MD_kcfi_type))
2660 Type = llvm::mdconst::extract<llvm::ConstantInt>(MD->getOperand(0));
2661 else
2662 continue;
2663
2664 StringRef Name = F.getName();
2665 if (!allowKCFIIdentifier(Name))
2666 continue;
2667
2668 std::string Asm = (".weak __kcfi_typeid_" + Name + "\n.set __kcfi_typeid_" +
2669 Name + ", " + Twine(Type->getZExtValue()) + "\n")
2670 .str();
2671 M.appendModuleInlineAsm(Asm);
2672 }
2673}
2674
2675void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
2676 bool IsIncompleteFunction,
2677 bool IsThunk) {
2678
2679 if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
2680 // If this is an intrinsic function, set the function's attributes
2681 // to the intrinsic's attributes.
2682 F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
2683 return;
2684 }
2685
2686 const auto *FD = cast<FunctionDecl>(GD.getDecl());
2687
2688 if (!IsIncompleteFunction)
2689 SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F,
2690 IsThunk);
2691
2692 // Add the Returned attribute for "this", except for iOS 5 and earlier
2693 // where substantial code, including the libstdc++ dylib, was compiled with
2694 // GCC and does not actually return "this".
2695 if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
2696 !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
2697 assert(!F->arg_empty() &&
2698 F->arg_begin()->getType()
2699 ->canLosslesslyBitCastTo(F->getReturnType()) &&
2700 "unexpected this return");
2701 F->addParamAttr(0, llvm::Attribute::Returned);
2702 }
2703
2704 // Only a few attributes are set on declarations; these may later be
2705 // overridden by a definition.
2706
2707 setLinkageForGV(F, FD);
2708 setGVProperties(F, FD);
2709
2710 // Setup target-specific attributes.
2711 if (!IsIncompleteFunction && F->isDeclaration())
2713
2714 if (const auto *CSA = FD->getAttr<CodeSegAttr>())
2715 F->setSection(CSA->getName());
2716 else if (const auto *SA = FD->getAttr<SectionAttr>())
2717 F->setSection(SA->getName());
2718
2719 if (const auto *EA = FD->getAttr<ErrorAttr>()) {
2720 if (EA->isError())
2721 F->addFnAttr("dontcall-error", EA->getUserDiagnostic());
2722 else if (EA->isWarning())
2723 F->addFnAttr("dontcall-warn", EA->getUserDiagnostic());
2724 }
2725
2726 // If we plan on emitting this inline builtin, we can't treat it as a builtin.
2727 if (FD->isInlineBuiltinDeclaration()) {
2728 const FunctionDecl *FDBody;
2729 bool HasBody = FD->hasBody(FDBody);
2730 (void)HasBody;
2731 assert(HasBody && "Inline builtin declarations should always have an "
2732 "available body!");
2733 if (shouldEmitFunction(FDBody))
2734 F->addFnAttr(llvm::Attribute::NoBuiltin);
2735 }
2736
2738 // A replaceable global allocation function does not act like a builtin by
2739 // default, only if it is invoked by a new-expression or delete-expression.
2740 F->addFnAttr(llvm::Attribute::NoBuiltin);
2741 }
2742
2743 if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
2744 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2745 else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
2746 if (MD->isVirtual())
2747 F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2748
2749 // Don't emit entries for function declarations in the cross-DSO mode. This
2750 // is handled with better precision by the receiving DSO. But if jump tables
2751 // are non-canonical then we need type metadata in order to produce the local
2752 // jump table.
2753 if (!CodeGenOpts.SanitizeCfiCrossDso ||
2754 !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
2756
2757 if (LangOpts.Sanitize.has(SanitizerKind::KCFI))
2758 setKCFIType(FD, F);
2759
2760 if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
2762
2763 if (CodeGenOpts.InlineMaxStackSize != UINT_MAX)
2764 F->addFnAttr("inline-max-stacksize", llvm::utostr(CodeGenOpts.InlineMaxStackSize));
2765
2766 if (const auto *CB = FD->getAttr<CallbackAttr>()) {
2767 // Annotate the callback behavior as metadata:
2768 // - The callback callee (as argument number).
2769 // - The callback payloads (as argument numbers).
2770 llvm::LLVMContext &Ctx = F->getContext();
2771 llvm::MDBuilder MDB(Ctx);
2772
2773 // The payload indices are all but the first one in the encoding. The first
2774 // identifies the callback callee.
2775 int CalleeIdx = *CB->encoding_begin();
2776 ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
2777 F->addMetadata(llvm::LLVMContext::MD_callback,
2778 *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
2779 CalleeIdx, PayloadIndices,
2780 /* VarArgsArePassed */ false)}));
2781 }
2782}
2783
2784void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
2785 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2786 "Only globals with definition can force usage.");
2787 LLVMUsed.emplace_back(GV);
2788}
2789
2790void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
2791 assert(!GV->isDeclaration() &&
2792 "Only globals with definition can force usage.");
2793 LLVMCompilerUsed.emplace_back(GV);
2794}
2795
2797 assert((isa<llvm::Function>(GV) || !GV->isDeclaration()) &&
2798 "Only globals with definition can force usage.");
2799 if (getTriple().isOSBinFormatELF())
2800 LLVMCompilerUsed.emplace_back(GV);
2801 else
2802 LLVMUsed.emplace_back(GV);
2803}
2804
2805static void emitUsed(CodeGenModule &CGM, StringRef Name,
2806 std::vector<llvm::WeakTrackingVH> &List) {
2807 // Don't create llvm.used if there is no need.
2808 if (List.empty())
2809 return;
2810
2811 // Convert List to what ConstantArray needs.
2813 UsedArray.resize(List.size());
2814 for (unsigned i = 0, e = List.size(); i != e; ++i) {
2815 UsedArray[i] =
2816 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2817 cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
2818 }
2819
2820 if (UsedArray.empty())
2821 return;
2822 llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
2823
2824 auto *GV = new llvm::GlobalVariable(
2825 CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
2826 llvm::ConstantArray::get(ATy, UsedArray), Name);
2827
2828 GV->setSection("llvm.metadata");
2829}
2830
2831void CodeGenModule::emitLLVMUsed() {
2832 emitUsed(*this, "llvm.used", LLVMUsed);
2833 emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
2834}
2835
2837 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
2838 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2839}
2840
2841void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
2844 if (Opt.empty())
2845 return;
2846 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2847 LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
2848}
2849
2851 auto &C = getLLVMContext();
2852 if (getTarget().getTriple().isOSBinFormatELF()) {
2853 ELFDependentLibraries.push_back(
2854 llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
2855 return;
2856 }
2857
2860 auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
2861 LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
2862}
2863
2864/// Add link options implied by the given module, including modules
2865/// it depends on, using a postorder walk.
2869 // Import this module's parent.
2870 if (Mod->Parent && Visited.insert(Mod->Parent).second) {
2871 addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
2872 }
2873
2874 // Import this module's dependencies.
2875 for (Module *Import : llvm::reverse(Mod->Imports)) {
2876 if (Visited.insert(Import).second)
2877 addLinkOptionsPostorder(CGM, Import, Metadata, Visited);
2878 }
2879
2880 // Add linker options to link against the libraries/frameworks
2881 // described by this module.
2882 llvm::LLVMContext &Context = CGM.getLLVMContext();
2883 bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
2884
2885 // For modules that use export_as for linking, use that module
2886 // name instead.
2888 return;
2889
2890 for (const Module::LinkLibrary &LL : llvm::reverse(Mod->LinkLibraries)) {
2891 // Link against a framework. Frameworks are currently Darwin only, so we
2892 // don't to ask TargetCodeGenInfo for the spelling of the linker option.
2893 if (LL.IsFramework) {
2894 llvm::Metadata *Args[2] = {llvm::MDString::get(Context, "-framework"),
2895 llvm::MDString::get(Context, LL.Library)};
2896
2897 Metadata.push_back(llvm::MDNode::get(Context, Args));
2898 continue;
2899 }
2900
2901 // Link against a library.
2902 if (IsELF) {
2903 llvm::Metadata *Args[2] = {
2904 llvm::MDString::get(Context, "lib"),
2905 llvm::MDString::get(Context, LL.Library),
2906 };
2907 Metadata.push_back(llvm::MDNode::get(Context, Args));
2908 } else {
2910 CGM.getTargetCodeGenInfo().getDependentLibraryOption(LL.Library, Opt);
2911 auto *OptString = llvm::MDString::get(Context, Opt);
2912 Metadata.push_back(llvm::MDNode::get(Context, OptString));
2913 }
2914 }
2915}
2916
2917void CodeGenModule::EmitModuleInitializers(clang::Module *Primary) {
2918 assert(Primary->isNamedModuleUnit() &&
2919 "We should only emit module initializers for named modules.");
2920
2921 // Emit the initializers in the order that sub-modules appear in the
2922 // source, first Global Module Fragments, if present.
2923 if (auto GMF = Primary->getGlobalModuleFragment()) {
2924 for (Decl *D : getContext().getModuleInitializers(GMF)) {
2925 if (isa<ImportDecl>(D))
2926 continue;
2927 assert(isa<VarDecl>(D) && "GMF initializer decl is not a var?");
2929 }
2930 }
2931 // Second any associated with the module, itself.
2932 for (Decl *D : getContext().getModuleInitializers(Primary)) {
2933 // Skip import decls, the inits for those are called explicitly.
2934 if (isa<ImportDecl>(D))
2935 continue;
2937 }
2938 // Third any associated with the Privat eMOdule Fragment, if present.
2939 if (auto PMF = Primary->getPrivateModuleFragment()) {
2940 for (Decl *D : getContext().getModuleInitializers(PMF)) {
2941 assert(isa<VarDecl>(D) && "PMF initializer decl is not a var?");
2943 }
2944 }
2945}
2946
2947void CodeGenModule::EmitModuleLinkOptions() {
2948 // Collect the set of all of the modules we want to visit to emit link
2949 // options, which is essentially the imported modules and all of their
2950 // non-explicit child modules.
2951 llvm::SetVector<clang::Module *> LinkModules;
2954
2955 // Seed the stack with imported modules.
2956 for (Module *M : ImportedModules) {
2957 // Do not add any link flags when an implementation TU of a module imports
2958 // a header of that same module.
2959 if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2960 !getLangOpts().isCompilingModule())
2961 continue;
2962 if (Visited.insert(M).second)
2963 Stack.push_back(M);
2964 }
2965
2966 // Find all of the modules to import, making a little effort to prune
2967 // non-leaf modules.
2968 while (!Stack.empty()) {
2969 clang::Module *Mod = Stack.pop_back_val();
2970
2971 bool AnyChildren = false;
2972
2973 // Visit the submodules of this module.
2974 for (const auto &SM : Mod->submodules()) {
2975 // Skip explicit children; they need to be explicitly imported to be
2976 // linked against.
2977 if (SM->IsExplicit)
2978 continue;
2979
2980 if (Visited.insert(SM).second) {
2981 Stack.push_back(SM);
2982 AnyChildren = true;
2983 }
2984 }
2985
2986 // We didn't find any children, so add this module to the list of
2987 // modules to link against.
2988 if (!AnyChildren) {
2989 LinkModules.insert(Mod);
2990 }
2991 }
2992
2993 // Add link options for all of the imported modules in reverse topological
2994 // order. We don't do anything to try to order import link flags with respect
2995 // to linker options inserted by things like #pragma comment().
2997 Visited.clear();
2998 for (Module *M : LinkModules)
2999 if (Visited.insert(M).second)
3000 addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
3001 std::reverse(MetadataArgs.begin(), MetadataArgs.end());
3002 LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
3003
3004 // Add the linker options metadata flag.
3005 auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
3006 for (auto *MD : LinkerOptionsMetadata)
3007 NMD->addOperand(MD);
3008}
3009
3010void CodeGenModule::EmitDeferred() {
3011 // Emit deferred declare target declarations.
3012 if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
3014
3015 // Emit code for any potentially referenced deferred decls. Since a
3016 // previously unused static decl may become used during the generation of code
3017 // for a static function, iterate until no changes are made.
3018
3019 if (!DeferredVTables.empty()) {
3020 EmitDeferredVTables();
3021
3022 // Emitting a vtable doesn't directly cause more vtables to
3023 // become deferred, although it can cause functions to be
3024 // emitted that then need those vtables.
3025 assert(DeferredVTables.empty());
3026 }
3027
3028 // Emit CUDA/HIP static device variables referenced by host code only.
3029 // Note we should not clear CUDADeviceVarODRUsedByHost since it is still
3030 // needed for further handling.
3031 if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice)
3032 llvm::append_range(DeferredDeclsToEmit,
3033 getContext().CUDADeviceVarODRUsedByHost);
3034
3035 // Stop if we're out of both deferred vtables and deferred declarations.
3036 if (DeferredDeclsToEmit.empty())
3037 return;
3038
3039 // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
3040 // work, it will not interfere with this.
3041 std::vector<GlobalDecl> CurDeclsToEmit;
3042 CurDeclsToEmit.swap(DeferredDeclsToEmit);
3043
3044 for (GlobalDecl &D : CurDeclsToEmit) {
3045 // We should call GetAddrOfGlobal with IsForDefinition set to true in order
3046 // to get GlobalValue with exactly the type we need, not something that
3047 // might had been created for another decl with the same mangled name but
3048 // different type.
3049 llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
3051
3052 // In case of different address spaces, we may still get a cast, even with
3053 // IsForDefinition equal to true. Query mangled names table to get
3054 // GlobalValue.
3055 if (!GV)
3057
3058 // Make sure GetGlobalValue returned non-null.
3059 assert(GV);
3060
3061 // Check to see if we've already emitted this. This is necessary
3062 // for a couple of reasons: first, decls can end up in the
3063 // deferred-decls queue multiple times, and second, decls can end
3064 // up with definitions in unusual ways (e.g. by an extern inline
3065 // function acquiring a strong function redefinition). Just
3066 // ignore these cases.
3067 if (!GV->isDeclaration())
3068 continue;
3069
3070 // If this is OpenMP, check if it is legal to emit this global normally.
3071 if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
3072 continue;
3073
3074 // Otherwise, emit the definition and move on to the next one.
3075 EmitGlobalDefinition(D, GV);
3076
3077 // If we found out that we need to emit more decls, do that recursively.
3078 // This has the advantage that the decls are emitted in a DFS and related
3079 // ones are close together, which is convenient for testing.
3080 if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
3081 EmitDeferred();
3082 assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
3083 }
3084 }
3085}
3086
3087void CodeGenModule::EmitVTablesOpportunistically() {
3088 // Try to emit external vtables as available_externally if they have emitted
3089 // all inlined virtual functions. It runs after EmitDeferred() and therefore
3090 // is not allowed to create new references to things that need to be emitted
3091 // lazily. Note that it also uses fact that we eagerly emitting RTTI.
3092
3093 assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
3094 && "Only emit opportunistic vtables with optimizations");
3095
3096 for (const CXXRecordDecl *RD : OpportunisticVTables) {
3097 assert(getVTables().isVTableExternal(RD) &&
3098 "This queue should only contain external vtables");
3099 if (getCXXABI().canSpeculativelyEmitVTable(RD))
3100 VTables.GenerateClassData(RD);
3101 }
3102 OpportunisticVTables.clear();
3103}
3104
3106 if (Annotations.empty())
3107 return;
3108
3109 // Create a new global variable for the ConstantStruct in the Module.
3110 llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
3111 Annotations[0]->getType(), Annotations.size()), Annotations);
3112 auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
3113 llvm::GlobalValue::AppendingLinkage,
3114 Array, "llvm.global.annotations");
3115 gv->setSection(AnnotationSection);
3116}
3117
3118llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
3119 llvm::Constant *&AStr = AnnotationStrings[Str];
3120 if (AStr)
3121 return AStr;
3122
3123 // Not found yet, create a new global.
3124 llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
3125 auto *gv = new llvm::GlobalVariable(
3126 getModule(), s->getType(), true, llvm::GlobalValue::PrivateLinkage, s,
3127 ".str", nullptr, llvm::GlobalValue::NotThreadLocal,
3128 ConstGlobalsPtrTy->getAddressSpace());
3129 gv->setSection(AnnotationSection);
3130 gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3131 AStr = gv;
3132 return gv;
3133}
3134
3137 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
3138 if (PLoc.isValid())
3139 return EmitAnnotationString(PLoc.getFilename());
3140 return EmitAnnotationString(SM.getBufferName(Loc));
3141}
3142
3145 PresumedLoc PLoc = SM.getPresumedLoc(L);
3146 unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
3147 SM.getExpansionLineNumber(L);
3148 return llvm::ConstantInt::get(Int32Ty, LineNo);
3149}
3150
3151llvm::Constant *CodeGenModule::EmitAnnotationArgs(const AnnotateAttr *Attr) {
3152 ArrayRef<Expr *> Exprs = {Attr->args_begin(), Attr->args_size()};
3153 if (Exprs.empty())
3154 return llvm::ConstantPointerNull::get(ConstGlobalsPtrTy);
3155
3156 llvm::FoldingSetNodeID ID;
3157 for (Expr *E : Exprs) {
3158 ID.Add(cast<clang::ConstantExpr>(E)->getAPValueResult());
3159 }
3160 llvm::Constant *&Lookup = AnnotationArgs[ID.ComputeHash()];
3161 if (Lookup)
3162 return Lookup;
3163
3165 LLVMArgs.reserve(Exprs.size());
3166 ConstantEmitter ConstEmiter(*this);
3167 llvm::transform(Exprs, std::back_inserter(LLVMArgs), [&](const Expr *E) {
3168 const auto *CE = cast<clang::ConstantExpr>(E);
3169 return ConstEmiter.emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(),
3170 CE->getType());
3171 });
3172 auto *Struct = llvm::ConstantStruct::getAnon(LLVMArgs);
3173 auto *GV = new llvm::GlobalVariable(getModule(), Struct->getType(), true,
3174 llvm::GlobalValue::PrivateLinkage, Struct,
3175 ".args");
3176 GV->setSection(AnnotationSection);
3177 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3178 auto *Bitcasted = llvm::ConstantExpr::getBitCast(GV, GlobalsInt8PtrTy);
3179
3180 Lookup = Bitcasted;
3181 return Bitcasted;
3182}
3183
3184llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
3185 const AnnotateAttr *AA,
3186 SourceLocation L) {
3187 // Get the globals for file name, annotation, and the line number.
3188 llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
3189 *UnitGV = EmitAnnotationUnit(L),
3190 *LineNoCst = EmitAnnotationLineNo(L),
3191 *Args = EmitAnnotationArgs(AA);
3192
3193 llvm::Constant *GVInGlobalsAS = GV;
3194 if (GV->getAddressSpace() !=
3195 getDataLayout().getDefaultGlobalsAddressSpace()) {
3196 GVInGlobalsAS = llvm::ConstantExpr::getAddrSpaceCast(
3197 GV,
3198 llvm::PointerType::get(
3199 GV->getContext(), getDataLayout().getDefaultGlobalsAddressSpace()));
3200 }
3201
3202 // Create the ConstantStruct for the global annotation.
3203 llvm::Constant *Fields[] = {
3204 llvm::ConstantExpr::getBitCast(GVInGlobalsAS, GlobalsInt8PtrTy),
3205 llvm::ConstantExpr::getBitCast(AnnoGV, ConstGlobalsPtrTy),
3206 llvm::ConstantExpr::getBitCast(UnitGV, ConstGlobalsPtrTy),
3207 LineNoCst,
3208 Args,
3209 };
3210 return llvm::ConstantStruct::getAnon(Fields);
3211}
3212
3214 llvm::GlobalValue *GV) {
3215 assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
3216 // Get the struct elements for these annotations.
3217 for (const auto *I : D->specific_attrs<AnnotateAttr>())
3218 Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
3219}
3220
3222 SourceLocation Loc) const {
3223 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3224 // NoSanitize by function name.
3225 if (NoSanitizeL.containsFunction(Kind, Fn->getName()))
3226 return true;
3227 // NoSanitize by location. Check "mainfile" prefix.
3228 auto &SM = Context.getSourceManager();
3229 FileEntryRef MainFile = *SM.getFileEntryRefForID(SM.getMainFileID());
3230 if (NoSanitizeL.containsMainFile(Kind, MainFile.getName()))
3231 return true;
3232
3233 // Check "src" prefix.
3234 if (Loc.isValid())
3235 return NoSanitizeL.containsLocation(Kind, Loc);
3236 // If location is unknown, this may be a compiler-generated function. Assume
3237 // it's located in the main file.
3238 return NoSanitizeL.containsFile(Kind, MainFile.getName());
3239}
3240
3242 llvm::GlobalVariable *GV,
3243 SourceLocation Loc, QualType Ty,
3244 StringRef Category) const {
3245 const auto &NoSanitizeL = getContext().getNoSanitizeList();
3246 if (NoSanitizeL.containsGlobal(Kind, GV->getName(), Category))
3247 return true;
3248 auto &SM = Context.getSourceManager();
3249 if (NoSanitizeL.containsMainFile(
3250 Kind, SM.getFileEntryRefForID(SM.getMainFileID())->getName(),
3251 Category))
3252 return true;
3253 if (NoSanitizeL.containsLocation(Kind, Loc, Category))
3254 return true;
3255
3256 // Check global type.
3257 if (!Ty.isNull()) {
3258 // Drill down the array types: if global variable of a fixed type is
3259 // not sanitized, we also don't instrument arrays of them.
3260 while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
3261 Ty = AT->getElementType();
3263 // Only record types (classes, structs etc.) are ignored.
3264 if (Ty->isRecordType()) {
3265 std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
3266 if (NoSanitizeL.containsType(Kind, TypeStr, Category))
3267 return true;
3268 }
3269 }
3270 return false;
3271}
3272
3274 StringRef Category) const {
3275 const auto &XRayFilter = getContext().getXRayFilter();
3276 using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
3277 auto Attr = ImbueAttr::NONE;
3278 if (Loc.isValid())
3279 Attr = XRayFilter.shouldImbueLocation(Loc, Category);
3280 if (Attr == ImbueAttr::NONE)
3281 Attr = XRayFilter.shouldImbueFunction(Fn->getName());
3282 switch (Attr) {
3283 case ImbueAttr::NONE:
3284 return false;
3285 case ImbueAttr::ALWAYS:
3286 Fn->addFnAttr("function-instrument", "xray-always");
3287 break;
3288 case ImbueAttr::ALWAYS_ARG1:
3289 Fn->addFnAttr("function-instrument", "xray-always");
3290 Fn->addFnAttr("xray-log-args", "1");
3291 break;
3292 case ImbueAttr::NEVER:
3293 Fn->addFnAttr("function-instrument", "xray-never");
3294 break;
3295 }
3296 return true;
3297}
3298
3301 SourceLocation Loc) const {
3302 const auto &ProfileList = getContext().getProfileList();
3303 // If the profile list is empty, then instrument everything.
3304 if (ProfileList.isEmpty())
3305 return ProfileList::Allow;
3306 CodeGenOptions::ProfileInstrKind Kind = getCodeGenOpts().getProfileInstr();
3307 // First, check the function name.
3308 if (auto V = ProfileList.isFunctionExcluded(Fn->getName(), Kind))
3309 return *V;
3310 // Next, check the source location.
3311 if (Loc.isValid())
3312 if (auto V = ProfileList.isLocationExcluded(Loc, Kind))
3313 return *V;
3314 // If location is unknown, this may be a compiler-generated function. Assume
3315 // it's located in the main file.
3316 auto &SM = Context.getSourceManager();
3317 if (auto MainFile = SM.getFileEntryRefForID(SM.getMainFileID()))
3318 if (auto V = ProfileList.isFileExcluded(MainFile->getName(), Kind))
3319 return *V;
3320 return ProfileList.getDefault(Kind);
3321}
3322
3325 SourceLocation Loc) const {
3326 auto V = isFunctionBlockedByProfileList(Fn, Loc);
3327 if (V != ProfileList::Allow)
3328 return V;
3329
3330 auto NumGroups = getCodeGenOpts().ProfileTotalFunctionGroups;
3331 if (NumGroups > 1) {
3332 auto Group = llvm::crc32(arrayRefFromStringRef(Fn->getName())) % NumGroups;
3333 if (Group != getCodeGenOpts().ProfileSelectedFunctionGroup)
3334 return ProfileList::Skip;
3335 }
3336 return ProfileList::Allow;
3337}
3338
3339bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
3340 // Never defer when EmitAllDecls is specified.
3341 if (LangOpts.EmitAllDecls)
3342 return true;
3343
3344 const auto *VD = dyn_cast<VarDecl>(Global);
3345 if (VD &&
3346 ((CodeGenOpts.KeepPersistentStorageVariables &&
3347 (VD->getStorageDuration() == SD_Static ||
3348 VD->getStorageDuration() == SD_Thread)) ||
3349 (CodeGenOpts.KeepStaticConsts && VD->getStorageDuration() == SD_Static &&
3350 VD->getType().isConstQualified())))
3351 return true;
3352
3353 return getContext().DeclMustBeEmitted(Global);
3354}
3355
3356bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
3357 // In OpenMP 5.0 variables and function may be marked as
3358 // device_type(host/nohost) and we should not emit them eagerly unless we sure
3359 // that they must be emitted on the host/device. To be sure we need to have
3360 // seen a declare target with an explicit mentioning of the function, we know
3361 // we have if the level of the declare target attribute is -1. Note that we
3362 // check somewhere else if we should emit this at all.
3363 if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd) {
3364 std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr =
3365 OMPDeclareTargetDeclAttr::getActiveAttr(Global);
3366 if (!ActiveAttr || (*ActiveAttr)->getLevel() != (unsigned)-1)
3367 return false;
3368 }
3369
3370 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3372 // Implicit template instantiations may change linkage if they are later
3373 // explicitly instantiated, so they should not be emitted eagerly.
3374 return false;
3375 }
3376 if (const auto *VD = dyn_cast<VarDecl>(Global)) {
3377 if (Context.getInlineVariableDefinitionKind(VD) ==
3379 // A definition of an inline constexpr static data member may change
3380 // linkage later if it's redeclared outside the class.
3381 return false;
3382 if (CXX20ModuleInits && VD->getOwningModule() &&
3383 !VD->getOwningModule()->isModuleMapModule()) {
3384 // For CXX20, module-owned initializers need to be deferred, since it is
3385 // not known at this point if they will be run for the current module or
3386 // as part of the initializer for an imported one.
3387 return false;
3388 }
3389 }
3390 // If OpenMP is enabled and threadprivates must be generated like TLS, delay
3391 // codegen for global variables, because they may be marked as threadprivate.
3392 if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
3393 getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
3394 !Global->getType().isConstantStorage(getContext(), false, false) &&
3395 !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
3396 return false;
3397
3398 return true;
3399}
3400
3402 StringRef Name = getMangledName(GD);
3403
3404 // The UUID descriptor should be pointer aligned.
3406
3407 // Look for an existing global.
3408 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3409 return ConstantAddress(GV, GV->getValueType(), Alignment);
3410
3411 ConstantEmitter Emitter(*this);
3412 llvm::Constant *Init;
3413
3414 APValue &V = GD->getAsAPValue();
3415 if (!V.isAbsent()) {
3416 // If possible, emit the APValue version of the initializer. In particular,
3417 // this gets the type of the constant right.
3418 Init = Emitter.emitForInitializer(
3419 GD->getAsAPValue(), GD->getType().getAddressSpace(), GD->getType());
3420 } else {
3421 // As a fallback, directly construct the constant.
3422 // FIXME: This may get padding wrong under esoteric struct layout rules.
3423 // MSVC appears to create a complete type 'struct __s_GUID' that it
3424 // presumably uses to represent these constants.
3425 MSGuidDecl::Parts Parts = GD->getParts();
3426 llvm::Constant *Fields[4] = {
3427 llvm::ConstantInt::get(Int32Ty, Parts.Part1),
3428 llvm::ConstantInt::get(Int16Ty, Parts.Part2),
3429 llvm::ConstantInt::get(Int16Ty, Parts.Part3),
3430 llvm::ConstantDataArray::getRaw(
3431 StringRef(reinterpret_cast<char *>(Parts.Part4And5), 8), 8,
3432 Int8Ty)};
3433 Init = llvm::ConstantStruct::getAnon(Fields);
3434 }
3435
3436 auto *GV = new llvm::GlobalVariable(
3437 getModule(), Init->getType(),
3438 /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
3439 if (supportsCOMDAT())
3440 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3441 setDSOLocal(GV);
3442
3443 if (!V.isAbsent()) {
3444 Emitter.finalize(GV);
3445 return ConstantAddress(GV, GV->getValueType(), Alignment);
3446 }
3447
3448 llvm::Type *Ty = getTypes().ConvertTypeForMem(GD->getType());
3449 return ConstantAddress(GV, Ty, Alignment);
3450}
3451
3453 const UnnamedGlobalConstantDecl *GCD) {
3454 CharUnits Alignment = getContext().getTypeAlignInChars(GCD->getType());
3455
3456 llvm::GlobalVariable **Entry = nullptr;
3457 Entry = &UnnamedGlobalConstantDeclMap[GCD];
3458 if (*Entry)
3459 return ConstantAddress(*Entry, (*Entry)->getValueType(), Alignment);
3460
3461 ConstantEmitter Emitter(*this);
3462 llvm::Constant *Init;
3463
3464 const APValue &V = GCD->getValue();
3465
3466 assert(!V.isAbsent());
3467 Init = Emitter.emitForInitializer(V, GCD->getType().getAddressSpace(),
3468 GCD->getType());
3469
3470 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3471 /*isConstant=*/true,
3472 llvm::GlobalValue::PrivateLinkage, Init,
3473 ".constant");
3474 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
3475 GV->setAlignment(Alignment.getAsAlign());
3476
3477 Emitter.finalize(GV);
3478
3479 *Entry = GV;
3480 return ConstantAddress(GV, GV->getValueType(), Alignment);
3481}
3482
3484 const TemplateParamObjectDecl *TPO) {
3485 StringRef Name = getMangledName(TPO);
3486 CharUnits Alignment = getNaturalTypeAlignment(TPO->getType());
3487
3488 if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
3489 return ConstantAddress(GV, GV->getValueType(), Alignment);
3490
3491 ConstantEmitter Emitter(*this);
3492 llvm::Constant *Init = Emitter.emitForInitializer(
3493 TPO->getValue(), TPO->getType().getAddressSpace(), TPO->getType());
3494
3495 if (!Init) {
3496 ErrorUnsupported(TPO, "template parameter object");
3497 return ConstantAddress::invalid();
3498 }
3499
3500 llvm::GlobalValue::LinkageTypes Linkage =
3502 ? llvm::GlobalValue::LinkOnceODRLinkage
3503 : llvm::GlobalValue::InternalLinkage;
3504 auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
3505 /*isConstant=*/true, Linkage, Init, Name);
3506 setGVProperties(GV, TPO);
3507 if (supportsCOMDAT())
3508 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3509 Emitter.finalize(GV);
3510
3511 return ConstantAddress(GV, GV->getValueType(), Alignment);
3512}
3513
3515 const AliasAttr *AA = VD->getAttr<AliasAttr>();
3516 assert(AA && "No alias?");
3517
3518 CharUnits Alignment = getContext().getDeclAlign(VD);
3519 llvm::Type *DeclTy = getTypes().ConvertTypeForMem(VD->getType());
3520
3521 // See if there is already something with the target's name in the module.
3522 llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
3523 if (Entry)
3524 return ConstantAddress(Entry, DeclTy, Alignment);
3525
3526 llvm::Constant *Aliasee;
3527 if (isa<llvm::FunctionType>(DeclTy))
3528 Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
3529 GlobalDecl(cast<FunctionDecl>(VD)),
3530 /*ForVTable=*/false);
3531 else
3532 Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(), DeclTy, LangAS::Default,
3533 nullptr);
3534
3535 auto *F = cast<llvm::GlobalValue>(Aliasee);
3536 F->setLinkage(llvm::Function::ExternalWeakLinkage);
3537 WeakRefReferences.insert(F);
3538
3539 return ConstantAddress(Aliasee, DeclTy, Alignment);
3540}
3541
3543 const auto *Global = cast<ValueDecl>(GD.getDecl());
3544
3545 // Weak references don't produce any output by themselves.
3546 if (Global->hasAttr<WeakRefAttr>())
3547 return;
3548
3549 // If this is an alias definition (which otherwise looks like a declaration)
3550 // emit it now.
3551 if (Global->hasAttr<AliasAttr>())
3552 return EmitAliasDefinition(GD);
3553
3554 // IFunc like an alias whose value is resolved at runtime by calling resolver.
3555 if (Global->hasAttr<IFuncAttr>())
3556 return emitIFuncDefinition(GD);
3557
3558 // If this is a cpu_dispatch multiversion function, emit the resolver.
3559 if (Global->hasAttr<CPUDispatchAttr>())
3560 return emitCPUDispatchDefinition(GD);
3561
3562 // If this is CUDA, be selective about which declarations we emit.
3563 if (LangOpts.CUDA) {
3564 if (LangOpts.CUDAIsDevice) {
3565 if (!Global->hasAttr<CUDADeviceAttr>() &&
3566 !Global->hasAttr<CUDAGlobalAttr>() &&
3567 !Global->hasAttr<CUDAConstantAttr>() &&
3568 !Global->hasAttr<CUDASharedAttr>() &&
3571 return;
3572 } else {
3573 // We need to emit host-side 'shadows' for all global
3574 // device-side variables because the CUDA runtime needs their
3575 // size and host-side address in order to provide access to
3576 // their device-side incarnations.
3577
3578 // So device-only functions are the only things we skip.
3579 if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
3580 Global->hasAttr<CUDADeviceAttr>())
3581 return;
3582
3583 assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
3584 "Expected Variable or Function");
3585 }
3586 }
3587
3588 if (LangOpts.OpenMP) {
3589 // If this is OpenMP, check if it is legal to emit this global normally.
3590 if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
3591 return;
3592 if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
3593 if (MustBeEmitted(Global))
3595 return;
3596 }
3597 if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
3598 if (MustBeEmitted(Global))
3600 return;
3601 }
3602 }
3603
3604 // Ignore declarations, they will be emitted on their first use.
3605 if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
3606 // Forward declarations are emitted lazily on first use.
3607 if (!FD->doesThisDeclarationHaveABody()) {
3609 return;
3610
3611 StringRef MangledName = getMangledName(GD);
3612
3613 // Compute the function info and LLVM type.
3615 llvm::Type *Ty = getTypes().GetFunctionType(FI);
3616
3617 GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
3618 /*DontDefer=*/false);
3619 return;
3620 }
3621 } else {
3622 const auto *VD = cast<VarDecl>(Global);
3623 assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
3624 if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
3626 if (LangOpts.OpenMP) {
3627 // Emit declaration of the must-be-emitted declare target variable.
3628 if (std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
3629 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
3630
3631 // If this variable has external storage and doesn't require special
3632 // link handling we defer to its canonical definition.
3633 if (VD->hasExternalStorage() &&
3634 Res != OMPDeclareTargetDeclAttr::MT_Link)
3635 return;
3636
3637 bool UnifiedMemoryEnabled =
3639 if ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3640 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3641 !UnifiedMemoryEnabled) {
3642 (void)GetAddrOfGlobalVar(VD);
3643 } else {
3644 assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
3645 ((*Res == OMPDeclareTargetDeclAttr::MT_To ||
3646 *Res == OMPDeclareTargetDeclAttr::MT_Enter) &&
3647 UnifiedMemoryEnabled)) &&
3648 "Link clause or to clause with unified memory expected.");
3650 }
3651
3652 return;
3653 }
3654 }
3655 // If this declaration may have caused an inline variable definition to
3656 // change linkage, make sure that it's emitted.
3657 if (Context.getInlineVariableDefinitionKind(VD) ==
3660 return;
3661 }
3662 }
3663
3664 // Defer code generation to first use when possible, e.g. if this is an inline
3665 // function. If the global must always be emitted, do it eagerly if possible
3666 // to benefit from cache locality.
3667 if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
3668 // Emit the definition if it can't be deferred.
3669 EmitGlobalDefinition(GD);
3670 addEmittedDeferredDecl(GD);
3671 return;
3672 }
3673
3674 // If we're deferring emission of a C++ variable with an
3675 // initializer, remember the order in which it appeared in the file.
3676 if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
3677 cast<VarDecl>(Global)->hasInit()) {
3678 DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
3679 CXXGlobalInits.push_back(nullptr);
3680 }
3681
3682 StringRef MangledName = getMangledName(GD);
3683 if (GetGlobalValue(MangledName) != nullptr) {
3684 // The value has already been used and should therefore be emitted.
3685 addDeferredDeclToEmit(GD);
3686 } else if (MustBeEmitted(Global)) {
3687 // The value must be emitted, but cannot be emitted eagerly.
3688 assert(!MayBeEmittedEagerly(Global));
3689 addDeferredDeclToEmit(GD);
3690 } else {
3691 // Otherwise, remember that we saw a deferred decl with this name. The
3692 // first use of the mangled name will cause it to move into
3693 // DeferredDeclsToEmit.
3694 DeferredDecls[MangledName] = GD;
3695 }
3696}
3697
3698// Check if T is a class type with a destructor that's not dllimport.
3700 if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
3701 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
3702 if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
3703 return true;
3704
3705 return false;
3706}
3707
3708namespace {
3709 struct FunctionIsDirectlyRecursive
3710 : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
3711 const StringRef Name;
3712 const Builtin::Context &BI;
3713 FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
3714 : Name(N), BI(C) {}
3715
3716 bool VisitCallExpr(const CallExpr *E) {
3717 const FunctionDecl *FD = E->getDirectCallee();
3718 if (!FD)
3719 return false;
3720 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3721 if (Attr && Name == Attr->getLabel())
3722 return true;
3723 unsigned BuiltinID = FD->getBuiltinID();
3724 if (!BuiltinID || !BI.isLibFunction(BuiltinID))
3725 return false;
3726 StringRef BuiltinName = BI.getName(BuiltinID);
3727 if (BuiltinName.startswith("__builtin_") &&
3728 Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
3729 return true;
3730 }
3731 return false;
3732 }
3733
3734 bool VisitStmt(const Stmt *S) {
3735 for (const Stmt *Child : S->children())
3736 if (Child && this->Visit(Child))
3737 return true;
3738 return false;
3739 }
3740 };
3741
3742 // Make sure we're not referencing non-imported vars or functions.
3743 struct DLLImportFunctionVisitor
3744 : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
3745 bool SafeToInline = true;
3746
3747 bool shouldVisitImplicitCode() const { return true; }
3748
3749 bool VisitVarDecl(VarDecl *VD) {
3750 if (VD->getTLSKind()) {
3751 // A thread-local variable cannot be imported.
3752 SafeToInline = false;
3753 return SafeToInline;
3754 }
3755
3756 // A variable definition might imply a destructor call.
3758 SafeToInline = !HasNonDllImportDtor(VD->getType());
3759
3760 return SafeToInline;
3761 }
3762
3763 bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
3764 if (const auto *D = E->getTemporary()->getDestructor())
3765 SafeToInline = D->hasAttr<DLLImportAttr>();
3766 return SafeToInline;
3767 }
3768
3769 bool VisitDeclRefExpr(DeclRefExpr *E) {
3770 ValueDecl *VD = E->getDecl();
3771 if (isa<FunctionDecl>(VD))
3772 SafeToInline = VD->hasAttr<DLLImportAttr>();
3773 else if (VarDecl *V = dyn_cast<VarDecl>(VD))
3774 SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
3775 return SafeToInline;
3776 }
3777
3778 bool VisitCXXConstructExpr(CXXConstructExpr *E) {
3779 SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
3780 return SafeToInline;
3781 }
3782
3783 bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
3784 CXXMethodDecl *M = E->getMethodDecl();
3785 if (!M) {
3786 // Call through a pointer to member function. This is safe to inline.
3787 SafeToInline = true;
3788 } else {
3789 SafeToInline = M->hasAttr<DLLImportAttr>();
3790 }
3791 return SafeToInline;
3792 }
3793
3794 bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
3795 SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
3796 return SafeToInline;
3797 }
3798
3799 bool VisitCXXNewExpr(CXXNewExpr *E) {
3800 SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
3801 return SafeToInline;
3802 }
3803 };
3804}
3805
3806// isTriviallyRecursive - Check if this function calls another
3807// decl that, because of the asm attribute or the other decl being a builtin,
3808// ends up pointing to itself.
3809bool
3810CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
3811 StringRef Name;
3812 if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
3813 // asm labels are a special kind of mangling we have to support.
3814 AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
3815 if (!Attr)
3816 return false;
3817 Name = Attr->getLabel();
3818 } else {
3819 Name = FD->getName();
3820 }
3821
3822 FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
3823 const Stmt *Body = FD->getBody();
3824 return Body ? Walker.Visit(Body) : false;
3825}
3826
3827bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
3828 if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
3829 return true;
3830 const auto *F = cast<FunctionDecl>(GD.getDecl());
3831 if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
3832 return false;
3833
3834 if (F->hasAttr<DLLImportAttr>() && !F->hasAttr<AlwaysInlineAttr>()) {
3835 // Check whether it would be safe to inline this dllimport function.
3836 DLLImportFunctionVisitor Visitor;
3837 Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
3838 if (!Visitor.SafeToInline)
3839 return false;
3840
3841 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
3842 // Implicit destructor invocations aren't captured in the AST, so the
3843 // check above can't see them. Check for them manually here.
3844 for (const Decl *Member : Dtor->getParent()->decls())
3845 if (isa<FieldDecl>(Member))
3846 if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
3847 return false;
3848 for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
3849 if (HasNonDllImportDtor(B.getType()))
3850 return false;
3851 }
3852 }
3853
3854 // Inline builtins declaration must be emitted. They often are fortified
3855 // functions.
3856 if (F->isInlineBuiltinDeclaration())
3857 return true;
3858
3859 // PR9614. Avoid cases where the source code is lying to us. An available
3860 // externally function should have an equivalent function somewhere else,
3861 // but a function that calls itself through asm label/`__builtin_` trickery is
3862 // clearly not equivalent to the real implementation.
3863 // This happens in glibc's btowc and in some configure checks.
3864 return !isTriviallyRecursive(F);
3865}
3866
3867bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
3868 return CodeGenOpts.OptimizationLevel > 0;
3869}
3870
3871void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
3872 llvm::GlobalValue *GV) {
3873 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3874
3875 if (FD->isCPUSpecificMultiVersion()) {
3876 auto *Spec = FD->getAttr<CPUSpecificAttr>();
3877 for (unsigned I = 0; I < Spec->cpus_size(); ++I)
3878 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3879 } else if (FD->isTargetClonesMultiVersion()) {
3880 auto *Clone = FD->getAttr<TargetClonesAttr>();
3881 for (unsigned I = 0; I < Clone->featuresStrs_size(); ++I)
3882 if (Clone->isFirstOfVersion(I))
3883 EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
3884 // Ensure that the resolver function is also emitted.
3885 GetOrCreateMultiVersionResolver(GD);
3886 } else
3887 EmitGlobalFunctionDefinition(GD, GV);
3888}
3889
3890void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
3891 const auto *D = cast<ValueDecl>(GD.getDecl());
3892
3893 PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
3894 Context.getSourceManager(),
3895 "Generating code for declaration");
3896
3897 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3898 // At -O0, don't generate IR for functions with available_externally
3899 // linkage.
3900 if (!shouldEmitFunction(GD))
3901 return;
3902
3903 llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
3904 std::string Name;
3905 llvm::raw_string_ostream OS(Name);
3906 FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
3907 /*Qualified=*/true);
3908 return Name;
3909 });
3910
3911 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
3912 // Make sure to emit the definition(s) before we emit the thunks.
3913 // This is necessary for the generation of certain thunks.
3914 if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
3915 ABI->emitCXXStructor(GD);
3916 else if (FD->isMultiVersion())
3917 EmitMultiVersionFunctionDefinition(GD, GV);
3918 else
3919 EmitGlobalFunctionDefinition(GD, GV);
3920
3921 if (Method->isVirtual())
3922 getVTables().EmitThunks(GD);
3923
3924 return;
3925 }
3926
3927 if (FD->isMultiVersion())
3928 return EmitMultiVersionFunctionDefinition(GD, GV);
3929 return EmitGlobalFunctionDefinition(GD, GV);
3930 }
3931
3932 if (const auto *VD = dyn_cast<VarDecl>(D))
3933 return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
3934
3935 llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
3936}
3937
3938static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
3939 llvm::Function *NewFn);
3940
3941static unsigned
3943 const CodeGenFunction::MultiVersionResolverOption &RO) {
3944 unsigned Priority = 0;
3945 unsigned NumFeatures = 0;
3946 for (StringRef Feat : RO.Conditions.Features) {
3947 Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
3948 NumFeatures++;
3949 }
3950
3951 if (!RO.Conditions.Architecture.empty())
3952 Priority = std::max(
3953 Priority, TI.multiVersionSortPriority(RO.Conditions.Architecture));
3954
3955 Priority += TI.multiVersionFeatureCost() * NumFeatures;
3956
3957 return Priority;
3958}
3959
3960// Multiversion functions should be at most 'WeakODRLinkage' so that a different
3961// TU can forward declare the function without causing problems. Particularly
3962// in the cases of CPUDispatch, this causes issues. This also makes sure we
3963// work with internal linkage functions, so that the same function name can be
3964// used with internal linkage in multiple TUs.
3965llvm::GlobalValue::LinkageTypes getMultiversionLinkage(CodeGenModule &CGM,
3966 GlobalDecl GD) {
3967 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3968 if (FD->getFormalLinkage() == InternalLinkage)
3969 return llvm::GlobalValue::InternalLinkage;
3970 return llvm::GlobalValue::WeakODRLinkage;
3971}
3972
3973void CodeGenModule::emitMultiVersionFunctions() {
3974 std::vector<GlobalDecl> MVFuncsToEmit;
3975 MultiVersionFuncs.swap(MVFuncsToEmit);
3976 for (GlobalDecl GD : MVFuncsToEmit) {
3977 const auto *FD = cast<FunctionDecl>(GD.getDecl());
3978 assert(FD && "Expected a FunctionDecl");
3979
3981 if (FD->isTargetMultiVersion()) {
3983 FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
3984 GlobalDecl CurGD{
3985 (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
3986 StringRef MangledName = getMangledName(CurGD);
3987 llvm::Constant *Func = GetGlobalValue(MangledName);
3988 if (!Func) {
3989 if (CurFD->isDefined()) {
3990 EmitGlobalFunctionDefinition(CurGD, nullptr);
3991 Func = GetGlobalValue(MangledName);
3992 } else {
3993 const CGFunctionInfo &FI =
3995 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3996 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
3997 /*DontDefer=*/false, ForDefinition);
3998 }
3999 assert(Func && "This should have just been created");
4000 }
4002 const auto *TA = CurFD->getAttr<TargetAttr>();
4004 TA->getAddedFeatures(Feats);
4005 Options.emplace_back(cast<llvm::Function>(Func),
4006 TA->getArchitecture(), Feats);
4007 } else {
4008 const auto *TVA = CurFD->getAttr<TargetVersionAttr>();
4010 TVA->getFeatures(Feats);
4011 Options.emplace_back(cast<llvm::Function>(Func),
4012 /*Architecture*/ "", Feats);
4013 }
4014 });
4015 } else if (FD->isTargetClonesMultiVersion()) {
4016 const auto *TC = FD->getAttr<TargetClonesAttr>();
4017 for (unsigned VersionIndex = 0; VersionIndex < TC->featuresStrs_size();
4018 ++VersionIndex) {
4019 if (!TC->isFirstOfVersion(VersionIndex))
4020 continue;
4021 GlobalDecl CurGD{(FD->isDefined() ? FD->getDefinition() : FD),
4022 VersionIndex};
4023 StringRef Version = TC->getFeatureStr(VersionIndex);
4024 StringRef MangledName = getMangledName(CurGD);
4025 llvm::Constant *Func = GetGlobalValue(MangledName);
4026 if (!Func) {
4027 if (FD->isDefined()) {
4028 EmitGlobalFunctionDefinition(CurGD, nullptr);
4029 Func = GetGlobalValue(MangledName);
4030 } else {
4031 const CGFunctionInfo &FI =
4033 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4034 Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
4035 /*DontDefer=*/false, ForDefinition);
4036 }
4037 assert(Func && "This should have just been created");
4038 }
4039
4040 StringRef Architecture;
4042
4043 if (getTarget().getTriple().isAArch64()) {
4044 if (Version != "default") {
4046 Version.split(VerFeats, "+");
4047 for (auto &CurFeat : VerFeats)
4048 Feature.push_back(CurFeat.trim());
4049 }
4050 } else {
4051 if (Version.startswith("arch="))
4052 Architecture = Version.drop_front(sizeof("arch=") - 1);
4053 else if (Version != "default")
4054 Feature.push_back(Version);
4055 }
4056
4057 Options.emplace_back(cast<llvm::Function>(Func), Architecture, Feature);
4058 }
4059 } else {
4060 assert(0 && "Expected a target or target_clones multiversion function");
4061 continue;
4062 }
4063
4064 llvm::Constant *ResolverConstant = GetOrCreateMultiVersionResolver(GD);
4065 if (auto *IFunc = dyn_cast<llvm::GlobalIFunc>(ResolverConstant))
4066 ResolverConstant = IFunc->getResolver();
4067 llvm::Function *ResolverFunc = cast<llvm::Function>(ResolverConstant);
4068
4069 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4070
4071 if (!ResolverFunc->hasLocalLinkage() && supportsCOMDAT())
4072 ResolverFunc->setComdat(
4073 getModule().getOrInsertComdat(ResolverFunc->getName()));
4074
4075 const TargetInfo &TI = getTarget();
4076 llvm::stable_sort(
4077 Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
4078 const CodeGenFunction::MultiVersionResolverOption &RHS) {
4079 return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
4080 });
4081 CodeGenFunction CGF(*this);
4082 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4083 }
4084
4085 // Ensure that any additions to the deferred decls list caused by emitting a
4086 // variant are emitted. This can happen when the variant itself is inline and
4087 // calls a function without linkage.
4088 if (!MVFuncsToEmit.empty())
4089 EmitDeferred();
4090
4091 // Ensure that any additions to the multiversion funcs list from either the
4092 // deferred decls or the multiversion functions themselves are emitted.
4093 if (!MultiVersionFuncs.empty())
4094 emitMultiVersionFunctions();
4095}
4096
4097void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
4098 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4099 assert(FD && "Not a FunctionDecl?");
4100 assert(FD->isCPUDispatchMultiVersion() && "Not a multiversion function?");
4101 const auto *DD = FD->getAttr<CPUDispatchAttr>();
4102 assert(DD && "Not a cpu_dispatch Function?");
4103
4105 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4106
4107 StringRef ResolverName = getMangledName(GD);
4108 UpdateMultiVersionNames(GD, FD, ResolverName);
4109
4110 llvm::Type *ResolverType;
4111 GlobalDecl ResolverGD;
4112 if (getTarget().supportsIFunc()) {
4113 ResolverType = llvm::FunctionType::get(
4114 llvm::PointerType::get(DeclTy,
4115 getTypes().getTargetAddressSpace(FD->getType())),
4116 false);
4117 }
4118 else {
4119 ResolverType = DeclTy;
4120 ResolverGD = GD;
4121 }
4122
4123 auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
4124 ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
4125 ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
4126 if (supportsCOMDAT())
4127 ResolverFunc->setComdat(
4128 getModule().getOrInsertComdat(ResolverFunc->getName()));
4129
4131 const TargetInfo &Target = getTarget();
4132 unsigned Index = 0;
4133 for (const IdentifierInfo *II : DD->cpus()) {
4134 // Get the name of the target function so we can look it up/create it.
4135 std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
4136 getCPUSpecificMangling(*this, II->getName());
4137
4138 llvm::Constant *Func = GetGlobalValue(MangledName);
4139
4140 if (!Func) {
4141 GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
4142 if (ExistingDecl.getDecl() &&
4143 ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
4144 EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
4145 Func = GetGlobalValue(MangledName);
4146 } else {
4147 if (!ExistingDecl.getDecl())
4148 ExistingDecl = GD.getWithMultiVersionIndex(Index);
4149
4150 Func = GetOrCreateLLVMFunction(
4151 MangledName, DeclTy, ExistingDecl,
4152 /*ForVTable=*/false, /*DontDefer=*/true,
4153 /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
4154 }
4155 }
4156
4158 Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
4159 llvm::transform(Features, Features.begin(),
4160 [](StringRef Str) { return Str.substr(1); });
4161 llvm::erase_if(Features, [&Target](StringRef Feat) {
4162 return !Target.validateCpuSupports(Feat);
4163 });
4164 Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
4165 ++Index;
4166 }
4167
4168 llvm::stable_sort(
4169 Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
4170 const CodeGenFunction::MultiVersionResolverOption &RHS) {
4171 return llvm::X86::getCpuSupportsMask(LHS.Conditions.Features) >
4172 llvm::X86::getCpuSupportsMask(RHS.Conditions.Features);
4173 });
4174
4175 // If the list contains multiple 'default' versions, such as when it contains
4176 // 'pentium' and 'generic', don't emit the call to the generic one (since we
4177 // always run on at least a 'pentium'). We do this by deleting the 'least
4178 // advanced' (read, lowest mangling letter).
4179 while (Options.size() > 1 &&
4180 llvm::all_of(llvm::X86::getCpuSupportsMask(
4181 (Options.end() - 2)->Conditions.Features),
4182 [](auto X) { return X == 0; })) {
4183 StringRef LHSName = (Options.end() - 2)->Function->getName();
4184 StringRef RHSName = (Options.end() - 1)->Function->getName();
4185 if (LHSName.compare(RHSName) < 0)
4186 Options.erase(Options.end() - 2);
4187 else
4188 Options.erase(Options.end() - 1);
4189 }
4190
4191 CodeGenFunction CGF(*this);
4192 CGF.EmitMultiVersionResolver(ResolverFunc, Options);
4193
4194 if (getTarget().supportsIFunc()) {
4195 llvm::GlobalValue::LinkageTypes Linkage = getMultiversionLinkage(*this, GD);
4196 auto *IFunc = cast<llvm::GlobalValue>(GetOrCreateMultiVersionResolver(GD));
4197
4198 // Fix up function declarations that were created for cpu_specific before
4199 // cpu_dispatch was known
4200 if (!isa<llvm::GlobalIFunc>(IFunc)) {
4201 assert(cast<llvm::Function>(IFunc)->isDeclaration());
4202 auto *GI = llvm::GlobalIFunc::create(DeclTy, 0, Linkage, "", ResolverFunc,
4203 &getModule());
4204 GI->takeName(IFunc);
4205 IFunc->replaceAllUsesWith(GI);
4206 IFunc->eraseFromParent();
4207 IFunc = GI;
4208 }
4209
4210 std::string AliasName = getMangledNameImpl(
4211 *this, GD, FD, /*OmitMultiVersionMangling=*/true);
4212 llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
4213 if (!AliasFunc) {
4214 auto *GA = llvm::GlobalAlias::create(DeclTy, 0, Linkage, AliasName, IFunc,
4215 &getModule());
4216 SetCommonAttributes(GD, GA);
4217 }
4218 }
4219}
4220
4221/// If a dispatcher for the specified mangled name is not in the module, create
4222/// and return an llvm Function with the specified type.
4223llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
4224 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4225 assert(FD && "Not a FunctionDecl?");
4226
4227 std::string MangledName =
4228 getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
4229
4230 // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
4231 // a separate resolver).
4232 std::string ResolverName = MangledName;
4233 if (getTarget().supportsIFunc())
4234 ResolverName += ".ifunc";
4235 else if (FD->isTargetMultiVersion())
4236 ResolverName += ".resolver";
4237
4238 // If the resolver has already been created, just return it.
4239 if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
4240 return ResolverGV;
4241
4243 llvm::FunctionType *DeclTy = getTypes().GetFunctionType(FI);
4244
4245 // The resolver needs to be created. For target and target_clones, defer
4246 // creation until the end of the TU.
4248 MultiVersionFuncs.push_back(GD);
4249
4250 // For cpu_specific, don't create an ifunc yet because we don't know if the
4251 // cpu_dispatch will be emitted in this translation unit.
4252 if (getTarget().supportsIFunc() && !FD->isCPUSpecificMultiVersion()) {
4253 llvm::Type *ResolverType = llvm::FunctionType::get(
4254 llvm::PointerType::get(DeclTy,
4255 getTypes().getTargetAddressSpace(FD->getType())),
4256 false);
4257 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4258 MangledName + ".resolver", ResolverType, GlobalDecl{},
4259 /*ForVTable=*/false);
4260 llvm::GlobalIFunc *GIF =
4261 llvm::GlobalIFunc::create(DeclTy, 0, getMultiversionLinkage(*this, GD),
4262 "", Resolver, &getModule());
4263 GIF->setName(ResolverName);
4264 SetCommonAttributes(FD, GIF);
4265
4266 return GIF;
4267 }
4268
4269 llvm::Constant *Resolver = GetOrCreateLLVMFunction(
4270 ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
4271 assert(isa<llvm::GlobalValue>(Resolver) &&
4272 "Resolver should be created for the first time");
4273 SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
4274 return Resolver;
4275}
4276
4277/// GetOrCreateLLVMFunction - If the specified mangled name is not in the
4278/// module, create and return an llvm Function with the specified type. If there
4279/// is something in the module with the specified name, return it potentially
4280/// bitcasted to the right type.
4281///
4282/// If D is non-null, it specifies a decl that correspond to this. This is used
4283/// to set the attributes on the function when it is first created.
4284llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
4285 StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
4286 bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
4287 ForDefinition_t IsForDefinition) {
4288 const Decl *D = GD.getDecl();
4289
4290 // Any attempts to use a MultiVersion function should result in retrieving
4291 // the iFunc instead. Name Mangling will handle the rest of the changes.
4292 if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
4293 // For the device mark the function as one that should be emitted.
4294 if (getLangOpts().OpenMPIsTargetDevice && OpenMPRuntime &&
4295 !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
4296 !DontDefer && !IsForDefinition) {
4297 if (const FunctionDecl *FDDef = FD->getDefinition()) {
4298 GlobalDecl GDDef;
4299 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
4300 GDDef = GlobalDecl(CD, GD.getCtorType());
4301 else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
4302 GDDef = GlobalDecl(DD, GD.getDtorType());
4303 else
4304 GDDef = GlobalDecl(FDDef);
4305 EmitGlobal(GDDef);
4306 }
4307 }
4308
4309 if (FD->isMultiVersion()) {
4310 UpdateMultiVersionNames(GD, FD, MangledName);
4311 if (!IsForDefinition)
4312 return GetOrCreateMultiVersionResolver(GD);
4313 }
4314 }
4315
4316 // Lookup the entry, lazily creating it if necessary.
4317 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4318 if (Entry) {
4319 if (WeakRefReferences.erase(Entry)) {
4320 const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
4321 if (FD && !FD->hasAttr<WeakAttr>())
4322 Entry->setLinkage(llvm::Function::ExternalLinkage);
4323 }
4324
4325 // Handle dropped DLL attributes.
4326 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4327 !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
4328 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4329 setDSOLocal(Entry);
4330 }
4331
4332 // If there are two attempts to define the same mangled name, issue an
4333 // error.
4334 if (IsForDefinition && !Entry->isDeclaration()) {
4335 GlobalDecl OtherGD;
4336 // Check that GD is not yet in DiagnosedConflictingDefinitions is required
4337 // to make sure that we issue an error only once.
4338 if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4339 (GD.getCanonicalDecl().getDecl() !=
4340 OtherGD.getCanonicalDecl().getDecl()) &&
4341 DiagnosedConflictingDefinitions.insert(GD).second) {
4342 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4343 << MangledName;
4344 getDiags().Report(OtherGD.getDecl()->getLocation(),
4345 diag::note_previous_definition);
4346 }
4347 }
4348
4349 if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
4350 (Entry->getValueType() == Ty)) {
4351 return Entry;
4352 }
4353
4354 // Make sure the result is of the correct type.
4355 // (If function is requested for a definition, we always need to create a new
4356 // function, not just return a bitcast.)
4357 if (!IsForDefinition)
4358 return Entry;
4359 }
4360
4361 // This function doesn't have a complete type (for example, the return
4362 // type is an incomplete struct). Use a fake type instead, and make
4363 // sure not to try to set attributes.
4364 bool IsIncompleteFunction = false;
4365
4366 llvm::FunctionType *FTy;
4367 if (isa<llvm::FunctionType>(Ty)) {
4368 FTy = cast<llvm::FunctionType>(Ty);
4369 } else {
4370 FTy = llvm::FunctionType::get(VoidTy, false);
4371 IsIncompleteFunction = true;
4372 }
4373
4374 llvm::Function *F =
4375 llvm::Function::Create(FTy, llvm::Function::ExternalLinkage,
4376 Entry ? StringRef() : MangledName, &getModule());
4377
4378 // If we already created a function with the same mangled name (but different
4379 // type) before, take its name and add it to the list of functions to be
4380 // replaced with F at the end of CodeGen.
4381 //
4382 // This happens if there is a prototype for a function (e.g. "int f()") and
4383 // then a definition of a different type (e.g. "int f(int x)").
4384 if (Entry) {
4385 F->takeName(Entry);
4386
4387 // This might be an implementation of a function without a prototype, in
4388 // which case, try to do special replacement of calls which match the new
4389 // prototype. The really key thing here is that we also potentially drop
4390 // arguments from the call site so as to make a direct call, which makes the
4391 // inliner happier and suppresses a number of optimizer warnings (!) about
4392 // dropping arguments.
4393 if (!Entry->use_empty()) {
4395 Entry->removeDeadConstantUsers();
4396 }
4397
4398 addGlobalValReplacement(Entry, F);
4399 }
4400
4401 assert(F->getName() == MangledName && "name was uniqued!");
4402 if (D)
4403 SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
4404 if (ExtraAttrs.hasFnAttrs()) {
4405 llvm::AttrBuilder B(F->getContext(), ExtraAttrs.getFnAttrs());
4406 F->addFnAttrs(B);
4407 }
4408
4409 if (!DontDefer) {
4410 // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
4411 // each other bottoming out with the base dtor. Therefore we emit non-base
4412 // dtors on usage, even if there is no dtor definition in the TU.
4413 if (isa_and_nonnull<CXXDestructorDecl>(D) &&
4414 getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
4415 GD.getDtorType()))
4416 addDeferredDeclToEmit(GD);
4417
4418 // This is the first use or definition of a mangled name. If there is a
4419 // deferred decl with this name, remember that we need to emit it at the end
4420 // of the file.
4421 auto DDI = DeferredDecls.find(MangledName);
4422 if (DDI != DeferredDecls.end()) {
4423 // Move the potentially referenced deferred decl to the
4424 // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
4425 // don't need it anymore).
4426 addDeferredDeclToEmit(DDI->second);
4427 DeferredDecls.erase(DDI);
4428
4429 // Otherwise, there are cases we have to worry about where we're
4430 // using a declaration for which we must emit a definition but where
4431 // we might not find a top-level definition:
4432 // - member functions defined inline in their classes
4433 // - friend functions defined inline in some class
4434 // - special member functions with implicit definitions
4435 // If we ever change our AST traversal to walk into class methods,
4436 // this will be unnecessary.
4437 //
4438 // We also don't emit a definition for a function if it's going to be an
4439 // entry in a vtable, unless it's already marked as used.
4440 } else if (getLangOpts().CPlusPlus && D) {
4441 // Look for a declaration that's lexically in a record.
4442 for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
4443 FD = FD->getPreviousDecl()) {
4444 if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
4445 if (FD->doesThisDeclarationHaveABody()) {
4446 addDeferredDeclToEmit(GD.getWithDecl(FD));
4447 break;
4448 }
4449 }
4450 }
4451 }
4452 }
4453
4454 // Make sure the result is of the requested type.
4455 if (!IsIncompleteFunction) {
4456 assert(F->getFunctionType() == Ty);
4457 return F;
4458 }
4459
4460 return F;
4461}
4462
4463/// GetAddrOfFunction - Return the address of the given function. If Ty is
4464/// non-null, then this function will use the specified type if it has to
4465/// create it (this occurs when we see a definition of the function).
4466llvm::Constant *
4467CodeGenModule::GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty, bool ForVTable,
4468 bool DontDefer,
4469 ForDefinition_t IsForDefinition) {
4470 // If there was no specific requested type, just convert it now.
4471 if (!Ty) {
4472 const auto *FD = cast<FunctionDecl>(GD.getDecl());
4473 Ty = getTypes().ConvertType(FD->getType());
4474 }
4475
4476 // Devirtualized destructor calls may come through here instead of via
4477 // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
4478 // of the complete destructor when necessary.
4479 if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
4480 if (getTarget().getCXXABI().isMicrosoft() &&
4481 GD.getDtorType() == Dtor_Complete &&
4482 DD->getParent()->getNumVBases() == 0)
4483 GD = GlobalDecl(DD, Dtor_Base);
4484 }
4485
4486 StringRef MangledName = getMangledName(GD);
4487 auto *F = GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
4488 /*IsThunk=*/false, llvm::AttributeList(),
4489 IsForDefinition);
4490 // Returns kernel handle for HIP kernel stub function.
4491 if (LangOpts.CUDA && !LangOpts.CUDAIsDevice &&
4492 cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
4493 auto *Handle = getCUDARuntime().getKernelHandle(
4494 cast<llvm::Function>(F->stripPointerCasts()), GD);
4495 if (IsForDefinition)
4496 return F;
4497 return Handle;
4498 }
4499 return F;
4500}
4501
4503 llvm::GlobalValue *F =
4504 cast<llvm::GlobalValue>(GetAddrOfFunction(Decl)->stripPointerCasts());
4505
4506 return llvm::ConstantExpr::getBitCast(
4507 llvm::NoCFIValue::get(F),
4508 llvm::PointerType::get(VMContext, F->getAddressSpace()));
4509}
4510
4511static const FunctionDecl *
4513 TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
4515
4516 IdentifierInfo &CII = C.Idents.get(Name);
4517 for (const auto *Result : DC->lookup(&CII))
4518 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4519 return FD;
4520
4521 if (!C.getLangOpts().CPlusPlus)
4522 return nullptr;
4523
4524 // Demangle the premangled name from getTerminateFn()
4525 IdentifierInfo &CXXII =
4526 (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
4527 ? C.Idents.get("terminate")
4528 : C.Idents.get(Name);
4529
4530 for (const auto &N : {"__cxxabiv1", "std"}) {
4531 IdentifierInfo &NS = C.Idents.get(N);
4532 for (const auto *Result : DC->lookup(&NS)) {
4533 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
4534 if (auto *LSD = dyn_cast<LinkageSpecDecl>(Result))
4535 for (const auto *Result : LSD->lookup(&NS))
4536 if ((ND = dyn_cast<NamespaceDecl>(Result)))
4537 break;
4538
4539 if (ND)
4540 for (const auto *Result : ND->lookup(&CXXII))
4541 if (const auto *FD = dyn_cast<FunctionDecl>(Result))
4542 return FD;
4543 }
4544 }
4545
4546 return nullptr;
4547}
4548
4549/// CreateRuntimeFunction - Create a new runtime function with the specified
4550/// type and name.
4551llvm::FunctionCallee
4552CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
4553 llvm::AttributeList ExtraAttrs, bool Local,
4554 bool AssumeConvergent) {
4555 if (AssumeConvergent) {
4556 ExtraAttrs =
4557 ExtraAttrs.addFnAttribute(VMContext, llvm::Attribute::Convergent);
4558 }
4559
4560 llvm::Constant *C =
4561 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
4562 /*DontDefer=*/false, /*IsThunk=*/false,
4563 ExtraAttrs);
4564
4565 if (auto *F = dyn_cast<llvm::Function>(C)) {
4566 if (F->empty()) {
4567 F->setCallingConv(getRuntimeCC());
4568
4569 // In Windows Itanium environments, try to mark runtime functions
4570 // dllimport. For Mingw and MSVC, don't. We don't really know if the user
4571 // will link their standard library statically or dynamically. Marking
4572 // functions imported when they are not imported can cause linker errors
4573 // and warnings.
4574 if (!Local && getTriple().isWindowsItaniumEnvironment() &&
4575 !getCodeGenOpts().LTOVisibilityPublicStd) {
4576 const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
4577 if (!FD || FD->hasAttr<DLLImportAttr>()) {
4578 F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4579 F->setLinkage(llvm::GlobalValue::ExternalLinkage);
4580 }
4581 }
4582 setDSOLocal(F);
4583 }
4584 }
4585
4586 return {FTy, C};
4587}
4588
4589/// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
4590/// create and return an llvm GlobalVariable with the specified type and address
4591/// space. If there is something in the module with the specified name, return
4592/// it potentially bitcasted to the right type.
4593///
4594/// If D is non-null, it specifies a decl that correspond to this. This is used
4595/// to set the attributes on the global when it is first created.
4596///
4597/// If IsForDefinition is true, it is guaranteed that an actual global with
4598/// type Ty will be returned, not conversion of a variable with the same
4599/// mangled name but some other type.
4600llvm::Constant *
4601CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
4602 LangAS AddrSpace, const VarDecl *D,
4603 ForDefinition_t IsForDefinition) {
4604 // Lookup the entry, lazily creating it if necessary.
4605 llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4606 unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
4607 if (Entry) {
4608 if (WeakRefReferences.erase(Entry)) {
4609 if (D && !D->hasAttr<WeakAttr>())
4610 Entry->setLinkage(llvm::Function::ExternalLinkage);
4611 }
4612
4613 // Handle dropped DLL attributes.
4614 if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
4616 Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
4617
4618 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
4620
4621 if (Entry->getValueType() == Ty && Entry->getAddressSpace() == TargetAS)
4622 return Entry;
4623
4624 // If there are two attempts to define the same mangled name, issue an
4625 // error.
4626 if (IsForDefinition && !Entry->isDeclaration()) {
4627 GlobalDecl OtherGD;
4628 const VarDecl *OtherD;
4629
4630 // Check that D is not yet in DiagnosedConflictingDefinitions is required
4631 // to make sure that we issue an error only once.
4632 if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
4633 (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
4634 (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
4635 OtherD->hasInit() &&
4636 DiagnosedConflictingDefinitions.insert(D).second) {
4637 getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
4638 << MangledName;
4639 getDiags().Report(OtherGD.getDecl()->getLocation(),
4640 diag::note_previous_definition);
4641 }
4642 }
4643
4644 // Make sure the result is of the correct type.
4645 if (Entry->getType()->getAddressSpace() != TargetAS)
4646 return llvm::ConstantExpr::getAddrSpaceCast(
4647 Entry, llvm::PointerType::get(Ty->getContext(), TargetAS));
4648
4649 // (If global is requested for a definition, we always need to create a new
4650 // global, not just return a bitcast.)
4651 if (!IsForDefinition)
4652 return Entry;
4653 }
4654
4655 auto DAddrSpace = GetGlobalVarAddressSpace(D);
4656
4657 auto *GV = new llvm::GlobalVariable(
4658 getModule(), Ty, false, llvm::GlobalValue::ExternalLinkage, nullptr,
4659 MangledName, nullptr, llvm::GlobalVariable::NotThreadLocal,
4660 getContext().getTargetAddressSpace(DAddrSpace));
4661
4662 // If we already created a global with the same mangled name (but different
4663 // type) before, take its name and remove it from its parent.
4664 if (Entry) {
4665 GV->takeName(Entry);
4666
4667 if (!Entry->use_empty()) {
4668 llvm::Constant *NewPtrForOldDecl =
4669 llvm::ConstantExpr::getBitCast(GV, Entry->getType());
4670 Entry->replaceAllUsesWith(NewPtrForOldDecl);
4671 }
4672
4673 Entry->eraseFromParent();
4674 }
4675
4676 // This is the first use or definition of a mangled name. If there is a
4677 // deferred decl with this name, remember that we need to emit it at the end
4678 // of the file.
4679 auto DDI = DeferredDecls.find(MangledName);
4680 if (DDI != DeferredDecls.end()) {
4681 // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
4682 // list, and remove it from DeferredDecls (since we don't need it anymore).
4683 addDeferredDeclToEmit(DDI->second);
4684 DeferredDecls.erase(DDI);
4685 }
4686
4687 // Handle things which are present even on external declarations.
4688 if (D) {
4689 if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
4691
4692 // FIXME: This code is overly simple and should be merged with other global
4693 // handling.
4694 GV->setConstant(D->getType().isConstantStorage(getContext(), false, false));
4695
4696 GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
4697
4698 setLinkageForGV(GV, D);
4699
4700 if (D->getTLSKind()) {
4701 if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4702 CXXThreadLocals.push_back(D);
4703 setTLSMode(GV, *D);
4704 }
4705
4706 setGVProperties(GV, D);
4707
4708 // If required by the ABI, treat declarations of static data members with
4709 // inline initializers as definitions.
4710 if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
4711 EmitGlobalVarDefinition(D);
4712 }
4713
4714 // Emit section information for extern variables.
4715 if (D->hasExternalStorage()) {
4716 if (const SectionAttr *SA = D->getAttr<SectionAttr>())
4717 GV->setSection(SA->getName());
4718 }
4719
4720 // Handle XCore specific ABI requirements.
4721 if (getTriple().getArch() == llvm::Triple::xcore &&
4723 D->getType().isConstant(Context) &&
4725 GV->setSection(".cp.rodata");
4726
4727 // Check if we a have a const declaration with an initializer, we may be
4728 // able to emit it as available_externally to expose it's value to the
4729 // optimizer.
4730 if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
4731 D->getType().isConstQualified() && !GV->hasInitializer() &&
4732 !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
4733 const auto *Record =
4735 bool HasMutableFields = Record && Record->hasMutableFields();
4736 if (!HasMutableFields) {
4737 const VarDecl *InitDecl;
4738 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
4739 if (InitExpr) {
4740 ConstantEmitter emitter(*this);
4741 llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
4742 if (Init) {
4743 auto *InitType = Init->getType();
4744 if (GV->getValueType() != InitType) {
4745 // The type of the initializer does not match the definition.
4746 // This happens when an initializer has a different type from
4747 // the type of the global (because of padding at the end of a
4748 // structure for instance).
4749 GV->setName(StringRef());
4750 // Make a new global with the correct type, this is now guaranteed
4751 // to work.
4752 auto *NewGV = cast<llvm::GlobalVariable>(
4753 GetAddrOfGlobalVar(D, InitType, IsForDefinition)
4754 ->stripPointerCasts());
4755
4756 // Erase the old global, since it is no longer used.
4757 GV->eraseFromParent();
4758 GV = NewGV;
4759 } else {
4760 GV->setInitializer(Init);
4761 GV->setConstant(true);
4762 GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
4763 }
4764 emitter.finalize(GV);
4765 }
4766 }
4767 }
4768 }
4769 }
4770
4771 if (D &&
4774 // External HIP managed variables needed to be recorded for transformation
4775 // in both device and host compilations.
4776 if (getLangOpts().CUDA && D && D->hasAttr<HIPManagedAttr>() &&
4777 D->hasExternalStorage())
4779 }
4780
4781 if (D)
4782 SanitizerMD->reportGlobal(GV, *D);
4783
4784 LangAS ExpectedAS =
4785 D ? D->getType().getAddressSpace()
4786 : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
4787 assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS);
4788 if (DAddrSpace != ExpectedAS) {
4790 *this, GV, DAddrSpace, ExpectedAS, Ty->getPointerTo(TargetAS));
4791 }
4792
4793 return GV;
4794}
4795
4796llvm::Constant *
4798 const Decl *D = GD.getDecl();
4799
4800 if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
4801 return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
4802 /*DontDefer=*/false, IsForDefinition);
4803
4804 if (isa<CXXMethodDecl>(D)) {
4805 auto FInfo =
4806 &getTypes().arrangeCXXMethodDeclaration(cast<CXXMethodDecl>(D));
4807 auto Ty = getTypes().GetFunctionType(*FInfo);
4808 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4809 IsForDefinition);
4810 }
4811
4812 if (isa<FunctionDecl>(D)) {
4814 llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4815 return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
4816 IsForDefinition);
4817 }
4818
4819 return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr, IsForDefinition);
4820}
4821
4823 StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
4824 llvm::Align Alignment) {
4825 llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
4826 llvm::GlobalVariable *OldGV = nullptr;
4827
4828 if (GV) {
4829 // Check if the variable has the right type.
4830 if (GV->getValueType() == Ty)
4831 return GV;
4832
4833 // Because C++ name mangling, the only way we can end up with an already
4834 // existing global with the same name is if it has been declared extern "C".
4835 assert(GV->isDeclaration() && "Declaration has wrong type!");
4836 OldGV = GV;
4837 }
4838
4839 // Create a new variable.
4840 GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
4841 Linkage, nullptr, Name);
4842
4843 if (OldGV) {
4844 // Replace occurrences of the old variable if needed.
4845 GV->takeName(OldGV);
4846
4847 if (!OldGV->use_empty()) {
4848 llvm::Constant *NewPtrForOldDecl =
4849 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
4850 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
4851 }
4852
4853 OldGV->eraseFromParent();
4854 }
4855
4856 if (supportsCOMDAT() && GV->isWeakForLinker() &&
4857 !GV->hasAvailableExternallyLinkage())
4858 GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
4859
4860 GV->setAlignment(Alignment);
4861
4862 return GV;
4863}
4864
4865/// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
4866/// given global variable. If Ty is non-null and if the global doesn't exist,
4867/// then it will be created with the specified type instead of whatever the
4868/// normal requested type would be. If IsForDefinition is true, it is guaranteed
4869/// that an actual global with type Ty will be returned, not conversion of a
4870/// variable with the same mangled name but some other type.
4872 llvm::Type *Ty,
4873 ForDefinition_t IsForDefinition) {
4874 assert(D->hasGlobalStorage() && "Not a global variable");
4875 QualType ASTTy = D->getType();
4876 if (!Ty)
4877 Ty = getTypes().ConvertTypeForMem(ASTTy);
4878
4879 StringRef MangledName = getMangledName(D);
4880 return GetOrCreateLLVMGlobal(MangledName, Ty, ASTTy.getAddressSpace(), D,
4881 IsForDefinition);
4882}
4883
4884/// CreateRuntimeVariable - Create a new runtime global variable with the
4885/// specified type and name.
4886llvm::Constant *
4888 StringRef Name) {
4889 LangAS AddrSpace = getContext().getLangOpts().OpenCL ? LangAS::opencl_global
4891 auto *Ret = GetOrCreateLLVMGlobal(Name, Ty, AddrSpace, nullptr);
4892 setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
4893 return Ret;
4894}
4895
4897 assert(!D->getInit() && "Cannot emit definite definitions here!");
4898
4899 StringRef MangledName = getMangledName(D);
4900 llvm::GlobalValue *GV = GetGlobalValue(MangledName);
4901
4902 // We already have a definition, not declaration, with the same mangled name.
4903 // Emitting of declaration is not required (and actually overwrites emitted
4904 // definition).
4905 if (GV && !GV->isDeclaration())
4906 return;
4907
4908 // If we have not seen a reference to this variable yet, place it into the
4909 // deferred declarations table to be emitted if needed later.
4910 if (!MustBeEmitted(D) && !GV) {
4911 DeferredDecls[MangledName] = D;
4912 return;
4913 }
4914
4915 // The tentative definition is the only definition.
4916 EmitGlobalVarDefinition(D);
4917}
4918
4920 EmitExternalVarDeclaration(D);
4921}
4922
4924 return Context.toCharUnitsFromBits(
4925 getDataLayout().getTypeStoreSizeInBits(Ty));
4926}
4927
4929 if (LangOpts.OpenCL) {
4931 assert(AS == LangAS::opencl_global ||
4935 AS == LangAS::opencl_local ||
4937 return AS;
4938 }
4939
4940 if (LangOpts.SYCLIsDevice &&
4941 (!D || D->getType().getAddressSpace() == LangAS::Default))
4942 return LangAS::sycl_global;
4943
4944 if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
4945 if (D) {
4946 if (D->hasAttr<CUDAConstantAttr>())
4947 return LangAS::cuda_constant;
4948 if (D->hasAttr<CUDASharedAttr>())
4949 return LangAS::cuda_shared;
4950 if (D->hasAttr<CUDADeviceAttr>())
4951 return LangAS::cuda_device;
4952 if (D->getType().isConstQualified())
4953 return LangAS::cuda_constant;
4954 }
4955 return LangAS::cuda_device;
4956 }
4957
4958 if (LangOpts.OpenMP) {
4959 LangAS AS;
4960 if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
4961 return AS;
4962 }
4964}
4965
4967 // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
4968 if (LangOpts.OpenCL)
4970 if (LangOpts.SYCLIsDevice)
4971 return LangAS::sycl_global;
4972 if (LangOpts.HIP && LangOpts.CUDAIsDevice && getTriple().isSPIRV())
4973 // For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
4974 // instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
4975 // with OpVariable instructions with Generic storage class which is not
4976 // allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
4977 // UniformConstant storage class is not viable as pointers to it may not be
4978 // casted to Generic pointers which are used to model HIP's "flat" pointers.
4979 return LangAS::cuda_device;
4980 if (auto AS = getTarget().getConstantAddressSpace())
4981 return *AS;
4982 return LangAS::Default;
4983}
4984
4985// In address space agnostic languages, string literals are in default address
4986// space in AST. However, certain targets (e.g. amdgcn) request them to be
4987// emitted in constant address space in LLVM IR. To be consistent with other
4988// parts of AST, string literal global variables in constant address space
4989// need to be casted to default address space before being put into address
4990// map and referenced by other part of CodeGen.
4991// In OpenCL, string literals are in constant address space in AST, therefore
4992// they should not be casted to default address space.
4993static llvm::Constant *
4995 llvm::GlobalVariable *GV) {
4996 llvm::Constant *Cast = GV;
4997 if (!CGM.getLangOpts().OpenCL) {
4998 auto AS = CGM.GetGlobalConstantAddressSpace();
4999 if (AS != LangAS::Default)
5001 CGM, GV, AS, LangAS::Default,
5002 GV->getValueType()->getPointerTo(
5004 }
5005 return Cast;
5006}
5007
5008template<typename SomeDecl>
5010 llvm::GlobalValue *GV) {
5011 if (!getLangOpts().CPlusPlus)
5012 return;
5013
5014 // Must have 'used' attribute, or else inline assembly can't rely on
5015 // the name existing.
5016 if (!D->template hasAttr<UsedAttr>())
5017 return;
5018
5019 // Must have internal linkage and an ordinary name.
5020 if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
5021 return;
5022
5023 // Must be in an extern "C" context. Entities declared directly within
5024 // a record are not extern "C" even if the record is in such a context.
5025 const SomeDecl *First = D->getFirstDecl();
5026 if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
5027 return;
5028
5029 // OK, this is an internal linkage entity inside an extern "C" linkage
5030 // specification. Make a note of that so we can give it the "expected"
5031 // mangled name if nothing else is using that name.
5032 std::pair<StaticExternCMap::iterator, bool> R =
5033 StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
5034
5035 // If we have multiple internal linkage entities with the same name
5036 // in extern "C" regions, none of them gets that name.
5037 if (!R.second)
5038 R.first->second = nullptr;
5039}
5040
5041static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
5042 if (!CGM.supportsCOMDAT())
5043 return false;
5044
5045 if (D.hasAttr<SelectAnyAttr>())
5046 return true;
5047
5049 if (auto *VD = dyn_cast<VarDecl>(&D))
5051 else
5052 Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
5053
5054 switch (Linkage) {
5055 case GVA_Internal:
5057 case GVA_StrongExternal:
5058 return false;
5059 case GVA_DiscardableODR:
5060 case GVA_StrongODR:
5061 return true;
5062 }
5063 llvm_unreachable("No such linkage");
5064}
5065
5067 return getTriple().supportsCOMDAT();
5068}
5069
5071 llvm::GlobalObject &GO) {
5072 if (!shouldBeInCOMDAT(*this, D))
5073 return;
5074 GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
5075}
5076
5077/// Pass IsTentative as true if you want to create a tentative definition.
5078void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
5079 bool IsTentative) {
5080 // OpenCL global variables of sampler type are translated to function calls,
5081 // therefore no need to be translated.
5082 QualType ASTTy = D->getType();
5083 if (getLangOpts().OpenCL && ASTTy->isSamplerT())
5084 return;
5085
5086 // If this is OpenMP device, check if it is legal to emit this global
5087 // normally.
5088 if (LangOpts.OpenMPIsTargetDevice && OpenMPRuntime &&
5089 OpenMPRuntime->emitTargetGlobalVariable(D))
5090 return;
5091
5092 llvm::TrackingVH<llvm::Constant> Init;
5093 bool NeedsGlobalCtor = false;
5094 // Whether the definition of the variable is available externally.
5095 // If yes, we shouldn't emit the GloablCtor and GlobalDtor for the variable
5096 // since this is the job for its original source.
5097 bool IsDefinitionAvailableExternally =
5099 bool NeedsGlobalDtor =
5100 !IsDefinitionAvailableExternally &&
5102
5103 const VarDecl *InitDecl;
5104 const Expr *InitExpr = D->getAnyInitializer(InitDecl);
5105
5106 std::optional<ConstantEmitter> emitter;
5107
5108 // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
5109 // as part of their declaration." Sema has already checked for
5110 // error cases, so we just need to set Init to UndefValue.
5111 bool IsCUDASharedVar =
5112 getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
5113 // Shadows of initialized device-side global variables are also left
5114 // undefined.
5115 // Managed Variables should be initialized on both host side and device side.
5116 bool IsCUDAShadowVar =
5117 !getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5118 (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
5119 D->hasAttr<CUDASharedAttr>());
5120 bool IsCUDADeviceShadowVar =
5121 getLangOpts().CUDAIsDevice && !D->hasAttr<HIPManagedAttr>() &&
5124 if (getLangOpts().CUDA &&
5125 (IsCUDASharedVar || IsCUDAShadowVar || IsCUDADeviceShadowVar))
5126 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5127 else if (D->hasAttr<LoaderUninitializedAttr>())
5128 Init = llvm::UndefValue::get(getTypes().ConvertTypeForMem(ASTTy));
5129 else if (!InitExpr) {
5130 // This is a tentative definition; tentative definitions are
5131 // implicitly initialized with { 0 }.
5132 //
5133 // Note that tentative definitions are only emitted at the end of
5134 // a translation unit, so they should never have incomplete
5135 // type. In addition, EmitTentativeDefinition makes sure that we
5136 // never attempt to emit a tentative definition if a real one
5137 // exists. A use may still exists, however, so we still may need
5138 // to do a RAUW.
5139 assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
5140 Init = EmitNullConstant(D->getType());
5141 } else {
5142 initializedGlobalDecl = GlobalDecl(D);
5143 emitter.emplace(*this);
5144 llvm::Constant *Initializer = emitter->tryEmitForInitializer(*InitDecl);
5145 if (!Initializer) {
5146 QualType T = InitExpr->getType();
5147 if (D->getType()->isReferenceType())
5148 T = D->getType();
5149
5150 if (getLangOpts().CPlusPlus) {
5151 if (InitDecl->hasFlexibleArrayInit(getContext()))
5152 ErrorUnsupported(D, "flexible array initializer");
5153 Init = EmitNullConstant(T);
5154
5155 if (!IsDefinitionAvailableExternally)
5156 NeedsGlobalCtor = true;
5157 } else {
5158 ErrorUnsupported(D, "static initializer");
5159 Init = llvm::UndefValue::get(getTypes().ConvertType(T));
5160 }
5161 } else {
5162 Init = Initializer;
5163 // We don't need an initializer, so remove the entry for the delayed
5164 // initializer position (just in case this entry was delayed) if we
5165 // also don't need to register a destructor.
5166 if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
5167 DelayedCXXInitPosition.erase(D);
5168
5169#ifndef NDEBUG
5170 CharUnits VarSize = getContext().getTypeSizeInChars(ASTTy) +
5173 getDataLayout().getTypeAllocSize(Init->getType()));
5174 assert(VarSize == CstSize && "Emitted constant has unexpected size");
5175#endif
5176 }
5177 }
5178
5179 llvm::Type* InitType = Init->getType();
5180 llvm::Constant *Entry =
5181 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
5182
5183 // Strip off pointer casts if we got them.
5184 Entry = Entry->stripPointerCasts();
5185
5186 // Entry is now either a Function or GlobalVariable.
5187 auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
5188
5189 // We have a definition after a declaration with the wrong type.
5190 // We must make a new GlobalVariable* and update everything that used OldGV
5191 // (a declaration or tentative definition) with the new GlobalVariable*
5192 // (which will be a definition).
5193 //
5194 // This happens if there is a prototype for a global (e.g.
5195 // "extern int x[];") and then a definition of a different type (e.g.
5196 // "int x[10];"). This also happens when an initializer has a different type
5197 // from the type of the global (this happens with unions).
5198 if (!GV || GV->getValueType() != InitType ||
5199 GV->getType()->getAddressSpace() !=
5200 getContext().getTargetAddressSpace(GetGlobalVarAddressSpace(D))) {
5201
5202 // Move the old entry aside so that we'll create a new one.
5203 Entry->setName(StringRef());
5204
5205 // Make a new global with the correct type, this is now guaranteed to work.
5206 GV = cast<llvm::GlobalVariable>(
5207 GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative))
5208 ->stripPointerCasts());
5209
5210 // Replace all uses of the old global with the new global
5211 llvm::Constant *NewPtrForOldDecl =
5212 llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV,
5213 Entry->getType());
5214 Entry->replaceAllUsesWith(NewPtrForOldDecl);
5215
5216 // Erase the old global, since it is no longer used.
5217 cast<llvm::GlobalValue>(Entry)->eraseFromParent();
5218 }
5219
5221
5222 if (D->hasAttr<AnnotateAttr>())
5223 AddGlobalAnnotations(D, GV);
5224
5225 // Set the llvm linkage type as appropriate.
5226 llvm::GlobalValue::LinkageTypes Linkage = getLLVMLinkageVarDefinition(D);
5227
5228 // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
5229 // the device. [...]"
5230 // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
5231 // __device__, declares a variable that: [...]
5232 // Is accessible from all the threads within the grid and from the host
5233 // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
5234 // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
5235 if (LangOpts.CUDA) {
5236 if (LangOpts.CUDAIsDevice) {
5237 if (Linkage != llvm::GlobalValue::InternalLinkage &&
5238 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
5241 GV->setExternallyInitialized(true);
5242 } else {
5244 }
5246 }
5247
5248 GV->setInitializer(Init);
5249 if (emitter)
5250 emitter->finalize(GV);
5251
5252 // If it is safe to mark the global 'constant', do so now.
5253 GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
5254 D->getType().isConstantStorage(getContext(), true, true));
5255
5256 // If it is in a read-only section, mark it 'constant'.
5257 if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
5258 const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
5259 if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
5260 GV->setConstant(true);
5261 }
5262
5263 CharUnits AlignVal = getContext().getDeclAlign(D);
5264 // Check for alignment specifed in an 'omp allocate' directive.
5265 if (std::optional<CharUnits> AlignValFromAllocate =
5267 AlignVal = *AlignValFromAllocate;
5268 GV->setAlignment(AlignVal.getAsAlign());
5269
5270 // On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
5271 // function is only defined alongside the variable, not also alongside
5272 // callers. Normally, all accesses to a thread_local go through the
5273 // thread-wrapper in order to ensure initialization has occurred, underlying
5274 // variable will never be used other than the thread-wrapper, so it can be
5275 // converted to internal linkage.
5276 //
5277 // However, if the variable has the 'constinit' attribute, it _can_ be
5278 // referenced directly, without calling the thread-wrapper, so the linkage
5279 // must not be changed.
5280 //
5281 // Additionally, if the variable isn't plain external linkage, e.g. if it's
5282 // weak or linkonce, the de-duplication semantics are important to preserve,
5283 // so we don't change the linkage.
5284 if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
5285 Linkage == llvm::GlobalValue::ExternalLinkage &&
5286 Context.getTargetInfo().getTriple().isOSDarwin() &&
5287 !D->hasAttr<ConstInitAttr>())
5288 Linkage = llvm::GlobalValue::InternalLinkage;
5289
5290 GV->setLinkage(Linkage);
5291 if (D->hasAttr<DLLImportAttr>())
5292 GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
5293 else if (D->hasAttr<DLLExportAttr>())
5294 GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
5295 else
5296 GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
5297
5298 if (Linkage == llvm::GlobalVariable::CommonLinkage) {
5299 // common vars aren't constant even if declared const.
5300 GV->setConstant(false);
5301