/src/llvm-project/clang/lib/CodeGen/CGDeclCXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===// |
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 contains code dealing with code generation of C++ declarations |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "CGCXXABI.h" |
14 | | #include "CGHLSLRuntime.h" |
15 | | #include "CGObjCRuntime.h" |
16 | | #include "CGOpenMPRuntime.h" |
17 | | #include "CodeGenFunction.h" |
18 | | #include "TargetInfo.h" |
19 | | #include "clang/AST/Attr.h" |
20 | | #include "clang/Basic/LangOptions.h" |
21 | | #include "llvm/ADT/StringExtras.h" |
22 | | #include "llvm/IR/Intrinsics.h" |
23 | | #include "llvm/IR/MDBuilder.h" |
24 | | #include "llvm/Support/Path.h" |
25 | | |
26 | | using namespace clang; |
27 | | using namespace CodeGen; |
28 | | |
29 | | static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, |
30 | 0 | ConstantAddress DeclPtr) { |
31 | 0 | assert( |
32 | 0 | (D.hasGlobalStorage() || |
33 | 0 | (D.hasLocalStorage() && CGF.getContext().getLangOpts().OpenCLCPlusPlus)) && |
34 | 0 | "VarDecl must have global or local (in the case of OpenCL) storage!"); |
35 | 0 | assert(!D.getType()->isReferenceType() && |
36 | 0 | "Should not call EmitDeclInit on a reference!"); |
37 | | |
38 | 0 | QualType type = D.getType(); |
39 | 0 | LValue lv = CGF.MakeAddrLValue(DeclPtr, type); |
40 | |
|
41 | 0 | const Expr *Init = D.getInit(); |
42 | 0 | switch (CGF.getEvaluationKind(type)) { |
43 | 0 | case TEK_Scalar: { |
44 | 0 | CodeGenModule &CGM = CGF.CGM; |
45 | 0 | if (lv.isObjCStrong()) |
46 | 0 | CGM.getObjCRuntime().EmitObjCGlobalAssign(CGF, CGF.EmitScalarExpr(Init), |
47 | 0 | DeclPtr, D.getTLSKind()); |
48 | 0 | else if (lv.isObjCWeak()) |
49 | 0 | CGM.getObjCRuntime().EmitObjCWeakAssign(CGF, CGF.EmitScalarExpr(Init), |
50 | 0 | DeclPtr); |
51 | 0 | else |
52 | 0 | CGF.EmitScalarInit(Init, &D, lv, false); |
53 | 0 | return; |
54 | 0 | } |
55 | 0 | case TEK_Complex: |
56 | 0 | CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true); |
57 | 0 | return; |
58 | 0 | case TEK_Aggregate: |
59 | 0 | CGF.EmitAggExpr(Init, |
60 | 0 | AggValueSlot::forLValue(lv, CGF, AggValueSlot::IsDestructed, |
61 | 0 | AggValueSlot::DoesNotNeedGCBarriers, |
62 | 0 | AggValueSlot::IsNotAliased, |
63 | 0 | AggValueSlot::DoesNotOverlap)); |
64 | 0 | return; |
65 | 0 | } |
66 | 0 | llvm_unreachable("bad evaluation kind"); |
67 | 0 | } |
68 | | |
69 | | /// Emit code to cause the destruction of the given variable with |
70 | | /// static storage duration. |
71 | | static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, |
72 | 0 | ConstantAddress Addr) { |
73 | | // Honor __attribute__((no_destroy)) and bail instead of attempting |
74 | | // to emit a reference to a possibly nonexistent destructor, which |
75 | | // in turn can cause a crash. This will result in a global constructor |
76 | | // that isn't balanced out by a destructor call as intended by the |
77 | | // attribute. This also checks for -fno-c++-static-destructors and |
78 | | // bails even if the attribute is not present. |
79 | 0 | QualType::DestructionKind DtorKind = D.needsDestruction(CGF.getContext()); |
80 | | |
81 | | // FIXME: __attribute__((cleanup)) ? |
82 | |
|
83 | 0 | switch (DtorKind) { |
84 | 0 | case QualType::DK_none: |
85 | 0 | return; |
86 | | |
87 | 0 | case QualType::DK_cxx_destructor: |
88 | 0 | break; |
89 | | |
90 | 0 | case QualType::DK_objc_strong_lifetime: |
91 | 0 | case QualType::DK_objc_weak_lifetime: |
92 | 0 | case QualType::DK_nontrivial_c_struct: |
93 | | // We don't care about releasing objects during process teardown. |
94 | 0 | assert(!D.getTLSKind() && "should have rejected this"); |
95 | 0 | return; |
96 | 0 | } |
97 | | |
98 | 0 | llvm::FunctionCallee Func; |
99 | 0 | llvm::Constant *Argument; |
100 | |
|
101 | 0 | CodeGenModule &CGM = CGF.CGM; |
102 | 0 | QualType Type = D.getType(); |
103 | | |
104 | | // Special-case non-array C++ destructors, if they have the right signature. |
105 | | // Under some ABIs, destructors return this instead of void, and cannot be |
106 | | // passed directly to __cxa_atexit if the target does not allow this |
107 | | // mismatch. |
108 | 0 | const CXXRecordDecl *Record = Type->getAsCXXRecordDecl(); |
109 | 0 | bool CanRegisterDestructor = |
110 | 0 | Record && (!CGM.getCXXABI().HasThisReturn( |
111 | 0 | GlobalDecl(Record->getDestructor(), Dtor_Complete)) || |
112 | 0 | CGM.getCXXABI().canCallMismatchedFunctionType()); |
113 | | // If __cxa_atexit is disabled via a flag, a different helper function is |
114 | | // generated elsewhere which uses atexit instead, and it takes the destructor |
115 | | // directly. |
116 | 0 | bool UsingExternalHelper = !CGM.getCodeGenOpts().CXAAtExit; |
117 | 0 | if (Record && (CanRegisterDestructor || UsingExternalHelper)) { |
118 | 0 | assert(!Record->hasTrivialDestructor()); |
119 | 0 | CXXDestructorDecl *Dtor = Record->getDestructor(); |
120 | |
|
121 | 0 | Func = CGM.getAddrAndTypeOfCXXStructor(GlobalDecl(Dtor, Dtor_Complete)); |
122 | 0 | if (CGF.getContext().getLangOpts().OpenCL) { |
123 | 0 | auto DestAS = |
124 | 0 | CGM.getTargetCodeGenInfo().getAddrSpaceOfCxaAtexitPtrParam(); |
125 | 0 | auto DestTy = llvm::PointerType::get( |
126 | 0 | CGM.getLLVMContext(), CGM.getContext().getTargetAddressSpace(DestAS)); |
127 | 0 | auto SrcAS = D.getType().getQualifiers().getAddressSpace(); |
128 | 0 | if (DestAS == SrcAS) |
129 | 0 | Argument = Addr.getPointer(); |
130 | 0 | else |
131 | | // FIXME: On addr space mismatch we are passing NULL. The generation |
132 | | // of the global destructor function should be adjusted accordingly. |
133 | 0 | Argument = llvm::ConstantPointerNull::get(DestTy); |
134 | 0 | } else { |
135 | 0 | Argument = Addr.getPointer(); |
136 | 0 | } |
137 | | // Otherwise, the standard logic requires a helper function. |
138 | 0 | } else { |
139 | 0 | Addr = Addr.withElementType(CGF.ConvertTypeForMem(Type)); |
140 | 0 | Func = CodeGenFunction(CGM) |
141 | 0 | .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind), |
142 | 0 | CGF.needsEHCleanup(DtorKind), &D); |
143 | 0 | Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy); |
144 | 0 | } |
145 | | |
146 | 0 | CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument); |
147 | 0 | } |
148 | | |
149 | | /// Emit code to cause the variable at the given address to be considered as |
150 | | /// constant from this point onwards. |
151 | | static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, |
152 | 0 | llvm::Constant *Addr) { |
153 | 0 | return CGF.EmitInvariantStart( |
154 | 0 | Addr, CGF.getContext().getTypeSizeInChars(D.getType())); |
155 | 0 | } |
156 | | |
157 | 0 | void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) { |
158 | | // Do not emit the intrinsic if we're not optimizing. |
159 | 0 | if (!CGM.getCodeGenOpts().OptimizationLevel) |
160 | 0 | return; |
161 | | |
162 | | // Grab the llvm.invariant.start intrinsic. |
163 | 0 | llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start; |
164 | | // Overloaded address space type. |
165 | 0 | llvm::Type *ObjectPtr[1] = {Int8PtrTy}; |
166 | 0 | llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr); |
167 | | |
168 | | // Emit a call with the size in bytes of the object. |
169 | 0 | uint64_t Width = Size.getQuantity(); |
170 | 0 | llvm::Value *Args[2] = {llvm::ConstantInt::getSigned(Int64Ty, Width), Addr}; |
171 | 0 | Builder.CreateCall(InvariantStart, Args); |
172 | 0 | } |
173 | | |
174 | | void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, |
175 | | llvm::GlobalVariable *GV, |
176 | 0 | bool PerformInit) { |
177 | |
|
178 | 0 | const Expr *Init = D.getInit(); |
179 | 0 | QualType T = D.getType(); |
180 | | |
181 | | // The address space of a static local variable (DeclPtr) may be different |
182 | | // from the address space of the "this" argument of the constructor. In that |
183 | | // case, we need an addrspacecast before calling the constructor. |
184 | | // |
185 | | // struct StructWithCtor { |
186 | | // __device__ StructWithCtor() {...} |
187 | | // }; |
188 | | // __device__ void foo() { |
189 | | // __shared__ StructWithCtor s; |
190 | | // ... |
191 | | // } |
192 | | // |
193 | | // For example, in the above CUDA code, the static local variable s has a |
194 | | // "shared" address space qualifier, but the constructor of StructWithCtor |
195 | | // expects "this" in the "generic" address space. |
196 | 0 | unsigned ExpectedAddrSpace = getTypes().getTargetAddressSpace(T); |
197 | 0 | unsigned ActualAddrSpace = GV->getAddressSpace(); |
198 | 0 | llvm::Constant *DeclPtr = GV; |
199 | 0 | if (ActualAddrSpace != ExpectedAddrSpace) { |
200 | 0 | llvm::PointerType *PTy = |
201 | 0 | llvm::PointerType::get(getLLVMContext(), ExpectedAddrSpace); |
202 | 0 | DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy); |
203 | 0 | } |
204 | |
|
205 | 0 | ConstantAddress DeclAddr( |
206 | 0 | DeclPtr, GV->getValueType(), getContext().getDeclAlign(&D)); |
207 | |
|
208 | 0 | if (!T->isReferenceType()) { |
209 | 0 | if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd && |
210 | 0 | D.hasAttr<OMPThreadPrivateDeclAttr>()) { |
211 | 0 | (void)CGM.getOpenMPRuntime().emitThreadPrivateVarDefinition( |
212 | 0 | &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(), |
213 | 0 | PerformInit, this); |
214 | 0 | } |
215 | 0 | bool NeedsDtor = |
216 | 0 | D.needsDestruction(getContext()) == QualType::DK_cxx_destructor; |
217 | 0 | if (PerformInit) |
218 | 0 | EmitDeclInit(*this, D, DeclAddr); |
219 | 0 | if (D.getType().isConstantStorage(getContext(), true, !NeedsDtor)) |
220 | 0 | EmitDeclInvariant(*this, D, DeclPtr); |
221 | 0 | else |
222 | 0 | EmitDeclDestroy(*this, D, DeclAddr); |
223 | 0 | return; |
224 | 0 | } |
225 | | |
226 | 0 | assert(PerformInit && "cannot have constant initializer which needs " |
227 | 0 | "destruction for reference"); |
228 | 0 | RValue RV = EmitReferenceBindingToExpr(Init); |
229 | 0 | EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T); |
230 | 0 | } |
231 | | |
232 | | /// Create a stub function, suitable for being passed to atexit, |
233 | | /// which passes the given address to the given destructor function. |
234 | | llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD, |
235 | | llvm::FunctionCallee dtor, |
236 | 0 | llvm::Constant *addr) { |
237 | | // Get the destructor function type, void(*)(void). |
238 | 0 | llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false); |
239 | 0 | SmallString<256> FnName; |
240 | 0 | { |
241 | 0 | llvm::raw_svector_ostream Out(FnName); |
242 | 0 | CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD, Out); |
243 | 0 | } |
244 | |
|
245 | 0 | const CGFunctionInfo &FI = CGM.getTypes().arrangeNullaryFunction(); |
246 | 0 | llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction( |
247 | 0 | ty, FnName.str(), FI, VD.getLocation()); |
248 | |
|
249 | 0 | CodeGenFunction CGF(CGM); |
250 | |
|
251 | 0 | CGF.StartFunction(GlobalDecl(&VD, DynamicInitKind::AtExit), |
252 | 0 | CGM.getContext().VoidTy, fn, FI, FunctionArgList(), |
253 | 0 | VD.getLocation(), VD.getInit()->getExprLoc()); |
254 | | // Emit an artificial location for this function. |
255 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(CGF); |
256 | |
|
257 | 0 | llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr); |
258 | | |
259 | | // Make sure the call and the callee agree on calling convention. |
260 | 0 | if (auto *dtorFn = dyn_cast<llvm::Function>( |
261 | 0 | dtor.getCallee()->stripPointerCastsAndAliases())) |
262 | 0 | call->setCallingConv(dtorFn->getCallingConv()); |
263 | |
|
264 | 0 | CGF.FinishFunction(); |
265 | |
|
266 | 0 | return fn; |
267 | 0 | } |
268 | | |
269 | | /// Create a stub function, suitable for being passed to __pt_atexit_np, |
270 | | /// which passes the given address to the given destructor function. |
271 | | llvm::Function *CodeGenFunction::createTLSAtExitStub( |
272 | | const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr, |
273 | 0 | llvm::FunctionCallee &AtExit) { |
274 | 0 | SmallString<256> FnName; |
275 | 0 | { |
276 | 0 | llvm::raw_svector_ostream Out(FnName); |
277 | 0 | CGM.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&D, Out); |
278 | 0 | } |
279 | |
|
280 | 0 | const CGFunctionInfo &FI = CGM.getTypes().arrangeLLVMFunctionInfo( |
281 | 0 | getContext().IntTy, FnInfoOpts::None, {getContext().IntTy}, |
282 | 0 | FunctionType::ExtInfo(), {}, RequiredArgs::All); |
283 | | |
284 | | // Get the stub function type, int(*)(int,...). |
285 | 0 | llvm::FunctionType *StubTy = |
286 | 0 | llvm::FunctionType::get(CGM.IntTy, {CGM.IntTy}, true); |
287 | |
|
288 | 0 | llvm::Function *DtorStub = CGM.CreateGlobalInitOrCleanUpFunction( |
289 | 0 | StubTy, FnName.str(), FI, D.getLocation()); |
290 | |
|
291 | 0 | CodeGenFunction CGF(CGM); |
292 | |
|
293 | 0 | FunctionArgList Args; |
294 | 0 | ImplicitParamDecl IPD(CGM.getContext(), CGM.getContext().IntTy, |
295 | 0 | ImplicitParamKind::Other); |
296 | 0 | Args.push_back(&IPD); |
297 | 0 | QualType ResTy = CGM.getContext().IntTy; |
298 | |
|
299 | 0 | CGF.StartFunction(GlobalDecl(&D, DynamicInitKind::AtExit), ResTy, DtorStub, |
300 | 0 | FI, Args, D.getLocation(), D.getInit()->getExprLoc()); |
301 | | |
302 | | // Emit an artificial location for this function. |
303 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(CGF); |
304 | |
|
305 | 0 | llvm::CallInst *call = CGF.Builder.CreateCall(Dtor, Addr); |
306 | | |
307 | | // Make sure the call and the callee agree on calling convention. |
308 | 0 | if (auto *DtorFn = dyn_cast<llvm::Function>( |
309 | 0 | Dtor.getCallee()->stripPointerCastsAndAliases())) |
310 | 0 | call->setCallingConv(DtorFn->getCallingConv()); |
311 | | |
312 | | // Return 0 from function |
313 | 0 | CGF.Builder.CreateStore(llvm::Constant::getNullValue(CGM.IntTy), |
314 | 0 | CGF.ReturnValue); |
315 | |
|
316 | 0 | CGF.FinishFunction(); |
317 | |
|
318 | 0 | return DtorStub; |
319 | 0 | } |
320 | | |
321 | | /// Register a global destructor using the C atexit runtime function. |
322 | | void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl &VD, |
323 | | llvm::FunctionCallee dtor, |
324 | 0 | llvm::Constant *addr) { |
325 | | // Create a function which calls the destructor. |
326 | 0 | llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr); |
327 | 0 | registerGlobalDtorWithAtExit(dtorStub); |
328 | 0 | } |
329 | | |
330 | | /// Register a global destructor using the LLVM 'llvm.global_dtors' global. |
331 | | void CodeGenFunction::registerGlobalDtorWithLLVM(const VarDecl &VD, |
332 | | llvm::FunctionCallee Dtor, |
333 | 0 | llvm::Constant *Addr) { |
334 | | // Create a function which calls the destructor. |
335 | 0 | llvm::Function *dtorStub = createAtExitStub(VD, Dtor, Addr); |
336 | 0 | CGM.AddGlobalDtor(dtorStub); |
337 | 0 | } |
338 | | |
339 | 0 | void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) { |
340 | | // extern "C" int atexit(void (*f)(void)); |
341 | 0 | assert(dtorStub->getType() == |
342 | 0 | llvm::PointerType::get( |
343 | 0 | llvm::FunctionType::get(CGM.VoidTy, false), |
344 | 0 | dtorStub->getType()->getPointerAddressSpace()) && |
345 | 0 | "Argument to atexit has a wrong type."); |
346 | | |
347 | 0 | llvm::FunctionType *atexitTy = |
348 | 0 | llvm::FunctionType::get(IntTy, dtorStub->getType(), false); |
349 | |
|
350 | 0 | llvm::FunctionCallee atexit = |
351 | 0 | CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(), |
352 | 0 | /*Local=*/true); |
353 | 0 | if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee())) |
354 | 0 | atexitFn->setDoesNotThrow(); |
355 | |
|
356 | 0 | EmitNounwindRuntimeCall(atexit, dtorStub); |
357 | 0 | } |
358 | | |
359 | | llvm::Value * |
360 | 0 | CodeGenFunction::unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub) { |
361 | | // The unatexit subroutine unregisters __dtor functions that were previously |
362 | | // registered by the atexit subroutine. If the referenced function is found, |
363 | | // it is removed from the list of functions that are called at normal program |
364 | | // termination and the unatexit returns a value of 0, otherwise a non-zero |
365 | | // value is returned. |
366 | | // |
367 | | // extern "C" int unatexit(void (*f)(void)); |
368 | 0 | assert(dtorStub->getType() == |
369 | 0 | llvm::PointerType::get( |
370 | 0 | llvm::FunctionType::get(CGM.VoidTy, false), |
371 | 0 | dtorStub->getType()->getPointerAddressSpace()) && |
372 | 0 | "Argument to unatexit has a wrong type."); |
373 | | |
374 | 0 | llvm::FunctionType *unatexitTy = |
375 | 0 | llvm::FunctionType::get(IntTy, {dtorStub->getType()}, /*isVarArg=*/false); |
376 | |
|
377 | 0 | llvm::FunctionCallee unatexit = |
378 | 0 | CGM.CreateRuntimeFunction(unatexitTy, "unatexit", llvm::AttributeList()); |
379 | |
|
380 | 0 | cast<llvm::Function>(unatexit.getCallee())->setDoesNotThrow(); |
381 | |
|
382 | 0 | return EmitNounwindRuntimeCall(unatexit, dtorStub); |
383 | 0 | } |
384 | | |
385 | | void CodeGenFunction::EmitCXXGuardedInit(const VarDecl &D, |
386 | | llvm::GlobalVariable *DeclPtr, |
387 | 0 | bool PerformInit) { |
388 | | // If we've been asked to forbid guard variables, emit an error now. |
389 | | // This diagnostic is hard-coded for Darwin's use case; we can find |
390 | | // better phrasing if someone else needs it. |
391 | 0 | if (CGM.getCodeGenOpts().ForbidGuardVariables) |
392 | 0 | CGM.Error(D.getLocation(), |
393 | 0 | "this initialization requires a guard variable, which " |
394 | 0 | "the kernel does not support"); |
395 | |
|
396 | 0 | CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit); |
397 | 0 | } |
398 | | |
399 | | void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, |
400 | | llvm::BasicBlock *InitBlock, |
401 | | llvm::BasicBlock *NoInitBlock, |
402 | | GuardKind Kind, |
403 | 0 | const VarDecl *D) { |
404 | 0 | assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable"); |
405 | | |
406 | | // A guess at how many times we will enter the initialization of a |
407 | | // variable, depending on the kind of variable. |
408 | 0 | static const uint64_t InitsPerTLSVar = 1024; |
409 | 0 | static const uint64_t InitsPerLocalVar = 1024 * 1024; |
410 | |
|
411 | 0 | llvm::MDNode *Weights; |
412 | 0 | if (Kind == GuardKind::VariableGuard && !D->isLocalVarDecl()) { |
413 | | // For non-local variables, don't apply any weighting for now. Due to our |
414 | | // use of COMDATs, we expect there to be at most one initialization of the |
415 | | // variable per DSO, but we have no way to know how many DSOs will try to |
416 | | // initialize the variable. |
417 | 0 | Weights = nullptr; |
418 | 0 | } else { |
419 | 0 | uint64_t NumInits; |
420 | | // FIXME: For the TLS case, collect and use profiling information to |
421 | | // determine a more accurate brach weight. |
422 | 0 | if (Kind == GuardKind::TlsGuard || D->getTLSKind()) |
423 | 0 | NumInits = InitsPerTLSVar; |
424 | 0 | else |
425 | 0 | NumInits = InitsPerLocalVar; |
426 | | |
427 | | // The probability of us entering the initializer is |
428 | | // 1 / (total number of times we attempt to initialize the variable). |
429 | 0 | llvm::MDBuilder MDHelper(CGM.getLLVMContext()); |
430 | 0 | Weights = MDHelper.createBranchWeights(1, NumInits - 1); |
431 | 0 | } |
432 | |
|
433 | 0 | Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights); |
434 | 0 | } |
435 | | |
436 | | llvm::Function *CodeGenModule::CreateGlobalInitOrCleanUpFunction( |
437 | | llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI, |
438 | 0 | SourceLocation Loc, bool TLS, llvm::GlobalVariable::LinkageTypes Linkage) { |
439 | 0 | llvm::Function *Fn = llvm::Function::Create(FTy, Linkage, Name, &getModule()); |
440 | |
|
441 | 0 | if (!getLangOpts().AppleKext && !TLS) { |
442 | | // Set the section if needed. |
443 | 0 | if (const char *Section = getTarget().getStaticInitSectionSpecifier()) |
444 | 0 | Fn->setSection(Section); |
445 | 0 | } |
446 | |
|
447 | 0 | if (Linkage == llvm::GlobalVariable::InternalLinkage) |
448 | 0 | SetInternalFunctionAttributes(GlobalDecl(), Fn, FI); |
449 | |
|
450 | 0 | Fn->setCallingConv(getRuntimeCC()); |
451 | |
|
452 | 0 | if (!getLangOpts().Exceptions) |
453 | 0 | Fn->setDoesNotThrow(); |
454 | |
|
455 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::Address) && |
456 | 0 | !isInNoSanitizeList(SanitizerKind::Address, Fn, Loc)) |
457 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
458 | |
|
459 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) && |
460 | 0 | !isInNoSanitizeList(SanitizerKind::KernelAddress, Fn, Loc)) |
461 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeAddress); |
462 | |
|
463 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) && |
464 | 0 | !isInNoSanitizeList(SanitizerKind::HWAddress, Fn, Loc)) |
465 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress); |
466 | |
|
467 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) && |
468 | 0 | !isInNoSanitizeList(SanitizerKind::KernelHWAddress, Fn, Loc)) |
469 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress); |
470 | |
|
471 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::MemtagStack) && |
472 | 0 | !isInNoSanitizeList(SanitizerKind::MemtagStack, Fn, Loc)) |
473 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeMemTag); |
474 | |
|
475 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::Thread) && |
476 | 0 | !isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc)) |
477 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeThread); |
478 | |
|
479 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::Memory) && |
480 | 0 | !isInNoSanitizeList(SanitizerKind::Memory, Fn, Loc)) |
481 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
482 | |
|
483 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) && |
484 | 0 | !isInNoSanitizeList(SanitizerKind::KernelMemory, Fn, Loc)) |
485 | 0 | Fn->addFnAttr(llvm::Attribute::SanitizeMemory); |
486 | |
|
487 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) && |
488 | 0 | !isInNoSanitizeList(SanitizerKind::SafeStack, Fn, Loc)) |
489 | 0 | Fn->addFnAttr(llvm::Attribute::SafeStack); |
490 | |
|
491 | 0 | if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) && |
492 | 0 | !isInNoSanitizeList(SanitizerKind::ShadowCallStack, Fn, Loc)) |
493 | 0 | Fn->addFnAttr(llvm::Attribute::ShadowCallStack); |
494 | |
|
495 | 0 | return Fn; |
496 | 0 | } |
497 | | |
498 | | /// Create a global pointer to a function that will initialize a global |
499 | | /// variable. The user has requested that this pointer be emitted in a specific |
500 | | /// section. |
501 | | void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D, |
502 | | llvm::GlobalVariable *GV, |
503 | | llvm::Function *InitFunc, |
504 | 0 | InitSegAttr *ISA) { |
505 | 0 | llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable( |
506 | 0 | TheModule, InitFunc->getType(), /*isConstant=*/true, |
507 | 0 | llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr"); |
508 | 0 | PtrArray->setSection(ISA->getSection()); |
509 | 0 | addUsedGlobal(PtrArray); |
510 | | |
511 | | // If the GV is already in a comdat group, then we have to join it. |
512 | 0 | if (llvm::Comdat *C = GV->getComdat()) |
513 | 0 | PtrArray->setComdat(C); |
514 | 0 | } |
515 | | |
516 | | void |
517 | | CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D, |
518 | | llvm::GlobalVariable *Addr, |
519 | 0 | bool PerformInit) { |
520 | | |
521 | | // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__, |
522 | | // __constant__ and __shared__ variables defined in namespace scope, |
523 | | // that are of class type, cannot have a non-empty constructor. All |
524 | | // the checks have been done in Sema by now. Whatever initializers |
525 | | // are allowed are empty and we just need to ignore them here. |
526 | 0 | if (getLangOpts().CUDAIsDevice && !getLangOpts().GPUAllowDeviceInit && |
527 | 0 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || |
528 | 0 | D->hasAttr<CUDASharedAttr>())) |
529 | 0 | return; |
530 | | |
531 | | // Check if we've already initialized this decl. |
532 | 0 | auto I = DelayedCXXInitPosition.find(D); |
533 | 0 | if (I != DelayedCXXInitPosition.end() && I->second == ~0U) |
534 | 0 | return; |
535 | | |
536 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
537 | 0 | SmallString<256> FnName; |
538 | 0 | { |
539 | 0 | llvm::raw_svector_ostream Out(FnName); |
540 | 0 | getCXXABI().getMangleContext().mangleDynamicInitializer(D, Out); |
541 | 0 | } |
542 | | |
543 | | // Create a variable initialization function. |
544 | 0 | llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction( |
545 | 0 | FTy, FnName.str(), getTypes().arrangeNullaryFunction(), D->getLocation()); |
546 | |
|
547 | 0 | auto *ISA = D->getAttr<InitSegAttr>(); |
548 | 0 | CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn, D, Addr, |
549 | 0 | PerformInit); |
550 | |
|
551 | 0 | llvm::GlobalVariable *COMDATKey = |
552 | 0 | supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr; |
553 | |
|
554 | 0 | if (D->getTLSKind()) { |
555 | | // FIXME: Should we support init_priority for thread_local? |
556 | | // FIXME: We only need to register one __cxa_thread_atexit function for the |
557 | | // entire TU. |
558 | 0 | CXXThreadLocalInits.push_back(Fn); |
559 | 0 | CXXThreadLocalInitVars.push_back(D); |
560 | 0 | } else if (PerformInit && ISA) { |
561 | | // Contract with backend that "init_seg(compiler)" corresponds to priority |
562 | | // 200 and "init_seg(lib)" corresponds to priority 400. |
563 | 0 | int Priority = -1; |
564 | 0 | if (ISA->getSection() == ".CRT$XCC") |
565 | 0 | Priority = 200; |
566 | 0 | else if (ISA->getSection() == ".CRT$XCL") |
567 | 0 | Priority = 400; |
568 | |
|
569 | 0 | if (Priority != -1) |
570 | 0 | AddGlobalCtor(Fn, Priority, ~0U, COMDATKey); |
571 | 0 | else |
572 | 0 | EmitPointerToInitFunc(D, Addr, Fn, ISA); |
573 | 0 | } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) { |
574 | 0 | OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(), |
575 | 0 | PrioritizedCXXGlobalInits.size()); |
576 | 0 | PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn)); |
577 | 0 | } else if (isTemplateInstantiation(D->getTemplateSpecializationKind()) || |
578 | 0 | getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR || |
579 | 0 | D->hasAttr<SelectAnyAttr>()) { |
580 | | // C++ [basic.start.init]p2: |
581 | | // Definitions of explicitly specialized class template static data |
582 | | // members have ordered initialization. Other class template static data |
583 | | // members (i.e., implicitly or explicitly instantiated specializations) |
584 | | // have unordered initialization. |
585 | | // |
586 | | // As a consequence, we can put them into their own llvm.global_ctors entry. |
587 | | // |
588 | | // If the global is externally visible, put the initializer into a COMDAT |
589 | | // group with the global being initialized. On most platforms, this is a |
590 | | // minor startup time optimization. In the MS C++ ABI, there are no guard |
591 | | // variables, so this COMDAT key is required for correctness. |
592 | | // |
593 | | // SelectAny globals will be comdat-folded. Put the initializer into a |
594 | | // COMDAT group associated with the global, so the initializers get folded |
595 | | // too. |
596 | 0 | I = DelayedCXXInitPosition.find(D); |
597 | | // CXXGlobalInits.size() is the lex order number for the next deferred |
598 | | // VarDecl. Use it when the current VarDecl is non-deferred. Although this |
599 | | // lex order number is shared between current VarDecl and some following |
600 | | // VarDecls, their order of insertion into `llvm.global_ctors` is the same |
601 | | // as the lexing order and the following stable sort would preserve such |
602 | | // order. |
603 | 0 | unsigned LexOrder = |
604 | 0 | I == DelayedCXXInitPosition.end() ? CXXGlobalInits.size() : I->second; |
605 | 0 | AddGlobalCtor(Fn, 65535, LexOrder, COMDATKey); |
606 | 0 | if (COMDATKey && (getTriple().isOSBinFormatELF() || |
607 | 0 | getTarget().getCXXABI().isMicrosoft())) { |
608 | | // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in |
609 | | // llvm.used to prevent linker GC. |
610 | 0 | addUsedGlobal(COMDATKey); |
611 | 0 | } |
612 | | |
613 | | // If we used a COMDAT key for the global ctor, the init function can be |
614 | | // discarded if the global ctor entry is discarded. |
615 | | // FIXME: Do we need to restrict this to ELF and Wasm? |
616 | 0 | llvm::Comdat *C = Addr->getComdat(); |
617 | 0 | if (COMDATKey && C && |
618 | 0 | (getTarget().getTriple().isOSBinFormatELF() || |
619 | 0 | getTarget().getTriple().isOSBinFormatWasm())) { |
620 | 0 | Fn->setComdat(C); |
621 | 0 | } |
622 | 0 | } else { |
623 | 0 | I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash. |
624 | 0 | if (I == DelayedCXXInitPosition.end()) { |
625 | 0 | CXXGlobalInits.push_back(Fn); |
626 | 0 | } else if (I->second != ~0U) { |
627 | 0 | assert(I->second < CXXGlobalInits.size() && |
628 | 0 | CXXGlobalInits[I->second] == nullptr); |
629 | 0 | CXXGlobalInits[I->second] = Fn; |
630 | 0 | } |
631 | 0 | } |
632 | | |
633 | | // Remember that we already emitted the initializer for this global. |
634 | 0 | DelayedCXXInitPosition[D] = ~0U; |
635 | 0 | } |
636 | | |
637 | 0 | void CodeGenModule::EmitCXXThreadLocalInitFunc() { |
638 | 0 | getCXXABI().EmitThreadLocalInitFuncs( |
639 | 0 | *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars); |
640 | |
|
641 | 0 | CXXThreadLocalInits.clear(); |
642 | 0 | CXXThreadLocalInitVars.clear(); |
643 | 0 | CXXThreadLocals.clear(); |
644 | 0 | } |
645 | | |
646 | | /* Build the initializer for a C++20 module: |
647 | | This is arranged to be run only once regardless of how many times the module |
648 | | might be included transitively. This arranged by using a guard variable. |
649 | | |
650 | | If there are no initializers at all (and also no imported modules) we reduce |
651 | | this to an empty function (since the Itanium ABI requires that this function |
652 | | be available to a caller, which might be produced by a different |
653 | | implementation). |
654 | | |
655 | | First we call any initializers for imported modules. |
656 | | We then call initializers for the Global Module Fragment (if present) |
657 | | We then call initializers for the current module. |
658 | | We then call initializers for the Private Module Fragment (if present) |
659 | | */ |
660 | | |
661 | 0 | void CodeGenModule::EmitCXXModuleInitFunc(Module *Primary) { |
662 | 0 | assert(Primary->isInterfaceOrPartition() && |
663 | 0 | "The function should only be called for C++20 named module interface" |
664 | 0 | " or partition."); |
665 | | |
666 | 0 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
667 | 0 | CXXGlobalInits.pop_back(); |
668 | | |
669 | | // As noted above, we create the function, even if it is empty. |
670 | | // Module initializers for imported modules are emitted first. |
671 | | |
672 | | // Collect all the modules that we import |
673 | 0 | llvm::SmallSetVector<Module *, 8> AllImports; |
674 | | // Ones that we export |
675 | 0 | for (auto I : Primary->Exports) |
676 | 0 | AllImports.insert(I.getPointer()); |
677 | | // Ones that we only import. |
678 | 0 | for (Module *M : Primary->Imports) |
679 | 0 | AllImports.insert(M); |
680 | | // Ones that we import in the global module fragment or the private module |
681 | | // fragment. |
682 | 0 | for (Module *SubM : Primary->submodules()) { |
683 | 0 | assert((SubM->isGlobalModule() || SubM->isPrivateModule()) && |
684 | 0 | "The sub modules of C++20 module unit should only be global module " |
685 | 0 | "fragments or private module framents."); |
686 | 0 | assert(SubM->Exports.empty() && |
687 | 0 | "The global mdoule fragments and the private module fragments are " |
688 | 0 | "not allowed to export import modules."); |
689 | 0 | for (Module *M : SubM->Imports) |
690 | 0 | AllImports.insert(M); |
691 | 0 | } |
692 | |
|
693 | 0 | SmallVector<llvm::Function *, 8> ModuleInits; |
694 | 0 | for (Module *M : AllImports) { |
695 | | // No Itanium initializer in header like modules. |
696 | 0 | if (M->isHeaderLikeModule()) |
697 | 0 | continue; // TODO: warn of mixed use of module map modules and C++20? |
698 | | // We're allowed to skip the initialization if we are sure it doesn't |
699 | | // do any thing. |
700 | 0 | if (!M->isNamedModuleInterfaceHasInit()) |
701 | 0 | continue; |
702 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
703 | 0 | SmallString<256> FnName; |
704 | 0 | { |
705 | 0 | llvm::raw_svector_ostream Out(FnName); |
706 | 0 | cast<ItaniumMangleContext>(getCXXABI().getMangleContext()) |
707 | 0 | .mangleModuleInitializer(M, Out); |
708 | 0 | } |
709 | 0 | assert(!GetGlobalValue(FnName.str()) && |
710 | 0 | "We should only have one use of the initializer call"); |
711 | 0 | llvm::Function *Fn = llvm::Function::Create( |
712 | 0 | FTy, llvm::Function::ExternalLinkage, FnName.str(), &getModule()); |
713 | 0 | ModuleInits.push_back(Fn); |
714 | 0 | } |
715 | | |
716 | | // Add any initializers with specified priority; this uses the same approach |
717 | | // as EmitCXXGlobalInitFunc(). |
718 | 0 | if (!PrioritizedCXXGlobalInits.empty()) { |
719 | 0 | SmallVector<llvm::Function *, 8> LocalCXXGlobalInits; |
720 | 0 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
721 | 0 | PrioritizedCXXGlobalInits.end()); |
722 | 0 | for (SmallVectorImpl<GlobalInitData>::iterator |
723 | 0 | I = PrioritizedCXXGlobalInits.begin(), |
724 | 0 | E = PrioritizedCXXGlobalInits.end(); |
725 | 0 | I != E;) { |
726 | 0 | SmallVectorImpl<GlobalInitData>::iterator PrioE = |
727 | 0 | std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); |
728 | |
|
729 | 0 | for (; I < PrioE; ++I) |
730 | 0 | ModuleInits.push_back(I->second); |
731 | 0 | } |
732 | 0 | } |
733 | | |
734 | | // Now append the ones without specified priority. |
735 | 0 | for (auto *F : CXXGlobalInits) |
736 | 0 | ModuleInits.push_back(F); |
737 | |
|
738 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
739 | 0 | const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction(); |
740 | | |
741 | | // We now build the initializer for this module, which has a mangled name |
742 | | // as per the Itanium ABI . The action of the initializer is guarded so that |
743 | | // each init is run just once (even though a module might be imported |
744 | | // multiple times via nested use). |
745 | 0 | llvm::Function *Fn; |
746 | 0 | { |
747 | 0 | SmallString<256> InitFnName; |
748 | 0 | llvm::raw_svector_ostream Out(InitFnName); |
749 | 0 | cast<ItaniumMangleContext>(getCXXABI().getMangleContext()) |
750 | 0 | .mangleModuleInitializer(Primary, Out); |
751 | 0 | Fn = CreateGlobalInitOrCleanUpFunction( |
752 | 0 | FTy, llvm::Twine(InitFnName), FI, SourceLocation(), false, |
753 | 0 | llvm::GlobalVariable::ExternalLinkage); |
754 | | |
755 | | // If we have a completely empty initializer then we do not want to create |
756 | | // the guard variable. |
757 | 0 | ConstantAddress GuardAddr = ConstantAddress::invalid(); |
758 | 0 | if (!ModuleInits.empty()) { |
759 | | // Create the guard var. |
760 | 0 | llvm::GlobalVariable *Guard = new llvm::GlobalVariable( |
761 | 0 | getModule(), Int8Ty, /*isConstant=*/false, |
762 | 0 | llvm::GlobalVariable::InternalLinkage, |
763 | 0 | llvm::ConstantInt::get(Int8Ty, 0), InitFnName.str() + "__in_chrg"); |
764 | 0 | CharUnits GuardAlign = CharUnits::One(); |
765 | 0 | Guard->setAlignment(GuardAlign.getAsAlign()); |
766 | 0 | GuardAddr = ConstantAddress(Guard, Int8Ty, GuardAlign); |
767 | 0 | } |
768 | 0 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, ModuleInits, |
769 | 0 | GuardAddr); |
770 | 0 | } |
771 | | |
772 | | // We allow for the case that a module object is added to a linked binary |
773 | | // without a specific call to the the initializer. This also ensures that |
774 | | // implementation partition initializers are called when the partition |
775 | | // is not imported as an interface. |
776 | 0 | AddGlobalCtor(Fn); |
777 | | |
778 | | // See the comment in EmitCXXGlobalInitFunc about OpenCL global init |
779 | | // functions. |
780 | 0 | if (getLangOpts().OpenCL) { |
781 | 0 | GenKernelArgMetadata(Fn); |
782 | 0 | Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL); |
783 | 0 | } |
784 | |
|
785 | 0 | assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice || |
786 | 0 | getLangOpts().GPUAllowDeviceInit); |
787 | 0 | if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) { |
788 | 0 | Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL); |
789 | 0 | Fn->addFnAttr("device-init"); |
790 | 0 | } |
791 | | |
792 | | // We are done with the inits. |
793 | 0 | AllImports.clear(); |
794 | 0 | PrioritizedCXXGlobalInits.clear(); |
795 | 0 | CXXGlobalInits.clear(); |
796 | 0 | ModuleInits.clear(); |
797 | 0 | } |
798 | | |
799 | 0 | static SmallString<128> getTransformedFileName(llvm::Module &M) { |
800 | 0 | SmallString<128> FileName = llvm::sys::path::filename(M.getName()); |
801 | |
|
802 | 0 | if (FileName.empty()) |
803 | 0 | FileName = "<null>"; |
804 | |
|
805 | 0 | for (size_t i = 0; i < FileName.size(); ++i) { |
806 | | // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens |
807 | | // to be the set of C preprocessing numbers. |
808 | 0 | if (!isPreprocessingNumberBody(FileName[i])) |
809 | 0 | FileName[i] = '_'; |
810 | 0 | } |
811 | |
|
812 | 0 | return FileName; |
813 | 0 | } |
814 | | |
815 | 0 | static std::string getPrioritySuffix(unsigned int Priority) { |
816 | 0 | assert(Priority <= 65535 && "Priority should always be <= 65535."); |
817 | | |
818 | | // Compute the function suffix from priority. Prepend with zeroes to make |
819 | | // sure the function names are also ordered as priorities. |
820 | 0 | std::string PrioritySuffix = llvm::utostr(Priority); |
821 | 0 | PrioritySuffix = std::string(6 - PrioritySuffix.size(), '0') + PrioritySuffix; |
822 | |
|
823 | 0 | return PrioritySuffix; |
824 | 0 | } |
825 | | |
826 | | void |
827 | 0 | CodeGenModule::EmitCXXGlobalInitFunc() { |
828 | 0 | while (!CXXGlobalInits.empty() && !CXXGlobalInits.back()) |
829 | 0 | CXXGlobalInits.pop_back(); |
830 | | |
831 | | // When we import C++20 modules, we must run their initializers first. |
832 | 0 | SmallVector<llvm::Function *, 8> ModuleInits; |
833 | 0 | if (CXX20ModuleInits) |
834 | 0 | for (Module *M : ImportedModules) { |
835 | | // No Itanium initializer in header like modules. |
836 | 0 | if (M->isHeaderLikeModule()) |
837 | 0 | continue; |
838 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
839 | 0 | SmallString<256> FnName; |
840 | 0 | { |
841 | 0 | llvm::raw_svector_ostream Out(FnName); |
842 | 0 | cast<ItaniumMangleContext>(getCXXABI().getMangleContext()) |
843 | 0 | .mangleModuleInitializer(M, Out); |
844 | 0 | } |
845 | 0 | assert(!GetGlobalValue(FnName.str()) && |
846 | 0 | "We should only have one use of the initializer call"); |
847 | 0 | llvm::Function *Fn = llvm::Function::Create( |
848 | 0 | FTy, llvm::Function::ExternalLinkage, FnName.str(), &getModule()); |
849 | 0 | ModuleInits.push_back(Fn); |
850 | 0 | } |
851 | |
|
852 | 0 | if (ModuleInits.empty() && CXXGlobalInits.empty() && |
853 | 0 | PrioritizedCXXGlobalInits.empty()) |
854 | 0 | return; |
855 | | |
856 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
857 | 0 | const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction(); |
858 | | |
859 | | // Create our global prioritized initialization function. |
860 | 0 | if (!PrioritizedCXXGlobalInits.empty()) { |
861 | 0 | SmallVector<llvm::Function *, 8> LocalCXXGlobalInits; |
862 | 0 | llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(), |
863 | 0 | PrioritizedCXXGlobalInits.end()); |
864 | | // Iterate over "chunks" of ctors with same priority and emit each chunk |
865 | | // into separate function. Note - everything is sorted first by priority, |
866 | | // second - by lex order, so we emit ctor functions in proper order. |
867 | 0 | for (SmallVectorImpl<GlobalInitData >::iterator |
868 | 0 | I = PrioritizedCXXGlobalInits.begin(), |
869 | 0 | E = PrioritizedCXXGlobalInits.end(); I != E; ) { |
870 | 0 | SmallVectorImpl<GlobalInitData >::iterator |
871 | 0 | PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp()); |
872 | |
|
873 | 0 | LocalCXXGlobalInits.clear(); |
874 | |
|
875 | 0 | unsigned int Priority = I->first.priority; |
876 | 0 | llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction( |
877 | 0 | FTy, "_GLOBAL__I_" + getPrioritySuffix(Priority), FI); |
878 | | |
879 | | // Prepend the module inits to the highest priority set. |
880 | 0 | if (!ModuleInits.empty()) { |
881 | 0 | for (auto *F : ModuleInits) |
882 | 0 | LocalCXXGlobalInits.push_back(F); |
883 | 0 | ModuleInits.clear(); |
884 | 0 | } |
885 | |
|
886 | 0 | for (; I < PrioE; ++I) |
887 | 0 | LocalCXXGlobalInits.push_back(I->second); |
888 | |
|
889 | 0 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits); |
890 | 0 | AddGlobalCtor(Fn, Priority); |
891 | 0 | } |
892 | 0 | PrioritizedCXXGlobalInits.clear(); |
893 | 0 | } |
894 | |
|
895 | 0 | if (getCXXABI().useSinitAndSterm() && ModuleInits.empty() && |
896 | 0 | CXXGlobalInits.empty()) |
897 | 0 | return; |
898 | | |
899 | 0 | for (auto *F : CXXGlobalInits) |
900 | 0 | ModuleInits.push_back(F); |
901 | 0 | CXXGlobalInits.clear(); |
902 | | |
903 | | // Include the filename in the symbol name. Including "sub_" matches gcc |
904 | | // and makes sure these symbols appear lexicographically behind the symbols |
905 | | // with priority emitted above. Module implementation units behave the same |
906 | | // way as a non-modular TU with imports. |
907 | 0 | llvm::Function *Fn; |
908 | 0 | if (CXX20ModuleInits && getContext().getCurrentNamedModule() && |
909 | 0 | !getContext().getCurrentNamedModule()->isModuleImplementation()) { |
910 | 0 | SmallString<256> InitFnName; |
911 | 0 | llvm::raw_svector_ostream Out(InitFnName); |
912 | 0 | cast<ItaniumMangleContext>(getCXXABI().getMangleContext()) |
913 | 0 | .mangleModuleInitializer(getContext().getCurrentNamedModule(), Out); |
914 | 0 | Fn = CreateGlobalInitOrCleanUpFunction( |
915 | 0 | FTy, llvm::Twine(InitFnName), FI, SourceLocation(), false, |
916 | 0 | llvm::GlobalVariable::ExternalLinkage); |
917 | 0 | } else |
918 | 0 | Fn = CreateGlobalInitOrCleanUpFunction( |
919 | 0 | FTy, |
920 | 0 | llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())), |
921 | 0 | FI); |
922 | |
|
923 | 0 | CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, ModuleInits); |
924 | 0 | AddGlobalCtor(Fn); |
925 | | |
926 | | // In OpenCL global init functions must be converted to kernels in order to |
927 | | // be able to launch them from the host. |
928 | | // FIXME: Some more work might be needed to handle destructors correctly. |
929 | | // Current initialization function makes use of function pointers callbacks. |
930 | | // We can't support function pointers especially between host and device. |
931 | | // However it seems global destruction has little meaning without any |
932 | | // dynamic resource allocation on the device and program scope variables are |
933 | | // destroyed by the runtime when program is released. |
934 | 0 | if (getLangOpts().OpenCL) { |
935 | 0 | GenKernelArgMetadata(Fn); |
936 | 0 | Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL); |
937 | 0 | } |
938 | |
|
939 | 0 | assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice || |
940 | 0 | getLangOpts().GPUAllowDeviceInit); |
941 | 0 | if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) { |
942 | 0 | Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL); |
943 | 0 | Fn->addFnAttr("device-init"); |
944 | 0 | } |
945 | |
|
946 | 0 | ModuleInits.clear(); |
947 | 0 | } |
948 | | |
949 | 0 | void CodeGenModule::EmitCXXGlobalCleanUpFunc() { |
950 | 0 | if (CXXGlobalDtorsOrStermFinalizers.empty() && |
951 | 0 | PrioritizedCXXStermFinalizers.empty()) |
952 | 0 | return; |
953 | | |
954 | 0 | llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false); |
955 | 0 | const CGFunctionInfo &FI = getTypes().arrangeNullaryFunction(); |
956 | | |
957 | | // Create our global prioritized cleanup function. |
958 | 0 | if (!PrioritizedCXXStermFinalizers.empty()) { |
959 | 0 | SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8> LocalCXXStermFinalizers; |
960 | 0 | llvm::array_pod_sort(PrioritizedCXXStermFinalizers.begin(), |
961 | 0 | PrioritizedCXXStermFinalizers.end()); |
962 | | // Iterate over "chunks" of dtors with same priority and emit each chunk |
963 | | // into separate function. Note - everything is sorted first by priority, |
964 | | // second - by lex order, so we emit dtor functions in proper order. |
965 | 0 | for (SmallVectorImpl<StermFinalizerData>::iterator |
966 | 0 | I = PrioritizedCXXStermFinalizers.begin(), |
967 | 0 | E = PrioritizedCXXStermFinalizers.end(); |
968 | 0 | I != E;) { |
969 | 0 | SmallVectorImpl<StermFinalizerData>::iterator PrioE = |
970 | 0 | std::upper_bound(I + 1, E, *I, StermFinalizerPriorityCmp()); |
971 | |
|
972 | 0 | LocalCXXStermFinalizers.clear(); |
973 | |
|
974 | 0 | unsigned int Priority = I->first.priority; |
975 | 0 | llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction( |
976 | 0 | FTy, "_GLOBAL__a_" + getPrioritySuffix(Priority), FI); |
977 | |
|
978 | 0 | for (; I < PrioE; ++I) { |
979 | 0 | llvm::FunctionCallee DtorFn = I->second; |
980 | 0 | LocalCXXStermFinalizers.emplace_back(DtorFn.getFunctionType(), |
981 | 0 | DtorFn.getCallee(), nullptr); |
982 | 0 | } |
983 | |
|
984 | 0 | CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc( |
985 | 0 | Fn, LocalCXXStermFinalizers); |
986 | 0 | AddGlobalDtor(Fn, Priority); |
987 | 0 | } |
988 | 0 | PrioritizedCXXStermFinalizers.clear(); |
989 | 0 | } |
990 | |
|
991 | 0 | if (CXXGlobalDtorsOrStermFinalizers.empty()) |
992 | 0 | return; |
993 | | |
994 | | // Create our global cleanup function. |
995 | 0 | llvm::Function *Fn = |
996 | 0 | CreateGlobalInitOrCleanUpFunction(FTy, "_GLOBAL__D_a", FI); |
997 | |
|
998 | 0 | CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc( |
999 | 0 | Fn, CXXGlobalDtorsOrStermFinalizers); |
1000 | 0 | AddGlobalDtor(Fn); |
1001 | 0 | CXXGlobalDtorsOrStermFinalizers.clear(); |
1002 | 0 | } |
1003 | | |
1004 | | /// Emit the code necessary to initialize the given global variable. |
1005 | | void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, |
1006 | | const VarDecl *D, |
1007 | | llvm::GlobalVariable *Addr, |
1008 | 0 | bool PerformInit) { |
1009 | | // Check if we need to emit debug info for variable initializer. |
1010 | 0 | if (D->hasAttr<NoDebugAttr>()) |
1011 | 0 | DebugInfo = nullptr; // disable debug info indefinitely for this function |
1012 | |
|
1013 | 0 | CurEHLocation = D->getBeginLoc(); |
1014 | |
|
1015 | 0 | StartFunction(GlobalDecl(D, DynamicInitKind::Initializer), |
1016 | 0 | getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(), |
1017 | 0 | FunctionArgList()); |
1018 | | // Emit an artificial location for this function. |
1019 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
1020 | | |
1021 | | // Use guarded initialization if the global variable is weak. This |
1022 | | // occurs for, e.g., instantiated static data members and |
1023 | | // definitions explicitly marked weak. |
1024 | | // |
1025 | | // Also use guarded initialization for a variable with dynamic TLS and |
1026 | | // unordered initialization. (If the initialization is ordered, the ABI |
1027 | | // layer will guard the whole-TU initialization for us.) |
1028 | 0 | if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage() || |
1029 | 0 | (D->getTLSKind() == VarDecl::TLS_Dynamic && |
1030 | 0 | isTemplateInstantiation(D->getTemplateSpecializationKind()))) { |
1031 | 0 | EmitCXXGuardedInit(*D, Addr, PerformInit); |
1032 | 0 | } else { |
1033 | 0 | EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); |
1034 | 0 | } |
1035 | |
|
1036 | 0 | if (getLangOpts().HLSL) |
1037 | 0 | CGM.getHLSLRuntime().annotateHLSLResource(D, Addr); |
1038 | |
|
1039 | 0 | FinishFunction(); |
1040 | 0 | } |
1041 | | |
1042 | | void |
1043 | | CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, |
1044 | | ArrayRef<llvm::Function *> Decls, |
1045 | 0 | ConstantAddress Guard) { |
1046 | 0 | { |
1047 | 0 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
1048 | 0 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
1049 | 0 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
1050 | | // Emit an artificial location for this function. |
1051 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
1052 | |
|
1053 | 0 | llvm::BasicBlock *ExitBlock = nullptr; |
1054 | 0 | if (Guard.isValid()) { |
1055 | | // If we have a guard variable, check whether we've already performed |
1056 | | // these initializations. This happens for TLS initialization functions. |
1057 | 0 | llvm::Value *GuardVal = Builder.CreateLoad(Guard); |
1058 | 0 | llvm::Value *Uninit = Builder.CreateIsNull(GuardVal, |
1059 | 0 | "guard.uninitialized"); |
1060 | 0 | llvm::BasicBlock *InitBlock = createBasicBlock("init"); |
1061 | 0 | ExitBlock = createBasicBlock("exit"); |
1062 | 0 | EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock, |
1063 | 0 | GuardKind::TlsGuard, nullptr); |
1064 | 0 | EmitBlock(InitBlock); |
1065 | | // Mark as initialized before initializing anything else. If the |
1066 | | // initializers use previously-initialized thread_local vars, that's |
1067 | | // probably supposed to be OK, but the standard doesn't say. |
1068 | 0 | Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard); |
1069 | | |
1070 | | // The guard variable can't ever change again. |
1071 | 0 | EmitInvariantStart( |
1072 | 0 | Guard.getPointer(), |
1073 | 0 | CharUnits::fromQuantity( |
1074 | 0 | CGM.getDataLayout().getTypeAllocSize(GuardVal->getType()))); |
1075 | 0 | } |
1076 | |
|
1077 | 0 | RunCleanupsScope Scope(*this); |
1078 | | |
1079 | | // When building in Objective-C++ ARC mode, create an autorelease pool |
1080 | | // around the global initializers. |
1081 | 0 | if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) { |
1082 | 0 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
1083 | 0 | EmitObjCAutoreleasePoolCleanup(token); |
1084 | 0 | } |
1085 | |
|
1086 | 0 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) |
1087 | 0 | if (Decls[i]) |
1088 | 0 | EmitRuntimeCall(Decls[i]); |
1089 | |
|
1090 | 0 | Scope.ForceCleanup(); |
1091 | |
|
1092 | 0 | if (ExitBlock) { |
1093 | 0 | Builder.CreateBr(ExitBlock); |
1094 | 0 | EmitBlock(ExitBlock); |
1095 | 0 | } |
1096 | 0 | } |
1097 | |
|
1098 | 0 | FinishFunction(); |
1099 | 0 | } |
1100 | | |
1101 | | void CodeGenFunction::GenerateCXXGlobalCleanUpFunc( |
1102 | | llvm::Function *Fn, |
1103 | | ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH, |
1104 | | llvm::Constant *>> |
1105 | 0 | DtorsOrStermFinalizers) { |
1106 | 0 | { |
1107 | 0 | auto NL = ApplyDebugLocation::CreateEmpty(*this); |
1108 | 0 | StartFunction(GlobalDecl(), getContext().VoidTy, Fn, |
1109 | 0 | getTypes().arrangeNullaryFunction(), FunctionArgList()); |
1110 | | // Emit an artificial location for this function. |
1111 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
1112 | | |
1113 | | // Emit the cleanups, in reverse order from construction. |
1114 | 0 | for (unsigned i = 0, e = DtorsOrStermFinalizers.size(); i != e; ++i) { |
1115 | 0 | llvm::FunctionType *CalleeTy; |
1116 | 0 | llvm::Value *Callee; |
1117 | 0 | llvm::Constant *Arg; |
1118 | 0 | std::tie(CalleeTy, Callee, Arg) = DtorsOrStermFinalizers[e - i - 1]; |
1119 | |
|
1120 | 0 | llvm::CallInst *CI = nullptr; |
1121 | 0 | if (Arg == nullptr) { |
1122 | 0 | assert( |
1123 | 0 | CGM.getCXXABI().useSinitAndSterm() && |
1124 | 0 | "Arg could not be nullptr unless using sinit and sterm functions."); |
1125 | 0 | CI = Builder.CreateCall(CalleeTy, Callee); |
1126 | 0 | } else |
1127 | 0 | CI = Builder.CreateCall(CalleeTy, Callee, Arg); |
1128 | | |
1129 | | // Make sure the call and the callee agree on calling convention. |
1130 | 0 | if (llvm::Function *F = dyn_cast<llvm::Function>(Callee)) |
1131 | 0 | CI->setCallingConv(F->getCallingConv()); |
1132 | 0 | } |
1133 | 0 | } |
1134 | |
|
1135 | 0 | FinishFunction(); |
1136 | 0 | } |
1137 | | |
1138 | | /// generateDestroyHelper - Generates a helper function which, when |
1139 | | /// invoked, destroys the given object. The address of the object |
1140 | | /// should be in global memory. |
1141 | | llvm::Function *CodeGenFunction::generateDestroyHelper( |
1142 | | Address addr, QualType type, Destroyer *destroyer, |
1143 | 0 | bool useEHCleanupForArray, const VarDecl *VD) { |
1144 | 0 | FunctionArgList args; |
1145 | 0 | ImplicitParamDecl Dst(getContext(), getContext().VoidPtrTy, |
1146 | 0 | ImplicitParamKind::Other); |
1147 | 0 | args.push_back(&Dst); |
1148 | |
|
1149 | 0 | const CGFunctionInfo &FI = |
1150 | 0 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy, args); |
1151 | 0 | llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI); |
1152 | 0 | llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction( |
1153 | 0 | FTy, "__cxx_global_array_dtor", FI, VD->getLocation()); |
1154 | |
|
1155 | 0 | CurEHLocation = VD->getBeginLoc(); |
1156 | |
|
1157 | 0 | StartFunction(GlobalDecl(VD, DynamicInitKind::GlobalArrayDestructor), |
1158 | 0 | getContext().VoidTy, fn, FI, args); |
1159 | | // Emit an artificial location for this function. |
1160 | 0 | auto AL = ApplyDebugLocation::CreateArtificial(*this); |
1161 | |
|
1162 | 0 | emitDestroy(addr, type, destroyer, useEHCleanupForArray); |
1163 | |
|
1164 | 0 | FinishFunction(); |
1165 | |
|
1166 | 0 | return fn; |
1167 | 0 | } |