Skip to content

Commit c6936c3

Browse files
committed
Auto merge of rust-lang#152371 - nnethercote:rm-QueryDispatcher, r=Zalathar
Remove `QueryDispatcher` `QueryDispatcher` is a trait that existed purely because `rustc_query_system` had code that didn't have access to `TyCtxt`. That is no longer the case, so the trait can be removed. r? @Zalathar
2 parents 4cd4c18 + f995804 commit c6936c3

File tree

8 files changed

+160
-266
lines changed

8 files changed

+160
-266
lines changed

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use std::ops::Deref;
22

3+
use rustc_data_structures::fingerprint::Fingerprint;
34
use rustc_data_structures::sync::{AtomicU64, WorkerLocal};
45
use rustc_hir::def_id::{DefId, LocalDefId};
56
use rustc_hir::hir_id::OwnerId;
67
use rustc_macros::HashStable;
78
use rustc_query_system::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
9+
use rustc_query_system::ich::StableHashingContext;
810
pub(crate) use rustc_query_system::query::QueryJobId;
9-
use rustc_query_system::query::{CycleError, CycleErrorHandling, HashResult, QueryCache};
11+
use rustc_query_system::query::{CycleError, CycleErrorHandling, QueryCache};
1012
use rustc_span::{ErrorGuaranteed, Span};
1113
pub use sealed::IntoQueryParam;
1214

@@ -30,6 +32,8 @@ pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
3032
pub type IsLoadableFromDiskFn<'tcx, Key> =
3133
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
3234

35+
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
36+
3337
/// Stores function pointers and other metadata for a particular query.
3438
///
3539
/// Used indirectly by query plumbing in `rustc_query_system` via a trait,

compiler/rustc_query_impl/src/execution.rs

Lines changed: 64 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use rustc_middle::dep_graph::DepsType;
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_query_system::dep_graph::{DepGraphData, DepNodeKey, HasDepContext};
1111
use rustc_query_system::query::{
12-
ActiveKeyStatus, CycleError, CycleErrorHandling, QueryCache, QueryContext, QueryDispatcher,
13-
QueryJob, QueryJobId, QueryJobInfo, QueryLatch, QueryMap, QueryMode, QueryStackDeferred,
14-
QueryStackFrame, QueryState, incremental_verify_ich, report_cycle,
12+
ActiveKeyStatus, CycleError, CycleErrorHandling, QueryCache, QueryContext, QueryJob,
13+
QueryJobId, QueryJobInfo, QueryLatch, QueryMap, QueryMode, QueryStackDeferred, QueryStackFrame,
14+
QueryState, incremental_verify_ich, report_cycle,
1515
};
1616
use rustc_span::{DUMMY_SP, Span};
1717

1818
use crate::dep_graph::{DepContext, DepNode, DepNodeIndex};
19-
use crate::plumbing::QueryCtxt;
19+
use crate::{QueryCtxt, QueryFlags, SemiDynamicQueryDispatcher};
2020

2121
#[inline]
2222
fn equivalent_key<K: Eq, V>(k: &K) -> impl Fn(&(K, V)) -> bool + '_ {
@@ -95,23 +95,21 @@ where
9595

9696
#[cold]
9797
#[inline(never)]
98-
fn mk_cycle<'tcx, Q>(query: Q, qcx: QueryCtxt<'tcx>, cycle_error: CycleError) -> Q::Value
99-
where
100-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
101-
{
98+
fn mk_cycle<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
99+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
100+
qcx: QueryCtxt<'tcx>,
101+
cycle_error: CycleError,
102+
) -> C::Value {
102103
let error = report_cycle(qcx.tcx.sess, &cycle_error);
103104
handle_cycle_error(query, qcx, &cycle_error, error)
104105
}
105106

