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
8 changes: 8 additions & 0 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ bool swift::isInstructionTriviallyDead(SILInstruction *inst) {
if (isa<BorrowedFromInst>(inst))
return false;

// A dead `destructure_struct` with an owned argument can appear for a non-copyable or
// non-escapable struct which has only trivial elements. The instruction is not trivially
// dead because it ends the lifetime of its operand.
if (isa<DestructureStructInst>(inst) &&
inst->getOperand(0)->getOwnershipKind() == OwnershipKind::Owned) {
return false;
}

// These invalidate enums so "write" memory, but that is not an essential
// operation so we can remove these if they are trivially dead.
if (isa<UncheckedTakeEnumDataAddrInst>(inst))
Expand Down
16 changes: 16 additions & 0 deletions test/SILOptimizer/sil_combine_moveonly.sil
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct S : ~Copyable {
deinit {}
}

struct S2 : ~Copyable {
@_hasStorage var a: Int { get set }
@_hasStorage var b: Int { get set }
}

struct FileDescriptor: ~Copyable {
var fd: Builtin.Int64

Expand Down Expand Up @@ -169,3 +174,14 @@ bb0:
%13 = tuple ()
return %13
}

// CHECK-LABEL: sil [ossa] @dont_remove_destructure_struct_of_non_copyable :
// CHECK: destructure_struct
// CHECK-LABEL: } // end sil function 'dont_remove_destructure_struct_of_non_copyable'
sil [ossa] @dont_remove_destructure_struct_of_non_copyable : $@convention(method) (@owned S2) -> () {
bb0(%0 : @owned $S2):
(%1, %2) = destructure_struct %0
%r = tuple ()
return %r
}