1 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-linux-gnu -std=c++2a %s -emit-llvm -o - | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
2 // RUN: %clang_cc1 -no-opaque-pointers -triple x86_64-apple-darwin12 -std=c++2a %s -emit-llvm -o - | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
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.
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
18 extern thread_local
int a
;
19 extern thread_local constinit
int b
;
21 // CHECK-LABEL: define{{.*}} i32 @_Z5get_av()
22 // CHECK: call {{(cxx_fast_tlscc )?}}i32* @_ZTW1a()
24 int get_a() { return a
; }
26 // LINUX-LABEL: define linkonce_odr {{.*}} @_ZTW1a()
28 // LINUX: call void @_ZTH1a()
30 // DARWIN-NOT: define {{.*}}@_ZTW1a()
32 // CHECK-LABEL: define{{.*}} i32 @_Z5get_bv()
34 // CHECK: [[B_ADDR:%.+]] = call align 4 i32* @llvm.threadlocal.address.p0i32(i32* align 4 @b)
35 // CHECK: load i32, i32* [[B_ADDR]]
38 int get_b() { return b
; }
40 // CHECK-NOT: define {{.*}} @_ZTW1b()
42 extern thread_local
int c
;
44 // CHECK-LABEL: define{{.*}} i32 @_Z5get_cv()
45 // LINUX: call {{(cxx_fast_tlscc )?}}i32* @_ZTW1c()
46 // CHECK: load i32, i32* %
48 int get_c() { return c
; }
50 // Note: use of 'c' does not trigger initialization of 'd', because 'c' has a
51 // constant initializer.
52 // DARWIN-LABEL: define cxx_fast_tlscc {{.*}} @_ZTW1c()
53 // LINUX-LABEL: define weak_odr {{.*}} @_ZTW1c()
56 // CHECK: [[C_ADDR:%.+]] = call align 4 i32* @llvm.threadlocal.address.p0i32(i32* align 4 @c)
57 // CHECK: ret i32* [[C_ADDR]]
60 thread_local
int c
= 0;
62 // PR51079: We must assume an incomplete class type might have non-trivial
63 // destruction, and so speculatively call the thread wrapper.
65 // CHECK-LABEL: define {{.*}} @_Z6get_e3v(
66 // CHECK: call {{.*}}* @_ZTW2e3()
68 extern thread_local constinit
struct DestructedFwdDecl e3
;
69 DestructedFwdDecl
&get_e3() { return e3
; }
73 // CHECK: define {{.*}}[[D_INIT:@__cxx_global_var_init[^(]*]](
74 // CHECK: call {{.*}} @_Z6d_initv()
75 thread_local
int d
= d_init();
82 extern thread_local constinit Destructed e
;
83 // CHECK-LABEL: define{{.*}} i32 @_Z5get_ev()
84 // CHECK: call {{.*}}* @_ZTW1e()
86 int get_e() { return e
.n
; }
88 // CHECK: define {{.*}}[[E2_INIT:@__cxx_global_var_init[^(]*]](
89 // LINUX: call {{.*}} @__cxa_thread_atexit({{.*}} @_ZN10DestructedD1Ev {{.*}} @e2
90 // DARWIN: call {{.*}} @_tlv_atexit({{.*}} @_ZN10DestructedD1Ev {{.*}} @e2
91 thread_local constinit Destructed e2
;
93 thread_local constinit
int f
= 4;
95 // CHECK-LABEL: define {{.*}}__tls_init
96 // CHECK: call {{.*}} [[D_INIT]]
97 // CHECK: call {{.*}} [[E2_INIT]]
99 // Because the call wrapper may be called speculatively (and simply because
100 // it's required by the ABI), it must always be emitted for an external linkage
101 // variable, even if the variable has constant initialization and constant
103 struct NotDestructed
{ int n
= 0; };
104 thread_local constinit NotDestructed nd
;
105 // CHECK-LABEL: define {{.*}} @_ZTW2nd