Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d6f99e5

Browse files
committedJan 2, 2023
Auto merge of #106307 - Nilstrieb:dynamic->static, r=cjgillot
Abolish `QueryVTable` in favour of more assoc items on `QueryConfig` This may introduce additional mono _but_ may help const fold things better and especially may help not constructing a `QueryVTable` anymore which is cheap but not free.
2 parents 23b1cc1 + 9fe4efe commit d6f99e5

File tree

5 files changed

+95
-114
lines changed

5 files changed

+95
-114
lines changed
 

‎compiler/rustc_query_impl/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc_query_system::query::*;
3434
pub use rustc_query_system::query::{deadlock, QueryContext};
3535

3636
pub use rustc_query_system::query::QueryConfig;
37-
pub(crate) use rustc_query_system::query::QueryVTable;
3837

3938
mod on_disk_cache;
4039
pub use on_disk_cache::OnDiskCache;

‎compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -493,28 +493,32 @@ macro_rules! define_queries {
493493
&tcx.query_caches.$name
494494
}
495495

496+
fn execute_query(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Stored {
497+
tcx.$name(key)
498+
}
499+
496500
#[inline]
497-
fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
498-
QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
499-
{
500-
let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
501-
let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
502-
QueryVTable {
503-
anon: is_anon!([$($modifiers)*]),
504-
eval_always: is_eval_always!([$($modifiers)*]),
505-
depth_limit: depth_limit!([$($modifiers)*]),
506-
feedable: feedable!([$($modifiers)*]),
507-
dep_kind: dep_graph::DepKind::$name,
508-
hash_result: hash_result!([$($modifiers)*]),
509-
handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
510-
compute,
511-
try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None },
512-
}
501+
// key is only sometimes used
502+
#[allow(unused_variables)]
503+
fn compute(qcx: QueryCtxt<'tcx>, key: &Self::Key) -> fn(TyCtxt<'tcx>, Self::Key) -> Self::Value {
504+
get_provider!([$($modifiers)*][qcx, $name, key])
513505
}
514506

515-
fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
516-
tcx.$name(k)
507+
#[inline]
508+
fn try_load_from_disk(qcx: QueryCtxt<'tcx>, key: &Self::Key) -> rustc_query_system::query::TryLoadFromDisk<QueryCtxt<'tcx>, Self> {
509+
let cache_on_disk = Self::cache_on_disk(qcx.tcx, key);
510+
if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None }
517511
}
512+
513+
const ANON: bool = is_anon!([$($modifiers)*]);
514+
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
515+
const DEPTH_LIMIT: bool = depth_limit!([$($modifiers)*]);
516+
const FEEDABLE: bool = feedable!([$($modifiers)*]);
517+
518+
const DEP_KIND: rustc_middle::dep_graph::DepKind = dep_graph::DepKind::$name;
519+
const HANDLE_CYCLE_ERROR: rustc_query_system::HandleCycleError = handle_cycle_error!([$($modifiers)*]);
520+
521+
const HASH_RESULT: rustc_query_system::query::HashResult<QueryCtxt<'tcx>, Self> = hash_result!([$($modifiers)*]);
518522
})*
519523

520524
#[allow(nonstandard_style)]
Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Query configuration and description traits.
22
3-
use crate::dep_graph::DepNode;
4-
use crate::dep_graph::SerializedDepNodeIndex;
3+
use crate::dep_graph::{DepNode, DepNodeParams, SerializedDepNodeIndex};
54
use crate::error::HandleCycleError;
65
use crate::ich::StableHashingContext;
76
use crate::query::caches::QueryCache;
@@ -11,10 +10,16 @@ use rustc_data_structures::fingerprint::Fingerprint;
1110
use std::fmt::Debug;
1211
use std::hash::Hash;
1312

