Skip to content

Commit 05542b6

Browse files
committed
[C++20] [Modules] Don't generate the defintition for non-const available external variables
Close #93497 The root cause of the problem is, we mark the variable from other modules as constnant in LLVM incorrectly. This patch fixes this problem by not emitting the defintition for non-const available external variables. Since the non const available externally variable is not helpful to the optimization.
1 parent ea20647 commit 05542b6

File tree

3 files changed

+96
-4
lines changed

3 files changed

+96
-4
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5341,6 +5341,15 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
53415341
!IsDefinitionAvailableExternally &&
53425342
D->needsDestruction(getContext()) == QualType::DK_cxx_destructor;
53435343

5344+
// It is helpless to emit the definition for an available_externally variable
5345+
// which can't be marked as const.
5346+
// We don't need to check if it needs global ctor or dtor. See the above
5347+
// comment for ideas.
5348+
if (IsDefinitionAvailableExternally &&
5349+
(!D->hasConstantInitialization() ||
5350+
!D->getType().isConstantStorage(getContext(), true, true)))
5351+
return;
5352+
53445353
const VarDecl *InitDecl;
53455354
const Expr *InitExpr = D->getAnyInitializer(InitDecl);
53465355

clang/test/CodeGenCXX/partitions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ export int use() {
4040
}
4141

4242
// FIXME: The definition of the variables shouldn't be exported too.
43-
// CHECK: @_ZW3mod1a = available_externally global
44-
// CHECK: @_ZW3mod1b = available_externally global
43+
// CHECK: @_ZW3mod1a = external global
44+
// CHECK: @_ZW3mod1b = external global
4545
// CHECK: declare{{.*}} i32 @_ZW3mod3foov
4646
// CHECK: declare{{.*}} i32 @_ZW3mod3barv
4747

48-
// CHECK-OPT: @_ZW3mod1a = available_externally global
49-
// CHECK-OPT: @_ZW3mod1b = available_externally global
48+
// CHECK-OPT: @_ZW3mod1a = external global
49+
// CHECK-OPT: @_ZW3mod1b = external global
5050
// CHECK-OPT: declare{{.*}} i32 @_ZW3mod3foov
5151
// CHECK-OPT: declare{{.*}} i32 @_ZW3mod3barv

clang/test/Modules/pr93497.cppm

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
// RUN: split-file %s %t
4+
5+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/mod.cppm \
6+
// RUN: -emit-module-interface -o %t/mod.pcm
7+
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %t/use.cpp \
8+
// RUN: -fmodule-file=mod=%t/mod.pcm -emit-llvm \
9+
// RUN: -o - | FileCheck %t/use.cpp
10+
11+
//--- mod.cppm
12+
export module mod;
13+
14+
export struct Thing {
15+
static const Thing One;
16+
explicit Thing(int raw) :raw(raw) { }
17+
int raw;
18+
};
19+
20+
const Thing Thing::One = Thing(1);
21+
22+
export struct C {
23+
int value;
24+
};
25+
export const C ConstantValue = {1};
26+
27+
export const C *ConstantPtr = &ConstantValue;
28+
29+
C NonConstantValue = {1};
30+
export const C &ConstantRef = NonConstantValue;
31+
32+
//--- use.cpp
33+
import mod;
34+
35+
int consume(int);
36+
int consumeC(C);
37+
38+
extern "C" __attribute__((noinline)) inline int unneeded() {
39+
return consume(43);
40+
}
41+
42+
extern "C" __attribute__((noinline)) inline int needed() {
43+
return consume(43);
44+
}
45+
46+
int use() {
47+
Thing t1 = Thing::One;
48+
return consume(t1.raw);
49+
}
50+
51+
int use2() {
52+
if (ConstantValue.value)
53+
return consumeC(ConstantValue);
54+
return unneeded();
55+
}
56+
57+
int use3() {
58+
if (ConstantPtr->value)
59+
return consumeC(*ConstantPtr);
60+
return needed();
61+
}
62+
63+
int use4() {
64+
if (ConstantRef.value)
65+
return consumeC(ConstantRef);
66+
return needed();
67+
}
68+
69+
// CHECK: @_ZNW3mod5Thing3OneE = external
70+
// CHECK: @_ZW3mod13ConstantValue ={{.*}}available_externally{{.*}} constant
71+
// CHECK: @_ZW3mod11ConstantPtr = external
72+
// CHECK: @_ZW3mod16NonConstantValue = external
73+
74+
// Check that the middle end can optimize the program by the constant information.
75+
// CHECK-NOT: @unneeded(
76+
77+
// Check that the use of ConstantPtr won't get optimized incorrectly.
78+
// CHECK-LABEL: @_Z4use3v(
79+
// CHECK: @needed(
80+
81+
// Check that the use of ConstantRef won't get optimized incorrectly.
82+
// CHECK-LABEL: @_Z4use4v(
83+
// CHECK: @needed(

0 commit comments

Comments
 (0)