Skip to content

Commit ed655f8

Browse files
authored
Add clang patches to make globals used for array initialization codegen constant (#441)
Backport llvm/llvm-project@7a85aa9 and llvm/llvm-project@4a2757d
1 parent dff6760 commit ed655f8

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
From 1ad05dc3517a7cf8224c2866c0f7fb09246d4ee3 Mon Sep 17 00:00:00 2001
2+
From: Haonan Yang <[email protected]>
3+
Date: Wed, 17 May 2023 12:48:42 +0800
4+
Subject: [PATCH] Make globals used for array initialization codegen constant
5+
6+
This combines https://reviews.llvm.org/D146211 and https://reviews.llvm.org/D145369
7+
8+
1. Emit const globals with constexpr destructor as constant LLVM values
9+
This follows 2b4fa53 which made Clang not emit destructor calls for such
10+
objects. However, they would still not get emitted as constants since
11+
CodeGenModule::isTypeConstant() returns false if the destructor is
12+
constexpr. This change adds a param to make isTypeConstant() ignore the
13+
dtor, allowing the caller to check it instead.
14+
2. Make globals used for array initialization codegen constant
15+
As pointed out in D133835 these globals will never be written to
16+
(they're only used for trivially copyable types), so they can always be
17+
constant.
18+
---
19+
clang/lib/CodeGen/CGDecl.cpp | 11 +++++++----
20+
clang/lib/CodeGen/CGDeclCXX.cpp | 4 +++-
21+
clang/lib/CodeGen/CGExpr.cpp | 2 +-
22+
clang/lib/CodeGen/CGExprAgg.cpp | 4 ++--
23+
clang/lib/CodeGen/CGExprConstant.cpp | 12 ++++++------
24+
clang/lib/CodeGen/CodeGenModule.cpp | 16 +++++++++-------
25+
clang/lib/CodeGen/CodeGenModule.h | 2 +-
26+
clang/lib/CodeGen/TargetInfo.cpp | 2 +-
27+
clang/test/CodeGen/init.c | 2 +-
28+
clang/test/CodeGen/label-array-aggregate-init.c | 2 +-
29+
clang/test/CodeGenCXX/const-init-cxx2a.cpp | 4 ++--
30+
11 files changed, 34 insertions(+), 27 deletions(-)
31+
32+
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
33+
index 1729c7ed3c31..76a8d24af147 100644
34+
--- a/clang/lib/CodeGen/CGDecl.cpp
35+
+++ b/clang/lib/CodeGen/CGDecl.cpp
36+
@@ -367,13 +367,14 @@ CodeGenFunction::AddInitializerToStaticVarDecl(const VarDecl &D,
37+
OldGV->eraseFromParent();
38+
}
39+
40+
- GV->setConstant(CGM.isTypeConstant(D.getType(), true));
41+
+ bool NeedsDtor =
42+
+ D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
43+
+ GV->setConstant(CGM.isTypeConstant(D.getType(), true, !NeedsDtor));
44+
GV->setInitializer(Init);
45+
46+
emitter.finalize(GV);
47+
48+
- if (D.needsDestruction(getContext()) == QualType::DK_cxx_destructor &&
49+
- HaveInsertPoint()) {
50+
+ if (NeedsDtor && HaveInsertPoint()) {
51+
// We have a constant initializer, but a nontrivial destructor. We still
52+
// need to perform a guarded "initialization" in order to register the
53+
// destructor.
54+
@@ -1433,10 +1434,12 @@ CodeGenFunction::EmitAutoVarAlloca(const VarDecl &D) {
55+
// emit it as a global instead.
56+
// Exception is if a variable is located in non-constant address space
57+
// in OpenCL.
58+
+ bool NeedsDtor =
59+
+ D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
60+
if ((!getLangOpts().OpenCL ||
61+
Ty.getAddressSpace() == LangAS::opencl_constant) &&
62+
(CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
63+
- !isEscapingByRef && CGM.isTypeConstant(Ty, true))) {
64+
+ !isEscapingByRef && CGM.isTypeConstant(Ty, true, !NeedsDtor))) {
65+
EmitStaticVarDecl(D, llvm::GlobalValue::InternalLinkage);
66+
67+
// Signal this condition to later callbacks.
68+
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
69+
index 5a8500364295..7fd77d17e4b0 100644
70+
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
71+
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
72+
@@ -211,9 +211,11 @@ void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D,
73+
&D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
74+
PerformInit, this);
75+
}
76+
+ bool NeedsDtor =
77+
+ D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
78+
if (PerformInit)
79+
EmitDeclInit(*this, D, DeclAddr);
80+
- if (CGM.isTypeConstant(D.getType(), true))
81+
+ if (CGM.isTypeConstant(D.getType(), true, !NeedsDtor))
82+
EmitDeclInvariant(*this, D, DeclPtr);
83+
else
84+
EmitDeclDestroy(*this, D, DeclAddr);
85+
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
86+
index 9e8770573d70..2abf472e499d 100644
87+
--- a/clang/lib/CodeGen/CGExpr.cpp
88+
+++ b/clang/lib/CodeGen/CGExpr.cpp
89+
@@ -394,7 +394,7 @@ static Address createReferenceTemporary(CodeGenFunction &CGF,
90+
QualType Ty = Inner->getType();
91+
if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
92+
(Ty->isArrayType() || Ty->isRecordType()) &&
93+
- CGF.CGM.isTypeConstant(Ty, true))
94+
+ CGF.CGM.isTypeConstant(Ty, true, false))
95+
if (auto Init = ConstantEmitter(CGF).tryEmitAbstract(Inner, Ty)) {
96+
if (auto AddrSpace = CGF.getTarget().getConstantAddressSpace()) {
97+
auto AS = AddrSpace.getValue();
98+
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
99+
index fb96d70732e8..10b8ba7f5301 100644
100+
--- a/clang/lib/CodeGen/CGExprAgg.cpp
101+
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
102+
@@ -502,8 +502,8 @@ void AggExprEmitter::EmitArrayInit(Address DestPtr, llvm::ArrayType *AType,
103+
if (llvm::Constant *C = Emitter.tryEmitForInitializer(E, AS, ArrayQTy)) {
104+
auto GV = new llvm::GlobalVariable(
105+
CGM.getModule(), C->getType(),
106+
- CGM.isTypeConstant(ArrayQTy, /* ExcludeCtorDtor= */ true),
107+
- llvm::GlobalValue::PrivateLinkage, C, "constinit",
108+
+ /* isConstant= */ true, llvm::GlobalValue::PrivateLinkage, C,
109+
+ "constinit",
110+
/* InsertBefore= */ nullptr, llvm::GlobalVariable::NotThreadLocal,
111+
CGM.getContext().getTargetAddressSpace(AS));
112+
Emitter.finalize(GV);
113+
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
114+
index c6b2930faece..48c5911cd42a 100644
115+
--- a/clang/lib/CodeGen/CGExprConstant.cpp
116+
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
117+
@@ -912,12 +912,12 @@ static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM,
118+
return ConstantAddress::invalid();
119+
}
120+
121+
- auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
122+
- CGM.isTypeConstant(E->getType(), true),
123+
- llvm::GlobalValue::InternalLinkage,
124+
- C, ".compoundliteral", nullptr,
125+
- llvm::GlobalVariable::NotThreadLocal,
126+
- CGM.getContext().getTargetAddressSpace(addressSpace));
127+
+ auto GV = new llvm::GlobalVariable(
128+
+ CGM.getModule(), C->getType(),
129+
+ CGM.isTypeConstant(E->getType(), true, false),
130+
+ llvm::GlobalValue::InternalLinkage, C, ".compoundliteral", nullptr,
131+
+ llvm::GlobalVariable::NotThreadLocal,
132+
+ CGM.getContext().getTargetAddressSpace(addressSpace));
133+
emitter.finalize(GV);
134+
GV->setAlignment(Align.getAsAlign());
135+
CGM.setAddrOfConstantCompoundLiteral(E, GV);
136+
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
137+
index 4ae8ce7e5ccf..a596607e5370 100644
138+
--- a/clang/lib/CodeGen/CodeGenModule.cpp
139+
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
140+
@@ -2430,7 +2430,7 @@ bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
141+
// codegen for global variables, because they may be marked as threadprivate.
142+
if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
143+
getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
144+
- !isTypeConstant(Global->getType(), false) &&
145+
+ !isTypeConstant(Global->getType(), false, false) &&
146+
!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
147+
return false;
148+
149+
@@ -3436,8 +3436,9 @@ CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
150+
///
151+
/// If ExcludeCtor is true, the duration when the object's constructor runs
152+
/// will not be considered. The caller will need to verify that the object is
153+
-/// not written to during its construction.
154+
-bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
155+
+/// not written to during its construction. ExcludeDtor works similarly.
156+
+bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor,
157+
+ bool ExcludeDtor) {
158+
if (!Ty.isConstant(Context) && !Ty->isReferenceType())
159+
return false;
160+
161+
@@ -3445,7 +3446,7 @@ bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
162+
if (const CXXRecordDecl *Record
163+
= Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
164+
return ExcludeCtor && !Record->hasMutableFields() &&
165+
- Record->hasTrivialDestructor();
166+
+ (Record->hasTrivialDestructor() || ExcludeDtor);
167+
}
168+
169+
return true;
170+
@@ -3555,7 +3556,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
171+
172+
// FIXME: This code is overly simple and should be merged with other global
173+
// handling.
174+
- GV->setConstant(isTypeConstant(D->getType(), false));
175+
+ GV->setConstant(isTypeConstant(D->getType(), false, false));
176+
177+
GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
178+
179+
@@ -4123,7 +4124,7 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
180+
181+
// If it is safe to mark the global 'constant', do so now.
182+
GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
183+
- isTypeConstant(D->getType(), true));
184+
+ isTypeConstant(D->getType(), true, true));
185+
186+
// If it is in a read-only section, mark it 'constant'.
187+
if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
188+
@@ -5160,7 +5161,8 @@ ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary(
189+
emitter.emplace(*this);
190+
InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
191+
MaterializedType);
192+
- Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
193+
+ Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/ Value,
194+
+ /*ExcludeDtor*/ false);
195+
Type = InitialValue->getType();
196+
} else {
197+
// No initializer, the initialization will be provided when we
198+
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
199+
index a6c4a1f7b278..b4021d587eee 100644
200+
--- a/clang/lib/CodeGen/CodeGenModule.h
201+
+++ b/clang/lib/CodeGen/CodeGenModule.h
202+
@@ -758,7 +758,7 @@ public:
203+
return getTBAAAccessInfo(AccessType);
204+
}
205+
206+
- bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor);
207+
+ bool isTypeConstant(QualType QTy, bool ExcludeCtor, bool ExcludeDtor);
208+
209+
bool isPaddedAtomicType(QualType type);
210+
bool isPaddedAtomicType(const AtomicType *type);
211+
diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
212+
index a061651d8b21..e41f1620939d 100644
213+
--- a/clang/lib/CodeGen/TargetInfo.cpp
214+
+++ b/clang/lib/CodeGen/TargetInfo.cpp
215+
@@ -9059,7 +9059,7 @@ AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
216+
if (AddrSpace != LangAS::Default)
217+
return AddrSpace;
218+
219+
- if (CGM.isTypeConstant(D->getType(), false)) {
220+
+ if (CGM.isTypeConstant(D->getType(), false, false)) {
221+
if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
222+
return ConstAS.getValue();
223+
}
224+
diff --git a/clang/test/CodeGen/init.c b/clang/test/CodeGen/init.c
225+
index 71aba39b1244..a1ce9e6779b3 100644
226+
--- a/clang/test/CodeGen/init.c
227+
+++ b/clang/test/CodeGen/init.c
228+
@@ -10,7 +10,7 @@ unsigned v2[2][3] = {[0 ... 1][0 ... 1] = 2222, 3333};
229+
230+
// CHECK-DAG: [1 x %struct.M] [%struct.M { [2 x %struct.I] [%struct.I { [3 x i32] [i32 4, i32 4, i32 0] }, %struct.I { [3 x i32] [i32 4, i32 4, i32 5] }] }],
231+
// CHECK-DAG: [2 x [3 x i32]] {{[[][[]}}3 x i32] [i32 2222, i32 2222, i32 0], [3 x i32] [i32 2222, i32 2222, i32 3333]],
232+
-// CHECK-DAG: [[INIT14:.*]] = private global [16 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 0, i32 0, i32 0, i32 0], align 4
233+
+// CHECK-DAG: [[INIT14:.*]] = private constant [16 x i32] [i32 0, i32 0, i32 0, i32 0, i32 0, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 17, i32 0, i32 0, i32 0, i32 0], align 4
234+
235+
void f1() {
236+
// Scalars in braces.
237+
diff --git a/clang/test/CodeGen/label-array-aggregate-init.c b/clang/test/CodeGen/label-array-aggregate-init.c
238+
index 5cefd8d270c0..3175c2a6a292 100644
239+
--- a/clang/test/CodeGen/label-array-aggregate-init.c
240+
+++ b/clang/test/CodeGen/label-array-aggregate-init.c
241+
@@ -1,6 +1,6 @@
242+
// RUN: %clang -cc1 -triple x86_64-windows-msvc -emit-llvm %s -o - | FileCheck %s
243+
244+
-// CHECK: @constinit = private global [3 x i8*] [i8* blockaddress(@main, %L), i8* null, i8* null]
245+
+// CHECK: @constinit = private constant [3 x i8*] [i8* blockaddress(@main, %L), i8* null, i8* null]
246+
247+
void receivePtrs(void **);
248+
249+
diff --git a/clang/test/CodeGenCXX/const-init-cxx2a.cpp b/clang/test/CodeGenCXX/const-init-cxx2a.cpp
250+
index 1195b912c255..abe3eb2d0c60 100644
251+
--- a/clang/test/CodeGenCXX/const-init-cxx2a.cpp
252+
+++ b/clang/test/CodeGenCXX/const-init-cxx2a.cpp
253+
@@ -11,10 +11,10 @@ struct B {
254+
constexpr ~B() { n *= 5; }
255+
int n = 123;
256+
};
257+
-// CHECK: @b = global {{.*}} i32 123
258+
+// CHECK: @b = constant {{.*}} i32 123
259+
extern constexpr B b = B();
260+
261+
-// CHECK: @_ZL1c = internal global {{.*}} i32 123
262+
+// CHECK: @_ZL1c = internal constant {{.*}} i32 123
263+
const B c;
264+
int use_c() { return c.n; }
265+
266+
--
267+
2.31.1
268+

0 commit comments

Comments
 (0)