CGDeclCXX.cpp 45 KB

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