Skip to content

Commit 5142b41

Browse files
authored
Merge pull request #19504 from eeckstein/fix-simplifycfg-crash
SIL optimizer: consider non-branch terminators with a single successor for critical edges.
2 parents 8ac9a65 + a015d91 commit 5142b41

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/SILOptimizer/Utils/CFG.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,11 @@ bool swift::isCriticalEdge(TermInst *T, unsigned EdgeIdx) {
405405
assert(T->getSuccessors().size() > EdgeIdx && "Not enough successors");
406406

407407
auto SrcSuccs = T->getSuccessors();
408-
if (SrcSuccs.size() <= 1)
408+
409+
if (SrcSuccs.size() <= 1 &&
410+
// Also consider non-branch instructions with a single successor for
411+
// critical edges, for example: a switch_enum of a single-case enum.
412+
(isa<BranchInst>(T) || isa<CondBranchInst>(T)))
409413
return false;
410414

411415
SILBasicBlock *DestBB = SrcSuccs[EdgeIdx];

test/SILOptimizer/simplify_cfg.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,32 @@ exit:
16841684
return %9999 : $()
16851685
}
16861686

1687+
// CHECK-LABEL: sil @dont_crash_jump_threading_single_case_switch_enums
1688+
// CHECK-NOT: switch_enum
1689+
// CHECK: } // end sil function
1690+
sil @dont_crash_jump_threading_single_case_switch_enums : $@convention(thin) () -> () {
1691+
bb0:
1692+
br bb1(undef : $OneCase)
1693+
1694+
bb1(%19 : $OneCase):
1695+
switch_enum %19 : $OneCase, case #OneCase.First!enumelt.1: bb2
1696+
1697+
bb2:
1698+
switch_enum %19 : $OneCase, case #OneCase.First!enumelt.1: bb3
1699+
1700+
bb3:
1701+
%48 = enum $OneCase, #OneCase.First!enumelt.1
1702+
cond_br undef, bb6, bb7
1703+
1704+
bb6:
1705+
%72 = tuple ()
1706+
return %72 : $()
1707+
1708+
bb7:
1709+
br bb1(%48 : $OneCase)
1710+
}
1711+
1712+
16871713
enum IntEnum : Int32 {
16881714
case E0
16891715
case E1

0 commit comments

Comments
 (0)