1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with code generation of C++ declarations
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenCXXABI.h"
14#include "CIRGenFunction.h"
15#include "CIRGenModule.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Mangle.h"
18#include "clang/Basic/LangOptions.h"
19#include "clang/CIR/Dialect/IR/CIRAttrs.h"
20
21using namespace clang;
22using namespace clang::CIRGen;
23
24void CIRGenFunction::emitCXXGuardedInit(const VarDecl &varDecl,
25 cir::GlobalOp globalOp,
26 bool performInit) {
27 // If we've been asked to forbid guard variables, emit an error now.
28 // This diagnostic is hard-coded for Darwin's use case; we can find
29 // better phrasing if someone else needs it.
30 if (cgm.getCodeGenOpts().ForbidGuardVariables)
31 cgm.error(loc: varDecl.getLocation(), error: "guard variables are forbidden");
32
33 // Compute the mangled guard variable name and set the dynamic_init_guard
34 // attribute BEFORE emitting initialization. This ensures that GetGlobalOps
35 // created during initialization (e.g., in the ctor region) will see the
36 // attribute and be marked with static_local accordingly.
37 llvm::SmallString<256> guardName;
38 {
39 llvm::raw_svector_ostream out(guardName);
40 cgm.getCXXABI().getMangleContext().mangleStaticGuardVariable(D: &varDecl, out);
41 }
42
43 // Mark the global as requiring guarded dynamic initialization, with the
44 // guard name. The emission of the guard/acquire is done during
45 // LoweringPrepare.
46 auto guardAttr = mlir::StringAttr::get(context: &cgm.getMLIRContext(), bytes: guardName);
47
48 globalOp.setDynamicInitGuardAttr(
49 cir::DynamicInitGuardAttr::get(context: &cgm.getMLIRContext(), name: guardAttr));
50
51 if (varDecl.isStaticLocal())
52 cgm.emitCXXStaticLocalVarDeclInit(varDecl: &varDecl, addr: globalOp, performInit);
53 else
54 cgm.emitCXXGlobalVarDeclInit(varDecl: &varDecl, addr: globalOp, performInit);
55}
56
57void CIRGenModule::setGlobalTlsReferences(const VarDecl &vd,
58 cir::GlobalOp globalOp) {
59 assert(!vd.isStaticLocal() && vd.getTLSKind());
60
61 // C doesn't need guarded thread-local init, because it can't have
62 // non-constant init.
63 if (!getLangOpts().CPlusPlus)
64 return;
65
66 // TLS Static doesn't need a wrapper.
67 if (vd.getTLSKind() != VarDecl::TLS_Dynamic)
68 return;
69
70 llvm::SmallString<256> wrapperFuncName;
71 llvm::SmallString<256> initFuncName;
72 llvm::SmallString<256> guardName;
73
74 if (getCXXABI().getMangleContext().getKind() == MangleContext::MK_Itanium) {
75 llvm::raw_svector_ostream wrapperOut(wrapperFuncName);
76 llvm::raw_svector_ostream initOut(initFuncName);
77 llvm::raw_svector_ostream guardStream(guardName);
78
79 auto &mc = cast<ItaniumMangleContext>(Val&: getCXXABI().getMangleContext());
80 mc.mangleItaniumThreadLocalWrapper(D: &vd, wrapperOut);
81 mc.mangleItaniumThreadLocalInit(D: &vd, initOut);
82 if (globalOp.hasWeakLinkage() || globalOp.hasLinkOnceLinkage() ||
83 isTemplateInstantiation(Kind: vd.getTemplateSpecializationKind())) {
84 getCXXABI().getMangleContext().mangleStaticGuardVariable(D: &vd,
85 guardStream);
86 }
87
88 } else {
89 errorNYI(vd.getSourceRange(),
90 "setGlobalTlsReferences: non-itanium mangler");
91 return;
92 }
93 globalOp.setTlsRefsAttr(cir::ThreadLocalGlobalWrapperInitAttr::get(
94 context: &getMLIRContext(), wrapper: wrapperFuncName, init: initFuncName, guard: guardName));
95}
96
97void CIRGenModule::emitCXXGlobalVarDeclInitFunc(const VarDecl *vd,
98 cir::GlobalOp addr,
99 bool performInit) {
100 // Sema has already checked that the initializers of device-side variables
101 // are empty, so they can be ignored.
102 if (getLangOpts().CUDAIsDevice && !getLangOpts().GPUAllowDeviceInit &&
103 (vd->hasAttr<CUDADeviceAttr>() || vd->hasAttr<CUDAConstantAttr>() ||
104 vd->hasAttr<CUDASharedAttr>()))
105 return;
106
107 // Classic CodeGen dispatches guarded initialization through
108 // CGCXXABI::EmitGuardedInit, which the Microsoft ABI overrides with a
109 // completely different (bitmask-based "magic statics") guard scheme
110 // instead of the Itanium __cxa_guard_acquire/__cxa_guard_release calls
111 // that emitCXXGuardedInit/LoweringPrepare unconditionally assume here.
112 assert(!cir::MissingFeatures::msabi());
113
114 if (addr.hasWeakLinkage() || addr.hasLinkOnceLinkage() ||
115 (vd->getTLSKind() == VarDecl::TLS_Dynamic &&
116 isTemplateInstantiation(Kind: vd->getTemplateSpecializationKind()))) {
117 CIRGenFunction(*this, builder).emitCXXGuardedInit(varDecl: *vd, globalOp: addr, performInit);
118 } else {
119 emitCXXGlobalVarDeclInit(varDecl: vd, addr, performInit);
120 }
121}
122