Skip to content

Commit 43293ea

Browse files
authored
Merge pull request #35604 from atrick/fix-simplifycfg-tramp
Fix a SimplifyCFG typo that leads to unbounded optimization
2 parents d77d7fa + 8948f75 commit 43293ea

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
13931393
// Eliminating the trampoline can expose opportunities to improve the
13941394
// new block we branch to.
13951395
if (LoopHeaders.count(DestBB))
1396-
LoopHeaders.insert(BB);
1396+
LoopHeaders.insert(trampolineDest.destBB);
13971397

13981398
addToWorklist(trampolineDest.destBB);
13991399
BI->eraseFromParent();

test/SILOptimizer/simplify_cfg_simple.sil

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ sil_stage canonical
77
import Builtin
88
import Swift
99

10+
internal enum Enum {
11+
case one
12+
case two
13+
}
14+
1015
// CHECK-LABEL: sil @simple_test : $@convention(thin) () -> () {
1116
// CHECK: bb0:
1217
// CHECK-NEXT: tuple
@@ -27,3 +32,64 @@ bb3:
2732
%9999 = tuple ()
2833
return %9999 : $()
2934
}
35+
36+
// Test that SimplifyCFG::simplifyBlocks, tryJumpThreading does not
37+
// perform unbounded loop peeling.
38+
//
39+
// rdar://73357726 ([SR-14068]: Compiling with optimisation runs indefinitely for grpc-swift)
40+
// CHECK-LABEL: sil @testInfinitePeeling : $@convention(method) (Builtin.Int64, Enum) -> () {
41+
//
42+
// There is only one switch_enum blocks, and it is no longer in a loop.
43+
// CHECK: bb0(%0 : $Builtin.Int64, %1 : $Enum):
44+
// CHECK: switch_enum %1 : $Enum, case #Enum.one!enumelt: bb3, case #Enum.two!enumelt: bb4
45+
// CHECK: bb1:
46+
// CHECK: br bb8
47+
// CHECK: bb2:
48+
// CHECK: br bb5(%{{.*}} : $Enum)
49+
//
50+
// This is the original cond_br block
51+
// CHECK: bb3:
52+
// CHECK: cond_br %{{.*}}, bb2, bb1
53+
// CHECK: bb4:
54+
// CHECK: br bb5(%1 : $Enum)
55+
//
56+
// This is the cond_br block after jump-threading.
57+
// CHECK: bb5(%{{.*}} : $Enum):
58+
// CHECK: cond_br %{{.*}}, bb6, bb7
59+
// CHECK: bb6:
60+
// CHECK: br bb5(%{{.*}} : $Enum)
61+
// CHECK: bb7:
62+
// CHECK: br bb8
63+
// CHECK: bb8:
64+
// CHECK: return %19 : $()
65+
// CHECK-LABEL: } // end sil function 'testInfinitePeeling'
66+
sil @testInfinitePeeling : $@convention(method) (Builtin.Int64, Enum) -> () {
67+
bb0(%0 : $Builtin.Int64, %1 : $Enum):
68+
%2 = integer_literal $Builtin.Int64, 99999999
69+
br bb1(%0 : $Builtin.Int64, %1 : $Enum)
70+
71+
bb1(%4 : $Builtin.Int64, %5 : $Enum):
72+
switch_enum %5 : $Enum, case #Enum.one!enumelt: bb4, default bb5
73+
74+
bb2(%7 : $Builtin.Int64, %8 : $Enum):
75+
%9 = builtin "cmp_slt_Int64"(%2 : $Builtin.Int64, %7 : $Builtin.Int64) : $Builtin.Int1
76+
cond_br %9, bb3, bb6
77+
78+
bb3:
79+
br bb1(%7 : $Builtin.Int64, %8 : $Enum)
80+
81+
bb4:
82+
%12 = integer_literal $Builtin.Int64, 1
83+
%13 = integer_literal $Builtin.Int1, -1
84+
%14 = builtin "sadd_with_overflow_Int64"(%4 : $Builtin.Int64, %12 : $Builtin.Int64, %13 : $Builtin.Int1) : $(Builtin.Int64, Builtin.Int1)
85+
%15 = tuple_extract %14 : $(Builtin.Int64, Builtin.Int1), 0
86+
%16 = enum $Enum, #Enum.two!enumelt
87+
br bb2(%15 : $Builtin.Int64, %16 : $Enum)
88+
89+
bb5:
90+
br bb2(%2 : $Builtin.Int64, %5 : $Enum)
91+
92+
bb6:
93+
%19 = tuple ()
94+
return %19 : $()
95+
}

0 commit comments

Comments
 (0)