Skip to content

Commit 073327b

Browse files
committed
Create simplify_cfg_checkcast_jumpthread.sil test.
For staging in OSSA jump-threading. Currently disabled.
1 parent 23599f0 commit 073327b

File tree

1 file changed

+382
-0
lines changed

1 file changed

+382
-0
lines changed
Lines changed: 382 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,382 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -jumpthread-simplify-cfg | %FileCheck %s
2+
3+
// TODO: replace all the XCHECK lines with CHECK lines
4+
5+
sil_stage canonical
6+
7+
import Builtin
8+
import Swift
9+
10+
class Klass {
11+
var a: Int
12+
deinit
13+
init()
14+
}
15+
16+
sil [ossa] @consume_klass : $@convention(thin) (@owned Klass) -> ()
17+
sil [ossa] @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
18+
sil [ossa] @get_klass : $@convention(thin) () -> @owned Klass
19+
20+
sil [ossa] @unknown : $@convention(thin) () -> ()
21+
22+
struct KlassWrapper {
23+
var k: Klass
24+
}
25+
26+
class Base {
27+
@inline(never) func inner()
28+
func middle()
29+
func outer()
30+
}
31+
class Derived : Base {
32+
override func inner()
33+
@inline(never) final override func middle()
34+
}
35+
36+
class Final : Derived {
37+
}
38+
39+
sil [ossa] @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
40+
sil [ossa] @_TFC3ccb4Base6middlefS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
41+
42+
// CHECK-LABEL: sil [ossa] @redundant_checked_cast_br
43+
sil [ossa] @redundant_checked_cast_br : $@convention(method) (@guaranteed Base) -> () {
44+
bb0(%0 : @guaranteed $Base):
45+
// XCHECK: [[METHOD:%.*]] = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
46+
%1 = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
47+
// XCHECK: checked_cast_br [exact] %0 : $Base to Base, [[SUCCESS:bb[0-9]+]], [[FAIL:bb[0-9]+]]
48+
checked_cast_br [exact] %0 : $Base to Base, bb2, bb7
49+
50+
// XCHECK: bb1
51+
bb1:
52+
%3 = tuple ()
53+
return %3 : $()
54+
55+
bb2(%5 : @guaranteed $Base):
56+
// XCHECK: [[SUCCESS]]
57+
%7 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
58+
// XCHECK-NOT: checked_cast_br
59+
checked_cast_br [exact] %0 : $Base to Base, bb3, bb5
60+
// XCHECK: [[INNER:%.*]] = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
61+
// XCHECK: apply [[INNER]]
62+
// XCHECK: br bb1
63+
64+
bb3(%9 : @guaranteed $Base):
65+
// XCHECK: [[FAIL]]
66+
// XCHECK-NOT: function-ref
67+
// XCHECK: apply [[METHOD]]
68+
69+
%10 = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
70+
%11 = apply %10(%0) : $@convention(method) (@guaranteed Base) -> ()
71+
br bb4
72+
73+
bb4:
74+
%13 = tuple ()
75+
br bb6(%13 : $())
76+
77+
bb5(%defaultBB2 : @guaranteed $Base):
78+
%15 = apply %7(%0) : $@convention(method) (@guaranteed Base) -> ()
79+
br bb4
80+
81+
bb6(%17 : $()):
82+
br bb1
83+
84+
bb7(%defaultBB0 : @guaranteed $Base):
85+
%19 = apply %1(%0) : $@convention(method) (@guaranteed Base) -> ()
86+
br bb1
87+
}
88+
89+
// CHECK-LABEL: sil [ossa] @redundant_checked_cast_br_owned
90+
sil [ossa] @redundant_checked_cast_br_owned : $@convention(method) (@guaranteed Base) -> () {
91+
// XCHECK: [[COPY:%.*]] = copy_value %0
92+
// XCHECK: [[METHOD:%.*]] = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
93+
// XCHECK: checked_cast_br [exact] [[COPY]] : $Base to Base, [[SUCCESS:bb[0-9]+]], [[FAIL:bb[0-9]+]]
94+
bb0(%0 : @guaranteed $Base):
95+
%copy = copy_value %0 : $Base
96+
%1 = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
97+
checked_cast_br [exact] %copy : $Base to Base, bb2, bb7
98+
99+
// XCHECK: bb1
100+
bb1:
101+
%3 = tuple ()
102+
return %3 : $()
103+
104+
// XCHECK: [[SUCCESS]]([[SUCCESSARG:%.*]] : @owned $Base)
105+
// XCHECK-NOT: checked_cast_br
106+
// XCHECK: [[INNER:%.*]] = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
107+
// XCHECK: apply [[INNER]]([[SUCCESSARG]])
108+
// XCHECK: br bb1
109+
bb2(%5 : @owned $Base):
110+
%7 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
111+
checked_cast_br [exact] %5 : $Base to Base, bb3, bb5
112+
113+
// XCHECK: [[FAIL]]([[FAILARG:%.*]] : @owned $Base)
114+
// XCHECK-NOT: function-ref
115+
// XCHECK: apply [[METHOD]]([[FAILARG]])
116+
bb3(%9 : @owned $Base):
117+
%10 = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
118+
%11 = apply %10(%9) : $@convention(method) (@guaranteed Base) -> ()
119+
destroy_value %9 : $Base
120+
br bb4
121+
122+
bb4:
123+
%13 = tuple ()
124+
br bb6(%13 : $())
125+
126+
bb5(%defaultBB2 : @owned $Base):
127+
%15 = apply %7(%defaultBB2) : $@convention(method) (@guaranteed Base) -> ()
128+
destroy_value %defaultBB2 : $Base
129+
br bb4
130+
131+
bb6(%17 : $()):
132+
br bb1
133+
134+
bb7(%defaultBB0 : @owned $Base):
135+
%19 = apply %1(%defaultBB0) : $@convention(method) (@guaranteed Base) -> ()
136+
destroy_value %defaultBB0 : $Base
137+
br bb1
138+
}
139+
140+
// CHECK-LABEL: sil [ossa] @not_redundant_checked_cast_br : $@convention(method) (@guaranteed Base) -> () {
141+
sil [ossa] @not_redundant_checked_cast_br : $@convention(method) (@guaranteed Base) -> () {
142+
bb0(%0 : @guaranteed $Base):
143+
// XCHECK: [[METHOD:%.*]] = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
144+
%1 = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
145+
// XCHECK: checked_cast_br [exact] %0 : $Base to Base, [[SUCCESS:bb[0-9]+]], [[FAIL:bb[0-9]+]]
146+
checked_cast_br [exact] %0 : $Base to Base, bb2, bb7
147+
148+
// XCHECK: bb1:
149+
// XCHECK: tuple ()
150+
// XCHECK: return
151+
152+
bb1:
153+
%3 = tuple ()
154+
return %3 : $()
155+
156+
bb2(%5 : @guaranteed $Base):
157+
// XCHECK: [[SUCCESS]]
158+
// XCHECK: [[METHOD2:%.*]] = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
159+
%7 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
160+
%8 = apply %7(%0) : $@convention(method) (@guaranteed Base) -> ()
161+
br bb4
162+
163+
bb3(%9 : @guaranteed $Derived):
164+
%10 = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
165+
%11 = apply %10(%0) : $@convention(method) (@guaranteed Base) -> ()
166+
br bb4
167+
168+
bb4:
169+
%13 = tuple ()
170+
br bb6(%13 : $())
171+
172+
bb5(%9a : @guaranteed $Base):
173+
%14 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
174+
%15 = apply %14(%0) : $@convention(method) (@guaranteed Base) -> ()
175+
br bb4
176+
177+
bb6(%17 : $()):
178+
br bb1
179+
180+
bb7(%10a : @guaranteed $Base):
181+
// XCHECK: checked_cast_br [exact] %0 : $Base to Derived
182+
checked_cast_br [exact] %0 : $Base to Derived, bb3, bb5
183+
}
184+
185+
// CHECK-LABEL: sil [ossa] @failing_checked_cast_br
186+
sil [ossa] @failing_checked_cast_br : $@convention(method) (@guaranteed Base) -> () {
187+
bb0(%0 : @guaranteed $Base):
188+
// XCHECK: [[METHOD:%.*]] = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
189+
%1 = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
190+
// XCHECK: checked_cast_br [exact] %0 : $Base to Base, [[SUCCESS:bb[0-9]+]], [[FAIL:bb[0-9]+]]
191+
checked_cast_br [exact] %0 : $Base to Base, bb2, bb7
192+
193+
// CHECK-LABEL: bb1
194+
bb1:
195+
%3 = tuple ()
196+
return %3 : $()
197+
198+
bb2(%5 : @guaranteed $Base):
199+
// XCHECK: [[SUCCESS]]([[SUCCESSARG:%.*]] : @guaranteed $Base)
200+
// XCHECK: [[METHOD2:%.*]] = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
201+
%7 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
202+
// XCHECK-NOT: checked_cast_br [exact] %0 : $Base to Derived
203+
// XCHECK: apply [[METHOD2]]([[SUCCESSARG]])
204+
// Check that checked_cast_br [exact] was replaced by a branch to the failure BB of the checked_cast_br.
205+
// This is because bb2 is reached via the success branch of the checked_cast_br [exact] from bb0.
206+
// It means that the exact dynamic type of %0 is $Base. Thus it cannot be $Derived.
207+
// XCHECK: br bb1
208+
checked_cast_br [exact] %5 : $Base to Derived, bb3, bb5
209+
210+
bb3(%9 : @guaranteed $Derived):
211+
%10 = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
212+
%11 = apply %10(%0) : $@convention(method) (@guaranteed Base) -> ()
213+
br bb4
214+
215+
bb4:
216+
%13 = tuple ()
217+
br bb6(%13 : $())
218+
219+
bb5(%9o : @guaranteed $Base):
220+
%15 = apply %7(%9o) : $@convention(method) (@guaranteed Base) -> ()
221+
br bb4
222+
223+
bb6(%17 : $()):
224+
br bb1
225+
226+
bb7(%anotherDefaultPayload : @guaranteed $Base):
227+
%19 = apply %1(%0) : $@convention(method) (@guaranteed Base) -> ()
228+
br bb1
229+
}
230+
231+
// CHECK-LABEL: sil [ossa] @failing_checked_cast_br_owned
232+
sil [ossa] @failing_checked_cast_br_owned : $@convention(method) (@guaranteed Base) -> () {
233+
// XCHECK: [[COPY:%.*]] = copy_value %0
234+
// XCHECK: [[METHOD:%.*]] = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
235+
// XCHECK: checked_cast_br [exact] [[COPY]] : $Base to Base, [[SUCCESS:bb[0-9]+]], [[FAIL:bb[0-9]+]]
236+
bb0(%0 : @guaranteed $Base):
237+
%copy = copy_value %0 : $Base
238+
%1 = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
239+
checked_cast_br [exact] %copy : $Base to Base, bb2, bb7
240+
241+
// CHECK-LABEL: bb1
242+
bb1:
243+
%3 = tuple ()
244+
return %3 : $()
245+
246+
// XCHECK: [[SUCCESS]]([[SUCCESSARG:%.*]] : @owned $Base)
247+
// XCHECK: [[METHOD2:%.*]] = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
248+
// XCHECK-NOT: checked_cast_br [exact] %0 : $Base to Derived
249+
// XCHECK: apply [[METHOD2]]([[SUCCESSARG]])
250+
// Check that checked_cast_br [exact] was replaced by a branch to the failure BB of the checked_cast_br.
251+
// This is because bb2 is reached via the success branch of the checked_cast_br [exact] from bb0.
252+
// It means that the exact dynamic type of %0 is $Base. Thus it cannot be $Derived.
253+
// XCHECK: br bb1
254+
bb2(%5 : @owned $Base):
255+
%7 = class_method %0 : $Base, #Base.inner : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
256+
checked_cast_br [exact] %5 : $Base to Derived, bb3, bb5
257+
258+
bb3(%9 : @owned $Derived):
259+
%upcast = upcast %9 : $Derived to $Base
260+
%10 = function_ref @_TFC3ccb4Base5innerfS0_FT_T_ : $@convention(method) (@guaranteed Base) -> ()
261+
%11 = apply %10(%upcast) : $@convention(method) (@guaranteed Base) -> ()
262+
destroy_value %upcast : $Base
263+
br bb4
264+
265+
bb4:
266+
%13 = tuple ()
267+
br bb6(%13 : $())
268+
269+
bb5(%9o : @owned $Base):
270+
%15 = apply %7(%9o) : $@convention(method) (@guaranteed Base) -> ()
271+
destroy_value %9o : $Base
272+
br bb4
273+
274+
bb6(%17 : $()):
275+
br bb1
276+
277+
bb7(%anotherDefaultPayload : @owned $Base):
278+
%19 = apply %1(%0) : $@convention(method) (@guaranteed Base) -> ()
279+
destroy_value %anotherDefaultPayload : $Base
280+
br bb1
281+
}
282+
283+
sil [ossa] @unknown2 : $@convention(thin) () -> ()
284+
285+
// CHECK-LABEL: no_checked_cast_br_threading_with_alloc_ref_stack
286+
// XCHECK: checked_cast_br
287+
// XCHECK: apply
288+
// XCHECK: apply
289+
// XCHECK: checked_cast_br
290+
// XCHECK: apply
291+
// XCHECK: apply
292+
// XCHECK: return
293+
sil [ossa] @no_checked_cast_br_threading_with_alloc_ref_stack : $@convention(method) (@guaranteed Base) -> () {
294+
bb0(%0 : @guaranteed $Base):
295+
%fu = function_ref @unknown : $@convention(thin) () -> ()
296+
%fu2 = function_ref @unknown2 : $@convention(thin) () -> ()
297+
checked_cast_br [exact] %0 : $Base to Base, bb1, bb2
298+
299+
bb1(%1 : @guaranteed $Base):
300+
apply %fu() : $@convention(thin) () -> ()
301+
br bb3
302+
303+
bb2(%1a : @guaranteed $Base):
304+
apply %fu2() : $@convention(thin) () -> ()
305+
br bb3
306+
307+
bb3:
308+
%a = alloc_ref [stack] $Base
309+
checked_cast_br [exact] %0 : $Base to Base, bb4, bb5
310+
311+
bb4(%2 : @guaranteed $Base):
312+
apply %fu() : $@convention(thin) () -> ()
313+
br bb6
314+
315+
bb5(%2a : @guaranteed $Base):
316+
apply %fu2() : $@convention(thin) () -> ()
317+
br bb6
318+
319+
bb6:
320+
dealloc_ref [stack] %a : $Base
321+
%r = tuple()
322+
return %r : $()
323+
}
324+
325+
// Test a redundant checked_cast_br that has success, failure paths, and unknown paths.
326+
//
327+
// TODO: this is currently a bailout.
328+
//
329+
//!!! CHECKME
330+
sil [ossa] @redundant_checked_cast_br_joined_success_fail_unknown : $@convention(method) (@guaranteed Base) -> () {
331+
bb0(%0 : @guaranteed $Base):
332+
%middle = class_method %0 : $Base, #Base.middle : (Base) -> () -> (), $@convention(method) (@guaranteed Base) -> ()
333+
checked_cast_br [exact] %0 : $Base to Base, bb1, bb4
334+
335+
bb1(%successBB0 : @guaranteed $Base):
336+
cond_br undef, bb2, bb3
337+
338+
bb2:
339+
%successBB0call2 = apply %middle(%successBB0) : $@convention(method) (@guaranteed Base) -> ()
340+
%successBB0borrow2 = begin_borrow %successBB0 : $Base
341+
br bb8(%successBB0borrow2 : $Base)
342+
343+
bb3:
344+
%successBB0call3 = apply %middle(%successBB0) : $@convention(method) (@guaranteed Base) -> ()
345+
%successBB0borrow3 = begin_borrow %successBB0 : $Base
346+
br bb7(%successBB0borrow3 : $Base)
347+
348+
bb4(%failBB0 : @guaranteed $Base):
349+
cond_br undef, bb5, bb6
350+
351+
bb5:
352+
%failBB0call5 = apply %middle(%failBB0) : $@convention(method) (@guaranteed Base) -> ()
353+
%failBB0borrow5 = begin_borrow %failBB0 : $Base
354+
br bb7(%failBB0borrow5 : $Base)
355+
356+
bb6:
357+
%failBB0call6 = apply %middle(%failBB0) : $@convention(method) (@guaranteed Base) -> ()
358+
%failBB0borrow6 = begin_borrow %failBB0 : $Base
359+
br bb8(%failBB0borrow6 : $Base)
360+
361+
bb7(%unknown : @guaranteed $Base):
362+
%unknownCall = apply %middle(%unknown) : $@convention(method) (@guaranteed Base) -> ()
363+
br bb8(%unknown : $Base)
364+
365+
bb8(%joined : @guaranteed $Base):
366+
%joinedCall = apply %middle(%joined) : $@convention(method) (@guaranteed Base) -> ()
367+
checked_cast_br [exact] %joined : $Base to Base, bb9, bb10
368+
369+
bb9(%successBB7 : @guaranteed $Base):
370+
%successBB7call8 = apply %middle(%successBB7) : $@convention(method) (@guaranteed Base) -> ()
371+
br bb11
372+
373+
bb10(%failBB7 : @guaranteed $Base):
374+
%failBB7call8 = apply %middle(%failBB7) : $@convention(method) (@guaranteed Base) -> ()
375+
br bb11
376+
377+
bb11:
378+
end_borrow %joined : $Base
379+
%20 = tuple ()
380+
return %20 : $()
381+
382+
}

0 commit comments

Comments
 (0)