Skip to content

[InstructionDeleter] Don't delete-as dead instructions which produce owned move-only values. #68108

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
Aug 24, 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
34 changes: 33 additions & 1 deletion lib/SILOptimizer/Utils/InstructionDeleter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
//
//===----------------------------------------------------------------------===//

#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/Test.h"
#include "swift/SILOptimizer/Utils/ConstExpr.h"
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"

using namespace swift;
Expand Down Expand Up @@ -60,6 +61,21 @@ static bool isScopeAffectingInstructionDead(SILInstruction *inst,
if (!hasOnlyEndOfScopeOrEndOfLifetimeUses(inst)) {
return false;
}

// If inst has any owned move-only value as a result, deleting it may shorten
// that value's lifetime which is illegal according to language rules.
//
// In particular, this check is needed before returning true when
// getSingleValueCopyOrCast returns true. That function returns true for
// move_value instructions. And `move_value %moveOnlyValue` must not be
// deleted.
for (auto result : inst->getResults()) {
if (result->getType().isPureMoveOnly() &&
result->getOwnershipKind() == OwnershipKind::Owned) {
return false;
}
}

// If inst is a copy or beginning of scope, inst is dead, since we know that
// it is used only in a destroy_value or end-of-scope instruction.
if (getSingleValueCopyOrCast(inst))
Expand Down Expand Up @@ -289,6 +305,22 @@ bool InstructionDeleter::deleteIfDead(SILInstruction *inst, bool fixLifetime) {
return false;
}

namespace swift::test {
// Arguments:
// - instruction: the instruction to delete
// Dumps:
// - the function
static FunctionTest DeleterDeleteIfDeadTest(
"deleter-delete-if-dead", [](auto &function, auto &arguments, auto &test) {
auto *inst = arguments.takeInstruction();
InstructionDeleter deleter;
llvm::dbgs() << "Deleting-if-dead " << *inst;
auto deleted = deleter.deleteIfDead(inst);
llvm::dbgs() << "deleteIfDead returned " << deleted << "\n";
function.dump();
});
} // namespace swift::test

void InstructionDeleter::forceDeleteAndFixLifetimes(SILInstruction *inst) {
SILFunction *fun = inst->getFunction();
bool preserveDebugInfo =
Expand Down
30 changes: 30 additions & 0 deletions test/SILOptimizer/instruction_deleter.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-sil-opt -test-runner %s -o /dev/null 2>&1 | %FileCheck %s

struct MOS : ~Copyable {}

sil @getMOS : $() -> (@owned MOS)
sil @barrier : $() -> ()

// CHECK-LABEL: begin running test {{.*}} on dontDeleteDeadMoveOnlyValue
// CHECK: Deleting-if-dead {{.*}} move_value
// CHECK: deleteIfDead returned 0
// CHECK-LABEL: sil [ossa] @dontDeleteDeadMoveOnlyValue : {{.*}} {
// CHECK: [[GET:%[^,]+]] = function_ref @getMOS
// CHECK: [[BARRIER:%[^,]+]] = function_ref @barrier
// CHECK: [[MOS:%[^,]+]] = apply [[GET]]()
// CHECK: [[MOV:%[^,]+]] = move_value [[MOS]]
// CHECK: apply [[BARRIER]]()
// CHECK: destroy_value [[MOV]]
// CHECK-LABEL: } // end sil function 'dontDeleteDeadMoveOnlyValue'
// CHECK-LABEL: end running test {{.*}} on dontDeleteDeadMoveOnlyValue
sil [ossa] @dontDeleteDeadMoveOnlyValue : $() -> () {
%get = function_ref @getMOS : $@convention(thin) () -> (@owned MOS)
%barrier = function_ref @barrier : $@convention(thin) () -> ()
%mos = apply %get() : $@convention(thin) () -> (@owned MOS)
test_specification "deleter-delete-if-dead @instruction"
%mov = move_value %mos : $MOS
apply %barrier() : $@convention(thin) () -> ()
destroy_value %mov : $MOS
%retval = tuple ()
return %retval : $()
}