Skip to content

Commit 10b52f0

Browse files
committed
Make dep node indices persistent between sessions
1 parent c52cee1 commit 10b52f0

File tree

15 files changed

+422
-337
lines changed

15 files changed

+422
-337
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3802,6 +3802,7 @@ dependencies = [
38023802
"rustc_data_structures",
38033803
"rustc_fs_util",
38043804
"rustc_hir",
3805+
"rustc_query_system",
38053806
"rustc_session",
38063807
"rustc_span",
38073808
"serialize",

src/librustc/dep_graph/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ mod dep_node;
1111

1212
pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
1313
pub use rustc_query_system::dep_graph::{
14-
debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
15-
WorkProduct, WorkProductFileKind, WorkProductId,
14+
debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, WorkProduct, WorkProductFileKind,
15+
WorkProductId,
1616
};
1717

1818
pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
@@ -159,8 +159,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
159159
try_load_from_on_disk_cache(*self, dep_node)
160160
}
161161

162-
fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
163-
self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
162+
fn load_diagnostics(&self, dep_node_index: DepNodeIndex) -> Vec<Diagnostic> {
163+
self.queries.on_disk_cache.load_diagnostics(*self, dep_node_index)
164164
}
165165

166166
fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::dep_graph::SerializedDepNodeIndex;
1+
use crate::dep_graph::DepNodeIndex;
22
use crate::mir;
33
use crate::mir::interpret::{GlobalId, LitToConstInput};
44
use crate::traits;

src/librustc/ty/query/on_disk_cache.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1+
use crate::dep_graph::DepNodeIndex;
22
use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
33
use crate::mir::{self, interpret};
44
use crate::ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
@@ -12,7 +12,7 @@ use rustc_data_structures::thin_vec::ThinVec;
1212
use rustc_errors::Diagnostic;
1313
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
1414
use rustc_hir::definitions::DefPathHash;
15-
use rustc_index::vec::{Idx, IndexVec};
15+
use rustc_index::vec::IndexVec;
1616
use rustc_serialize::{
1717
opaque, Decodable, Decoder, Encodable, Encoder, SpecializedDecoder, SpecializedEncoder,
1818
UseSpecializedDecodable, UseSpecializedEncodable,
@@ -60,11 +60,11 @@ pub struct OnDiskCache<'sess> {
6060

6161
// A map from dep-node to the position of the cached query result in
6262
// `serialized_data`.
63-
query_result_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
63+
query_result_index: FxHashMap<DepNodeIndex, AbsoluteBytePos>,
6464

6565
// A map from dep-node to the position of any associated diagnostics in
6666
// `serialized_data`.
67-
prev_diagnostics_index: FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
67+
prev_diagnostics_index: FxHashMap<DepNodeIndex, AbsoluteBytePos>,
6868

6969
alloc_decoding_state: AllocDecodingState,
7070
}
@@ -80,8 +80,8 @@ struct Footer {
8080
interpret_alloc_index: Vec<u32>,
8181
}
8282

83-
type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
84-
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
83+
type EncodedQueryResultIndex = Vec<(DepNodeIndex, AbsoluteBytePos)>;
84+
type EncodedDiagnosticsIndex = Vec<(DepNodeIndex, AbsoluteBytePos)>;
8585
type EncodedDiagnostics = Vec<Diagnostic>;
8686

