Skip to content

[pred-memopt] Simplify logic around lifetime extending a value that is consumed in a loop. #30619

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
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
57 changes: 10 additions & 47 deletions lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,46 +1199,25 @@ void AvailableValueAggregator::addHandOffCopyDestroysForPhis(
// no further work to do.
auto *loadOperand = &load->getAllOperands()[0];
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks);
bool didInsertDestroy = false;
bool consumedInLoop = checker.completeConsumingUseSet(
phi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
SILBuilderWithScope builder(iter);
builder.emitDestroyValueOperation(loc, phi);
didInsertDestroy = true;
});

// If we did not insert any destroy_values, we either:
//
// 1. Are consuming the value in a loop, but the loop is an infinite loop
// so we shouldn't insert a destroy.
//
// 2. Are not consuming the value in the loop and our phi is strongly
// control equivalent with the load_borrow, so we insert a destroy
// /after/ the load_borrow.
if (!didInsertDestroy) {
if (!consumedInLoop) {
auto next = std::next(load->getIterator());
SILBuilderWithScope builder(next);
builder.emitDestroyValueOperation(next->getLoc(), phi);
}
continue;
}

// Ok, we found some leaking blocks and potentially that our load is
// "consumed" inside a different loop in the loop nest from cvi. If we are
// consumed in the loop, then our visit should have inserted all of the
// necessary destroys for us by inserting the destroys on the loop
// boundaries. So, exit now.
// Ok, we found some leaking blocks and potentially that our load is
// "consumed" inside a different loop in the loop nest from cvi. If we are
// consumed in the loop, then our visit should have inserted all of the
// necessary destroys for us by inserting the destroys on the loop
// boundaries. So, exit now.
// boundaries. So, continue.
//
// NOTE: This includes cases where due to an infinite loop, we did not
// insert /any/ destroys since the loop has no boundary in a certain sense.
if (consumedInLoop) {
continue;
}

// Otherwise, we had a branching case,
// Otherwise, we need to insert one last destroy after the load for our phi.
auto next = std::next(load->getIterator());
SILBuilderWithScope builder(next);
builder.emitDestroyValueOperation(next->getLoc(), phi);
Expand Down Expand Up @@ -1304,41 +1283,25 @@ void AvailableValueAggregator::addMissingDestroysForCopiedValues(
// no further work to do.
auto *loadOperand = &load->getAllOperands()[0];
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks);
bool didInsertDestroy = false;
bool consumedInLoop = checker.completeConsumingUseSet(
cvi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
SILBuilderWithScope builder(iter);
builder.emitDestroyValueOperation(loc, cvi);
didInsertDestroy = true;
});

// If we did not insert any destroy_values, we either:
//
// 1. Are consuming the value in a loop, but the loop is an infinite loop
// so we shouldn't insert a destroy.
//
// 2. Are not consuming the value in the loop and our phi is strongly
// control equivalent with the load_borrow, so we insert a destroy
// /after/ the load_borrow.
if (!didInsertDestroy) {
if (!consumedInLoop) {
auto next = std::next(load->getIterator());
SILBuilderWithScope builder(next);
builder.emitDestroyValueOperation(next->getLoc(), cvi);
}
continue;
}

// Ok, we found some leaking blocks and potentially that our load is
// "consumed" inside a different loop in the loop nest from cvi. If we are
// consumed in the loop, then our visit should have inserted all of the
// necessary destroys for us by inserting the destroys on the loop
// boundaries. So, exit now.
// boundaries. So, continue.
//
// NOTE: This includes cases where due to an infinite loop, we did not
// insert /any/ destroys since the loop has no boundary in a certain sense.
if (consumedInLoop) {
continue;
}

// Otherwise, we had a branching case,
// Otherwise, we need to insert one last destroy after the load for our phi.
auto next = std::next(load->getIterator());
SILBuilderWithScope builder(next);
builder.emitDestroyValueOperation(next->getLoc(), cvi);
Expand Down