Skip to content

Commit 6636471

Browse files
authored
Merge pull request #71825 from Azoy/some-sil-atomic-tests
[test] Add a SIL optimizer test that ensures we dont destroy atomics before use
2 parents 557628c + 38be421 commit 6636471

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// RUN: %target-swift-frontend -O -emit-sil -disable-availability-checking %s | %FileCheck %s
2+
3+
// REQUIRES: synchronization
4+
5+
import Synchronization
6+
7+
//===----------------------------------------------------------------------===//
8+
// Ensure that we don't destroy the atomic before operations
9+
//===----------------------------------------------------------------------===//
10+
11+
// CHECK-LABEL: sil {{.*}} @localLoad {{.*}} {
12+
// CHECK: [[ATOMIC:%.*]] = alloc_stack [lexical] $Atomic<Int>
13+
// CHECK: [[ATOMIC_PTR:%.*]] = address_to_pointer [[ATOMIC]]
14+
// CHECK: builtin "atomicload_monotonic_Int64"([[ATOMIC_PTR]] : $Builtin.RawPointer)
15+
// CHECK: destroy_addr [[ATOMIC]] : $*Atomic<Int>
16+
// CHECK-NEXT: dealloc_stack [[ATOMIC]] : $*Atomic<Int>
17+
// CHECK-LABEL: } // end sil function 'localLoad'
18+
@_silgen_name("localLoad")
19+
func localLoad() -> Int {
20+
let x = Atomic(128)
21+
return x.load(ordering: .relaxed)
22+
}
23+
24+
// CHECK-LABEL: sil {{.*}} @localStore {{.*}} {
25+
// CHECK: [[ATOMIC:%.*]] = alloc_stack [lexical] $Atomic<Int>
26+
// CHECK: [[ATOMIC_PTR:%.*]] = address_to_pointer [[ATOMIC]]
27+
// CHECK: builtin "atomicstore_release_Int64"([[ATOMIC_PTR]] : $Builtin.RawPointer
28+
// CHECK: destroy_addr [[ATOMIC]] : $*Atomic<Int>
29+
// CHECK-NEXT: dealloc_stack [[ATOMIC]] : $*Atomic<Int>
30+
// CHECK-LABEL: } // end sil function 'localStore'
31+
@_silgen_name("localStore")
32+
func localStore() {
33+
let x = Atomic(128)
34+
x.store(0, ordering: .releasing)
35+
}
36+
37+
// CHECK-LABEL: sil {{.*}} @localExchange {{.*}} {
38+
// CHECK: [[ATOMIC:%.*]] = alloc_stack [lexical] $Atomic<Int>
39+
// CHECK: [[ATOMIC_PTR:%.*]] = address_to_pointer [[ATOMIC]]
40+
// CHECK: builtin "atomicrmw_xchg_acquire_Int64"([[ATOMIC_PTR]] : $Builtin.RawPointer
41+
// CHECK: destroy_addr [[ATOMIC]] : $*Atomic<Int>
42+
// CHECK-NEXT: dealloc_stack [[ATOMIC]] : $*Atomic<Int>
43+
// CHECK-LABEL: } // end sil function 'localExchange'
44+
@_silgen_name("localExchange")
45+
func localExchange() -> Int {
46+
let x = Atomic(128)
47+
return x.exchange(0, ordering: .acquiring)
48+
}
49+
50+
// CHECK-LABEL: sil {{.*}} @localCompareExchange {{.*}} {
51+
// CHECK: [[ATOMIC:%.*]] = alloc_stack [lexical] $Atomic<Int>
52+
// CHECK: [[ATOMIC_PTR:%.*]] = address_to_pointer [[ATOMIC]]
53+
// CHECK: builtin "cmpxchg_seqcst_seqcst_Int64"([[ATOMIC_PTR]] : $Builtin.RawPointer
54+
// CHECK: destroy_addr [[ATOMIC]] : $*Atomic<Int>
55+
// CHECK-NEXT: dealloc_stack [[ATOMIC]] : $*Atomic<Int>
56+
// CHECK-LABEL: } // end sil function 'localCompareExchange'
57+
@_silgen_name("localCompareExchange")
58+
func localCompareExchange() -> (exchanged: Bool, original: Int) {
59+
let x = Atomic(128)
60+
return x.compareExchange(
61+
expected: 128,
62+
desired: 316,
63+
ordering: .sequentiallyConsistent
64+
)
65+
}

0 commit comments

Comments
 (0)