106-
fn handle_cycle_error<'tcx, Q>(
107-
query: Q,
107+
fn handle_cycle_error<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
108+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
108109
qcx: QueryCtxt<'tcx>,
109110
cycle_error: &CycleError,
110111
error: Diag<'_>,
111-
) -> Q::Value
112-
where
113-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
114-
{
112+
) -> C::Value {
115113
match query.cycle_error_handling() {
116114
CycleErrorHandling::Error => {
117115
let guar = error.emit();
@@ -205,15 +203,12 @@ where
205203

206204
#[cold]
207205
#[inline(never)]
208-
fn cycle_error<'tcx, Q>(
209-
query: Q,
206+
fn cycle_error<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
207+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
210208
qcx: QueryCtxt<'tcx>,
211209
try_execute: QueryJobId,
212210
span: Span,
213-
) -> (Q::Value, Option<DepNodeIndex>)
214-
where
215-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
216-
{
211+
) -> (C::Value, Option<DepNodeIndex>) {
217212
// Ensure there was no errors collecting all active jobs.
218213
// We need the complete map to ensure we find a cycle to break.
219214
let query_map = qcx
@@ -226,17 +221,14 @@ where
226221
}
227222

228223
#[inline(always)]
229-
fn wait_for_query<'tcx, Q>(
230-
query: Q,
224+
fn wait_for_query<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
225+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
231226
qcx: QueryCtxt<'tcx>,
232227
span: Span,
233-
key: Q::Key,
228+
key: C::Key,
234229
latch: QueryLatch<'tcx>,
235230
current: Option<QueryJobId>,
236-
) -> (Q::Value, Option<DepNodeIndex>)
237-
where
238-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
239-
{
231+
) -> (C::Value, Option<DepNodeIndex>) {
240232
// For parallel queries, we'll block and wait until the query running
241233
// in another thread has completed. Record how long we wait in the
242234
// self-profiler.
@@ -275,16 +267,13 @@ where
275267
}
276268

277269
#[inline(never)]
278-
fn try_execute_query<'tcx, Q, const INCR: bool>(
279-
query: Q,
270+
fn try_execute_query<'tcx, C: QueryCache, const FLAGS: QueryFlags, const INCR: bool>(
271+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
280272
qcx: QueryCtxt<'tcx>,
281273
span: Span,
282-
key: Q::Key,
274+
key: C::Key,
283275
dep_node: Option<DepNode>,
284-
) -> (Q::Value, Option<DepNodeIndex>)
285-
where
286-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
287-
{
276+
) -> (C::Value, Option<DepNodeIndex>) {
288277
let state = query.query_state(qcx);
289278
let key_hash = sharded::make_hash(&key);
290279
let mut state_lock = state.active.lock_shard_by_hash(key_hash);
@@ -315,7 +304,7 @@ where
315304
// Drop the lock before we start executing the query
316305
drop(state_lock);
317306

318-
execute_job::<Q, INCR>(query, qcx, state, key, key_hash, id, dep_node)
307+
execute_job::<C, FLAGS, INCR>(query, qcx, state, key, key_hash, id, dep_node)
319308
}
320309
Entry::Occupied(mut entry) => {
321310
match &mut entry.get_mut().1 {
@@ -344,18 +333,15 @@ where
344333
}
345334

346335
#[inline(always)]
347-
fn execute_job<'tcx, Q, const INCR: bool>(
348-
query: Q,
336+
fn execute_job<'tcx, C: QueryCache, const FLAGS: QueryFlags, const INCR: bool>(
337+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
349338
qcx: QueryCtxt<'tcx>,
350-
state: &'tcx QueryState<'tcx, Q::Key>,
351-
key: Q::Key,
339+
state: &'tcx QueryState<'tcx, C::Key>,
340+
key: C::Key,
352341
key_hash: u64,
353342
id: QueryJobId,
354343
dep_node: Option<DepNode>,
355-
) -> (Q::Value, Option<DepNodeIndex>)
356-
where
357-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
358-
{
344+
) -> (C::Value, Option<DepNodeIndex>) {
359345
// Use `JobOwner` so the query will be poisoned if executing it panics.
360346
let job_owner = JobOwner { state, key };
361347

@@ -409,15 +395,12 @@ where
409395

410396
// Fast path for when incr. comp. is off.
411397
#[inline(always)]
412-
fn execute_job_non_incr<'tcx, Q>(
413-
query: Q,
398+
fn execute_job_non_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
399+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
414400
qcx: QueryCtxt<'tcx>,
415-
key: Q::Key,
401+
key: C::Key,
416402
job_id: QueryJobId,
417-
) -> (Q::Value, DepNodeIndex)
418-
where
419-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
420-
{
403+
) -> (C::Value, DepNodeIndex) {
421404
debug_assert!(!qcx.tcx.dep_graph.is_fully_enabled());
422405

423406
// Fingerprint the key, just to assert that it doesn't
@@ -445,17 +428,14 @@ where
445428
}
446429

