Skip to content

Fix drop_deinit side effects. #66279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2023
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
11 changes: 9 additions & 2 deletions include/swift/SIL/SILNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,15 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
// effects relative to other OSSA values like copy_value.
SINGLE_VALUE_INST(MoveValueInst, move_value, SingleValueInstruction, None,
DoesNotRelease)
SINGLE_VALUE_INST(DropDeinitInst, drop_deinit, SingleValueInstruction, None,
DoesNotRelease)
// A drop_deinit has no user-level side effects. It does, however, change the
// information about the owenership of its operand, and therefore cannot be
// arbitrarily deleted. It effectively wraps the type of the operand so that
// the resulting value is an equivalent type without a user-defined deinit.
//
// TODO: Add an OwnershipEffect type of side effect for instructions like
// drop_deinit to indicate their ownership effects.
SINGLE_VALUE_INST(DropDeinitInst, drop_deinit, SingleValueInstruction,
MayHaveSideEffects, DoesNotRelease)
// A canary value inserted by a SIL generating frontend to signal to the move
// checker to check a specific value. Valid only in Raw SIL. The relevant
// checkers should remove the mark_must_check instruction after successfully
Expand Down
1 change: 1 addition & 0 deletions lib/SIL/Utils/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2698,6 +2698,7 @@ void swift::visitAccessedAddress(SILInstruction *I,
case SILInstructionKind::DeinitExistentialValueInst:
case SILInstructionKind::DestroyAddrInst:
case SILInstructionKind::DestroyValueInst:
case SILInstructionKind::DropDeinitInst:
case SILInstructionKind::EndAccessInst:
case SILInstructionKind::EndApplyInst:
case SILInstructionKind::EndBorrowInst:
Expand Down
25 changes: 25 additions & 0 deletions test/SILOptimizer/dead_code_elimination_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ struct CAndBit {
var bit: Int1
}

@_moveOnly
struct MO {
deinit
}

sil @dummy : $@convention(thin) () -> ()

// CHECK-LABEL: sil [ossa] @dead1 :
Expand Down Expand Up @@ -407,3 +412,23 @@ exit:
%retval = tuple ()
return %retval : $()
}

// Test that DCE does not eliminate a drop_deinit.
//
// rdar://109863801 ([move-only] DCE removes drop_deinit, so
// user-defined deinitializers call themselves recursively at -O)
//
// CHECK-LABEL: sil hidden [ossa] @testDropDeinit : $@convention(method) (@owned MO) -> () {
// CHECK: [[DROP:%.*]] = drop_deinit %0 : $MO
// CHECK: end_lifetime [[DROP]] : $MO
// CHECK-LABEL: } // end sil function 'testDropDeinit'
//
// MO.deinit
sil hidden [ossa] @testDropDeinit : $@convention(method) (@owned MO) -> () {
bb0(%0 : @owned $MO):
debug_value %0 : $MO, let, name "self", argno 1, implicit
%61 = drop_deinit %0 : $MO
end_lifetime %61 : $MO
%63 = tuple ()
return %63 : $()
}