Skip to content

Commit dac6c03

Browse files
committed
Add tests for simple jump threading
1 parent 5d2967c commit dac6c03

File tree

8 files changed

+180
-183
lines changed

8 files changed

+180
-183
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,11 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
924924
if (destTerminator->isFunctionExiting())
925925
return false;
926926

927+
// There is no benefit duplicating such a destination.
928+
if (DestBB->getSinglePredecessorBlock() != nullptr) {
929+
return false;
930+
}
931+
927932
// Jump threading only makes sense if there is an argument on the branch
928933
// (which is reacted on in the DestBB), or if this goes through a memory
929934
// location (switch_enum_addr is the only address-instruction which we
@@ -942,6 +947,7 @@ bool SimplifyCFG::tryJumpThreading(BranchInst *BI) {
942947
for (unsigned i : indices(BI->getArgs())) {
943948
SILValue Arg = BI->getArg(i);
944949

950+
// TODO: Verify if we need to jump thread to remove releases in OSSA.
945951
// If the value being substituted on is release there is a chance we could
946952
// remove the release after jump threading.
947953
if (!Arg->getType().isTrivial(*SrcBB->getParent()) &&

lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ struct SimplifyCFGSimplifyTermWithIdenticalDestBlocks : UnitTest {
360360
}
361361
};
362362

363+
struct SimplifyCFGTryJumpThreading : UnitTest {
364+
SimplifyCFGTryJumpThreading(UnitTestRunner *pass) : UnitTest(pass) {}
365+
void invoke(Arguments &arguments) override {
366+
auto *passToRun = cast<SILFunctionTransform>(createSimplifyCFG());
367+
passToRun->injectPassManager(getPass()->getPassManager());
368+
passToRun->injectFunction(getFunction());
369+
SimplifyCFG(*getFunction(), *passToRun, /*VerifyAll=*/false,
370+
/*EnableJumpThread=*/false)
371+
.tryJumpThreading(cast<BranchInst>(arguments.takeInstruction()));
372+
}
373+
};
374+
363375
// Arguments:
364376
// - string: list of characters, each of which specifies subsequent arguments
365377
// - A: (block) argument
@@ -533,6 +545,8 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
533545
ADD_UNIT_TEST_SUBCLASS(
534546
"simplify-cfg-simplify-term-with-identical-dest-blocks",
535547
SimplifyCFGSimplifyTermWithIdenticalDestBlocks)
548+
ADD_UNIT_TEST_SUBCLASS("simplify-cfg-try-jump-threading",
549+
SimplifyCFGTryJumpThreading)
536550

537551
ADD_UNIT_TEST_SUBCLASS("test-specification-parsing", TestSpecificationTest)
538552
ADD_UNIT_TEST_SUBCLASS("visit-adjacent-reborrows-of-phi", VisitAdjacentReborrowsOfPhiTest)

test/SILOptimizer/simplify_cfg.sil

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,6 @@ bb5(%r : $Builtin.Int32):
10491049
// CHECK: bb1(
10501050
// CHECK: cond_br {{.*}}, bb3, bb2
10511051
// CHECK: bb2:
1052-
// CHECK: enum $Optional<Int32>, #Optional.some
10531052
// CHECK: br bb1(
10541053
// CHECK: bb3:
10551054
// CHECK: return

test/SILOptimizer/simplify_cfg_args.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,17 +751,14 @@ enum ThreeWay {
751751
// CHECK: switch_enum undef : $ThreeWay, case #ThreeWay.one!enumelt: bb4, case #ThreeWay.two!enumelt: bb2, case #ThreeWay.three!enumelt: bb3
752752
// CHECK: bb2:
753753
// CHECK: [[P2:%.*]] = load %{{.*}} : $*Payload
754-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P2]] : $Payload
755754
// CHECK: dealloc_stack %{{.*}} : $*Payload
756755
// CHECK: br bb1(undef : $Builtin.Int64, [[P2]] : $Payload)
757756
// CHECK: bb3:
758757
// CHECK: [[P3:%.*]] = load %{{.*}} : $*Payload
759-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P3]] : $Payload
760758
// CHECK: dealloc_stack %{{.*}} : $*Payload
761759
// CHECK: br bb7
762760
// CHECK: bb4:
763761
// CHECK: [[P4:%.*]] = load %{{.*}} : $*Payload
764-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P4]] : $Payload
765762
// CHECK: dealloc_stack %{{.*}} : $*Payload
766763
// CHECK: cond_br undef, bb5, bb6
767764
// CHECK: bb5:

