Skip to content

Commit

Permalink
Make metadata decoding use AllocDecodingState/Session.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Jun 1, 2018
1 parent 24dfcbe commit 22f6163
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 35 deletions.
5 changes: 5 additions & 0 deletions src/librustc_metadata/creader.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -222,6 +223,9 @@ impl<'a> CrateLoader<'a> {
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))
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_metadata/cstore.rs
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand Down
44 changes: 10 additions & 34 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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()
}),
}
}
}
Expand Down Expand Up @@ -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<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> {
Expand Down Expand Up @@ -299,22 +286,11 @@ impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for DecodeContext<'a, 'tcx> {

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());
},
)
})
}
}

Expand Down

0 comments on commit 22f6163

Please sign in to comment.