Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,15 @@ private extension StoreInst {

var hasValidOwnershipForDeadStoreElimination: Bool {
switch storeOwnership {
case .unqualified, .trivial:
case .unqualified:
return true
case .trivial:
// 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
return source.type.isTrivial(in: parentFunction)
case .initialize, .assign:
// In OSSA, non-trivial values cannot be dead-store eliminated because that could shrink
// the lifetime of the original stored value (because it's not kept in memory anymore).
Expand Down
11 changes: 11 additions & 0 deletions test/SILOptimizer/dead_store_elim.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,17 @@ bb0(%0 : $Int, %1 : @owned $IntAndFoo, %2 : $*IntAndFoo):
return %r : $()
}

// We don't support this, yet.
// Just check that we don't crash.
sil [ossa] @test_trivial_store_of_non_trivial_enum : $@convention(thin) (@owned Optional<String>) -> @out Optional<String> {
bb0(%0 : $*Optional<String>, %1 : @owned $Optional<String>):
%2 = enum $Optional<String>, #Optional.none!enumelt
store %2 to [trivial] %0
store %1 to [assign] %0
%r = tuple ()
return %r
}

// CHECK-LABEL: sil @test_bind_memory :
// CHECK: store %0
// CHECK: end sil function 'test_bind_memory'
Expand Down