Skip to content

[ownership] Replace all uses outside of the SILOwnershipVerifier of LinearLifetimeChecker::checkValue() and make it a private implementation detail. #30602

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
157 changes: 44 additions & 113 deletions include/swift/SIL/LinearLifetimeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,103 +28,8 @@ class SILInstruction;
class SILModule;
class SILValue;
class DeadEndBlocks;

namespace ownership {

struct ErrorBehaviorKind {
enum inner_t {
Invalid = 0,
ReturnFalse = 1,
PrintMessage = 2,
Assert = 4,
ReturnFalseOnLeak = 8,
PrintMessageAndReturnFalse = PrintMessage | ReturnFalse,
PrintMessageAndAssert = PrintMessage | Assert,
ReturnFalseOnLeakAssertOtherwise = ReturnFalseOnLeak | Assert,
} Value;

ErrorBehaviorKind() : Value(Invalid) {}
ErrorBehaviorKind(inner_t Inner) : Value(Inner) { assert(Value != Invalid); }

bool shouldAssert() const {
assert(Value != Invalid);
return Value & Assert;
}

bool shouldReturnFalseOnLeak() const {
assert(Value != Invalid);
return Value & ReturnFalseOnLeak;
}

bool shouldPrintMessage() const {
assert(Value != Invalid);
return Value & PrintMessage;
}

bool shouldReturnFalse() const {
assert(Value != Invalid);
return Value & ReturnFalse;
}
};

} // end namespace ownership

class LinearLifetimeError {
ownership::ErrorBehaviorKind errorBehavior;
bool foundUseAfterFree = false;
bool foundLeak = false;
bool foundOverConsume = false;

public:
LinearLifetimeError(ownership::ErrorBehaviorKind errorBehavior)
: errorBehavior(errorBehavior) {}

bool getFoundError() const {
return foundUseAfterFree || foundLeak || foundOverConsume;
}

bool getFoundLeak() const { return foundLeak; }

bool getFoundUseAfterFree() const { return foundUseAfterFree; }

bool getFoundOverConsume() const { return foundOverConsume; }

void handleLeak(llvm::function_ref<void()> &&messagePrinterFunc) {
foundLeak = true;

if (errorBehavior.shouldPrintMessage())
messagePrinterFunc();

if (errorBehavior.shouldReturnFalseOnLeak())
return;

// We already printed out our error if we needed to, so don't pass it along.
handleError([]() {});
}

void handleOverConsume(llvm::function_ref<void()> &&messagePrinterFunc) {
foundOverConsume = true;
handleError(std::move(messagePrinterFunc));
}

void handleUseAfterFree(llvm::function_ref<void()> &&messagePrinterFunc) {
foundUseAfterFree = true;
handleError(std::move(messagePrinterFunc));
}

private:
void handleError(llvm::function_ref<void()> &&messagePrinterFunc) {
if (errorBehavior.shouldPrintMessage())
messagePrinterFunc();

if (errorBehavior.shouldReturnFalse()) {
return;
}

assert(errorBehavior.shouldAssert() && "At this point, we should assert");
llvm_unreachable("triggering standard assertion failure routine");
}
};
class SILOwnershipVerifier;
class SILValueOwnershipChecker;

