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 // LINUX: call void @_ZTHN1VIiE1mE()
126 // DARWIN: call cxx_fast_tlscc void @_ZTHN1VIiE1mE()
127 // CHECK: ret i32* @_ZN1VIiE1mE
128
129
130 struct S { S(); ~S(); };
131 struct T { ~T(); };
132
133 // CHECK-LABEL: define void @_Z8tls_dtorv()
tls_dtor()134 void tls_dtor() {
135 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1s
136 // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZZ8tls_dtorvE1s)
137 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
138 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZZ8tls_dtorvE1s{{.*}} @__dso_handle
139 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1s
140 static thread_local S s;
141
142 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1t
143 // CHECK-NOT: _ZN1T
144 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
145 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1TD1Ev {{.*}}@_ZZ8tls_dtorvE1t{{.*}} @__dso_handle
146 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1t
147 static thread_local T t;
148
149 // CHECK: load i8, i8* @_ZGVZ8tls_dtorvE1u
150 // CHECK: call void @_ZN1SC1Ev(%struct.S* @_ZGRZ8tls_dtorvE1u_)
151 // LINUX: call i32 @__cxa_thread_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
152 // DARWIN: call i32 @_tlv_atexit({{.*}}@_ZN1SD1Ev {{.*}} @_ZGRZ8tls_dtorvE1u_{{.*}} @__dso_handle
153 // CHECK: store i8 1, i8* @_ZGVZ8tls_dtorvE1u
154 static thread_local const S &u = S();
155 }
156
157 // LINUX: declare i32 @__cxa_thread_atexit(void (i8*)*, i8*, i8*)
158 // DARWIN: declare i32 @_tlv_atexit(void (i8*)*, i8*, i8*)
159
160 // CHECK: define {{.*}} @_Z7PR15991v(
PR15991()161 int PR15991() {
162 thread_local int n;
163 auto l = [] { return n; };
164 return l();
165 }
166
167 struct PR19254 {
168 static thread_local int n;
169 int f();
170 };
171 // CHECK: define {{.*}} @_ZN7PR192541fEv(
f()172 int PR19254::f() {
173 // LINUX: call void @_ZTHN7PR192541nE(
174 // DARWIN: call cxx_fast_tlscc i32* @_ZTWN7PR192541nE(
175 return this->n;
176 }
177
178 namespace {
179 thread_local int anon_i{1};
180 }
set_anon_i()181 void set_anon_i() {
182 anon_i = 2;
183 }
184 // LINUX-LABEL: define internal i32* @_ZTWN12_GLOBAL__N_16anon_iE()
185 // DARWIN-LABEL: define internal cxx_fast_tlscc i32* @_ZTWN12_GLOBAL__N_16anon_iE()
186
187 // CHECK: define {{.*}} @[[V_M_INIT:.*]]()
188 // CHECK: load i8, i8* bitcast (i64* @_ZGVN1VIiE1mE to i8*)
189 // CHECK: %[[V_M_INITIALIZED:.*]] = icmp eq i8 %{{.*}}, 0
190 // CHECK: br i1 %[[V_M_INITIALIZED]],
191 // need init:
192 // CHECK: call i32 @_Z1gv()
193 // CHECK: store i32 %{{.*}}, i32* @_ZN1VIiE1mE, align 4
194 // CHECK: store i64 1, i64* @_ZGVN1VIiE1mE
195 // CHECK: br label
196
197 // CHECK: define {{.*}}@[[GLOBAL_INIT:.*]]()
198 // CHECK: call void @[[C_INIT]]()
199 // CHECK: call void @[[E_INIT]]()
200
201
202 // CHECK: define {{.*}}@__tls_init()
203 // CHECK: load i8, i8* @__tls_guard
204 // CHECK: %[[NEED_TLS_INIT:.*]] = icmp eq i8 %{{.*}}, 0
205 // CHECK: br i1 %[[NEED_TLS_INIT]],
206 // init:
207 // CHECK: store i8 1, i8* @__tls_guard
208 // CHECK: call void @[[A_INIT]]()
209 // CHECK: call void @[[D_INIT]]()
210 // CHECK: call void @[[U_M_INIT]]()
211 // CHECK: call void @[[V_M_INIT]]()
212
213
214 // LIUNX: define weak_odr hidden i32* @_ZTW1a() {
215 // DARWIN: define cxx_fast_tlscc i32* @_ZTW1a()
216 // LINUX: call void @_ZTH1a()
217 // DARWIN: call cxx_fast_tlscc void @_ZTH1a()
218 // CHECK: ret i32* @a
219 // CHECK: }
220
221
222 // LINUX: declare extern_weak void @_ZTH1b() [[ATTR:#[0-9]+]]
223
224
225 // LINUX-LABEL: define internal i32* @_ZTWL1d()
226 // DARWIN-LABEL: define internal cxx_fast_tlscc i32* @_ZTWL1d()
227 // LINUX: call void @_ZTHL1d()
228 // DARWIN: call cxx_fast_tlscc void @_ZTHL1d()
229 // CHECK: ret i32* @_ZL1d
230
231 // LINUX-LABEL: define weak_odr hidden i32* @_ZTWN1U1mE()
232 // DARWIN-LABEL: define cxx_fast_tlscc i32* @_ZTWN1U1mE()
233 // LINUX: call void @_ZTHN1U1mE()
234 // DARWIN: call cxx_fast_tlscc void @_ZTHN1U1mE()
235 // CHECK: ret i32* @_ZN1U1mE
236
237 // LINUX: attributes [[ATTR]] = { {{.+}} }
238