Skip to content

Commit 451b902

Browse files
authored
Merge pull request #40428 from eeckstein/fix-silcombine-crash
SILCombine: fix a crash when optimizing stack allocations of existentials
2 parents 9eda90f + 3cb63c0 commit 451b902

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,11 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
654654
continue;
655655
}
656656

657+
if (isa<DeinitExistentialAddrInst>(Op->getUser())) {
658+
eraseInstFromFunction(*Op->getUser());
659+
continue;
660+
}
661+
657662
if (!isa<DeallocStackInst>(Op->getUser()))
658663
continue;
659664

test/SILOptimizer/sil_combine_concrete_existential.sil

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,35 @@ bb0(%0 : $ResourceKitProtocol, %1 : $ViewController):
692692
return %19 : $()
693693
}
694694

695+
sil @calleWhichThrows : $@convention(thin) () -> (@out S, @error Error)
696+
697+
// CHECK-LABEL: sil @initExistentialWithDeinitExistential
698+
// CHECK: [[S:%[0-9]+]] = alloc_stack $S
699+
// CHECK-NOT: init_existential_addr
700+
// CHECK: try_apply %{{[0-9]+}}([[S]])
701+
// CHECK: destroy_addr [[S]] : $*S
702+
// CHECK: dealloc_stack [[S]] : $*S
703+
// CHECK: } // end sil function 'initExistentialWithDeinitExistential'
704+
sil @initExistentialWithDeinitExistential : $@convention(thin) () -> @error Error {
705+
bb0:
706+
%4 = alloc_stack $PPP
707+
%5 = function_ref @calleWhichThrows : $@convention(thin) () -> (@out S, @error Error)
708+
%6 = init_existential_addr %4 : $*PPP, $S
709+
try_apply %5(%6) : $@convention(thin) () -> (@out S, @error Error), normal bb1, error bb3
710+
711+
bb1(%8 : $()):
712+
destroy_addr %4 : $*PPP
713+
dealloc_stack %4 : $*PPP
714+
%18 = tuple ()
715+
return %18 : $()
716+
717+
718+
bb3(%20 : $Error):
719+
deinit_existential_addr %4 : $*PPP
720+
dealloc_stack %4 : $*PPP
721+
throw %20 : $Error
722+
}
723+
695724
sil_vtable SubscriptionViewControllerBuilder {}
696725
sil_vtable SubscriptionViewController {}
697726
sil_vtable ViewController {}

0 commit comments

Comments
 (0)