Skip to content

Commit 012c356

Browse files
committed
Select cache values to verify by key fingerprint, not value fingerprint
1 parent e7b5955 commit 012c356

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,15 @@ impl DepGraphData {
705705
self.previous.value_fingerprint_for_index(prev_index)
706706
}
707707

708+
/// The number of incremental sessions in this graph's lineage, from
709+
/// [`SerializedDepGraph::session_count`]. Advances by one per successful
710+
/// session; a failed session does not commit a graph, so a re-run sees
711+
/// the same count.
712+
#[inline]
713+
pub fn session_count(&self) -> u64 {
714+
self.previous.session_count()
715+
}
716+
708717
#[inline]
709718
pub(crate) fn prev_node_of(&self, prev_index: SerializedDepNodeIndex) -> &DepNode {
710719
self.previous.index_to_node(prev_index)

compiler/rustc_query_impl/src/execution.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::hash::Hash;
22
use std::mem::ManuallyDrop;
33

4-
use rustc_data_structures::fingerprint::Fingerprint;
4+
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
55
use rustc_data_structures::hash_table::{Entry, HashTable};
66
use rustc_data_structures::stack::ensure_sufficient_stack;
77
use rustc_data_structures::sync::{DynSend, DynSync};
@@ -490,12 +490,21 @@ fn execute_job_incr<'tcx, C: QueryCache>(
490490
/// specified, re-hash results from the cache and make sure that they have the
491491
/// expected fingerprint.
492492
///
493-
/// If not, we still seek to verify a subset of fingerprints loaded from disk.
494-
/// Re-hashing results is fairly expensive, so we can't currently afford to
495-
/// verify every hash. This subset should still give us some coverage of
496-
/// potential bugs.
497-
pub(crate) fn should_verify_loaded_value(tcx: TyCtxt<'_>, prev_fingerprint: Fingerprint) -> bool {
498-
prev_fingerprint.split().1.as_u64().is_multiple_of(32)
493+
/// If not, we still verify a subset: re-hashing is too expensive to do for
494+
/// every value. The subset rotates with the session count, covering the whole
495+
/// cache every 32 sessions, and is deterministic so that a verification
496+
/// failure reproduces on retry.
497+
///
498+
/// `to_smaller_hash` mixes both fingerprint halves because neither half is
499+
/// evenly distributed on its own (`DefPathHash` keys share the
500+
/// `StableCrateId`, `HirId` keys contain a sequential id).
501+
pub(crate) fn should_verify_loaded_value(
502+
tcx: TyCtxt<'_>,
503+
dep_graph_data: &DepGraphData,
504+
key_fingerprint: PackedFingerprint,
505+
) -> bool {
506+
let hash = Fingerprint::from(key_fingerprint).to_smaller_hash().as_u64();
507+
hash % 32 == dep_graph_data.session_count() % 32
499508
|| tcx.sess.opts.unstable_opts.incremental_verify_ich
500509
}
501510

@@ -532,8 +541,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
532541
dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
533542
}
534543

535-
let prev_fingerprint = dep_graph_data.prev_value_fingerprint_of(prev_index);
536-
let verify = should_verify_loaded_value(tcx, prev_fingerprint);
544+
let verify = should_verify_loaded_value(tcx, dep_graph_data, dep_node.key_fingerprint);
537545

538546
(value, verify)
539547
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ pub(crate) fn promote_from_disk_inner<'tcx, C: QueryCache>(
179179

180180
// Verify the fingerprints of the same subset of loaded values as
181181
// `load_from_disk_or_invoke_provider_green` does.
182-
let prev_fingerprint = dep_graph_data.prev_value_fingerprint_of(prev_index);
183-
if should_verify_loaded_value(tcx, prev_fingerprint) {
182+
if should_verify_loaded_value(tcx, dep_graph_data, dep_node.key_fingerprint) {
184183
incremental_verify_ich(
185184
tcx,
186185
dep_graph_data,

0 commit comments

Comments
 (0)