Skip to content

Commit c4a451e

Browse files
committed
Make QueryCache generic on the context.
1 parent 57c3177 commit c4a451e

File tree

5 files changed

+50
-46
lines changed

5 files changed

+50
-46
lines changed

src/librustc/ty/query/caches.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
use crate::dep_graph::DepNodeIndex;
2+
use crate::ty::query::config::QueryContext;
23
use crate::ty::query::plumbing::{QueryLookup, QueryState, QueryStateShard};
3-
use crate::ty::TyCtxt;
44

55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::sharded::Sharded;
77
use std::default::Default;
88
use std::hash::Hash;
99
use std::marker::PhantomData;
1010

11-
pub(crate) trait CacheSelector<K, V> {
12-
type Cache: QueryCache<Key = K, Value = V>;
11+
pub(crate) trait CacheSelector<CTX: QueryContext, K, V> {
12+
type Cache: QueryCache<CTX, Key = K, Value = V>;
1313
}
1414

15-
pub(crate) trait QueryCache: Default {
15+
pub(crate) trait QueryCache<CTX: QueryContext>: Default {
1616
type Key;
1717
type Value;
1818
type Sharded: Default;
@@ -21,9 +21,9 @@ pub(crate) trait QueryCache: Default {
2121
/// It returns the shard index and a lock guard to the shard,
2222
/// which will be used if the query is not in the cache and we need
2323
/// to compute it.
24-
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
24+
fn lookup<R, GetCache, OnHit, OnMiss>(
2525
&self,
26-
state: &'tcx QueryState<TyCtxt<'tcx>, Self>,
26+
state: &QueryState<CTX, Self>,
2727
get_cache: GetCache,
2828
key: Self::Key,
2929
// `on_hit` can be called while holding a lock to the query state shard.
@@ -32,14 +32,14 @@ pub(crate) trait QueryCache: Default {
3232
) -> R
3333
where
3434
GetCache: for<'a> Fn(
35-
&'a mut QueryStateShard<TyCtxt<'tcx>, Self::Key, Self::Sharded>,
35+
&'a mut QueryStateShard<CTX, Self::Key, Self::Sharded>,
3636
) -> &'a mut Self::Sharded,
3737
OnHit: FnOnce(&Self::Value, DepNodeIndex) -> R,
38-
OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, TyCtxt<'tcx>, Self::Key, Self::Sharded>) -> R;
38+
OnMiss: FnOnce(Self::Key, QueryLookup<'_, CTX, Self::Key, Self::Sharded>) -> R;
3939

4040
fn complete(
4141
&self,
42-
tcx: TyCtxt<'tcx>,
42+
tcx: CTX,
4343
lock_sharded_storage: &mut Self::Sharded,
4444
key: Self::Key,
4545
value: Self::Value,
@@ -58,7 +58,7 @@ pub(crate) trait QueryCache: Default {
5858

5959
pub struct DefaultCacheSelector;
6060

61-
impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector {
61+
impl<CTX: QueryContext, K: Eq + Hash, V: Clone> CacheSelector<CTX, K, V> for DefaultCacheSelector {
6262
type Cache = DefaultCache<K, V>;
6363
}
6464

@@ -70,26 +70,25 @@ impl<K, V> Default for DefaultCache<K, V> {
7070
}
7171
}
7272

73-
impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
73+
impl<CTX: QueryContext, K: Eq + Hash, V: Clone> QueryCache<CTX> for DefaultCache<K, V> {
7474
type Key = K;
7575
type Value = V;
7676
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
7777

7878
#[inline(always)]
79-
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
79+
fn lookup<R, GetCache, OnHit, OnMiss>(
8080
&self,
81-
state: &'tcx QueryState<TyCtxt<'tcx>, Self>,
81+
state: &QueryState<CTX, Self>,
8282
get_cache: GetCache,
8383
key: K,
8484
on_hit: OnHit,
8585
on_miss: OnMiss,
8686
) -> R
8787
where
88-
GetCache: for<'a> Fn(
89-
&'a mut QueryStateShard<TyCtxt<'tcx>, K, Self::Sharded>,
90-
) -> &'a mut Self::Sharded,
88+
GetCache:
89+
for<'a> Fn(&'a mut QueryStateShard<CTX, K, Self::Sharded>) -> &'a mut Self::Sharded,
9190
OnHit: FnOnce(&V, DepNodeIndex) -> R,
92-
OnMiss: FnOnce(K, QueryLookup<'tcx, TyCtxt<'tcx>, K, Self::Sharded>) -> R,
91+
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
9392
{
9493
let mut lookup = state.get_lookup(&key);
9594
let lock = &mut *lookup.lock;
@@ -102,7 +101,7 @@ impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
102101
#[inline]
103102
fn complete(
104103
&self,
105-
_: TyCtxt<'tcx>,
104+
_: CTX,
106105
lock_sharded_storage: &mut Self::Sharded,
107106
key: K,
108107
value: V,

src/librustc/ty/query/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
3838
const EVAL_ALWAYS: bool;
3939
const DEP_KIND: DepKind;
4040

41-
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
41+
type Cache: QueryCache<CTX, Key = Self::Key, Value = Self::Value>;
4242

4343
// Don't use this method to access query results, instead use the methods on TyCtxt
4444
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;

src/librustc/ty/query/plumbing.rs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl<CTX: QueryContext, K, C: Default> Default for QueryStateShard<CTX, K, C> {
5151
}
5252
}
5353

54-
pub(crate) struct QueryState<CTX: QueryContext, C: QueryCache> {
54+
pub(crate) struct QueryState<CTX: QueryContext, C: QueryCache<CTX>> {
5555
cache: C,
5656
shards: Sharded<QueryStateShard<CTX, C::Key, C::Sharded>>,
5757
#[cfg(debug_assertions)]
5858
pub(super) cache_hits: AtomicUsize,
5959
}
6060

61-
impl<CTX: QueryContext, C: QueryCache> QueryState<CTX, C> {
61+
impl<CTX: QueryContext, C: QueryCache<CTX>> QueryState<CTX, C> {
6262
pub(super) fn get_lookup<K2: Hash>(
6363
&'tcx self,
6464
key: &K2,
@@ -86,7 +86,7 @@ enum QueryResult<CTX: QueryContext> {
8686
Poisoned,
8787
}
8888

89-
impl<CTX: QueryContext, C: QueryCache> QueryState<CTX, C> {
89+
impl<CTX: QueryContext, C: QueryCache<CTX>> QueryState<CTX, C> {
9090
pub(super) fn iter_results<R>(
9191
&self,
9292
f: impl for<'a> FnOnce(
@@ -130,7 +130,7 @@ impl<CTX: QueryContext, C: QueryCache> QueryState<CTX, C> {
130130
}
131131
}
132132

133-
impl<CTX: QueryContext, C: QueryCache> Default for QueryState<CTX, C> {
133+
impl<CTX: QueryContext, C: QueryCache<CTX>> Default for QueryState<CTX, C> {
134134
fn default() -> QueryState<CTX, C> {
135135
QueryState {
136136
cache: C::default(),
@@ -152,7 +152,7 @@ pub(crate) struct QueryLookup<'tcx, CTX: QueryContext, K, C> {
152152
/// This will poison the relevant query if dropped.
153153
struct JobOwner<'tcx, CTX: QueryContext, C>
154154
where
155-
C: QueryCache,
155+
C: QueryCache<CTX>,
156156
C::Key: Eq + Hash + Clone + Debug,
157157
C::Value: Clone,
158158
{
@@ -161,9 +161,9 @@ where
161161
id: QueryJobId,
162162
}
163163

164-
impl<'tcx, C: QueryCache> JobOwner<'tcx, TyCtxt<'tcx>, C>
164+
impl<'tcx, C> JobOwner<'tcx, TyCtxt<'tcx>, C>
165165
where
166-
C: QueryCache,
166+
C: QueryCache<TyCtxt<'tcx>> + 'tcx,
167167
C::Key: Eq + Hash + Clone + Debug,
168168
C::Value: Clone,
169169
{
@@ -176,12 +176,12 @@ where
176176
/// This function is inlined because that results in a noticeable speed-up
177177
/// for some compile-time benchmarks.
178178
#[inline(always)]
179-
fn try_start<Q>(
179+
fn try_start<'a, 'b, Q>(
180180
tcx: TyCtxt<'tcx>,
181181
span: Span,
182182
key: &C::Key,
183-
mut lookup: QueryLookup<'tcx, TyCtxt<'tcx>, C::Key, C::Sharded>,
184-
) -> TryGetJob<'tcx, C>
183+
mut lookup: QueryLookup<'a, TyCtxt<'tcx>, C::Key, C::Sharded>,
184+
) -> TryGetJob<'b, TyCtxt<'tcx>, C>
185185
where
186186
Q: QueryDescription<TyCtxt<'tcx>, Key = C::Key, Value = C::Value, Cache = C>,
187187
{
@@ -262,16 +262,16 @@ where
262262
}
263263
}
264264

265-
impl<'tcx, CTX: QueryContext, C: QueryCache> JobOwner<'tcx, CTX, C>
265+
impl<'tcx, CTX: QueryContext, C> JobOwner<'tcx, CTX, C>
266266
where
267-
C: QueryCache,
267+
C: QueryCache<CTX>,
268268
C::Key: Eq + Hash + Clone + Debug,
269269
C::Value: Clone,
270270
{
271271
/// Completes the query by updating the query cache with the `result`,
272272
/// signals the waiter and forgets the JobOwner, so it won't poison the query
273273
#[inline(always)]
274-
fn complete(self, tcx: TyCtxt<'tcx>, result: &C::Value, dep_node_index: DepNodeIndex) {
274+
fn complete(self, tcx: CTX, result: &C::Value, dep_node_index: DepNodeIndex) {
275275
// We can move out of `self` here because we `mem::forget` it below
276276
let key = unsafe { ptr::read(&self.key) };
277277
let state = self.state;
@@ -304,7 +304,7 @@ where
304304
(result, diagnostics.into_inner())
305305
}
306306

307-
impl<'tcx, CTX: QueryContext, C: QueryCache> Drop for JobOwner<'tcx, CTX, C>
307+
impl<'tcx, CTX: QueryContext, C: QueryCache<CTX>> Drop for JobOwner<'tcx, CTX, C>
308308
where
309309
C::Key: Eq + Hash + Clone + Debug,
310310
C::Value: Clone,
@@ -338,13 +338,13 @@ pub(crate) struct CycleError<CTX: QueryContext> {
338338
}
339339

340340
/// The result of `try_start`.
341-
enum TryGetJob<'tcx, C: QueryCache>
341+
enum TryGetJob<'tcx, CTX: QueryContext, C: QueryCache<CTX>>
342342
where
343343
C::Key: Eq + Hash + Clone + Debug,
344344
C::Value: Clone,
345345
{
346346
/// The query is not yet started. Contains a guard to the cache eventually used to start it.
347-
NotYetStarted(JobOwner<'tcx, TyCtxt<'tcx>, C>),
347+
NotYetStarted(JobOwner<'tcx, CTX, C>),
348348

349349
/// The query was already completed.
350350
/// Returns the result of the query and its dep-node index
@@ -504,9 +504,9 @@ impl<'tcx> TyCtxt<'tcx> {
504504
on_miss: OnMiss,
505505
) -> R
506506
where
507-
C: QueryCache,
507+
C: QueryCache<TyCtxt<'tcx>>,
508508
OnHit: FnOnce(&C::Value, DepNodeIndex) -> R,
509-
OnMiss: FnOnce(C::Key, QueryLookup<'tcx, TyCtxt<'tcx>, C::Key, C::Sharded>) -> R,
509+
OnMiss: FnOnce(C::Key, QueryLookup<'_, TyCtxt<'tcx>, C::Key, C::Sharded>) -> R,
510510
{
511511
state.cache.lookup(
512512
state,
@@ -550,7 +550,12 @@ impl<'tcx> TyCtxt<'tcx> {
550550
self,
551551
span: Span,
552552
key: Q::Key,
553-
lookup: QueryLookup<'tcx, TyCtxt<'tcx>, Q::Key, <Q::Cache as QueryCache>::Sharded>,
553+
lookup: QueryLookup<
554+
'_,
555+
TyCtxt<'tcx>,
556+
Q::Key,
557+
<Q::Cache as QueryCache<TyCtxt<'tcx>>>::Sharded,
558+
>,
554559
) -> Q::Value {
555560
let job = match JobOwner::try_start::<Q>(self, span, &key, lookup) {
556561
TryGetJob::NotYetStarted(job) => job,
@@ -866,14 +871,14 @@ macro_rules! is_eval_always {
866871
}
867872

868873
macro_rules! query_storage {
869-
([][$K:ty, $V:ty]) => {
870-
<<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache
874+
(<$tcx:tt>[][$K:ty, $V:ty]) => {
875+
<<$K as Key>::CacheSelector as CacheSelector<TyCtxt<$tcx>, $K, $V>>::Cache
871876
};
872-
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
877+
(<$tcx:tt>[storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
873878
$ty
874879
};
875-
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
876-
query_storage!([$($($modifiers)*)*][$($args)*])
880+
(<$tcx:tt>[$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
881+
query_storage!(<$tcx>[$($($modifiers)*)*][$($args)*])
877882
};
878883
}
879884

@@ -989,7 +994,7 @@ macro_rules! define_queries_inner {
989994
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
990995
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
991996

992-
type Cache = query_storage!([$($modifiers)*][$K, $V]);
997+
type Cache = query_storage!(<$tcx>[$($modifiers)*][$K, $V]);
993998

994999
#[inline(always)]
9951000
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {

src/librustc/ty/query/profiling_support.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
163163
query_state: &QueryState<TyCtxt<'tcx>, C>,
164164
string_cache: &mut QueryKeyStringCache,
165165
) where
166-
C: QueryCache,
166+
C: QueryCache<TyCtxt<'tcx>>,
167167
C::Key: Debug + Clone,
168168
{
169169
tcx.prof.with_profiler(|profiler| {

src/librustc/ty/query/stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct QueryStats {
3838
local_def_id_keys: Option<usize>,
3939
}
4040

41-
fn stats<CTX: QueryContext, C: QueryCache>(
41+
fn stats<CTX: QueryContext, C: QueryCache<CTX>>(
4242
name: &'static str,
4343
map: &QueryState<CTX, C>,
4444
) -> QueryStats {

0 commit comments

Comments
 (0)