Skip to content

Commit f74fd03

Browse files
committed
Make QueryAccessor argument a type.
1 parent 8c1c90b commit f74fd03

File tree

6 files changed

+127
-89
lines changed

6 files changed

+127
-89
lines changed

src/librustc/ty/query/caches.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) trait QueryCache: Default {
2323
/// to compute it.
2424
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
2525
&self,
26-
state: &'tcx QueryState<'tcx, Self>,
26+
state: &'tcx QueryState<TyCtxt<'tcx>, 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,10 +32,10 @@ pub(crate) trait QueryCache: Default {
3232
) -> R
3333
where
3434
GetCache: for<'a> Fn(
35-
&'a mut QueryStateShard<'tcx, Self::Key, Self::Sharded>,
35+
&'a mut QueryStateShard<TyCtxt<'tcx>, Self::Key, Self::Sharded>,
3636
) -> &'a mut Self::Sharded,
3737
OnHit: FnOnce(&Self::Value, DepNodeIndex) -> R,
38-
OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, Self::Key, Self::Sharded>) -> R;
38+
OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, TyCtxt<'tcx>, Self::Key, Self::Sharded>) -> R;
3939

4040
fn complete(
4141
&self,
@@ -78,17 +78,18 @@ impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> {
7878
#[inline(always)]
7979
fn lookup<'tcx, R, GetCache, OnHit, OnMiss>(
8080
&self,
81-
state: &'tcx QueryState<'tcx, Self>,
81+
state: &'tcx QueryState<TyCtxt<'tcx>, Self>,
8282
get_cache: GetCache,
8383
key: K,
8484
on_hit: OnHit,
8585
on_miss: OnMiss,
8686
) -> R
8787
where
88-
GetCache:
89-
for<'a> Fn(&'a mut QueryStateShard<'tcx, K, Self::Sharded>) -> &'a mut Self::Sharded,
88+
GetCache: for<'a> Fn(
89+
&'a mut QueryStateShard<TyCtxt<'tcx>, K, Self::Sharded>,
90+
) -> &'a mut Self::Sharded,
9091
OnHit: FnOnce(&V, DepNodeIndex) -> R,
91-
OnMiss: FnOnce(K, QueryLookup<'tcx, K, Self::Sharded>) -> R,
92+
OnMiss: FnOnce(K, QueryLookup<'tcx, TyCtxt<'tcx>, K, Self::Sharded>) -> R,
9293
{
9394
let mut lookup = state.get_lookup(&key);
9495
let lock = &mut *lookup.lock;

src/librustc/ty/query/config.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,32 @@ pub trait QueryConfig<CTX> {
2323
type Value: Clone;
2424
}
2525

26-
pub(crate) trait QueryAccessors<'tcx>: QueryConfig<TyCtxt<'tcx>> {
26+
pub trait QueryContext: Copy {
27+
type Query;
28+
}
29+
30+
pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
2731
const ANON: bool;
2832
const EVAL_ALWAYS: bool;
2933
const DEP_KIND: DepKind;
3034

3135
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
3236

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

36-
fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
40+
fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode;
3741

3842
// Don't use this method to compute query results, instead use the methods on TyCtxt
39-
fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
43+
fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
4044

4145
fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
4246
-> Option<Fingerprint>;
4347

44-
fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
48+
fn handle_cycle_error(tcx: CTX, error: CycleError<CTX>) -> Self::Value;
4549
}
4650

47-
pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
51+
pub(crate) trait QueryDescription<'tcx>: QueryAccessors<TyCtxt<'tcx>> {
4852
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
4953

5054
#[inline]
@@ -57,7 +61,11 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5761
}
5862
}
5963

60-
impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
64+
impl<'tcx, M> QueryDescription<'tcx> for M
65+
where
66+
M: QueryAccessors<TyCtxt<'tcx>, Key = DefId>,
67+
//M::Cache: QueryCache<DefId, M::Value>,
68+
{
6169
default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
6270
if !tcx.sess.verbose() {
6371
format!("processing `{}`", tcx.def_path_str(def_id)).into()

src/librustc/ty/query/job.rs

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::dep_graph::DepKind;
22
use crate::ty::context::TyCtxt;
3+
use crate::ty::query::config::QueryContext;
34
use crate::ty::query::plumbing::CycleError;
45
use crate::ty::query::Query;
56
use crate::ty::tls;
@@ -27,13 +28,13 @@ use {
2728

2829
/// Represents a span and a query key.
2930
#[derive(Clone, Debug)]
30-
pub struct QueryInfo<'tcx> {
31+
pub struct QueryInfo<CTX: QueryContext> {
3132
/// The span corresponding to the reason for which this query was required.
3233
pub span: Span,
33-
pub query: Query<'tcx>,
34+
pub query: CTX::Query,
3435
}
3536

36-
type QueryMap<'tcx> = FxHashMap<QueryJobId, QueryJobInfo<'tcx>>;
37+
type QueryMap<'tcx> = FxHashMap<QueryJobId, QueryJobInfo<TyCtxt<'tcx>>>;
3738

3839
/// A value uniquely identifiying an active query job within a shard in the query cache.
3940
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
@@ -72,19 +73,19 @@ impl QueryJobId {
7273
}
7374

7475
#[cfg(parallel_compiler)]
75-
fn latch<'a, 'tcx>(self, map: &'a QueryMap<'tcx>) -> Option<&'a QueryLatch<'tcx>> {
76+
fn latch<'a, 'tcx>(self, map: &'a QueryMap<'tcx>) -> Option<&'a QueryLatch<TyCtxt<'tcx>>> {
7677
map.get(&self).unwrap().job.latch.as_ref()
7778
}
7879
}
7980

80-
pub struct QueryJobInfo<'tcx> {
81-
pub info: QueryInfo<'tcx>,
82-
pub job: QueryJob<'tcx>,
81+
pub struct QueryJobInfo<CTX: QueryContext> {
82+
pub info: QueryInfo<CTX>,
83+
pub job: QueryJob<CTX>,
8384
}
8485

8586
/// Represents an active query job.
8687
#[derive(Clone)]
87-
pub struct QueryJob<'tcx> {
88+
pub struct QueryJob<CTX: QueryContext> {
8889
pub id: QueryShardJobId,
8990

9091
/// The span corresponding to the reason for which this query was required.
@@ -95,12 +96,12 @@ pub struct QueryJob<'tcx> {
9596

9697
/// The latch that is used to wait on this job.
9798
#[cfg(parallel_compiler)]
98-
latch: Option<QueryLatch<'tcx>>,
99+
latch: Option<QueryLatch<CTX>>,
99100

100-
dummy: PhantomData<QueryLatch<'tcx>>,
101+
dummy: PhantomData<QueryLatch<CTX>>,
101102
}
102103

103-
impl<'tcx> QueryJob<'tcx> {
104+
impl<CTX: QueryContext> QueryJob<CTX> {
104105
/// Creates a new query job.
105106
pub fn new(id: QueryShardJobId, span: Span, parent: Option<QueryJobId>) -> Self {
106107
QueryJob {
@@ -114,15 +115,15 @@ impl<'tcx> QueryJob<'tcx> {
114115
}
115116

116117
#[cfg(parallel_compiler)]
117-
pub(super) fn latch(&mut self, _id: QueryJobId) -> QueryLatch<'tcx> {
118+
pub(super) fn latch(&mut self, _id: QueryJobId) -> QueryLatch<CTX> {
118119
if self.latch.is_none() {
119120
self.latch = Some(QueryLatch::new());
120121
}
121122
self.latch.as_ref().unwrap().clone()
122123
}
123124

124125
#[cfg(not(parallel_compiler))]
125-
pub(super) fn latch(&mut self, id: QueryJobId) -> QueryLatch<'tcx> {
126+
pub(super) fn latch(&mut self, id: QueryJobId) -> QueryLatch<CTX> {
126127
QueryLatch { id, dummy: PhantomData }
127128
}
128129

@@ -138,14 +139,18 @@ impl<'tcx> QueryJob<'tcx> {
138139

139140
#[cfg(not(parallel_compiler))]
140141
#[derive(Clone)]
141-
pub(super) struct QueryLatch<'tcx> {
142+
pub(super) struct QueryLatch<CTX> {
142143
id: QueryJobId,
143-
dummy: PhantomData<&'tcx ()>,
144+
dummy: PhantomData<CTX>,
144145
}
145146

146147
#[cfg(not(parallel_compiler))]
147-
impl<'tcx> QueryLatch<'tcx> {
148-
pub(super) fn find_cycle_in_stack(&self, tcx: TyCtxt<'tcx>, span: Span) -> CycleError<'tcx> {
148+
impl<'tcx> QueryLatch<TyCtxt<'tcx>> {
149+
pub(super) fn find_cycle_in_stack(
150+
&self,
151+
tcx: TyCtxt<'tcx>,
152+
span: Span,
153+
) -> CycleError<TyCtxt<'tcx>> {
149154
let query_map = tcx.queries.try_collect_active_jobs().unwrap();
150155

151156
// Get the current executing query (waiter) and find the waitee amongst its parents
@@ -181,44 +186,50 @@ impl<'tcx> QueryLatch<'tcx> {
181186
}
182187

183188
#[cfg(parallel_compiler)]
184-
struct QueryWaiter<'tcx> {
189+
struct QueryWaiter<CTX: QueryContext> {
185190
query: Option<QueryJobId>,
186191
condvar: Condvar,
187192
span: Span,
188-
cycle: Lock<Option<CycleError<'tcx>>>,
193+
cycle: Lock<Option<CycleError<CTX>>>,
189194
}
190195

191196
#[cfg(parallel_compiler)]
192-
impl<'tcx> QueryWaiter<'tcx> {
197+
impl<CTX: QueryContext> QueryWaiter<CTX> {
193198
fn notify(&self, registry: &rayon_core::Registry) {
194199
rayon_core::mark_unblocked(registry);
195200
self.condvar.notify_one();
196201
}
197202
}
198203

199204
#[cfg(parallel_compiler)]
200-
struct QueryLatchInfo<'tcx> {
205+
struct QueryLatchInfo<CTX: QueryContext> {
201206
complete: bool,
202-
waiters: Vec<Lrc<QueryWaiter<'tcx>>>,
207+
waiters: Vec<Lrc<QueryWaiter<CTX>>>,
203208
}
204209

205210
#[cfg(parallel_compiler)]
206211
#[derive(Clone)]
207-
pub(super) struct QueryLatch<'tcx> {
208-
info: Lrc<Mutex<QueryLatchInfo<'tcx>>>,
212+
pub(super) struct QueryLatch<CTX: QueryContext> {
213+
info: Lrc<Mutex<QueryLatchInfo<CTX>>>,
209214
}
210215

211216
#[cfg(parallel_compiler)]
212-
impl<'tcx> QueryLatch<'tcx> {
217+
impl<CTX: QueryContext> QueryLatch<CTX> {
213218
fn new() -> Self {
214219
QueryLatch {
215220
info: Lrc::new(Mutex::new(QueryLatchInfo { complete: false, waiters: Vec::new() })),
216221
}
217222
}
223+
}
218224

225+
#[cfg(parallel_compiler)]
226+
impl<'tcx> QueryLatch<TyCtxt<'tcx>> {
219227
/// Awaits for the query job to complete.
220-
#[cfg(parallel_compiler)]
221-
pub(super) fn wait_on(&self, tcx: TyCtxt<'tcx>, span: Span) -> Result<(), CycleError<'tcx>> {
228+
pub(super) fn wait_on(
229+
&self,
230+
tcx: TyCtxt<'tcx>,
231+
span: Span,
232+
) -> Result<(), CycleError<TyCtxt<'tcx>>> {
222233
tls::with_related_context(tcx, move |icx| {
223234
let waiter = Lrc::new(QueryWaiter {
224235
query: icx.query,
@@ -237,9 +248,12 @@ impl<'tcx> QueryLatch<'tcx> {
237248
}
238249
})
239250
}
251+
}
240252

253+
#[cfg(parallel_compiler)]
254+
impl<CTX: QueryContext> QueryLatch<CTX> {
241255
/// Awaits the caller on this latch by blocking the current thread.
242-
fn wait_on_inner(&self, waiter: &Lrc<QueryWaiter<'tcx>>) {
256+
fn wait_on_inner(&self, waiter: &Lrc<QueryWaiter<CTX>>) {
243257
let mut info = self.info.lock();
244258
if !info.complete {
245259
// We push the waiter on to the `waiters` list. It can be accessed inside
@@ -273,7 +287,7 @@ impl<'tcx> QueryLatch<'tcx> {
273287

274288
/// Removes a single waiter from the list of waiters.
275289
/// This is used to break query cycles.
276-
fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter<'tcx>> {
290+
fn extract_waiter(&self, waiter: usize) -> Lrc<QueryWaiter<CTX>> {
277291
let mut info = self.info.lock();
278292
debug_assert!(!info.complete);
279293
// Remove the waiter from the list of waiters
@@ -427,7 +441,7 @@ fn pick_query<'a, 'tcx, T, F: Fn(&T) -> (Span, QueryJobId)>(
427441
fn remove_cycle<'tcx>(
428442
query_map: &QueryMap<'tcx>,
429443
jobs: &mut Vec<QueryJobId>,
430-
wakelist: &mut Vec<Lrc<QueryWaiter<'tcx>>>,
444+
wakelist: &mut Vec<Lrc<QueryWaiter<TyCtxt<'tcx>>>>,
431445
tcx: TyCtxt<'tcx>,
432446
) -> bool {
433447
let mut visited = FxHashSet::default();

0 commit comments

Comments
 (0)