Skip to content

Commit 67c31bc

Browse files
authored
Merge pull request #11654 from eeckstein/fix-simplify-cfg-4.0
[4.0] SimplifyCFG: fix hang caused by infinite loop in CFG
2 parents 39a6a01 + e0857df commit 67c31bc

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,9 +1090,19 @@ static SILBasicBlock *getTrampolineDest(SILBasicBlock *SBB) {
10901090
if (!BI)
10911091
return nullptr;
10921092

1093-
// Disallow infinite loops.
1094-
if (BI->getDestBB() == SBB)
1095-
return nullptr;
1093+
// Disallow infinite loops through SBB.
1094+
llvm::SmallPtrSet<SILBasicBlock *, 8> VisitedBBs;
1095+
BranchInst *NextBI = BI;
1096+
do {
1097+
SILBasicBlock *NextBB = NextBI->getDestBB();
1098+
// We don't care about infinite loops after SBB.
1099+
if (!VisitedBBs.insert(NextBB).second)
1100+
break;
1101+
// Only if the infinite loop goes through SBB directly we bail.
1102+
if (NextBB == SBB)
1103+
return nullptr;
1104+
NextBI = dyn_cast<BranchInst>(NextBB->getTerminator());
1105+
} while (NextBI);
10961106

10971107
auto BrArgs = BI->getArgs();
10981108
if (BrArgs.size() != SBB->getNumArguments())

test/SILOptimizer/simplify_cfg.sil

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,3 +2859,38 @@ bb5(%e : $MyError):
28592859
bb6(%44 : $Builtin.Int8):
28602860
return %44 : $Builtin.Int8
28612861
}
2862+
2863+
// CHECK-LABEL: sil @dont_hang
2864+
// CHECK: bb6:
2865+
// CHECK: integer_literal $Builtin.Int64, 1
2866+
// CHECK-NEXT: br bb5
2867+
// CHECK-NEXT: }
2868+
sil @dont_hang : $@convention(thin) () -> () {
2869+
bb0:
2870+
cond_br undef, bb1, bb4
2871+
2872+
bb1:
2873+
%0 = integer_literal $Builtin.Int64, 1
2874+
cond_br undef, bb2, bb6
2875+
2876+
bb2:
2877+
br bb3
2878+
2879+
bb3:
2880+
br bb5
2881+
2882+
bb4:
2883+
%1 = integer_literal $Builtin.Int64, 1
2884+
br bb3
2885+
2886+
bb5:
2887+
br bb2
2888+
2889+
bb6:
2890+
%2 = integer_literal $Builtin.Int64, 1
2891+
br bb7
2892+
2893+
bb7:
2894+
br bb5
2895+
}
2896+

0 commit comments

Comments
 (0)