Skip to content

Commit bd157b3

Browse files
committed
ForwardingUtils.swift unit tests
1 parent 75ea382 commit bd157b3

File tree

2 files changed

+300
-0
lines changed

2 files changed

+300
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/ForwardingUtils.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,20 @@ private struct VisitForwardedUses : ForwardingDefUseWalker {
238238
return visitor(.deadValue(value))
239239
}
240240
}
241+
242+
let forwardingUseDefTest = FunctionTest("forwarding_use_def_test") {
243+
function, arguments, context in
244+
let value = arguments.takeValue()
245+
for introducer in gatherLifetimeIntroducers(for: value, context) {
246+
print("INTRODUCER: \(introducer)")
247+
}
248+
}
249+
250+
let forwardingDefUseTest = FunctionTest("forwarding_def_use_test") {
251+
function, arguments, context in
252+
let value = arguments.takeValue()
253+
_ = visitForwardedUses(introducer: value, context) { operand in
254+
print("USE: \(operand)")
255+
return .continueWalk
256+
}
257+
}
Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
// RUN: %target-sil-opt -test-runner -enable-sil-opaque-values %s -o /dev/null 2>&1 | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
5+
sil_stage raw
6+
7+
import Builtin
8+
9+
class C {
10+
init()
11+
}
12+
13+
struct CPair {
14+
var a: C
15+
var b: C
16+
}
17+
18+
public enum OptionalC {
19+
case none
20+
case some(C)
21+
}
22+
23+
// Test all the basic singleForwarededOperand implementations.
24+
//
25+
// CHECK-LABEL: begin running test 1 of 2 on forwardingTest: forwarding_def_use_test with: %0
26+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $(OptionalC, C)
27+
// CHECK-LABEL: end running test 1 of 2 on forwardingTest: forwarding_def_use_test with: %0
28+
//
29+
// CHECK-LABEL: begin running test 2 of 2 on forwardingTest: forwarding_use_def_test with: %final
30+
// CHECK: INTRODUCER: %0 = argument of bb0 : $C
31+
// CHECK: INTRODUCER: %1 = argument of bb0 : $C
32+
// CHECK-LABEL: end running test 2 of 2 on forwardingTest: forwarding_use_def_test with: %final
33+
sil [ossa] @forwardingTest : $@convention(thin) (@owned C, @owned C) -> () {
34+
entry(%0 : @owned $C, %1 : @owned $C):
35+
%word0 = integer_literal $Builtin.Word, 0
36+
%word1 = integer_literal $Builtin.Word, 1
37+
%bridge = ref_to_bridge_object %0: $C, %word0: $Builtin.Word
38+
%ref = bridge_object_to_ref %bridge: $Builtin.BridgeObject to $C
39+
%dependence = mark_dependence %ref: $C on %1: $C
40+
%cast = unchecked_ref_cast %dependence: $C to $C
41+
%pair1 = struct $CPair (%cast: $C, %1: $C)
42+
(%pair_a, %pair_b) = destructure_struct %pair1: $CPair
43+
%optional = enum $OptionalC, #OptionalC.some!enumelt, %pair_a: $C
44+
%final = tuple (%optional: $OptionalC, %pair_b: $C)
45+
46+
specify_test "forwarding_def_use_test %0"
47+
specify_test "forwarding_use_def_test %final"
48+
49+
destroy_value %final: $(OptionalC, C)
50+
%void = tuple()
51+
return %void : $()
52+
}
53+
54+
// In opaque SIL values mode, handle the tuple_pack_extract forwaded operand.
55+
//
56+
// CHECK-LABEL: begin running test 1 of 2 on tuplePackExtractTest: forwarding_def_use_test with: %tuple
57+
// CHECK: USE: operand #0 of end_borrow %1 : $(repeat each T)
58+
// CHECK: USE: operand #0 of %{{.*}} = copy_value %{{.*}} : $@pack_element("00000000-0000-0000-0000-000000000002") each U_1
59+
// CHECK-LABEL: end running test 1 of 2 on tuplePackExtractTest: forwarding_def_use_test with: %tuple
60+
//
61+
// CHECK-LABEL: begin running test 2 of 2 on tuplePackExtractTest: forwarding_use_def_test with: %final
62+
// CHECK: INTRODUCER: %1 = load_borrow %0 : $*(repeat each T)
63+
// CHECK-LABEL: end running test 2 of 2 on tuplePackExtractTest: forwarding_use_def_test with: %final
64+
sil [ossa] @tuplePackExtractTest : $@convention(thin) <each T> (@in_guaranteed (repeat each T)) -> () {
65+
entry(%tuple_addr : $*(repeat each T)):
66+
%tuple = load_borrow %tuple_addr : $*(repeat each T)
67+
%zero = integer_literal $Builtin.Word, 0
68+
%index = dynamic_pack_index %zero of $Pack{repeat each T}
69+
%opened = open_pack_element %index of <each U_1> at <Pack{repeat each T}>, shape $U_1, uuid "00000000-0000-0000-0000-000000000002"
70+
%final = tuple_pack_extract %index of %tuple : $(repeat each T) as $@pack_element("00000000-0000-0000-0000-000000000002") U_1
71+
72+
specify_test "forwarding_def_use_test %tuple"
73+
specify_test "forwarding_use_def_test %final"
74+
75+
%copy = copy_value %final : $@pack_element("00000000-0000-0000-0000-000000000002") U_1
76+
end_borrow %tuple : $(repeat each T)
77+
destroy_value %copy : $@pack_element("00000000-0000-0000-0000-000000000002") U_1
78+
%retval = tuple ()
79+
return %retval : $()
80+
}
81+
82+
// Handle forwaded guaranteed values.
83+
//
84+
// CHECK-LABEL: begin running test 1 of 2 on forwardingGuaranteedTest: forwarding_def_use_test with: %0
85+
// CHECK: USE: dead value: %5 = tuple (%3 : $C, %4 : $C)
86+
// CHECK-LABEL: end running test 1 of 2 on forwardingGuaranteedTest: forwarding_def_use_test with: %0
87+
//
88+
// CHECK-LABEL: begin running test 2 of 2 on forwardingGuaranteedTest: forwarding_use_def_test with: %t1
89+
// CHECK: INTRODUCER: %0 = argument of bb0 : $C
90+
// CHECK: INTRODUCER: %1 = argument of bb0 : $C
91+
// CHECK-LABEL: end running test 2 of 2 on forwardingGuaranteedTest: forwarding_use_def_test with: %t1
92+
sil [ossa] @forwardingGuaranteedTest : $@convention(thin) (@guaranteed C, @guaranteed C) -> () {
93+
entry(%0 : @guaranteed $C, %1 : @guaranteed $C):
94+
%t0 = tuple (%0: $C, %1: $C)
95+
%t00 = tuple_extract %t0: $(C, C), 0
96+
%t01 = tuple_extract %t0: $(C, C), 1
97+
%t1 = tuple (%t00: $C, %t01: $C)
98+
99+
specify_test "forwarding_def_use_test %0"
100+
specify_test "forwarding_use_def_test %t1"
101+
102+
%void = tuple()
103+
return %void : $()
104+
}
105+
106+
// A borrow scope currently intoduces a lifetime.
107+
//
108+
// CHECK-LABEL: begin running test 1 of 2 on forwardingBorrowTest: forwarding_def_use_test with: %0
109+
// CHECK: USE: operand #0 of destroy_value %0 : $CPair
110+
// CHECK: USE: operand #0 of %{{.*}} = begin_borrow %0 : $CPair
111+
// CHECK-LABEL: end running test 1 of 2 on forwardingBorrowTest: forwarding_def_use_test with: %0
112+
//
113+
// CHECK-LABEL: begin running test 2 of 2 on forwardingBorrowTest: forwarding_use_def_test with: %field
114+
// CHECK: INTRODUCER: %{{.*}} = begin_borrow %0 : $CPair
115+
// CHECK-LABEL: end running test 2 of 2 on forwardingBorrowTest: forwarding_use_def_test with: %field
116+
sil [ossa] @forwardingBorrowTest : $@convention(thin) (@owned CPair) -> () {
117+
entry(%0 : @owned $CPair):
118+
%borrow = begin_borrow %0 : $CPair
119+
%field = struct_extract %borrow: $CPair, #CPair.a
120+
121+
specify_test "forwarding_def_use_test %0"
122+
specify_test "forwarding_use_def_test %field"
123+
124+
end_borrow %borrow : $CPair
125+
destroy_value %0: $CPair
126+
%void = tuple()
127+
return %void : $()
128+
}
129+
130+
// Test forwarding switch_enum.
131+
//
132+
// CHECK-LABEL: begin running test 1 of 2 on forwardingSwitchTest: forwarding_def_use_test with: %0
133+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $C
134+
// CHECK-LABEL: end running test 1 of 2 on forwardingSwitchTest: forwarding_def_use_test with: %0
135+
//
136+
// CHECK-LABEL: begin running test 2 of 2 on forwardingSwitchTest: forwarding_use_def_test with: %result
137+
// CHECK: INTRODUCER: %0 = argument of bb0 : $OptionalC
138+
// CHECK-LABEL: end running test 2 of 2 on forwardingSwitchTest: forwarding_use_def_test with: %result
139+
sil [ossa] @forwardingSwitchTest : $@convention(thin) (@owned OptionalC) -> () {
140+
entry(%0 : @owned $OptionalC):
141+
specify_test "forwarding_def_use_test %0"
142+
switch_enum %0 : $OptionalC, case #OptionalC.some: bbSome, default bbNone
143+
144+
bbSome(%result: @owned $C):
145+
specify_test "forwarding_use_def_test %result"
146+
destroy_value %result: $C
147+
br bbExit
148+
149+
bbNone:
150+
br bbExit
151+
152+
bbExit:
153+
%void = tuple()
154+
return %void : $()
155+
}
156+
157+
// Test forwarding checked_cast_br.
158+
//
159+
// CHECK-LABEL: begin running test 1 of 2 on forwardingCheckedCastTest: forwarding_def_use_test with: %0
160+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $C
161+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $C
162+
// CHECK-LABEL: end running test 1 of 2 on forwardingCheckedCastTest: forwarding_def_use_test with: %0
163+
//
164+
// CHECK-LABEL: begin running test 2 of 2 on forwardingCheckedCastTest: forwarding_use_def_test with: %passResult
165+
// CHECK: INTRODUCER: %0 = argument of bb0 : $C
166+
// CHECK-LABEL: end running test 2 of 2 on forwardingCheckedCastTest: forwarding_use_def_test with: %passResult
167+
sil [ossa] @forwardingCheckedCastTest : $@convention(thin) (@owned C) -> () {
168+
entry(%0 : @owned $C):
169+
specify_test "forwarding_def_use_test %0"
170+
checked_cast_br C in %0 : $C to C, bbPass, bbFail
171+
172+
bbPass(%passResult: @owned $C):
173+
specify_test "forwarding_use_def_test %passResult"
174+
destroy_value %passResult: $C
175+
br bbExit
176+
177+
bbFail(%failResult: @owned $C):
178+
destroy_value %failResult: $C
179+
br bbExit
180+
181+
bbExit:
182+
%void = tuple()
183+
return %void : $()
184+
}
185+
186+
// Test forwarding phis.
187+
//
188+
// CHECK-LABEL: begin running test 1 of 2 on forwardingPhiTest: forwarding_def_use_test with: %0
189+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $C
190+
// CHECK-LABEL: end running test 1 of 2 on forwardingPhiTest: forwarding_def_use_test with: %0
191+
//
192+
// CHECK-LABEL: begin running test 2 of 2 on forwardingPhiTest: forwarding_use_def_test with: %cast
193+
// CHECK: INTRODUCER: %0 = argument of bb0 : $C
194+
// CHECK-LABEL: end running test 2 of 2 on forwardingPhiTest: forwarding_use_def_test with: %cast
195+
sil [ossa] @forwardingPhiTest : $@convention(thin) (@owned C) -> () {
196+
entry(%0 : @owned $C):
197+
specify_test "forwarding_def_use_test %0"
198+
br bbLoop(%0 : $C)
199+
200+
bbLoop(%loop : @owned $C):
201+
%cast = unchecked_ref_cast %loop: $C to $C
202+
cond_br undef, bbTail, bbExit
203+
204+
bbTail:
205+
br bbLoop(%cast: $C)
206+
207+
bbExit:
208+
specify_test "forwarding_use_def_test %cast"
209+
destroy_value %cast: $C
210+
%void = tuple()
211+
return %void : $()
212+
}
213+
214+
// Chain of owned values. Needs to resolve in a resonable amount of time.
215+
//
216+
// CHECK-LABEL: begin running test 1 of 2 on forwardingTuplesTest: forwarding_def_use_test with: %0
217+
// CHECK: USE: operand #0 of destroy_value %{{.*}} : $(C, C)
218+
// CHECK-LABEL: end running test 1 of 2 on forwardingTuplesTest: forwarding_def_use_test with: %0
219+
//
220+
// CHECK-LABEL: begin running test 2 of 2 on forwardingTuplesTest: forwarding_use_def_test with: %final
221+
// CHECK: INTRODUCER: %0 = argument of bb0 : $C
222+
// CHECK: INTRODUCER: %1 = argument of bb0 : $C
223+
// CHECK-LABEL: end running test 2 of 2 on forwardingTuplesTest: forwarding_use_def_test with: %final
224+
sil [ossa] @forwardingTuplesTest : $@convention(thin) (@owned C, @owned C) -> () {
225+
entry(%0 : @owned $C, %1 : @owned $C):
226+
specify_test "forwarding_def_use_test %0"
227+
228+
%t0 = tuple (%0: $C, %1: $C)
229+
(%t1_0, %t1_1) = destructure_tuple %t0: $(C, C)
230+
%t1 = tuple (%t1_0: $C, %t1_1: $C)
231+
(%t2_0, %t2_1) = destructure_tuple %t1: $(C, C)
232+
%t2 = tuple (%t2_0: $C, %t2_1: $C)
233+
(%t3_0, %t3_1) = destructure_tuple %t2: $(C, C)
234+
%t3 = tuple (%t3_0: $C, %t3_1: $C)
235+
(%t4_0, %t4_1) = destructure_tuple %t3: $(C, C)
236+
%t4 = tuple (%t4_0: $C, %t4_1: $C)
237+
(%t5_0, %t5_1) = destructure_tuple %t4: $(C, C)
238+
%t5 = tuple (%t5_0: $C, %t5_1: $C)
239+
(%t6_0, %t6_1) = destructure_tuple %t5: $(C, C)
240+
%t6 = tuple (%t6_0: $C, %t6_1: $C)
241+
(%t7_0, %t7_1) = destructure_tuple %t6: $(C, C)
242+
%t7 = tuple (%t7_0: $C, %t7_1: $C)
243+
(%t8_0, %t8_1) = destructure_tuple %t7: $(C, C)
244+
%t8 = tuple (%t8_0: $C, %t8_1: $C)
245+
(%t9_0, %t9_1) = destructure_tuple %t8: $(C, C)
246+
%t9 = tuple (%t9_0: $C, %t9_1: $C)
247+
(%t10_0, %t10_1) = destructure_tuple %t9: $(C, C)
248+
%t10 = tuple (%t10_0: $C, %t10_1: $C)
249+
(%t11_0, %t11_1) = destructure_tuple %t10: $(C, C)
250+
%t11 = tuple (%t11_0: $C, %t11_1: $C)
251+
(%t12_0, %t12_1) = destructure_tuple %t11: $(C, C)
252+
%t12 = tuple (%t12_0: $C, %t12_1: $C)
253+
(%t13_0, %t13_1) = destructure_tuple %t12: $(C, C)
254+
%t13 = tuple (%t13_0: $C, %t13_1: $C)
255+
(%t14_0, %t14_1) = destructure_tuple %t13: $(C, C)
256+
%t14 = tuple (%t14_0: $C, %t14_1: $C)
257+
(%t15_0, %t15_1) = destructure_tuple %t14: $(C, C)
258+
%t15 = tuple (%t15_0: $C, %t15_1: $C)
259+
(%t16_0, %t16_1) = destructure_tuple %t15: $(C, C)
260+
%t16 = tuple (%t16_0: $C, %t16_1: $C)
261+
(%t17_0, %t17_1) = destructure_tuple %t16: $(C, C)
262+
%t17 = tuple (%t17_0: $C, %t17_1: $C)
263+
(%t18_0, %t18_1) = destructure_tuple %t17: $(C, C)
264+
%t18 = tuple (%t18_0: $C, %t18_1: $C)
265+
(%t19_0, %t19_1) = destructure_tuple %t18: $(C, C)
266+
%t19 = tuple (%t19_0: $C, %t19_1: $C)
267+
(%t20_0, %t20_1) = destructure_tuple %t19: $(C, C)
268+
%t20 = tuple (%t20_0: $C, %t20_1: $C)
269+
(%t21_0, %t21_1) = destructure_tuple %t20: $(C, C)
270+
%t21 = tuple (%t21_0: $C, %t21_1: $C)
271+
(%t22_0, %t22_1) = destructure_tuple %t21: $(C, C)
272+
%t22 = tuple (%t22_0: $C, %t22_1: $C)
273+
(%t23_0, %t23_1) = destructure_tuple %t22: $(C, C)
274+
%t23 = tuple (%t23_0: $C, %t23_1: $C)
275+
(%t24_0, %t24_1) = destructure_tuple %t23: $(C, C)
276+
%final = tuple (%t24_0: $C, %t24_1: $C)
277+
278+
specify_test "forwarding_use_def_test %final"
279+
280+
destroy_value %final: $(C, C)
281+
%void = tuple()
282+
return %void : $()
283+
}

0 commit comments

Comments
 (0)