Make metadata decoding use AllocDecodingState/Session.

This commit is contained in:
Michael Woerister 2018-05-29 10:26:26 +02:00
parent 24dfcbef9c
commit 22f6163db4
3 changed files with 19 additions and 35 deletions

View File

@ -19,6 +19,7 @@
use rustc::hir::svh::Svh;
use rustc::middle::allocator::AllocatorKind;
use rustc::middle::cstore::DepKind;
use rustc::mir::interpret::AllocDecodingState;
use rustc::session::{Session, CrateDisambiguator};
use rustc::session::config::{Sanitizer, self};
use rustc_target::spec::{PanicStrategy, TargetTriple};
@ -222,6 +223,9 @@ fn register_crate(&mut self,
crate_root.def_path_table.decode((&metadata, self.sess))
});
let interpret_alloc_index: Vec<u32> = crate_root.interpret_alloc_index
.decode(&metadata)
.collect();
let trait_impls = crate_root
.impls
.decode((&metadata, self.sess))
@ -242,6 +246,7 @@ fn register_crate(&mut self,
cnum,
dependencies: Lock::new(dependencies),
codemap_import_info: RwLock::new(vec![]),
alloc_decoding_state: AllocDecodingState::new(interpret_alloc_index),
dep_kind: Lock::new(dep_kind),
source: cstore::CrateSource {
dylib,

View File

@ -12,10 +12,10 @@
// crates and libraries
use schema;
use rustc::hir::def_id::{CrateNum, DefIndex};
use rustc::hir::map::definitions::DefPathTable;
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
use rustc::mir::interpret::AllocDecodingState;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc::util::nodemap::{FxHashMap, NodeMap};
@ -66,6 +66,9 @@ pub struct CrateMetadata {
pub dependencies: Lock<Vec<CrateNum>>,
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>,
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
pub alloc_decoding_state: AllocDecodingState,
pub root: schema::CrateRoot,
/// For each public item in this crate, we encode a key. When the

View File

@ -25,12 +25,12 @@
use rustc::ich::Fingerprint;
use rustc::middle::lang_items;
use rustc::mir::{self, interpret};
use rustc::mir::interpret::AllocDecodingSession;
use rustc::session::Session;
use rustc::ty::{self, Ty, TyCtxt};
use rustc::ty::codec::TyDecoder;
use rustc::mir::Mir;
use rustc::util::captures::Captures;
use rustc::util::nodemap::FxHashMap;
use std::io;
use std::mem;
@ -55,11 +55,8 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
lazy_state: LazyState,
// interpreter allocation cache
interpret_alloc_cache: FxHashMap<usize, interpret::AllocId>,
// Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand
interpret_alloc_index: Option<Vec<u32>>,
// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
alloc_decoding_session: Option<AllocDecodingSession<'a>>,
}
/// Abstract over the various ways one can create metadata decoders.
@ -78,8 +75,9 @@ fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
tcx,
last_filemap_index: 0,
lazy_state: LazyState::NoNode,
interpret_alloc_cache: FxHashMap::default(),
interpret_alloc_index: None,
alloc_decoding_session: self.cdata().map(|cdata| {
cdata.alloc_decoding_state.new_decoding_session()
}),
}
}
}
@ -178,17 +176,6 @@ fn read_lazy_distance(&mut self, min_size: usize) -> Result<usize, <Self as Deco
self.lazy_state = LazyState::Previous(position + min_size);
Ok(position)
}
fn interpret_alloc(&mut self, idx: usize) -> usize {
if let Some(index) = self.interpret_alloc_index.as_mut() {
return index[idx] as usize;
}
let cdata = self.cdata();
let index: Vec<u32> = cdata.root.interpret_alloc_index.decode(cdata).collect();
let pos = index[idx];
self.interpret_alloc_index = Some(index);
pos as usize
}
}
impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
@ -299,22 +286,11 @@ fn specialized_decode(&mut self) -> Result<LocalDefId, Self::Error> {
impl<'a, 'tcx> SpecializedDecoder<interpret::AllocId> for DecodeContext<'a, 'tcx> {
fn specialized_decode(&mut self) -> Result<interpret::AllocId, Self::Error> {
let tcx = self.tcx.unwrap();
let idx = usize::decode(self)?;
if let Some(cached) = self.interpret_alloc_cache.get(&idx).cloned() {
return Ok(cached);
if let Some(alloc_decoding_session) = self.alloc_decoding_session {
alloc_decoding_session.decode_alloc_id(self)
} else {
bug!("Attempting to decode interpret::AllocId without CrateMetadata")
}
let pos = self.interpret_alloc(idx);
self.with_position(pos, |this| {
interpret::specialized_decode_alloc_id(
this,
tcx,
|this, alloc_id| {
assert!(this.interpret_alloc_cache.insert(idx, alloc_id).is_none());
},
)
})
}
}