Skip to content

Commit 500ddc5

Browse files
committed
Auto merge of #77871 - Julian-Wollersberger:less-query-context, r=oli-obk
Make fewer types generic over QueryContext While trying to refactor `rustc_query_system::query::QueryContext` to make it dyn-safe, I noticed some smaller things: * QueryConfig doesn't need to be generic over QueryContext * ~~The `kind` field on QueryJobId is unused~~ * Some unnecessary where clauses * Many types in `job.rs` where generic over `QueryContext` but only needed `QueryContext::Query`. If handle_cycle_error() could be refactored to not take `error: CycleError<CTX::Query>`, all those bounds could be removed as well. Changing `find_cycle_in_stack()` in job.rs to not take a `tcx` argument is the only functional change here. Everything else is just updating type signatures. (aka compile-error driven development ^^) ~~Currently there is a weird bug where memory usage suddenly skyrockets when running UI tests. I'll investigate that tomorrow. A perf run probably won't make sense before that is fixed.~~ EDIT: `kind` actually is used by `Eq`, and re-adding it fixed the memory issue.
2 parents 6b9fbf2 + 52cedca commit 500ddc5

File tree

8 files changed

+216
-174
lines changed

8 files changed

+216
-174
lines changed

compiler/rustc_middle/src/ty/query/plumbing.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl QueryContext for TyCtxt<'tcx> {
4040

