Skip to content

Commit 22f6163

Browse files
Make metadata decoding use AllocDecodingState/Session.
1 parent 24dfcbe commit 22f6163

File tree

3 files changed

+19
-35
lines changed

3 files changed

+19
-35
lines changed

src/librustc_metadata/creader.rs

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX};
1919
use rustc::hir::svh::Svh;
2020
use rustc::middle::allocator::AllocatorKind;
2121
use rustc::middle::cstore::DepKind;
22+
use rustc::mir::interpret::AllocDecodingState;
2223
use rustc::session::{Session, CrateDisambiguator};
2324
use rustc::session::config::{Sanitizer, self};
2425
use rustc_target::spec::{PanicStrategy, TargetTriple};
@@ -222,6 +223,9 @@ impl<'a> CrateLoader<'a> {
222223
crate_root.def_path_table.decode((&metadata, self.sess))
223224
});
224225

226+
let interpret_alloc_index: Vec<u32> = crate_root.interpret_alloc_index
227+
.decode(&metadata)
228+
.collect();
225229
let trait_impls = crate_root
226230
.impls
227231
.decode((&metadata, self.sess))
@@ -242,6 +246,7 @@ impl<'a> CrateLoader<'a> {
242246
cnum,
243247
dependencies: Lock::new(dependencies),
244248
codemap_import_info: RwLock::new(vec![]),
249+
alloc_decoding_state: AllocDecodingState::new(interpret_alloc_index),
245250
dep_kind: Lock::new(dep_kind),
246251
source: cstore::CrateSource {
247252
dylib,

src/librustc_metadata/cstore.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
// crates and libraries
1313

1414
use schema;
15-
1615
use rustc::hir::def_id::{CrateNum, DefIndex};
1716
use rustc::hir::map::definitions::DefPathTable;
1817
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
18+
use rustc::mir::interpret::AllocDecodingState;
1919
use rustc_data_structures::indexed_vec::IndexVec;
2020
use rustc::util::nodemap::{FxHashMap, NodeMap};
2121

@@ -66,6 +66,9 @@ pub struct CrateMetadata {
6666
pub dependencies: Lock<Vec<CrateNum>>,
6767
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>,
6868

69+
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
70+
pub alloc_decoding_state: AllocDecodingState,
71+
6972
pub root: schema::CrateRoot,
7073

7174
/// For each public item in this crate, we encode a key. When the

src/librustc_metadata/decoder.rs

+10-34
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ use rustc::hir::def_id::{CrateNum, DefId, DefIndex,
2525
use rustc::ich::Fingerprint;
2626
use rustc::middle::lang_items;
2727
use rustc::mir::{self, interpret};
28+
use rustc::mir::interpret::AllocDecodingSession;
2829
use rustc::session::Session;
2930
use rustc::ty::{self, Ty, TyCtxt};
3031
use rustc::ty::codec::TyDecoder;
3132
use rustc::mir::Mir;
3233
use rustc::util::captures::Captures;
33-
use rustc::util::nodemap::FxHashMap;
3434

3535
use std::io;
3636
use std::mem;
@@ -55,11 +55,8 @@ pub struct DecodeContext<'a, 'tcx: 'a> {
5555

5656
lazy_state: LazyState,
5757

58-
// interpreter allocation cache
59-
interpret_alloc_cache: FxHashMap<usize, interpret::AllocId>,
60-
61-
// Read from the LazySeq CrateRoot::inpterpret_alloc_index on demand
62-
interpret_alloc_index: Option<Vec<u32>>,
58+
// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
59+
alloc_decoding_session: Option<AllocDecodingSession<'a>>,
6360
}
6461

6562
/// Abstract over the various ways one can create metadata decoders.
@@ -78,8 +75,9 @@ pub trait Metadata<'a, 'tcx>: Copy {
7875
tcx,
7976
last_filemap_index: 0,
8077
lazy_state: LazyState::NoNode,
81-
interpret_alloc_cache: FxHashMap::default(),
82-
interpret_alloc_index: None,
78+
alloc_decoding_session: self.cdata().map(|cdata| {
79+
cdata.alloc_decoding_state.new_decoding_session()
80+
}),
8381
}
8482
}
8583
}
@@ -178,17 +176,6 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
178176
self.lazy_state = LazyState::Previous(position + min_size);
179177
Ok(position)
180178
}
181-
182-
fn interpret_alloc(&mut self, idx: usize) -> usize {
183-
if let Some(index) = self.interpret_alloc_index.as_mut() {
184-
return index[idx] as usize;
185-
}
186-
let cdata = self.cdata();
187-
let index: Vec<u32> = cdata.root.interpret_alloc_index.decode(cdata).collect();
188-
let pos = index[idx];
189-
self.interpret_alloc_index = Some(index);
190-
pos as usize
191-
}
192179
}
193180

194181
impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
@@ -299,22 +286,11 @@ impl<'a, 'tcx> SpecializedDecoder<LocalDefId> for DecodeContext<'a, 'tcx> {
299286

300287
impl<'a, 'tcx> SpecializedDecoder<interpret::AllocId> for DecodeContext<'a, 'tcx> {
301288
fn specialized_decode(&mut self) -> Result<interpret::AllocId, Self::Error> {
302-
let tcx = self.tcx.unwrap();
303-
let idx = usize::decode(self)?;
304-
305-
if let Some(cached) = self.interpret_alloc_cache.get(&idx).cloned() {
306-
return Ok(cached);
289+
if let Some(alloc_decoding_session) = self.alloc_decoding_session {
290+
alloc_decoding_session.decode_alloc_id(self)
291+
} else {
292+
bug!("Attempting to decode interpret::AllocId without CrateMetadata")
307293
}
308-
let pos = self.interpret_alloc(idx);
309-
self.with_position(pos, |this| {
310-
interpret::specialized_decode_alloc_id(
311-
this,
312-
tcx,
313-
|this, alloc_id| {
314-
assert!(this.interpret_alloc_cache.insert(idx, alloc_id).is_none());
315-
},
316-
)
317-
})
318294
}
319295
}
320296

0 commit comments

Comments
 (0)