Skip to content

Commit cd66b87

Browse files
committed
Add unsafeAddress SIL tests.
1 parent a4c970f commit cd66b87

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

test/SILOptimizer/unsafeAddress.swift

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// RUN: %target-swift-frontend -module-name Swift -emit-sil %s | %FileCheck %s
2+
3+
@_silgen_name("makeUpAPointer")
4+
func makeUpAPointer<T: ~Copyable>() -> UnsafePointer<T>
5+
@_silgen_name("makeUpAMutablePointer")
6+
func makeUpAPointer<T: ~Copyable>() -> UnsafeMutablePointer<T>
7+
@_silgen_name("makeUpAnInt")
8+
func makeUpAnInt() -> Int
9+
10+
class X {}
11+
12+
struct NC: ~Copyable {
13+
var x: Any = X()
14+
deinit {}
15+
}
16+
17+
struct S {
18+
var data: NC {
19+
unsafeAddress { return makeUpAPointer() }
20+
}
21+
22+
var mutableData: NC {
23+
unsafeAddress { return makeUpAPointer() }
24+
unsafeMutableAddress { return makeUpAPointer() }
25+
}
26+
}
27+
28+
struct SNC: ~Copyable {
29+
var data: NC {
30+
unsafeAddress { return makeUpAPointer() }
31+
}
32+
33+
var mutableData: NC {
34+
unsafeAddress { return makeUpAPointer() }
35+
unsafeMutableAddress { return makeUpAPointer() }
36+
}
37+
}
38+
39+
class C {
40+
final var data: NC {
41+
unsafeAddress { return makeUpAPointer() }
42+
}
43+
44+
final var mutableData: NC {
45+
unsafeAddress { return makeUpAPointer() }
46+
unsafeMutableAddress { return makeUpAPointer() }
47+
}
48+
}
49+
50+
func borrow(_ nc: borrowing NC) {}
51+
func mod(_ nc: inout NC) {}
52+
53+
// CHECK-LABEL: sil hidden @$s4main11testCBorrow1cyAA1CC_tF : $@convention(thin) (@guaranteed C) -> () {
54+
// CHECK: [[ADR:%.*]] = pointer_to_address %4 to [strict] $*NC
55+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on %0
56+
// CHECK: begin_access [read] [unsafe] [[MD]]
57+
// CHECK: apply
58+
// CHECK: end_access
59+
// CHECK-LABEL: } // end sil function '$s4main11testCBorrow1cyAA1CC_tF'
60+
func testCBorrow(c: C) {
61+
borrow(c.data)
62+
}
63+
64+
// CHECK-LABEL: sil hidden @$s4main8testCMod1cyAA1CC_tF : $@convention(thin) (@guaranteed C) -> () {
65+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
66+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on %0
67+
// CHECK: begin_access [modify] [unsafe] [[MD]]
68+
// CHECK: apply
69+
// CHECK: end_access
70+
// CHECK-LABEL: } // end sil function '$s4main8testCMod1cyAA1CC_tF'
71+
func testCMod(c: C) {
72+
mod(&c.mutableData)
73+
}
74+
75+
// CHECK-LABEL: sil hidden @$s4main11testSBorrow1syAA1SV_tF : $@convention(thin) (S) -> () {
76+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
77+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on %0
78+
// CHECK: begin_access [read] [unsafe] [[MD]]
79+
// CHECK: apply
80+
// CHECK: end_access
81+
// CHECK-LABEL: } // end sil function '$s4main11testSBorrow1syAA1SV_tF'
82+
func testSBorrow(s: S) {
83+
borrow(s.data)
84+
}
85+
86+
// CHECK-LABEL: sil hidden @$s4main8testSMod1syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
87+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0
88+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
89+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
90+
// CHECK: begin_access [modify] [unsafe] [[MD]]
91+
// CHECK: apply
92+
// CHECK: end_access
93+
// CHECK-LABEL: } // end sil function '$s4main8testSMod1syAA1SVz_tF'
94+
func testSMod(s: inout S) {
95+
mod(&s.mutableData)
96+
}
97+
98+
// CHECK-LABEL: sil hidden @$s4main16testSInoutBorrow5mut_syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
99+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0
100+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
101+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
102+
// CHECK: begin_access [read] [unsafe] [[MD]]
103+
// CHECK: apply
104+
// CHECK: end_access
105+
// CHECK-LABEL: } // end sil function '$s4main16testSInoutBorrow5mut_syAA1SVz_tF'
106+
func testSInoutBorrow(mut_s s: inout S) {
107+
borrow(s.data)
108+
}
109+
110+
// CHECK-LABEL: sil hidden @$s4main19testSInoutMutBorrow5mut_syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
111+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0
112+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
113+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
114+
// CHECK: begin_access [read] [unsafe] [[MD]]
115+
// CHECK: apply
116+
// CHECK: end_access
117+
// CHECK-LABEL: } // end sil function '$s4main19testSInoutMutBorrow5mut_syAA1SVz_tF'
118+
func testSInoutMutBorrow(mut_s s: inout S) {
119+
borrow(s.mutableData)
120+
}
121+
122+
// CHECK-LABEL: sil hidden @$s4main13testSInoutMod5mut_syAA1SVz_tF : $@convention(thin) (@inout S) -> () {
123+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0
124+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
125+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
126+
// CHECK: begin_access [modify] [unsafe] [[MD]]
127+
// CHECK: apply
128+
// CHECK: end_access
129+
// CHECK-LABEL: } // end sil function '$s4main13testSInoutMod5mut_syAA1SVz_tF'
130+
func testSInoutMod(mut_s s: inout S) {
131+
mod(&s.mutableData)
132+
}
133+
134+
// CHECK-LABEL: sil hidden @$s4main13testSNCBorrow3sncyAA3SNCV_tF : $@convention(thin) (@guaranteed SNC) -> () {
135+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
136+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on %0
137+
// CHECK: begin_access [read] [unsafe] [[MD]]
138+
// CHECK: apply
139+
// CHECK: end_access
140+
// CHECK-LABEL: } // end sil function '$s4main13testSNCBorrow3sncyAA3SNCV_tF'
141+
func testSNCBorrow(snc: borrowing SNC) {
142+
borrow(snc.data)
143+
}
144+
145+
// CHECK-LABEL: sil hidden @$s4main16testSNCMutBorrow3sncyAA3SNCV_tF : $@convention(thin) (@guaranteed SNC) -> () {
146+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
147+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on %0
148+
// CHECK: begin_access [read] [unsafe] [[MD]]
149+
// CHECK: apply
150+
// CHECK: end_access
151+
// CHECK-LABEL: } // end sil function '$s4main16testSNCMutBorrow3sncyAA3SNCV_tF'
152+
func testSNCMutBorrow(snc: borrowing SNC) {
153+
borrow(snc.mutableData)
154+
}
155+
156+
// CHECK-LABEL: sil hidden @$s4main10testSNCMod7mut_sncyAA3SNCVz_tF : $@convention(thin) (@inout SNC) -> () {
157+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0
158+
// CHECK: [[ADR:%.*]] = pointer_to_address %{{.*}} to [strict] $*NC
159+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADR]] on [[ACCESS]]
160+
// CHECK: begin_access [modify] [unsafe] [[MD]]
161+
// CHECK: apply
162+
// CHECK: end_access
163+
// CHECK-LABEL: } // end sil function '$s4main10testSNCMod7mut_sncyAA3SNCVz_tF'
164+
func testSNCMod(mut_snc snc: inout SNC) {
165+
mod(&snc.mutableData)
166+
}

0 commit comments

Comments
 (0)