Skip to content

Commit b46d288

Browse files
committed
Auto merge of #152841 - Zalathar:unerased, r=<try>
Streamline `QueryVTableUnerased` into `QueryVTableGetter`
2 parents fbd6934 + 3680e69 commit b46d288

3 files changed

Lines changed: 52 additions & 71 deletions

File tree

compiler/rustc_query_impl/src/dep_kind_vtables.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, KeyFingerprintStyle};
33
use rustc_middle::query::QueryCache;
44

55
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};
6-
use crate::{QueryDispatcherUnerased, QueryFlags};
6+
use crate::{QueryFlags, QueryVTableGetter};
77

88
/// [`DepKindVTable`] constructors for special dep kinds that aren't queries.
99
#[expect(non_snake_case, reason = "use non-snake case to avoid collision with query names")]
@@ -110,18 +110,17 @@ mod non_query {
110110

111111
/// Shared implementation of the [`DepKindVTable`] constructor for queries.
112112
/// Called from macro-generated code for each query.
113-
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache, const FLAGS: QueryFlags>(
113+
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, const FLAGS: QueryFlags>(
114114
is_eval_always: bool,
115115
) -> DepKindVTable<'tcx>
116116
where
117-
Q: QueryDispatcherUnerased<'tcx, Cache, FLAGS>,
118-
Cache: QueryCache + 'tcx,
117+
Q: QueryVTableGetter<'tcx, FLAGS>,
119118
{
120119
let is_anon = FLAGS.is_anon;
121120
let key_fingerprint_style = if is_anon {
122121
KeyFingerprintStyle::Opaque
123122
} else {
124-
<Cache::Key as DepNodeKey<'tcx>>::key_fingerprint_style()
123+
<Q::Cache as QueryCache>::Key::key_fingerprint_style()
125124
};
126125

127126
if is_anon || !key_fingerprint_style.reconstructible() {
@@ -139,12 +138,8 @@ where
139138
is_anon,
140139
is_eval_always,
141140
key_fingerprint_style,
142-
force_from_dep_node: Some(|tcx, dep_node, _| {
143-
force_from_dep_node_inner(Q::query_dispatcher(tcx), tcx, dep_node)
144-
}),
145-
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
146-
try_load_from_on_disk_cache_inner(Q::query_dispatcher(tcx), tcx, dep_node)
147-
}),
141+
force_from_dep_node: Some(force_from_dep_node_inner::<'tcx, Q, FLAGS>),
142+
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache_inner::<'tcx, Q, FLAGS>),
148143
name: Q::NAME,
149144
}
150145
}

compiler/rustc_query_impl/src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,22 @@ impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> SemiDynamicQueryDispatcher<'t
217217
}
218218
}
219219

220-
/// Provides access to vtable-like operations for a query
221-
/// (by creating a [`SemiDynamicQueryDispatcher`]),
222-
/// but also keeps track of the "unerased" value type of the query
223-
/// (i.e. the actual result type in the query declaration).
220+
/// Trait that knows how to look up the [`QueryVTable`] for a particular query,
221+
/// and can combine it with static query flags to produce a
222+
/// [`SemiDynamicQueryDispatcher`].
224223
///
225224
/// This trait allows some per-query code to be defined in generic functions
226225
/// with a trait bound, instead of having to be defined inline within a macro
227226
/// expansion.
228227
///
229228
/// There is one macro-generated implementation of this trait for each query,
230-
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
231-
trait QueryDispatcherUnerased<'tcx, C: QueryCache, const FLAGS: QueryFlags> {
232-
type UnerasedValue;
229+
/// on the type `rustc_query_impl::query_impl::$name::VTableGetter`.
230+
trait QueryVTableGetter<'tcx, const FLAGS: QueryFlags>: 'tcx {
231+
type Cache: QueryCache + 'tcx;
233232

234233
const NAME: &'static &'static str;
235234

236-
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, C, FLAGS>;
237-
238-
fn restore_val(value: C::Value) -> Self::UnerasedValue;
235+
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, Self::Cache, FLAGS>;
239236
}
240237

241238
pub fn query_system<'tcx>(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::dep_graph::DepKindVTable;
1717
use rustc_middle::dep_graph::{
1818
self, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex, dep_kinds,
1919
};
20+
use rustc_middle::query::erase::{self, Erasable, Erased};
2021
use rustc_middle::query::on_disk_cache::{
2122
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
2223
};
@@ -34,7 +35,7 @@ use rustc_span::def_id::LOCAL_CRATE;
3435
use crate::error::{QueryOverflow, QueryOverflowNote};
3536
use crate::execution::{all_inactive, force_query};
3637
use crate::job::{QueryJobMap, find_dep_kind_root};
37-
use crate::{QueryDispatcherUnerased, QueryFlags, SemiDynamicQueryDispatcher};
38+
use crate::{QueryFlags, QueryVTableGetter};
3839