test/SILOptimizer/simplify_cfg_args_ossa.sil

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,17 +730,14 @@ enum ThreeWay {
730730
// CHECK: switch_enum undef : $ThreeWay, case #ThreeWay.one!enumelt: bb4, case #ThreeWay.two!enumelt: bb2, case #ThreeWay.three!enumelt: bb3
731731
// CHECK: bb2:
732732
// CHECK: [[P2:%.*]] = load [trivial] %{{.*}} : $*Payload
733-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P2]] : $Payload
734733
// CHECK: dealloc_stack %{{.*}} : $*Payload
735734
// CHECK: br bb1(undef : $Builtin.Int64, [[P2]] : $Payload)
736735
// CHECK: bb3:
737736
// CHECK: [[P3:%.*]] = load [trivial] %{{.*}} : $*Payload
738-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P3]] : $Payload
739737
// CHECK: dealloc_stack %{{.*}} : $*Payload
740738
// CHECK: br bb7
741739
// CHECK: bb4:
742740
// CHECK: [[P4:%.*]] = load [trivial] %{{.*}} : $*Payload
743-
// CHECK: enum $MyEnum, #MyEnum.payload!enumelt, [[P4]] : $Payload
744741
// CHECK: dealloc_stack %{{.*}} : $*Payload
745742
// CHECK: cond_br undef, bb6, bb5
746743
// CHECK: bb5:

