Skip to content

Commit dea5507

Browse files
committed
Rename collect_active_jobs to several distinct names
1 parent f60a0f1 commit dea5507

5 files changed

Lines changed: 50 additions & 21 deletions

File tree

compiler/rustc_interface/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub(crate) fn run_in_thread_pool_with_globals<
247247
let query_map = rustc_span::set_session_globals_then(unsafe { &*(session_globals as *const SessionGlobals) }, || {
248248
// Ensure there was no errors collecting all active jobs.
249249
// We need the complete map to ensure we find a cycle to break.
250-
QueryCtxt::new(tcx).collect_active_jobs(false).expect("failed to collect active queries in deadlock handler")
250+
QueryCtxt::new(tcx).collect_active_jobs_from_all_queries(false).expect("failed to collect active queries in deadlock handler")
251251
});
252252
break_query_cycles(query_map, &registry);
253253
})

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
8181
tls::with_related_context(self.tcx, |icx| icx.query)
8282
}
8383

84-
/// Returns a map of currently active query jobs.
84+
/// Returns a map of currently active query jobs, collected from all queries.
8585
///
8686
/// If `require_complete` is `true`, this function locks all shards of the
8787
/// query results to produce a complete map, which always returns `Ok`.
@@ -91,12 +91,15 @@ impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
9191
/// Prefer passing `false` to `require_complete` to avoid potential deadlocks,
9292
/// especially when called from within a deadlock handler, unless a
9393
/// complete map is needed and no deadlock is possible at this call site.
94-
fn collect_active_jobs(self, require_complete: bool) -> Result<QueryMap<'tcx>, QueryMap<'tcx>> {
94+
fn collect_active_jobs_from_all_queries(
95+
self,
96+
require_complete: bool,
97+
) -> Result<QueryMap<'tcx>, QueryMap<'tcx>> {
9598
let mut jobs = QueryMap::default();
9699
let mut complete = true;
97100

98-
for collect in super::COLLECT_ACTIVE_JOBS.iter() {
99-
if collect(self.tcx, &mut jobs, require_complete).is_none() {
101+
for gather_fn in crate::PER_QUERY_GATHER_ACTIVE_JOBS_FNS.iter() {
102+
if gather_fn(self.tcx, &mut jobs, require_complete).is_none() {
100103
complete = false;
101104
}
102105
}
@@ -164,7 +167,9 @@ impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
164167
}
165168

166169
fn depth_limit_error(self, job: QueryJobId) {
167-
let query_map = self.collect_active_jobs(true).expect("failed to collect active queries");
170+
let query_map = self
171+
.collect_active_jobs_from_all_queries(true)
172+
.expect("failed to collect active queries");
168173
let (info, depth) = job.find_dep_kind_root(query_map);
169174

170175
let suggested_limit = match self.tcx.recursion_limit() {
@@ -733,7 +738,10 @@ macro_rules! define_queries {
733738
}
734739
}
735740

736-
pub(crate) fn collect_active_jobs<'tcx>(
741+
/// Internal per-query plumbing for collecting the set of active jobs for this query.
742+
///
743+
/// Should only be called through `PER_QUERY_GATHER_ACTIVE_JOBS_FNS`.
744+
pub(crate) fn gather_active_jobs_outer<'tcx>(
737745
tcx: TyCtxt<'tcx>,
738746
qmap: &mut QueryMap<'tcx>,
739747
require_complete: bool,
@@ -743,7 +751,8 @@ macro_rules! define_queries {
743751
let name = stringify!($name);
744752
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
745753
};
746-
let res = tcx.query_system.states.$name.collect_active_jobs(
754+
// We can call `gather_active_jobs_inner` here.
755+
let res = tcx.query_system.states.$name.gather_active_jobs_inner(
747756
tcx,
748757
make_query,
749758
qmap,
@@ -818,10 +827,17 @@ macro_rules! define_queries {
818827

819828
// These arrays are used for iteration and can't be indexed by `DepKind`.
820829

821-
const COLLECT_ACTIVE_JOBS: &[
822-
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<'tcx>, bool) -> Option<()>
823-
] =
824-
&[$(query_impl::$name::collect_active_jobs),*];
830+
/// Used by `collect_active_jobs_from_all_queries` to iterate over all
831+
/// queries, and gather the active jobs for each query.
832+
///
833+
/// (We arbitrarily use the word "gather" when collecting the jobs for
834+
/// each individual query, so that we have distinct function names to
835+
/// grep for.)
836+
const PER_QUERY_GATHER_ACTIVE_JOBS_FNS: &[
837+
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<'tcx>, require_complete: bool) -> Option<()>
838+
] = &[
839+
$(query_impl::$name::gather_active_jobs_outer),*
840+
];
825841

826842
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
827843
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryKeyStringCache)

compiler/rustc_query_system/src/query/job.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ impl<'tcx> QueryInfo<QueryStackDeferred<'tcx>> {
3535
}
3636
}
3737

38+
/// Map from query job IDs to job information collected by
39+
/// [`QueryContext::collect_active_jobs_from_all_queries`].
3840
pub type QueryMap<'tcx> = FxHashMap<QueryJobId, QueryJobInfo<'tcx>>;
3941

4042
/// A value uniquely identifying an active query job.
@@ -616,7 +618,7 @@ pub fn print_query_stack<'tcx, Qcx: QueryContext<'tcx>>(
616618
let mut count_total = 0;
617619

618620
// Make use of a partial query map if we fail to take locks collecting active queries.
619-
let query_map = match qcx.collect_active_jobs(false) {
621+
let query_map = match qcx.collect_active_jobs_from_all_queries(false) {
620622
Ok(query_map) => query_map,
621623
Err(query_map) => query_map,
622624
};

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ pub trait QueryContext<'tcx>: HasDepContext {
166166
/// Get the query information from the TLS context.
167167
fn current_query_job(self) -> Option<QueryJobId>;
168168

169-
fn collect_active_jobs(self, require_complete: bool) -> Result<QueryMap<'tcx>, QueryMap<'tcx>>;
169+
fn collect_active_jobs_from_all_queries(
170+
self,
171+
require_complete: bool,
172+
) -> Result<QueryMap<'tcx>, QueryMap<'tcx>>;
170173

171174
fn lift_query_info(self, info: &QueryStackDeferred<'tcx>) -> QueryStackFrameExtra;
172175

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_data_structures::fingerprint::Fingerprint;
1111
use rustc_data_structures::hash_table::{self, Entry, HashTable};
1212
use rustc_data_structures::sharded::{self, Sharded};
1313
use rustc_data_structures::stack::ensure_sufficient_stack;
14-
use rustc_data_structures::sync::LockGuard;
1514
use rustc_data_structures::{outline, sync};
1615
use rustc_errors::{Diag, FatalError, StashKey};
1716
use rustc_span::{DUMMY_SP, Span};
@@ -79,7 +78,10 @@ where
7978
self.active.lock_shards().all(|shard| shard.is_empty())
8079
}
8180

82-
pub fn collect_active_jobs<Qcx: Copy>(
81+
/// Internal plumbing for collecting the set of active jobs for this query.
82+
///
83+
/// Should only be called from `gather_active_jobs_outer`.
84+
pub fn gather_active_jobs_inner<Qcx: Copy>(
8385
&self,
8486
qcx: Qcx,
8587
make_query: fn(Qcx, K) -> QueryStackFrame<QueryStackDeferred<'tcx>>,
@@ -88,23 +90,26 @@ where
8890
) -> Option<()> {
8991
let mut active = Vec::new();
9092

91-
let mut collect = |iter: LockGuard<'_, HashTable<(K, ActiveKeyStatus<'tcx>)>>| {
92-
for (k, v) in iter.iter() {
93+
// Helper to gather active jobs from a single shard.
94+
let mut gather_shard_jobs = |shard: &HashTable<(K, ActiveKeyStatus<'tcx>)>| {
95+
for (k, v) in shard.iter() {
9396
if let ActiveKeyStatus::Started(ref job) = *v {
9497
active.push((*k, job.clone()));
9598
}
9699
}
97100
};
98101

102+
// Lock shards and gather jobs from each shard.
99103
if require_complete {
100104
for shard in self.active.lock_shards() {
101-
collect(shard);
105+
gather_shard_jobs(&shard);
102106
}
103107
} else {
104108
// We use try_lock_shards here since we are called from the
105109
// deadlock handler, and this shouldn't be locked.
106110
for shard in self.active.try_lock_shards() {
107-
collect(shard?);
111+
let shard = shard?;
112+
gather_shard_jobs(&shard);
108113
}
109114
}
110115

@@ -294,7 +299,10 @@ where
294299
{
295300
// Ensure there was no errors collecting all active jobs.
296301
// We need the complete map to ensure we find a cycle to break.
297-
let query_map = qcx.collect_active_jobs(false).ok().expect("failed to collect active queries");
302+
let query_map = qcx
303+
.collect_active_jobs_from_all_queries(false)
304+
.ok()
305+
.expect("failed to collect active queries");
298306

299307
let error = try_execute.find_cycle_in_stack(query_map, &qcx.current_query_job(), span);
300308
(mk_cycle(query, qcx, error.lift(qcx)), None)

0 commit comments

Comments
 (0)