-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Remove slow HashSet during miri stack frame creation #49274
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
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9839e5f
Remove slow HashSet during miri stack frame creation
oli-obk 9fa14e4
Skip checking for Storage* statements in constants/statics
oli-obk bf8e4f2
Vec<_> -> IndexVec<Local, _>
oli-obk b18b776
Replace uses of `Hash(Map|Set)` with `FxHash(Map|Set)` in miri
oli-obk 4ea4dd2
Don't allocate a local array at all if there are no locals
oli-obk f9019ae
Simplify local accessors
oli-obk 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
use std::collections::HashSet; | ||
use std::fmt::Write; | ||
|
||
use rustc::hir::def_id::DefId; | ||
use rustc::hir::def::Def; | ||
use rustc::hir::map::definitions::DefPathData; | ||
use rustc::middle::const_val::{ConstVal, ErrKind}; | ||
use rustc::mir; | ||
use rustc::ty::layout::{self, Size, Align, HasDataLayout, LayoutOf, TyLayout}; | ||
use rustc::ty::subst::{Subst, Substs}; | ||
use rustc::ty::{self, Ty, TyCtxt}; | ||
use rustc::ty::maps::TyCtxtAt; | ||
use rustc_data_structures::indexed_vec::Idx; | ||
use rustc_data_structures::indexed_vec::{IndexVec, Idx}; | ||
use rustc::middle::const_val::FrameInfo; | ||
use syntax::codemap::{self, Span}; | ||
use syntax::ast::Mutability; | ||
|
@@ -71,12 +71,12 @@ pub struct Frame<'mir, 'tcx: 'mir> { | |
pub return_place: Place, | ||
|
||
/// The list of locals for this stack frame, stored in order as | ||
/// `[arguments..., variables..., temporaries...]`. The locals are stored as `Option<Value>`s. | ||
/// `[return_ptr, arguments..., variables..., temporaries...]`. The locals are stored as `Option<Value>`s. | ||
/// `None` represents a local that is currently dead, while a live local | ||
/// can either directly contain `PrimVal` or refer to some part of an `Allocation`. | ||
/// | ||
/// Before being initialized, arguments are `Value::ByVal(PrimVal::Undef)` and other locals are `None`. | ||
pub locals: Vec<Option<Value>>, | ||
pub locals: IndexVec<mir::Local, Option<Value>>, | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// Current position within the function | ||
|
@@ -383,39 +383,29 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
) -> EvalResult<'tcx> { | ||
::log_settings::settings().indentation += 1; | ||
|
||
/// Return the set of locals that have a storage annotation anywhere | ||
fn collect_storage_annotations<'mir, 'tcx>(mir: &'mir mir::Mir<'tcx>) -> HashSet<mir::Local> { | ||
use rustc::mir::StatementKind::*; | ||
|
||
let mut set = HashSet::new(); | ||
for block in mir.basic_blocks() { | ||
for stmt in block.statements.iter() { | ||
match stmt.kind { | ||
StorageLive(local) | | ||
StorageDead(local) => { | ||
set.insert(local); | ||
let locals = if mir.local_decls.len() > 1 { | ||
let mut locals = IndexVec::from_elem(Some(Value::ByVal(PrimVal::Undef)), &mir.local_decls); | ||
match self.tcx.describe_def(instance.def_id()) { | ||
// statics and constants don't have `Storage*` statements, no need to look for them | ||
Some(Def::Static(..)) | Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => {}, | ||
_ => { | ||
trace!("push_stack_frame: {:?}: num_bbs: {}", span, mir.basic_blocks().len()); | ||
for block in mir.basic_blocks() { | ||
for stmt in block.statements.iter() { | ||
use rustc::mir::StatementKind::{StorageDead, StorageLive}; | ||
match stmt.kind { | ||
StorageLive(local) | | ||
StorageDead(local) => locals[local] = None, | ||
_ => {} | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
set | ||
} | ||
|
||
// Subtract 1 because `local_decls` includes the ReturnMemoryPointer, but we don't store a local | ||
// `Value` for that. | ||
let num_locals = mir.local_decls.len() - 1; | ||
|
||
let locals = { | ||
let annotated_locals = collect_storage_annotations(mir); | ||
let mut locals = vec![None; num_locals]; | ||
for i in 0..num_locals { | ||
let local = mir::Local::new(i + 1); | ||
if !annotated_locals.contains(&local) { | ||
locals[i] = Some(Value::ByVal(PrimVal::Undef)); | ||
} | ||
}, | ||
} | ||
locals | ||
} else { | ||
// don't allocate at all for trivial constants | ||
IndexVec::new() | ||
}; | ||
|
||
self.stack.push(Frame { | ||
|
@@ -973,8 +963,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
pub fn force_allocation(&mut self, place: Place) -> EvalResult<'tcx, Place> { | ||
let new_place = match place { | ||
Place::Local { frame, local } => { | ||
// -1 since we don't store the return value | ||
match self.stack[frame].locals[local.index() - 1] { | ||
match self.stack[frame].locals[local] { | ||
None => return err!(DeadLocal), | ||
Some(Value::ByRef(ptr, align)) => { | ||
Place::Ptr { | ||
|
@@ -988,7 +977,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
let ty = self.monomorphize(ty, self.stack[frame].instance.substs); | ||
let layout = self.layout_of(ty)?; | ||
let ptr = self.alloc_ptr(ty)?; | ||
self.stack[frame].locals[local.index() - 1] = | ||
self.stack[frame].locals[local] = | ||
Some(Value::ByRef(ptr.into(), layout.align)); // it stays live | ||
let place = Place::from_ptr(ptr, layout.align); | ||
self.write_value(ValTy { value: val, ty }, place)?; | ||
|
@@ -1702,13 +1691,11 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M | |
|
||
impl<'mir, 'tcx> Frame<'mir, 'tcx> { | ||
pub fn get_local(&self, local: mir::Local) -> EvalResult<'tcx, Value> { | ||
// Subtract 1 because we don't store a value for the ReturnPointer, the local with index 0. | ||
self.locals[local.index() - 1].ok_or(EvalErrorKind::DeadLocal.into()) | ||
self.locals[local].ok_or(EvalErrorKind::DeadLocal.into()) | ||
} | ||
|
||
fn set_local(&mut self, local: mir::Local, value: Value) -> EvalResult<'tcx> { | ||
// Subtract 1 because we don't store a value for the ReturnPointer, the local with index 0. | ||
match self.locals[local.index() - 1] { | ||
match self.locals[local] { | ||
None => err!(DeadLocal), | ||
Some(ref mut local) => { | ||
*local = value; | ||
|
@@ -1720,17 +1707,17 @@ impl<'mir, 'tcx> Frame<'mir, 'tcx> { | |
pub fn storage_live(&mut self, local: mir::Local) -> EvalResult<'tcx, Option<Value>> { | ||
trace!("{:?} is now live", local); | ||
|
||
let old = self.locals[local.index() - 1]; | ||
self.locals[local.index() - 1] = Some(Value::ByVal(PrimVal::Undef)); // StorageLive *always* kills the value that's currently stored | ||
let old = self.locals[local]; | ||
self.locals[local] = Some(Value::ByVal(PrimVal::Undef)); // StorageLive *always* kills the value that's currently stored | ||
return Ok(old); | ||
} | ||
|
||
/// Returns the old value of the local | ||
pub fn storage_dead(&mut self, local: mir::Local) -> EvalResult<'tcx, Option<Value>> { | ||
trace!("{:?} is now dead", local); | ||
|
||
let old = self.locals[local.index() - 1]; | ||
self.locals[local.index() - 1] = None; | ||
let old = self.locals[local]; | ||
self.locals[local] = None; | ||
return Ok(old); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just |
||
} | ||
} |
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.
Would be a good idea to check for
Hash{Map,Set}
used by miri code: if it needs to be a hash map/set, it should beFxHash{Map,Set}
instead.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.
Done, found a few in
interpret::memory