447430
#[inline(always)]
448-
fn execute_job_incr<'tcx, Q>(
449-
query: Q,
431+
fn execute_job_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
432+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
450433
qcx: QueryCtxt<'tcx>,
451434
dep_graph_data: &DepGraphData<DepsType>,
452-
key: Q::Key,
435+
key: C::Key,
453436
mut dep_node_opt: Option<DepNode>,
454437
job_id: QueryJobId,
455-
) -> (Q::Value, DepNodeIndex)
456-
where
457-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
458-
{
438+
) -> (C::Value, DepNodeIndex) {
459439
if !query.anon() && !query.eval_always() {
460440
// `to_dep_node` is expensive for some `DepKind`s.
461441
let dep_node = dep_node_opt.get_or_insert_with(|| query.construct_dep_node(qcx.tcx, &key));
@@ -495,16 +475,13 @@ where
495475
}
496476

497477
#[inline(always)]
498-
fn try_load_from_disk_and_cache_in_memory<'tcx, Q>(
499-
query: Q,
478+
fn try_load_from_disk_and_cache_in_memory<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
479+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
500480
dep_graph_data: &DepGraphData<DepsType>,
501481
qcx: QueryCtxt<'tcx>,
502-
key: &Q::Key,
482+
key: &C::Key,
503483
dep_node: &DepNode,
504-
) -> Option<(Q::Value, DepNodeIndex)>
505-
where
506-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
507-
{
484+
) -> Option<(C::Value, DepNodeIndex)> {
508485
// Note this function can be called concurrently from the same query
509486
// We must ensure that this is handled correctly.
510487

@@ -598,15 +575,12 @@ where
598575
///
599576
/// Note: The optimization is only available during incr. comp.
600577
#[inline(never)]
601-
fn ensure_must_run<'tcx, Q>(
602-
query: Q,
578+
fn ensure_must_run<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
579+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
603580
qcx: QueryCtxt<'tcx>,
604-
key: &Q::Key,
581+
key: &C::Key,
605582
check_cache: bool,
606-
) -> (bool, Option<DepNode>)
607-
where
608-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
609-
{
583+
) -> (bool, Option<DepNode>) {
610584
if query.eval_always() {
611585
return (true, None);
612586
}
@@ -644,31 +618,25 @@ where
644618
}
645619

646620
#[inline(always)]
647-
pub(super) fn get_query_non_incr<'tcx, Q>(
648-
query: Q,
621+
pub(super) fn get_query_non_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
622+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
649623
qcx: QueryCtxt<'tcx>,
650624
span: Span,
651-
key: Q::Key,
652-
) -> Q::Value
653-
where
654-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
655-
{
625+
key: C::Key,
626+
) -> C::Value {
656627
debug_assert!(!qcx.tcx.dep_graph.is_fully_enabled());
657628

658-
ensure_sufficient_stack(|| try_execute_query::<Q, false>(query, qcx, span, key, None).0)
629+
ensure_sufficient_stack(|| try_execute_query::<C, FLAGS, false>(query, qcx, span, key, None).0)
659630
}
660631

661632
#[inline(always)]
662-
pub(super) fn get_query_incr<'tcx, Q>(
663-
query: Q,
633+
pub(super) fn get_query_incr<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
634+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
664635
qcx: QueryCtxt<'tcx>,
665636
span: Span,
666-
key: Q::Key,
637+
key: C::Key,
667638
mode: QueryMode,
668-
) -> Option<Q::Value>
669-
where
670-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
671-
{
639+
) -> Option<C::Value> {
672640
debug_assert!(qcx.tcx.dep_graph.is_fully_enabled());
673641

674642
let dep_node = if let QueryMode::Ensure { check_cache } = mode {
@@ -681,18 +649,21 @@ where
681649
None
682650
};
683651

684-
let (result, dep_node_index) =
685-
ensure_sufficient_stack(|| try_execute_query::<Q, true>(query, qcx, span, key, dep_node));
652+
let (result, dep_node_index) = ensure_sufficient_stack(|| {
653+
try_execute_query::<C, FLAGS, true>(query, qcx, span, key, dep_node)
654+
});
686655
if let Some(dep_node_index) = dep_node_index {
687656
qcx.tcx.dep_graph.read_index(dep_node_index)
688657
}
689658
Some(result)
690659
}
691660

692-
pub(super) fn force_query<'tcx, Q>(query: Q, qcx: QueryCtxt<'tcx>, key: Q::Key, dep_node: DepNode)
693-
where
694-
Q: QueryDispatcher<'tcx, Qcx = QueryCtxt<'tcx>>,
695-
{
661+
pub(crate) fn force_query<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
662+
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
663+
qcx: QueryCtxt<'tcx>,
664+
key: C::Key,
665+
dep_node: DepNode,
666+
) {
696667
// We may be concurrently trying both execute and force a query.
697668
// Ensure that only one of them runs the query.
698669
if let Some((_, index)) = query.query_cache(qcx).lookup(&key) {
@@ -703,6 +674,6 @@ where
703674
debug_assert!(!query.anon());
704675

705676
ensure_sufficient_stack(|| {
706-
try_execute_query::<Q, true>(query, qcx, DUMMY_SP, key, Some(dep_node))
677+
try_execute_query::<C, FLAGS, true>(query, qcx, DUMMY_SP, key, Some(dep_node))
707678
});
708679
}

0 commit comments

Comments
 (0)