Skip to content

Commit d0f627c

Browse files
committed
Merge pull request #2510 from eeckstein/fix-simplify-cfg
SimplifyCFG: fix crash in case an enum payload is an enum
2 parents 68f4cf9 + 4c17897 commit d0f627c

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,14 @@ static NullablePtr<EnumElementDecl> getEnumCase(SILValue Val,
789789

790790
EnumElementDecl *CommonCase = nullptr;
791791
for (std::pair<SILBasicBlock *, SILValue> Incoming : IncomingVals) {
792+
TermInst *TI = Incoming.first->getTerminator();
793+
794+
// If the terminator of the incoming value is e.g. a switch_enum, the
795+
// incoming value is the switch_enum operand and not the enum payload
796+
// (which would be the real incoming value of the argument).
797+
if (!isa<BranchInst>(TI) && !isa<CondBranchInst>(TI))
798+
return nullptr;
799+
792800
NullablePtr<EnumElementDecl> IncomingCase =
793801
getEnumCase(Incoming.second, Incoming.first, RecursionDepth + 1);
794802
if (!IncomingCase)

test/SILOptimizer/simplify_cfg.sil

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,3 +2629,35 @@ bb3(%4 : $Optional<Int>):
26292629
}
26302630

26312631

2632+
// CHECK-LABEL: sil @dont_crash_on_enum_payload_is_enum
2633+
// CHECK: bb0(%0 : $TwoCase):
2634+
// CHECK: switch_enum %0
2635+
// CHECK: bb1:
2636+
// CHECK: return
2637+
sil @dont_crash_on_enum_payload_is_enum : $@convention(thin) (TwoCase) -> () {
2638+
bb0(%0 : $TwoCase):
2639+
%x = enum $Optional<TwoCase>, #Optional.some!enumelt.1, %0 : $TwoCase
2640+
br bb1(%x : $Optional<TwoCase>)
2641+
2642+
bb1(%10 : $Optional<TwoCase>):
2643+
switch_enum %10 : $Optional<TwoCase>, case #Optional.some!enumelt.1: bb2, case #Optional.none!enumelt: bb5
2644+
2645+
bb2(%1 : $TwoCase):
2646+
switch_enum %1 : $TwoCase, case #TwoCase.First!enumelt: bb3, case #TwoCase.Second!enumelt: bb4
2647+
2648+
bb3:
2649+
%f1 = function_ref @unknown : $@convention(thin) () -> ()
2650+
%n1 = apply %f1() : $@convention(thin) () -> ()
2651+
br bb6
2652+
2653+
bb4:
2654+
br bb6
2655+
2656+
bb5:
2657+
br bb6
2658+
2659+
bb6:
2660+
%6 = tuple ()
2661+
return %6 : $()
2662+
}
2663+

0 commit comments

Comments
 (0)