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