-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[OwnershipUtils] Classify moves from limited-use values as redundant. #64448
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
nate-chandler
merged 1 commit into
swiftlang:main
from
nate-chandler:eliminate-more-moves
Mar 20, 2023
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2383,21 +2383,62 @@ bool swift::isNestedLexicalBeginBorrow(BeginBorrowInst *bbi) { | |
} | ||
|
||
bool swift::isRedundantMoveValue(MoveValueInst *mvi) { | ||
// Check whether the moved-from value's lifetime and the moved-to value's | ||
// lifetime have the same (1) ownership, (2) lexicality, and (3) escaping. | ||
// | ||
// Along the way, also check for cases where they have different values for | ||
// those characteristics but it doesn't matter because of how limited the uses | ||
// of the original value are (for now, whether the move is the only use). | ||
|
||
auto original = mvi->getOperand(); | ||
|
||
// If the moved-from value has none ownership, hasPointerEscape can't handle | ||
// it, so it can't be used to determine whether escaping matches. | ||
// (1) Ownership matches? | ||
// (The new value always has owned ownership.) | ||
if (original->getOwnershipKind() != OwnershipKind::Owned) { | ||
return false; | ||
} | ||
|
||
// First, check whether lexicality matches, the cheaper check. | ||
// (2) Lexicality matches? | ||
if (mvi->isLexical() != original->isLexical()) { | ||
return false; | ||
} | ||
|
||
// Then, check whether escaping matches, the more expensive check. | ||
if (hasPointerEscape(mvi) != hasPointerEscape(original)) { | ||
// The move doesn't alter constraints: ownership and lexicality match. | ||
|
||
auto *singleUser = | ||
original->getSingleUse() ? original->getSingleUse()->getUser() : nullptr; | ||
if (mvi == singleUser && !SILArgument::asPhi(original)) { | ||
assert(!hasPointerEscape(original)); | ||
// The moved-from value's only use is the move_value, and the moved-from | ||
// value isn't a phi. So, it doesn't escape. (A phi's incoming values | ||
// might escape.) | ||
// | ||
// Still, no optimization is enabled by separating the two lifetimes. | ||
// The moved-from value's lifetime could not be shrunk regardless of whether | ||
// the moved-to value escapes. | ||
return true; | ||
} | ||
|
||
auto moveHasEscape = hasPointerEscape(mvi); | ||
auto originalHasEscape = hasPointerEscape(original); | ||
|
||
// (3) Escaping matches? (Expensive check, saved for last.) | ||
if (moveHasEscape != originalHasEscape) { | ||
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. Might be more clear:
|
||
if (!originalHasEscape) { | ||
auto *singleConsumingUser = | ||
original->getSingleConsumingUse() | ||
? original->getSingleConsumingUse()->getUser() | ||
: nullptr; | ||
if (mvi == singleConsumingUser) { | ||
// The moved-from value's only consuming use is the move_value and it | ||
// doesn't escape. | ||
// | ||
// Although the moved-to value escapes, no optimization is enabled by | ||
// separating the two lifetimes. The moved-from value's lifetime | ||
// already couldn't be shrunk. | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
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.
The comments are great. But I still somehow missed that that singleUser check is actually part of condition #3, and only a way to short-circuit the complete, but slower, logic below.
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.
Follow-up at #64483 .