-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathaccess_sink.sil
282 lines (260 loc) · 11.5 KB
/
access_sink.sil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// RUN: %target-sil-opt -sil-print-types -access-enforcement-release %s -enable-sil-verify-all | %FileCheck %s
//
// Test the AccessEnforcementReleaseSinking pass in isolation.
// This ensures that no upstream passes have removed SIL-level access markers
// that are required to ensure the pass is not overly optimistic.
sil_stage canonical
import Builtin
import Swift
import SwiftShims
struct X {
@_hasStorage var i: Int64 { get set }
init(i: Int64)
init()
}
var globalX: X
sil_global hidden @globalX : $X
sil hidden_external [global_init] @globalAddressor : $@convention(thin) () -> Builtin.RawPointer
// public func testSimpleRelease() {
// Checks the simple case of release sinking
//
// CHECK-LABEL: sil @testSimpleRelease : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-LABEL: } // end sil function 'testSimpleRelease'
sil @testSimpleRelease : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testMultiBlocklSimpleRelease() {
// Checks the simple case of release sinking with the begin_access in a different block
//
// CHECK-LABEL: sil @testMultiBlocklSimpleRelease : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: br bb1
// CHECK: bb1
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-LABEL: } // end sil function 'testMultiBlocklSimpleRelease'
sil @testMultiBlocklSimpleRelease : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
br bb1
bb1:
release_value %2 : $X
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testMultiBlocklBailOnRelease() {
// Checks bailing (for now) on the simple case due to the release being in a different block
//
// CHECK-LABEL: sil @testMultiBlocklBailOnRelease : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-NEXT: br bb1
// CHECK: bb1
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-LABEL: } // end sil function 'testMultiBlocklBailOnRelease'
sil @testMultiBlocklBailOnRelease : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
br bb1
bb1:
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testApplyBarrier() {
// Checks we don't sink across apply-site barrier
//
// CHECK-LABEL: sil @testApplyBarrier : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK: apply
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-LABEL: } // end sil function 'testApplyBarrier'
sil @testApplyBarrier : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
%u0 = function_ref @globalAddressor : $@convention(thin) () -> Builtin.RawPointer
%u1 = apply %u0() : $@convention(thin) () -> Builtin.RawPointer
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testUniquenessBarrier() {
// Checks we don't sink across a uniqueness check
//
// CHECK-LABEL: sil @testUniquenessBarrier : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-NEXT: is_unique
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-LABEL: } // end sil function 'testUniquenessBarrier'
sil @testUniquenessBarrier : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
is_unique %1 : $*X
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testBeginBarrier() {
// Checks we don't sink across begin_access barrier
//
// CHECK-LABEL: sil @testBeginBarrier : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-NEXT: [[BEGIN2:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: end_access [[BEGIN2]] : $*X
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-LABEL: } // end sil function 'testBeginBarrier'
sil @testBeginBarrier : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
%b0 = begin_access [modify] [dynamic] %0 : $*X
end_access %b0 : $*X
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testSinkCrossMultiEnds() {
// Checks that we choose the *bottom* end_access when sinking
//
// CHECK-LABEL: sil @testSinkCrossMultiEnds : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[BEGIN2:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: end_access [[BEGIN2]] : $*X
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-LABEL: } // end sil function 'testSinkCrossMultiEnds'
sil @testSinkCrossMultiEnds : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%b0 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
end_access %b0 : $*X
end_access %1 : $*X
%ret = tuple ()
return %ret : $()
}
// public func testSinkAfterBarrierEncounter() {
// Checks that we sink after barrier resetting
//
// CHECK-LABEL: sil @testSinkAfterBarrierEncounter : $@convention(thin) () -> () {
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
// CHECK-NEXT: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-NEXT: [[LOADED:%.*]] = load [[BEGIN]] : $*X
// CHECK-NEXT: end_access [[BEGIN]] : $*X
// CHECK-NEXT: release_value [[LOADED]]
// CHECK-NEXT: [[BEGIN2:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
// CHECK-LABEL: } // end sil function 'testSinkAfterBarrierEncounter'
sil @testSinkAfterBarrierEncounter : $@convention(thin) () -> () {
bb0:
%0 = global_addr @globalX: $*X
%1 = begin_access [modify] [dynamic] %0 : $*X
%2 = load %1 : $*X
release_value %2 : $X
end_access %1 : $*X
%b0 = begin_access [modify] [dynamic] %0 : $*X
end_access %b0 : $*X
%ret = tuple ()
return %ret : $()
}
// testSinkAfterEscapingClosureCheck:
// <rdar://problem/45846920> TestFoundation, TestProcess, closure
// argument passed as @noescape to Objective-C has escaped
class IntWrapper {
var value: Builtin.Int64
init(v: Builtin.Int64) {
value = v
}
}
sil @takesNoEscapeBlockClosure : $@convention(thin) (@guaranteed @convention(block) @noescape () -> ()) -> ()
sil @closureThatModifiesCapture: $@convention(thin) ({ var Builtin.Int64 }) -> ()
sil [reabstraction_thunk] @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
sil [reabstraction_thunk] @withoutActuallyEscapingThunk : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
// The Copy release cannot be moved below the destroy_not_escaped_closure.
//
// CHECK-LABEL: sil @testSinkAfterEscapingClosureCheck : $@convention(thin) (@guaranteed IntWrapper) -> () {
// CHECK: bb0(%0 : $IntWrapper):
// CHECK: [[BA:%.*]] = begin_access [read] [dynamic] %{{.*}} : $*Builtin.Int64
// CHECK: [[COPY:%.*]] = copy_block %{{.*}} : $@convention(block) @noescape () -> ()
// CHECK: [[F:%.*]] = function_ref @takesNoEscapeBlockClosure : $@convention(thin) (@guaranteed @convention(block) @noescape () -> ()) -> ()
// CHECK: apply [[F]]([[COPY]]) : $@convention(thin) (@guaranteed @convention(block) @noescape () -> ()) -> ()
// CHECK: strong_release [[COPY]] : $@convention(block) @noescape () -> ()
// CHECK: destroy_not_escaped_closure
// CHECK: cond_fail
// CHECK: end_access [[BA]] : $*Builtin.Int64
// CHECK-LABEL: } // end sil function 'testSinkAfterEscapingClosureCheck'
sil @testSinkAfterEscapingClosureCheck : $@convention(thin) (@guaranteed IntWrapper) -> () {
bb0(%0 : $IntWrapper):
%va = ref_element_addr %0 : $IntWrapper, #IntWrapper.value
%ba = begin_access [read] [dynamic] %va : $*Builtin.Int64
%value = load %ba : $*Builtin.Int64
%box = alloc_box ${ var Builtin.Int64 }
%boxaddr = project_box %box : ${ var Builtin.Int64 }, 0
store %value to %boxaddr : $*Builtin.Int64
%closureF = function_ref @closureThatModifiesCapture : $@convention(thin) ({ var Builtin.Int64 }) -> ()
%closure = partial_apply [callee_guaranteed] %closureF(%box) : $@convention(thin) ({ var Builtin.Int64 }) -> ()
%conv = convert_escape_to_noescape %closure : $@callee_guaranteed () -> () to $@noescape @callee_guaranteed () -> ()
%thunk = function_ref @withoutActuallyEscapingThunk : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
%pathunk = partial_apply [callee_guaranteed] %thunk(%conv) : $@convention(thin) (@noescape @callee_guaranteed () -> ()) -> ()
%md = mark_dependence %pathunk : $@callee_guaranteed () -> () on %conv : $@noescape @callee_guaranteed () -> ()
strong_retain %md : $@callee_guaranteed () -> ()
%allocblock = alloc_stack $@block_storage @callee_guaranteed () -> ()
%blockaddr = project_block_storage %allocblock : $*@block_storage @callee_guaranteed () -> ()
store %md to %blockaddr : $*@callee_guaranteed () -> ()
%blockF = function_ref @thunkForCalleeGuaranteed : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> ()
%initblock = init_block_storage_header %allocblock : $*@block_storage @callee_guaranteed () -> (), invoke %blockF : $@convention(c) (@inout_aliasable @block_storage @callee_guaranteed () -> ()) -> (), type $@convention(block) @noescape () -> ()
%copyblock = copy_block %initblock : $@convention(block) @noescape () -> ()
%f = function_ref @takesNoEscapeBlockClosure : $@convention(thin) (@guaranteed @convention(block) @noescape () -> ()) -> ()
%call = apply %f(%copyblock) : $@convention(thin) (@guaranteed @convention(block) @noescape () -> ()) -> ()
strong_release %copyblock : $@convention(block) @noescape () -> ()
%isescape = destroy_not_escaped_closure %md : $@callee_guaranteed () -> ()
cond_fail %isescape : $Builtin.Int1
end_access %ba : $*Builtin.Int64
destroy_addr %blockaddr : $*@callee_guaranteed() -> ()
dealloc_stack %allocblock : $*@block_storage @callee_guaranteed () -> ()
strong_release %box : ${ var Builtin.Int64 }
%ret = tuple ()
return %ret : $()
}