3940
fn depth_limit_error<'tcx>(tcx: TyCtxt<'tcx>, job: QueryJobId) {
4041
let job_map =
@@ -335,15 +336,16 @@ where
335336
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
336337
}
337338

338-
pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: QueryFlags>(
339-
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
339+
pub(crate) fn encode_query_results_inner<'a, 'tcx, Value, Q, const FLAGS: QueryFlags>(
340340
tcx: TyCtxt<'tcx>,
341341
encoder: &mut CacheEncoder<'a, 'tcx>,
342342
query_result_index: &mut EncodedDepNodeIndex,
343343
) where
344-
Q: QueryDispatcherUnerased<'tcx, C, FLAGS>,
345-
Q::UnerasedValue: Encodable<CacheEncoder<'a, 'tcx>>,
344+
Value: Erasable + Encodable<CacheEncoder<'a, 'tcx>>,
345+
Q: QueryVTableGetter<'tcx, FLAGS>,
346+
Q::Cache: QueryCache<Value = Erased<Value>>,
346347
{
348+
let query = Q::query_dispatcher(tcx);
347349
let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name());
348350

349351
assert!(all_inactive(query.query_state(tcx)));
@@ -357,15 +359,16 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: Quer
357359

358360
// Encode the type check tables with the `SerializedDepNodeIndex`
359361
// as tag.
360-
encoder.encode_tagged(dep_node, &Q::restore_val(*value));
362+
encoder.encode_tagged(dep_node, &erase::restore_val(*value));
361363
}
362364
});
363365
}
364366

365-
pub(crate) fn query_key_hash_verify<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
366-
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
367-
tcx: TyCtxt<'tcx>,
368-
) {
367+
pub(crate) fn query_key_hash_verify_inner<'tcx, Q, const FLAGS: QueryFlags>(tcx: TyCtxt<'tcx>)
368+
where
369+
Q: QueryVTableGetter<'tcx, FLAGS>,
370+
{
371+
let query = Q::query_dispatcher(tcx);
369372
let _timer = tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name());
370373

371374
let cache = query.query_cache(tcx);
@@ -389,19 +392,21 @@ pub(crate) fn query_key_hash_verify<'tcx, C: QueryCache, const FLAGS: QueryFlags
389392
}
390393