8787
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
@@ -224,11 +224,10 @@ impl<'sess> OnDiskCache<'sess> {
224224
.current_diagnostics
225225
.borrow()
226226
.iter()
227-
.map(|(dep_node_index, diagnostics)| {
227+
.map(|(&dep_node_index, diagnostics)| {
228228
let pos = AbsoluteBytePos::new(encoder.position());
229229
// Let's make sure we get the expected type here.
230230
let diagnostics: &EncodedDiagnostics = diagnostics;
231-
let dep_node_index = SerializedDepNodeIndex::new(dep_node_index.index());
232231
encoder.encode_tagged(dep_node_index, diagnostics)?;
233232

234233
Ok((dep_node_index, pos))
@@ -304,7 +303,7 @@ impl<'sess> OnDiskCache<'sess> {
304303
pub fn load_diagnostics(
305304
&self,
306305
tcx: TyCtxt<'_>,
307-
dep_node_index: SerializedDepNodeIndex,
306+
dep_node_index: DepNodeIndex,
308307
) -> Vec<Diagnostic> {
309308
let diagnostics: Option<EncodedDiagnostics> =
310309
self.load_indexed(tcx, dep_node_index, &self.prev_diagnostics_index, "diagnostics");
@@ -328,11 +327,11 @@ impl<'sess> OnDiskCache<'sess> {
328327
}
329328

330329
/// Returns the cached query result if there is something in the cache for
331-
/// the given `SerializedDepNodeIndex`; otherwise returns `None`.
330+
/// the given `DepNodeIndex`; otherwise returns `None`.
332331
pub fn try_load_query_result<T>(
333332
&self,
334333
tcx: TyCtxt<'_>,
335-
dep_node_index: SerializedDepNodeIndex,
334+
dep_node_index: DepNodeIndex,
336335
) -> Option<T>
337336
where
338337
T: Decodable,
@@ -361,8 +360,8 @@ impl<'sess> OnDiskCache<'sess> {
361360
fn load_indexed<'tcx, T>(
362361
&self,
363362
tcx: TyCtxt<'tcx>,
364-
dep_node_index: SerializedDepNodeIndex,
365-
index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
363+
dep_node_index: DepNodeIndex,
364+
index: &FxHashMap<DepNodeIndex, AbsoluteBytePos>,
366365
debug_tag: &'static str,
367366
) -> Option<T>
368367
where
@@ -1009,12 +1008,10 @@ where
10091008
state.iter_results(|results| {
10101009
for (key, value, dep_node) in results {
10111010
if Q::cache_on_disk(tcx, key.clone(), Some(&value)) {
1012-
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
1013-
10141011
// Record position of the cache entry.
10151012
query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));
10161013

1017-
// Encode the type check tables with the `SerializedDepNodeIndex`
1014+
// Encode the type check tables with the `DepNodeIndex`
10181015
// as tag.
10191016
encoder.encode_tagged(dep_node, &value)?;
10201017
}

src/librustc_data_structures/sharded.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ pub struct Sharded<T> {
3030
impl<T: Default> Default for Sharded<T> {
3131
#[inline]
3232
fn default() -> Self {
33-
Self::new(T::default)
33+
Self::new(|_| T::default())
3434
}
3535
}
3636

3737
impl<T> Sharded<T> {
3838
#[inline]
39-
pub fn new(mut value: impl FnMut() -> T) -> Self {
39+
pub fn new(mut value: impl FnMut(usize) -> T) -> Self {
4040
// Create a vector of the values we want
4141
let mut values: SmallVec<[_; SHARDS]> =
42-
(0..SHARDS).map(|_| CacheAligned(Lock::new(value()))).collect();
42+
(0..SHARDS).map(|i| CacheAligned(Lock::new(value(i)))).collect();
4343

4444
// Create an uninitialized array
4545
let mut shards: mem::MaybeUninit<[CacheAligned<Lock<T>>; SHARDS]> =
@@ -63,6 +63,15 @@ impl<T> Sharded<T> {
6363
if SHARDS == 1 { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
6464
}
6565

66+
/// The shard is selected by hashing `val` with `FxHasher`.
67+
pub fn get_shard_by_value_mut<K: Hash + ?Sized>(&mut self, val: &K) -> &mut T {
68+
if SHARDS == 1 {
69+
self.shards[0].0.get_mut()
70+
} else {
71+
self.shards[self.get_shard_index_by_hash(make_hash(val))].0.get_mut()
72+
}
73+
}
74+
6675
/// Get a shard with a pre-computed hash value. If `get_shard_by_value` is
6776
/// ever used in combination with `get_shard_by_hash` on a single `Sharded`
6877
/// instance, then `hash` must be computed with `FxHasher`. Otherwise,

src/librustc_incremental/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ rustc_ast = { path = "../librustc_ast" }
2121
rustc_span = { path = "../librustc_span" }
2222
rustc_fs_util = { path = "../librustc_fs_util" }
2323
rustc_session = { path = "../librustc_session" }
24+
rustc_query_system = { path = "../librustc_query_system" }

src/librustc_incremental/persist/load.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Code to save/load the dep-graph from files.
22
3-
use rustc::dep_graph::{PreviousDepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
3+
use rustc::dep_graph::{DepKind, PreviousDepGraph, SerializedDepGraph};
44
use rustc::ty::query::OnDiskCache;
55
use rustc::ty::TyCtxt;
66
use rustc_data_structures::fx::FxHashMap;
7+
use rustc_query_system::dep_graph::{CurrentDepGraph, DepGraphArgs};
78
use rustc_serialize::opaque::Decoder;
89
use rustc_serialize::Decodable as RustcDecodable;
910
use rustc_session::Session;
@@ -22,16 +23,14 @@ pub fn dep_graph_tcx_init(tcx: TyCtxt<'_>) {
2223
tcx.allocate_metadata_dep_nodes();
2324
}
2425

25-
type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;
26-
2726
pub enum LoadResult<T> {
2827
Ok { data: T },
2928
DataOutOfDate,
3029
Error { message: String },
3130
}
3231

33-
impl LoadResult<(PreviousDepGraph, WorkProductMap)> {
34-
pub fn open(self, sess: &Session) -> (PreviousDepGraph, WorkProductMap) {
32+
impl LoadResult<DepGraphArgs<DepKind>> {
33+
pub fn open(self, sess: &Session) -> DepGraphArgs<DepKind> {
3534
match self {
3635
LoadResult::Error { message } => {
3736
sess.warn(&message);
@@ -88,7 +87,7 @@ impl<T> MaybeAsync<T> {
8887
}
8988
}
9089

91-
pub type DepGraphFuture = MaybeAsync<LoadResult<(PreviousDepGraph, WorkProductMap)>>;
90+
pub type DepGraphFuture = MaybeAsync<LoadResult<DepGraphArgs<DepKind>>>;
9291

9392
/// Launch a thread and load the dependency graph in the background.
9493
pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
@@ -188,7 +187,13 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
188187
let dep_graph = SerializedDepGraph::decode(&mut decoder)
189188
.expect("Error reading cached dep-graph");
190189

191-
LoadResult::Ok { data: (PreviousDepGraph::new(dep_graph), prev_work_products) }
190+
let (prev_graph, state) = PreviousDepGraph::new_and_state(dep_graph);
191+
let current = prof
192+
.generic_activity("incr_comp_load_setup_dep_graph")
193+
.run(|| CurrentDepGraph::new(&prev_graph));
194+
LoadResult::Ok {
195+
data: DepGraphArgs { state, prev_graph, prev_work_products, current },
196+
}
192197
}
193198
}
194199
}))

src/librustc_interface/queries.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,15 @@ impl<'tcx> Queries<'tcx> {
191191
Ok(match self.dep_graph_future()?.take() {
192192
None => DepGraph::new_disabled(),
193193
Some(future) => {
194-
let (prev_graph, prev_work_products) =
195-
self.session().time("blocked_on_dep_graph_loading", || {
196-
future
197-
.open()
198-
.unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
199-
message: format!("could not decode incremental cache: {:?}", e),
200-
})
201-
.open(self.session())
202-
});
203-
DepGraph::new(prev_graph, prev_work_products)
194+
let args = self.session().time("blocked_on_dep_graph_loading", || {
195+
future
196+
.open()
197+
.unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
198+
message: format!("could not decode incremental cache: {:?}", e),
199+
})
200+
.open(self.session())
201+
});
202+
DepGraph::new(args)
204203
}
205204
})
206205
})

src/librustc_macros/src/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn add_query_description_impl(
324324
#[inline]
325325
fn try_load_from_disk(
326326
#tcx: TyCtxt<'tcx>,
327-
#id: SerializedDepNodeIndex
327+
#id: DepNodeIndex
328328
) -> Option<Self::Value> {
329329
#block
330330
}
@@ -335,7 +335,7 @@ fn add_query_description_impl(
335335
#[inline]
336336
fn try_load_from_disk(
337337
tcx: TyCtxt<'tcx>,
338-
id: SerializedDepNodeIndex
338+
id: DepNodeIndex
339339
) -> Option<Self::Value> {
340340
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
341341
}

0 commit comments

Comments
 (0)