Skip to content
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

introduce local-scope to prevent StorageLive/StorageDead in statics #42023

Merged
merged 2 commits into from
May 23, 2017

Conversation

nikomatsakis
Copy link
Contributor

In investigating #36799, I found that we were creating storage-live/storage-dead instructions in statics/constants, where they are not needed. This arose due to the fix for local scopes. This PR tries to fix that (and adds a test -- I'm curious if there is a way to make that test more targeted, though).

r? @arielb1

@nikomatsakis
Copy link
Contributor Author

NB: Didn't have time to fully test; let travis run :)

/// will be evaluated at compilation time. This is used to
/// suppress the addition of `StorageLive`/`StorageDead`
/// statements as well as `drop` statements.
building_const: bool,
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't Cx have a way to get at this?

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 see an obvious one, but it could store e.g. the MirSource

/// Returns the scope that we should use as the lifetime of an
/// operand. Basically, an operand must live until it is consumed.
/// This is similar to, but not quite the same as, the temporary
/// scope (which can be larger or smaller).
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong in the general case - why don't we rely on temporary_scope of None?

Copy link
Contributor Author

@nikomatsakis nikomatsakis May 16, 2017

Choose a reason for hiding this comment

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

I was just trying to preserve the existing logic. To be honest, I don't quite understand the reasoning here -- it does seem to me that top-most scope is not necessarily correct. @arielb1 can maybe elaborate more -- it was introduced in arielb1@1425eed as part of the PR to fix #38669, though I didn't find the PR itself (presumably I could trace to find the bors commit...)

Copy link
Contributor

Choose a reason for hiding this comment

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

In an expression like foo(bar(), baz()), we want the operands holding the result of bar() and baz() to be destroyed right after foo is called, and if calling baz panics then bar is destroyed first - at least that is the ordering that makes the most sense to me.

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 be clear, when you say "destroyed" here, you don't mean that we will run any destructors, you just mean that we'll reclaim the storage, right? (Because I would expect the values to have been moved to the callee in any case...)

Copy link
Contributor

Choose a reason for hiding this comment

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

More concretely, if we have foo(Box::new(2), panic!()), because we evaluate arguments from left to right, we allocate the Box and then free it (because foo can also panic, we actually emit a drop flag there - but a "sufficiently smart" drop unswitching pass could eliminate that). We also emit a drop after the call on the "normal" path, but that drop is a no-op because the value is already consumed.

Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to add the above to the comment.

if self.building_const {
None
} else {
Some(self.topmost_scope()) // FIXME(#6393)
Copy link
Contributor

Choose a reason for hiding this comment

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

FIXME(#6393) should be moved to the call-sites - it's about writing the code in one line after we get NLL.

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 don't understand what you mean by "writing the code in one line"

Copy link
Contributor

Choose a reason for hiding this comment

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

self.as_operand(block, self.local_scope(), expr)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see. I don't think I'd even bother with a FIXME then.

@arielb1
Copy link
Contributor

arielb1 commented May 16, 2017

So this is intended to be a performance optimization?

@nikomatsakis
Copy link
Contributor Author

So this is intended to be a performance optimization?

Memory usage, primarily, but it probably affects compilation time more generally, yeah. (If you have large constants)

@arielb1 arielb1 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 16, 2017
@nikomatsakis
Copy link
Contributor Author

@arielb1 @eddyb so I switched this over to use MirSource directly, which also fixed a dumb bug where I was marking all fn as a "static/const".

@arielb1
Copy link
Contributor

arielb1 commented May 18, 2017

r=me, but if you want to improve the comment that's ok too.

@carols10cents
Copy link
Member

Sooo what's the state of this PR? @nikomatsakis are you going to improve the comment in the way @arielb1 suggested, or do you officially r+ @arielb1?

@nikomatsakis
Copy link
Contributor Author

I'll improve the comment. I think it's otherwise r=arielb1, from what I understood.

@nikomatsakis
Copy link
Contributor Author

@bors r=arielb1

@bors
Copy link
Contributor

bors commented May 22, 2017

📌 Commit 8a4e593 has been approved by arielb1

@bors
Copy link
Contributor

bors commented May 23, 2017

⌛ Testing commit 8a4e593 with merge 5b13bff...

bors added a commit that referenced this pull request May 23, 2017
introduce local-scope to prevent `StorageLive`/`StorageDead` in statics

In investigating #36799, I found that we were creating storage-live/storage-dead instructions in statics/constants, where they are not needed. This arose due to the fix for local scopes. This PR tries to fix that (and adds a test -- I'm curious if there is a way to make that test more targeted, though).

r? @arielb1
@bors
Copy link
Contributor

bors commented May 23, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: arielb1
Pushing 5b13bff to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants