Skip to content

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

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
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
17 changes: 17 additions & 0 deletions lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ struct PrintTypeLowering : UnitTest {
}
};

// Arguments:
// - instruction: the instruction to delete
// Dumps:
// - the function
struct DeleterDeleteIfDeadTest : UnitTest {
DeleterDeleteIfDeadTest(UnitTestRunner *pass) : UnitTest(pass) {}
void invoke(Arguments &arguments) override {
auto *inst = arguments.takeInstruction();
InstructionDeleter deleter;
llvm::dbgs() << "Deleting-if-dead " << *inst;
auto deleted = deleter.deleteIfDead(inst);
llvm::dbgs() << "deleteIfDead returned " << deleted << "\n";
getFunction()->dump();
}
};

//===----------------------------------------------------------------------===//
// MARK: OSSA Lifetime Unit Tests
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -975,6 +991,7 @@ void UnitTestRunner::withTest(StringRef name, Doit doit) {
ADD_UNIT_TEST_SUBCLASS("function-get-self-argument-index", FunctionGetSelfArgumentIndex)
ADD_UNIT_TEST_SUBCLASS("has-pointer-escape", OwnershipUtilsHasPointerEscape)
ADD_UNIT_TEST_SUBCLASS("print-type-lowering", PrintTypeLowering)
ADD_UNIT_TEST_SUBCLASS("deleter-delete-if-dead", DeleterDeleteIfDeadTest)
ADD_UNIT_TEST_SUBCLASS("interior-liveness", InteriorLivenessTest)
ADD_UNIT_TEST_SUBCLASS("is-deinit-barrier", IsDeinitBarrierTest)
ADD_UNIT_TEST_SUBCLASS("is-lexical", IsLexicalTest)
Expand Down
17 changes: 16 additions & 1 deletion lib/SILOptimizer/Utils/InstructionDeleter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
//
//===----------------------------------------------------------------------===//

#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "swift/SIL/SILFunction.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 +60,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
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 -unit-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 : $()
}