Skip to content

Commit 18ed6b7

Browse files
committed
Make QueryCache parameters associated types.
1 parent 303246c commit 18ed6b7

File tree

5 files changed

+96
-94
lines changed

5 files changed

+96
-94
lines changed

src/librustc/ty/query/caches.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::sharded::Sharded;
77
use std::default::Default;
88
use std::hash::Hash;
9+
use std::marker::PhantomData;
910

1011
pub(crate) trait CacheSelector<K, V> {
11-
type Cache: QueryCache<K, V>;
12+
type Cache: QueryCache<Key = K, Value = V>;
1213
}
1314

14-
pub(crate) trait QueryCache<K, V>: Default {
15+
pub(crate) trait QueryCache: Default {
16+
type Key;
17+
type Value;
1518
type Sharded: Default;
1619

1720
/// Checks if the query is already computed and in the cache.
@@ -20,52 +23,62 @@ pub(crate) trait QueryCache<K, V>: Default {
2023
/// to compute it.
2124
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
2225
&self,
23-
state: &'tcx QueryStateImpl<'tcx, K, V, Self>,
26+
state: &'tcx QueryStateImpl<'tcx, Self>,
2427
get_cache: GetCache,
25-
key: K,
28+
key: Self::Key,
2629
// `on_hit` can be called while holding a lock to the query state shard.
2730
on_hit: OnHit,
2831
on_miss: OnMiss,
2932
) -> R
3033
where
31-
GetCache:
32-
for<'a> Fn(&'a mut QueryStateShard<'tcx, K, Self::Sharded>) -> &'a mut Self::Sharded,
33-
OnHit: FnOnce(&V, DepNodeIndex) -> R,
34-
OnMiss: FnOnce(K, QueryLookup<'tcx, K, Self::Sharded>) -> R;
34+
GetCache: for<'a> Fn(
35+
&'a mut QueryStateShard<'tcx, Self::Key, Self::Sharded>,
36+
) -> &'a mut Self::Sharded,
37+
OnHit: FnOnce(&Self::Value, DepNodeIndex) -> R,
38+
OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, Self::Key, Self::Sharded>) -> R;
3539

3640
fn complete(
3741
&self,
3842
tcx: TyCtxt<'tcx>,
3943
lock_sharded_storage: &mut Self::Sharded,
40-
key: K,
41-
value: V,
44+
key: Self::Key,
45+
value: Self::Value,
4246
index: DepNodeIndex,
4347
);
4448

4549
fn iter<R, L>(
4650
&self,
4751
shards: &Sharded<L>,
4852
get_shard: impl Fn(&mut L) -> &mut Self::Sharded,
49-
f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R,
53+
f: impl for<'a> FnOnce(
54+
Box<dyn Iterator<Item = (&'a Self::Key, &'a Self::Value, DepNodeIndex)> + 'a>,
55+
) -> R,
5056
) -> R;
5157
}
5258

5359
pub struct DefaultCacheSelector;
5460

5561
impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
56-
type Cache = DefaultCache;
62+
type Cache = DefaultCache<K, V>;
5763
}
5864

59-
#[derive(Default)]
60-
pub struct DefaultCache;
65+
pub struct DefaultCache<K, V>(PhantomData<(K, V)>);
66+
67+
impl<K, V> Default for DefaultCache<K, V> {
68+
fn default() -> Self {
69+
DefaultCache(PhantomData)
70+
}
71+
}
6172

62-
impl<K: Eq + Hash, V: Clone> QueryCache<K, V> for DefaultCache {
73+
impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
74+
type Key = K;
75+
type Value = V;
6376
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
6477

6578
#[inline(always)]
6679
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
6780
&self,
68-
state: &'tcx QueryStateImpl<'tcx, K, V, Self>,
81+
state: &'tcx QueryStateImpl<'tcx, Self>,
6982
get_cache: GetCache,
7083
key: K,
7184
on_hit: OnHit,

src/librustc/ty/query/config.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
3030
const EVAL_ALWAYS: bool;
3131
const DEP_KIND: DepKind;
3232

33-
type Cache: QueryCache<Self::Key, Self::Value>;
33+
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
3434

3535
// Don't use this method to access query results, instead use the methods on TyCtxt
3636
fn query_state<'a>(tcx: TyCtxt<'tcx>) -> &'a QueryState<'tcx, Self>;
@@ -59,10 +59,7 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5959
}
6060
}
6161

