From c4a451e5af8ed1153a5a7a127c594265bd44e624 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 11 Mar 2020 22:02:50 +0100 Subject: [PATCH] Make QueryCache generic on the context. --- src/librustc/ty/query/caches.rs | 35 +++++++------- src/librustc/ty/query/config.rs | 2 +- src/librustc/ty/query/plumbing.rs | 55 ++++++++++++---------- src/librustc/ty/query/profiling_support.rs | 2 +- src/librustc/ty/query/stats.rs | 2 +- 5 files changed, 50 insertions(+), 46 deletions(-) diff --git a/src/librustc/ty/query/caches.rs b/src/librustc/ty/query/caches.rs index 7dd858142da..f740fada1e5 100644 --- a/src/librustc/ty/query/caches.rs +++ b/src/librustc/ty/query/caches.rs @@ -1,6 +1,6 @@ use crate::dep_graph::DepNodeIndex; +use crate::ty::query::config::QueryContext; use crate::ty::query::plumbing::{QueryLookup, QueryState, QueryStateShard}; -use crate::ty::TyCtxt; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sharded::Sharded; @@ -8,11 +8,11 @@ use std::hash::Hash; use std::marker::PhantomData; -pub(crate) trait CacheSelector { - type Cache: QueryCache; +pub(crate) trait CacheSelector { + type Cache: QueryCache; } -pub(crate) trait QueryCache: Default { +pub(crate) trait QueryCache: Default { type Key; type Value; type Sharded: Default; @@ -21,9 +21,9 @@ pub(crate) trait QueryCache: Default { /// It returns the shard index and a lock guard to the shard, /// which will be used if the query is not in the cache and we need /// to compute it. - fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( + fn lookup( &self, - state: &'tcx QueryState, Self>, + state: &QueryState, get_cache: GetCache, key: Self::Key, // `on_hit` can be called while holding a lock to the query state shard. @@ -32,14 +32,14 @@ fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( ) -> R where GetCache: for<'a> Fn( - &'a mut QueryStateShard, Self::Key, Self::Sharded>, + &'a mut QueryStateShard, ) -> &'a mut Self::Sharded, OnHit: FnOnce(&Self::Value, DepNodeIndex) -> R, - OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, TyCtxt<'tcx>, Self::Key, Self::Sharded>) -> R; + OnMiss: FnOnce(Self::Key, QueryLookup<'_, CTX, Self::Key, Self::Sharded>) -> R; fn complete( &self, - tcx: TyCtxt<'tcx>, + tcx: CTX, lock_sharded_storage: &mut Self::Sharded, key: Self::Key, value: Self::Value, @@ -58,7 +58,7 @@ fn iter( pub struct DefaultCacheSelector; -impl CacheSelector for DefaultCacheSelector { +impl CacheSelector for DefaultCacheSelector { type Cache = DefaultCache; } @@ -70,26 +70,25 @@ fn default() -> Self { } } -impl QueryCache for DefaultCache { +impl QueryCache for DefaultCache { type Key = K; type Value = V; type Sharded = FxHashMap; #[inline(always)] - fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( + fn lookup( &self, - state: &'tcx QueryState, Self>, + state: &QueryState, get_cache: GetCache, key: K, on_hit: OnHit, on_miss: OnMiss, ) -> R where - GetCache: for<'a> Fn( - &'a mut QueryStateShard, K, Self::Sharded>, - ) -> &'a mut Self::Sharded, + GetCache: + for<'a> Fn(&'a mut QueryStateShard) -> &'a mut Self::Sharded, OnHit: FnOnce(&V, DepNodeIndex) -> R, - OnMiss: FnOnce(K, QueryLookup<'tcx, TyCtxt<'tcx>, K, Self::Sharded>) -> R, + OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R, { let mut lookup = state.get_lookup(&key); let lock = &mut *lookup.lock; @@ -102,7 +101,7 @@ fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( #[inline] fn complete( &self, - _: TyCtxt<'tcx>, + _: CTX, lock_sharded_storage: &mut Self::Sharded, key: K, value: V, diff --git a/src/librustc/ty/query/config.rs b/src/librustc/ty/query/config.rs index e65f3094820..fd2f855d5b1 100644 --- a/src/librustc/ty/query/config.rs +++ b/src/librustc/ty/query/config.rs @@ -38,7 +38,7 @@ pub(crate) trait QueryAccessors: QueryConfig { const EVAL_ALWAYS: bool; const DEP_KIND: DepKind; - type Cache: QueryCache; + type Cache: QueryCache; // Don't use this method to access query results, instead use the methods on TyCtxt fn query_state<'a>(tcx: CTX) -> &'a QueryState; diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index c0f2c4a11bc..8462610a6b6 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -51,14 +51,14 @@ fn default() -> QueryStateShard { } } -pub(crate) struct QueryState { +pub(crate) struct QueryState> { cache: C, shards: Sharded>, #[cfg(debug_assertions)] pub(super) cache_hits: AtomicUsize, } -impl QueryState { +impl> QueryState { pub(super) fn get_lookup( &'tcx self, key: &K2, @@ -86,7 +86,7 @@ enum QueryResult { Poisoned, } -impl QueryState { +impl> QueryState { pub(super) fn iter_results( &self, f: impl for<'a> FnOnce( @@ -130,7 +130,7 @@ pub(super) fn try_collect_active_jobs( } } -impl Default for QueryState { +impl> Default for QueryState { fn default() -> QueryState { QueryState { cache: C::default(), @@ -152,7 +152,7 @@ pub(crate) struct QueryLookup<'tcx, CTX: QueryContext, K, C> { /// This will poison the relevant query if dropped. struct JobOwner<'tcx, CTX: QueryContext, C> where - C: QueryCache, + C: QueryCache, C::Key: Eq + Hash + Clone + Debug, C::Value: Clone, { @@ -161,9 +161,9 @@ struct JobOwner<'tcx, CTX: QueryContext, C> id: QueryJobId, } -impl<'tcx, C: QueryCache> JobOwner<'tcx, TyCtxt<'tcx>, C> +impl<'tcx, C> JobOwner<'tcx, TyCtxt<'tcx>, C> where - C: QueryCache, + C: QueryCache> + 'tcx, C::Key: Eq + Hash + Clone + Debug, C::Value: Clone, { @@ -176,12 +176,12 @@ impl<'tcx, C: QueryCache> JobOwner<'tcx, TyCtxt<'tcx>, C> /// This function is inlined because that results in a noticeable speed-up /// for some compile-time benchmarks. #[inline(always)] - fn try_start( + fn try_start<'a, 'b, Q>( tcx: TyCtxt<'tcx>, span: Span, key: &C::Key, - mut lookup: QueryLookup<'tcx, TyCtxt<'tcx>, C::Key, C::Sharded>, - ) -> TryGetJob<'tcx, C> + mut lookup: QueryLookup<'a, TyCtxt<'tcx>, C::Key, C::Sharded>, + ) -> TryGetJob<'b, TyCtxt<'tcx>, C> where Q: QueryDescription, Key = C::Key, Value = C::Value, Cache = C>, { @@ -262,16 +262,16 @@ fn try_start( } } -impl<'tcx, CTX: QueryContext, C: QueryCache> JobOwner<'tcx, CTX, C> +impl<'tcx, CTX: QueryContext, C> JobOwner<'tcx, CTX, C> where - C: QueryCache, + C: QueryCache, C::Key: Eq + Hash + Clone + Debug, C::Value: Clone, { /// Completes the query by updating the query cache with the `result`, /// signals the waiter and forgets the JobOwner, so it won't poison the query #[inline(always)] - fn complete(self, tcx: TyCtxt<'tcx>, result: &C::Value, dep_node_index: DepNodeIndex) { + fn complete(self, tcx: CTX, result: &C::Value, dep_node_index: DepNodeIndex) { // We can move out of `self` here because we `mem::forget` it below let key = unsafe { ptr::read(&self.key) }; let state = self.state; @@ -304,7 +304,7 @@ fn with_diagnostics(f: F) -> (R, ThinVec) (result, diagnostics.into_inner()) } -impl<'tcx, CTX: QueryContext, C: QueryCache> Drop for JobOwner<'tcx, CTX, C> +impl<'tcx, CTX: QueryContext, C: QueryCache> Drop for JobOwner<'tcx, CTX, C> where C::Key: Eq + Hash + Clone + Debug, C::Value: Clone, @@ -338,13 +338,13 @@ pub(crate) struct CycleError { } /// The result of `try_start`. -enum TryGetJob<'tcx, C: QueryCache> +enum TryGetJob<'tcx, CTX: QueryContext, C: QueryCache> where C::Key: Eq + Hash + Clone + Debug, C::Value: Clone, { /// The query is not yet started. Contains a guard to the cache eventually used to start it. - NotYetStarted(JobOwner<'tcx, TyCtxt<'tcx>, C>), + NotYetStarted(JobOwner<'tcx, CTX, C>), /// The query was already completed. /// Returns the result of the query and its dep-node index @@ -504,9 +504,9 @@ fn try_get_cached( on_miss: OnMiss, ) -> R where - C: QueryCache, + C: QueryCache>, OnHit: FnOnce(&C::Value, DepNodeIndex) -> R, - OnMiss: FnOnce(C::Key, QueryLookup<'tcx, TyCtxt<'tcx>, C::Key, C::Sharded>) -> R, + OnMiss: FnOnce(C::Key, QueryLookup<'_, TyCtxt<'tcx>, C::Key, C::Sharded>) -> R, { state.cache.lookup( state, @@ -550,7 +550,12 @@ fn try_execute_query> + 'tcx>( self, span: Span, key: Q::Key, - lookup: QueryLookup<'tcx, TyCtxt<'tcx>, Q::Key, ::Sharded>, + lookup: QueryLookup< + '_, + TyCtxt<'tcx>, + Q::Key, + >>::Sharded, + >, ) -> Q::Value { let job = match JobOwner::try_start::(self, span, &key, lookup) { TryGetJob::NotYetStarted(job) => job, @@ -866,14 +871,14 @@ macro_rules! is_eval_always { } macro_rules! query_storage { - ([][$K:ty, $V:ty]) => { - <<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache + (<$tcx:tt>[][$K:ty, $V:ty]) => { + <<$K as Key>::CacheSelector as CacheSelector, $K, $V>>::Cache }; - ([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => { + (<$tcx:tt>[storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => { $ty }; - ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => { - query_storage!([$($($modifiers)*)*][$($args)*]) + (<$tcx:tt>[$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => { + query_storage!(<$tcx>[$($($modifiers)*)*][$($args)*]) }; } @@ -989,7 +994,7 @@ impl<$tcx> QueryAccessors> for queries::$name<$tcx> { const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]); const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node; - type Cache = query_storage!([$($modifiers)*][$K, $V]); + type Cache = query_storage!(<$tcx>[$($modifiers)*][$K, $V]); #[inline(always)] fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState, Self::Cache> { diff --git a/src/librustc/ty/query/profiling_support.rs b/src/librustc/ty/query/profiling_support.rs index a540a18a19f..616fbaafab9 100644 --- a/src/librustc/ty/query/profiling_support.rs +++ b/src/librustc/ty/query/profiling_support.rs @@ -163,7 +163,7 @@ pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>( query_state: &QueryState, C>, string_cache: &mut QueryKeyStringCache, ) where - C: QueryCache, + C: QueryCache>, C::Key: Debug + Clone, { tcx.prof.with_profiler(|profiler| { diff --git a/src/librustc/ty/query/stats.rs b/src/librustc/ty/query/stats.rs index e6578d1eb5f..a13f00dc6d4 100644 --- a/src/librustc/ty/query/stats.rs +++ b/src/librustc/ty/query/stats.rs @@ -38,7 +38,7 @@ struct QueryStats { local_def_id_keys: Option, } -fn stats( +fn stats>( name: &'static str, map: &QueryState, ) -> QueryStats { -- 2.44.0