Thanks to visit codestin.com
Credit goes to clang.llvm.org

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