Skip to content

Commit 2440cca

Browse files
committed
Auto merge of #111808 - Zoxc:query-structs-trim, r=cjgillot
Replace `QueryStruct` with arrays local to `rustc_query_impl` This removes `QueryStruct` and instead uses constant arrays of function pointers for `try_collect_active_jobs`, `alloc_self_profile_query_strings` and `encode_query_results`. This further decouples `rustc_query_impl` from `rustc_middle`. r? `@cjgillot`
2 parents 9d871b0 + aa5d436 commit 2440cca

File tree

4 files changed

+77
-82
lines changed

4 files changed

+77
-82
lines changed

compiler/rustc_middle/src/query/plumbing.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,12 @@ impl QueryKeyStringCache {
3232
}
3333
}
3434

35-
#[derive(Clone, Copy)]
36-
pub struct QueryStruct<'tcx> {
37-
pub try_collect_active_jobs: fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>,
38-
pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache),
39-
pub encode_query_results:
40-
Option<fn(TyCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>,
41-
}
42-
4335
pub struct DynamicQuery<'tcx, C: QueryCache> {
4436
pub name: &'static str,
4537
pub eval_always: bool,
46-
pub dep_kind: rustc_middle::dep_graph::DepKind,
38+
pub dep_kind: DepKind,
4739
pub handle_cycle_error: HandleCycleError,
48-
pub query_state: FieldOffset<QueryStates<'tcx>, QueryState<C::Key, crate::dep_graph::DepKind>>,
40+
pub query_state: FieldOffset<QueryStates<'tcx>, QueryState<C::Key, DepKind>>,
4941
pub query_cache: FieldOffset<QueryCaches<'tcx>, C>,
5042
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
5143
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
@@ -60,16 +52,14 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
6052
pub loadable_from_disk:
6153
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
6254
pub hash_result: HashResult<C::Value>,
63-
pub value_from_cycle_error:
64-
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<crate::dep_graph::DepKind>]) -> C::Value,
55+
pub value_from_cycle_error: fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> C::Value,
6556
pub format_value: fn(&C::Value) -> String,
6657
}
6758

6859
pub struct QuerySystemFns<'tcx> {
6960
pub engine: QueryEngine,
7061
pub local_providers: Providers,
7162
pub extern_providers: ExternProviders,
72-
pub query_structs: Vec<QueryStruct<'tcx>>,
7363
pub encode_query_results: fn(
7464
tcx: TyCtxt<'tcx>,
7565
encoder: &mut CacheEncoder<'_, 'tcx>,

compiler/rustc_query_impl/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ use rustc_middle::arena::Arena;
2323
use rustc_middle::dep_graph::DepNodeIndex;
2424
use rustc_middle::dep_graph::{self, DepKind, DepKindStruct};
2525
use rustc_middle::query::erase::{erase, restore, Erase};
26-
use rustc_middle::query::on_disk_cache::OnDiskCache;
27-
use rustc_middle::query::plumbing::{DynamicQuery, QuerySystem, QuerySystemFns};
26+
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
27+
use rustc_middle::query::plumbing::{
28+
DynamicQuery, QueryKeyStringCache, QuerySystem, QuerySystemFns,
29+
};
2830
use rustc_middle::query::AsLocalKey;
2931
use rustc_middle::query::{
3032
queries, DynamicQueries, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
@@ -215,7 +217,6 @@ pub fn query_system<'tcx>(
215217
engine: engine(incremental),
216218
local_providers,
217219
extern_providers,
218-
query_structs: make_dep_kind_array!(query_structs).to_vec(),
219220
encode_query_results: encode_all_query_results,
220221
try_mark_green: try_mark_green,
221222
},

compiler/rustc_query_impl/src/plumbing.rs

+68-64
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ impl QueryContext for QueryCtxt<'_> {
8181
fn try_collect_active_jobs(self) -> Option<QueryMap<DepKind>> {
8282
let mut jobs = QueryMap::default();
8383

84-
for query in &self.query_system.fns.query_structs {
85-
(query.try_collect_active_jobs)(self.tcx, &mut jobs);
84+
for collect in super::TRY_COLLECT_ACTIVE_JOBS.iter() {
85+
collect(self.tcx, &mut jobs);
8686
}
8787

8888
Some(jobs)
@@ -183,10 +183,8 @@ pub(super) fn encode_all_query_results<'tcx>(
183183
encoder: &mut CacheEncoder<'_, 'tcx>,
184184
query_result_index: &mut EncodedDepNodeIndex,
185185
) {
186-
for query in &tcx.query_system.fns.query_structs {
187-
if let Some(encode) = query.encode_query_results {
188-
encode(tcx, encoder, query_result_index);
189-
}
186+
for encode in super::ENCODE_QUERY_RESULTS.iter().copied().filter_map(|e| e) {
187+
encode(tcx, encoder, query_result_index);
190188
}
191189
}
192190

@@ -476,6 +474,16 @@ where
476474
}
477475
}
478476

477+
macro_rules! item_if_cached {
478+
([] $tokens:tt) => {};
479+
([(cache) $($rest:tt)*] { $($tokens:tt)* }) => {
480+
$($tokens)*
481+
};
482+
([$other:tt $($modifiers:tt)*] $tokens:tt) => {
483+
item_if_cached! { [$($modifiers)*] $tokens }
484+
};
485+
}
486+
479487
macro_rules! expand_if_cached {
480488
([], $tokens:expr) => {{
481489
None
@@ -633,6 +641,43 @@ macro_rules! define_queries {
633641
restore::<queries::$name::Value<'tcx>>(value)
634642
}
635643
}
644+
645+
pub fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap<DepKind>) {
646+
let make_query = |tcx, key| {
647+
let kind = rustc_middle::dep_graph::DepKind::$name;
648+
let name = stringify!($name);
649+
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
650+
};
651+
tcx.query_system.states.$name.try_collect_active_jobs(
652+
tcx,
653+
make_query,
654+
qmap,
655+
).unwrap();
656+
}
657+
658+
pub fn alloc_self_profile_query_strings<'tcx>(tcx: TyCtxt<'tcx>, string_cache: &mut QueryKeyStringCache) {
659+
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
660+
tcx,
661+
stringify!($name),
662+
&tcx.query_system.caches.$name,
663+
string_cache,
664+
)
665+
}
666+
667+
item_if_cached! { [$($modifiers)*] {
668+
pub fn encode_query_results<'tcx>(
669+
tcx: TyCtxt<'tcx>,
670+
encoder: &mut CacheEncoder<'_, 'tcx>,
671+
query_result_index: &mut EncodedDepNodeIndex
672+
) {
673+
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
674+
query_impl::$name::QueryType::config(tcx),
675+
QueryCtxt::new(tcx),
676+
encoder,
677+
query_result_index,
678+
)
679+
}
680+
}}
636681
})*}
637682

