-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Refactor swift::findPointerEscape and handle additional cases #66723
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76a8f0c
Remove old hack in transitive address use walker for store_borrow
meg-gupta 031255c
Rename hasPointerEscape -> findPointerEscape
meg-gupta 4862ecc
Consolidate swift::findPointerEscape(SILValue) and swift::findPointer…
meg-gupta 6d3b4e0
Handle borrow uses and interior pointer uses in swift::findPointerEscape
meg-gupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,33 +25,52 @@ | |
|
||
using namespace swift; | ||
|
||
bool swift::hasPointerEscape(BorrowedValue original) { | ||
ValueWorklist worklist(original->getFunction()); | ||
worklist.push(*original); | ||
bool swift::findPointerEscape(SILValue original) { | ||
if (original->getOwnershipKind() != OwnershipKind::Owned && | ||
original->getOwnershipKind() != OwnershipKind::Guaranteed) { | ||
return false; | ||
} | ||
|
||
if (auto *phi = SILArgument::asPhi(*original)) { | ||
ValueWorklist worklist(original->getFunction()); | ||
worklist.push(original); | ||
if (auto *phi = SILArgument::asPhi(original)) { | ||
phi->visitTransitiveIncomingPhiOperands([&](auto *phi, auto *operand) { | ||
worklist.pushIfNotVisited(operand->get()); | ||
return true; | ||
}); | ||
} | ||
|
||
while (auto value = worklist.pop()) { | ||
for (auto *op : value->getUses()) { | ||
switch (op->getOperandOwnership()) { | ||
case OperandOwnership::ForwardingUnowned: | ||
for (auto use : value->getUses()) { | ||
switch (use->getOperandOwnership()) { | ||
case OperandOwnership::PointerEscape: | ||
case OperandOwnership::ForwardingUnowned: | ||
return true; | ||
|
||
case OperandOwnership::ForwardingConsume: { | ||
auto *branch = dyn_cast<BranchInst>(use->getUser()); | ||
if (!branch) { | ||
// Non-phi forwarding consumes end the lifetime of an owned value. | ||
break; | ||
} | ||
auto *phi = branch->getDestBB()->getArgument(use->getOperandNumber()); | ||
worklist.pushIfNotVisited(phi); | ||
break; | ||
} | ||
case OperandOwnership::Borrow: { | ||
auto borrowOp = BorrowingOperand(use); | ||
if (auto borrowValue = borrowOp.getBorrowIntroducingUserResult()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can a yielded or captured value escape? I think we talked about this being illegal because it crosses a function boundary. But the implementation here doesn't explain why we're ignoring these cases. We should at least explain that it was intentional. |
||
worklist.pushIfNotVisited(borrowValue.value); | ||
} | ||
break; | ||
} | ||
case OperandOwnership::Reborrow: { | ||
SILArgument *phi = PhiOperand(op).getValue(); | ||
SILArgument *phi = PhiOperand(use).getValue(); | ||
worklist.pushIfNotVisited(phi); | ||
break; | ||
} | ||
|
||
case OperandOwnership::GuaranteedForwarding: { | ||
// This may follow guaranteed phis. | ||
ForwardingOperand(op).visitForwardedValues([&](SILValue result) { | ||
ForwardingOperand(use).visitForwardedValues([&](SILValue result) { | ||
// Do not include transitive uses with 'none' ownership | ||
if (result->getOwnershipKind() == OwnershipKind::None) | ||
return true; | ||
|
@@ -60,42 +79,11 @@ bool swift::hasPointerEscape(BorrowedValue original) { | |
}); | ||
break; | ||
} | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool swift::hasPointerEscape(SILValue original) { | ||
if (auto borrowedValue = BorrowedValue(original)) { | ||
return hasPointerEscape(borrowedValue); | ||
} | ||
assert(original->getOwnershipKind() == OwnershipKind::Owned); | ||
|
||
ValueWorklist worklist(original->getFunction()); | ||
worklist.push(original); | ||
if (auto *phi = SILArgument::asPhi(original)) { | ||
phi->visitTransitiveIncomingPhiOperands([&](auto *phi, auto *operand) { | ||
worklist.pushIfNotVisited(operand->get()); | ||
return true; | ||
}); | ||
} | ||
while (auto value = worklist.pop()) { | ||
for (auto use : value->getUses()) { | ||
switch (use->getOperandOwnership()) { | ||
case OperandOwnership::PointerEscape: | ||
case OperandOwnership::ForwardingUnowned: | ||
return true; | ||
case OperandOwnership::ForwardingConsume: { | ||
auto *branch = dyn_cast<BranchInst>(use->getUser()); | ||
if (!branch) { | ||
// Non-phi forwarding consumes end the lifetime of an owned value. | ||
break; | ||
case OperandOwnership::InteriorPointer: { | ||
if (InteriorPointerOperand(use).findTransitiveUses() != | ||
AddressUseKind::NonEscaping) { | ||
return true; | ||
} | ||
auto *phi = branch->getDestBB()->getArgument(use->getOperandNumber()); | ||
worklist.pushIfNotVisited(phi); | ||
break; | ||
} | ||
default: | ||
|
@@ -2312,27 +2300,27 @@ bool swift::isRedundantMoveValue(MoveValueInst *mvi) { | |
// | ||
// Check this in two ways, one cheaper than the other. | ||
|
||
// First, avoid calling hasPointerEscape(original). | ||
// First, avoid calling findPointerEscape(original). | ||
// | ||
// If the original value is not a phi (a phi's incoming values might have | ||
// escaping uses) and its only user is the move, then it doesn't escape. Also | ||
// if its only user is the move, then its only _consuming_ user is the move. | ||
auto *singleUser = | ||
original->getSingleUse() ? original->getSingleUse()->getUser() : nullptr; | ||
if (mvi == singleUser && !SILArgument::asPhi(original)) { | ||
assert(!hasPointerEscape(original)); | ||
assert(!findPointerEscape(original)); | ||
assert(original->getSingleConsumingUse()->getUser() == mvi); | ||
// - !escaping(original) | ||
// - singleConsumingUser(original) == move | ||
return true; | ||
} | ||
|
||
// Second, call hasPointerEscape(original). | ||
// Second, call findPointerEscape(original). | ||
// | ||
// Explicitly check both | ||
// - !escaping(original) | ||
// - singleConsumingUser(original) == move | ||
auto originalHasEscape = hasPointerEscape(original); | ||
auto originalHasEscape = findPointerEscape(original); | ||
auto *singleConsumingUser = original->getSingleConsumingUse() | ||
? original->getSingleConsumingUse()->getUser() | ||
: nullptr; | ||
|
@@ -2341,6 +2329,6 @@ bool swift::isRedundantMoveValue(MoveValueInst *mvi) { | |
} | ||
|
||
// (3) Escaping matches? (Expensive check, saved for last.) | ||
auto moveHasEscape = hasPointerEscape(mvi); | ||
auto moveHasEscape = findPointerEscape(mvi); | ||
return moveHasEscape == originalHasEscape; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the TODO comment now, but shouldn't we still report a dead projection as either a PointerEscape or Unknown? Otherwise, clients will need to separately look for and handle any possible dead projections. I don't think clients can currently handle that. Dead projections are unexpected but still legal SIL.