1 // RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-apple-macosx10.9.0 | FileCheck %s
2 // REQUIRES: x86-registered-target
3 // Also test serialization of atomic operations here, to avoid duplicating the
5 // RUN: %clang_cc1 %s -emit-pch -o %t -triple=x86_64-apple-macosx10.9.0
6 // RUN: %clang_cc1 %s -include-pch %t -triple=x86_64-apple-macosx10.9.0 -emit-llvm -o - | FileCheck %s
7 #ifndef ALREADY_INCLUDED
8 #define ALREADY_INCLUDED
10 // Basic IRGen tests for __c11_atomic_* and GNU __atomic_*
12 typedef enum memory_order
{
13 memory_order_relaxed
, memory_order_consume
, memory_order_acquire
,
14 memory_order_release
, memory_order_acq_rel
, memory_order_seq_cst
17 int fi1(_Atomic(int) *i
) {
19 // CHECK: load atomic i32, ptr {{.*}} seq_cst, align 4
20 return __c11_atomic_load(i
, memory_order_seq_cst
);
25 // CHECK: load atomic i32, ptr {{.*}} seq_cst, align 4
27 __atomic_load(i
, &v
, memory_order_seq_cst
);
33 // CHECK: load atomic i32, ptr {{.*}} seq_cst, align 4
34 return __atomic_load_n(i
, memory_order_seq_cst
);
37 void fi2(_Atomic(int) *i
) {
39 // CHECK: store atomic i32 {{.*}} seq_cst, align 4
40 __c11_atomic_store(i
, 1, memory_order_seq_cst
);
45 // CHECK: store atomic i32 {{.*}} seq_cst, align 4
47 __atomic_store(i
, &v
, memory_order_seq_cst
);
52 // CHECK: store atomic i32 {{.*}} seq_cst, align 4
53 __atomic_store_n(i
, 1, memory_order_seq_cst
);
56 int fi3(_Atomic(int) *i
) {
58 // CHECK: atomicrmw and {{.*}} seq_cst, align 4
60 return __c11_atomic_fetch_and(i
, 1, memory_order_seq_cst
);
65 // CHECK: atomicrmw xor {{.*}} seq_cst, align 4
67 return __atomic_fetch_xor(i
, 1, memory_order_seq_cst
);
72 // CHECK: atomicrmw add {{.*}} seq_cst, align 4
74 return __atomic_add_fetch(i
, 1, memory_order_seq_cst
);
79 // CHECK: atomicrmw nand {{.*}} seq_cst, align 4
81 return __atomic_fetch_nand(i
, 1, memory_order_seq_cst
);
86 // CHECK: atomicrmw nand {{.*}} seq_cst, align 4
89 return __atomic_nand_fetch(i
, 1, memory_order_seq_cst
);
92 _Bool
fi4(_Atomic(int) *i
) {
94 // CHECK: cmpxchg ptr {{.*}} acquire acquire, align 4
96 return __c11_atomic_compare_exchange_strong(i
, &cmp
, 1, memory_order_acquire
, memory_order_acquire
);
101 // CHECK: cmpxchg ptr {{.*}} acquire acquire, align 4
104 return __atomic_compare_exchange(i
, &cmp
, &desired
, 0, memory_order_acquire
, memory_order_acquire
);
109 // CHECK: cmpxchg weak ptr {{.*}} acquire acquire, align 4
111 return __atomic_compare_exchange_n(i
, &cmp
, 1, 1, memory_order_acquire
, memory_order_acquire
);
114 float ff1(_Atomic(float) *d
) {
116 // CHECK: load atomic i32, ptr {{.*}} monotonic, align 4
117 return __c11_atomic_load(d
, memory_order_relaxed
);
120 void ff2(_Atomic(float) *d
) {
122 // CHECK: store atomic i32 {{.*}} release, align 4
123 __c11_atomic_store(d
, 1, memory_order_release
);
126 float ff3(_Atomic(float) *d
) {
127 return __c11_atomic_exchange(d
, 2, memory_order_seq_cst
);
130 int* fp1(_Atomic(int*) *p
) {
132 // CHECK: load atomic i64, ptr {{.*}} seq_cst, align 8
133 return __c11_atomic_load(p
, memory_order_seq_cst
);
136 int* fp2(_Atomic(int*) *p
) {
138 // CHECK: store i64 4
139 // CHECK: atomicrmw add {{.*}} monotonic, align 8
140 return __c11_atomic_fetch_add(p
, 1, memory_order_relaxed
);
145 // CHECK: store i64 4
146 // CHECK: atomicrmw sub {{.*}} monotonic, align 8
147 // Note, the GNU builtins do not multiply by sizeof(T)!
148 return __atomic_fetch_sub(p
, 4, memory_order_relaxed
);
151 _Complex
float fc(_Atomic(_Complex
float) *c
) {
153 // CHECK: atomicrmw xchg ptr {{.*}} seq_cst, align 8
154 return __c11_atomic_exchange(c
, 2, memory_order_seq_cst
);
157 typedef struct X
{ int x
; } X
;
158 X
fs(_Atomic(X
) *c
) {
160 // CHECK: atomicrmw xchg ptr {{.*}} seq_cst, align 4
161 return __c11_atomic_exchange(c
, (X
){2}, memory_order_seq_cst
);
166 // CHECK: atomicrmw xchg ptr {{.*}} seq_cst, align 4
168 __atomic_exchange(c
, d
, &ret
, memory_order_seq_cst
);
172 _Bool
fsb(_Bool
*c
) {
174 // CHECK: atomicrmw xchg ptr {{.*}} seq_cst, align 1
175 return __atomic_exchange_n(c
, 1, memory_order_seq_cst
);
180 void test_and_set(void) {
181 // CHECK: atomicrmw xchg ptr @flag1, i8 1 seq_cst, align 1
182 __atomic_test_and_set(&flag1
, memory_order_seq_cst
);
183 // CHECK: atomicrmw volatile xchg ptr @flag2, i8 1 acquire, align 1
184 __atomic_test_and_set(&flag2
, memory_order_acquire
);
185 // CHECK: store atomic volatile i8 0, ptr @flag2 release, align 1
186 __atomic_clear(&flag2
, memory_order_release
);
187 // CHECK: store atomic i8 0, ptr @flag1 seq_cst, align 1
188 __atomic_clear(&flag1
, memory_order_seq_cst
);
198 int lock_free(struct Incomplete
*incomplete
) {
201 // CHECK: call zeroext i1 @__atomic_is_lock_free(i64 noundef 3, ptr noundef null)
202 __c11_atomic_is_lock_free(3);
204 // CHECK: call zeroext i1 @__atomic_is_lock_free(i64 noundef 16, ptr noundef {{.*}}@sixteen{{.*}})
205 __atomic_is_lock_free(16, &sixteen
);
207 // CHECK: call zeroext i1 @__atomic_is_lock_free(i64 noundef 17, ptr noundef {{.*}}@seventeen{{.*}})
208 __atomic_is_lock_free(17, &seventeen
);
210 // CHECK: call zeroext i1 @__atomic_is_lock_free(i64 noundef 4, {{.*}})
211 __atomic_is_lock_free(4, incomplete
);
214 // CHECK: call zeroext i1 @__atomic_is_lock_free(i64 noundef 4, {{.*}})
215 __atomic_is_lock_free(4, cs
+1);
218 __atomic_always_lock_free(3, 0);
219 __atomic_always_lock_free(16, 0);
220 __atomic_always_lock_free(17, 0);
221 __atomic_always_lock_free(16, &sixteen
);
222 __atomic_always_lock_free(17, &seventeen
);
225 __atomic_is_lock_free(4, &n
);
228 return __c11_atomic_is_lock_free(sizeof(_Atomic(int)));
231 // Tests for atomic operations on big values. These should call the functions
233 // http://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary#The_Library_interface
242 struct bar smallThing
, thing1
, thing2
;
244 _Atomic(struct foo
) bigAtomic
;
246 void structAtomicStore(void) {
247 // CHECK: @structAtomicStore
249 __c11_atomic_store(&bigAtomic
, f
, 5);
250 // CHECK: call void @__atomic_store(i64 noundef 512, ptr noundef @bigAtomic,
253 __atomic_store(&smallThing
, &b
, 5);
254 // CHECK: call void @__atomic_store(i64 noundef 3, ptr noundef @smallThing
256 __atomic_store(&bigThing
, &f
, 5);
257 // CHECK: call void @__atomic_store(i64 noundef 512, ptr noundef @bigThing
259 void structAtomicLoad(void) {
260 // CHECK: @structAtomicLoad
261 struct foo f
= __c11_atomic_load(&bigAtomic
, 5);
262 // CHECK: call void @__atomic_load(i64 noundef 512, ptr noundef @bigAtomic,
265 __atomic_load(&smallThing
, &b
, 5);
266 // CHECK: call void @__atomic_load(i64 noundef 3, ptr noundef @smallThing
268 __atomic_load(&bigThing
, &f
, 5);
269 // CHECK: call void @__atomic_load(i64 noundef 512, ptr noundef @bigThing
271 struct foo
structAtomicExchange(void) {
272 // CHECK: @structAtomicExchange
275 __atomic_exchange(&f
, &bigThing
, &old
, 5);
276 // CHECK: call void @__atomic_exchange(i64 noundef 512, {{.*}}, ptr noundef @bigThing,
278 return __c11_atomic_exchange(&bigAtomic
, f
, 5);
279 // CHECK: call void @__atomic_exchange(i64 noundef 512, ptr noundef @bigAtomic,
281 int structAtomicCmpExchange(void) {
282 // CHECK: @structAtomicCmpExchange
283 _Bool x
= __atomic_compare_exchange(&smallThing
, &thing1
, &thing2
, 1, 5, 5);
284 // CHECK: call zeroext i1 @__atomic_compare_exchange(i64 noundef 3, {{.*}} @smallThing{{.*}} @thing1{{.*}} @thing2
289 return x
& __c11_atomic_compare_exchange_strong(&bigAtomic
, &f
, g
, 5, 5);
290 // CHECK: call zeroext i1 @__atomic_compare_exchange(i64 noundef 512, ptr noundef @bigAtomic,
293 // Check that no atomic operations are used in any initialisation of _Atomic
295 _Atomic(int) atomic_init_i
= 42;
297 // CHECK: @atomic_init_foo
298 void atomic_init_foo(void)
308 __c11_atomic_init(&j
, 42);
314 // Check this doesn't crash
315 // CHECK: @test_atomic_array_param(
316 void test_atomic_array_param(_Atomic(struct foo
) a
) {
317 test_atomic_array_param(a
);