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 -emit-llvm %s -O2 -disable-llvm-passes -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX --check-prefix=CHECK-OPT %s
3 // RUN: %clang_cc1 -std=c++11 -femulated-tls -emit-llvm %s -o - \
4 // RUN: -triple x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
5 // RUN: %clang_cc1 -std=c++11 -emit-llvm %s -o - -triple x86_64-apple-darwin12 | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
6
7 // RUN: %clang_cc1 -std=c++11 -fno-use-cxa-atexit -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
8 // RUN: %clang_cc1 -std=c++11 -fno-use-cxa-atexit -emit-llvm %s -O2 -disable-llvm-passes -o - -triple x86_64-linux-gnu | FileCheck --check-prefix=CHECK --check-prefix=LINUX --check-prefix=CHECK-OPT %s
9 // RUN: %clang_cc1 -std=c++11 -fno-use-cxa-atexit -femulated-tls -emit-llvm %s -o - \
10 // RUN: -triple x86_64-linux-gnu 2>&1 | FileCheck --check-prefix=CHECK --check-prefix=LINUX %s
11 // RUN: %clang_cc1 -std=c++11 -fno-use-cxa-atexit -emit-llvm %s -o - -triple x86_64-apple-darwin12 | FileCheck --check-prefix=CHECK --check-prefix=DARWIN %s
12
13 int f();
14 int g();
15
16 // LINUX-DAG: @a = thread_local global i32 0
17 // DARWIN-DAG: @a = internal thread_local global i32 0
18 thread_local int a = f();
19 extern thread_local int b;
20 // CHECK-DAG: @c = global i32 0
21 int c = b;
22 // CHECK-DAG: @_ZL1d = internal thread_local global i32 0
23 static thread_local int d = g();
24
25 struct U { static thread_local int m; };
26 // LINUX-DAG: @_ZN1U1mE = thread_local global i32 0
27 // DARWIN-DAG: @_ZN1U1mE = internal thread_local global i32 0
28 thread_local int U::m = f();
29
30 namespace MismatchedInitType {
31 // Check that we don't crash here when we're forced to create a new global
32 // variable (with a different type) when we add the initializer.
33 union U {
34 int a;
35 float f;
U()36 constexpr U() : f(0.0) {}
37 };
38 static thread_local U u;
39 void *p = &u;
40 }
41
42 template<typename T> struct V { static thread_local int m; };
43 template<typename T> thread_local int V<T>::m = g();
44
45 template<typename T> struct W { static thread_local int m; };
46 template<typename T> thread_local int W<T>::m = 123;
47
48 struct Dtor { ~Dtor(); };
49 template<typename T> struct X { static thread_local Dtor m; };
50 template<typename T> thread_local Dtor X<T>::m;
51
52 // CHECK-DAG: @e = global
53 void *e = V<int>::m + W<int>::m + &X<int>::m;
54
55 template thread_local int V<float>::m;
56 template thread_local int W<float>::m;
57 template thread_local Dtor X<float>::m;
58
59 extern template thread_local int V<char>::m;
60 extern template thread_local int W<char>::m;
61 extern template thread_local Dtor X<char>::m;
62
63 void *e2 = V<char>::m + W<char>::m + &X<char>::m;
64
65 // CHECK-DAG: @_ZN1VIiE1mE = linkonce_odr thread_local global i32 0
66 // CHECK-DAG: @_ZN1WIiE1mE = linkonce_odr thread_local global i32 123
67 // CHECK-DAG: @_ZN1XIiE1mE = linkonce_odr thread_local global {{.*}}
68 // CHECK-DAG: @_ZN1VIfE1mE = weak_odr thread_local global i32 0
69 // CHECK-DAG: @_ZN1WIfE1mE = weak_odr thread_local global i32 123
70 // CHECK-DAG: @_ZN1XIfE1mE = weak_odr thread_local global {{.*}}
71
72 // CHECK-DAG: @_ZZ1fvE1n = internal thread_local global i32 0
73
74 // CHECK-DAG: @_ZGVZ1fvE1n = internal thread_local global i8 0
75
76 // CHECK-DAG: @_ZZ8tls_dtorvE1s = internal thread_local global
77 // CHECK-DAG: @_ZGVZ8tls_dtorvE1s = internal thread_local global i8 0
78
79 // CHECK-DAG: @_ZZ8tls_dtorvE1t = internal thread_local global
80 // CHECK-DAG: @_ZGVZ8tls_dtorvE1t = internal thread_local global i8 0
81
82 // CHECK-DAG: @_ZZ8tls_dtorvE1u = internal thread_local global
83 // CHECK-DAG: @_ZGVZ8tls_dtorvE1u = internal thread_local global i8 0
84 // CHECK-DAG: @_ZGRZ8tls_dtorvE1u_ = internal thread_local global
85
86 // CHECK-DAG: @_ZGVN1VIiE1mE = linkonce_odr thread_local global i64 0
87
88 // CHECK-DAG: @__tls_guard = internal thread_local global i8 0
89
90 // CHECK-DAG: @llvm.global_ctors = appending global {{.*}} @[[GLOBAL_INIT:[^ ]*]]
91
92 // LINUX-DAG: @_ZTH1a = alias void (), void ()* @__tls_init
93 // DARWIN-DAG: @_ZTH1a = internal alias void (), void ()* @__tls_init
94 // LINUX-DAG: @_ZTHN1U1mE = alias void (), void ()* @__tls_init
95 // DARWIN-DAG: @_ZTHN1U1mE = internal alias void (), void ()* @__tls_init
96 // CHECK-DAG: @_ZTHN1VIiE1mE = linkonce_odr alias void (), void ()* @[[V_M_INIT:[^, ]*]]
97 // CHECK-DAG: @_ZTHN1XIiE1mE = linkonce_odr alias void (), void ()* @[[X_M_INIT:[^, ]*]]
98 // CHECK-DAG: @_ZTHN1VIfE1mE = weak_odr alias void (), void ()* @[[VF_M_INIT:[^, ]*]]
99 // CHECK-DAG: @_ZTHN1XIfE1mE = weak_odr alias void (), void ()* @[[XF_M_INIT:[^, ]*]]
100 // FIXME: We really want a CHECK-DAG-NOT for these.
101 // CHECK-NOT: @_ZTHN1WIiE1mE =
102 // CHECK-NOT: @_ZTHN1WIfE1mE =
103 // CHECK-NOT: @_ZTHL1d =
104
105
106 // Individual variable initialization functions:
107
108 // CHECK: define {{.*}} @[[A_INIT:.*]]()
109 // CHECK: call i32 @_Z1fv()
110 // CHECK-NEXT: store i32 {{.*}}, i32* @a, align 4
111
112 // CHECK-LABEL: define i32 @_Z1fv()
f()113 int f() {
114 // CHECK: %[[GUARD:.*]] = load i8, i8* @_ZGVZ1fvE1n, align 1
115 // CHECK: %[[NEED_INIT:.*]] = icmp eq i8 %[[GUARD]], 0
116 // CHECK: br i1 %[[NEED_INIT]]
117
118 // CHECK: %[[CALL:.*]] = call i32 @_Z1gv()
119 // CHECK: store i32 %[[CALL]], i32* @_ZZ1fvE1n, align 4
120 // CHECK: store i8 1, i8* @_ZGVZ1fvE1n
121 // CHECK: br label
122 static thread_local int n = g();
123
124 // CHECK: load i32, i32* @_ZZ1fvE1n, align 4
125 return n;
126 }
127
128 // CHECK: define {{.*}} @[[C_INIT:.*]]()
129 // LINUX: call i32* @_ZTW1b()
130 // DARWIN: call cxx_fast_tlscc i32* @_ZTW1b()
131 // CHECK-NEXT: load i32, i32* %{{.*}}, align 4
132 // CHECK-NEXT: store i32 %{{.*}}, i32* @c, align 4
133
134 // LINUX-LABEL: define linkonce_odr hidden i32* @_ZTW1b()
135 // LINUX: br i1 icmp ne (void ()* @_ZTH1b, void ()* null),
136 // not null:
137 // LINUX: call void @_ZTH1b()
138 // LINUX: br label
139 // finally:
140 // LINUX: ret i32* @b
141 // DARWIN-LABEL: declare cxx_fast_tlscc i32* @_ZTW1b()
142 // There is no definition of the thread wrapper on Darwin for external TLV.
143
144 // CHECK: define {{.*}} @[[D_INIT:.*]]()
145 // CHECK: call i32 @_Z1gv()
146 // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZL1d, align 4
147
148 // CHECK: define {{.*}} @[[U_M_INIT:.*]]()
149 // CHECK: call i32 @_Z1fv()
150 // CHECK-NEXT: store i32 %{{.*}}, i32* @_ZN1U1mE, align 4
151
152 // CHECK: define {{.*}} @[[E_INIT:.*]]()
153 // LINUX: call i32* @_ZTWN1VIiE1mE()
154 // DARWIN: call cxx_fast_tlscc i32* @_ZTWN1VIiE1mE()
155 // CHECK-NEXT: load i32, i32* %{{.*}}, align 4
156 // LINUX: call {{.*}}* @_ZTWN1XIiE1mE()
157 // DARWIN: call cxx_fast_tlscc {{.*}}* @_ZTWN1XIiE1mE()
158 // CHECK: store {{.*}} @e
159
160 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1VIiE1mE()
161 // DARWIN-LABEL: define weak_odr hidden cxx_fast_tlscc i32* @_ZTWN1VIiE1mE()
162 // LINUX: call void @_ZTHN1VIiE1mE()
163 // DARWIN: call cxx_fast_tlscc void @_ZTHN1VIiE1mE()
164 // CHECK: ret i32* @_ZN1VIiE1mE
165
166 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1WIiE1mE()
167 // DARWIN-LABEL: define weak_odr hidden cxx_fast_tlscc i32* @_ZTWN1WIiE1mE()
168 // CHECK-NOT: call
169 // CHECK: ret i32* @_ZN1WIiE1mE
170
171 // LINUX-LABEL: define weak_odr hidden {{.*}}* @_ZTWN1XIiE1mE()
172 // DARWIN-LABEL: define weak_odr hidden cxx_fast_tlscc {{.*}}* @_ZTWN1XIiE1mE()
173 // LINUX: call void @_ZTHN1XIiE1mE()
174 // DARWIN: call cxx_fast_tlscc void @_ZTHN1XIiE1mE()
175 // CHECK: ret {{.*}}* @_ZN1XIiE1mE
176
177 // LINUX: define internal void @[[VF_M_INIT]]()
178 // DARWIN: define internal cxx_fast_tlscc void @[[VF_M_INIT]]()
179 // LINUX-SAME: comdat($_ZN1VIfE1mE)
180 // DARWIN-NOT: comdat
181 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1VIfE1mE to i8*)
182 // CHECK: %[[VF_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
183 // CHECK: br i1 %[[VF_M_INITIALIZED]],
184 // need init:
185 // CHECK: call i32 @_Z1gv()
186 // CHECK: store i32 %{{.*}}, i32* @_ZN1VIfE1mE, align 4
187 // CHECK: store i64 1, i64* @_ZGVN1VIfE1mE
188 // CHECK: br label
189
190 // LINUX: define internal void @[[XF_M_INIT]]()
191 // DARWIN: define internal cxx_fast_tlscc void @[[XF_M_INIT]]()
192 // LINUX-SAME: comdat($_ZN1XIfE1mE)
193 // DARWIN-NOT: comdat
194 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1XIfE1mE to i8*)
195 // CHECK: %[[XF_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
196 // CHECK: br i1 %[[XF_M_INITIALIZED]],
197 // need init:
198 // LINUX: call {{.*}}__cxa_thread_atexit
199 // DARWIN: call {{.*}}_tlv_atexit
200 // CHECK: store i64 1, i64* @_ZGVN1XIfE1mE
201 // CHECK: br label
202
203 // LINUX: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
204 // DARWIN: declare i32 @_tlv_atexit(void (i8*)*, i8*, i8*)
205
206 // DARWIN: declare cxx_fast_tlscc i32* @_ZTWN1VIcE1mE()
207 // LINUX: define linkonce_odr hidden i32* @_ZTWN1VIcE1mE() {{#[0-9]+}} comdat {
208 // LINUX: br i1 icmp ne (void ()* @_ZTHN1VIcE1mE,
209 // LINUX: call void @_ZTHN1VIcE1mE()
210 // LINUX: ret i32* @_ZN1VIcE1mE
211
212 // DARWIN: declare cxx_fast_tlscc i32* @_ZTWN1WIcE1mE()
213 // LINUX: define linkonce_odr hidden i32* @_ZTWN1WIcE1mE() {{#[0-9]+}} comdat {
214 // LINUX: br i1 icmp ne (void ()* @_ZTHN1WIcE1mE,
215 // LINUX: call void @_ZTHN1WIcE1mE()
216 // LINUX: ret i32* @_ZN1WIcE1mE
217
218 // DARWIN: declare cxx_fast_tlscc {{.*}}* @_ZTWN1XIcE1mE()
219 // LINUX: define linkonce_odr hidden {{.*}}* @_ZTWN1XIcE1mE() {{#[0-9]+}} comdat {
220 // LINUX: br i1 icmp ne (void ()* @_ZTHN1XIcE1mE,
221 // LINUX: call void @_ZTHN1XIcE1mE()
222 // LINUX: ret {{.*}}* @_ZN1XIcE1mE
223
224 struct S { S(); ~S(); };
225 struct T { ~T(); };
226
227 // CHECK-LABEL: define void @_Z8tls_dtorv()
tls_dtor()228 void tls_dtor() {
229 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1s
230 // CHECK: call void @_ZN1SC1Ev(%struct.S* {{[^,]*}} @_ZZ8tls_dtorvE1s)
231 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
232 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
233 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1s
234 static thread_local S s;
235
236 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1t
237 // CHECK-NOT: _ZN1T
238 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
239 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
240 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1t
241 static thread_local T t;
242
243 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1u
244 // CHECK: call void @_ZN1SC1Ev(%struct.S* {{[^,]*}} @_ZGRZ8tls_dtorvE1u_)
245 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
246 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
247 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1u
248 static thread_local const S &u = S();
249 }
250
251 // CHECK: define {{.*}} @_Z7PR15991v(
PR15991()252 int PR15991() {
253 thread_local int n;
254 auto l = [] { return n; };
255 return l();
256 }
257
258 struct PR19254 {
259 static thread_local int n;
260 int f();
261 };
262 // CHECK: define {{.*}} @_ZN7PR192541fEv(
f()263 int PR19254::f() {
264 // LINUX: call void @_ZTHN7PR192541nE(
265 // DARWIN: call cxx_fast_tlscc i32* @_ZTWN7PR192541nE(
266 return this->n;
267 }
268
269 namespace {
270 thread_local int anon_i{f()};
271 }
set_anon_i()272 void set_anon_i() {
273 anon_i = 2;
274 }
275 // LINUX-LABEL: define internal i32* @_ZTWN12_GLOBAL__N_16anon_iE()
276 // DARWIN-LABEL: define internal cxx_fast_tlscc i32* @_ZTWN12_GLOBAL__N_16anon_iE()
277
278 // LINUX: define internal void @[[V_M_INIT]]()
279 // DARWIN: define internal cxx_fast_tlscc void @[[V_M_INIT]]()
280 // LINUX-SAME: comdat($_ZN1VIiE1mE)
281 // DARWIN-NOT: comdat
282 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1VIiE1mE to i8*)
283 // CHECK: %[[V_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
284 // CHECK: br i1 %[[V_M_INITIALIZED]],
285 // need init:
286 // CHECK: call i32 @_Z1gv()
287 // CHECK: store i32 %{{.*}}, i32* @_ZN1VIiE1mE, align 4
288 // CHECK: store i64 1, i64* @_ZGVN1VIiE1mE
289 // CHECK: br label
290
291 // LINUX: define internal void @[[X_M_INIT]]()
292 // DARWIN: define internal cxx_fast_tlscc void @[[X_M_INIT]]()
293 // LINUX-SAME: comdat($_ZN1XIiE1mE)
294 // DARWIN-NOT: comdat
295 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1XIiE1mE to i8*)
296 // CHECK: %[[X_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
297 // CHECK: br i1 %[[X_M_INITIALIZED]],
298 // need init:
299 // LINUX: call {{.*}}__cxa_thread_atexit
300 // DARWIN: call {{.*}}_tlv_atexit
301 // CHECK: store i64 1, i64* @_ZGVN1XIiE1mE
302 // CHECK: br label
303
304 // CHECK: define {{.*}}@[[GLOBAL_INIT:.*]]()
305 // CHECK: call void @[[C_INIT]]()
306 // CHECK: call void @[[E_INIT]]()
307
308
309 // CHECK: define {{.*}}@__tls_init()
310 // CHECK: load i8, i8* @__tls_guard
311 // CHECK: %[[NEED_TLS_INIT:.*]] = icmp eq i8 %{{.*}}, 0
312 // CHECK: br i1 %[[NEED_TLS_INIT]],
313 // init:
314 // CHECK: store i8 1, i8* @__tls_guard
315 // CHECK-OPT: call {}* @llvm.invariant.start.p0i8(i64 1, i8* @__tls_guard)
316 // CHECK-NOT: call void @[[V_M_INIT]]()
317 // CHECK: call void @[[A_INIT]]()
318 // CHECK-NOT: call void @[[V_M_INIT]]()
319 // CHECK: call void @[[D_INIT]]()
320 // CHECK-NOT: call void @[[V_M_INIT]]()
321 // CHECK: call void @[[U_M_INIT]]()
322 // CHECK-NOT: call void @[[V_M_INIT]]()
323
324
325 // LINUX: define weak_odr hidden i32* @_ZTW1a()
326 // DARWIN: define cxx_fast_tlscc i32* @_ZTW1a()
327 // LINUX: call void @_ZTH1a()
328 // DARWIN: call cxx_fast_tlscc void @_ZTH1a()
329 // CHECK: ret i32* @a
330 // CHECK: }
331
332
333 // Should not emit a thread wrapper for internal-linkage unused variable 'd'.
334 // We separately check that 'd' does in fact get initialized with the other
335 // thread-local variables in this TU.
336 // CHECK-NOT: define {{.*}} @_ZTWL1d()
337
338 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
339 // DARWIN-LABEL: define cxx_fast_tlscc i32* @_ZTWN1U1mE()
340 // LINUX: call void @_ZTHN1U1mE()
341 // DARWIN: call cxx_fast_tlscc void @_ZTHN1U1mE()
342 // CHECK: ret i32* @_ZN1U1mE
343
344 // LINUX: declare extern_weak void @_ZTH1b() [[ATTR:#[0-9]+]]
345
346 // LINUX: attributes [[ATTR]] = { {{.+}} }
347