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`
This commit is contained in:
bors 2023-05-21 22:18:01 +00:00
commit 2440ccabc8
4 changed files with 77 additions and 82 deletions

View File

@ -32,20 +32,12 @@ pub fn new() -> QueryKeyStringCache {
} }
} }
#[derive(Clone, Copy)]
pub struct QueryStruct<'tcx> {
pub try_collect_active_jobs: fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>) -> Option<()>,
pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache),
pub encode_query_results:
Option<fn(TyCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>,
}
pub struct DynamicQuery<'tcx, C: QueryCache> { pub struct DynamicQuery<'tcx, C: QueryCache> {
pub name: &'static str, pub name: &'static str,
pub eval_always: bool, pub eval_always: bool,
pub dep_kind: rustc_middle::dep_graph::DepKind, pub dep_kind: DepKind,
pub handle_cycle_error: HandleCycleError, pub handle_cycle_error: HandleCycleError,
pub query_state: FieldOffset<QueryStates<'tcx>, QueryState<C::Key, crate::dep_graph::DepKind>>, pub query_state: FieldOffset<QueryStates<'tcx>, QueryState<C::Key, DepKind>>,
pub query_cache: FieldOffset<QueryCaches<'tcx>, C>, pub query_cache: FieldOffset<QueryCaches<'tcx>, C>,
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool, pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value, pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
@ -60,8 +52,7 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
pub loadable_from_disk: pub loadable_from_disk:
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool, fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
pub hash_result: HashResult<C::Value>, pub hash_result: HashResult<C::Value>,
pub value_from_cycle_error: pub value_from_cycle_error: fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> C::Value,
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<crate::dep_graph::DepKind>]) -> C::Value,
pub format_value: fn(&C::Value) -> String, pub format_value: fn(&C::Value) -> String,
} }
@ -69,7 +60,6 @@ pub struct QuerySystemFns<'tcx> {
pub engine: QueryEngine, pub engine: QueryEngine,
pub local_providers: Providers, pub local_providers: Providers,
pub extern_providers: ExternProviders, pub extern_providers: ExternProviders,
pub query_structs: Vec<QueryStruct<'tcx>>,
pub encode_query_results: fn( pub encode_query_results: fn(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
encoder: &mut CacheEncoder<'_, 'tcx>, encoder: &mut CacheEncoder<'_, 'tcx>,

View File

@ -23,8 +23,10 @@
use rustc_middle::dep_graph::DepNodeIndex; use rustc_middle::dep_graph::DepNodeIndex;
use rustc_middle::dep_graph::{self, DepKind, DepKindStruct}; use rustc_middle::dep_graph::{self, DepKind, DepKindStruct};
use rustc_middle::query::erase::{erase, restore, Erase}; use rustc_middle::query::erase::{erase, restore, Erase};
use rustc_middle::query::on_disk_cache::OnDiskCache; use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use rustc_middle::query::plumbing::{DynamicQuery, QuerySystem, QuerySystemFns}; use rustc_middle::query::plumbing::{
DynamicQuery, QueryKeyStringCache, QuerySystem, QuerySystemFns,
};
use rustc_middle::query::AsLocalKey; use rustc_middle::query::AsLocalKey;
use rustc_middle::query::{ use rustc_middle::query::{
queries, DynamicQueries, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates, queries, DynamicQueries, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
@ -215,7 +217,6 @@ pub fn query_system<'tcx>(
engine: engine(incremental), engine: engine(incremental),
local_providers, local_providers,
extern_providers, extern_providers,
query_structs: make_dep_kind_array!(query_structs).to_vec(),
encode_query_results: encode_all_query_results, encode_query_results: encode_all_query_results,
try_mark_green: try_mark_green, try_mark_green: try_mark_green,
}, },

View File

@ -81,8 +81,8 @@ fn current_query_job(self) -> Option<QueryJobId> {
fn try_collect_active_jobs(self) -> Option<QueryMap<DepKind>> { fn try_collect_active_jobs(self) -> Option<QueryMap<DepKind>> {
let mut jobs = QueryMap::default(); let mut jobs = QueryMap::default();
for query in &self.query_system.fns.query_structs { for collect in super::TRY_COLLECT_ACTIVE_JOBS.iter() {
(query.try_collect_active_jobs)(self.tcx, &mut jobs); collect(self.tcx, &mut jobs);
} }
Some(jobs) Some(jobs)
@ -183,11 +183,9 @@ pub(super) fn encode_all_query_results<'tcx>(
encoder: &mut CacheEncoder<'_, 'tcx>, encoder: &mut CacheEncoder<'_, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex, query_result_index: &mut EncodedDepNodeIndex,
) { ) {
for query in &tcx.query_system.fns.query_structs { for encode in super::ENCODE_QUERY_RESULTS.iter().copied().filter_map(|e| e) {
if let Some(encode) = query.encode_query_results {
encode(tcx, encoder, query_result_index); encode(tcx, encoder, query_result_index);
} }
}
} }
macro_rules! handle_cycle_error { macro_rules! handle_cycle_error {
@ -476,6 +474,16 @@ pub(crate) fn query_callback<'tcx, Q>(is_anon: bool, is_eval_always: bool) -> De
} }
} }
macro_rules! item_if_cached {
([] $tokens:tt) => {};
([(cache) $($rest:tt)*] { $($tokens:tt)* }) => {
$($tokens)*
};
([$other:tt $($modifiers:tt)*] $tokens:tt) => {
item_if_cached! { [$($modifiers)*] $tokens }
};
}
macro_rules! expand_if_cached { macro_rules! expand_if_cached {
([], $tokens:expr) => {{ ([], $tokens:expr) => {{
None None
@ -633,6 +641,43 @@ fn restore(value: <Self::Config as QueryConfig<QueryCtxt<'tcx>>>::Value) -> Self
restore::<queries::$name::Value<'tcx>>(value) restore::<queries::$name::Value<'tcx>>(value)
} }
} }
pub fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap<DepKind>) {
let make_query = |tcx, key| {
let kind = rustc_middle::dep_graph::DepKind::$name;
let name = stringify!($name);
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
};
tcx.query_system.states.$name.try_collect_active_jobs(
tcx,
make_query,
qmap,
).unwrap();
}
pub fn alloc_self_profile_query_strings<'tcx>(tcx: TyCtxt<'tcx>, string_cache: &mut QueryKeyStringCache) {
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
tcx,
stringify!($name),
&tcx.query_system.caches.$name,
string_cache,
)
}
item_if_cached! { [$($modifiers)*] {
pub fn encode_query_results<'tcx>(
tcx: TyCtxt<'tcx>,
encoder: &mut CacheEncoder<'_, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex
) {
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
query_impl::$name::QueryType::config(tcx),
QueryCtxt::new(tcx),
encoder,
query_result_index,
)
}
}}
})*} })*}
pub(crate) fn engine(incremental: bool) -> QueryEngine { pub(crate) fn engine(incremental: bool) -> QueryEngine {
@ -655,6 +700,23 @@ pub fn dynamic_queries<'tcx>() -> DynamicQueries<'tcx> {
} }
} }
// These arrays are used for iteration and can't be indexed by `DepKind`.
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<DepKind>)] =
&[$(query_impl::$name::try_collect_active_jobs),*];
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)
] = &[$(query_impl::$name::alloc_self_profile_query_strings),*];
const ENCODE_QUERY_RESULTS: &[
Option<for<'tcx> fn(
TyCtxt<'tcx>,
&mut CacheEncoder<'_, 'tcx>,
&mut EncodedDepNodeIndex)
>
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
#[allow(nonstandard_style)] #[allow(nonstandard_style)]
mod query_callbacks { mod query_callbacks {
use super::*; use super::*;
@ -720,64 +782,6 @@ pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
})* })*
} }
mod query_structs {
use super::*;
use rustc_middle::query::plumbing::{QueryKeyStringCache, QueryStruct};
use rustc_middle::dep_graph::DepKind;
use crate::QueryConfigRestored;
pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
fn noop_try_collect_active_jobs(_: TyCtxt<'_>, _: &mut QueryMap<DepKind>) -> Option<()> {
None
}
fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {}
QueryStruct {
try_collect_active_jobs: noop_try_collect_active_jobs,
alloc_self_profile_query_strings: noop_alloc_self_profile_query_strings,
encode_query_results: None,
}
}
pub(super) use dummy_query_struct as Null;
pub(super) use dummy_query_struct as Red;
pub(super) use dummy_query_struct as TraitSelect;
pub(super) use dummy_query_struct as CompileCodegenUnit;
pub(super) use dummy_query_struct as CompileMonoItem;
$(
pub(super) const fn $name<'tcx>() -> QueryStruct<'tcx> { QueryStruct {
try_collect_active_jobs: |tcx, qmap| {
let make_query = |tcx, key| {
let kind = rustc_middle::dep_graph::DepKind::$name;
let name = stringify!($name);
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
};
tcx.query_system.states.$name.try_collect_active_jobs(
tcx,
make_query,
qmap,
)
},
alloc_self_profile_query_strings: |tcx, string_cache| {
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
tcx,
stringify!($name),
&tcx.query_system.caches.$name,
string_cache,
)
},
encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index|
$crate::plumbing::encode_query_results::<query_impl::$name::QueryType<'tcx>>(
query_impl::$name::QueryType::config(tcx),
QueryCtxt::new(tcx),
encoder,
query_result_index,
)
),
}})*
}
pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] { pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks)) arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
} }

View File

@ -243,7 +243,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
let mut string_cache = QueryKeyStringCache::new(); let mut string_cache = QueryKeyStringCache::new();
for query in &tcx.query_system.fns.query_structs { for alloc in super::ALLOC_SELF_PROFILE_QUERY_STRINGS.iter() {
(query.alloc_self_profile_query_strings)(tcx, &mut string_cache); alloc(tcx, &mut string_cache)
} }
} }