Skip to content

Commit a46b8bc

Browse files
authored
Unrolled build for #151976
Rollup merge of #151976 - Zalathar:collect-active-jobs, r=nnethercote Rename `collect_active_jobs` to several distinct names Key renames: - Function `collect_active_jobs` → `collect_active_jobs_from_all_queries` (method in trait `QueryContext`) - Constant `COLLECT_ACTIVE_JOBS` → `PER_QUERY_GATHER_ACTIVE_JOBS_FNS` (list of per-query function pointers) - Function `collect_active_jobs` → `gather_active_jobs` (per-query function in `query_impl::$name`) - Function `collect_active_jobs` → `gather_active_jobs_inner` (method in `QueryState`) - Giving these four things distinct names makes it a lot easier to tell them apart! - The switch from “collect” (all queries) to “gather” (single query) is intended to make the different parts a bit more memorable and searchable; I couldn't think of a more natural way to express this distinction so I settled for two different synonyms. There should be no change to compiler behaviour. r? nnethercote (or compiler)
2 parents 46c86ae + e58538c commit a46b8bc

5 files changed

Lines changed: 52 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
@@ -254,7 +254,7 @@ internal compiler error: query cycle handler thread panicked, aborting process";
254254
|| {
255255
// Ensure there were no errors collecting all active jobs.
256256
// We need the complete map to ensure we find a cycle to break.
257-
QueryCtxt::new(tcx).collect_active_jobs(false).expect(
257+
QueryCtxt::new(tcx).collect_active_jobs_from_all_queries(false).expect(
258258
"failed to collect active queries in deadlock handler",
259259
)
260260
},

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ impl<'tcx> QueryCtxt<'tcx> {
5050
}
5151

5252
fn depth_limit_error(self, job: QueryJobId) {
53-
let query_map = self.collect_active_jobs(true).expect("failed to collect active queries");
53+
let query_map = self
54+
.collect_active_jobs_from_all_queries(true)
55+
.expect("failed to collect active queries");
5456
let (info, depth) = job.find_dep_kind_root(query_map);
5557

5658
let suggested_limit = match self.tcx.recursion_limit() {
@@ -98,7 +100,7 @@ impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
98100
tls::with_related_context(self.tcx, |icx| icx.query)
99101
}
100102

101-
/// Returns a map of currently active query jobs.
103+
/// Returns a map of currently active query jobs, collected from all queries.
102104
///
103105
/// If `require_complete` is `true`, this function locks all shards of the
104106
/// query results to produce a complete map, which always returns `Ok`.
@@ -108,12 +110,15 @@ impl<'tcx> QueryContext<'tcx> for QueryCtxt<'tcx> {
108110
/// Prefer passing `false` to `require_complete` to avoid potential deadlocks,
109111
/// especially when called from within a deadlock handler, unless a
110112
/// complete map is needed and no deadlock is possible at this call site.
111-
fn collect_active_jobs(self, require_complete: bool) -> Result<QueryMap<'tcx>, QueryMap<'tcx>> {
113+
fn collect_active_jobs_from_all_queries(
114+
self,
115+
require_complete: bool,
116+
) -> Result<QueryMap<'tcx>, QueryMap<'tcx>> {
112117
let mut jobs = QueryMap::default();
113118
let mut complete = true;
114119

115-
for collect in super::COLLECT_ACTIVE_JOBS.iter() {
116-
if collect(self.tcx, &mut jobs, require_complete).is_none() {
120+
for gather_fn in crate::PER_QUERY_GATHER_ACTIVE_JOBS_FNS.iter() {
121+
if gather_fn(self.tcx, &mut jobs, require_complete).is_none() {
117122
complete = false;
118123
}
119124
}
@@ -731,7 +736,10 @@ macro_rules! define_queries {
731736
}
732737
}
733738

734-
pub(crate) fn collect_active_jobs<'tcx>(
739+
/// Internal per-query plumbing for collecting the set of active jobs for this query.
740+
///
741+
/// Should only be called through `PER_QUERY_GATHER_ACTIVE_JOBS_FNS`.
742+
pub(crate) fn gather_active_jobs<'tcx>(
735743
tcx: TyCtxt<'tcx>,
736744
qmap: &mut QueryMap<'tcx>,
737745
require_complete: bool,
@@ -741,12 +749,15 @@ macro_rules! define_queries {
741749
let name = stringify!($name);
742750
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
743751
};
744-
let res = tcx.query_system.states.$name.collect_active_jobs(
752+
753+
// Call `gather_active_jobs_inner` to do the actual work.
754+
let res = tcx.query_system.states.$name.gather_active_jobs_inner(
745755
tcx,
746756
make_frame,
747757
qmap,
748758
require_complete,
749759
);
760+
750761
// this can be called during unwinding, and the function has a `try_`-prefix, so
751762
// don't `unwrap()` here, just manually check for `None` and do best-effort error
752763
// reporting.
@@ -816,10 +827,17 @@ macro_rules! define_queries {
816827

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

819-
const COLLECT_ACTIVE_JOBS: &[
820-
for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap<'tcx>, bool) -> Option<()>
821-
] =
822-
&[$(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),*
840+
];
823841

824842
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[
825843
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
@@ -32,6 +32,8 @@ impl<'tcx> QueryInfo<QueryStackDeferred<'tcx>> {
3232
}
3333
}
3434

35+
/// Map from query job IDs to job information collected by
36+
/// [`QueryContext::collect_active_jobs_from_all_queries`].
3537
pub type QueryMap<'tcx> = FxHashMap<QueryJobId, QueryJobInfo<'tcx>>;
3638

3739
/// A value uniquely identifying an active query job.
@@ -613,7 +615,7 @@ pub fn print_query_stack<'tcx, Qcx: QueryContext<'tcx>>(
613615
let mut count_total = 0;
614616

615617
// Make use of a partial query map if we fail to take locks collecting active queries.
616-
let query_map = match qcx.collect_active_jobs(false) {
618+
let query_map = match qcx.collect_active_jobs_from_all_queries(false) {
617619
Ok(query_map) => query_map,
618620
Err(query_map) => query_map,
619621
};

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
/// Load a side effect associated to the node in the previous session.
172175
fn load_side_effect(

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`.
84+
pub fn gather_active_jobs_inner<Qcx: Copy>(
8385
&self,
8486
qcx: Qcx,
8587
make_frame: 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()), None)

0 commit comments

Comments
 (0)