638683
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -655,6 +700,23 @@ macro_rules! define_queries {
655700
}
656701
}
657702

703+
// These arrays are used for iteration and can't be indexed by `DepKind`.
704+
705+
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>)] =
706+
&[$(query_impl::$name::try_collect_active_jobs),*];
707+
708+
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
709+
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)
710+
] = &[$(query_impl::$name::alloc_self_profile_query_strings),*];
711+
712+
const ENCODE_QUERY_RESULTS: &[
713+
Option<for<'tcx> fn(
714+
TyCtxt<'tcx>,
715+
&mut CacheEncoder<'_, 'tcx>,
716+
&mut EncodedDepNodeIndex)
717+
>
718+
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
719+
658720
#[allow(nonstandard_style)]
659721
mod query_callbacks {
660722
use super::*;
@@ -720,64 +782,6 @@ macro_rules! define_queries {
720782
})*
721783
}
722784

723-
mod query_structs {
724-
use super::*;
725-
use rustc_middle::query::plumbing::{QueryKeyStringCache, QueryStruct};
726-
use rustc_middle::dep_graph::DepKind;
727-
use crate::QueryConfigRestored;
728-
729-
pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
730-
fn noop_try_collect_active_jobs(_: TyCtxt<'_>, _: &mut QueryMap<DepKind>) -> Option<()> {
731-
None
732-
}
733-
fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {}
734-
735-
QueryStruct {
736-
try_collect_active_jobs: noop_try_collect_active_jobs,
737-
alloc_self_profile_query_strings: noop_alloc_self_profile_query_strings,
738-
encode_query_results: None,
739-
}
740-
}
741-
742-
pub(super) use dummy_query_struct as Null;
743-
pub(super) use dummy_query_struct as Red;
744-
pub(super) use dummy_query_struct as TraitSelect;
745-
pub(super) use dummy_query_struct as CompileCodegenUnit;
746-
pub(super) use dummy_query_struct as CompileMonoItem;
747-
748-
$(
749-
pub(super) const fn $name<'tcx>() -> QueryStruct<'tcx> { QueryStruct {
750-
try_collect_active_jobs: |tcx, qmap| {
751-
let make_query = |tcx, key| {
752-
let kind = rustc_middle::dep_graph::DepKind::$name;
753-
let name = stringify!($name);
754-
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
755-
};
756-
tcx.query_system.states.$name.try_collect_active_jobs(
757-
tcx,
758-
make_query,
759-
qmap,
760-
)
761-
},
762-
alloc_self_profile_query_strings: |tcx, string_cache| {
763-
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
764-
tcx,
765-
stringify!($name),
766-
&tcx.query_system.caches.$name,
767-
string_cache,
768-
)
769-
},
770-
encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index|
771-
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
772-
query_impl::$name::QueryType::config(tcx),
773-
QueryCtxt::new(tcx),
774-
encoder,
775-
query_result_index,
776-
)
777-
),
778-
}})*
779-
}
780-
781785
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
782786
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
783787
}

compiler/rustc_query_impl/src/profiling_support.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
243243

244244
let mut string_cache = QueryKeyStringCache::new();
245245

246-
for query in &tcx.query_system.fns.query_structs {
247-
(query.alloc_self_profile_query_strings)(tcx, &mut string_cache);
246+
for alloc in super::ALLOC_SELF_PROFILE_QUERY_STRINGS.iter() {
247+
alloc(tcx, &mut string_cache)
248248
}
249249
}

0 commit comments

Comments
 (0)