1 //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This contains code dealing with code generation of C++ declarations
11 //===----------------------------------------------------------------------===//
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"
26 using namespace clang
;
27 using namespace CodeGen
;
29 static void EmitDeclInit(CodeGenFunction
&CGF
, const VarDecl
&D
,
30 ConstantAddress DeclPtr
) {
32 (D
.hasGlobalStorage() ||
33 (D
.hasLocalStorage() && CGF
.getContext().getLangOpts().OpenCLCPlusPlus
)) &&
34 "VarDecl must have global or local (in the case of OpenCL) storage!");
35 assert(!D
.getType()->isReferenceType() &&
36 "Should not call EmitDeclInit on a reference!");
38 QualType type
= D
.getType();
39 LValue lv
= CGF
.MakeAddrLValue(DeclPtr
, type
);
41 const Expr
*Init
= D
.getInit();
42 switch (CGF
.getEvaluationKind(type
)) {
44 CodeGenModule
&CGM
= CGF
.CGM
;
45 if (lv
.isObjCStrong())
46 CGM
.getObjCRuntime().EmitObjCGlobalAssign(CGF
, CGF
.EmitScalarExpr(Init
),
47 DeclPtr
, D
.getTLSKind());
48 else if (lv
.isObjCWeak())
49 CGM
.getObjCRuntime().EmitObjCWeakAssign(CGF
, CGF
.EmitScalarExpr(Init
),
52 CGF
.EmitScalarInit(Init
, &D
, lv
, false);
56 CGF
.EmitComplexExprIntoLValue(Init
, lv
, /*isInit*/ true);
60 AggValueSlot::forLValue(lv
, CGF
, AggValueSlot::IsDestructed
,
61 AggValueSlot::DoesNotNeedGCBarriers
,
62 AggValueSlot::IsNotAliased
,
63 AggValueSlot::DoesNotOverlap
));
66 llvm_unreachable("bad evaluation kind");
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 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 QualType::DestructionKind DtorKind
= D
.needsDestruction(CGF
.getContext());
81 // FIXME: __attribute__((cleanup)) ?
84 case QualType::DK_none
:
87 case QualType::DK_cxx_destructor
:
90 case QualType::DK_objc_strong_lifetime
:
91 case QualType::DK_objc_weak_lifetime
:
92 case QualType::DK_nontrivial_c_struct
:
93 // We don't care about releasing objects during process teardown.
94 assert(!D
.getTLSKind() && "should have rejected this");
98 llvm::FunctionCallee Func
;
99 llvm::Constant
*Argument
;
101 CodeGenModule
&CGM
= CGF
.CGM
;
102 QualType Type
= D
.getType();
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
108 const CXXRecordDecl
*Record
= Type
->getAsCXXRecordDecl();
109 bool CanRegisterDestructor
=
110 Record
&& (!CGM
.getCXXABI().HasThisReturn(
111 GlobalDecl(Record
->getDestructor(), Dtor_Complete
)) ||
112 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
116 bool UsingExternalHelper
= !CGM
.getCodeGenOpts().CXAAtExit
;
117 if (Record
&& (CanRegisterDestructor
|| UsingExternalHelper
)) {
118 assert(!Record
->hasTrivialDestructor());
119 CXXDestructorDecl
*Dtor
= Record
->getDestructor();
121 Func
= CGM
.getAddrAndTypeOfCXXStructor(GlobalDecl(Dtor
, Dtor_Complete
));
122 if (CGF
.getContext().getLangOpts().OpenCL
) {
124 CGM
.getTargetCodeGenInfo().getAddrSpaceOfCxaAtexitPtrParam();
125 auto DestTy
= CGF
.getTypes().ConvertType(Type
)->getPointerTo(
126 CGM
.getContext().getTargetAddressSpace(DestAS
));
127 auto SrcAS
= D
.getType().getQualifiers().getAddressSpace();
129 Argument
= llvm::ConstantExpr::getBitCast(Addr
.getPointer(), DestTy
);
131 // FIXME: On addr space mismatch we are passing NULL. The generation
132 // of the global destructor function should be adjusted accordingly.
133 Argument
= llvm::ConstantPointerNull::get(DestTy
);
135 Argument
= llvm::ConstantExpr::getBitCast(
136 Addr
.getPointer(), CGF
.getTypes().ConvertType(Type
)->getPointerTo());
138 // Otherwise, the standard logic requires a helper function.
140 Addr
= Addr
.withElementType(CGF
.ConvertTypeForMem(Type
));
141 Func
= CodeGenFunction(CGM
)
142 .generateDestroyHelper(Addr
, Type
, CGF
.getDestroyer(DtorKind
),
143 CGF
.needsEHCleanup(DtorKind
), &D
);
144 Argument
= llvm::Constant::getNullValue(CGF
.Int8PtrTy
);
147 CGM
.getCXXABI().registerGlobalDtor(CGF
, D
, Func
, Argument
);
150 /// Emit code to cause the variable at the given address to be considered as
151 /// constant from this point onwards.
152 static void EmitDeclInvariant(CodeGenFunction
&CGF
, const VarDecl
&D
,
153 llvm::Constant
*Addr
) {
154 return CGF
.EmitInvariantStart(
155 Addr
, CGF
.getContext().getTypeSizeInChars(D
.getType()));
158 void CodeGenFunction::EmitInvariantStart(llvm::Constant
*Addr
, CharUnits Size
) {
159 // Do not emit the intrinsic if we're not optimizing.
160 if (!CGM
.getCodeGenOpts().OptimizationLevel
)
163 // Grab the llvm.invariant.start intrinsic.
164 llvm::Intrinsic::ID InvStartID
= llvm::Intrinsic::invariant_start
;
165 // Overloaded address space type.
166 llvm::Type
*ObjectPtr
[1] = {Int8PtrTy
};
167 llvm::Function
*InvariantStart
= CGM
.getIntrinsic(InvStartID
, ObjectPtr
);
169 // Emit a call with the size in bytes of the object.
170 uint64_t Width
= Size
.getQuantity();
171 llvm::Value
*Args
[2] = { llvm::ConstantInt::getSigned(Int64Ty
, Width
),
172 llvm::ConstantExpr::getBitCast(Addr
, Int8PtrTy
)};
173 Builder
.CreateCall(InvariantStart
, Args
);
176 void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl
&D
,
177 llvm::GlobalVariable
*GV
,
180 const Expr
*Init
= D
.getInit();
181 QualType T
= D
.getType();
183 // The address space of a static local variable (DeclPtr) may be different
184 // from the address space of the "this" argument of the constructor. In that
185 // case, we need an addrspacecast before calling the constructor.
187 // struct StructWithCtor {
188 // __device__ StructWithCtor() {...}
190 // __device__ void foo() {
191 // __shared__ StructWithCtor s;
195 // For example, in the above CUDA code, the static local variable s has a
196 // "shared" address space qualifier, but the constructor of StructWithCtor
197 // expects "this" in the "generic" address space.
198 unsigned ExpectedAddrSpace
= getTypes().getTargetAddressSpace(T
);
199 unsigned ActualAddrSpace
= GV
->getAddressSpace();
200 llvm::Constant
*DeclPtr
= GV
;
201 if (ActualAddrSpace
!= ExpectedAddrSpace
) {
202 llvm::PointerType
*PTy
=
203 llvm::PointerType::get(getLLVMContext(), ExpectedAddrSpace
);
204 DeclPtr
= llvm::ConstantExpr::getAddrSpaceCast(DeclPtr
, PTy
);
207 ConstantAddress
DeclAddr(
208 DeclPtr
, GV
->getValueType(), getContext().getDeclAlign(&D
));
210 if (!T
->isReferenceType()) {
211 if (getLangOpts().OpenMP
&& !getLangOpts().OpenMPSimd
&&
212 D
.hasAttr
<OMPThreadPrivateDeclAttr
>()) {
213 (void)CGM
.getOpenMPRuntime().emitThreadPrivateVarDefinition(
214 &D
, DeclAddr
, D
.getAttr
<OMPThreadPrivateDeclAttr
>()->getLocation(),
218 D
.needsDestruction(getContext()) == QualType::DK_cxx_destructor
;
220 EmitDeclInit(*this, D
, DeclAddr
);
221 if (CGM
.isTypeConstant(D
.getType(), true, !NeedsDtor
))
222 EmitDeclInvariant(*this, D
, DeclPtr
);
224 EmitDeclDestroy(*this, D
, DeclAddr
);
228 assert(PerformInit
&& "cannot have constant initializer which needs "
229 "destruction for reference");
230 RValue RV
= EmitReferenceBindingToExpr(Init
);
231 EmitStoreOfScalar(RV
.getScalarVal(), DeclAddr
, false, T
);
234 /// Create a stub function, suitable for being passed to atexit,
235 /// which passes the given address to the given destructor function.
236 llvm::Function
*CodeGenFunction::createAtExitStub(const VarDecl
&VD
,
237 llvm::FunctionCallee dtor
,
238 llvm::Constant
*addr
) {
239 // Get the destructor function type, void(*)(void).
240 llvm::FunctionType
*ty
= llvm::FunctionType::get(CGM
.VoidTy
, false);
241 SmallString
<256> FnName
;
243 llvm::raw_svector_ostream
Out(FnName
);
244 CGM
.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&VD
, Out
);
247 const CGFunctionInfo
&FI
= CGM
.getTypes().arrangeNullaryFunction();
248 llvm::Function
*fn
= CGM
.CreateGlobalInitOrCleanUpFunction(
249 ty
, FnName
.str(), FI
, VD
.getLocation());
251 CodeGenFunction
CGF(CGM
);
253 CGF
.StartFunction(GlobalDecl(&VD
, DynamicInitKind::AtExit
),
254 CGM
.getContext().VoidTy
, fn
, FI
, FunctionArgList(),
255 VD
.getLocation(), VD
.getInit()->getExprLoc());
256 // Emit an artificial location for this function.
257 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
259 llvm::CallInst
*call
= CGF
.Builder
.CreateCall(dtor
, addr
);
261 // Make sure the call and the callee agree on calling convention.
262 if (auto *dtorFn
= dyn_cast
<llvm::Function
>(
263 dtor
.getCallee()->stripPointerCastsAndAliases()))
264 call
->setCallingConv(dtorFn
->getCallingConv());
266 CGF
.FinishFunction();
271 /// Create a stub function, suitable for being passed to __pt_atexit_np,
272 /// which passes the given address to the given destructor function.
273 llvm::Function
*CodeGenFunction::createTLSAtExitStub(
274 const VarDecl
&D
, llvm::FunctionCallee Dtor
, llvm::Constant
*Addr
,
275 llvm::FunctionCallee
&AtExit
) {
276 SmallString
<256> FnName
;
278 llvm::raw_svector_ostream
Out(FnName
);
279 CGM
.getCXXABI().getMangleContext().mangleDynamicAtExitDestructor(&D
, Out
);
282 const CGFunctionInfo
&FI
= CGM
.getTypes().arrangeLLVMFunctionInfo(
283 getContext().IntTy
, /*instanceMethod=*/false, /*chainCall=*/false,
284 {getContext().IntTy
}, FunctionType::ExtInfo(), {}, RequiredArgs::All
);
286 // Get the stub function type, int(*)(int,...).
287 llvm::FunctionType
*StubTy
=
288 llvm::FunctionType::get(CGM
.IntTy
, {CGM
.IntTy
}, true);
290 llvm::Function
*DtorStub
= CGM
.CreateGlobalInitOrCleanUpFunction(
291 StubTy
, FnName
.str(), FI
, D
.getLocation());
293 CodeGenFunction
CGF(CGM
);
295 FunctionArgList Args
;
296 ImplicitParamDecl
IPD(CGM
.getContext(), CGM
.getContext().IntTy
,
297 ImplicitParamDecl::Other
);
298 Args
.push_back(&IPD
);
299 QualType ResTy
= CGM
.getContext().IntTy
;
301 CGF
.StartFunction(GlobalDecl(&D
, DynamicInitKind::AtExit
), ResTy
, DtorStub
,
302 FI
, Args
, D
.getLocation(), D
.getInit()->getExprLoc());
304 // Emit an artificial location for this function.
305 auto AL
= ApplyDebugLocation::CreateArtificial(CGF
);
307 llvm::CallInst
*call
= CGF
.Builder
.CreateCall(Dtor
, Addr
);
309 // Make sure the call and the callee agree on calling convention.
310 if (auto *DtorFn
= dyn_cast
<llvm::Function
>(
311 Dtor
.getCallee()->stripPointerCastsAndAliases()))
312 call
->setCallingConv(DtorFn
->getCallingConv());
314 // Return 0 from function
315 CGF
.Builder
.CreateStore(llvm::Constant::getNullValue(CGM
.IntTy
),
318 CGF
.FinishFunction();
323 /// Register a global destructor using the C atexit runtime function.
324 void CodeGenFunction::registerGlobalDtorWithAtExit(const VarDecl
&VD
,
325 llvm::FunctionCallee dtor
,
326 llvm::Constant
*addr
) {
327 // Create a function which calls the destructor.
328 llvm::Constant
*dtorStub
= createAtExitStub(VD
, dtor
, addr
);
329 registerGlobalDtorWithAtExit(dtorStub
);
332 void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant
*dtorStub
) {
333 // extern "C" int atexit(void (*f)(void));
334 assert(dtorStub
->getType() ==
335 llvm::PointerType::get(
336 llvm::FunctionType::get(CGM
.VoidTy
, false),
337 dtorStub
->getType()->getPointerAddressSpace()) &&
338 "Argument to atexit has a wrong type.");
340 llvm::FunctionType
*atexitTy
=
341 llvm::FunctionType::get(IntTy
, dtorStub
->getType(), false);
343 llvm::FunctionCallee atexit
=
344 CGM
.CreateRuntimeFunction(atexitTy
, "atexit", llvm::AttributeList(),
346 if (llvm::Function
*atexitFn
= dyn_cast
<llvm::Function
>(atexit
.getCallee()))
347 atexitFn
->setDoesNotThrow();
349 EmitNounwindRuntimeCall(atexit
, dtorStub
);
353 CodeGenFunction::unregisterGlobalDtorWithUnAtExit(llvm::Constant
*dtorStub
) {
354 // The unatexit subroutine unregisters __dtor functions that were previously
355 // registered by the atexit subroutine. If the referenced function is found,
356 // it is removed from the list of functions that are called at normal program
357 // termination and the unatexit returns a value of 0, otherwise a non-zero
358 // value is returned.
360 // extern "C" int unatexit(void (*f)(void));
361 assert(dtorStub
->getType() ==
362 llvm::PointerType::get(
363 llvm::FunctionType::get(CGM
.VoidTy
, false),
364 dtorStub
->getType()->getPointerAddressSpace()) &&
365 "Argument to unatexit has a wrong type.");
367 llvm::FunctionType
*unatexitTy
=
368 llvm::FunctionType::get(IntTy
, {dtorStub
->getType()}, /*isVarArg=*/false);
370 llvm::FunctionCallee unatexit
=
371 CGM
.CreateRuntimeFunction(unatexitTy
, "unatexit", llvm::AttributeList());
373 cast
<llvm::Function
>(unatexit
.getCallee())->setDoesNotThrow();
375 return EmitNounwindRuntimeCall(unatexit
, dtorStub
);
378 void CodeGenFunction::EmitCXXGuardedInit(const VarDecl
&D
,
379 llvm::GlobalVariable
*DeclPtr
,
381 // If we've been asked to forbid guard variables, emit an error now.
382 // This diagnostic is hard-coded for Darwin's use case; we can find
383 // better phrasing if someone else needs it.
384 if (CGM
.getCodeGenOpts().ForbidGuardVariables
)
385 CGM
.Error(D
.getLocation(),
386 "this initialization requires a guard variable, which "
387 "the kernel does not support");
389 CGM
.getCXXABI().EmitGuardedInit(*this, D
, DeclPtr
, PerformInit
);
392 void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value
*NeedsInit
,
393 llvm::BasicBlock
*InitBlock
,
394 llvm::BasicBlock
*NoInitBlock
,
397 assert((Kind
== GuardKind::TlsGuard
|| D
) && "no guarded variable");
399 // A guess at how many times we will enter the initialization of a
400 // variable, depending on the kind of variable.
401 static const uint64_t InitsPerTLSVar
= 1024;
402 static const uint64_t InitsPerLocalVar
= 1024 * 1024;
404 llvm::MDNode
*Weights
;
405 if (Kind
== GuardKind::VariableGuard
&& !D
->isLocalVarDecl()) {
406 // For non-local variables, don't apply any weighting for now. Due to our
407 // use of COMDATs, we expect there to be at most one initialization of the
408 // variable per DSO, but we have no way to know how many DSOs will try to
409 // initialize the variable.
413 // FIXME: For the TLS case, collect and use profiling information to
414 // determine a more accurate brach weight.
415 if (Kind
== GuardKind::TlsGuard
|| D
->getTLSKind())
416 NumInits
= InitsPerTLSVar
;
418 NumInits
= InitsPerLocalVar
;
420 // The probability of us entering the initializer is
421 // 1 / (total number of times we attempt to initialize the variable).
422 llvm::MDBuilder
MDHelper(CGM
.getLLVMContext());
423 Weights
= MDHelper
.createBranchWeights(1, NumInits
- 1);
426 Builder
.CreateCondBr(NeedsInit
, InitBlock
, NoInitBlock
, Weights
);
429 llvm::Function
*CodeGenModule::CreateGlobalInitOrCleanUpFunction(
430 llvm::FunctionType
*FTy
, const Twine
&Name
, const CGFunctionInfo
&FI
,
431 SourceLocation Loc
, bool TLS
, llvm::GlobalVariable::LinkageTypes Linkage
) {
432 llvm::Function
*Fn
= llvm::Function::Create(FTy
, Linkage
, Name
, &getModule());
434 if (!getLangOpts().AppleKext
&& !TLS
) {
435 // Set the section if needed.
436 if (const char *Section
= getTarget().getStaticInitSectionSpecifier())
437 Fn
->setSection(Section
);
440 if (Linkage
== llvm::GlobalVariable::InternalLinkage
)
441 SetInternalFunctionAttributes(GlobalDecl(), Fn
, FI
);
443 Fn
->setCallingConv(getRuntimeCC());
445 if (!getLangOpts().Exceptions
)
446 Fn
->setDoesNotThrow();
448 if (getLangOpts().Sanitize
.has(SanitizerKind::Address
) &&
449 !isInNoSanitizeList(SanitizerKind::Address
, Fn
, Loc
))
450 Fn
->addFnAttr(llvm::Attribute::SanitizeAddress
);
452 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelAddress
) &&
453 !isInNoSanitizeList(SanitizerKind::KernelAddress
, Fn
, Loc
))
454 Fn
->addFnAttr(llvm::Attribute::SanitizeAddress
);
456 if (getLangOpts().Sanitize
.has(SanitizerKind::HWAddress
) &&
457 !isInNoSanitizeList(SanitizerKind::HWAddress
, Fn
, Loc
))
458 Fn
->addFnAttr(llvm::Attribute::SanitizeHWAddress
);
460 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelHWAddress
) &&
461 !isInNoSanitizeList(SanitizerKind::KernelHWAddress
, Fn
, Loc
))
462 Fn
->addFnAttr(llvm::Attribute::SanitizeHWAddress
);
464 if (getLangOpts().Sanitize
.has(SanitizerKind::MemtagStack
) &&
465 !isInNoSanitizeList(SanitizerKind::MemtagStack
, Fn
, Loc
))
466 Fn
->addFnAttr(llvm::Attribute::SanitizeMemTag
);
468 if (getLangOpts().Sanitize
.has(SanitizerKind::Thread
) &&
469 !isInNoSanitizeList(SanitizerKind::Thread
, Fn
, Loc
))
470 Fn
->addFnAttr(llvm::Attribute::SanitizeThread
);
472 if (getLangOpts().Sanitize
.has(SanitizerKind::Memory
) &&
473 !isInNoSanitizeList(SanitizerKind::Memory
, Fn
, Loc
))
474 Fn
->addFnAttr(llvm::Attribute::SanitizeMemory
);
476 if (getLangOpts().Sanitize
.has(SanitizerKind::KernelMemory
) &&
477 !isInNoSanitizeList(SanitizerKind::KernelMemory
, Fn
, Loc
))
478 Fn
->addFnAttr(llvm::Attribute::SanitizeMemory
);
480 if (getLangOpts().Sanitize
.has(SanitizerKind::SafeStack
) &&
481 !isInNoSanitizeList(SanitizerKind::SafeStack
, Fn
, Loc
))
482 Fn
->addFnAttr(llvm::Attribute::SafeStack
);
484 if (getLangOpts().Sanitize
.has(SanitizerKind::ShadowCallStack
) &&
485 !isInNoSanitizeList(SanitizerKind::ShadowCallStack
, Fn
, Loc
))
486 Fn
->addFnAttr(llvm::Attribute::ShadowCallStack
);
491 /// Create a global pointer to a function that will initialize a global
492 /// variable. The user has requested that this pointer be emitted in a specific
494 void CodeGenModule::EmitPointerToInitFunc(const VarDecl
*D
,
495 llvm::GlobalVariable
*GV
,
496 llvm::Function
*InitFunc
,
498 llvm::GlobalVariable
*PtrArray
= new llvm::GlobalVariable(
499 TheModule
, InitFunc
->getType(), /*isConstant=*/true,
500 llvm::GlobalValue::PrivateLinkage
, InitFunc
, "__cxx_init_fn_ptr");
501 PtrArray
->setSection(ISA
->getSection());
502 addUsedGlobal(PtrArray
);
504 // If the GV is already in a comdat group, then we have to join it.
505 if (llvm::Comdat
*C
= GV
->getComdat())
506 PtrArray
->setComdat(C
);
510 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl
*D
,
511 llvm::GlobalVariable
*Addr
,
514 // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
515 // __constant__ and __shared__ variables defined in namespace scope,
516 // that are of class type, cannot have a non-empty constructor. All
517 // the checks have been done in Sema by now. Whatever initializers
518 // are allowed are empty and we just need to ignore them here.
519 if (getLangOpts().CUDAIsDevice
&& !getLangOpts().GPUAllowDeviceInit
&&
520 (D
->hasAttr
<CUDADeviceAttr
>() || D
->hasAttr
<CUDAConstantAttr
>() ||
521 D
->hasAttr
<CUDASharedAttr
>()))
524 if (getLangOpts().OpenMP
&&
525 getOpenMPRuntime().emitDeclareTargetVarDefinition(D
, Addr
, PerformInit
))
528 // Check if we've already initialized this decl.
529 auto I
= DelayedCXXInitPosition
.find(D
);
530 if (I
!= DelayedCXXInitPosition
.end() && I
->second
== ~0U)
533 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
534 SmallString
<256> FnName
;
536 llvm::raw_svector_ostream
Out(FnName
);
537 getCXXABI().getMangleContext().mangleDynamicInitializer(D
, Out
);
540 // Create a variable initialization function.
541 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
542 FTy
, FnName
.str(), getTypes().arrangeNullaryFunction(), D
->getLocation());
544 auto *ISA
= D
->getAttr
<InitSegAttr
>();
545 CodeGenFunction(*this).GenerateCXXGlobalVarDeclInitFunc(Fn
, D
, Addr
,
548 llvm::GlobalVariable
*COMDATKey
=
549 supportsCOMDAT() && D
->isExternallyVisible() ? Addr
: nullptr;
551 if (D
->getTLSKind()) {
552 // FIXME: Should we support init_priority for thread_local?
553 // FIXME: We only need to register one __cxa_thread_atexit function for the
555 CXXThreadLocalInits
.push_back(Fn
);
556 CXXThreadLocalInitVars
.push_back(D
);
557 } else if (PerformInit
&& ISA
) {
558 // Contract with backend that "init_seg(compiler)" corresponds to priority
559 // 200 and "init_seg(lib)" corresponds to priority 400.
561 if (ISA
->getSection() == ".CRT$XCC")
563 else if (ISA
->getSection() == ".CRT$XCL")
567 AddGlobalCtor(Fn
, Priority
, ~0U, COMDATKey
);
569 EmitPointerToInitFunc(D
, Addr
, Fn
, ISA
);
570 } else if (auto *IPA
= D
->getAttr
<InitPriorityAttr
>()) {
571 OrderGlobalInitsOrStermFinalizers
Key(IPA
->getPriority(),
572 PrioritizedCXXGlobalInits
.size());
573 PrioritizedCXXGlobalInits
.push_back(std::make_pair(Key
, Fn
));
574 } else if (isTemplateInstantiation(D
->getTemplateSpecializationKind()) ||
575 getContext().GetGVALinkageForVariable(D
) == GVA_DiscardableODR
||
576 D
->hasAttr
<SelectAnyAttr
>()) {
577 // C++ [basic.start.init]p2:
578 // Definitions of explicitly specialized class template static data
579 // members have ordered initialization. Other class template static data
580 // members (i.e., implicitly or explicitly instantiated specializations)
581 // have unordered initialization.
583 // As a consequence, we can put them into their own llvm.global_ctors entry.
585 // If the global is externally visible, put the initializer into a COMDAT
586 // group with the global being initialized. On most platforms, this is a
587 // minor startup time optimization. In the MS C++ ABI, there are no guard
588 // variables, so this COMDAT key is required for correctness.
590 // SelectAny globals will be comdat-folded. Put the initializer into a
591 // COMDAT group associated with the global, so the initializers get folded
593 I
= DelayedCXXInitPosition
.find(D
);
594 // CXXGlobalInits.size() is the lex order number for the next deferred
595 // VarDecl. Use it when the current VarDecl is non-deferred. Although this
596 // lex order number is shared between current VarDecl and some following
597 // VarDecls, their order of insertion into `llvm.global_ctors` is the same
598 // as the lexing order and the following stable sort would preserve such
601 I
== DelayedCXXInitPosition
.end() ? CXXGlobalInits
.size() : I
->second
;
602 AddGlobalCtor(Fn
, 65535, LexOrder
, COMDATKey
);
603 if (COMDATKey
&& (getTriple().isOSBinFormatELF() ||
604 getTarget().getCXXABI().isMicrosoft())) {
605 // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in
606 // llvm.used to prevent linker GC.
607 addUsedGlobal(COMDATKey
);
610 // If we used a COMDAT key for the global ctor, the init function can be
611 // discarded if the global ctor entry is discarded.
612 // FIXME: Do we need to restrict this to ELF and Wasm?
613 llvm::Comdat
*C
= Addr
->getComdat();
614 if (COMDATKey
&& C
&&
615 (getTarget().getTriple().isOSBinFormatELF() ||
616 getTarget().getTriple().isOSBinFormatWasm())) {
620 I
= DelayedCXXInitPosition
.find(D
); // Re-do lookup in case of re-hash.
621 if (I
== DelayedCXXInitPosition
.end()) {
622 CXXGlobalInits
.push_back(Fn
);
623 } else if (I
->second
!= ~0U) {
624 assert(I
->second
< CXXGlobalInits
.size() &&
625 CXXGlobalInits
[I
->second
] == nullptr);
626 CXXGlobalInits
[I
->second
] = Fn
;
630 // Remember that we already emitted the initializer for this global.
631 DelayedCXXInitPosition
[D
] = ~0U;
634 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
635 getCXXABI().EmitThreadLocalInitFuncs(
636 *this, CXXThreadLocals
, CXXThreadLocalInits
, CXXThreadLocalInitVars
);
638 CXXThreadLocalInits
.clear();
639 CXXThreadLocalInitVars
.clear();
640 CXXThreadLocals
.clear();
643 /* Build the initializer for a C++20 module:
644 This is arranged to be run only once regardless of how many times the module
645 might be included transitively. This arranged by using a guard variable.
647 If there are no initalizers at all (and also no imported modules) we reduce
648 this to an empty function (since the Itanium ABI requires that this function
649 be available to a caller, which might be produced by a different
652 First we call any initializers for imported modules.
653 We then call initializers for the Global Module Fragment (if present)
654 We then call initializers for the current module.
655 We then call initializers for the Private Module Fragment (if present)
658 void CodeGenModule::EmitCXXModuleInitFunc(Module
*Primary
) {
659 while (!CXXGlobalInits
.empty() && !CXXGlobalInits
.back())
660 CXXGlobalInits
.pop_back();
662 // As noted above, we create the function, even if it is empty.
663 // Module initializers for imported modules are emitted first.
665 // Collect all the modules that we import
666 SmallVector
<Module
*> AllImports
;
667 // Ones that we export
668 for (auto I
: Primary
->Exports
)
669 AllImports
.push_back(I
.getPointer());
670 // Ones that we only import.
671 for (Module
*M
: Primary
->Imports
)
672 AllImports
.push_back(M
);
674 SmallVector
<llvm::Function
*, 8> ModuleInits
;
675 for (Module
*M
: AllImports
) {
676 // No Itanium initializer in header like modules.
677 if (M
->isHeaderLikeModule())
678 continue; // TODO: warn of mixed use of module map modules and C++20?
679 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
680 SmallString
<256> FnName
;
682 llvm::raw_svector_ostream
Out(FnName
);
683 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
684 .mangleModuleInitializer(M
, Out
);
686 assert(!GetGlobalValue(FnName
.str()) &&
687 "We should only have one use of the initializer call");
688 llvm::Function
*Fn
= llvm::Function::Create(
689 FTy
, llvm::Function::ExternalLinkage
, FnName
.str(), &getModule());
690 ModuleInits
.push_back(Fn
);
693 // Add any initializers with specified priority; this uses the same approach
694 // as EmitCXXGlobalInitFunc().
695 if (!PrioritizedCXXGlobalInits
.empty()) {
696 SmallVector
<llvm::Function
*, 8> LocalCXXGlobalInits
;
697 llvm::array_pod_sort(PrioritizedCXXGlobalInits
.begin(),
698 PrioritizedCXXGlobalInits
.end());
699 for (SmallVectorImpl
<GlobalInitData
>::iterator
700 I
= PrioritizedCXXGlobalInits
.begin(),
701 E
= PrioritizedCXXGlobalInits
.end();
703 SmallVectorImpl
<GlobalInitData
>::iterator PrioE
=
704 std::upper_bound(I
+ 1, E
, *I
, GlobalInitPriorityCmp());
706 for (; I
< PrioE
; ++I
)
707 ModuleInits
.push_back(I
->second
);
711 // Now append the ones without specified priority.
712 for (auto *F
: CXXGlobalInits
)
713 ModuleInits
.push_back(F
);
715 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
716 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
718 // We now build the initializer for this module, which has a mangled name
719 // as per the Itanium ABI . The action of the initializer is guarded so that
720 // each init is run just once (even though a module might be imported
721 // multiple times via nested use).
724 SmallString
<256> InitFnName
;
725 llvm::raw_svector_ostream
Out(InitFnName
);
726 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
727 .mangleModuleInitializer(Primary
, Out
);
728 Fn
= CreateGlobalInitOrCleanUpFunction(
729 FTy
, llvm::Twine(InitFnName
), FI
, SourceLocation(), false,
730 llvm::GlobalVariable::ExternalLinkage
);
732 // If we have a completely empty initializer then we do not want to create
733 // the guard variable.
734 ConstantAddress GuardAddr
= ConstantAddress::invalid();
735 if (!AllImports
.empty() || !PrioritizedCXXGlobalInits
.empty() ||
736 !CXXGlobalInits
.empty()) {
737 // Create the guard var.
738 llvm::GlobalVariable
*Guard
= new llvm::GlobalVariable(
739 getModule(), Int8Ty
, /*isConstant=*/false,
740 llvm::GlobalVariable::InternalLinkage
,
741 llvm::ConstantInt::get(Int8Ty
, 0), InitFnName
.str() + "__in_chrg");
742 CharUnits GuardAlign
= CharUnits::One();
743 Guard
->setAlignment(GuardAlign
.getAsAlign());
744 GuardAddr
= ConstantAddress(Guard
, Int8Ty
, GuardAlign
);
746 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, ModuleInits
,
750 // We allow for the case that a module object is added to a linked binary
751 // without a specific call to the the initializer. This also ensures that
752 // implementation partition initializers are called when the partition
753 // is not imported as an interface.
756 // See the comment in EmitCXXGlobalInitFunc about OpenCL global init
758 if (getLangOpts().OpenCL
) {
759 GenKernelArgMetadata(Fn
);
760 Fn
->setCallingConv(llvm::CallingConv::SPIR_KERNEL
);
763 assert(!getLangOpts().CUDA
|| !getLangOpts().CUDAIsDevice
||
764 getLangOpts().GPUAllowDeviceInit
);
765 if (getLangOpts().HIP
&& getLangOpts().CUDAIsDevice
) {
766 Fn
->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL
);
767 Fn
->addFnAttr("device-init");
770 // We are done with the inits.
772 PrioritizedCXXGlobalInits
.clear();
773 CXXGlobalInits
.clear();
777 static SmallString
<128> getTransformedFileName(llvm::Module
&M
) {
778 SmallString
<128> FileName
= llvm::sys::path::filename(M
.getName());
780 if (FileName
.empty())
783 for (size_t i
= 0; i
< FileName
.size(); ++i
) {
784 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
785 // to be the set of C preprocessing numbers.
786 if (!isPreprocessingNumberBody(FileName
[i
]))
793 static std::string
getPrioritySuffix(unsigned int Priority
) {
794 assert(Priority
<= 65535 && "Priority should always be <= 65535.");
796 // Compute the function suffix from priority. Prepend with zeroes to make
797 // sure the function names are also ordered as priorities.
798 std::string PrioritySuffix
= llvm::utostr(Priority
);
799 PrioritySuffix
= std::string(6 - PrioritySuffix
.size(), '0') + PrioritySuffix
;
801 return PrioritySuffix
;
805 CodeGenModule::EmitCXXGlobalInitFunc() {
806 while (!CXXGlobalInits
.empty() && !CXXGlobalInits
.back())
807 CXXGlobalInits
.pop_back();
809 // When we import C++20 modules, we must run their initializers first.
810 SmallVector
<llvm::Function
*, 8> ModuleInits
;
811 if (CXX20ModuleInits
)
812 for (Module
*M
: ImportedModules
) {
813 // No Itanium initializer in header like modules.
814 if (M
->isHeaderLikeModule())
816 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
817 SmallString
<256> FnName
;
819 llvm::raw_svector_ostream
Out(FnName
);
820 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
821 .mangleModuleInitializer(M
, Out
);
823 assert(!GetGlobalValue(FnName
.str()) &&
824 "We should only have one use of the initializer call");
825 llvm::Function
*Fn
= llvm::Function::Create(
826 FTy
, llvm::Function::ExternalLinkage
, FnName
.str(), &getModule());
827 ModuleInits
.push_back(Fn
);
830 if (ModuleInits
.empty() && CXXGlobalInits
.empty() &&
831 PrioritizedCXXGlobalInits
.empty())
834 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
835 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
837 // Create our global prioritized initialization function.
838 if (!PrioritizedCXXGlobalInits
.empty()) {
839 SmallVector
<llvm::Function
*, 8> LocalCXXGlobalInits
;
840 llvm::array_pod_sort(PrioritizedCXXGlobalInits
.begin(),
841 PrioritizedCXXGlobalInits
.end());
842 // Iterate over "chunks" of ctors with same priority and emit each chunk
843 // into separate function. Note - everything is sorted first by priority,
844 // second - by lex order, so we emit ctor functions in proper order.
845 for (SmallVectorImpl
<GlobalInitData
>::iterator
846 I
= PrioritizedCXXGlobalInits
.begin(),
847 E
= PrioritizedCXXGlobalInits
.end(); I
!= E
; ) {
848 SmallVectorImpl
<GlobalInitData
>::iterator
849 PrioE
= std::upper_bound(I
+ 1, E
, *I
, GlobalInitPriorityCmp());
851 LocalCXXGlobalInits
.clear();
853 unsigned int Priority
= I
->first
.priority
;
854 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
855 FTy
, "_GLOBAL__I_" + getPrioritySuffix(Priority
), FI
);
857 // Prepend the module inits to the highest priority set.
858 if (!ModuleInits
.empty()) {
859 for (auto *F
: ModuleInits
)
860 LocalCXXGlobalInits
.push_back(F
);
864 for (; I
< PrioE
; ++I
)
865 LocalCXXGlobalInits
.push_back(I
->second
);
867 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, LocalCXXGlobalInits
);
868 AddGlobalCtor(Fn
, Priority
);
870 PrioritizedCXXGlobalInits
.clear();
873 if (getCXXABI().useSinitAndSterm() && ModuleInits
.empty() &&
874 CXXGlobalInits
.empty())
877 for (auto *F
: CXXGlobalInits
)
878 ModuleInits
.push_back(F
);
879 CXXGlobalInits
.clear();
881 // Include the filename in the symbol name. Including "sub_" matches gcc
882 // and makes sure these symbols appear lexicographically behind the symbols
883 // with priority emitted above. Module implementation units behave the same
884 // way as a non-modular TU with imports.
886 if (CXX20ModuleInits
&& getContext().getCurrentNamedModule() &&
887 !getContext().getCurrentNamedModule()->isModuleImplementation()) {
888 SmallString
<256> InitFnName
;
889 llvm::raw_svector_ostream
Out(InitFnName
);
890 cast
<ItaniumMangleContext
>(getCXXABI().getMangleContext())
891 .mangleModuleInitializer(getContext().getCurrentNamedModule(), Out
);
892 Fn
= CreateGlobalInitOrCleanUpFunction(
893 FTy
, llvm::Twine(InitFnName
), FI
, SourceLocation(), false,
894 llvm::GlobalVariable::ExternalLinkage
);
896 Fn
= CreateGlobalInitOrCleanUpFunction(
898 llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())),
901 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn
, ModuleInits
);
904 // In OpenCL global init functions must be converted to kernels in order to
905 // be able to launch them from the host.
906 // FIXME: Some more work might be needed to handle destructors correctly.
907 // Current initialization function makes use of function pointers callbacks.
908 // We can't support function pointers especially between host and device.
909 // However it seems global destruction has little meaning without any
910 // dynamic resource allocation on the device and program scope variables are
911 // destroyed by the runtime when program is released.
912 if (getLangOpts().OpenCL
) {
913 GenKernelArgMetadata(Fn
);
914 Fn
->setCallingConv(llvm::CallingConv::SPIR_KERNEL
);
917 assert(!getLangOpts().CUDA
|| !getLangOpts().CUDAIsDevice
||
918 getLangOpts().GPUAllowDeviceInit
);
919 if (getLangOpts().HIP
&& getLangOpts().CUDAIsDevice
) {
920 Fn
->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL
);
921 Fn
->addFnAttr("device-init");
927 void CodeGenModule::EmitCXXGlobalCleanUpFunc() {
928 if (CXXGlobalDtorsOrStermFinalizers
.empty() &&
929 PrioritizedCXXStermFinalizers
.empty())
932 llvm::FunctionType
*FTy
= llvm::FunctionType::get(VoidTy
, false);
933 const CGFunctionInfo
&FI
= getTypes().arrangeNullaryFunction();
935 // Create our global prioritized cleanup function.
936 if (!PrioritizedCXXStermFinalizers
.empty()) {
937 SmallVector
<CXXGlobalDtorsOrStermFinalizer_t
, 8> LocalCXXStermFinalizers
;
938 llvm::array_pod_sort(PrioritizedCXXStermFinalizers
.begin(),
939 PrioritizedCXXStermFinalizers
.end());
940 // Iterate over "chunks" of dtors with same priority and emit each chunk
941 // into separate function. Note - everything is sorted first by priority,
942 // second - by lex order, so we emit dtor functions in proper order.
943 for (SmallVectorImpl
<StermFinalizerData
>::iterator
944 I
= PrioritizedCXXStermFinalizers
.begin(),
945 E
= PrioritizedCXXStermFinalizers
.end();
947 SmallVectorImpl
<StermFinalizerData
>::iterator PrioE
=
948 std::upper_bound(I
+ 1, E
, *I
, StermFinalizerPriorityCmp());
950 LocalCXXStermFinalizers
.clear();
952 unsigned int Priority
= I
->first
.priority
;
953 llvm::Function
*Fn
= CreateGlobalInitOrCleanUpFunction(
954 FTy
, "_GLOBAL__a_" + getPrioritySuffix(Priority
), FI
);
956 for (; I
< PrioE
; ++I
) {
957 llvm::FunctionCallee DtorFn
= I
->second
;
958 LocalCXXStermFinalizers
.emplace_back(DtorFn
.getFunctionType(),
959 DtorFn
.getCallee(), nullptr);
962 CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc(
963 Fn
, LocalCXXStermFinalizers
);
964 AddGlobalDtor(Fn
, Priority
);
966 PrioritizedCXXStermFinalizers
.clear();
969 if (CXXGlobalDtorsOrStermFinalizers
.empty())
972 // Create our global cleanup function.
974 CreateGlobalInitOrCleanUpFunction(FTy
, "_GLOBAL__D_a", FI
);
976 CodeGenFunction(*this).GenerateCXXGlobalCleanUpFunc(
977 Fn
, CXXGlobalDtorsOrStermFinalizers
);
979 CXXGlobalDtorsOrStermFinalizers
.clear();
982 /// Emit the code necessary to initialize the given global variable.
983 void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function
*Fn
,
985 llvm::GlobalVariable
*Addr
,
987 // Check if we need to emit debug info for variable initializer.
988 if (D
->hasAttr
<NoDebugAttr
>())
989 DebugInfo
= nullptr; // disable debug info indefinitely for this function
991 CurEHLocation
= D
->getBeginLoc();
993 StartFunction(GlobalDecl(D
, DynamicInitKind::Initializer
),
994 getContext().VoidTy
, Fn
, getTypes().arrangeNullaryFunction(),
996 // Emit an artificial location for this function.
997 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
999 // Use guarded initialization if the global variable is weak. This
1000 // occurs for, e.g., instantiated static data members and
1001 // definitions explicitly marked weak.
1003 // Also use guarded initialization for a variable with dynamic TLS and
1004 // unordered initialization. (If the initialization is ordered, the ABI
1005 // layer will guard the whole-TU initialization for us.)
1006 if (Addr
->hasWeakLinkage() || Addr
->hasLinkOnceLinkage() ||
1007 (D
->getTLSKind() == VarDecl::TLS_Dynamic
&&
1008 isTemplateInstantiation(D
->getTemplateSpecializationKind()))) {
1009 EmitCXXGuardedInit(*D
, Addr
, PerformInit
);
1011 EmitCXXGlobalVarDeclInit(*D
, Addr
, PerformInit
);
1014 if (getLangOpts().HLSL
)
1015 CGM
.getHLSLRuntime().annotateHLSLResource(D
, Addr
);
1021 CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function
*Fn
,
1022 ArrayRef
<llvm::Function
*> Decls
,
1023 ConstantAddress Guard
) {
1025 auto NL
= ApplyDebugLocation::CreateEmpty(*this);
1026 StartFunction(GlobalDecl(), getContext().VoidTy
, Fn
,
1027 getTypes().arrangeNullaryFunction(), FunctionArgList());
1028 // Emit an artificial location for this function.
1029 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1031 llvm::BasicBlock
*ExitBlock
= nullptr;
1032 if (Guard
.isValid()) {
1033 // If we have a guard variable, check whether we've already performed
1034 // these initializations. This happens for TLS initialization functions.
1035 llvm::Value
*GuardVal
= Builder
.CreateLoad(Guard
);
1036 llvm::Value
*Uninit
= Builder
.CreateIsNull(GuardVal
,
1037 "guard.uninitialized");
1038 llvm::BasicBlock
*InitBlock
= createBasicBlock("init");
1039 ExitBlock
= createBasicBlock("exit");
1040 EmitCXXGuardedInitBranch(Uninit
, InitBlock
, ExitBlock
,
1041 GuardKind::TlsGuard
, nullptr);
1042 EmitBlock(InitBlock
);
1043 // Mark as initialized before initializing anything else. If the
1044 // initializers use previously-initialized thread_local vars, that's
1045 // probably supposed to be OK, but the standard doesn't say.
1046 Builder
.CreateStore(llvm::ConstantInt::get(GuardVal
->getType(),1), Guard
);
1048 // The guard variable can't ever change again.
1051 CharUnits::fromQuantity(
1052 CGM
.getDataLayout().getTypeAllocSize(GuardVal
->getType())));
1055 RunCleanupsScope
Scope(*this);
1057 // When building in Objective-C++ ARC mode, create an autorelease pool
1058 // around the global initializers.
1059 if (getLangOpts().ObjCAutoRefCount
&& getLangOpts().CPlusPlus
) {
1060 llvm::Value
*token
= EmitObjCAutoreleasePoolPush();
1061 EmitObjCAutoreleasePoolCleanup(token
);
1064 for (unsigned i
= 0, e
= Decls
.size(); i
!= e
; ++i
)
1066 EmitRuntimeCall(Decls
[i
]);
1068 Scope
.ForceCleanup();
1071 Builder
.CreateBr(ExitBlock
);
1072 EmitBlock(ExitBlock
);
1079 void CodeGenFunction::GenerateCXXGlobalCleanUpFunc(
1081 ArrayRef
<std::tuple
<llvm::FunctionType
*, llvm::WeakTrackingVH
,
1083 DtorsOrStermFinalizers
) {
1085 auto NL
= ApplyDebugLocation::CreateEmpty(*this);
1086 StartFunction(GlobalDecl(), getContext().VoidTy
, Fn
,
1087 getTypes().arrangeNullaryFunction(), FunctionArgList());
1088 // Emit an artificial location for this function.
1089 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1091 // Emit the cleanups, in reverse order from construction.
1092 for (unsigned i
= 0, e
= DtorsOrStermFinalizers
.size(); i
!= e
; ++i
) {
1093 llvm::FunctionType
*CalleeTy
;
1094 llvm::Value
*Callee
;
1095 llvm::Constant
*Arg
;
1096 std::tie(CalleeTy
, Callee
, Arg
) = DtorsOrStermFinalizers
[e
- i
- 1];
1098 llvm::CallInst
*CI
= nullptr;
1099 if (Arg
== nullptr) {
1101 CGM
.getCXXABI().useSinitAndSterm() &&
1102 "Arg could not be nullptr unless using sinit and sterm functions.");
1103 CI
= Builder
.CreateCall(CalleeTy
, Callee
);
1105 CI
= Builder
.CreateCall(CalleeTy
, Callee
, Arg
);
1107 // Make sure the call and the callee agree on calling convention.
1108 if (llvm::Function
*F
= dyn_cast
<llvm::Function
>(Callee
))
1109 CI
->setCallingConv(F
->getCallingConv());
1116 /// generateDestroyHelper - Generates a helper function which, when
1117 /// invoked, destroys the given object. The address of the object
1118 /// should be in global memory.
1119 llvm::Function
*CodeGenFunction::generateDestroyHelper(
1120 Address addr
, QualType type
, Destroyer
*destroyer
,
1121 bool useEHCleanupForArray
, const VarDecl
*VD
) {
1122 FunctionArgList args
;
1123 ImplicitParamDecl
Dst(getContext(), getContext().VoidPtrTy
,
1124 ImplicitParamDecl::Other
);
1125 args
.push_back(&Dst
);
1127 const CGFunctionInfo
&FI
=
1128 CGM
.getTypes().arrangeBuiltinFunctionDeclaration(getContext().VoidTy
, args
);
1129 llvm::FunctionType
*FTy
= CGM
.getTypes().GetFunctionType(FI
);
1130 llvm::Function
*fn
= CGM
.CreateGlobalInitOrCleanUpFunction(
1131 FTy
, "__cxx_global_array_dtor", FI
, VD
->getLocation());
1133 CurEHLocation
= VD
->getBeginLoc();
1135 StartFunction(GlobalDecl(VD
, DynamicInitKind::GlobalArrayDestructor
),
1136 getContext().VoidTy
, fn
, FI
, args
);
1137 // Emit an artificial location for this function.
1138 auto AL
= ApplyDebugLocation::CreateArtificial(*this);
1140 emitDestroy(addr
, type
, destroyer
, useEHCleanupForArray
);