Skip to content

Commit 2c9175d

Browse files
committed
Rename trait QueryConfig to QueryDispatcher
1 parent 89d7695 commit 2c9175d

5 files changed

Lines changed: 82 additions & 55 deletions

File tree

compiler/rustc_query_impl/src/lib.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_query_system::Value;
2121
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
2222
use rustc_query_system::ich::StableHashingContext;
2323
use rustc_query_system::query::{
24-
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryConfig, QueryMap, QueryMode,
24+
CycleError, CycleErrorHandling, HashResult, QueryCache, QueryDispatcher, QueryMap, QueryMode,
2525
QueryStackDeferred, QueryState, get_query_incr, get_query_non_incr,
2626
};
2727
use rustc_span::{ErrorGuaranteed, Span};
@@ -36,7 +36,13 @@ pub use crate::plumbing::{QueryCtxt, query_key_hash_verify_all};
3636
mod profiling_support;
3737
pub use self::profiling_support::alloc_self_profile_query_strings;
3838

39-
struct DynamicConfig<
39+
/// Combines a [`QueryVTable`] with some additional compile-time booleans
40+
/// to implement [`QueryDispatcher`], for use by code in [`rustc_query_system`].
41+
///
42+
/// Baking these boolean flags into the type gives a modest but measurable
43+
/// improvement to compiler perf and compiler code size; see
44+
/// <https://github.com/rust-lang/rust/pull/151633>.
45+
struct SemiDynamicQueryDispatcher<
4046
'tcx,
4147
C: QueryCache,
4248
const ANON: bool,
@@ -46,20 +52,23 @@ struct DynamicConfig<
4652
vtable: &'tcx QueryVTable<'tcx, C>,
4753
}
4854

55+
// Manually implement Copy/Clone, because deriving would put trait bounds on the cache type.
4956
impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDABLE: bool> Copy
50-
for DynamicConfig<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
57+
for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
5158
{
5259
}
5360
impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDABLE: bool> Clone
54-
for DynamicConfig<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
61+
for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
5562
{
5663
fn clone(&self) -> Self {
5764
*self
5865
}
5966
}
6067

68+
// This is `impl QueryDispatcher for SemiDynamicQueryDispatcher`.
6169
impl<'tcx, C: QueryCache, const ANON: bool, const DEPTH_LIMIT: bool, const FEEDABLE: bool>
62-
QueryConfig<QueryCtxt<'tcx>> for DynamicConfig<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
70+
QueryDispatcher<QueryCtxt<'tcx>>
71+
for SemiDynamicQueryDispatcher<'tcx, C, ANON, DEPTH_LIMIT, FEEDABLE>
6372
where
6473
for<'a> C::Key: HashStable<StableHashingContext<'a>>,
6574
{
@@ -193,17 +202,28 @@ where
193202
}
194203
}
195204

196-
/// This is implemented per query. It allows restoring query values from their erased state
197-
/// and constructing a QueryConfig.
198-
trait QueryConfigRestored<'tcx> {
199-
type RestoredValue;
200-
type Config: QueryConfig<QueryCtxt<'tcx>>;
205+
/// Provides access to vtable-like operations for a query
206+
/// (by creating a [`QueryDispatcher`]),
207+
/// but also keeps track of the "unerased" value type of the query
208+
/// (i.e. the actual result type in the query declaration).
209+
///
210+
/// This trait allows some per-query code to be defined in generic functions
211+
/// with a trait bound, instead of having to be defined inline within a macro
212+
/// expansion.
213+
///
214+
/// There is one macro-generated implementation of this trait for each query,
215+
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
216+
trait QueryDispatcherUnerased<'tcx> {
217+
type UnerasedValue;
218+
type Dispatcher: QueryDispatcher<QueryCtxt<'tcx>>;
201219

202220
const NAME: &'static &'static str;
203221

204-
fn config(tcx: TyCtxt<'tcx>) -> Self::Config;
205-
fn restore(value: <Self::Config as QueryConfig<QueryCtxt<'tcx>>>::Value)
206-
-> Self::RestoredValue;
222+
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher;
223+
224+
fn restore_val(
225+
value: <Self::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Value,
226+
) -> Self::UnerasedValue;
207227
}
208228

209229
pub fn query_system<'a>(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ use rustc_middle::ty::{self, TyCtxt};
2727
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
2828
use rustc_query_system::ich::StableHashingContext;
2929
use rustc_query_system::query::{
30-
QueryCache, QueryConfig, QueryContext, QueryJobId, QueryMap, QuerySideEffect,
30+
QueryCache, QueryContext, QueryDispatcher, QueryJobId, QueryMap, QuerySideEffect,
3131
QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra, force_query,
3232
};
3333
use rustc_query_system::{QueryOverflow, QueryOverflowNote};
3434
use rustc_serialize::{Decodable, Encodable};
3535
use rustc_span::def_id::LOCAL_CRATE;
3636

37-
use crate::QueryConfigRestored;
37+
use crate::QueryDispatcherUnerased;
3838

3939
/// Implements [`QueryContext`] for use by [`rustc_query_system`], since that
4040
/// crate does not have direct access to [`TyCtxt`].
@@ -387,13 +387,13 @@ pub(crate) fn create_query_frame<
387387
}
388388