test/SILOptimizer/simplify_cfg_opaque.sil

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// RUN: %target-sil-opt -unit-test-runner -sil-infinite-jump-threading-budget %s 2>&1
2+
3+
import Builtin
4+
import Swift
5+
6+
class Klass {
7+
var val: Int
8+
}
9+
10+
class AnyKlass { }
11+
12+
enum FakeOptional<T> {
13+
case some(T)
14+
case none
15+
}
16+
17+
sil @get_klass : $@convention(thin) () -> @owned Klass
18+
19+
sil [ossa] @test_simplify_switch_enum_jump_threading1 : $@convention(thin) (@owned Klass) -> () {
20+
bb0(%0 : @owned $Klass):
21+
test_specification "simplify-cfg-try-jump-threading @instruction[1]"
22+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
23+
br bb1(%1 : $FakeOptional<Klass>)
24+
25+
bb1(%3 : @owned $FakeOptional<Klass>):
26+
switch_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb3, case #FakeOptional.none!enumelt: bb2
27+
28+
bb2:
29+
br bb4
30+
31+
bb3(%6 : @owned $Klass):
32+
destroy_value %6 : $Klass
33+
br bb4
34+
35+
bb4:
36+
%t = tuple ()
37+
return %t : $()
38+
}
39+
40+
41+
sil [ossa] @test_simplify_switch_enum_jump_threading2 : $@convention(thin) (@owned Klass) -> () {
42+
bb0(%0 : @owned $Klass):
43+
cond_br undef, bb1, bb2
44+
45+
bb1:
46+
test_specification "simplify-cfg-try-jump-threading @instruction[2]"
47+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
48+
br bb3(%1 : $FakeOptional<Klass>)
49+
50+
bb2:
51+
test_specification "simplify-cfg-try-jump-threading @instruction[5]"
52+
destroy_value %0 : $Klass
53+
%2 = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
54+
br bb3(%2 : $FakeOptional<Klass>)
55+
56+
bb3(%3 : @owned $FakeOptional<Klass>):
57+
switch_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb5, case #FakeOptional.none!enumelt: bb4
58+
59+
bb4:
60+
br bb6
61+
62+
bb5(%6 : @owned $Klass):
63+
destroy_value %6 : $Klass
64+
br bb6
65+
66+
bb6:
67+
%t = tuple ()
68+
return %t : $()
69+
}
70+
71+
sil [ossa] @test_simplify_switch_enum_jump_threading3 : $@convention(thin) (@owned Klass) -> Builtin.Int1 {
72+
bb0(%0 : @owned $Klass):
73+
%t = integer_literal $Builtin.Int1, 1
74+
%f = integer_literal $Builtin.Int1, 0
75+
cond_br undef, bb1, bb2
76+
77+
bb1:
78+
test_specification "simplify-cfg-try-jump-threading @instruction[4]"
79+
%1 = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
80+
br bb3(%1 : $FakeOptional<Klass>)
81+
82+
bb2:
83+
destroy_value %0 : $Klass
84+
%2 = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
85+
br bb3(%2 : $FakeOptional<Klass>)
86+
87+
bb3(%3 : @owned $FakeOptional<Klass>):
88+
%4 = select_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: %t, case #FakeOptional.none!enumelt: %f : $Builtin.Int1
89+
destroy_value %3 : $FakeOptional<Klass>
90+
br bb4
91+
92+
bb4:
93+
return %4 : $Builtin.Int1
94+
}
95+
96+
sil [ossa] @test_jump_thread_ref_ele_loop : $@convention(thin) () -> () {
97+
bb0:
98+
test_specification "simplify-cfg-try-jump-threading @instruction[3]"
99+
%f = function_ref @get_klass : $@convention(thin) () -> @owned Klass
100+
cond_br undef, bb1, bb2
101+
102+
bb1:
103+
%c1 = apply %f() : $@convention(thin) () -> @owned Klass
104+
br bb3(%c1 : $Klass)
105+
106+
bb2:
107+
%c2 = apply %f() : $@convention(thin) () -> @owned Klass
108+
br bb3(%c2 : $Klass)
109+
110+
bb3(%arg : @owned $Klass):
111+
%b = begin_borrow %arg : $Klass
112+
%ele = ref_element_addr %b : $Klass, #Klass.val
113+
br bb4
114+
115+
bb4:
116+
%addr = begin_access [read] [dynamic] %ele : $*Int
117+
%ld = load [trivial] %addr : $*Int
118+
end_access %addr : $*Int
119+
cond_br undef, bb4a, bb5
120+
121+
bb4a:
122+
br bb4
123+
124+
bb5:
125+
end_borrow %b : $Klass
126+
destroy_value %arg : $Klass
127+
%z = tuple ()
128+
return %z : $()
129+
}
130+
131+
sil [ossa] @test_jump_thread_checked_cast_value : $@convention(thin) (@owned AnyKlass, @owned AnyKlass) -> () {
132+
bb0(%0 : @owned $AnyKlass, %1 : @owned $AnyKlass):
133+
cond_br undef, bb1, bb2
134+
135+
bb1:
136+
test_specification "simplify-cfg-try-jump-threading @instruction[2]"
137+
%2 = copy_value %0 : $AnyKlass
138+
br bb6(%2 : $AnyKlass)
139+
140+
bb2:
141+
%3 = copy_value %1 : $AnyKlass
142+
br bb6(%3 : $AnyKlass)
143+
144+
bb6(%4 : @owned $AnyKlass):
145+
destroy_value %0 : $AnyKlass
146+
destroy_value %1 : $AnyKlass
147+
checked_cast_br %4 : $AnyKlass to Klass, bb7, bb8
148+
149+
bb7(%k : @owned $Klass):
150+
destroy_value %k : $Klass
151+
br bb9
152+
153+
bb8(%fail : @owned $AnyKlass):
154+
destroy_value %fail : $AnyKlass
155+
br bb9
156+
157+
bb9:
158+
%999 = tuple ()
159+
return %999 : $()
160+
}

0 commit comments

Comments
 (0)