Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dep node indices persistent between sessions #62038

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,7 @@ dependencies = [
"rustc_data_structures",
"rustc_fs_util",
"rustc_hir",
"rustc_query_system",
"rustc_session",
"rustc_span",
"serialize",
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod dep_node;

pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
pub use rustc_query_system::dep_graph::{
debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
WorkProduct, WorkProductFileKind, WorkProductId,
debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, WorkProduct, WorkProductFileKind,
WorkProductId,
};

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

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

fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dep_graph::SerializedDepNodeIndex;
use crate::dep_graph::DepNodeIndex;
use crate::mir;
use crate::mir::interpret::{GlobalId, LitToConstInput};
use crate::traits;
Expand Down
29 changes: 13 additions & 16 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
use crate::dep_graph::DepNodeIndex;
use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
use crate::mir::{self, interpret};
use crate::ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
Expand All @@ -12,7 +12,7 @@ use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
use rustc_hir::definitions::DefPathHash;
use rustc_index::vec::{Idx, IndexVec};
use rustc_index::vec::IndexVec;
use rustc_serialize::{
opaque, Decodable, Decoder, Encodable, Encoder, SpecializedDecoder, SpecializedEncoder,
UseSpecializedDecodable, UseSpecializedEncodable,
Expand Down Expand Up @@ -60,11 +60,11 @@ pub struct OnDiskCache<'sess> {

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

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

alloc_decoding_state: AllocDecodingState,
}
Expand All @@ -80,8 +80,8 @@ struct Footer {
interpret_alloc_index: Vec<u32>,
}

type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
type EncodedQueryResultIndex = Vec<(DepNodeIndex, AbsoluteBytePos)>;
type EncodedDiagnosticsIndex = Vec<(DepNodeIndex, AbsoluteBytePos)>;
type EncodedDiagnostics = Vec<Diagnostic>;

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

Ok((dep_node_index, pos))
Expand Down Expand Up @@ -304,7 +303,7 @@ impl<'sess> OnDiskCache<'sess> {
pub fn load_diagnostics(
&self,
tcx: TyCtxt<'_>,
dep_node_index: SerializedDepNodeIndex,
dep_node_index: DepNodeIndex,
) -> Vec<Diagnostic> {
let diagnostics: Option<EncodedDiagnostics> =
self.load_indexed(tcx, dep_node_index, &self.prev_diagnostics_index, "diagnostics");
Expand All @@ -328,11 +327,11 @@ impl<'sess> OnDiskCache<'sess> {
}

/// Returns the cached query result if there is something in the cache for
/// the given `SerializedDepNodeIndex`; otherwise returns `None`.
/// the given `DepNodeIndex`; otherwise returns `None`.
pub fn try_load_query_result<T>(
&self,
tcx: TyCtxt<'_>,
dep_node_index: SerializedDepNodeIndex,
dep_node_index: DepNodeIndex,
) -> Option<T>
where
T: Decodable,
Expand Down Expand Up @@ -361,8 +360,8 @@ impl<'sess> OnDiskCache<'sess> {
fn load_indexed<'tcx, T>(
&self,
tcx: TyCtxt<'tcx>,
dep_node_index: SerializedDepNodeIndex,
index: &FxHashMap<SerializedDepNodeIndex, AbsoluteBytePos>,
dep_node_index: DepNodeIndex,
index: &FxHashMap<DepNodeIndex, AbsoluteBytePos>,
debug_tag: &'static str,
) -> Option<T>
where
Expand Down Expand Up @@ -1009,12 +1008,10 @@ where
state.iter_results(|results| {
for (key, value, dep_node) in results {
if Q::cache_on_disk(tcx, key.clone(), Some(&value)) {
let dep_node = SerializedDepNodeIndex::new(dep_node.index());

// Record position of the cache entry.
query_result_index.push((dep_node, AbsoluteBytePos::new(encoder.position())));

// Encode the type check tables with the `SerializedDepNodeIndex`
// Encode the type check tables with the `DepNodeIndex`
// as tag.
encoder.encode_tagged(dep_node, &value)?;
}
Expand Down
15 changes: 12 additions & 3 deletions src/librustc_data_structures/sharded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ pub struct Sharded<T> {
impl<T: Default> Default for Sharded<T> {
#[inline]
fn default() -> Self {
Self::new(T::default)
Self::new(|_| T::default())
}
}

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

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

/// The shard is selected by hashing `val` with `FxHasher`.
pub fn get_shard_by_value_mut<K: Hash + ?Sized>(&mut self, val: &K) -> &mut T {
if SHARDS == 1 {
self.shards[0].0.get_mut()
} else {
self.shards[self.get_shard_index_by_hash(make_hash(val))].0.get_mut()
}
}

/// Get a shard with a pre-computed hash value. If `get_shard_by_value` is
/// ever used in combination with `get_shard_by_hash` on a single `Sharded`
/// instance, then `hash` must be computed with `FxHasher`. Otherwise,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_incremental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ rustc_ast = { path = "../librustc_ast" }
rustc_span = { path = "../librustc_span" }
rustc_fs_util = { path = "../librustc_fs_util" }
rustc_session = { path = "../librustc_session" }
rustc_query_system = { path = "../librustc_query_system" }
19 changes: 12 additions & 7 deletions src/librustc_incremental/persist/load.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Code to save/load the dep-graph from files.

use rustc::dep_graph::{PreviousDepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
use rustc::dep_graph::{DepKind, PreviousDepGraph, SerializedDepGraph};
use rustc::ty::query::OnDiskCache;
use rustc::ty::TyCtxt;
use rustc_data_structures::fx::FxHashMap;
use rustc_query_system::dep_graph::{CurrentDepGraph, DepGraphArgs};
use rustc_serialize::opaque::Decoder;
use rustc_serialize::Decodable as RustcDecodable;
use rustc_session::Session;
Expand All @@ -22,16 +23,14 @@ pub fn dep_graph_tcx_init(tcx: TyCtxt<'_>) {
tcx.allocate_metadata_dep_nodes();
}

type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;

pub enum LoadResult<T> {
Ok { data: T },
DataOutOfDate,
Error { message: String },
}

impl LoadResult<(PreviousDepGraph, WorkProductMap)> {
pub fn open(self, sess: &Session) -> (PreviousDepGraph, WorkProductMap) {
impl LoadResult<DepGraphArgs<DepKind>> {
pub fn open(self, sess: &Session) -> DepGraphArgs<DepKind> {
match self {
LoadResult::Error { message } => {
sess.warn(&message);
Expand Down Expand Up @@ -88,7 +87,7 @@ impl<T> MaybeAsync<T> {
}
}

pub type DepGraphFuture = MaybeAsync<LoadResult<(PreviousDepGraph, WorkProductMap)>>;
pub type DepGraphFuture = MaybeAsync<LoadResult<DepGraphArgs<DepKind>>>;

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

LoadResult::Ok { data: (PreviousDepGraph::new(dep_graph), prev_work_products) }
let (prev_graph, state) = PreviousDepGraph::new_and_state(dep_graph);
let current = prof
.generic_activity("incr_comp_load_setup_dep_graph")
.run(|| CurrentDepGraph::new(&prev_graph));
LoadResult::Ok {
data: DepGraphArgs { state, prev_graph, prev_work_products, current },
}
}
}
}))
Expand Down
19 changes: 9 additions & 10 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,15 @@ impl<'tcx> Queries<'tcx> {
Ok(match self.dep_graph_future()?.take() {
None => DepGraph::new_disabled(),
Some(future) => {
let (prev_graph, prev_work_products) =
self.session().time("blocked_on_dep_graph_loading", || {
future
.open()
.unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
message: format!("could not decode incremental cache: {:?}", e),
})
.open(self.session())
});
DepGraph::new(prev_graph, prev_work_products)
let args = self.session().time("blocked_on_dep_graph_loading", || {
future
.open()
.unwrap_or_else(|e| rustc_incremental::LoadResult::Error {
message: format!("could not decode incremental cache: {:?}", e),
})
.open(self.session())
});
DepGraph::new(args)
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn add_query_description_impl(
#[inline]
fn try_load_from_disk(
#tcx: TyCtxt<'tcx>,
#id: SerializedDepNodeIndex
#id: DepNodeIndex
) -> Option<Self::Value> {
#block
}
Expand All @@ -335,7 +335,7 @@ fn add_query_description_impl(
#[inline]
fn try_load_from_disk(
tcx: TyCtxt<'tcx>,
id: SerializedDepNodeIndex
id: DepNodeIndex
) -> Option<Self::Value> {
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
}
Expand Down
Loading