Skip to content

Commit aca3d06

Browse files
committed
Fix Darwin 'constinit thread_local' variables.
Unlike other platforms using ItaniumCXXABI, Darwin does not allow the creation of a thread-wrapper function for a variable in the TU of users. Because of this, it can set the linkage of the thread-local symbol to internal, with the assumption that no TUs other than the one defining the variable will need it. However, constinit thread_local variables do not require the use of the thread-wrapper call, so users reference the variable directly. Thus, it must not be converted to internal, or users will get a link failure. This was a regression introduced by the optimization in 0022382. Differential Revision: https://reviews.llvm.org/D80417
1 parent 6022efb commit aca3d06

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
@@ -4136,17 +4136,24 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
41364136

41374137
GV->setAlignment(getContext().getDeclAlign(D).getAsAlign());
41384138

4139-
// On Darwin, if the normal linkage of a C++ thread_local variable is
4140-
// LinkOnce or Weak, we keep the normal linkage to prevent multiple
4141-
// copies within a linkage unit; otherwise, the backing variable has
4142-
// internal linkage and all accesses should just be calls to the
4143-
// Itanium-specified entry point, which has the normal linkage of the
4144-
// variable. This is to preserve the ability to change the implementation
4145-
// behind the scenes.
4146-
if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
4139+
// On Darwin, unlike other Itanium C++ ABI platforms, the thread-wrapper
4140+
// function is only defined alongside the variable, not also alongside
4141+
// callers. Normally, all accesses to a thread_local go through the
4142+
// thread-wrapper in order to ensure initialization has occurred, underlying
4143+
// variable will never be used other than the thread-wrapper, so it can be
4144+
// converted to internal linkage.
4145+
//
4146+
// However, if the variable has the 'constinit' attribute, it _can_ be
4147+
// referenced directly, without calling the thread-wrapper, so the linkage
4148+
// must not be changed.
4149+
//
4150+
// Additionally, if the variable isn't plain external linkage, e.g. if it's
4151+
// weak or linkonce, the de-duplication semantics are important to preserve,
4152+
// so we don't change the linkage.
4153+
if (D->getTLSKind() == VarDecl::TLS_Dynamic &&
4154+
Linkage == llvm::GlobalValue::ExternalLinkage &&
41474155
Context.getTargetInfo().getTriple().isOSDarwin() &&
4148-
!llvm::GlobalVariable::isLinkOnceLinkage(Linkage) &&
4149-
!llvm::GlobalVariable::isWeakLinkage(Linkage))
4156+
!D->hasAttr<ConstInitAttr>())
41504157
Linkage = llvm::GlobalValue::InternalLinkage;
41514158

41524159
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)