1 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
2 // RUN: %clang_cc1 -std=c++11 -femulated-tls -emit-llvm %s -o - \
3 // RUN:     -triple x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
4 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-apple-darwin12 | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
5 
6 int f();
7 int g();
8 
9 // LINUX: @a = thread_local global i32 0
10 // DARWIN: @a = internal thread_local global i32 0
11 thread_local int a = f();
12 extern thread_local int b;
13 // CHECK: @c = global i32 0
14 int c = b;
15 // CHECK: @_ZL1d = internal thread_local global i32 0
16 static thread_local int d = g();
17 
18 struct U { static thread_local int m; };
19 // LINUX: @_ZN1U1mE = thread_local global i32 0
20 // DARWIN: @_ZN1U1mE = internal thread_local global i32 0
21 thread_local int U::m = f();
22 
23 namespace MismatchedInitType {
24   // Check that we don't crash here when we're forced to create a new global
25   // variable (with a different type) when we add the initializer.
26   union U {
27     int a;
28     float f;
U()29     constexpr U() : f(0.0) {}
30   };
31   static thread_local U u;
32   void *p = &u;
33 }
34 
35 template<typename T> struct V { static thread_local int m; };
36 template<typename T> thread_local int V<T>::m = g();
37 
38 // CHECK: @e = global i32 0
39 int e = V<int>::m;
40 
41 // CHECK: @_ZN1VIiE1mE = linkonce_odr thread_local global i32 0
42 
43 // CHECK: @_ZZ1fvE1n = internal thread_local global i32 0
44 
45 // CHECK: @_ZGVZ1fvE1n = internal thread_local global i8 0
46 
47 // CHECK: @_ZZ8tls_dtorvE1s = internal thread_local global
48 // CHECK: @_ZGVZ8tls_dtorvE1s = internal thread_local global i8 0
49 
50 // CHECK: @_ZZ8tls_dtorvE1t = internal thread_local global
51 // CHECK: @_ZGVZ8tls_dtorvE1t = internal thread_local global i8 0
52 
53 // CHECK: @_ZZ8tls_dtorvE1u = internal thread_local global
54 // CHECK: @_ZGVZ8tls_dtorvE1u = internal thread_local global i8 0
55 // CHECK: @_ZGRZ8tls_dtorvE1u_ = internal thread_local global
56 
57 // CHECK: @_ZGVN1VIiE1mE = linkonce_odr thread_local global i64 0
58 
59 // CHECK: @__tls_guard = internal thread_local global i8 0
60 
61 // CHECK: @llvm.global_ctors = appending global {{.*}} @[[GLOBAL_INIT:[^ ]*]]
62 
63 // LINUX: @_ZTH1a = alias void (), void ()* @__tls_init
64 // DARWIN: @_ZTH1a = internal alias void (), void ()* @__tls_init
65 // CHECK: @_ZTHL1d = internal alias void (), void ()* @__tls_init
66 // LINUX: @_ZTHN1U1mE = alias void (), void ()* @__tls_init
67 // DARWIN: @_ZTHN1U1mE = internal alias void (), void ()* @__tls_init
68 // CHECK: @_ZTHN1VIiE1mE = linkonce_odr alias void (), void ()* @__tls_init
69 
70 
71 // Individual variable initialization functions:
72 
73 // CHECK: define {{.*}} @[[A_INIT:.*]]()
74 // CHECK: call i32 @_Z1fv()
75 // CHECK-NEXT: store i32 {{.*}}, i32* @a, align 4
76 
77 // CHECK-LABEL: define i32 @_Z1fv()
f()78 int f() {
79   // CHECK: %[[GUARD:.*]] = load i8, i8* @_ZGVZ1fvE1n, align 1
80   // CHECK: %[[NEED_INIT:.*]] = icmp eq i8 %[[GUARD]], 0
81   // CHECK: br i1 %[[NEED_INIT]]
82 
83   // CHECK: %[[CALL:.*]] = call i32 @_Z1gv()
84   // CHECK: store i32 %[[CALL]], i32* @_ZZ1fvE1n, align 4
85   // CHECK: store i8 1, i8* @_ZGVZ1fvE1n
86   // CHECK: br label
87   static thread_local int n = g();
88 
89   // CHECK: load i32, i32* @_ZZ1fvE1n, align 4
90   return n;
91 }
92 
93 // CHECK: define {{.*}} @[[C_INIT:.*]]()
94 // LINUX: call i32* @_ZTW1b()
95 // DARWIN: call cxx_fast_tlscc i32* @_ZTW1b()
96 // CHECK-NEXT: load i32, i32* %{{.*}}, align 4
97 // CHECK-NEXT: store i32 %{{.*}}, i32* @c, align 4
98 
99 // LINUX-LABEL: define weak_odr hidden i32* @_ZTW1b()
100 // LINUX: br i1 icmp ne (void ()* @_ZTH1b, void ()* null),
101 // not null:
102 // LINUX: call void @_ZTH1b()
103 // LINUX: br label
104 // finally:
105 // LINUX: ret i32* @b
106 // DARWIN-LABEL: declare cxx_fast_tlscc i32* @_ZTW1b()
107 // There is no definition of the thread wrapper on Darwin for external TLV.
108 
109 // CHECK: define {{.*}} @[[D_INIT:.*]]()
110 // CHECK: call i32 @_Z1gv()
111 // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZL1d, align 4
112 
113 // CHECK: define {{.*}} @[[U_M_INIT:.*]]()
114 // CHECK: call i32 @_Z1fv()
115 // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZN1U1mE, align 4
116 
117 // CHECK: define {{.*}} @[[E_INIT:.*]]()
118 // LINUX: call i32* @_ZTWN1VIiE1mE()
119 // DARWIN: call cxx_fast_tlscc i32* @_ZTWN1VIiE1mE()
120 // CHECK-NEXT: load i32, i32* %{{.*}}, align 4
121 // CHECK-NEXT: store i32 %{{.*}}, i32* @e, align 4
122 
123 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1VIiE1mE()
124 // DARWIN-LABEL: define weak_odr hidden cxx_fast_tlscc i32* @_ZTWN1VIiE1mE()
125 // CHECK: call void @_ZTHN1VIiE1mE()
126 // CHECK: ret i32* @_ZN1VIiE1mE
127 
128 
129 struct S { S(); ~S(); };
130 struct T { ~T(); };
131 
132 // CHECK-LABEL: define void @_Z8tls_dtorv()
tls_dtor()133 void tls_dtor() {
134   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1s
135   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZZ8tls_dtorvE1s)
136   // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
137   // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
138   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1s
139   static thread_local S s;
140 
141   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1t
142   // CHECK-NOT: _ZN1T
143   // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
144   // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
145   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1t
146   static thread_local T t;
147 
148   // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1u
149   // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZGRZ8tls_dtorvE1u_)
150   // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
151   // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
152   // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1u
153   static thread_local const S &u = S();
154 }
155 
156 // LINUX: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
157 // DARWIN: declare i32 @_tlv_atexit(void (i8*)*, i8*, i8*)
158 
159 // CHECK: define {{.*}} @_Z7PR15991v(
PR15991()160 int PR15991() {
161   thread_local int n;
162   auto l = [] { return n; };
163   return l();
164 }
165 
166 struct PR19254 {
167   static thread_local int n;
168   int f();
169 };
170 // CHECK: define {{.*}} @_ZN7PR192541fEv(
f()171 int PR19254::f() {
172   // LINUX: call void @_ZTHN7PR192541nE(
173   // DARWIN: call cxx_fast_tlscc i32* @_ZTWN7PR192541nE(
174   return this->n;
175 }
176 
177 namespace {
178 thread_local int anon_i{1};
179 }
set_anon_i()180 void set_anon_i() {
181   anon_i = 2;
182 }
183 // LINUX-LABEL: define internal i32* @_ZTWN12_GLOBAL__N_16anon_iE()
184 // DARWIN-LABEL: define internal cxx_fast_tlscc i32* @_ZTWN12_GLOBAL__N_16anon_iE()
185 
186 // CHECK: define {{.*}} @[[V_M_INIT:.*]]()
187 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1VIiE1mE to i8*)
188 // CHECK: %[[V_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
189 // CHECK: br i1 %[[V_M_INITIALIZED]],
190 // need init:
191 // CHECK: call i32 @_Z1gv()
192 // CHECK: store i32 %{{.*}}, i32* @_ZN1VIiE1mE, align 4
193 // CHECK: store i64 1, i64* @_ZGVN1VIiE1mE
194 // CHECK: br label
195 
196 // CHECK: define {{.*}}@[[GLOBAL_INIT:.*]]()
197 // CHECK: call void @[[C_INIT]]()
198 // CHECK: call void @[[E_INIT]]()
199 
200 
201 // CHECK: define {{.*}}@__tls_init()
202 // CHECK: load i8, i8* @__tls_guard
203 // CHECK: %[[NEED_TLS_INIT:.*]] = icmp eq i8 %{{.*}}, 0
204 // CHECK: br i1 %[[NEED_TLS_INIT]],
205 // init:
206 // CHECK: store i8 1, i8* @__tls_guard
207 // CHECK: call void @[[A_INIT]]()
208 // CHECK: call void @[[D_INIT]]()
209 // CHECK: call void @[[U_M_INIT]]()
210 // CHECK: call void @[[V_M_INIT]]()
211 
212 
213 // LIUNX: define weak_odr hidden i32* @_ZTW1a() {
214 // DARWIN: define cxx_fast_tlscc i32* @_ZTW1a()
215 // CHECK:   call void @_ZTH1a()
216 // CHECK:   ret i32* @a
217 // CHECK: }
218 
219 
220 // LINUX: declare extern_weak void @_ZTH1b()
221 
222 
223 // LINUX-LABEL: define internal i32* @_ZTWL1d()
224 // DARWIN-LABEL: define internal cxx_fast_tlscc i32* @_ZTWL1d()
225 // CHECK: call void @_ZTHL1d()
226 // CHECK: ret i32* @_ZL1d
227 
228 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
229 // DARWIN-LABEL: define cxx_fast_tlscc i32* @_ZTWN1U1mE()
230 // CHECK: call void @_ZTHN1U1mE()
231 // CHECK: ret i32* @_ZN1U1mE
232