Skip to content

Commit 9053cce

Browse files
committed
SimplifyCFG: fix an ownership verifier error caused by switch_enum simplification
If constant folding a switch_enum ends up in branching to a no-payload case, the enum value still needs to be destroyed to satisfy the ownership verifier. #74903 rdar://131726690
1 parent ebf3ec7 commit 9053cce

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,9 @@ bool SimplifyCFG::simplifySwitchEnumBlock(SwitchEnumInst *SEI) {
21912191
SEI->eraseFromParent();
21922192
updateBorrowedFromPhis(PM, { cast<SILPhiArgument>(LiveBlock->getArgument(0)) });
21932193
} else {
2194+
if (SEI->getOperand()->getOwnershipKind() == OwnershipKind::Owned) {
2195+
Builder.createDestroyValue(loc, SEI->getOperand());
2196+
}
21942197
Builder.createBranch(loc, LiveBlock);
21952198
SEI->eraseFromParent();
21962199
}

test/SILOptimizer/simplify_cfg_ossa.sil

+32
Original file line numberDiff line numberDiff line change
@@ -1887,3 +1887,35 @@ bb6(%21 : $Optional<Int>):
18871887
return %22 : $()
18881888
}
18891889

1890+
1891+
// CHECK-LABEL: sil [ossa] @simplify_switch_enum3 :
1892+
// CHECK-NOT: switch_enum
1893+
// CHECK: destroy_value %{{[0-9]+}} : $Optional<String>
1894+
// CHECK: } // end sil function 'simplify_switch_enum3'
1895+
sil [ossa] @simplify_switch_enum3 : $@convention(thin) () -> () {
1896+
bb0:
1897+
cond_br undef, bb1, bb2
1898+
1899+
bb1:
1900+
%1 = enum $Optional<String>, #Optional.none!enumelt
1901+
br bb3(%1 : $Optional<String>)
1902+
1903+
bb2:
1904+
%4 = enum $Optional<String>, #Optional.none!enumelt
1905+
br bb3(%4 : $Optional<String>)
1906+
1907+
bb3(%10 : @owned $Optional<String>):
1908+
debug_value %10 : $Optional<String>
1909+
switch_enum %10 : $Optional<String>, case #Optional.some!enumelt: bb5, case #Optional.none!enumelt: bb4
1910+
1911+
bb4:
1912+
br bb6
1913+
1914+
bb5(%14 : @owned $String):
1915+
destroy_value %14 : $String
1916+
br bb6
1917+
1918+
bb6:
1919+
%17 = tuple ()
1920+
return %17 : $()
1921+
}

0 commit comments

Comments
 (0)