Skip to content

Use a session counter to make anon dep nodes unique #139236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,7 @@ pub(super) struct CurrentDepGraph<D: Deps> {
/// ID from the previous session. In order to side-step this problem, we make
/// sure that anonymous `NodeId`s allocated in different sessions don't overlap.
/// This is implemented by mixing a session-key into the ID fingerprint of
/// each anon node. The session-key is just a random number generated when
/// the `DepGraph` is created.
/// each anon node. The session-key is a hash of the number of previous sessions.
anon_id_seed: Fingerprint,

/// These are simple counters that are for profiling and
Expand All @@ -1186,12 +1185,8 @@ impl<D: Deps> CurrentDepGraph<D> {
record_stats: bool,
previous: Arc<SerializedDepGraph>,
) -> Self {
use std::time::{SystemTime, UNIX_EPOCH};

let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
let nanos = duration.as_nanos();
let mut stable_hasher = StableHasher::new();
nanos.hash(&mut stable_hasher);
previous.session_count().hash(&mut stable_hasher);
let anon_id_seed = stable_hasher.finish();

#[cfg(debug_assertions)]
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_query_system/src/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub struct SerializedDepGraph {
/// Stores a map from fingerprints to nodes per dep node kind.
/// This is the reciprocal of `nodes`.
index: Vec<UnhashMap<PackedFingerprint, SerializedDepNodeIndex>>,
/// The number of previous compilation sessions. This is used to generate
/// unique anon dep nodes per session.
session_count: u64,
}

impl SerializedDepGraph {
Expand Down Expand Up @@ -146,6 +149,11 @@ impl SerializedDepGraph {
pub fn node_count(&self) -> usize {
self.nodes.len()
}

#[inline]
pub fn session_count(&self) -> u64 {
self.session_count
}
}

/// A packed representation of an edge's start index and byte width.
Expand Down Expand Up @@ -252,6 +260,8 @@ impl SerializedDepGraph {
.map(|_| UnhashMap::with_capacity_and_hasher(d.read_u32() as usize, Default::default()))
.collect();

let session_count = d.read_u64();

for (idx, node) in nodes.iter_enumerated() {
if index[node.kind.as_usize()].insert(node.hash, idx).is_some() {
// Side effect nodes can have duplicates
Expand All @@ -273,6 +283,7 @@ impl SerializedDepGraph {
edge_list_indices,
edge_list_data,
index,
session_count,
})
}
}
Expand Down Expand Up @@ -601,7 +612,7 @@ impl<D: Deps> EncoderState<D> {
stats: _,
kind_stats,
marker: _,
previous: _,
previous,
} = self;

let node_count = total_node_count.try_into().unwrap();
Expand All @@ -612,6 +623,8 @@ impl<D: Deps> EncoderState<D> {
count.encode(&mut encoder);
}

previous.session_count.checked_add(1).unwrap().encode(&mut encoder);

debug!(?node_count, ?edge_count);
debug!("position: {:?}", encoder.position());
IntEncodedWithFixedSize(node_count).encode(&mut encoder);
Expand Down
Loading