From 6b0beecd9597153ddea2e00f4ce3fea4e63e93ab Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 26 Feb 2026 08:23:04 +1100 Subject: [PATCH] Remove `PER_QUERY_GATHER_ACTIVE_JOBS_FNS`. And also `gather_active_jobs` for each query. This is done by generating `collect_active_jobs_from_all_queries` and having it do things more directly. --- compiler/rustc_query_impl/src/execution.rs | 13 ++-- compiler/rustc_query_impl/src/job.rs | 4 +- compiler/rustc_query_impl/src/lib.rs | 1 - compiler/rustc_query_impl/src/plumbing.rs | 89 ++++++++-------------- 4 files changed, 42 insertions(+), 65 deletions(-) diff --git a/compiler/rustc_query_impl/src/execution.rs b/compiler/rustc_query_impl/src/execution.rs index 856a6692adc9..ec67702507fb 100644 --- a/compiler/rustc_query_impl/src/execution.rs +++ b/compiler/rustc_query_impl/src/execution.rs @@ -16,11 +16,10 @@ use rustc_middle::verify_ich::incremental_verify_ich; use rustc_span::{DUMMY_SP, Span}; +use crate::collect_active_jobs_from_all_queries; use crate::dep_graph::{DepNode, DepNodeIndex}; use crate::job::{QueryJobInfo, QueryJobMap, find_cycle_in_stack, report_cycle}; -use crate::plumbing::{ - collect_active_jobs_from_all_queries, current_query_job, next_job_id, start_query, -}; +use crate::plumbing::{current_query_job, next_job_id, start_query}; #[inline] fn equivalent_key(k: &K) -> impl Fn(&(K, V)) -> bool + '_ { @@ -44,8 +43,12 @@ pub(crate) fn all_inactive<'tcx, K>(state: &QueryState<'tcx, K>) -> bool { /// Internal plumbing for collecting the set of active jobs for this query. /// -/// Should only be called from `gather_active_jobs`. -pub(crate) fn gather_active_jobs_inner<'tcx, C>( +/// Should only be called from `collect_active_jobs_from_all_queries`. +/// +/// (We arbitrarily use the word "gather" when collecting the jobs for +/// each individual query, so that we have distinct function names to +/// grep for.) +pub(crate) fn gather_active_jobs<'tcx, C>( query: &'tcx QueryVTable<'tcx, C>, tcx: TyCtxt<'tcx>, require_complete: bool, diff --git a/compiler/rustc_query_impl/src/job.rs b/compiler/rustc_query_impl/src/job.rs index 2d9824a783ea..228c611df365 100644 --- a/compiler/rustc_query_impl/src/job.rs +++ b/compiler/rustc_query_impl/src/job.rs @@ -14,7 +14,7 @@ use rustc_session::Session; use rustc_span::{DUMMY_SP, Span}; -use crate::plumbing::collect_active_jobs_from_all_queries; +use crate::collect_active_jobs_from_all_queries; /// Map from query job IDs to job information collected by /// `collect_active_jobs_from_all_queries`. @@ -26,7 +26,7 @@ pub struct QueryJobMap<'tcx> { impl<'tcx> QueryJobMap<'tcx> { /// Adds information about a job ID to the job map. /// - /// Should only be called by `gather_active_jobs_inner`. + /// Should only be called by `gather_active_jobs`. pub(crate) fn insert(&mut self, id: QueryJobId, info: QueryJobInfo<'tcx>) { self.map.insert(id, info); } diff --git a/compiler/rustc_query_impl/src/lib.rs b/compiler/rustc_query_impl/src/lib.rs index f1a4f3cad25a..cbaab61d3c7a 100644 --- a/compiler/rustc_query_impl/src/lib.rs +++ b/compiler/rustc_query_impl/src/lib.rs @@ -19,7 +19,6 @@ pub use crate::dep_kind_vtables::make_dep_kind_vtables; pub use crate::job::{QueryJobMap, break_query_cycles, print_query_stack}; -pub use crate::plumbing::collect_active_jobs_from_all_queries; use crate::plumbing::{encode_all_query_results, try_mark_green}; use crate::profiling_support::QueryKeyStringCache; pub use crate::profiling_support::alloc_self_profile_query_strings; diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 8ac6b385b889..98985fd5f172 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -29,9 +29,10 @@ use rustc_serialize::{Decodable, Encodable}; use rustc_span::def_id::LOCAL_CRATE; +use crate::collect_active_jobs_from_all_queries; use crate::error::{QueryOverflow, QueryOverflowNote}; use crate::execution::{all_inactive, force_query}; -use crate::job::{QueryJobMap, find_dep_kind_root}; +use crate::job::find_dep_kind_root; fn depth_limit_error<'tcx>(tcx: TyCtxt<'tcx>, job: QueryJobId) { let job_map = @@ -94,32 +95,6 @@ pub(crate) fn start_query<'tcx, R>( }) } -/// Returns a map of currently active query jobs, collected from all queries. -/// -/// If `require_complete` is `true`, this function locks all shards of the -/// query results to produce a complete map, which always returns `Ok`. -/// Otherwise, it may return an incomplete map as an error if any shard -/// lock cannot be acquired. -/// -/// Prefer passing `false` to `require_complete` to avoid potential deadlocks, -/// especially when called from within a deadlock handler, unless a -/// complete map is needed and no deadlock is possible at this call site. -pub fn collect_active_jobs_from_all_queries<'tcx>( - tcx: TyCtxt<'tcx>, - require_complete: bool, -) -> Result, QueryJobMap<'tcx>> { - let mut job_map_out = QueryJobMap::default(); - let mut complete = true; - - for gather_fn in crate::PER_QUERY_GATHER_ACTIVE_JOBS_FNS.iter() { - if gather_fn(tcx, require_complete, &mut job_map_out).is_none() { - complete = false; - } - } - - if complete { Ok(job_map_out) } else { Err(job_map_out) } -} - pub(super) fn try_mark_green<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool { tcx.dep_graph.try_mark_green(tcx, dep_node).is_some() } @@ -618,22 +593,6 @@ fn query_vtable(tcx: TyCtxt<'tcx>) -> &'tcx QueryVTable<'tcx, Self::Cache> { } } - /// Internal per-query plumbing for collecting the set of active jobs for this query. - /// - /// Should only be called through `PER_QUERY_GATHER_ACTIVE_JOBS_FNS`. - pub(crate) fn gather_active_jobs<'tcx>( - tcx: TyCtxt<'tcx>, - require_complete: bool, - job_map_out: &mut QueryJobMap<'tcx>, - ) -> Option<()> { - crate::execution::gather_active_jobs_inner( - &tcx.query_system.query_vtables.$name, - tcx, - require_complete, - job_map_out, - ) - } - pub(crate) fn alloc_self_profile_query_strings<'tcx>( tcx: TyCtxt<'tcx>, string_cache: &mut QueryKeyStringCache @@ -672,21 +631,37 @@ pub fn make_query_vtables<'tcx>(incremental: bool) -> queries::QueryVTables<'tcx // These arrays are used for iteration and can't be indexed by `DepKind`. - /// Used by `collect_active_jobs_from_all_queries` to iterate over all - /// queries, and gather the active jobs for each query. + /// Returns a map of currently active query jobs, collected from all queries. /// - /// (We arbitrarily use the word "gather" when collecting the jobs for - /// each individual query, so that we have distinct function names to - /// grep for.) - const PER_QUERY_GATHER_ACTIVE_JOBS_FNS: &[ - for<'tcx> fn( - tcx: TyCtxt<'tcx>, - require_complete: bool, - job_map_out: &mut QueryJobMap<'tcx>, - ) -> Option<()> - ] = &[ - $( $crate::query_impl::$name::gather_active_jobs ),* - ]; + /// If `require_complete` is `true`, this function locks all shards of the + /// query results to produce a complete map, which always returns `Ok`. + /// Otherwise, it may return an incomplete map as an error if any shard + /// lock cannot be acquired. + /// + /// Prefer passing `false` to `require_complete` to avoid potential deadlocks, + /// especially when called from within a deadlock handler, unless a + /// complete map is needed and no deadlock is possible at this call site. + pub fn collect_active_jobs_from_all_queries<'tcx>( + tcx: TyCtxt<'tcx>, + require_complete: bool, + ) -> Result, QueryJobMap<'tcx>> { + let mut job_map_out = QueryJobMap::default(); + let mut complete = true; + + $( + let res = crate::execution::gather_active_jobs( + &tcx.query_system.query_vtables.$name, + tcx, + require_complete, + &mut job_map_out, + ); + if res.is_none() { + complete = false; + } + )* + + if complete { Ok(job_map_out) } else { Err(job_map_out) } + } const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[ for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)