From 22f6163db47dfb1c5c02bcd0543eb0f4f92dd164 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 29 May 2018 10:26:26 +0200 Subject: [PATCH] Make metadata decoding use AllocDecodingState/Session. --- src/librustc_metadata/creader.rs | 5 ++++ src/librustc_metadata/cstore.rs | 5 +++- src/librustc_metadata/decoder.rs | 44 ++++++++------------------------ 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 2467d5cf97c17..e41b3f5f53b25 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -19,6 +19,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX}; 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 @@ impl<'a> CrateLoader<'a> { crate_root.def_path_table.decode((&metadata, self.sess)) }); + let interpret_alloc_index: Vec = crate_root.interpret_alloc_index + .decode(&metadata) + .collect(); let trait_impls = crate_root .impls .decode((&metadata, self.sess)) @@ -242,6 +246,7 @@ impl<'a> CrateLoader<'a> { 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, diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index 763563eabe0e9..2bc5f60748664 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -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>, pub codemap_import_info: RwLock>, + /// 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 diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index 11c653895fce0..69e873bb95d25 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -25,12 +25,12 @@ use rustc::hir::def_id::{CrateNum, DefId, DefIndex, 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, - - // Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand - interpret_alloc_index: Option>, + // Used for decoding interpret::AllocIds in a cached & thread-safe manner. + alloc_decoding_session: Option>, } /// Abstract over the various ways one can create metadata decoders. @@ -78,8 +75,9 @@ pub trait Metadata<'a, 'tcx>: Copy { 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 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { 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 = 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 @@ impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { impl<'a, 'tcx> SpecializedDecoder for DecodeContext<'a, 'tcx> { fn specialized_decode(&mut self) -> Result { - 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()); - }, - ) - }) } }