62-
impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M
63-
where
64-
<M as QueryAccessors<'tcx>>::Cache: QueryCache<DefId, <M as QueryConfig<'tcx>>::Value>,
65-
{
62+
impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
6663
default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
6764
if !tcx.sess.verbose() {
6865
format!("processing `{}`", tcx.def_path_str(def_id)).into()

src/librustc/ty/query/plumbing.rs

+55-58
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use crate::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
66
use crate::ty::query::caches::QueryCache;
7-
use crate::ty::query::config::{QueryAccessors, QueryConfig, QueryDescription};
7+
use crate::ty::query::config::{QueryAccessors, QueryDescription};
88
use crate::ty::query::job::{QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryShardJobId};
99
use crate::ty::query::Query;
1010
use crate::ty::tls;
@@ -49,22 +49,20 @@ impl<'tcx, K, C: Default> Default for QueryStateShard<'tcx, K, C> {
4949
}
5050
}
5151

52-
pub(crate) type QueryState<'tcx, Q> = QueryStateImpl<
53-
'tcx,
54-
<Q as QueryConfig<'tcx>>::Key,
55-
<Q as QueryConfig<'tcx>>::Value,
56-
<Q as QueryAccessors<'tcx>>::Cache,
57-
>;
52+
pub(crate) type QueryState<'tcx, Q> = QueryStateImpl<'tcx, <Q as QueryAccessors<'tcx>>::Cache>;
5853

59-
pub(crate) struct QueryStateImpl<'tcx, K, V, C: QueryCache<K, V>> {
54+
pub(crate) struct QueryStateImpl<'tcx, C: QueryCache> {
6055
pub(super) cache: C,
61-
pub(super) shards: Sharded<QueryStateShard<'tcx, K, C::Sharded>>,
56+
pub(super) shards: Sharded<QueryStateShard<'tcx, C::Key, C::Sharded>>,
6257
#[cfg(debug_assertions)]
6358
pub(super) cache_hits: AtomicUsize,
6459
}
6560

