Skip to content

Commit de48c2f

Browse files
authored
Merge pull request #84946 from eeckstein/fix-simplify-cfg-6.2
[6.2] SimplifyCFG: insert compensating destroy when replacing a `switch_enum`
2 parents d7bb1fe + c4c1c50 commit de48c2f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,12 @@ bool SimplifyCFG::simplifySwitchEnumUnreachableBlocks(SwitchEnumInst *SEI) {
17911791
}
17921792

17931793
if (Dest->args_empty()) {
1794-
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), Dest);
1794+
SILBuilderWithScope builder(SEI);
1795+
if (SEI->getOperand()->getOwnershipKind() == OwnershipKind::Owned) {
1796+
// Note that a `destroy_value` would be wrong for non-copyable enums with deinits.
1797+
builder.createEndLifetime(SEI->getLoc(), SEI->getOperand());
1798+
}
1799+
builder.createBranch(SEI->getLoc(), Dest);
17951800

17961801
addToWorklist(SEI->getParent());
17971802
addToWorklist(Dest);

test/SILOptimizer/simplify_cfg_ossa.sil

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-sil-opt -sil-print-types -enable-objc-interop -enable-sil-verify-all %s -simplify-cfg | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types -enable-objc-interop -enable-experimental-feature MoveOnlyEnumDeinits -enable-sil-verify-all %s -simplify-cfg | %FileCheck %s
2+
3+
// REQUIRES: swift_feature_MoveOnlyEnumDeinits
24

35
// OSSA form of tests from simplify_cfg.sil and simplify_cfg_simple.sil.
46
//
@@ -64,6 +66,12 @@ struct S {
6466
var b: NonTrivial
6567
}
6668

69+
enum NCE: ~Copyable {
70+
case a
71+
case b(AnyObject)
72+
deinit
73+
}
74+
6775
///////////
6876
// Tests //
6977
///////////
@@ -1944,6 +1952,44 @@ bb3:
19441952
return %t : $()
19451953
}
19461954

1955+
// CHECK-LABEL: sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block :
1956+
// CHECK: bb0(%0 : @owned $Optional<AnyObject>):
1957+
// CHECK-NEXT: end_lifetime %0
1958+
// CHECK-NEXT: tuple
1959+
// CHECK: } // end sil function 'insert_compensating_endlifetime_in_switch_enum_destination_block'
1960+
sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block : $@convention(thin) (@owned Optional<AnyObject>) -> () {
1961+
bb0(%0 : @owned $Optional<AnyObject>):
1962+
switch_enum %0, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt: bb2
1963+
1964+
bb1:
1965+
%15 = tuple ()
1966+
return %15
1967+
1968+
bb2(%4 : @owned $AnyObject):
1969+
destroy_value %4
1970+
unreachable
1971+
}
1972+
1973+
// CHECK-LABEL: sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block2 :
1974+
// CHECK: bb0(%0 : @owned $NCE):
1975+
// CHECK-NEXT: %1 = drop_deinit %0
1976+
// CHECK-NEXT: end_lifetime %1
1977+
// CHECK-NEXT: tuple
1978+
// CHECK: } // end sil function 'insert_compensating_endlifetime_in_switch_enum_destination_block2'
1979+
sil [ossa] @insert_compensating_endlifetime_in_switch_enum_destination_block2 : $@convention(thin) (@owned NCE) -> () {
1980+
bb0(%0 : @owned $NCE):
1981+
%1 = drop_deinit %0
1982+
switch_enum %1, case #NCE.a!enumelt: bb1, case #NCE.b!enumelt: bb2
1983+
1984+
bb1:
1985+
%15 = tuple ()
1986+
return %15
1987+
1988+
bb2(%4 : @owned $AnyObject):
1989+
destroy_value %4
1990+
unreachable
1991+
}
1992+
19471993
// CHECK-LABEL: sil [ossa] @replace_phi_arg_with_borrowed_from_use :
19481994
// CHECK: bb3([[R:%.*]] : @reborrow $B):
19491995
// CHECK: bb6([[G:%.*]] : @guaranteed $E):

0 commit comments

Comments
 (0)