391394
/// Implementation of [`DepKindVTable::try_load_from_on_disk_cache`] for queries.
392-
pub(crate) fn try_load_from_on_disk_cache_inner<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
393-
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
395+
pub(crate) fn try_load_from_on_disk_cache_inner<'tcx, Q, const FLAGS: QueryFlags>(
394396
tcx: TyCtxt<'tcx>,
395397
dep_node: DepNode,
396-
) {
398+
) where
399+
Q: QueryVTableGetter<'tcx, FLAGS>,
400+
{
397401
debug_assert!(tcx.dep_graph.is_green(&dep_node));
398402

399-
let key = C::Key::try_recover_key(tcx, &dep_node).unwrap_or_else(|| {
403+
let key = <Q::Cache as QueryCache>::Key::try_recover_key(tcx, &dep_node).unwrap_or_else(|| {
400404
panic!(
401405
"Failed to recover key for {dep_node:?} with key fingerprint {}",
402406
dep_node.key_fingerprint
403407
)
404408
});
409+
let query = Q::query_dispatcher(tcx);
405410
if query.will_cache_on_disk_for_key(tcx, &key) {
406411
// Call `tcx.$query(key)` for its side-effect of loading the disk-cached
407412
// value into memory.
@@ -442,11 +447,14 @@ where
442447
}
443448

444449
/// Implementation of [`DepKindVTable::force_from_dep_node`] for queries.
445-
pub(crate) fn force_from_dep_node_inner<'tcx, C: QueryCache, const FLAGS: QueryFlags>(
446-
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
450+
pub(crate) fn force_from_dep_node_inner<'tcx, Q, const FLAGS: QueryFlags>(
447451
tcx: TyCtxt<'tcx>,
448452
dep_node: DepNode,
449-
) -> bool {
453+
_prev_index: SerializedDepNodeIndex, // Required by vtable but not used by queries
454+
) -> bool
455+
where
456+
Q: QueryVTableGetter<'tcx, FLAGS>,
457+
{
450458
// We must avoid ever having to call `force_from_dep_node()` for a
451459
// `DepNode::codegen_unit`:
452460
// Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
@@ -465,7 +473,8 @@ pub(crate) fn force_from_dep_node_inner<'tcx, C: QueryCache, const FLAGS: QueryF
465473
"calling force_from_dep_node() on dep_kinds::codegen_unit"
466474
);
467475

468-
if let Some(key) = C::Key::try_recover_key(tcx, &dep_node) {
476+
if let Some(key) = <Q::Cache as QueryCache>::Key::try_recover_key(tcx, &dep_node) {
477+
let query = Q::query_dispatcher(tcx);
469478
force_query(query, tcx, key, dep_node);
470479
true
471480
} else {
@@ -485,7 +494,6 @@ macro_rules! define_queries {
485494

486495
pub(crate) mod query_impl { $(pub(crate) mod $name {
487496
use super::super::*;
488-
use std::marker::PhantomData;
489497
use ::rustc_middle::query::erase::{self, Erased};
490498

491499
pub(crate) mod get_query_incr {
@@ -503,7 +511,7 @@ macro_rules! define_queries {
503511
#[cfg(debug_assertions)]
504512
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
505513
execution::get_query_incr(
506-
QueryType::query_dispatcher(tcx),
514+
VTableGetter::query_dispatcher(tcx),
507515
tcx,
508516
span,
509517
key,
@@ -523,7 +531,7 @@ macro_rules! define_queries {
523531
__mode: QueryMode,
524532
) -> Option<Erased<queries::$name::Value<'tcx>>> {
525533
Some(execution::get_query_non_incr(
526-
QueryType::query_dispatcher(tcx),
534+
VTableGetter::query_dispatcher(tcx),
527535
tcx,
528536
span,
529537
key,
@@ -616,39 +624,28 @@ macro_rules! define_queries {
616624
}
617625
}
618626

619-
#[derive(Copy, Clone, Default)]
620-
pub(crate) struct QueryType<'tcx> {
621-
data: PhantomData<&'tcx ()>
622-
}
623-
624627
const FLAGS: QueryFlags = QueryFlags {
625628
is_anon: is_anon!([$($modifiers)*]),
626629
is_depth_limit: depth_limit!([$($modifiers)*]),
627630
is_feedable: feedable!([$($modifiers)*]),
628631
};
629632

630-
impl<'tcx> QueryDispatcherUnerased<'tcx, queries::$name::Storage<'tcx>, FLAGS>
631-
for QueryType<'tcx>
632-
{
633-
type UnerasedValue = queries::$name::Value<'tcx>;
633+
/// Marker type that implements [`QueryVTableGetter`] for this query.
634+
pub(crate) enum VTableGetter {}
635+
636+
impl<'tcx> QueryVTableGetter<'tcx, FLAGS> for VTableGetter {
637+
type Cache = queries::$name::Storage<'tcx>;
634638

635639
const NAME: &'static &'static str = &stringify!($name);
636640

637641
#[inline(always)]
638642
fn query_dispatcher(tcx: TyCtxt<'tcx>)
639-
-> SemiDynamicQueryDispatcher<'tcx, queries::$name::Storage<'tcx>, FLAGS>
643+
-> SemiDynamicQueryDispatcher<'tcx, Self::Cache, FLAGS>
640644
{
641645
SemiDynamicQueryDispatcher {
642646
vtable: &tcx.query_system.query_vtables.$name,
643647
}
644648
}
645-
646-
#[inline(always)]
647-
fn restore_val(value: <queries::$name::Storage<'tcx> as QueryCache>::Value)
648-
-> Self::UnerasedValue
649-
{
650-
erase::restore_val::<queries::$name::Value<'tcx>>(value)
651-
}
652649
}
653650

654651
/// Internal per-query plumbing for collecting the set of active jobs for this query.
@@ -702,12 +699,7 @@ macro_rules! define_queries {
702699
encoder: &mut CacheEncoder<'_, 'tcx>,
703700
query_result_index: &mut EncodedDepNodeIndex
704701
) {
705-
$crate::plumbing::encode_query_results::<
706-
query_impl::$name::QueryType<'tcx>,
707-
_,
708-
_
709-
> (
710-
query_impl::$name::QueryType::query_dispatcher(tcx),
702+
$crate::plumbing::encode_query_results_inner::<_, VTableGetter, _>(
711703
tcx,
712704
encoder,
713705
query_result_index,
@@ -716,10 +708,7 @@ macro_rules! define_queries {
716708
}
717709

718710
pub(crate) fn query_key_hash_verify<'tcx>(tcx: TyCtxt<'tcx>) {
719-
$crate::plumbing::query_key_hash_verify(
720-
query_impl::$name::QueryType::query_dispatcher(tcx),
721-
tcx,
722-
)
711+
$crate::plumbing::query_key_hash_verify_inner::<VTableGetter, _>(tcx)
723712
}
724713
})*}
725714

@@ -793,8 +782,8 @@ macro_rules! define_queries {
793782
$(
794783
/// `DepKindVTable` constructor for this query.
795784
pub(crate) fn $name<'tcx>() -> DepKindVTable<'tcx> {
796-
use $crate::query_impl::$name::QueryType;
797-
make_dep_kind_vtable_for_query::<QueryType<'tcx>, _, _>(
785+
use $crate::query_impl::$name::VTableGetter;
786+
make_dep_kind_vtable_for_query::<VTableGetter, _>(
798787
is_eval_always!([$($modifiers)*]),
799788
)
800789
}

0 commit comments

Comments
 (0)