Skip to content

[DCE] Keep outer borrows alive when reborrowing. #39939

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

Conversation

nate-chandler
Copy link
Contributor

Previously, when encountering a borrow of a guaranteed value, the end_borrows of that reborrow were marked alive. Only doing that enables end_borrows of the outer borrow scope can be marked as dead. The result is that uses of the reborrowed value (including its end_borrow) can outstrip the outer borrow scope, which is illegal.

Here, the outer borrow scope's end_borrows are marked alive. To do that, the originally borrowed values have to be identified via findGuaranteedReferenceRoots.

The new utility looks through ownership forwarding instructions to find
the original values with guaranteed ownership.
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler requested a review from atrick October 27, 2021 19:10
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - eebe1d7dcfc637b08fa4b24c494f53553956a5d7

Previously, when encountering a borrow of a guaranteed value, the
end_borrows of that reborrow were marked alive.  Only doing that enables
end_borrows of the outer borrow scope can be marked as dead.  The result
is that uses of the reborrowed value (including its end_borrow) can
outstrip the outer borrow scope, which is illegal.

Here, the outer borrow scope's end_borrows are marked alive.  To do
that, the originally borrowed values have to be identified via
findGuaranteedReferenceRoots.
@nate-chandler nate-chandler force-pushed the dce/keep-outer-borrow-scope-alive-when-reborrowing branch from eebe1d7 to 0a18c62 Compare October 27, 2021 20:55
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@@ -204,6 +204,11 @@ SILValue findReferenceRoot(SILValue ref);
/// Find the first owned root of the reference.
SILValue findOwnershipReferenceRoot(SILValue ref);

/// Look through all ownership forwarding instructions to find the values which
/// were originally borrowed.
void findGuaranteedReferenceRoots(SILValue value,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This already exists, why are you inventing a new form of this? I don't know why we have the one on line 205 as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do different things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nate-chandler you want

bool getAllBorrowIntroducingValues(SILValue value,
SmallVectorImpl &out);

that's in OwnershipUtils.h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know of that function. It looks like the behavior is slightly different, but I'll see if it does what is needed here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nate and I spoke about this. I have some stuff reliant on this. He is going to in a subsequent commit look into eliminating this and using the already written API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, I am going to determine whether these should be combined or not.

@nate-chandler nate-chandler merged commit 0336167 into swiftlang:main Oct 28, 2021
@nate-chandler nate-chandler deleted the dce/keep-outer-borrow-scope-alive-when-reborrowing branch October 28, 2021 22:31
@atrick
Copy link
Contributor

atrick commented Oct 29, 2021

@nate-chandler this looks good. As we discussed, the logic for "find borrow introducers" should be centralized in SIL/Utils/OwnershipUtils because it models a structural requirement of SIL independent of optimization. It needs to be complete and verified. So we also need to call it from the SILVerifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants