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