Skip to content

Commit

Permalink
Monomorphise load_from_disk_and_cache_in_memory.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 7, 2020
1 parent 0155f5a commit 87d0eac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
46 changes: 38 additions & 8 deletions src/librustc/ty/query/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ pub(crate) struct QueryVtable<'tcx, K, V> {
pub compute: fn(TyCtxt<'tcx>, K) -> V,

pub hash_result: fn(&mut StableHashingContext<'_>, &V) -> Option<Fingerprint>,
pub cache_on_disk: fn(TyCtxt<'tcx>, K, Option<&V>) -> bool,
pub try_load_from_disk: fn(TyCtxt<'tcx>, SerializedDepNodeIndex) -> Option<V>,
}

impl<'tcx, K, V> QueryVtable<'tcx, K, V> {
pub(crate) fn compute(&self, tcx: TyCtxt<'tcx>, key: K) -> V {
(self.compute)(tcx, key)
}

pub(crate) fn hash_result(
&self,
hcx: &mut StableHashingContext<'_>,
value: &V,
) -> Option<Fingerprint> {
(self.hash_result)(hcx, value)
}

pub(crate) fn cache_on_disk(&self, tcx: TyCtxt<'tcx>, key: K, value: Option<&V>) -> bool {
(self.cache_on_disk)(tcx, key, value)
}

pub(crate) fn try_load_from_disk(
&self,
tcx: TyCtxt<'tcx>,
index: SerializedDepNodeIndex,
) -> Option<V> {
(self.try_load_from_disk)(tcx, index)
}
}

pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
Expand All @@ -54,14 +82,6 @@ pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
-> Option<Fingerprint>;

fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;

fn reify() -> QueryVtable<'tcx, Self::Key, Self::Value> {
QueryVtable {
eval_always: Self::EVAL_ALWAYS,
compute: Self::compute,
hash_result: Self::hash_result,
}
}
}

pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
Expand All @@ -75,6 +95,16 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}

fn reify() -> QueryVtable<'tcx, Self::Key, Self::Value> {
QueryVtable {
eval_always: Self::EVAL_ALWAYS,
compute: Self::compute,
hash_result: Self::hash_result,
cache_on_disk: Self::cache_on_disk,
try_load_from_disk: Self::try_load_from_disk,
}
}
}

impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
Expand Down
25 changes: 14 additions & 11 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,12 @@ impl<'tcx> TyCtxt<'tcx> {
let marked = tcx.dep_graph.try_mark_green_and_read(tcx, &dep_node);
marked.map(|(prev_dep_node_index, dep_node_index)| {
(
tcx.load_from_disk_and_cache_in_memory::<Q>(
tcx.load_from_disk_and_cache_in_memory(
key.clone(),
prev_dep_node_index,
dep_node_index,
&dep_node,
&Q::reify(),
),
dep_node_index,
)
Expand All @@ -603,24 +604,25 @@ impl<'tcx> TyCtxt<'tcx> {
result
}

fn load_from_disk_and_cache_in_memory<Q: QueryDescription<'tcx>>(
fn load_from_disk_and_cache_in_memory<K: Clone, V>(
self,
key: Q::Key,
key: K,
prev_dep_node_index: SerializedDepNodeIndex,
dep_node_index: DepNodeIndex,
dep_node: &DepNode,
) -> Q::Value {
query: &QueryVtable<'tcx, K, V>,
) -> V {
// Note this function can be called concurrently from the same query
// We must ensure that this is handled correctly.

debug_assert!(self.dep_graph.is_green(dep_node));

// First we try to load the result from the on-disk cache.
let result = if Q::cache_on_disk(self, key.clone(), None)
let result = if query.cache_on_disk(self, key.clone(), None)
&& self.sess.opts.debugging_opts.incremental_queries
{
let prof_timer = self.prof.incr_cache_loading();
let result = Q::try_load_from_disk(self, prev_dep_node_index);
let result = query.try_load_from_disk(self, prev_dep_node_index);
prof_timer.finish_with_query_invocation_id(dep_node_index.into());

// We always expect to find a cached result for things that
Expand All @@ -644,7 +646,7 @@ impl<'tcx> TyCtxt<'tcx> {
let prof_timer = self.prof.query_provider();

// The dep-graph for this computation is already in-place.
let result = self.dep_graph.with_ignore(|| Q::compute(self, key));
let result = self.dep_graph.with_ignore(|| query.compute(self, key));

prof_timer.finish_with_query_invocation_id(dep_node_index.into());

Expand All @@ -654,19 +656,20 @@ impl<'tcx> TyCtxt<'tcx> {
// If `-Zincremental-verify-ich` is specified, re-hash results from
// the cache and make sure that they have the expected fingerprint.
if unlikely!(self.sess.opts.debugging_opts.incremental_verify_ich) {
self.incremental_verify_ich::<Q>(&result, dep_node, dep_node_index);
self.incremental_verify_ich(&result, dep_node, dep_node_index, query);
}

result
}

#[inline(never)]
#[cold]
fn incremental_verify_ich<Q: QueryDescription<'tcx>>(
fn incremental_verify_ich<K, V>(
self,
result: &Q::Value,
result: &V,
dep_node: &DepNode,
dep_node_index: DepNodeIndex,
query: &QueryVtable<'tcx, K, V>,
) {
use crate::ich::Fingerprint;

Expand All @@ -680,7 +683,7 @@ impl<'tcx> TyCtxt<'tcx> {
debug!("BEGIN verify_ich({:?})", dep_node);
let mut hcx = self.create_stable_hashing_context();

let new_hash = Q::hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
let new_hash = query.hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
debug!("END verify_ich({:?})", dep_node);

let old_hash = self.dep_graph.fingerprint_of(dep_node_index);
Expand Down

0 comments on commit 87d0eac

Please sign in to comment.