Skip to content

Commit 7b6fd23

Browse files
committed
BorrowIntroducer unit tests
1 parent 8dd3159 commit 7b6fd23

File tree

3 files changed

+326
-82
lines changed

3 files changed

+326
-82
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/Test.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ public func registerOptimizerTests() {
138138
registerFunctionTests(
139139
parseTestSpecificationTest,
140140
forwardingUseDefTest,
141-
forwardingDefUseTest
141+
forwardingDefUseTest,
142+
borrowIntroducersTest,
143+
enclosingValuesTest,
144+
linearLivenessTest,
145+
interiorLivenessTest
142146
)
143147

144148
// Finally register the thunk they all call through.

test/SILOptimizer/borrow_introducer_unit.sil

Lines changed: 178 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,81 @@ struct PairC {
1818
var second: C
1919
}
2020

21-
class C {}
21+
class C {
22+
var field: C
23+
}
2224

2325
class D : C {}
2426

27+
sil @coro : $@yield_once @convention(thin) () -> @yields @guaranteed C
28+
2529
// The introducer of an introducer is always itself.
2630

27-
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: @trace[0]
31+
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: %0
2832
// CHECK: } // end sil function 'introducer_identity'
2933
// CHECK: Introducers:
3034
// CHECK-NEXT: %0 = argument of bb0 : $C
31-
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: @trace[0]
35+
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: %0
36+
37+
// CHECK-LABEL: introducer_identity: borrow_introducers with: %0
38+
// CHECK: } // end sil function 'introducer_identity'
39+
// CHECK: Borrow introducers for: %0 = argument of bb0 : $C
40+
// CHECK-NEXT: %0 = argument of bb0 : $C
41+
// CHECK-NEXT: introducer_identity: borrow_introducers with: %0
3242

33-
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: @trace[1]
43+
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: %borrow1
3444
// CHECK: } // end sil function 'introducer_identity'
3545
// CHECK: Introducers:
3646
// CHECK-NEXT: begin_borrow %0 : $C
37-
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: @trace[1]
47+
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: %borrow1
3848

39-
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: @trace[2]
49+
// CHECK-LABEL: introducer_identity: borrow_introducers with: %borrow1
50+
// CHECK: } // end sil function 'introducer_identity'
51+
// CHECK: Borrow introducers for: %{{.*}} = begin_borrow %0 : $C
52+
// CHECK-NEXT: begin_borrow %0 : $C
53+
// CHECK-NEXT: introducer_identity: borrow_introducers with: %borrow1
54+
55+
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: %borrow2
4056
// CHECK: } // end sil function 'introducer_identity'
4157
// CHECK: Introducers:
4258
// CHECK-NEXT: load_borrow %1 : $*C
43-
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: @trace[2]
59+
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: %borrow2
60+
61+
// CHECK-LABEL: introducer_identity: borrow_introducers with: %borrow2
62+
// CHECK: } // end sil function 'introducer_identity'
63+
// CHECK: Borrow introducers for: {{.*}} = load_borrow %1 : $*C
64+
// CHECK-NEXT: load_borrow %1 : $*C
65+
// CHECK-NEXT: introducer_identity: borrow_introducers with: %borrow2
4466

45-
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: @trace[3]
67+
// CHECK-LABEL: introducer_identity: find-borrow-introducers with: %reborrow
4668
// CHECK: } // end sil function 'introducer_identity'
4769
// CHECK: Introducers:
4870
// CHECK-NEXT: argument of bb1 : $C
49-
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: @trace[3]
71+
// CHECK-NEXT: introducer_identity: find-borrow-introducers with: %reborrow
5072

73+
// CHECK-LABEL: introducer_identity: borrow_introducers with: %reborrow
74+
// CHECK: } // end sil function 'introducer_identity'
75+
// CHECK: Borrow introducers for: %{{.*}} = argument of bb1 : $C
76+
// CHECK-NEXT: argument of bb1 : $C
77+
// CHECK-NEXT: introducer_identity: borrow_introducers with: %reborrow
5178
sil [ossa] @introducer_identity : $@convention(thin) (@guaranteed C, @in C) -> () {
5279
entry(%0 : @guaranteed $C, %1 : $*C):
53-
specify_test "find-borrow-introducers @trace[0]"
54-
debug_value [trace] %0 : $C
80+
specify_test "find-borrow-introducers %0"
81+
specify_test "borrow_introducers %0"
5582
%borrow1 = begin_borrow %0 : $C
56-
specify_test "find-borrow-introducers @trace[1]"
57-
debug_value [trace] %borrow1 : $C
83+
specify_test "find-borrow-introducers %borrow1"
84+
specify_test "borrow_introducers %borrow1"
5885

5986
%borrow2 = load_borrow %1 : $*C
60-
specify_test "find-borrow-introducers @trace[2]"
61-
debug_value [trace] %borrow2 : $C
87+
specify_test "find-borrow-introducers %borrow2"
88+
specify_test "borrow_introducers %borrow2"
6289
end_borrow %borrow2 : $C
6390

6491
br exit(%borrow1 : $C)
6592

6693
exit(%reborrow : @guaranteed $C):
67-
specify_test "find-borrow-introducers @trace[3]"
68-
debug_value [trace] %reborrow : $C
94+
specify_test "find-borrow-introducers %reborrow"
95+
specify_test "borrow_introducers %reborrow"
6996
end_borrow %reborrow : $C
7097
destroy_addr %1 : $*C
7198
%retval = tuple ()
@@ -75,19 +102,24 @@ exit(%reborrow : @guaranteed $C):
75102
// There is no introducer if the guaranteed value is produced from a
76103
// trivial value.
77104

78-
// CHECK-LABEL: introducer_trivial: find-borrow-introducers with: @trace[0]
105+
// CHECK-LABEL: introducer_trivial: find-borrow-introducers with: %phi
79106
// CHECK: } // end sil function 'introducer_trivial'
80107
// CHECK: Introducers:
81-
// CHECK-NEXT: introducer_trivial: find-borrow-introducers with: @trace[0]
108+
// CHECK-NEXT: introducer_trivial: find-borrow-introducers with: %phi
109+
110+
// CHECK-LABEL: introducer_trivial: borrow_introducers with: %phi
111+
// CHECK: } // end sil function 'introducer_trivial'
112+
// CHECK: Borrow introducers for: %2 = argument of bb1 : $FakeOptional<C>
113+
// CHECK-NEXT: introducer_trivial: borrow_introducers with: %phi
82114

83115
sil [ossa] @introducer_trivial : $@convention(thin) () -> () {
84116
entry:
85117
%trivial = enum $FakeOptional<C>, #FakeOptional.none!enumelt
86118
br none(%trivial : $FakeOptional<C>)
87119

88120
none(%phi : @guaranteed $FakeOptional<C>):
89-
specify_test "find-borrow-introducers @trace[0]"
90-
debug_value [trace] %phi : $FakeOptional<C>
121+
specify_test "find-borrow-introducers %phi"
122+
specify_test "borrow_introducers %phi"
91123
%retval = tuple ()
92124
return %retval : $()
93125
}
@@ -97,19 +129,24 @@ none(%phi : @guaranteed $FakeOptional<C>):
97129
// TODO: When we have a reborrow flag, an unreachable phi can be
98130
// either a reborrow or forwarded, as long as it has no end_borrow.
99131

100-
// CHECK-LABEL: introducer_unreachable: find-borrow-introducers with: @trace[0]
132+
// CHECK-LABEL: introducer_unreachable: find-borrow-introducers with: %phiCycle
101133
// CHECK: } // end sil function 'introducer_unreachable'
102134
// CHECK: Introducers:
103135
// CHECK-NEXT: argument of bb1 : $C
104-
// CHECK-NEXT: introducer_unreachable: find-borrow-introducers with: @trace[0]
136+
// CHECK-NEXT: introducer_unreachable: find-borrow-introducers with: %phiCycle
105137

138+
// CHECK-LABEL: introducer_unreachable: borrow_introducers with: %phiCycle
139+
// CHECK: } // end sil function 'introducer_unreachable'
140+
// CHECK: Borrow introducers for: %1 = argument of bb1 : $C
141+
// CHECK-NEXT: argument of bb1 : $C
142+
// CHECK-NEXT: introducer_unreachable: borrow_introducers with: %phiCycle
106143
sil [ossa] @introducer_unreachable : $@convention(thin) () -> () {
107144
entry:
108145
br exit
109146

110147
unreachable_loop(%phiCycle : @guaranteed $C):
111-
specify_test "find-borrow-introducers @trace[0]"
112-
debug_value [trace] %phiCycle : $C
148+
specify_test "find-borrow-introducers %phiCycle"
149+
specify_test "borrow_introducers %phiCycle"
113150
br unreachable_loop(%phiCycle : $C)
114151

115152
exit:
@@ -120,12 +157,17 @@ exit:
120157
// All data flow paths through phis and aggregates originate from the
121158
// same function argument.
122159

123-
// CHECK-LABEL: introducer_single: find-borrow-introducers with: @trace[0]
160+
// CHECK-LABEL: introducer_single: find-borrow-introducers with: %aggregate2
124161
// CHECK: } // end sil function 'introducer_single'
125162
// CHECK: Introducers:
126163
// CHECK-NEXT: %0 = argument of bb0 : $C
127-
// CHECK-NEXT: introducer_single: find-borrow-introducers with: @trace[0]
164+
// CHECK-NEXT: introducer_single: find-borrow-introducers with: %aggregate2
128165

166+
// CHECK-LABEL: introducer_single: borrow_introducers with: %aggregate2
167+
// CHECK: } // end sil function 'introducer_single'
168+
// CHECK: Borrow introducers for: %11 = struct $PairC (%9 : $C, %10 : $C)
169+
// CHECK-NEXT: %0 = argument of bb0 : $C
170+
// CHECK-NEXT: introducer_single: borrow_introducers with: %aggregate2
129171
sil [ossa] @introducer_single : $@convention(thin) (@guaranteed C) -> () {
130172
entry(%0 : @guaranteed $C):
131173
%cast = unconditional_checked_cast %0 : $C to D
@@ -141,8 +183,8 @@ bb1(%payload : @guaranteed $D):
141183
%first = struct_extract %aggregate : $PairC, #PairC.first
142184
%second = struct_extract %aggregate : $PairC, #PairC.second
143185
%aggregate2 = struct $PairC(%first : $C, %second : $C)
144-
specify_test "find-borrow-introducers @trace[0]"
145-
debug_value [trace] %aggregate2 : $PairC
186+
specify_test "find-borrow-introducers %aggregate2"
187+
specify_test "borrow_introducers %aggregate2"
146188
br exit
147189

148190
bb2:
@@ -161,14 +203,21 @@ exit:
161203
// %reborrow. Therefore, %reborrow is an introducer from separate phi
162204
// paths, but should only appear in the introducer list once.
163205
//
164-
// CHECK-LABEL: introducer_revisit_reborrow: find-borrow-introducers with: @trace[0]
206+
// CHECK-LABEL: introducer_revisit_reborrow: find-borrow-introducers with: %aggregate2
165207
// CHECK: bb1([[REBORROW:%.*]] : @reborrow @guaranteed $C
166208
// CHECK: } // end sil function 'introducer_revisit_reborrow'
167209
// CHECK: Introducers:
168210
// CHECK-NEXT: [[REBORROW]] = argument of bb1 : $C
169211
// CHECK-NEXT: %0 = argument of bb0 : $C
170-
// CHECK-NEXT: introducer_revisit_reborrow: find-borrow-introducers with: @trace[0]
212+
// CHECK-NEXT: introducer_revisit_reborrow: find-borrow-introducers with: %aggregate2
171213

214+
// CHECK-LABEL: introducer_revisit_reborrow: borrow_introducers with: %aggregate2
215+
// CHECK: bb1([[REBORROW:%.*]] : @reborrow @guaranteed $C
216+
// CHECK: } // end sil function 'introducer_revisit_reborrow'
217+
// CHECK: Borrow introducers for: %{{.*}} = struct $PairC (%{{.*}} : $C, %{{.*}} : $C)
218+
// CHECK-NEXT: [[REBORROW]] = argument of bb1 : $C
219+
// CHECK-NEXT: %0 = argument of bb0 : $C
220+
// CHECK-NEXT: introducer_revisit_reborrow: borrow_introducers with: %aggregate2
172221
sil [ossa] @introducer_revisit_reborrow : $@convention(thin) (@guaranteed C) -> () {
173222
entry(%0 : @guaranteed $C):
174223
%borrow1 = begin_borrow %0 : $C
@@ -178,8 +227,8 @@ entry(%0 : @guaranteed $C):
178227
bb2(%reborrow : @guaranteed $C, %phi : @guaranteed $PairC):
179228
%first = struct_extract %phi : $PairC, #PairC.first
180229
%aggregate2 = struct $PairC(%reborrow : $C, %first : $C)
181-
specify_test "find-borrow-introducers @trace[0]"
182-
debug_value [trace] %aggregate2 : $PairC
230+
specify_test "find-borrow-introducers %aggregate2"
231+
specify_test "borrow_introducers %aggregate2"
183232
br exit
184233

185234
exit:
@@ -191,7 +240,7 @@ exit:
191240
// %phi originates from %0, %borrow1, & %borrow2. %borrow1 is, however,
192241
// reborrowed in bb2.
193242

194-
// CHECK-LABEL: introducer_multiple_borrow: find-borrow-introducers with: @trace[0]
243+
// CHECK-LABEL: introducer_multiple_borrow: find-borrow-introducers with: %aggregate2
195244
// CHECK: begin_borrow %0 : $C
196245
// CHECK: [[BORROW2:%.*]] = begin_borrow %0 : $C
197246
// CHECK: bb1([[REBORROW:%.*]] : @reborrow @guaranteed $C
@@ -200,8 +249,18 @@ exit:
200249
// CHECK-NEXT: [[REBORROW]] = argument of bb1 : $C
201250
// CHECK-NEXT: %0 = argument of bb0 : $C
202251
// CHECK-NEXT: [[BORROW2]] = begin_borrow %0 : $C
203-
// CHECK-NEXT: introducer_multiple_borrow: find-borrow-introducers with: @trace[0]
252+
// CHECK-NEXT: introducer_multiple_borrow: find-borrow-introducers with: %aggregate2
204253

254+
// CHECK-LABEL: introducer_multiple_borrow: borrow_introducers with: %aggregate2
255+
// CHECK: begin_borrow %0 : $C
256+
// CHECK: [[BORROW2:%.*]] = begin_borrow %0 : $C
257+
// CHECK: bb1([[REBORROW:%.*]] : @reborrow @guaranteed $C
258+
// CHECK: } // end sil function 'introducer_multiple_borrow'
259+
// CHECK: Borrow introducers for: %{{.*}} = struct $PairC (%{{.*}} : $C, %{{.*}} : $C)
260+
// CHECK-NEXT: [[REBORROW]] = argument of bb1 : $C
261+
// CHECK-NEXT: %0 = argument of bb0 : $C
262+
// CHECK-NEXT: [[BORROW2]] = begin_borrow %0 : $C
263+
// CHECK-NEXT: introducer_multiple_borrow: borrow_introducers with: %aggregate2
205264
sil [ossa] @introducer_multiple_borrow : $@convention(thin) (@guaranteed C) -> () {
206265
entry(%0 : @guaranteed $C):
207266
%borrow1 = begin_borrow %0 : $C
@@ -212,8 +271,8 @@ entry(%0 : @guaranteed $C):
212271
bb2(%reborrow : @guaranteed $C, %phi : @guaranteed $PairC):
213272
%first = struct_extract %phi : $PairC, #PairC.first
214273
%aggregate2 = struct $PairC(%reborrow : $C, %first : $C)
215-
specify_test "find-borrow-introducers @trace[0]"
216-
debug_value [trace] %aggregate2 : $PairC
274+
specify_test "find-borrow-introducers %aggregate2"
275+
specify_test "borrow_introducers %aggregate2"
217276
br exit
218277

219278
exit:
@@ -223,28 +282,102 @@ exit:
223282
return %retval : $()
224283
}
225284

226-
// CHECK-LABEL: begin running test 1 of 1 on introducer_dependence: find-borrow-introducers with: @trace[0]
285+
// CHECK-LABEL: introducer_dependence: find-borrow-introducers with: %dependent
227286
// CHECK: Introducers:
228287
// CHECK: %0 = argument of bb0 : $C
229-
// CHECK-LABEL: end running test 1 of 1 on introducer_dependence: find-borrow-introducers with: @trace[0]
288+
// CHECK-LABEL: introducer_dependence: find-borrow-introducers with: %dependent
289+
290+
// CHECK-LABEL: introducer_dependence: borrow_introducers with: %dependent
291+
// CHECK: Borrow introducers for: %{{.*}} = mark_dependence %0 : $C on %1 : $Builtin.NativeObject
292+
// CHECK: %0 = argument of bb0 : $C
293+
// CHECK-NEXT: introducer_dependence: borrow_introducers with: %dependent
230294
sil [ossa] @introducer_dependence : $@convention(thin) (@guaranteed C, @guaranteed Builtin.NativeObject) -> () {
231295
entry(%0 : @guaranteed $C, %1 : @guaranteed $Builtin.NativeObject):
232-
specify_test "find-borrow-introducers @trace[0]"
296+
specify_test "find-borrow-introducers %dependent"
297+
specify_test "borrow_introducers %dependent"
233298
%dependent = mark_dependence %0 : $C on %1 : $Builtin.NativeObject
234-
debug_value [trace] %dependent : $C
235299
%retval = tuple ()
236300
return %retval : $()
237301
}
238302

239-
// CHECK-LABEL: begin running test 1 of 1 on introducer_bridge: find-borrow-introducers with: @trace[0]
303+
// CHECK-LABEL: introducer_bridge: find-borrow-introducers with: %bridge
240304
// CHECK: Introducers:
241305
// CHECK: %0 = argument of bb0 : $C
242-
// CHECK-LABEL: end running test 1 of 1 on introducer_bridge: find-borrow-introducers with: @trace[0]
306+
// CHECK-LABEL: introducer_bridge: find-borrow-introducers with: %bridge
307+
308+
// CHECK-LABEL: introducer_bridge: borrow_introducers with: %bridge
309+
// CHECK: Borrow introducers for: %{{.*}} = ref_to_bridge_object %0 : $C, %1 : $Builtin.Word
310+
// CHECK: %0 = argument of bb0 : $C
311+
// CHECK-NEXT: introducer_bridge: borrow_introducers with: %bridge
243312
sil [ossa] @introducer_bridge : $@convention(thin) (@guaranteed C, Builtin.Word) -> () {
244313
entry(%0 : @guaranteed $C, %1 : $Builtin.Word):
245-
specify_test "find-borrow-introducers @trace[0]"
314+
specify_test "find-borrow-introducers %bridge"
315+
specify_test "borrow_introducers %bridge"
246316
%bridge = ref_to_bridge_object %0 : $C, %1 : $Builtin.Word
247-
debug_value [trace] %bridge : $Builtin.BridgeObject
317+
%retval = tuple ()
318+
return %retval : $()
319+
}
320+
321+
// CHECK-LABEL: introducer_begin_apply: borrow_introducers with: %bridge
322+
// CHECK: Borrow introducers for: %{{.*}} = ref_to_bridge_object %{{.*}} : $C, %0 : $Builtin.Word
323+
// CHECK-NEXT: (**%{{.*}}**, %{{.*}}) = begin_apply %{{.*}}() : $@yield_once @convention(thin) () -> @yields @guaranteed C
324+
// CHECK-NEXT: introducer_begin_apply: borrow_introducers with: %bridge
325+
sil [ossa] @introducer_begin_apply : $@convention(thin) (Builtin.Word) -> () {
326+
bb0(%0 : $Builtin.Word):
327+
%f = function_ref @coro : $@yield_once @convention(thin) () -> @yields @guaranteed C
328+
(%c, %t) = begin_apply %f() : $@yield_once @convention(thin) () -> @yields @guaranteed C
329+
specify_test "borrow_introducers %bridge"
330+
%bridge = ref_to_bridge_object %c : $C, %0 : $Builtin.Word
331+
cond_br undef, bb1, bb2
332+
bb1:
333+
end_apply %t
334+
%r = tuple ()
335+
return %r : $()
336+
bb2:
337+
abort_apply %t
338+
unreachable
339+
}
340+
341+
// CHECK-LABEL: introducer_example: borrow_introducers with: %0
342+
// CHECK: Borrow introducers for: %0 = argument of bb0 : $C
343+
// CHECK-NEXT: %0 = argument of bb0 : $C
344+
// CHECK-NEXT: introducer_example: borrow_introducers with: %0
345+
346+
// CHECK-LABEL: introducer_example: borrow_introducers with: %borrow0
347+
// CHECK: Borrow introducers for: %{{.*}} = begin_borrow %0 : $C
348+
// CHECK-NEXT: %{{.*}} = begin_borrow %0 : $C
349+
// CHECK-NEXT: introducer_example: borrow_introducers with: %borrow0
350+
351+
// CHECK-LABEL: introducer_example: borrow_introducers with: %first
352+
// CHECK: Borrow introducers for: %{{.*}} = struct_extract %{{.*}} : $PairC, #PairC.first
353+
// CHECK-NEXT: %{{.*}} = begin_borrow %0 : $C
354+
// CHECK-NEXT: %{{.*}} = argument of bb0 : $C
355+
// CHECK-NEXT: introducer_example: borrow_introducers with: %first
356+
357+
// CHECK-LABEL: introducer_example: borrow_introducers with: %field
358+
// CHECK: Borrow introducers for: %{{.*}} = ref_element_addr %{{.*}} : $C, #C.field
359+
// CHECK-NEXT: introducer_example: borrow_introducers with: %field
360+
361+
// CHECK-LABEL: introducer_example: borrow_introducers with: %load
362+
// CHECK-LABEL: Borrow introducers for: %{{.*}} = load_borrow %{{.*}} : $*C
363+
// CHECK-NEXT: %{{.*}} = load_borrow %{{.*}} : $*C
364+
// CHECK-NEXT: introducer_example: borrow_introducers with: %
365+
sil [ossa] @introducer_example : $@convention(thin) (@owned C, @guaranteed C) -> () {
366+
bb0(%0 : @owned $C,
367+
%1 : @guaranteed $C):
368+
specify_test "borrow_introducers %0"
369+
%borrow0 = begin_borrow %0 : $C
370+
specify_test "borrow_introducers %borrow0"
371+
%pair = struct $PairC(%borrow0 : $C, %1 : $C)
372+
%first = struct_extract %pair : $PairC, #PairC.first
373+
specify_test "borrow_introducers %first"
374+
%field = ref_element_addr %first : $C, #C.field
375+
specify_test "borrow_introducers %field"
376+
%load = load_borrow %field : $*C
377+
specify_test "borrow_introducers %load"
378+
end_borrow %load : $C
379+
end_borrow %borrow0 : $C
380+
destroy_value %0 : $C
248381
%retval = tuple ()
249382
return %retval : $()
250383
}

0 commit comments

Comments
 (0)