Skip to content

[LifetimeCompletion] Mark lifetimes in dead-end loops. #73859

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

Conversation

nate-chandler
Copy link
Contributor

The new instructions are inserted after every "user" (according to InteriorLiveness' SSAPrunedLiveness instance) outside the linear liveness boundary.

@nate-chandler nate-chandler requested a review from atrick May 23, 2024 20:25
@nate-chandler nate-chandler force-pushed the lifetime-completion/20240523/1 branch 2 times, most recently from f7cc892 to 92eaad0 Compare May 25, 2024 16:45
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler
Copy link
Contributor Author

@swift-ci please test windows platform

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

Please update this commit message:

[SIL] Add end_linear_lifetime instruction.

@@ -38,6 +38,13 @@ struct LinearLivenessVisitor :
linearLiveness(linearLiveness){}

bool handleUsePoint(Operand *use, UseLifetimeConstraint useConstraint) {
if (isa<ExtendLifetimeInst>(use->getUser())) {
Copy link
Contributor

@atrick atrick May 26, 2024

Choose a reason for hiding this comment

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

I'm confused about the handleUsePoint contract.

If extend_liveness should be passed to handleUsePoint as LifetimeEnding, then that should be specified in the function interface comment (similar to the comment in the commit log) since that is an inversion of the expected value. Then we should not need additional checks within the visitor implementations of handleUsePoint.

Preferably, extend_liveness would be passed to handleUsePoint as NonLifetimeEnding to avoid confusion, and the visitor implementations of handleUsePoint will check for extend_liveness if they need to invert the value. In that case, the explanatory comment in the commit message needs to be on each visitor implementation that inverts the sense of extend_lifetime.

Instead, this diff shows extra checks for extend_lifetime on both sides of handleUsePoint and there aren't any comments to explain away the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. Fixed to pass NonLifetimeEnding to handleUsePoint. And actually, is there any reason that we want the extend_lifetime instructions to be added to PrunedLiveness with /*lifetimeEnding=*/true? I haven't thought of one--we still get all users within the LinearLifetime boundary either way--so I removed that special behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

is there any reason that we want the extend_lifetime instructions to be added to PrunedLiveness with /lifetimeEnding=/true? I haven't thought of one--we still get all users within the LinearLifetime boundary either way--so I removed that special behavior.

Oh, wonderful. I just assumed that you needed to invert the lifetimeEnding value in PrunedLiveness to avoid special cases elsewhere. It's always better if the code doesn't need to lie about anything though.

@nate-chandler nate-chandler force-pushed the lifetime-completion/20240523/1 branch 3 times, most recently from 6499dc0 to 9107952 Compare May 30, 2024 22:10
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

@nate-chandler nate-chandler marked this pull request as ready for review May 30, 2024 22:13
@nate-chandler nate-chandler requested a review from eeckstein as a code owner May 30, 2024 22:13
Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

Awesome!

@@ -245,6 +259,53 @@ void State::initializeAllNonConsumingUses(
// Consuming Use Initialization
//===----------------------------------------------------------------------===//

void State::initializeAllExtendLifetimeUses(
ArrayRef<Operand *> linearLifetimeEndingUses,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this parameter be extendLifetimeUses to make things clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Fixed.

@@ -389,6 +392,14 @@ void OSSALifetimeCompletion::visitAvailabilityBoundary(
visitor.propagateAvailablity(result);

visitor.visitAvailabilityBoundary(result, visit);

visitUsersOutsideLinearLivenessBoundary(
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this makes AvailabilityBoundaryVisitor a four step process. Its class-level comment is no longer replete.

Copy link
Contributor Author

@nate-chandler nate-chandler Jun 5, 2024

Choose a reason for hiding this comment

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

Added the new step to the class-level comment. It does feel now (after this PR, not after expanding the comment) like the factoring/naming here is not quite ideal (there's the availability boundary and then there are these infinite loop instructions), but we can tweak that later on.

Copy link
Contributor Author

@nate-chandler nate-chandler Jun 5, 2024

Choose a reason for hiding this comment

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

Clarified the top-level comment to indicate that AvailabilityBoundaryVisitor only does half of OSSALifetimeCompletion::visitAvailabilityBoundary. Also gave the class only a single public member function.

@nate-chandler nate-chandler force-pushed the lifetime-completion/20240523/1 branch 2 times, most recently from c380bab to 12589b6 Compare June 5, 2024 15:54
@nate-chandler
Copy link
Contributor Author

@swift-ci please test

Clarify methods.  Unfortunately, without other changes I haven't
identified, the `enum class` can't be made an `enum` for ideal usage.
It visits the availability boundary, so call it
`visitAvailabilityBoundary`.  It's not clear what "unreachable lifetime
ends" are.
It indicates that the value's lifetime continues to at least this point.
The boundary formed by all consuming uses together with these
instructions will encompass all uses of the value.
When visiting consumes, also visit `extend_lifetime` instructions.
These instructions are not lifetime ending, but together with the
consumes, they enclose the users of a value.

Add a flag to LinearLiveness to control whether these instructions are
added so that the verifier can use verify that all such instructions
appear outside the linear lifetime boundary (not including them).
When visiting an availability boundary, note what kind of end is
involved.  For now, there's only one.
VisitAvailabilityBoundary now has a single public method.
It visits users discovered by an SSAPrunedLiveness instance (such as
that used by InteriorLiveness) which are outside the LinearLifetime
boundary.
The new instructions are inserted after every "user" (according to
InteriorLiveness' SSAPrunedLiveness instance) outside the linear
liveness boundary.
@nate-chandler nate-chandler force-pushed the lifetime-completion/20240523/1 branch from 12589b6 to 84da0ed Compare June 5, 2024 23:28
@nate-chandler
Copy link
Contributor Author

@swift-ci please smoke test

@nate-chandler nate-chandler enabled auto-merge June 5, 2024 23:28
@nate-chandler nate-chandler merged commit 40e8b15 into swiftlang:main Jun 6, 2024
3 checks passed
@nate-chandler nate-chandler deleted the lifetime-completion/20240523/1 branch June 6, 2024 13:54
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.

2 participants