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