Skip to content

Commit 3e750f9

Browse files
committed
SimplifyCFG: Fix a missing borrowed-from when doing jump threading
Fixes a compiler crash rdar://129805179
1 parent 7cfbe97 commit 3e750f9

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
205205
BasicBlockCloner(SILBasicBlock *origBB, SILPassManager *pm, DeadEndBlocks *deBlocks = nullptr)
206206
: SILCloner(*origBB->getParent()), origBB(origBB), deBlocks(deBlocks), pm(pm) {}
207207

208+
void registerBlockWithNewPhiArg(SILBasicBlock *b) {
209+
blocksWithNewPhiArgs.push_back(b);
210+
}
211+
208212
bool canCloneBlock() {
209213
for (auto &inst : *origBB) {
210214
if (!canCloneInstruction(&inst))

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
232232
(*ThreadedSuccessorBlock->args_begin())->getType() &&
233233
"Argument types must match");
234234
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, {UED});
235+
Cloner.registerBlockWithNewPhiArg(ThreadedSuccessorBlock);
235236
} else {
236237
assert(SEI->getDefaultBB() == ThreadedSuccessorBlock);
237238
auto *OldBlockArg = ThreadedSuccessorBlock->getArgument(0);

test/SILOptimizer/simplify_cfg_ossa_dom_jumpthread.sil

+46
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ struct FakeBool {
2727

2828
class Klass {}
2929

30+
struct F {
31+
let a: Int?
32+
let e: String
33+
}
34+
3035
sil @external_f : $@convention(thin) () -> ()
3136
// func testThread3(a : Int32) -> Int32 {
3237
// (enum b, val) = (a ? (_true, 16) : (_false, 17))
@@ -886,3 +891,44 @@ bb10:
886891
return %21 : $()
887892
}
888893

894+
// CHECK-LABEL: sil [ossa] @jump_thread_new_phi_arg
895+
// CHECK: switch_enum %0 : $Optional<F>
896+
// CHECK: switch_enum {{%[0-9]+}} : $Optional<Int>
897+
// CHECK-NOT: switch_enum
898+
// CHECK: } // end sil function 'jump_thread_new_phi_arg'
899+
sil [ossa] @jump_thread_new_phi_arg : $@convention(thin) (@guaranteed Optional<F>) -> Optional<Int> {
900+
bb0(%0 : @guaranteed $Optional<F>):
901+
switch_enum %0 : $Optional<F>, case #Optional.some!enumelt: bb2, case #Optional.none!enumelt: bb1
902+
903+
bb1:
904+
%4 = enum $Optional<Int>, #Optional.none!enumelt
905+
br bb3(%4 : $Optional<Int>)
906+
907+
908+
bb2(%6 : @guaranteed $F):
909+
%7 = struct_extract %6 : $F, #F.a
910+
br bb3(%7 : $Optional<Int>)
911+
912+
913+
bb3(%9 : $Optional<Int>):
914+
switch_enum %9 : $Optional<Int>, case #Optional.some!enumelt: bb4, case #Optional.none!enumelt: bb5
915+
916+
bb4(%11 : $Int):
917+
br bb8(%9 : $Optional<Int>)
918+
919+
bb5:
920+
switch_enum %0 : $Optional<F>, case #Optional.some!enumelt: bb6, case #Optional.none!enumelt: bb7
921+
922+
923+
bb6(%14 : @guaranteed $F):
924+
%15 = struct_extract %14 : $F, #F.a
925+
br bb8(%15 : $Optional<Int>)
926+
927+
bb7:
928+
%17 = enum $Optional<Int>, #Optional.none!enumelt
929+
br bb8(%17 : $Optional<Int>)
930+
931+
932+
bb8(%19 : $Optional<Int>):
933+
return %19 : $Optional<Int>
934+
}

0 commit comments

Comments
 (0)