Skip to content

Commit 6188578

Browse files
authored
Unrolled build for #152229
Rollup merge of #152229 - Zoxc:rem-det-pick, r=nnethercote Remove deterministic picking from query cycle handling This removes the deterministic picking of queries from the query cycle handling. The sets we pick from are themselves non-deterministic, making this ineffective. There may be additional entry points to the cycle which we won't discover until after we resume from the cycle handler and run more code.
2 parents c78a294 + 9f7d502 commit 6188578

5 files changed

Lines changed: 31 additions & 61 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4492,7 +4492,6 @@ dependencies = [
44924492
"rustc_abi",
44934493
"rustc_data_structures",
44944494
"rustc_errors",
4495-
"rustc_hashes",
44964495
"rustc_hir",
44974496
"rustc_index",
44984497
"rustc_macros",

compiler/rustc_middle/src/query/stack.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::mem::transmute;
44
use std::sync::Arc;
55

66
use rustc_data_structures::sync::{DynSend, DynSync};
7-
use rustc_hashes::Hash64;
87
use rustc_hir::def::DefKind;
98
use rustc_span::Span;
109
use rustc_span::def_id::DefId;
@@ -23,9 +22,6 @@ pub struct QueryStackFrame<I> {
2322
pub info: I,
2423

2524
pub dep_kind: DepKind,
26-
/// This hash is used to deterministically pick
27-
/// a query to remove cycles in the parallel compiler.
28-
pub hash: Hash64,
2925
pub def_id: Option<DefId>,
3026
/// A def-id that is extracted from a `Ty` in a query key
3127
pub def_id_for_ty_in_cycle: Option<DefId>,
@@ -36,18 +32,16 @@ impl<'tcx> QueryStackFrame<QueryStackDeferred<'tcx>> {
3632
pub fn new(
3733
info: QueryStackDeferred<'tcx>,
3834
dep_kind: DepKind,
39-
hash: Hash64,
4035
def_id: Option<DefId>,
4136
def_id_for_ty_in_cycle: Option<DefId>,
4237
) -> Self {
43-
Self { info, def_id, dep_kind, hash, def_id_for_ty_in_cycle }
38+
Self { info, def_id, dep_kind, def_id_for_ty_in_cycle }
4439
}
4540

4641
pub fn lift(&self) -> QueryStackFrame<QueryStackFrameExtra> {
4742
QueryStackFrame {
4843
info: self.info.extract(),
4944
dep_kind: self.dep_kind,
50-
hash: self.hash,
5145
def_id: self.def_id,
5246
def_id_for_ty_in_cycle: self.def_id_for_ty_in_cycle,
5347
}

compiler/rustc_query_impl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ measureme = "12.0.1"
99
rustc_abi = { path = "../rustc_abi" }
1010
rustc_data_structures = { path = "../rustc_data_structures" }
1111
rustc_errors = { path = "../rustc_errors" }
12-
rustc_hashes = { path = "../rustc_hashes" }
1312
rustc_hir = { path = "../rustc_hir" }
1413
rustc_index = { path = "../rustc_index" }
1514
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_query_impl/src/job.rs

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -209,27 +209,6 @@ fn connected_to_root<'tcx>(
209209
visit_waiters(job_map, query, |_, successor| connected_to_root(job_map, successor, visited))
210210
}
211211

212-
// Deterministically pick an query from a list
213-
fn pick_query<'a, 'tcx, T, F>(job_map: &QueryJobMap<'tcx>, queries: &'a [T], f: F) -> &'a T
214-
where
215-
F: Fn(&T) -> (Span, QueryJobId),
216-
{
217-
// Deterministically pick an entry point
218-
// FIXME: Sort this instead
219-
queries
220-
.iter()
221-
.min_by_key(|v| {
222-
let (span, query) = f(v);
223-
let hash = job_map.frame_of(query).hash;
224-
// Prefer entry points which have valid spans for nicer error messages
225-
// We add an integer to the tuple ensuring that entry points
226-
// with valid spans are picked first
227-
let span_cmp = if span == DUMMY_SP { 1 } else { 0 };
228-
(span_cmp, hash)
229-
})
230-
.unwrap()
231-
}
232-
233212
/// Looks for query cycles starting from the last query in `jobs`.
234213
/// If a cycle is found, all queries in the cycle is removed from `jobs` and
235214
/// the function return true.
@@ -263,48 +242,56 @@ fn remove_cycle<'tcx>(
263242
}
264243
}
265244

245+
struct EntryPoint {
246+
query_in_cycle: QueryJobId,
247+
waiter: Option<(Span, QueryJobId)>,
248+
}
249+
266250
// Find the queries in the cycle which are
267251
// connected to queries outside the cycle
268252
let entry_points = stack
269253
.iter()
270-
.filter_map(|&(span, query)| {
271-
if job_map.parent_of(query).is_none() {
254+
.filter_map(|&(_, query_in_cycle)| {
255+
if job_map.parent_of(query_in_cycle).is_none() {
272256
// This query is connected to the root (it has no query parent)
273-
Some((span, query, None))
257+
Some(EntryPoint { query_in_cycle, waiter: None })
274258
} else {
275-
let mut waiters = Vec::new();
276-
// Find all the direct waiters who lead to the root
277-
let _ = visit_waiters(job_map, query, |span, waiter| {
259+
let mut waiter_on_cycle = None;
260+
// Find a direct waiter who leads to the root
261+
let _ = visit_waiters(job_map, query_in_cycle, |span, waiter| {
278262
// Mark all the other queries in the cycle as already visited
279263
let mut visited = FxHashSet::from_iter(stack.iter().map(|q| q.1));
280264

281265
if connected_to_root(job_map, waiter, &mut visited).is_break() {
282-
waiters.push((span, waiter));
266+
waiter_on_cycle = Some((span, waiter));
267+
ControlFlow::Break(None)
268+
} else {
269+
ControlFlow::Continue(())
283270
}
284-
285-
ControlFlow::Continue(())
286271
});
287-
if waiters.is_empty() {
288-
None
289-
} else {
290-
// Deterministically pick one of the waiters to show to the user
291-
let waiter = *pick_query(job_map, &waiters, |s| *s);
292-
Some((span, query, Some(waiter)))
293-
}
272+
273+
waiter_on_cycle.map(|waiter_on_cycle| EntryPoint {
274+
query_in_cycle,
275+
waiter: Some(waiter_on_cycle),
276+
})
294277
}
295278
})
296-
.collect::<Vec<(Span, QueryJobId, Option<(Span, QueryJobId)>)>>();
279+
.collect::<Vec<EntryPoint>>();
297280

298-
// Deterministically pick an entry point
299-
let (_, entry_point, usage) = pick_query(job_map, &entry_points, |e| (e.0, e.1));
281+
// Pick an entry point, preferring ones with waiters
282+
let entry_point = entry_points
283+
.iter()
284+
.find(|entry_point| entry_point.waiter.is_some())
285+
.unwrap_or(&entry_points[0]);
300286

301287
// Shift the stack so that our entry point is first
302-
let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point);
288+
let entry_point_pos =
289+
stack.iter().position(|(_, query)| *query == entry_point.query_in_cycle);
303290
if let Some(pos) = entry_point_pos {
304291
stack.rotate_left(pos);
305292
}
306293

307-
let usage = usage.map(|(span, job)| (span, job_map.frame_of(job).clone()));
294+
let usage = entry_point.waiter.map(|(span, job)| (span, job_map.frame_of(job).clone()));
308295

309296
// Create the cycle error
310297
let error = CycleError {

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
55
use std::num::NonZero;
66

7-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
87
use rustc_data_structures::sync::{DynSend, DynSync};
98
use rustc_data_structures::unord::UnordMap;
10-
use rustc_hashes::Hash64;
119
use rustc_hir::def_id::DefId;
1210
use rustc_hir::limit::Limit;
1311
use rustc_index::Idx;
@@ -319,18 +317,11 @@ where
319317
{
320318
let kind = vtable.dep_kind;
321319

322-
let hash = tcx.with_stable_hashing_context(|mut hcx| {
323-
let mut hasher = StableHasher::new();
324-
kind.as_usize().hash_stable(&mut hcx, &mut hasher);
325-
key.hash_stable(&mut hcx, &mut hasher);
326-
hasher.finish::<Hash64>()
327-
});
328-
329320
let def_id: Option<DefId> = key.key_as_def_id();
330321
let def_id_for_ty_in_cycle: Option<DefId> = key.def_id_for_ty_in_cycle();
331322

332323
let info = QueryStackDeferred::new((tcx, vtable, key), mk_query_stack_frame_extra);
333-
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
324+
QueryStackFrame::new(info, kind, def_id, def_id_for_ty_in_cycle)
334325
}
335326

336327
pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: QueryFlags>(

0 commit comments

Comments
 (0)