Merge pull request #84946 from eeckstein/fix-simplify-cfg-6.2

[6.2] SimplifyCFG: insert compensating destroy when replacing a `switch_enum`
This commit is contained in:
eeckstein
2025-10-23 20:47:01 +02:00
committed by GitHub
2 changed files with 53 additions and 2 deletions

View File

@@ -1791,7 +1791,12 @@ bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
} }
if (Dest->args_empty()) { if (Dest->args_empty()) {
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), Dest); SILBuilderWithScope builder(SEI);
if (SEI->getOperand()->getOwnershipKind() == OwnershipKind::Owned) {
// Note that a `destroy_value` would be wrong for non-copyable enums with deinits.
builder.createEndLifetime(SEI->getLoc(), SEI->getOperand());
}
builder.createBranch(SEI->getLoc(), Dest);
addToWorklist(SEI->getParent()); addToWorklist(SEI->getParent());
addToWorklist(Dest); addToWorklist(Dest);

View File

@@ -1,4 +1,6 @@
// RUN: %target-sil-opt -sil-print-types -enable-objc-interop -enable-sil-verify-all %s -simplify-cfg | %FileCheck %s // RUN: %target-sil-opt -sil-print-types -enable-objc-interop -enable-experimental-feature MoveOnlyEnumDeinits -enable-sil-verify-all %s -simplify-cfg | %FileCheck %s
// REQUIRES: swift_feature_MoveOnlyEnumDeinits
// OSSA form of tests from simplify_cfg.sil and simplify_cfg_simple.sil. // OSSA form of tests from simplify_cfg.sil and simplify_cfg_simple.sil.
// //
@@ -64,6 +66,12 @@ struct S {
var b: NonTrivial var b: NonTrivial
} }
enum NCE: ~Copyable {
case a
case b(AnyObject)
deinit
}
/////////// ///////////
// Tests // // Tests //
/////////// ///////////
@@ -1944,6 +1952,44 @@ bb3:
return %t : $() return %t : $()
} }
// CHECK-LABEL: sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block :
// CHECK: bb0(%0 : @owned $Optional<AnyObject>):
// CHECK-NEXT: end_lifetime %0
// CHECK-NEXT: tuple
// CHECK: } // end sil function 'insert_compensating_endlifetime_in_switch_enum_destination_block'
sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block : $@convention(thin) (@owned Optional<AnyObject>) -> () {
bb0(%0 : @owned $Optional<AnyObject>):
switch_enum %0, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2
bb1:
%15 = tuple ()
return %15
bb2(%4 : @owned $AnyObject):
destroy_value %4
unreachable
}
// CHECK-LABEL: sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block2 :
// CHECK: bb0(%0 : @owned $NCE):
// CHECK-NEXT: %1 = drop_deinit %0
// CHECK-NEXT: end_lifetime %1
// CHECK-NEXT: tuple
// CHECK: } // end sil function 'insert_compensating_endlifetime_in_switch_enum_destination_block2'
sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block2 : $@convention(thin) (@owned NCE) -> () {
bb0(%0 : @owned $NCE):
%1 = drop_deinit %0
switch_enum %1, case #NCE.a!enumelt: bb1, case #NCE.b!enumelt: bb2
bb1:
%15 = tuple ()
return %15
bb2(%4 : @owned $AnyObject):
destroy_value %4
unreachable
}
// CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use : // CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use :
// CHECK: bb3([[R:%.*]] : @reborrow $B): // CHECK: bb3([[R:%.*]] : @reborrow $B):
// CHECK: bb6([[G:%.*]] : @guaranteed $E): // CHECK: bb6([[G:%.*]] : @guaranteed $E):