Skip to content

Commit 903f65f

Browse files
committed
Simplify hashing.
1 parent a87de89 commit 903f65f

File tree

3 files changed

+26
-35
lines changed

3 files changed

+26
-35
lines changed

compiler/rustc_query_impl/src/plumbing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ macro_rules! define_queries {
339339
} else {
340340
Some(key.default_span(*tcx))
341341
};
342-
let hash = {
342+
let hash = || {
343343
let mut hcx = tcx.create_stable_hashing_context();
344344
let mut hasher = StableHasher::new();
345345
std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
346346
key.hash_stable(&mut hcx, &mut hasher);
347-
hasher.finish()
347+
hasher.finish::<u64>()
348348
};
349349

350350
QueryStackFrame::new(name, description, span, hash)

compiler/rustc_query_system/src/query/job.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use std::num::NonZeroU32;
1414

1515
#[cfg(parallel_compiler)]
1616
use {
17+
crate::dep_graph::DepKind,
1718
parking_lot::{Condvar, Mutex},
1819
rustc_data_structures::fx::FxHashSet,
19-
rustc_data_structures::stable_hasher::{HashStable, StableHasher},
2020
rustc_data_structures::sync::Lock,
2121
rustc_data_structures::sync::Lrc,
2222
rustc_data_structures::{jobserver, OnDrop},
@@ -417,30 +417,23 @@ where
417417

418418
// Deterministically pick an query from a list
419419
#[cfg(parallel_compiler)]
420-
fn pick_query<'a, CTX, T, F>(
421-
query_map: &QueryMap<CTX::DepKind>,
422-
tcx: CTX,
423-
queries: &'a [T],
424-
f: F,
425-
) -> &'a T
420+
fn pick_query<'a, D, T, F>(query_map: &QueryMap<D>, queries: &'a [T], f: F) -> &'a T
426421
where
427-
CTX: QueryContext,
428-
F: Fn(&T) -> (Span, QueryJobId<CTX::DepKind>),
422+
D: Copy + Clone + Eq + Hash,
423+
F: Fn(&T) -> (Span, QueryJobId<D>),
429424
{
430425
// Deterministically pick an entry point
431426
// FIXME: Sort this instead
432-
let mut hcx = tcx.dep_context().create_stable_hashing_context();
433427
queries
434428
.iter()
435429
.min_by_key(|v| {
436430
let (span, query) = f(v);
437-
let mut stable_hasher = StableHasher::new();
438-
query.query(query_map).hash_stable(&mut hcx, &mut stable_hasher);
431+
let hash = query.query(query_map).hash;
439432
// Prefer entry points which have valid spans for nicer error messages
440433
// We add an integer to the tuple ensuring that entry points
441434
// with valid spans are picked first
442435
let span_cmp = if span == DUMMY_SP { 1 } else { 0 };
443-
(span_cmp, stable_hasher.finish::<u64>())
436+
(span_cmp, hash)
444437
})
445438
.unwrap()
446439
}
@@ -451,11 +444,10 @@ where
451444
/// If a cycle was not found, the starting query is removed from `jobs` and
452445
/// the function returns false.
453446
#[cfg(parallel_compiler)]
454-
fn remove_cycle<CTX: QueryContext>(
455-
query_map: &QueryMap<CTX::DepKind>,
456-
jobs: &mut Vec<QueryJobId<CTX::DepKind>>,
457-
wakelist: &mut Vec<Lrc<QueryWaiter<CTX::DepKind>>>,
458-
tcx: CTX,
447+
fn remove_cycle<D: DepKind>(
448+
query_map: &QueryMap<D>,
449+
jobs: &mut Vec<QueryJobId<D>>,
450+
wakelist: &mut Vec<Lrc<QueryWaiter<D>>>,
459451
) -> bool {
460452
let mut visited = FxHashSet::default();
461453
let mut stack = Vec::new();
@@ -505,15 +497,15 @@ fn remove_cycle<CTX: QueryContext>(
505497
None
506498
} else {
507499
// Deterministically pick one of the waiters to show to the user
508-
let waiter = *pick_query(query_map, tcx, &waiters, |s| *s);
500+
let waiter = *pick_query(query_map, &waiters, |s| *s);
509501
Some((span, query, Some(waiter)))
510502
}
511503
}
512504
})
513-
.collect::<Vec<(Span, QueryJobId<CTX::DepKind>, Option<(Span, QueryJobId<CTX::DepKind>)>)>>();
505+
.collect::<Vec<(Span, QueryJobId<D>, Option<(Span, QueryJobId<D>)>)>>();
514506

515507
// Deterministically pick an entry point
516-
let (_, entry_point, usage) = pick_query(query_map, tcx, &entry_points, |e| (e.0, e.1));
508+
let (_, entry_point, usage) = pick_query(query_map, &entry_points, |e| (e.0, e.1));
517509

518510
// Shift the stack so that our entry point is first
519511
let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point);
@@ -570,7 +562,7 @@ pub fn deadlock<CTX: QueryContext>(tcx: CTX, registry: &rayon_core::Registry) {
570562
let mut found_cycle = false;
571563

572564
while jobs.len() > 0 {
573-
if remove_cycle(&query_map, &mut jobs, &mut wakelist, tcx) {
565+
if remove_cycle(&query_map, &mut jobs, &mut wakelist) {
574566
found_cycle = true;
575567
}
576568
}

compiler/rustc_query_system/src/query/mod.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ pub use self::config::{QueryAccessors, QueryConfig, QueryDescription};
1616

1717
use crate::dep_graph::{DepNode, DepNodeIndex, HasDepContext, SerializedDepNodeIndex};
1818

19-
use rustc_data_structures::fingerprint::Fingerprint;
20-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2119
use rustc_data_structures::sync::Lock;
2220
use rustc_data_structures::thin_vec::ThinVec;
2321
use rustc_errors::Diagnostic;
@@ -34,7 +32,8 @@ pub struct QueryStackFrame {
3432
span: Option<Span>,
3533
/// This hash is used to deterministically pick
3634
/// a query to remove cycles in the parallel compiler.
37-
hash: Fingerprint,
35+
#[cfg(parallel_compiler)]
36+
hash: u64,
3837
}
3938

4039
impl QueryStackFrame {
@@ -43,9 +42,15 @@ impl QueryStackFrame {
4342
name: &'static str,
4443
description: String,
4544
span: Option<Span>,
46-
hash: Fingerprint,
45+
_hash: impl FnOnce() -> u64,
4746
) -> Self {
48-
Self { name, hash, description, span }
47+
Self {
48+
name,
49+
description,
50+
span,
51+
#[cfg(parallel_compiler)]
52+
hash: _hash(),
53+
}
4954
}
5055

5156
// FIXME(eddyb) Get more valid `Span`s on queries.
@@ -58,12 +63,6 @@ impl QueryStackFrame {
5863
}
5964
}
6065

61-
impl<CTX> HashStable<CTX> for QueryStackFrame {
62-
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
63-
self.hash.hash_stable(hcx, hasher)
64-
}
65-
}
66-
6766
pub trait QueryContext: HasDepContext {
6867
/// Get string representation from DefPath.
6968
fn def_path_str(&self, def_id: DefId) -> String;

0 commit comments

Comments
 (0)