13+
pub type HashResult<Qcx, Q> =
14+
Option<fn(&mut StableHashingContext<'_>, &<Q as QueryConfig<Qcx>>::Value) -> Fingerprint>;
15+
16+
pub type TryLoadFromDisk<Qcx, Q> =
17+
Option<fn(Qcx, SerializedDepNodeIndex) -> Option<<Q as QueryConfig<Qcx>>::Value>>;
18+
1419
pub trait QueryConfig<Qcx: QueryContext> {
1520
const NAME: &'static str;
1621

17-
type Key: Eq + Hash + Clone + Debug;
22+
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Clone + Debug;
1823
type Value: Debug;
1924
type Stored: Debug + Clone + std::borrow::Borrow<Self::Value>;
2025

@@ -30,39 +35,27 @@ pub trait QueryConfig<Qcx: QueryContext> {
3035
where
3136
Qcx: 'a;
3237

33-
// Don't use this method to compute query results, instead use the methods on TyCtxt
34-
fn make_vtable(tcx: Qcx, key: &Self::Key) -> QueryVTable<Qcx, Self::Key, Self::Value>;
35-
3638
fn cache_on_disk(tcx: Qcx::DepContext, key: &Self::Key) -> bool;
3739

3840
// Don't use this method to compute query results, instead use the methods on TyCtxt
3941
fn execute_query(tcx: Qcx::DepContext, k: Self::Key) -> Self::Stored;
40-
}
4142

42-
#[derive(Copy, Clone)]
43-
pub struct QueryVTable<Qcx: QueryContext, K, V> {
44-
pub anon: bool,
45-
pub dep_kind: Qcx::DepKind,
46-
pub eval_always: bool,
47-
pub depth_limit: bool,
48-
pub feedable: bool,
49-
50-
pub compute: fn(Qcx::DepContext, K) -> V,
51-
pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
52-
pub handle_cycle_error: HandleCycleError,
53-
// NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
54-
pub try_load_from_disk: Option<fn(Qcx, SerializedDepNodeIndex) -> Option<V>>,
55-
}
43+
fn compute(tcx: Qcx, key: &Self::Key) -> fn(Qcx::DepContext, Self::Key) -> Self::Value;
5644

57-
impl<Qcx: QueryContext, K, V> QueryVTable<Qcx, K, V> {
58-
pub(crate) fn to_dep_node(&self, tcx: Qcx::DepContext, key: &K) -> DepNode<Qcx::DepKind>
59-
where
60-
K: crate::dep_graph::DepNodeParams<Qcx::DepContext>,
61-
{
62-
DepNode::construct(tcx, self.dep_kind, key)
63-
}
45+
fn try_load_from_disk(qcx: Qcx, idx: &Self::Key) -> TryLoadFromDisk<Qcx, Self>;
46+
47+
const ANON: bool;
48+
const EVAL_ALWAYS: bool;
49+
const DEPTH_LIMIT: bool;
50+
const FEEDABLE: bool;
51+
52+
const DEP_KIND: Qcx::DepKind;
53+
const HANDLE_CYCLE_ERROR: HandleCycleError;
54+
55+
const HASH_RESULT: HashResult<Qcx, Self>;
6456

65-
pub(crate) fn compute(&self, tcx: Qcx::DepContext, key: K) -> V {
66-
(self.compute)(tcx, key)
57+
// Just here for convernience and checking that the key matches the kind, don't override this.
58+
fn construct_dep_node(tcx: Qcx::DepContext, key: &Self::Key) -> DepNode<Qcx::DepKind> {
59+
DepNode::construct(tcx, Self::DEP_KIND, key)
6760
}
6861
}

‎compiler/rustc_query_system/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use self::caches::{
1212
};
1313

1414
mod config;
15-
pub use self::config::{QueryConfig, QueryVTable};
15+
pub use self::config::{HashResult, QueryConfig, TryLoadFromDisk};
1616

1717
use crate::dep_graph::DepKind;
1818
use crate::dep_graph::{DepNodeIndex, HasDepContext, SerializedDepNodeIndex};

‎compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
//! generate the actual methods on tcx which find and execute the provider,
33
//! manage the caches, and so forth.
44
5-
use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex, DepNodeParams};
5+
use crate::dep_graph::{DepContext, DepKind, DepNode, DepNodeIndex};
66
use crate::ich::StableHashingContext;
77
use crate::query::caches::QueryCache;
8-
use crate::query::config::QueryVTable;
98
use crate::query::job::{report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo};
109
use crate::query::{QueryContext, QueryMap, QuerySideEffects, QueryStackFrame};
1110
use crate::values::Value;
@@ -361,44 +360,42 @@ where
361360
})
362361
}
363362