/// A class used to validate linear lifetime with respect to an SSA-like
/// definition.
Expand All @@ -140,6 +45,14 @@ class LinearLifetimeError {
/// uses must not be reachable from each other and jointly post-dominate all
/// consuming uses as well as the defining block/instruction.
class LinearLifetimeChecker {
public:
class Error;
struct ErrorBehaviorKind;

private:
friend class SILOwnershipVerifier;
friend class SILValueOwnershipChecker;

SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks;
DeadEndBlocks &deadEndBlocks;

Expand All @@ -148,6 +61,36 @@ class LinearLifetimeChecker {
DeadEndBlocks &deadEndBlocks)
: visitedBlocks(visitedBlocks), deadEndBlocks(deadEndBlocks) {}

/// Returns true that \p value forms a linear lifetime with consuming uses \p
/// consumingUses, non consuming uses \p nonConsumingUses. Returns false
/// otherwise.
bool validateLifetime(SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses);

/// Given a value and a consuming use of that value, compute a non-unique
/// minimal set of insertion points that together with \p consumingUse
/// post-dominate and end the lifetime of \p value.
///
/// Returns true if we completed the consuming use set and discovered that \p
/// consumingUse is not strongly control equivalent to value (meaning
/// consumingUse is not in the same loop in the loop nest as value).
///
/// \p scratch A scratch buffer to be used during our computation. If nullptr,
/// routine will create its own small vector.
bool completeConsumingUseSet(
SILValue value, Operand *consumingUse,
SmallVectorImpl<SILBasicBlock *> &scratch,
function_ref<void(SILBasicBlock::iterator insertPt)> visitor);

/// Overload without scratch space.
bool completeConsumingUseSet(
SILValue value, Operand *consumingUse,
function_ref<void(SILBasicBlock::iterator insertPt)> visitor) {
SmallVector<SILBasicBlock *, 4> scratch;
return completeConsumingUseSet(value, consumingUse, scratch, visitor);
}

private:
/// Returns true if:
///
/// 1. No consuming uses are reachable from any other consuming use, from any
Expand All @@ -164,22 +107,10 @@ class LinearLifetimeChecker {
/// error.
/// \p leakingBlocks If non-null a list of blocks where the value was detected
/// to leak. Can be used to insert missing destroys.
LinearLifetimeError
checkValue(SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses,
ownership::ErrorBehaviorKind errorBehavior,
SmallVectorImpl<SILBasicBlock *> *leakingBlocks = nullptr);

/// Returns true that \p value forms a linear lifetime with consuming uses \p
/// consumingUses, non consuming uses \p nonConsumingUses. Returns false
/// otherwise.
bool validateLifetime(SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses) {
return !checkValue(value, consumingUses, nonConsumingUses,
ownership::ErrorBehaviorKind::ReturnFalse,
nullptr /*leakingBlocks*/)
.getFoundError();
}
Error checkValue(SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses,
ErrorBehaviorKind errorBehavior,
SmallVectorImpl<SILBasicBlock *> *leakingBlocks = nullptr);
};

} // namespace swift
Expand Down
38 changes: 32 additions & 6 deletions lib/SIL/LinearLifetimeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "sil-linear-lifetime-checker"
#include "swift/SIL/LinearLifetimeChecker.h"
#include "LinearLifetimeCheckerPrivate.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILBasicBlock.h"
Expand All @@ -31,7 +31,6 @@
#include "llvm/Support/Debug.h"

using namespace swift;
using namespace swift::ownership;

//===----------------------------------------------------------------------===//
// Declarations
Expand All @@ -51,7 +50,7 @@ struct State {

/// The result error object that use to signal either that no errors were
/// found or if errors are found the specific type of error that was found.
LinearLifetimeError error;
LinearLifetimeChecker::Error error;

/// The blocks that we have already visited.
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks;
Expand Down Expand Up @@ -82,7 +81,7 @@ struct State {
SmallSetVector<SILBasicBlock *, 8> successorBlocksThatMustBeVisited;

State(SILValue value, SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
ErrorBehaviorKind errorBehavior,
LinearLifetimeChecker::ErrorBehaviorKind errorBehavior,
SmallVectorImpl<SILBasicBlock *> *leakingBlocks,
ArrayRef<Operand *> consumingUses, ArrayRef<Operand *> nonConsumingUses)
: value(value), beginBlock(value->getParentBlock()), error(errorBehavior),
Expand All @@ -91,7 +90,7 @@ struct State {

State(SILBasicBlock *beginBlock,
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
ErrorBehaviorKind errorBehavior,
LinearLifetimeChecker::ErrorBehaviorKind errorBehavior,
SmallVectorImpl<SILBasicBlock *> *leakingBlocks,
ArrayRef<Operand *> consumingUses, ArrayRef<Operand *> nonConsumingUses)
: value(), beginBlock(beginBlock), error(errorBehavior),
Expand Down Expand Up @@ -495,7 +494,7 @@ void State::checkDataflowEndState(DeadEndBlocks &deBlocks) {
// Top Level Entrypoints
//===----------------------------------------------------------------------===//

LinearLifetimeError LinearLifetimeChecker::checkValue(
LinearLifetimeChecker::Error LinearLifetimeChecker::checkValue(
SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses, ErrorBehaviorKind errorBehavior,
SmallVectorImpl<SILBasicBlock *> *leakingBlocks) {
Expand Down Expand Up @@ -588,3 +587,30 @@ LinearLifetimeError LinearLifetimeChecker::checkValue(
state.checkDataflowEndState(deadEndBlocks);
return state.error;
}

bool LinearLifetimeChecker::completeConsumingUseSet(
SILValue value, Operand *consumingUse,
SmallVectorImpl<SILBasicBlock *> &scratch,
function_ref<void(SILBasicBlock::iterator)> visitor) {
auto error = checkValue(value, {consumingUse}, {},
ErrorBehaviorKind::ReturnFalse, &scratch);

if (!error.getFoundError()) {
return false;
}

while (!scratch.empty()) {
visitor(scratch.pop_back_val()->begin());
}

// Return true if we found an over consume (meaning our use is in a loop).
return error.getFoundOverConsume();
}

bool LinearLifetimeChecker::validateLifetime(
SILValue value, ArrayRef<Operand *> consumingUses,
ArrayRef<Operand *> nonConsumingUses) {
return !checkValue(value, consumingUses, nonConsumingUses,
ErrorBehaviorKind::ReturnFalse, nullptr /*leakingBlocks*/)
.getFoundError();
}
114 changes: 114 additions & 0 deletions lib/SIL/LinearLifetimeCheckerPrivate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//===--- LinearLifetimeCheckerPrivate.h -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_SIL_LINEARLIFETIMECHECKER_PRIVATE_H
#define SWIFT_SIL_LINEARLIFETIMECHECKER_PRIVATE_H

#include "swift/SIL/LinearLifetimeChecker.h"

namespace swift {

struct LinearLifetimeChecker::ErrorBehaviorKind {
enum inner_t {
Invalid = 0,
ReturnFalse = 1,
PrintMessage = 2,
Assert = 4,
ReturnFalseOnLeak = 8,
PrintMessageAndReturnFalse = PrintMessage | ReturnFalse,
PrintMessageAndAssert = PrintMessage | Assert,
ReturnFalseOnLeakAssertOtherwise = ReturnFalseOnLeak | Assert,
} Value;

ErrorBehaviorKind() : Value(Invalid) {}
ErrorBehaviorKind(inner_t Inner) : Value(Inner) { assert(Value != Invalid); }

bool shouldAssert() const {
assert(Value != Invalid);
return Value & Assert;
}

bool shouldReturnFalseOnLeak() const {
assert(Value != Invalid);
return Value & ReturnFalseOnLeak;
}

bool shouldPrintMessage() const {
assert(Value != Invalid);
return Value & PrintMessage;
}

bool shouldReturnFalse() const {
assert(Value != Invalid);
return Value & ReturnFalse;
}
};

class LinearLifetimeChecker::Error {
ErrorBehaviorKind errorBehavior;
bool foundUseAfterFree = false;
bool foundLeak = false;
bool foundOverConsume = false;

public:
Error(ErrorBehaviorKind errorBehavior) : errorBehavior(errorBehavior) {}

bool getFoundError() const {
return foundUseAfterFree || foundLeak || foundOverConsume;
}

bool getFoundLeak() const { return foundLeak; }

bool getFoundUseAfterFree() const { return foundUseAfterFree; }

bool getFoundOverConsume() const { return foundOverConsume; }

void handleLeak(llvm::function_ref<void()> &&messagePrinterFunc) {
foundLeak = true;

if (errorBehavior.shouldPrintMessage())
messagePrinterFunc();

if (errorBehavior.shouldReturnFalseOnLeak())
return;

// We already printed out our error if we needed to, so don't pass it along.
handleError([]() {});
}

void handleOverConsume(llvm::function_ref<void()> &&messagePrinterFunc) {
foundOverConsume = true;
handleError(std::move(messagePrinterFunc));
}

void handleUseAfterFree(llvm::function_ref<void()> &&messagePrinterFunc) {
foundUseAfterFree = true;
handleError(std::move(messagePrinterFunc));
}

private:
void handleError(llvm::function_ref<void()> &&messagePrinterFunc) {
if (errorBehavior.shouldPrintMessage())
messagePrinterFunc();

if (errorBehavior.shouldReturnFalse()) {
return;
}

assert(errorBehavior.shouldAssert() && "At this point, we should assert");
llvm_unreachable("triggering standard assertion failure routine");
}
};

} // namespace swift

#endif
Loading