Skip to content

Liveness cleanup - preparation for OSSA liveness #61475

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 3 commits into from
Oct 7, 2022
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/OwnershipUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ class DeadEndBlocks;
class MultiDefPrunedLiveness;
struct BorrowedValue;

/// Returns true if v is an address or trivial.
bool isValueAddressOrTrivial(SILValue v);
//===----------------------------------------------------------------------===//
// Forwarding Utilities
//
// TODO: encapsulate in a ForwardingInstruction abstraction
//===----------------------------------------------------------------------===//

/// Is the opcode that produces \p value capable of forwarding guaranteed
/// values?
Expand Down Expand Up @@ -108,6 +111,10 @@ inline bool isForwardingConsume(SILValue value) {
return canOpcodeForwardOwnedValues(value);
}

//===----------------------------------------------------------------------===//
// Ownership Def-Use Utilities
//===----------------------------------------------------------------------===//

bool hasPointerEscape(BorrowedValue value);

/// Find leaf "use points" of \p guaranteedValue that determine its lifetime
Expand Down
5 changes: 0 additions & 5 deletions lib/SIL/Utils/OwnershipUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ bool swift::hasPointerEscape(BorrowedValue value) {
return false;
}

bool swift::isValueAddressOrTrivial(SILValue v) {
return v->getType().isAddress() ||
v->getOwnershipKind() == OwnershipKind::None;
}

bool swift::canOpcodeForwardGuaranteedValues(SILValue value) {
// If we have an argument from a transforming terminator, we can forward
// guaranteed.
Expand Down
90 changes: 73 additions & 17 deletions lib/SIL/Utils/PrunedLiveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,32 +442,68 @@ template class PrunedLiveRange<MultiDefPrunedLiveness>;
// SSAPrunedLiveness
//===----------------------------------------------------------------------===//

void SSAPrunedLiveness::findBoundariesInBlock(
SILBasicBlock *block, bool isLiveOut,
PrunedLivenessBoundary &boundary) const {
assert(isInitialized());

// For SSA, a live-out block cannot have a boundary.
if (isLiveOut)
return;
/// Given live-within (non-live-out) \p block, find the last user.
void findBoundaryInNonDefBlock(SILBasicBlock *block,
PrunedLivenessBoundary &boundary,
const PrunedLiveness &liveness) {
assert(liveness.getBlockLiveness(block) == PrunedLiveBlocks::LiveWithin);

bool isDefBlockState = isDefBlock(block);
for (SILInstruction &inst : llvm::reverse(*block)) {
if (isDefBlockState && isDef(&inst)) {
if (liveness.isInterestingUser(&inst)) {
boundary.lastUsers.push_back(&inst);
return;
}
}
llvm_unreachable("live-within block must contain an interesting use");
}

/// Given a live-within \p block that contains an SSA definition, and knowledge
/// that all live uses are dominated by that single definition, find either the
/// last user or a dead def.
///
/// A live range with a single definition cannot have any uses above that
/// definition in the same block. This even holds for unreachable self-loops.
void findBoundaryInSSADefBlock(SILNode *ssaDef,
PrunedLivenessBoundary &boundary,
const PrunedLiveness &liveness) {
// defInst is null for argument defs.
SILInstruction *defInst = dyn_cast<SILInstruction>(ssaDef);
for (SILInstruction &inst : llvm::reverse(*ssaDef->getParentBlock())) {
if (&inst == defInst) {
boundary.deadDefs.push_back(cast<SILNode>(&inst));
return;
}
if (isInterestingUser(&inst)) {
if (liveness.isInterestingUser(&inst)) {
boundary.lastUsers.push_back(&inst);
return;
}
}
auto *deadArg = dyn_cast<SILArgument>(def);
assert(deadArg && deadArg->getParent() == block
auto *deadArg = dyn_cast<SILArgument>(ssaDef);
assert(deadArg
&& "findBoundariesInBlock must be called on a live block");
boundary.deadDefs.push_back(deadArg);
}

void SSAPrunedLiveness::findBoundariesInBlock(
SILBasicBlock *block, bool isLiveOut,
PrunedLivenessBoundary &boundary) const {
assert(isInitialized());

// For SSA, a live-out block cannot have a boundary.
if (isLiveOut)
return;

// Handle live-within block
if (!isDefBlock(block)) {
findBoundaryInNonDefBlock(block, boundary, *this);
return;
}
// Find either the last user or a dead def
auto *defInst = def->getDefiningInstruction();
SILNode *defNode = defInst ? cast<SILNode>(defInst) : cast<SILArgument>(def);
findBoundaryInSSADefBlock(defNode, boundary, *this);
}

//===----------------------------------------------------------------------===//
// MultiDefPrunedLiveness
//===----------------------------------------------------------------------===//
Expand All @@ -476,25 +512,45 @@ void MultiDefPrunedLiveness::findBoundariesInBlock(
SILBasicBlock *block, bool isLiveOut,
PrunedLivenessBoundary &boundary) const {
assert(isInitialized());
unsigned prevCount = boundary.deadDefs.size() + boundary.lastUsers.size();

if (!isDefBlock(block)) {
// A live-out block with no defs cannot have a boundary.
if (!isLiveOut) {
findBoundaryInNonDefBlock(block, boundary, *this);
}
return;
}
// Handle def blocks...
//
// First, check for an SSA live range
if (++defs.begin() == defs.end()) {
// For SSA, a live-out block cannot have a boundary.
if (!isLiveOut) {
findBoundaryInSSADefBlock(*defs.begin(), boundary, *this);
}
return;
}
// Handle a live-out or live-within block with potentially multiple defs
unsigned prevCount = boundary.deadDefs.size() + boundary.lastUsers.size();
bool isLive = isLiveOut;
bool isDefBlockState = isDefBlock(block);
for (auto &inst : llvm::reverse(*block)) {
// Check if the instruction is a def before checking whether it is a
// use. The same instruction can be both a dead def and boundary use.
if (isDefBlockState && isDef(&inst)) {
if (isDef(&inst)) {
if (!isLive) {
boundary.deadDefs.push_back(cast<SILNode>(&inst));
}
isLive = false;
}
// Note: the same instruction could potentially be both a dead def and last
// user. The liveness boundary supports this, although it won't happen in
// any context where we care about inserting code on the boundary.
if (!isLive && isInterestingUser(&inst)) {
boundary.lastUsers.push_back(&inst);
isLive = true;
}
}
if (!isLive && isDefBlockState) {
if (!isLive) {
for (SILArgument *deadArg : block->getArguments()) {
if (defs.contains(deadArg)) {
boundary.deadDefs.push_back(deadArg);
Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/Verifier/SILOwnershipVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ bool SILValueOwnershipChecker::checkValueWithoutLifetimeEndingUses(
}
}

if (!isValueAddressOrTrivial(value)) {
if (value->getOwnershipKind() != OwnershipKind::None) {
return !errorBuilder.handleMalformedSIL([&] {
if (value->getOwnershipKind() == OwnershipKind::Owned) {
llvm::errs() << "Error! Found a leaked owned value that was never "
Expand Down