Skip to content

Fix Mem2Reg for store [assign] #35805

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 1 commit into from
Feb 8, 2021
Merged

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Feb 6, 2021

This PR simplifies the handling of store [assign] in Mem2Reg by always converting it to a store [init].
Also fixes an edge case where store [assign] in a non entry block was not the first store of an alloc_stack with uses in multiple blocks.

@meg-gupta meg-gupta changed the title [DNM] Fix Mem2Reg for store [assign] Fix Mem2Reg for store [assign] Feb 7, 2021
@meg-gupta meg-gupta marked this pull request as ready for review February 7, 2021 01:04
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta requested a review from gottesmm February 7, 2021 01:05
@@ -459,6 +459,7 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
// RunningVal is the current value in the stack location.
// We don't know the value of the alloca until we find the first store.
SILValue RunningVal = SILValue();
SILValue LastRunningVal = SILValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

You should add a comment here explaining what LastRunningVal is and why it is different than RunningVal.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could also change this to be called PreviousRunningVal because "last" might mean "the last in a list" whereas previous always means "the one before this one".

Copy link
Contributor

@gottesmm gottesmm Feb 7, 2021

Choose a reason for hiding this comment

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

The information that I would want here is something with the following information (feel free to wordsmith).

LastRunningVal is the value that we saw stored in the stack
location /immediately before/ we updated the RunningVal at the
last store. The reason why we track this information on top of
the current running value is that if the last store was an assign
and turns out to be dead, we need to insert a destroy_value on
that value.

assert(RunningVal);
// We have to create a destroy_value of the RunningVal when we see a store
// [assign].
// A RunningVal will be be available if :
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clean up this comment. My thought:

      // We have to create a destroy_value of the RunningVal when we see a store
      // [assign].
      //
      // A RunningVal will be available if:
      //
      // 1. store [assign] was in an entry block.
      // 2. store [assign] is not the first store in a non-entry block

Copy link
Contributor

@zoecarver zoecarver left a comment

Choose a reason for hiding this comment

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

Other than the small comment, this looks good to me. Thanks!

@@ -459,6 +459,7 @@ StackAllocationPromoter::promoteAllocationInBlock(SILBasicBlock *BB) {
// RunningVal is the current value in the stack location.
// We don't know the value of the alloca until we find the first store.
SILValue RunningVal = SILValue();
SILValue LastRunningVal = SILValue();
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also change this to be called PreviousRunningVal because "last" might mean "the last in a list" whereas previous always means "the one before this one".

This PR simplifies the handling of store [assign] in Mem2Reg by always
converting it to a store [init].
Also fixes an edge case where store [assign] in a non entry block was
not the first store of an alloc_stack with uses in multiple blocks.
@meg-gupta
Copy link
Contributor Author

@gottesmm thanks for suggesting transforming store [assign] -> store [init]. store [assign] handling is much simpler now.

@meg-gupta
Copy link
Contributor Author

@swift-ci test

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

Looks beautiful to me! Really happy with where this patch ended up!

@gottesmm
Copy link
Contributor

gottesmm commented Feb 7, 2021

@swift-ci test windows platform

2 similar comments
@meg-gupta
Copy link
Contributor Author

@swift-ci test windows platform

@meg-gupta
Copy link
Contributor Author

@swift-ci test windows platform

@meg-gupta meg-gupta merged commit 30a57fc into swiftlang:main Feb 8, 2021
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