-
Notifications
You must be signed in to change notification settings - Fork 13.5k
optimize redundant borrows and escaping paths in NLL #53177
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
bors
merged 9 commits into
rust-lang:master
from
nikomatsakis:nll-redundant-borrows-and-escaping-values
Aug 10, 2018
+388
−202
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb1702f
compute liveness later
nikomatsakis db7a07c
update comment to more accurately describe the limitations
nikomatsakis 5147d38
promote liveness to a directory module
nikomatsakis d1ce8e8
move `liveness_map` into the `liveness` module
nikomatsakis a92bf8d
liveness_map: rustfmt
nikomatsakis 887296e
make it possible to customize the `RegionGraph` direction
nikomatsakis 3b7989d
avoid computing liveness when a variable doesn't need it
nikomatsakis 1bae4f5
optimize redundant borrows
nikomatsakis ff7f6d5
don't walk MIR if no local variables need liveness
nikomatsakis 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
src/librustc_mir/borrow_check/nll/type_check/liveness/liveness_map.rs
This file contains hidden or 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,97 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! For the NLL computation, we need to compute liveness, but only for those | ||
//! local variables whose types contain regions. The others are not of interest | ||
//! to us. This file defines a new index type (LocalWithRegion) that indexes into | ||
//! a list of "variables whose type contain regions". It also defines a map from | ||
//! Local to LocalWithRegion and vice versa -- this map can be given to the | ||
//! liveness code so that it only operates over variables with regions in their | ||
//! types, instead of all variables. | ||
use borrow_check::nll::ToRegionVid; | ||
use rustc::mir::{Local, Mir}; | ||
use rustc::ty::{RegionVid, TyCtxt}; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_data_structures::indexed_vec::{Idx, IndexVec}; | ||
use util::liveness::LiveVariableMap; | ||
|
||
/// Map between Local and LocalWithRegion indices: the purpose of this | ||
/// map is to define the subset of local variables for which we need | ||
/// to do a liveness computation. We only need to compute whether a | ||
/// variable `X` is live if that variable contains some region `R` in | ||
/// its type where `R` is not known to outlive a free region (i.e., | ||
/// where `R` may be valid for just a subset of the fn body). | ||
crate struct NllLivenessMap { | ||
/// For each local variable, contains `Some(i)` if liveness is | ||
/// needed for this variable. | ||
pub from_local: IndexVec<Local, Option<LocalWithRegion>>, | ||
|
||
/// For each `LocalWithRegion`, maps back to the original `Local` index. | ||
pub to_local: IndexVec<LocalWithRegion, Local>, | ||
} | ||
|
||
impl LiveVariableMap for NllLivenessMap { | ||
fn from_local(&self, local: Local) -> Option<Self::LiveVar> { | ||
self.from_local[local] | ||
} | ||
|
||
type LiveVar = LocalWithRegion; | ||
|
||
fn from_live_var(&self, local: Self::LiveVar) -> Local { | ||
self.to_local[local] | ||
} | ||
|
||
fn num_variables(&self) -> usize { | ||
self.to_local.len() | ||
} | ||
} | ||
|
||
impl NllLivenessMap { | ||
crate fn compute( | ||
tcx: TyCtxt<'_, '_, 'tcx>, | ||
free_regions: &FxHashSet<RegionVid>, | ||
mir: &Mir<'tcx>, | ||
) -> Self { | ||
let mut to_local = IndexVec::default(); | ||
let from_local: IndexVec<Local, Option<_>> = mir.local_decls | ||
.iter_enumerated() | ||
.map(|(local, local_decl)| { | ||
if tcx.all_free_regions_meet(&local_decl.ty, |r| { | ||
free_regions.contains(&r.to_region_vid()) | ||
}) { | ||
// If all the regions in the type are free regions | ||
// (or there are no regions), then we don't need | ||
// to track liveness for this variable. | ||
None | ||
} else { | ||
Some(to_local.push(local)) | ||
} | ||
}) | ||
.collect(); | ||
|
||
debug!("{} total variables", mir.local_decls.len()); | ||
debug!("{} variables need liveness", to_local.len()); | ||
debug!("{} regions outlive free regions", free_regions.len()); | ||
|
||
Self { | ||
from_local, | ||
to_local, | ||
} | ||
} | ||
|
||
/// True if there are no local variables that need liveness computation. | ||
crate fn is_empty(&self) -> bool { | ||
self.to_local.is_empty() | ||
} | ||
} | ||
|
||
/// Index given to each local variable whose type contains a region. | ||
newtype_index!(LocalWithRegion); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
34 changes: 14 additions & 20 deletions
34
src/test/ui/borrowck/promote-ref-mut-in-let-issue-46557.nll.stderr
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
its sort of a shame that we lose the "three-point error message" as a consequence of this PR, at least for this example.
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.
Yeah, I think we can fix this by being a bit smarter. What happens is that our current logic around "three points" is based on the liveness computation -- but here we never find a region that has any liveness bits set, and hence we never go searching for a later use. But we do know that the final value that is returned is linked to this borrow region somehow (and we have the chain), so we ought to be able to identify the scenario and find something in the return to highlight. But I didn't write the code for it. Maybe we should file a follow-up issue?