66-
impl<'tcx, K, V, C: QueryCache<K, V>> QueryStateImpl<'tcx, K, V, C> {
67-
pub(super) fn get_lookup<K2: Hash>(&'tcx self, key: &K2) -> QueryLookup<'tcx, K, C::Sharded> {
61+
impl<'tcx, C: QueryCache> QueryStateImpl<'tcx, C> {
62+
pub(super) fn get_lookup<K2: Hash>(
63+
&'tcx self,
64+
key: &K2,
65+
) -> QueryLookup<'tcx, C::Key, C::Sharded> {
6866
// We compute the key's hash once and then use it for both the
6967
// shard lookup and the hashmap lookup. This relies on the fact
7068
// that both of them use `FxHasher`.
@@ -88,10 +86,12 @@ pub(super) enum QueryResult<'tcx> {
8886
Poisoned,
8987
}
9088

91-
impl<'tcx, K, V, C: QueryCache<K, V>> QueryStateImpl<'tcx, K, V, C> {
89+
impl<'tcx, C: QueryCache> QueryStateImpl<'tcx, C> {
9290
pub fn iter_results<R>(
9391
&self,
94-
f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R,
92+
f: impl for<'a> FnOnce(
93+
Box<dyn Iterator<Item = (&'a C::Key, &'a C::Value, DepNodeIndex)> + 'a>,
94+
) -> R,
9595
) -> R {
9696
self.cache.iter(&self.shards, |shard| &mut shard.cache, f)
9797
}
@@ -104,11 +104,11 @@ impl<'tcx, K, V, C: QueryCache<K, V>> QueryStateImpl<'tcx, K, V, C> {
104104
pub(super) fn try_collect_active_jobs(
105105
&self,
106106
kind: DepKind,
107-
make_query: impl Fn(K) -> Query<'tcx> + Copy,
107+
make_query: impl Fn(C::Key) -> Query<'tcx> + Copy,
108108
jobs: &mut FxHashMap<QueryJobId, QueryJobInfo<'tcx>>,
109109
) -> Option<()>
110110
where
111-
K: Clone,
111+
C::Key: Clone,
112112
{
113113
// We use try_lock_shards here since we are called from the
114114
// deadlock handler, and this shouldn't be locked.
@@ -131,8 +131,8 @@ impl<'tcx, K, V, C: QueryCache<K, V>> QueryStateImpl<'tcx, K, V, C> {
131131
}
132132
}
133133

134-
impl<'tcx, K, V, C: QueryCache<K, V>> Default for QueryStateImpl<'tcx, K, V, C> {
135-
fn default() -> QueryStateImpl<'tcx, K, V, C> {
134+
impl<'tcx, C: QueryCache> Default for QueryStateImpl<'tcx, C> {
135+
fn default() -> QueryStateImpl<'tcx, C> {
136136
QueryStateImpl {
137137
cache: C::default(),
138138
shards: Default::default(),
@@ -151,27 +151,22 @@ pub(crate) struct QueryLookup<'tcx, K, C> {
151151

152152
/// A type representing the responsibility to execute the job in the `job` field.
153153
/// This will poison the relevant query if dropped.
154-
pub(super) type JobOwner<'tcx, Q> = JobOwnerImpl<
155-
'tcx,
156-
<Q as QueryConfig<'tcx>>::Key,
157-
<Q as QueryConfig<'tcx>>::Value,
158-
<Q as QueryAccessors<'tcx>>::Cache,
159-
>;
160-
161-
pub(super) struct JobOwnerImpl<'tcx, K, V, C: QueryCache<K, V>>
154+
pub(super) struct JobOwner<'tcx, C>
162155
where
163-
K: Eq + Hash + Clone + Debug,
164-
V: Clone,
156+
C: QueryCache,
157+
C::Key: Eq + Hash + Clone + Debug,
158+
C::Value: Clone,
165159
{
166-
state: &'tcx QueryStateImpl<'tcx, K, V, C>,
167-
key: K,
160+
state: &'tcx QueryStateImpl<'tcx, C>,
161+
key: C::Key,
168162
id: QueryJobId,
169163
}
170164

171-
impl<'tcx, K, V, C: QueryCache<K, V>> JobOwnerImpl<'tcx, K, V, C>
165+
impl<'tcx, C: QueryCache> JobOwner<'tcx, C>
172166
where
173-
K: Eq + Hash + Clone + Debug,
174-
V: Clone,
167+
C: QueryCache,
168+
C::Key: Eq + Hash + Clone + Debug,
169+
C::Value: Clone,
175170
{
176171
/// Either gets a `JobOwner` corresponding the query, allowing us to
177172
/// start executing the query, or returns with the result of the query.
@@ -185,13 +180,11 @@ where
185180
pub(super) fn try_start<Q>(
186181
tcx: TyCtxt<'tcx>,
187182
span: Span,
188-
key: &K,
189-
mut lookup: QueryLookup<'tcx, K, C::Sharded>,
190-
) -> TryGetJob<'tcx, Q>
183+
key: &C::Key,
184+
mut lookup: QueryLookup<'tcx, C::Key, C::Sharded>,
185+
) -> TryGetJob<'tcx, C>
191186
where
192-
K: Eq + Hash + Clone + Debug,
193-
V: Clone,
194-
Q: QueryDescription<'tcx, Key = K, Value = V, Cache = C> + 'tcx,
187+
Q: QueryDescription<'tcx, Key = C::Key, Value = C::Value, Cache = C>,
195188
{
196189
let lock = &mut *lookup.lock;
197190

@@ -231,7 +224,7 @@ where
231224
entry.insert(QueryResult::Started(job));
232225

233226
let owner =
234-
JobOwnerImpl { state: Q::query_state(tcx), id: global_id, key: (*key).clone() };
227+
JobOwner { state: Q::query_state(tcx), id: global_id, key: (*key).clone() };
235228
return TryGetJob::NotYetStarted(owner);
236229
}
237230
};
@@ -272,7 +265,7 @@ where
272265
/// Completes the query by updating the query cache with the `result`,
273266
/// signals the waiter and forgets the JobOwner, so it won't poison the query
274267
#[inline(always)]
275-
pub(super) fn complete(self, tcx: TyCtxt<'tcx>, result: &V, dep_node_index: DepNodeIndex) {
268+
pub(super) fn complete(self, tcx: TyCtxt<'tcx>, result: &C::Value, dep_node_index: DepNodeIndex) {
276269
// We can move out of `self` here because we `mem::forget` it below
277270
let key = unsafe { ptr::read(&self.key) };
278271
let state = self.state;
@@ -305,10 +298,10 @@ where
305298
(result, diagnostics.into_inner())
306299
}
307300

308-
impl<'tcx, K, V, C: QueryCache<K, V>> Drop for JobOwnerImpl<'tcx, K, V, C>
301+
impl<'tcx, C: QueryCache> Drop for JobOwner<'tcx, C>
309302
where
310-
K: Eq + Hash + Clone + Debug,
311-
V: Clone,
303+
C::Key: Eq + Hash + Clone + Debug,
304+
C::Value: Clone,
312305
{
313306
#[inline(never)]
314307
#[cold]
@@ -339,18 +332,22 @@ pub struct CycleError<'tcx> {
339332
}
340333

341334
/// The result of `try_start`.
342-
pub(super) enum TryGetJob<'tcx, D: QueryDescription<'tcx>> {
335+
pub(super) enum TryGetJob<'tcx, C: QueryCache>
336+
where
337+
C::Key: Eq + Hash + Clone + Debug,
338+
C::Value: Clone,
339+
{
343340
/// The query is not yet started. Contains a guard to the cache eventually used to start it.
344-
NotYetStarted(JobOwner<'tcx, D>),
341+
NotYetStarted(JobOwner<'tcx, C>),
345342

346343
/// The query was already completed.
347344
/// Returns the result of the query and its dep-node index
348345
/// if it succeeded or a cycle error if it failed.
349346
#[cfg(parallel_compiler)]
350-
JobCompleted((D::Value, DepNodeIndex)),
347+
JobCompleted((C::Value, DepNodeIndex)),
351348

352349
/// Trying to execute the query resulted in a cycle.
353-
Cycle(D::Value),
350+
Cycle(C::Value),
354351
}
355352

356353
impl<'tcx> TyCtxt<'tcx> {
@@ -479,22 +476,22 @@ impl<'tcx> TyCtxt<'tcx> {
479476
/// which will be used if the query is not in the cache and we need
480477
/// to compute it.
481478
#[inline(always)]
482-
fn try_get_cached<K, V, C, R, OnHit, OnMiss>(
479+
fn try_get_cached<C, R, OnHit, OnMiss>(
483480
self,
484-
state: &'tcx QueryStateImpl<'tcx, K, V, C>,
485-
key: K,
481+
state: &'tcx QueryStateImpl<'tcx, C>,
482+
key: C::Key,
486483
// `on_hit` can be called while holding a lock to the query cache
487484
on_hit: OnHit,
488485
on_miss: OnMiss,
489486
) -> R
490487
where
491-
C: QueryCache<K, V>,
492-
OnHit: FnOnce(&V, DepNodeIndex) -> R,
493-
OnMiss: FnOnce(K, QueryLookup<'tcx, K, C::Sharded>) -> R,
488+
C: QueryCache,
489+
OnHit: FnOnce(&C::Value, DepNodeIndex) -> R,
490+
OnMiss: FnOnce(C::Key, QueryLookup<'tcx, C::Key, C::Sharded>) -> R,
494491
{
495492
state.cache.lookup(
496493
state,
497-
QueryStateShard::<K, C::Sharded>::get_cache,
494+
QueryStateShard::<C::Key, C::Sharded>::get_cache,
498495
key,
499496
|value, index| {
500497
if unlikely!(self.prof.enabled()) {
@@ -534,9 +531,9 @@ impl<'tcx> TyCtxt<'tcx> {
534531
self,
535532
span: Span,
536533
key: Q::Key,
537-
lookup: QueryLookup<'tcx, Q::Key, <Q::Cache as QueryCache<Q::Key, Q::Value>>::Sharded>,
534+
lookup: QueryLookup<'tcx, Q::Key, <Q::Cache as QueryCache>::Sharded>,
538535
) -> Q::Value {
539-
let job = match JobOwnerImpl::try_start::<Q>(self, span, &key, lookup) {
536+
let job = match JobOwner::try_start::<Q>(self, span, &key, lookup) {
540537
TryGetJob::NotYetStarted(job) => job,
541538
TryGetJob::Cycle(result) => return result,
542539
#[cfg(parallel_compiler)]
@@ -697,7 +694,7 @@ impl<'tcx> TyCtxt<'tcx> {
697694
fn force_query_with_job<Q: QueryDescription<'tcx> + 'tcx>(
698695
self,
699696
key: Q::Key,
700-
job: JobOwner<'tcx, Q>,
697+
job: JobOwner<'tcx, Q::Cache>,
701698
dep_node: DepNode,
702699
) -> (Q::Value, DepNodeIndex) {
703700
// If the following assertion triggers, it can have two reasons:
@@ -796,7 +793,7 @@ impl<'tcx> TyCtxt<'tcx> {
796793
// Cache hit, do nothing
797794
},
798795
|key, lookup| {
799-
let job = match JobOwnerImpl::try_start::<Q>(self, span, &key, lookup) {
796+
let job = match JobOwner::try_start::<Q>(self, span, &key, lookup) {
800797
TryGetJob::NotYetStarted(job) => job,
801798
TryGetJob::Cycle(_) => return,
802799
#[cfg(parallel_compiler)]

src/librustc/ty/query/profiling_support.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,14 @@ where
157157
/// Allocate the self-profiling query strings for a single query cache. This
158158
/// method is called from `alloc_self_profile_query_strings` which knows all
159159
/// the queries via macro magic.
160-
pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, K, V, C>(
160+
pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
161161
tcx: TyCtxt<'tcx>,
162162
query_name: &'static str,
163-
query_state: &QueryStateImpl<'tcx, K, V, C>,
163+
query_state: &QueryStateImpl<'tcx, C>,
164164
string_cache: &mut QueryKeyStringCache,
165165
) where
166-
K: Debug + Clone,
167-
C: QueryCache<K, V>,
166+
C: QueryCache,
167+
C::Key: Debug + Clone,
168168
{
169169
tcx.prof.with_profiler(|profiler| {
170170
let event_id_builder = profiler.event_id_builder();

0 commit comments

Comments
 (0)