Skip to content

Commit ddae8bc

Browse files
authored
Merge pull request #1412 from apple/PR-64909619
Fix Darwin 'constinit thread_local' variables.
2 parents 705d120 + 67da460 commit ddae8bc

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4065,17 +4065,24 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
40654065

40664066
GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
40674067

4068-
// On Darwin, if the normal linkage of a C++ thread_local variable is
4069-
// LinkOnce or Weak, we keep the normal linkage to prevent multiple
4070-
// copies within a linkage unit; otherwise, the backing variable has
4071-
// internal linkage and all accesses should just be calls to the
4072-
// Itanium-specified entry point, which has the normal linkage of the
4073-
// variable. This is to preserve the ability to change the implementation
4074-
// behind the scenes.
4075-
if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
4068+
// On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
4069+
// function is only defined alongside the variable, not also alongside
4070+
// callers. Normally, all accesses to a thread_local go through the
4071+
// thread-wrapper in order to ensure initialization has occurred, underlying
4072+
// variable will never be used other than the thread-wrapper, so it can be
4073+
// converted to internal linkage.
4074+
//
4075+
// However, if the variable has the 'constinit' attribute, it _can_ be
4076+
// referenced directly, without calling the thread-wrapper, so the linkage
4077+
// must not be changed.
4078+
//
4079+
// Additionally, if the variable isn't plain external linkage, e.g. if it's
4080+
// weak or linkonce, the de-duplication semantics are important to preserve,
4081+
// so we don't change the linkage.
4082+
if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
4083+
Linkage == llvm::GlobalValue::ExternalLinkage &&
40764084
Context.getTargetInfo().getTriple().isOSDarwin() &&
4077-
!llvm::GlobalVariable::isLinkOnceLinkage(Linkage) &&
4078-
!llvm::GlobalVariable::isWeakLinkage(Linkage))
4085+
!D->hasAttr<ConstInitAttr>())
40794086
Linkage = llvm::GlobalValue::InternalLinkage;
40804087

40814088
GV->setLinkage(Linkage);
Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,55 @@
1-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++2a %s -emit-llvm -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++2a %s -emit-llvm -o - | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
2+
// RUN: %clang_cc1 -triple x86_64-apple-darwin12 -std=c++2a %s -emit-llvm -o - | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
3+
4+
// Check variable definitions/declarations. Note that on Darwin, typically the
5+
// variable's symbol is marked internal, and only the _ZTW function is
6+
// exported. Except: constinit variables do get exported, even on darwin.
7+
8+
// CHECK-DAG: @a = external thread_local global i32
9+
// CHECK-DAG: @b = external thread_local global i32
10+
// LINUX-DAG: @c = thread_local global i32 0, align 4
11+
// DARWIN-DAG: @c = internal thread_local global i32 0, align 4
12+
// LINUX-DAG: @d = thread_local global i32 0, align 4
13+
// DARWIN-DAG: @d = internal thread_local global i32 0, align 4
14+
// CHECK-DAG: @e = external thread_local global %struct.Destructed, align 4
15+
// CHECK-DAG: @e2 = thread_local global %struct.Destructed zeroinitializer, align 4
16+
// CHECK-DAG: @f = thread_local global i32 4, align 4
217

3-
// CHECK-DAG: @a = external thread_local global i32
418
extern thread_local int a;
5-
6-
// CHECK-DAG: @b = external thread_local global i32
719
extern thread_local constinit int b;
820

9-
// CHECK-LABEL: define i32 @_Z1fv()
10-
// CHECK: call i32* @_ZTW1a()
21+
// CHECK-LABEL: define i32 @_Z5get_av()
22+
// CHECK: call {{(cxx_fast_tlscc )?}}i32* @_ZTW1a()
1123
// CHECK: }
12-
int f() { return a; }
24+
int get_a() { return a; }
1325

14-
// CHECK-LABEL: define linkonce_odr {{.*}} @_ZTW1a()
15-
// CHECK: br i1
16-
// CHECK: call void @_ZTH1a()
17-
// CHECK: }
26+
// LINUX-LABEL: define linkonce_odr {{.*}} @_ZTW1a()
27+
// LINUX: br i1
28+
// LINUX: call void @_ZTH1a()
29+
// LINUX: }
30+
// DARWIN-NOT: define {{.*}}@_ZTW1a()
1831

19-
// CHECK-LABEL: define i32 @_Z1gv()
32+
// CHECK-LABEL: define i32 @_Z5get_bv()
2033
// CHECK-NOT: call
2134
// CHECK: load i32, i32* @b
2235
// CHECK-NOT: call
2336
// CHECK: }
24-
int g() { return b; }
37+
int get_b() { return b; }
2538

2639
// CHECK-NOT: define {{.*}} @_ZTW1b()
2740

2841
extern thread_local int c;
2942

30-
// CHECK-LABEL: define i32 @_Z1hv()
31-
// CHECK: call i32* @_ZTW1c()
43+
// CHECK-LABEL: define i32 @_Z5get_cv()
44+
// LINUX: call {{(cxx_fast_tlscc )?}}i32* @_ZTW1c()
3245
// CHECK: load i32, i32* %
3346
// CHECK: }
34-
int h() { return c; }
47+
int get_c() { return c; }
3548

3649
// Note: use of 'c' does not trigger initialization of 'd', because 'c' has a
3750
// constant initializer.
38-
// CHECK-LABEL: define weak_odr {{.*}} @_ZTW1c()
51+
// DARWIN-LABEL: define cxx_fast_tlscc {{.*}} @_ZTW1c()
52+
// LINUX-LABEL: define weak_odr {{.*}} @_ZTW1c()
3953
// CHECK-NOT: br i1
4054
// CHECK-NOT: call
4155
// CHECK: ret i32* @c
@@ -55,15 +69,18 @@ struct Destructed {
5569
};
5670

5771
extern thread_local constinit Destructed e;
58-
// CHECK-LABEL: define i32 @_Z1iv()
72+
// CHECK-LABEL: define i32 @_Z5get_ev()
5973
// CHECK: call {{.*}}* @_ZTW1e()
6074
// CHECK: }
61-
int i() { return e.n; }
75+
int get_e() { return e.n; }
6276

6377
// CHECK: define {{.*}}[[E2_INIT:@__cxx_global_var_init[^(]*]](
64-
// CHECK: call {{.*}} @__cxa_thread_atexit({{.*}} @_ZN10DestructedD1Ev {{.*}} @e2
78+
// LINUX: call {{.*}} @__cxa_thread_atexit({{.*}} @_ZN10DestructedD1Ev {{.*}} @e2
79+
// DARWIN: call {{.*}} @_tlv_atexit({{.*}} @_ZN10DestructedD1Ev {{.*}} @e2
6580
thread_local constinit Destructed e2;
6681

82+
thread_local constinit int f = 4;
83+
6784
// CHECK-LABEL: define {{.*}}__tls_init
6885
// CHECK: call {{.*}} [[D_INIT]]
6986
// CHECK: call {{.*}} [[E2_INIT]]

0 commit comments

Comments
 (0)