From 87d0eac23243bfaf142bf3460d7ed6c2ed8f5dda Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 6 Mar 2020 22:43:08 +0100 Subject: [PATCH] Monomorphise load_from_disk_and_cache_in_memory. --- src/librustc/ty/query/config.rs | 46 +++++++++++++++++++++++++------ src/librustc/ty/query/plumbing.rs | 25 +++++++++-------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/librustc/ty/query/config.rs b/src/librustc/ty/query/config.rs index 7ffc1070a5464..6346e02b7e9b6 100644 --- a/src/librustc/ty/query/config.rs +++ b/src/librustc/ty/query/config.rs @@ -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, + pub cache_on_disk: fn(TyCtxt<'tcx>, K, Option<&V>) -> bool, + pub try_load_from_disk: fn(TyCtxt<'tcx>, SerializedDepNodeIndex) -> Option, +} + +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 { + (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 { + (self.try_load_from_disk)(tcx, index) + } } pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> { @@ -54,14 +82,6 @@ pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> { -> Option; 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> { @@ -75,6 +95,16 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> { fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option { 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 { diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index 82d7e0eac52ce..4c7db650be303 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -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::( + tcx.load_from_disk_and_cache_in_memory( key.clone(), prev_dep_node_index, dep_node_index, &dep_node, + &Q::reify(), ), dep_node_index, ) @@ -603,24 +604,25 @@ impl<'tcx> TyCtxt<'tcx> { result } - fn load_from_disk_and_cache_in_memory>( + fn load_from_disk_and_cache_in_memory( 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 @@ -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()); @@ -654,7 +656,7 @@ 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::(&result, dep_node, dep_node_index); + self.incremental_verify_ich(&result, dep_node, dep_node_index, query); } result @@ -662,11 +664,12 @@ impl<'tcx> TyCtxt<'tcx> { #[inline(never)] #[cold] - fn incremental_verify_ich>( + fn incremental_verify_ich( self, - result: &Q::Value, + result: &V, dep_node: &DepNode, dep_node_index: DepNodeIndex, + query: &QueryVtable<'tcx, K, V>, ) { use crate::ich::Fingerprint; @@ -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);