Skip to content

Commit 412fffc

Browse files
authored
Merge pull request #84905 from eeckstein/fix-simplify-cfg
SimplifyCFG: insert compensating destroy when replacing a `switch_enum`
2 parents ccea096 + eb71200 commit 412fffc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,11 @@ bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
17931793
}
17941794

17951795
if (Dest->args_empty()) {
1796-
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), Dest);
1796+
SILBuilderWithScope builder(SEI);
1797+
if (SEI->getOperand()->getOwnershipKind() == OwnershipKind::Owned) {
1798+
builder.createDestroyValue(SEI->getLoc(), SEI->getOperand());
1799+
}
1800+
builder.createBranch(SEI->getLoc(), Dest);
17971801

17981802
addToWorklist(SEI->getParent());
17991803
addToWorklist(Dest);

test/SILOptimizer/simplify_cfg_ossa.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,24 @@ bb3:
19441944
return %t : $()
19451945
}
19461946

1947+
// CHECK-LABEL: sil [ossa] @insert_compensating_destroy_in_switch_enum_destination_block :
1948+
// CHECK: bb0(%0 : @owned $Optional<AnyObject>):
1949+
// CHECK-NEXT: destroy_value %0
1950+
// CHECK-NEXT: tuple
1951+
// CHECK: } // end sil function 'insert_compensating_destroy_in_switch_enum_destination_block'
1952+
sil [ossa] @insert_compensating_destroy_in_switch_enum_destination_block : $@convention(thin) (@owned Optional<AnyObject>) -> () {
1953+
bb0(%0 : @owned $Optional<AnyObject>):
1954+
switch_enum %0, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2
1955+
1956+
bb1:
1957+
%15 = tuple ()
1958+
return %15
1959+
1960+
bb2(%4 : @owned $AnyObject):
1961+
destroy_value %4
1962+
unreachable
1963+
}
1964+
19471965
// CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use :
19481966
// CHECK: bb3([[R:%.*]] : @reborrow $B):
19491967
// CHECK: bb6([[G:%.*]] : @guaranteed $E):

0 commit comments

Comments
 (0)