Skip to content

Commit 171607a

Browse files
committed
DeadStoreElimination: fix a corner case of storing a trivial case of a non-trivial enum
Storing a trivial enum case in a non-trivial enum must be treated like a non-trivial init or assign, e.g. ``` %1 = enum $Optional<String>, #Optional.none!enumelt store %1 to [trivial] %0 // <- cannot delete this store! store %2 to [assign] %0 ```
1 parent d58dd6e commit 171607a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DeadStoreElimination.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,15 @@ private extension StoreInst {
167167

168168
var hasValidOwnershipForDeadStoreElimination: Bool {
169169
switch storeOwnership {
170-
case .unqualified, .trivial:
170+
case .unqualified:
171171
return true
172+
case .trivial:
173+
// Storing a trivial enum case in a non-trivial enum must be treated like a non-trivial
174+
// init or assign, e.g.
175+
// %1 = enum $Optional<String>, #Optional.none!enumelt
176+
// store %1 to [trivial] %0 // <- cannot delete this store!
177+
// store %2 to [assign] %0
178+
return source.type.isTrivial(in: parentFunction)
172179
case .initialize, .assign:
173180
// In OSSA, non-trivial values cannot be dead-store eliminated because that could shrink
174181
// the lifetime of the original stored value (because it's not kept in memory anymore).

test/SILOptimizer/dead_store_elim.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,17 @@ bb0(%0 : $Int, %1 : @owned $IntAndFoo, %2 : $*IntAndFoo):
16151615
return %r : $()
16161616
}
16171617

1618+
// We don't support this, yet.
1619+
// Just check that we don't crash.
1620+
sil [ossa] @test_trivial_store_of_non_trivial_enum : $@convention(thin) (@owned Optional<String>) -> @out Optional<String> {
1621+
bb0(%0 : $*Optional<String>, %1 : @owned $Optional<String>):
1622+
%2 = enum $Optional<String>, #Optional.none!enumelt
1623+
store %2 to [trivial] %0
1624+
store %1 to [assign] %0
1625+
%r = tuple ()
1626+
return %r
1627+
}
1628+
16181629
// CHECK-LABEL: sil @test_bind_memory :
16191630
// CHECK: store %0
16201631
// CHECK: end sil function 'test_bind_memory'

0 commit comments

Comments
 (0)