364-
fn try_execute_query<Qcx, C>(
363+
fn try_execute_query<Q, Qcx>(
365364
qcx: Qcx,
366-
state: &QueryState<C::Key, Qcx::DepKind>,
367-
cache: &C,
365+
state: &QueryState<Q::Key, Qcx::DepKind>,
366+
cache: &Q::Cache,
368367
span: Span,
369-
key: C::Key,
368+
key: Q::Key,
370369
dep_node: Option<DepNode<Qcx::DepKind>>,
371-
query: &QueryVTable<Qcx, C::Key, C::Value>,
372-
) -> (C::Stored, Option<DepNodeIndex>)
370+
) -> (Q::Stored, Option<DepNodeIndex>)
373371
where
374-
C: QueryCache,
375-
C::Key: Clone + DepNodeParams<Qcx::DepContext>,
376-
C::Value: Value<Qcx::DepContext, Qcx::DepKind>,
377-
C::Stored: Debug + std::borrow::Borrow<C::Value>,
372+
Q: QueryConfig<Qcx>,
378373
Qcx: QueryContext,
379374
{
380-
match JobOwner::<'_, C::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) {
375+
match JobOwner::<'_, Q::Key, Qcx::DepKind>::try_start(&qcx, state, span, key.clone()) {
381376
TryGetJob::NotYetStarted(job) => {
382-
let (result, dep_node_index) = execute_job(qcx, key.clone(), dep_node, query, job.id);
383-
if query.feedable {
377+
let (result, dep_node_index) =
378+
execute_job::<Q, Qcx>(qcx, key.clone(), dep_node, job.id);
379+
if Q::FEEDABLE {
384380
// We may have put a value inside the cache from inside the execution.
385381
// Verify that it has the same hash as what we have now, to ensure consistency.
386382
let _ = cache.lookup(&key, |cached_result, _| {
387-
let hasher = query.hash_result.expect("feedable forbids no_hash");
383+
let hasher = Q::HASH_RESULT.expect("feedable forbids no_hash");
384+
388385
let old_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, cached_result.borrow()));
389386
let new_hash = qcx.dep_context().with_stable_hashing_context(|mut hcx| hasher(&mut hcx, &result));
390387
debug_assert_eq!(
391388
old_hash, new_hash,
392389
"Computed query value for {:?}({:?}) is inconsistent with fed value,\ncomputed={:#?}\nfed={:#?}",
393-
query.dep_kind, key, result, cached_result,
390+
Q::DEP_KIND, key, result, cached_result,
394391
);
395392
});
396393
}
397394
let result = job.complete(cache, result, dep_node_index);
398395
(result, Some(dep_node_index))
399396
}
400397
TryGetJob::Cycle(error) => {
401-
let result = mk_cycle(qcx, error, query.handle_cycle_error, cache);
398+
let result = mk_cycle(qcx, error, Q::HANDLE_CYCLE_ERROR, cache);
402399
(result, None)
403400
}
404401
#[cfg(parallel_compiler)]
@@ -417,40 +414,38 @@ where
417414
}
418415
}
419416

