Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace QueryStruct with arrays local to rustc_query_impl #111808

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,12 @@ impl 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 name: &'static str,
pub eval_always: bool,
pub dep_kind: rustc_middle::dep_graph::DepKind,
pub dep_kind: DepKind,
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 cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
Expand All @@ -60,16 +52,14 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
pub loadable_from_disk:
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
pub hash_result: HashResult<C::Value>,
pub value_from_cycle_error:
fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<crate::dep_graph::DepKind>]) -> C::Value,
pub value_from_cycle_error: fn(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo<DepKind>]) -> C::Value,
pub format_value: fn(&C::Value) -> String,
}

pub struct QuerySystemFns<'tcx> {
pub engine: QueryEngine,
pub local_providers: Providers,
pub extern_providers: ExternProviders,
pub query_structs: Vec<QueryStruct<'tcx>>,
pub encode_query_results: fn(
tcx: TyCtxt<'tcx>,
encoder: &mut CacheEncoder<'_, 'tcx>,
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepNodeIndex;
use rustc_middle::dep_graph::{self, DepKind, DepKindStruct};
use rustc_middle::query::erase::{erase, restore, Erase};
use rustc_middle::query::on_disk_cache::OnDiskCache;
use rustc_middle::query::plumbing::{DynamicQuery, QuerySystem, QuerySystemFns};
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use rustc_middle::query::plumbing::{
DynamicQuery, QueryKeyStringCache, QuerySystem, QuerySystemFns,
};
use rustc_middle::query::AsLocalKey;
use rustc_middle::query::{
queries, DynamicQueries, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
Expand Down Expand Up @@ -215,7 +217,6 @@ pub fn query_system<'tcx>(
engine: engine(incremental),
local_providers,
extern_providers,
query_structs: make_dep_kind_array!(query_structs).to_vec(),
encode_query_results: encode_all_query_results,
try_mark_green: try_mark_green,
},
Expand Down
132 changes: 68 additions & 64 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ impl QueryContext for QueryCtxt<'_> {
fn try_collect_active_jobs(self) -> Option<QueryMap<DepKind>> {
let mut jobs = QueryMap::default();

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

Some(jobs)
Expand Down Expand Up @@ -183,10 +183,8 @@ pub(super) fn encode_all_query_results<'tcx>(
encoder: &mut CacheEncoder<'_, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex,
) {
for query in &tcx.query_system.fns.query_structs {
if let Some(encode) = query.encode_query_results {
encode(tcx, encoder, query_result_index);
}
for encode in super::ENCODE_QUERY_RESULTS.iter().copied().filter_map(|e| e) {
encode(tcx, encoder, query_result_index);
}
}

Expand Down Expand Up @@ -476,6 +474,16 @@ where
}
}

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 {
([], $tokens:expr) => {{
None
Expand Down Expand Up @@ -633,6 +641,43 @@ macro_rules! define_queries {
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 {
Expand All @@ -655,6 +700,23 @@ macro_rules! define_queries {
}
}

// 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: &[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment saying that those arrays are not indexed per DepKind, so should only be used for iteration?

Option<for<'tcx> fn(
TyCtxt<'tcx>,
&mut CacheEncoder<'_, 'tcx>,
&mut EncodedDepNodeIndex)
>
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use item_if_cached? Like this:

Suggested change
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
] = &[$( item_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results, ))*];

This will allow to remove the option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro has to expand to a single expression, so it's not quite that simple.


#[allow(nonstandard_style)]
mod query_callbacks {
use super::*;
Expand Down Expand Up @@ -720,64 +782,6 @@ macro_rules! define_queries {
})*
}

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>] {
arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_query_impl/src/profiling_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {

let mut string_cache = QueryKeyStringCache::new();

for query in &tcx.query_system.fns.query_structs {
(query.alloc_self_profile_query_strings)(tcx, &mut string_cache);
for alloc in super::ALLOC_SELF_PROFILE_QUERY_STRINGS.iter() {
alloc(tcx, &mut string_cache)
}
}