-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Clean up logic around live locals in generator analysis #71956
Merged
bors
merged 9 commits into
rust-lang:master
from
ecstatic-morse:remove-requires-storage-analysis
May 22, 2020
+254
−372
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
daea09c
Add `MaybeInitializedLocals` dataflow analysis
ecstatic-morse fc964c5
Clean up generator live locals analysis
ecstatic-morse 3508592
Update tests with new generator sizes
ecstatic-morse 157631b
Remove `MaybeRequiresStorage`
ecstatic-morse 90da274
Add comment explaining the extra `record_conflicts`
ecstatic-morse d8e0807
Add comment for strange conditional
ecstatic-morse def207e
Look for storage conflicts before terminator effect
ecstatic-morse dd49c6f
Document assumptions made in generator transform for analyses
ecstatic-morse 3ff9317
Document why we don't look at storage liveness
ecstatic-morse 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 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 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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//! A less precise version of `MaybeInitializedPlaces` whose domain is entire locals. | ||
//! | ||
//! A local will be maybe initialized if *any* projections of that local might be initialized. | ||
|
||
use crate::dataflow::{self, BottomValue, GenKill}; | ||
|
||
use rustc_index::bit_set::BitSet; | ||
use rustc_middle::mir::visit::{PlaceContext, Visitor}; | ||
use rustc_middle::mir::{self, BasicBlock, Local, Location}; | ||
|
||
pub struct MaybeInitializedLocals; | ||
|
||
impl BottomValue for MaybeInitializedLocals { | ||
/// bottom = uninit | ||
const BOTTOM_VALUE: bool = false; | ||
} | ||
|
||
impl dataflow::AnalysisDomain<'tcx> for MaybeInitializedLocals { | ||
type Idx = Local; | ||
|
||
const NAME: &'static str = "maybe_init_locals"; | ||
|
||
fn bits_per_block(&self, body: &mir::Body<'tcx>) -> usize { | ||
body.local_decls.len() | ||
} | ||
|
||
fn initialize_start_block(&self, body: &mir::Body<'tcx>, entry_set: &mut BitSet<Self::Idx>) { | ||
// Function arguments are initialized to begin with. | ||
for arg in body.args_iter() { | ||
entry_set.insert(arg); | ||
} | ||
} | ||
} | ||
|
||
impl dataflow::GenKillAnalysis<'tcx> for MaybeInitializedLocals { | ||
// The generator transform relies on the fact that this analysis does **not** use "before" | ||
// effects. | ||
|
||
fn statement_effect( | ||
&self, | ||
trans: &mut impl GenKill<Self::Idx>, | ||
statement: &mir::Statement<'tcx>, | ||
loc: Location, | ||
) { | ||
TransferFunction { trans }.visit_statement(statement, loc) | ||
} | ||
|
||
fn terminator_effect( | ||
&self, | ||
trans: &mut impl GenKill<Self::Idx>, | ||
terminator: &mir::Terminator<'tcx>, | ||
loc: Location, | ||
) { | ||
TransferFunction { trans }.visit_terminator(terminator, loc) | ||
} | ||
|
||
fn call_return_effect( | ||
&self, | ||
trans: &mut impl GenKill<Self::Idx>, | ||
_block: BasicBlock, | ||
_func: &mir::Operand<'tcx>, | ||
_args: &[mir::Operand<'tcx>], | ||
return_place: mir::Place<'tcx>, | ||
) { | ||
trans.gen(return_place.local) | ||
} | ||
|
||
/// See `Analysis::apply_yield_resume_effect`. | ||
fn yield_resume_effect( | ||
&self, | ||
trans: &mut impl GenKill<Self::Idx>, | ||
_resume_block: BasicBlock, | ||
resume_place: mir::Place<'tcx>, | ||
) { | ||
trans.gen(resume_place.local) | ||
} | ||
} | ||
|
||
struct TransferFunction<'a, T> { | ||
trans: &'a mut T, | ||
} | ||
|
||
impl<T> Visitor<'tcx> for TransferFunction<'a, T> | ||
where | ||
T: GenKill<Local>, | ||
{ | ||
fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) { | ||
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, NonUseContext}; | ||
match context { | ||
// These are handled specially in `call_return_effect` and `yield_resume_effect`. | ||
PlaceContext::MutatingUse(MutatingUseContext::Call | MutatingUseContext::Yield) => {} | ||
|
||
// Otherwise, when a place is mutated, we must consider it possibly initialized. | ||
PlaceContext::MutatingUse(_) => self.trans.gen(local), | ||
|
||
// If the local is moved out of, or if it gets marked `StorageDead`, consider it no | ||
// longer initialized. | ||
PlaceContext::NonUse(NonUseContext::StorageDead) | ||
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) => self.trans.kill(local), | ||
|
||
// All other uses do not affect this analysis. | ||
PlaceContext::NonUse( | ||
NonUseContext::StorageLive | ||
| NonUseContext::AscribeUserTy | ||
| NonUseContext::VarDebugInfo, | ||
) | ||
| PlaceContext::NonMutatingUse( | ||
NonMutatingUseContext::Inspect | ||
| NonMutatingUseContext::Copy | ||
| NonMutatingUseContext::SharedBorrow | ||
| NonMutatingUseContext::ShallowBorrow | ||
| NonMutatingUseContext::UniqueBorrow | ||
| NonMutatingUseContext::AddressOf | ||
| NonMutatingUseContext::Projection, | ||
) => {} | ||
} | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.
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.
So, this is now ignoring borrowed variables here, where the old analysis was not. With the equations you wrote down in the generator transform, I wouldn't think that this causes the size difference – in fact, I'd expect this to only affect movable generators, but it seems that that's incorrect?
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.
To clarify, you're saying that the version on stable is marking locals as needing to be saved across yield points if they are borrowed even in movable generators? I thought this was an oversight based on the comment next to
if !movable {
in the old code. Do we always need to worry about borrows across yield points?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.
It's definitely fine to ignore them in movable generators as that will cause rather acute UB when the generator is in fact moved around in memory.
I'm just wondering why the previous analysis cared about borrows if they don't impact immovable generators anyways, which is what we wanted to hold off on. And I also was trying to understand why the resulting generators are smaller.
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 previous analysis cared about them because it's possible for something to require storage even if it's been de-initialized. The address could have been captured from a borrow and unsafe code could use that to re-initialize the value.
By the definition of this MaybeInitializedLocals analysis, we shouldn't care about it, but we should still assume in the generator transform that anything borrowed and MaybeStorageLive might still require storage, even if it's not initialized.