4141
fn try_collect_active_jobs(
4242
&self,
43-
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self>>> {
43+
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
44+
{
4445
self.queries.try_collect_active_jobs()
4546
}
4647

@@ -353,7 +354,7 @@ macro_rules! define_queries_inner {
353354
$(pub type $name<$tcx> = $V;)*
354355
}
355356

356-
$(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
357+
$(impl<$tcx> QueryConfig for queries::$name<$tcx> {
357358
type Key = $($K)*;
358359
type Value = $V;
359360
type Stored = <
@@ -372,7 +373,7 @@ macro_rules! define_queries_inner {
372373
type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
373374

374375
#[inline(always)]
375-
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
376+
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query, Self::Cache> {
376377
&tcx.queries.$name
377378
}
378379

@@ -454,7 +455,7 @@ macro_rules! define_queries_inner {
454455
#[inline(always)]
455456
#[must_use]
456457
pub fn $name(self, key: query_helper_param_ty!($($K)*))
457-
-> <queries::$name<$tcx> as QueryConfig<TyCtxt<$tcx>>>::Stored
458+
-> <queries::$name<$tcx> as QueryConfig>::Stored
458459
{
459460
self.at(DUMMY_SP).$name(key.into_query_param())
460461
})*
@@ -493,7 +494,7 @@ macro_rules! define_queries_inner {
493494
$($(#[$attr])*
494495
#[inline(always)]
495496
pub fn $name(self, key: query_helper_param_ty!($($K)*))
496-
-> <queries::$name<$tcx> as QueryConfig<TyCtxt<$tcx>>>::Stored
497+
-> <queries::$name<$tcx> as QueryConfig>::Stored
497498
{
498499
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
499500
})*
@@ -527,7 +528,8 @@ macro_rules! define_queries_struct {
527528
fallback_extern_providers: Box<Providers>,
528529

529530
$($(#[$attr])* $name: QueryState<
530-
TyCtxt<$tcx>,
531+
crate::dep_graph::DepKind,
532+
<TyCtxt<$tcx> as QueryContext>::Query,
531533
<queries::$name<$tcx> as QueryAccessors<TyCtxt<'tcx>>>::Cache,
532534
>,)*
533535
}
@@ -548,7 +550,7 @@ macro_rules! define_queries_struct {
548550

549551
pub(crate) fn try_collect_active_jobs(
550552
&self
551-
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<TyCtxt<'tcx>>>> {
553+
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query>>> {
552554
let mut jobs = FxHashMap::default();
553555

554556
$(

compiler/rustc_middle/src/ty/query/profiling_support.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::profiling::SelfProfiler;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
77
use rustc_hir::definitions::DefPathData;
8-
use rustc_query_system::query::QueryCache;
9-
use rustc_query_system::query::QueryState;
8+
use rustc_query_system::query::{QueryCache, QueryContext, QueryState};
109
use std::fmt::Debug;
1110
use std::io::Write;
1211

@@ -231,7 +230,7 @@ where
231230
pub(super) fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
232231
tcx: TyCtxt<'tcx>,
233232
query_name: &'static str,
234-
query_state: &QueryState<TyCtxt<'tcx>, C>,
233+
query_state: &QueryState<crate::dep_graph::DepKind, <TyCtxt<'tcx> as QueryContext>::Query, C>,
235234
string_cache: &mut QueryKeyStringCache,
236235
) where
237236
C: QueryCache,

compiler/rustc_middle/src/ty/query/stats.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::ty::query::queries;
22
use crate::ty::TyCtxt;
33
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
4-
use rustc_query_system::query::QueryCache;
5-
use rustc_query_system::query::QueryState;
6-
use rustc_query_system::query::{QueryAccessors, QueryContext};
4+
use rustc_query_system::query::{QueryAccessors, QueryCache, QueryContext, QueryState};
75

86
use std::any::type_name;
7+
use std::hash::Hash;
98
use std::mem;
109
#[cfg(debug_assertions)]
1110
use std::sync::atomic::Ordering;
@@ -38,10 +37,12 @@ struct QueryStats {
3837
local_def_id_keys: Option<usize>,
3938
}
4039

41-
fn stats<CTX: QueryContext, C: QueryCache>(
42-
name: &'static str,
43-
map: &QueryState<CTX, C>,
44-
) -> QueryStats {
40+
fn stats<D, Q, C>(name: &'static str, map: &QueryState<D, Q, C>) -> QueryStats
41+
where
42+
D: Copy + Clone + Eq + Hash,
43+
Q: Clone,
44+
C: QueryCache,
45+
{
4546
let mut stats = QueryStats {
4647
name,
4748
#[cfg(debug_assertions)]
@@ -127,7 +128,8 @@ macro_rules! print_stats {
127128

128129
$($(
129130
queries.push(stats::<
130-
TyCtxt<'_>,
131+
crate::dep_graph::DepKind,
132+
<TyCtxt<'_> as QueryContext>::Query,
131133
<queries::$name<'_> as QueryAccessors<TyCtxt<'_>>>::Cache,
132134
>(
133135
stringify!($name),

compiler/rustc_query_system/src/query/caches.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::dep_graph::DepNodeIndex;
22
use crate::query::plumbing::{QueryLookup, QueryState};
3-
use crate::query::QueryContext;
43

54
use rustc_arena::TypedArena;
65
use rustc_data_structures::fx::FxHashMap;
76
use rustc_data_structures::sharded::Sharded;
87
use rustc_data_structures::sync::WorkerLocal;
98
use std::default::Default;
9+
use std::fmt::Debug;
1010
use std::hash::Hash;
1111
use std::marker::PhantomData;
1212

@@ -24,24 +24,24 @@ pub trait QueryStorage: Default {
2424
}
2525

2626
pub trait QueryCache: QueryStorage {
27-
type Key: Hash;
27+
type Key: Hash + Eq + Clone + Debug;
2828
type Sharded: Default;
2929

3030
/// Checks if the query is already computed and in the cache.
3131
/// It returns the shard index and a lock guard to the shard,
3232
/// which will be used if the query is not in the cache and we need
3333
/// to compute it.
34-
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
34+
fn lookup<D, Q, R, OnHit, OnMiss>(
3535
&self,
36-
state: &QueryState<CTX, Self>,
36+
state: &QueryState<D, Q, Self>,
3737
key: Self::Key,
3838
// `on_hit` can be called while holding a lock to the query state shard.
3939
on_hit: OnHit,
4040
on_miss: OnMiss,
4141
) -> R
4242
where
4343
OnHit: FnOnce(&Self::Stored, DepNodeIndex) -> R,
44-
OnMiss: FnOnce(Self::Key, QueryLookup<'_, CTX, Self::Key, Self::Sharded>) -> R;
44+
OnMiss: FnOnce(Self::Key, QueryLookup<'_, D, Q, Self::Key, Self::Sharded>) -> R;
4545

4646
fn complete(
4747
&self,
@@ -86,21 +86,25 @@ impl<K: Eq + Hash, V: Clone> QueryStorage for DefaultCache<K, V> {
8686
}
8787
}
8888

89-
impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
89+
impl<K, V> QueryCache for DefaultCache<K, V>
90+
where
91+
K: Eq + Hash + Clone + Debug,
92+
V: Clone,
93+
{
9094
type Key = K;
9195
type Sharded = FxHashMap<K, (V, DepNodeIndex)>;
9296

9397
#[inline(always)]
94-
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
98+
fn lookup<D, Q, R, OnHit, OnMiss>(
9599
&self,
96-
state: &QueryState<CTX, Self>,
100+
state: &QueryState<D, Q, Self>,
97101
key: K,
98102
on_hit: OnHit,
99103
on_miss: OnMiss,
100104
) -> R
101105
where
102106
OnHit: FnOnce(&V, DepNodeIndex) -> R,
103-
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
107+
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
104108
{
105109
let mut lookup = state.get_lookup(&key);
106110
let lock = &mut *lookup.lock;
@@ -164,21 +168,24 @@ impl<'tcx, K: Eq + Hash, V: 'tcx> QueryStorage for ArenaCache<'tcx, K, V> {
164168
}
165169
}
166170

167-
impl<'tcx, K: Eq + Hash, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V> {
171+
impl<'tcx, K, V: 'tcx> QueryCache for ArenaCache<'tcx, K, V>
172+
where
173+
K: Eq + Hash + Clone + Debug,
174+
{
168175
type Key = K;
169176
type Sharded = FxHashMap<K, &'tcx (V, DepNodeIndex)>;
170177

171178
#[inline(always)]
172-
fn lookup<CTX: QueryContext, R, OnHit, OnMiss>(
179+
fn lookup<D, Q, R, OnHit, OnMiss>(
173180
&self,
174-
state: &QueryState<CTX, Self>,
181+
state: &QueryState<D, Q, Self>,
175182
key: K,
176183
on_hit: OnHit,
177184
on_miss: OnMiss,
178185
) -> R
179186
where
180187
OnHit: FnOnce(&&'tcx V, DepNodeIndex) -> R,
181-
OnMiss: FnOnce(K, QueryLookup<'_, CTX, K, Self::Sharded>) -> R,
188+
OnMiss: FnOnce(K, QueryLookup<'_, D, Q, K, Self::Sharded>) -> R,
182189
{
183190
let mut lookup = state.get_lookup(&key);
184191
let lock = &mut *lookup.lock;

compiler/rustc_query_system/src/query/config.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use std::borrow::Cow;
1212
use std::fmt::Debug;
1313
use std::hash::Hash;
1414

15-
// The parameter `CTX` is required in librustc_middle:
16-
// implementations may need to access the `'tcx` lifetime in `CTX = TyCtxt<'tcx>`.
17-
pub trait QueryConfig<CTX> {
15+
pub trait QueryConfig {
1816
const NAME: &'static str;
1917
const CATEGORY: ProfileCategory;
2018

@@ -70,15 +68,15 @@ impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
7068
}
7169
}
7270

73-
pub trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
71+
pub trait QueryAccessors<CTX: QueryContext>: QueryConfig {
7472
const ANON: bool;
7573
const EVAL_ALWAYS: bool;
7674
const DEP_KIND: CTX::DepKind;
7775

7876
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
7977

8078
// Don't use this method to access query results, instead use the methods on TyCtxt
81-
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
79+
fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX::DepKind, CTX::Query, Self::Cache>;
8280

8381
fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode<CTX::DepKind>
8482
where

0 commit comments

Comments
 (0)