Skip to content

Commit ca42dd6

Browse files
committed
Sanity check fingerprints in the dep-graph.
1 parent 7c45772 commit ca42dd6

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,11 @@ pub(super) struct CurrentDepGraph<K: DepKind> {
916916
new_node_to_index: Sharded<FxHashMap<DepNode<K>, DepNodeIndex>>,
917917
prev_index_to_index: Lock<IndexVec<SerializedDepNodeIndex, Option<DepNodeIndex>>>,
918918

919+
/// This is used to verify that fingerprints do not change between the creation of a node
920+
/// and its recomputation.
921+
#[cfg(debug_assertions)]
922+
fingerprints: Lock<FxHashMap<DepNode<K>, Fingerprint>>,
923+
919924
/// Used to trap when a specific edge is added to the graph.
920925
/// This is used for debug purposes and is only active with `debug_assertions`.
921926
#[cfg(debug_assertions)]
@@ -999,17 +1004,27 @@ impl<K: DepKind> CurrentDepGraph<K> {
9991004
anon_id_seed,
10001005
#[cfg(debug_assertions)]
10011006
forbidden_edge,
1007+
#[cfg(debug_assertions)]
1008+
fingerprints: Lock::new(Default::default()),
10021009
total_read_count: AtomicU64::new(0),
10031010
total_duplicate_read_count: AtomicU64::new(0),
10041011
node_intern_event_id,
10051012
}
10061013
}
10071014

10081015
#[cfg(debug_assertions)]
1009-
fn record_edge(&self, dep_node_index: DepNodeIndex, key: DepNode<K>) {
1016+
fn record_edge(&self, dep_node_index: DepNodeIndex, key: DepNode<K>, fingerprint: Fingerprint) {
10101017
if let Some(forbidden_edge) = &self.forbidden_edge {
10111018
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
10121019
}
1020+
match self.fingerprints.lock().entry(key) {
1021+
Entry::Vacant(v) => {
1022+
v.insert(fingerprint);
1023+
}
1024+
Entry::Occupied(o) => {
1025+
assert_eq!(*o.get(), fingerprint, "Unstable fingerprints for {:?}", key);
1026+
}
1027+
}
10131028
}
10141029

10151030
/// Writes the node to the current dep-graph and allocates a `DepNodeIndex` for it.
@@ -1021,17 +1036,21 @@ impl<K: DepKind> CurrentDepGraph<K> {
10211036
edges: EdgesVec,
10221037
current_fingerprint: Fingerprint,
10231038
) -> DepNodeIndex {
1024-
match self.new_node_to_index.get_shard_by_value(&key).lock().entry(key) {
1039+
let dep_node_index = match self.new_node_to_index.get_shard_by_value(&key).lock().entry(key)
1040+
{
10251041
Entry::Occupied(entry) => *entry.get(),
10261042
Entry::Vacant(entry) => {
10271043
let dep_node_index =
10281044
self.encoder.borrow().send(profiler, key, current_fingerprint, edges);
10291045
entry.insert(dep_node_index);
1030-
#[cfg(debug_assertions)]
1031-
self.record_edge(dep_node_index, key);
10321046
dep_node_index
10331047
}
1034-
}
1048+
};
1049+
1050+
#[cfg(debug_assertions)]
1051+
self.record_edge(dep_node_index, key, current_fingerprint);
1052+
1053+
dep_node_index
10351054
}
10361055

10371056
fn intern_node(
@@ -1072,7 +1091,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
10721091
};
10731092

10741093
#[cfg(debug_assertions)]
1075-
self.record_edge(dep_node_index, key);
1094+
self.record_edge(dep_node_index, key, fingerprint);
10761095
(dep_node_index, Some((prev_index, DepNodeColor::Green(dep_node_index))))
10771096
} else {
10781097
if print_status {
@@ -1094,7 +1113,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
10941113
};
10951114

10961115
#[cfg(debug_assertions)]
1097-
self.record_edge(dep_node_index, key);
1116+
self.record_edge(dep_node_index, key, fingerprint);
10981117
(dep_node_index, Some((prev_index, DepNodeColor::Red)))
10991118
}
11001119
} else {
@@ -1119,7 +1138,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
11191138
};
11201139

11211140
#[cfg(debug_assertions)]
1122-
self.record_edge(dep_node_index, key);
1141+
self.record_edge(dep_node_index, key, Fingerprint::ZERO);
11231142
(dep_node_index, Some((prev_index, DepNodeColor::Red)))
11241143
}
11251144
} else {
@@ -1150,19 +1169,16 @@ impl<K: DepKind> CurrentDepGraph<K> {
11501169
Some(dep_node_index) => dep_node_index,
11511170
None => {
11521171
let key = prev_graph.index_to_node(prev_index);
1153-
let dep_node_index = self.encoder.borrow().send(
1154-
profiler,
1155-
key,
1156-
prev_graph.fingerprint_by_index(prev_index),
1157-
prev_graph
1158-
.edge_targets_from(prev_index)
1159-
.iter()
1160-
.map(|i| prev_index_to_index[*i].unwrap())
1161-
.collect(),
1162-
);
1172+
let edges = prev_graph
1173+
.edge_targets_from(prev_index)
1174+
.iter()
1175+
.map(|i| prev_index_to_index[*i].unwrap())
1176+
.collect();
1177+
let fingerprint = prev_graph.fingerprint_by_index(prev_index);
1178+
let dep_node_index = self.encoder.borrow().send(profiler, key, fingerprint, edges);
11631179
prev_index_to_index[prev_index] = Some(dep_node_index);
11641180
#[cfg(debug_assertions)]
1165-
self.record_edge(dep_node_index, key);
1181+
self.record_edge(dep_node_index, key, fingerprint);
11661182
dep_node_index
11671183
}
11681184
}

compiler/rustc_query_system/src/query/caches.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ where
117117
let mut lock = self.cache.get_shard_by_value(&key).lock();
118118
#[cfg(not(parallel_compiler))]
119119
let mut lock = self.cache.lock();
120+
// We may be overwriting another value. This is all right, since the dep-graph
121+
// will check that the fingerprint matches.
120122
lock.insert(key, (value.clone(), index));
121123
value
122124
}
@@ -202,6 +204,8 @@ where
202204
let mut lock = self.cache.get_shard_by_value(&key).lock();
203205
#[cfg(not(parallel_compiler))]
204206
let mut lock = self.cache.lock();
207+
// We may be overwriting another value. This is all right, since the dep-graph
208+
// will check that the fingerprint matches.
205209
lock.insert(key, value);
206210
&value.0
207211
}

0 commit comments

Comments
 (0)