From 903f65f215812ca4d40e1efc6a5f234e0ab5728c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 9 Feb 2021 18:53:38 +0100 Subject: [PATCH] Simplify hashing. --- compiler/rustc_query_impl/src/plumbing.rs | 4 +-- compiler/rustc_query_system/src/query/job.rs | 36 ++++++++------------ compiler/rustc_query_system/src/query/mod.rs | 21 ++++++------ 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 4618671ddf076..37a176de94196 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -339,12 +339,12 @@ macro_rules! define_queries { } else { Some(key.default_span(*tcx)) }; - let hash = { + let hash = || { let mut hcx = tcx.create_stable_hashing_context(); let mut hasher = StableHasher::new(); std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher); key.hash_stable(&mut hcx, &mut hasher); - hasher.finish() + hasher.finish::() }; QueryStackFrame::new(name, description, span, hash) diff --git a/compiler/rustc_query_system/src/query/job.rs b/compiler/rustc_query_system/src/query/job.rs index 30dce1e501893..35a2ac865f259 100644 --- a/compiler/rustc_query_system/src/query/job.rs +++ b/compiler/rustc_query_system/src/query/job.rs @@ -14,9 +14,9 @@ use std::num::NonZeroU32; #[cfg(parallel_compiler)] use { + crate::dep_graph::DepKind, parking_lot::{Condvar, Mutex}, rustc_data_structures::fx::FxHashSet, - rustc_data_structures::stable_hasher::{HashStable, StableHasher}, rustc_data_structures::sync::Lock, rustc_data_structures::sync::Lrc, rustc_data_structures::{jobserver, OnDrop}, @@ -417,30 +417,23 @@ where // Deterministically pick an query from a list #[cfg(parallel_compiler)] -fn pick_query<'a, CTX, T, F>( - query_map: &QueryMap, - tcx: CTX, - queries: &'a [T], - f: F, -) -> &'a T +fn pick_query<'a, D, T, F>(query_map: &QueryMap, queries: &'a [T], f: F) -> &'a T where - CTX: QueryContext, - F: Fn(&T) -> (Span, QueryJobId), + D: Copy + Clone + Eq + Hash, + F: Fn(&T) -> (Span, QueryJobId), { // Deterministically pick an entry point // FIXME: Sort this instead - let mut hcx = tcx.dep_context().create_stable_hashing_context(); queries .iter() .min_by_key(|v| { let (span, query) = f(v); - let mut stable_hasher = StableHasher::new(); - query.query(query_map).hash_stable(&mut hcx, &mut stable_hasher); + let hash = query.query(query_map).hash; // Prefer entry points which have valid spans for nicer error messages // We add an integer to the tuple ensuring that entry points // with valid spans are picked first let span_cmp = if span == DUMMY_SP { 1 } else { 0 }; - (span_cmp, stable_hasher.finish::()) + (span_cmp, hash) }) .unwrap() } @@ -451,11 +444,10 @@ where /// If a cycle was not found, the starting query is removed from `jobs` and /// the function returns false. #[cfg(parallel_compiler)] -fn remove_cycle( - query_map: &QueryMap, - jobs: &mut Vec>, - wakelist: &mut Vec>>, - tcx: CTX, +fn remove_cycle( + query_map: &QueryMap, + jobs: &mut Vec>, + wakelist: &mut Vec>>, ) -> bool { let mut visited = FxHashSet::default(); let mut stack = Vec::new(); @@ -505,15 +497,15 @@ fn remove_cycle( None } else { // Deterministically pick one of the waiters to show to the user - let waiter = *pick_query(query_map, tcx, &waiters, |s| *s); + let waiter = *pick_query(query_map, &waiters, |s| *s); Some((span, query, Some(waiter))) } } }) - .collect::, Option<(Span, QueryJobId)>)>>(); + .collect::, Option<(Span, QueryJobId)>)>>(); // Deterministically pick an entry point - let (_, entry_point, usage) = pick_query(query_map, tcx, &entry_points, |e| (e.0, e.1)); + let (_, entry_point, usage) = pick_query(query_map, &entry_points, |e| (e.0, e.1)); // Shift the stack so that our entry point is first let entry_point_pos = stack.iter().position(|(_, query)| query == entry_point); @@ -570,7 +562,7 @@ pub fn deadlock(tcx: CTX, registry: &rayon_core::Registry) { let mut found_cycle = false; while jobs.len() > 0 { - if remove_cycle(&query_map, &mut jobs, &mut wakelist, tcx) { + if remove_cycle(&query_map, &mut jobs, &mut wakelist) { found_cycle = true; } } diff --git a/compiler/rustc_query_system/src/query/mod.rs b/compiler/rustc_query_system/src/query/mod.rs index 81e2b4877fd60..aef8a13ccef32 100644 --- a/compiler/rustc_query_system/src/query/mod.rs +++ b/compiler/rustc_query_system/src/query/mod.rs @@ -16,8 +16,6 @@ pub use self::config::{QueryAccessors, QueryConfig, QueryDescription}; use crate::dep_graph::{DepNode, DepNodeIndex, HasDepContext, SerializedDepNodeIndex}; -use rustc_data_structures::fingerprint::Fingerprint; -use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::Diagnostic; @@ -34,7 +32,8 @@ pub struct QueryStackFrame { span: Option, /// This hash is used to deterministically pick /// a query to remove cycles in the parallel compiler. - hash: Fingerprint, + #[cfg(parallel_compiler)] + hash: u64, } impl QueryStackFrame { @@ -43,9 +42,15 @@ impl QueryStackFrame { name: &'static str, description: String, span: Option, - hash: Fingerprint, + _hash: impl FnOnce() -> u64, ) -> Self { - Self { name, hash, description, span } + Self { + name, + description, + span, + #[cfg(parallel_compiler)] + hash: _hash(), + } } // FIXME(eddyb) Get more valid `Span`s on queries. @@ -58,12 +63,6 @@ impl QueryStackFrame { } } -impl HashStable for QueryStackFrame { - fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { - self.hash.hash_stable(hcx, hasher) - } -} - pub trait QueryContext: HasDepContext { /// Get string representation from DefPath. fn def_path_str(&self, def_id: DefId) -> String;