420-
fn execute_job<Qcx, K, V>(
417+
fn execute_job<Q, Qcx>(
421418
qcx: Qcx,
422-
key: K,
419+
key: Q::Key,
423420
mut dep_node_opt: Option<DepNode<Qcx::DepKind>>,
424-
query: &QueryVTable<Qcx, K, V>,
425421
job_id: QueryJobId,
426-
) -> (V, DepNodeIndex)
422+
) -> (Q::Value, DepNodeIndex)
427423
where
428-
K: Clone + DepNodeParams<Qcx::DepContext>,
429-
V: Debug,
424+
Q: QueryConfig<Qcx>,
430425
Qcx: QueryContext,
431426
{
432427
let dep_graph = qcx.dep_context().dep_graph();
433428

434429
// Fast path for when incr. comp. is off.
435430
if !dep_graph.is_fully_enabled() {
436431
let prof_timer = qcx.dep_context().profiler().query_provider();
437-
let result = qcx.start_query(job_id, query.depth_limit, None, || {
438-
query.compute(*qcx.dep_context(), key)
432+
let result = qcx.start_query(job_id, Q::DEPTH_LIMIT, None, || {
433+
Q::compute(qcx, &key)(*qcx.dep_context(), key)
439434
});
440435
let dep_node_index = dep_graph.next_virtual_depnode_index();
441436
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
442437
return (result, dep_node_index);
443438
}
444439

445-
if !query.anon && !query.eval_always {
440+
if !Q::ANON && !Q::EVAL_ALWAYS {
446441
// `to_dep_node` is expensive for some `DepKind`s.
447442
let dep_node =
448-
dep_node_opt.get_or_insert_with(|| query.to_dep_node(*qcx.dep_context(), &key));
443+
dep_node_opt.get_or_insert_with(|| Q::construct_dep_node(*qcx.dep_context(), &key));
449444

450445
// The diagnostics for this query will be promoted to the current session during
451446
// `try_mark_green()`, so we can ignore them here.
452447
if let Some(ret) = qcx.start_query(job_id, false, None, || {
453-
try_load_from_disk_and_cache_in_memory(qcx, &key, &dep_node, query)
448+
try_load_from_disk_and_cache_in_memory::<Q, Qcx>(qcx, &key, &dep_node)
454449
}) {
455450
return ret;
456451
}
@@ -460,18 +455,19 @@ where
460455
let diagnostics = Lock::new(ThinVec::new());
461456

462457
let (result, dep_node_index) =
463-
qcx.start_query(job_id, query.depth_limit, Some(&diagnostics), || {
464-
if query.anon {
465-
return dep_graph.with_anon_task(*qcx.dep_context(), query.dep_kind, || {
466-
query.compute(*qcx.dep_context(), key)
458+
qcx.start_query(job_id, Q::DEPTH_LIMIT, Some(&diagnostics), || {
459+
if Q::ANON {
460+
return dep_graph.with_anon_task(*qcx.dep_context(), Q::DEP_KIND, || {
461+
Q::compute(qcx, &key)(*qcx.dep_context(), key)
467462
});
468463
}
469464

470465
// `to_dep_node` is expensive for some `DepKind`s.
471466
let dep_node =
472-
dep_node_opt.unwrap_or_else(|| query.to_dep_node(*qcx.dep_context(), &key));
467+
dep_node_opt.unwrap_or_else(|| Q::construct_dep_node(*qcx.dep_context(), &key));
473468

474-
dep_graph.with_task(dep_node, *qcx.dep_context(), key, query.compute, query.hash_result)
469+
let task = Q::compute(qcx, &key);
470+
dep_graph.with_task(dep_node, *qcx.dep_context(), key, task, Q::HASH_RESULT)
475471
});
476472

477473
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
@@ -480,7 +476,7 @@ where
480476
let side_effects = QuerySideEffects { diagnostics };
481477

482478
if std::intrinsics::unlikely(!side_effects.is_empty()) {
483-
if query.anon {
479+
if Q::ANON {
484480
qcx.store_side_effects_for_anon_node(dep_node_index, side_effects);
485481
} else {
486482
qcx.store_side_effects(dep_node_index, side_effects);
@@ -490,16 +486,14 @@ where
490486
(result, dep_node_index)
491487
}
492488

493-
fn try_load_from_disk_and_cache_in_memory<Qcx, K, V>(
489+
fn try_load_from_disk_and_cache_in_memory<Q, Qcx>(
494490
qcx: Qcx,
495-
key: &K,
491+
key: &Q::Key,
496492
dep_node: &DepNode<Qcx::DepKind>,
497-
query: &QueryVTable<Qcx, K, V>,
498-
) -> Option<(V, DepNodeIndex)>
493+
) -> Option<(Q::Value, DepNodeIndex)>
499494
where
500-
K: Clone,
495+
Q: QueryConfig<Qcx>,
501496
Qcx: QueryContext,
502-
V: Debug,
503497
{
504498
// Note this function can be called concurrently from the same query
505499
// We must ensure that this is handled correctly.
@@ -511,7 +505,7 @@ where
511505

512506
// First we try to load the result from the on-disk cache.
513507
// Some things are never cached on disk.
514-
if let Some(try_load_from_disk) = query.try_load_from_disk {
508+
if let Some(try_load_from_disk) = Q::try_load_from_disk(qcx, &key) {
515509
let prof_timer = qcx.dep_context().profiler().incr_cache_loading();
516510

517511
// The call to `with_query_deserialization` enforces that no new `DepNodes`
@@ -545,7 +539,7 @@ where
545539
if std::intrinsics::unlikely(
546540
try_verify || qcx.dep_context().sess().opts.unstable_opts.incremental_verify_ich,
547541
) {
548-
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query.hash_result);
542+
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, Q::HASH_RESULT);
549543
}
550544

551545
return Some((result, dep_node_index));
@@ -565,7 +559,7 @@ where
565559
let prof_timer = qcx.dep_context().profiler().query_provider();
566560

567561
// The dep-graph for this computation is already in-place.
568-
let result = dep_graph.with_ignore(|| query.compute(*qcx.dep_context(), key.clone()));
562+
let result = dep_graph.with_ignore(|| Q::compute(qcx, key)(*qcx.dep_context(), key.clone()));
569563

570564
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
571565

@@ -578,7 +572,7 @@ where
578572
//
579573
// See issue #82920 for an example of a miscompilation that would get turned into
580574
// an ICE by this check
581-
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, query.hash_result);
575+
incremental_verify_ich(*qcx.dep_context(), &result, dep_node, Q::HASH_RESULT);
582576

583577
Some((result, dep_node_index))
584578
}
@@ -699,23 +693,19 @@ fn incremental_verify_ich_failed(sess: &Session, dep_node: DebugArg<'_>, result:
699693
///
700694
/// Note: The optimization is only available during incr. comp.
701695
#[inline(never)]
702-
fn ensure_must_run<Qcx, K, V>(
703-
qcx: Qcx,
704-
key: &K,
705-
query: &QueryVTable<Qcx, K, V>,
706-
) -> (bool, Option<DepNode<Qcx::DepKind>>)
696+
fn ensure_must_run<Q, Qcx>(qcx: Qcx, key: &Q::Key) -> (bool, Option<DepNode<Qcx::DepKind>>)
707697
where
708-
K: crate::dep_graph::DepNodeParams<Qcx::DepContext>,
698+
Q: QueryConfig<Qcx>,
709699
Qcx: QueryContext,
710700
{
711-
if query.eval_always {
701+
if Q::EVAL_ALWAYS {
712702
return (true, None);
713703
}
714704

715705
// Ensuring an anonymous query makes no sense
716-
assert!(!query.anon);
706+
assert!(!Q::ANON);
717707

718-
let dep_node = query.to_dep_node(*qcx.dep_context(), key);
708+
let dep_node = Q::construct_dep_node(*qcx.dep_context(), key);
719709

720710
let dep_graph = qcx.dep_context().dep_graph();
721711
match dep_graph.try_mark_green(qcx, &dep_node) {
@@ -746,13 +736,11 @@ pub fn get_query<Q, Qcx, D>(qcx: Qcx, span: Span, key: Q::Key, mode: QueryMode)
746736
where
747737
D: DepKind,
748738
Q: QueryConfig<Qcx>,
749-
Q::Key: DepNodeParams<Qcx::DepContext>,
750739
Q::Value: Value<Qcx::DepContext, D>,
751740
Qcx: QueryContext,
752741
{
753-
let query = Q::make_vtable(qcx, &key);
754742
let dep_node = if let QueryMode::Ensure = mode {
755-
let (must_run, dep_node) = ensure_must_run(qcx, &key, &query);
743+
let (must_run, dep_node) = ensure_must_run::<Q, _>(qcx, &key);
756744
if !must_run {
757745
return None;
758746
}
@@ -761,14 +749,13 @@ where
761749
None
762750
};
763751

764-
let (result, dep_node_index) = try_execute_query(
752+
let (result, dep_node_index) = try_execute_query::<Q, Qcx>(
765753
qcx,
766754
Q::query_state(qcx),
767755
Q::query_cache(qcx),
768756
span,
769757
key,
770758
dep_node,
771-
&query,
772759
);
773760
if let Some(dep_node_index) = dep_node_index {
774761
qcx.dep_context().dep_graph().read_index(dep_node_index)
@@ -780,7 +767,6 @@ pub fn force_query<Q, Qcx, D>(qcx: Qcx, key: Q::Key, dep_node: DepNode<Qcx::DepK
780767
where
781768
D: DepKind,
782769
Q: QueryConfig<Qcx>,
783-
Q::Key: DepNodeParams<Qcx::DepContext>,
784770
Q::Value: Value<Qcx::DepContext, D>,
785771
Qcx: QueryContext,
786772
{
@@ -798,9 +784,8 @@ where
798784
Err(()) => {}
799785
}
800786

801-
let query = Q::make_vtable(qcx, &key);
802787
let state = Q::query_state(qcx);
803-
debug_assert!(!query.anon);
788+
debug_assert!(!Q::ANON);
804789

805-
try_execute_query(qcx, state, cache, DUMMY_SP, key, Some(dep_node), &query);
790+
try_execute_query::<Q, _>(qcx, state, cache, DUMMY_SP, key, Some(dep_node));
806791
}

0 commit comments

Comments
 (0)
Please sign in to comment.