389389
pub(crate) fn encode_query_results<'a, 'tcx, Q>(
390-
query: Q::Config,
390+
query: Q::Dispatcher,
391391
qcx: QueryCtxt<'tcx>,
392392
encoder: &mut CacheEncoder<'a, 'tcx>,
393393
query_result_index: &mut EncodedDepNodeIndex,
394394
) where
395-
Q: super::QueryConfigRestored<'tcx>,
396-
Q::RestoredValue: Encodable<CacheEncoder<'a, 'tcx>>,
395+
Q: QueryDispatcherUnerased<'tcx>,
396+
Q::UnerasedValue: Encodable<CacheEncoder<'a, 'tcx>>,
397397
{
398398
let _timer = qcx.tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name());
399399

@@ -408,13 +408,13 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
408408

409409
// Encode the type check tables with the `SerializedDepNodeIndex`
410410
// as tag.
411-
encoder.encode_tagged(dep_node, &Q::restore(*value));
411+
encoder.encode_tagged(dep_node, &Q::restore_val(*value));
412412
}
413413
});
414414
}
415415

416416
pub(crate) fn query_key_hash_verify<'tcx>(
417-
query: impl QueryConfig<QueryCtxt<'tcx>>,
417+
query: impl QueryDispatcher<QueryCtxt<'tcx>>,
418418
qcx: QueryCtxt<'tcx>,
419419
) {
420420
let _timer = qcx.tcx.prof.generic_activity_with_arg("query_key_hash_verify_for", query.name());
@@ -442,7 +442,7 @@ pub(crate) fn query_key_hash_verify<'tcx>(
442442

443443
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
444444
where
445-
Q: QueryConfig<QueryCtxt<'tcx>>,
445+
Q: QueryDispatcher<QueryCtxt<'tcx>>,
446446
{
447447
debug_assert!(tcx.dep_graph.is_green(&dep_node));
448448

@@ -488,7 +488,7 @@ where
488488

489489
fn force_from_dep_node<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
490490
where
491-
Q: QueryConfig<QueryCtxt<'tcx>>,
491+
Q: QueryDispatcher<QueryCtxt<'tcx>>,
492492
{
493493
// We must avoid ever having to call `force_from_dep_node()` for a
494494
// `DepNode::codegen_unit`:
@@ -521,9 +521,10 @@ pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q>(
521521
is_eval_always: bool,
522522
) -> DepKindVTable<'tcx>
523523
where
524-
Q: QueryConfigRestored<'tcx>,
524+
Q: QueryDispatcherUnerased<'tcx>,
525525
{
526-
let fingerprint_style = <Q::Config as QueryConfig<QueryCtxt<'tcx>>>::Key::fingerprint_style();
526+
let fingerprint_style =
527+
<Q::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Key::fingerprint_style();
527528

528529
if is_anon || !fingerprint_style.reconstructible() {
529530
return DepKindVTable {
@@ -541,10 +542,10 @@ where
541542
is_eval_always,
542543
fingerprint_style,
543544
force_from_dep_node: Some(|tcx, dep_node, _| {
544-
force_from_dep_node(Q::config(tcx), tcx, dep_node)
545+
force_from_dep_node(Q::query_dispatcher(tcx), tcx, dep_node)
545546
}),
546547
try_load_from_on_disk_cache: Some(|tcx, dep_node| {
547-
try_load_from_on_disk_cache(Q::config(tcx), tcx, dep_node)
548+
try_load_from_on_disk_cache(Q::query_dispatcher(tcx), tcx, dep_node)
548549
}),
549550
name: Q::NAME,
550551
}
@@ -613,7 +614,7 @@ macro_rules! define_queries {
613614
#[cfg(debug_assertions)]
614615
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
615616
get_query_incr(
616-
QueryType::config(tcx),
617+
QueryType::query_dispatcher(tcx),
617618
QueryCtxt::new(tcx),
618619
span,
619620
key,
@@ -633,7 +634,7 @@ macro_rules! define_queries {
633634
__mode: QueryMode,
634635
) -> Option<Erase<queries::$name::Value<'tcx>>> {
635636
Some(get_query_non_incr(
636-
QueryType::config(tcx),
637+
QueryType::query_dispatcher(tcx),
637638
QueryCtxt::new(tcx),
638639
span,
639640
key,
@@ -710,9 +711,9 @@ macro_rules! define_queries {
710711
data: PhantomData<&'tcx ()>
711712
}
712713

713-
impl<'tcx> QueryConfigRestored<'tcx> for QueryType<'tcx> {
714-
type RestoredValue = queries::$name::Value<'tcx>;
715-
type Config = DynamicConfig<
714+
impl<'tcx> QueryDispatcherUnerased<'tcx> for QueryType<'tcx> {
715+
type UnerasedValue = queries::$name::Value<'tcx>;
716+
type Dispatcher = SemiDynamicQueryDispatcher<
716717
'tcx,
717718
queries::$name::Storage<'tcx>,
718719
{ is_anon!([$($modifiers)*]) },
@@ -723,14 +724,14 @@ macro_rules! define_queries {
723724
const NAME: &'static &'static str = &stringify!($name);
724725

725726
#[inline(always)]
726-
fn config(tcx: TyCtxt<'tcx>) -> Self::Config {
727-
DynamicConfig {
727+
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> Self::Dispatcher {
728+
SemiDynamicQueryDispatcher {
728729
vtable: &tcx.query_system.query_vtables.$name,
729730
}
730731
}
731732

732733
#[inline(always)]
733-
fn restore(value: <Self::Config as QueryConfig<QueryCtxt<'tcx>>>::Value) -> Self::RestoredValue {
734+
fn restore_val(value: <Self::Dispatcher as QueryDispatcher<QueryCtxt<'tcx>>>::Value) -> Self::UnerasedValue {
734735
restore::<queries::$name::Value<'tcx>>(value)
735736
}
736737
}
@@ -782,7 +783,7 @@ macro_rules! define_queries {
782783
query_result_index: &mut EncodedDepNodeIndex
783784
) {
784785
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
785-
query_impl::$name::QueryType::config(tcx),
786+
query_impl::$name::QueryType::query_dispatcher(tcx),
786787
QueryCtxt::new(tcx),
787788
encoder,
788789
query_result_index,
@@ -792,7 +793,7 @@ macro_rules! define_queries {
792793

793794
pub(crate) fn query_key_hash_verify<'tcx>(tcx: TyCtxt<'tcx>) {
794795
$crate::plumbing::query_key_hash_verify(
795-
query_impl::$name::QueryType::config(tcx),
796+
query_impl::$name::QueryType::query_dispatcher(tcx),
796797
QueryCtxt::new(tcx),
797798
)
798799
}

compiler/rustc_query_system/src/query/config.rs renamed to compiler/rustc_query_system/src/query/dispatcher.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Query configuration and description traits.
2-
31
use std::fmt::Debug;
42
use std::hash::Hash;
53

@@ -14,7 +12,15 @@ use crate::query::{CycleError, CycleErrorHandling, DepNodeIndex, QueryContext, Q
1412

1513
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
1614

17-
pub trait QueryConfig<Qcx: QueryContext>: Copy {
15+
/// Trait that can be used as a vtable for a single query, providing operations
16+
/// and metadata for that query.
17+
///
18+
/// Implemented by `rustc_query_impl::SemiDynamicQueryDispatcher`, which
19+
/// mostly delegates to `rustc_middle::query::plumbing::QueryVTable`.
20+
/// Those types are not visible from this `rustc_query_system` crate.
21+
///
22+
/// "Dispatcher" should be understood as a near-synonym of "vtable".
23+
pub trait QueryDispatcher<Qcx: QueryContext>: Copy {
1824
fn name(self) -> &'static str;
1925

2026
// `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_span::Span;
1313
use rustc_span::def_id::DefId;
1414

1515
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
16-
pub use self::config::{HashResult, QueryConfig};
16+
pub use self::dispatcher::{HashResult, QueryDispatcher};
1717
pub use self::job::{
1818
QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap, break_query_cycles, print_query_stack,
1919
report_cycle,
@@ -22,7 +22,7 @@ pub use self::plumbing::*;
2222
use crate::dep_graph::{DepKind, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
2323

2424
mod caches;
25-
mod config;
25+
mod dispatcher;
2626
mod job;
2727
mod plumbing;
2828

0 commit comments

Comments
 (0)