Skip to content

Commit 43e9502

Browse files
committed
Add a flag to check depnodes for collisions.
1 parent 6fa1fff commit 43e9502

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn build_dep_graph(
170170
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
171171

172172
Some(DepGraph::new(
173-
&sess.prof,
173+
sess,
174174
prev_graph,
175175
prev_work_products,
176176
encoder,

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_data_structures::steal::Steal;
88
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering};
99
use rustc_index::IndexVec;
1010
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
11+
use rustc_session::Session;
1112
use smallvec::{smallvec, SmallVec};
1213
use std::assert_matches::assert_matches;
1314
use std::collections::hash_map::Entry;
@@ -114,7 +115,7 @@ where
114115

115116
impl<K: DepKind> DepGraph<K> {
116117
pub fn new(
117-
profiler: &SelfProfilerRef,
118+
session: &Session,
118119
prev_graph: SerializedDepGraph<K>,
119120
prev_work_products: FxHashMap<WorkProductId, WorkProduct>,
120121
encoder: FileEncoder,
@@ -124,7 +125,7 @@ impl<K: DepKind> DepGraph<K> {
124125
let prev_graph_node_count = prev_graph.node_count();
125126

126127
let current = CurrentDepGraph::new(
127-
profiler,
128+
session,
128129
prev_graph_node_count,
129130
encoder,
130131
record_graph,
@@ -135,7 +136,7 @@ impl<K: DepKind> DepGraph<K> {
135136

136137
// Instantiate a dependy-less node only once for anonymous queries.
137138
let _green_node_index = current.intern_new_node(
138-
profiler,
139+
&session.prof,
139140
DepNode { kind: DepKind::NULL, hash: current.anon_id_seed.into() },
140141
smallvec![],
141142
Fingerprint::ZERO,
@@ -144,7 +145,7 @@ impl<K: DepKind> DepGraph<K> {
144145

145146
// Instantiate a dependy-less red node only once for anonymous queries.
146147
let (red_node_index, red_node_prev_index_and_color) = current.intern_node(
147-
profiler,
148+
&session.prof,
148149
&prev_graph,
149150
DepNode { kind: DepKind::RED, hash: Fingerprint::ZERO.into() },
150151
smallvec![],
@@ -1075,6 +1076,11 @@ pub(super) struct CurrentDepGraph<K: DepKind> {
10751076
#[cfg(debug_assertions)]
10761077
forbidden_edge: Option<EdgeFilter<K>>,
10771078

1079+
/// Used to verify the absence of hash collisions among DepNodes.
1080+
/// This field is only `Some` if the `-Z incremental_verify_depnodes` option is present.
1081+
#[cfg(debug_assertions)]
1082+
seen_dep_nodes: Option<Lock<FxHashSet<DepNode<K>>>>,
1083+
10781084
/// Anonymous `DepNode`s are nodes whose IDs we compute from the list of
10791085
/// their edges. This has the beneficial side-effect that multiple anonymous
10801086
/// nodes can be coalesced into one without changing the semantics of the
@@ -1102,7 +1108,7 @@ pub(super) struct CurrentDepGraph<K: DepKind> {
11021108

11031109
impl<K: DepKind> CurrentDepGraph<K> {
11041110
fn new(
1105-
profiler: &SelfProfilerRef,
1111+
session: &Session,
11061112
prev_graph_node_count: usize,
11071113
encoder: FileEncoder,
11081114
record_graph: bool,
@@ -1132,7 +1138,8 @@ impl<K: DepKind> CurrentDepGraph<K> {
11321138

11331139
let new_node_count_estimate = 102 * prev_graph_node_count / 100 + 200;
11341140

1135-
let node_intern_event_id = profiler
1141+
let node_intern_event_id = session
1142+
.prof
11361143
.get_or_alloc_cached_string("incr_comp_intern_dep_graph_node")
11371144
.map(EventId::from_label);
11381145

@@ -1155,6 +1162,13 @@ impl<K: DepKind> CurrentDepGraph<K> {
11551162
forbidden_edge,
11561163
#[cfg(debug_assertions)]
11571164
fingerprints: Lock::new(IndexVec::from_elem_n(None, new_node_count_estimate)),
1165+
#[cfg(debug_assertions)]
1166+
seen_dep_nodes: session.opts.unstable_opts.incremental_verify_depnodes.then(|| {
1167+
Lock::new(FxHashSet::with_capacity_and_hasher(
1168+
new_node_count_estimate,
1169+
Default::default(),
1170+
))
1171+
}),
11581172
total_read_count: AtomicU64::new(0),
11591173
total_duplicate_read_count: AtomicU64::new(0),
11601174
node_intern_event_id,
@@ -1185,6 +1199,13 @@ impl<K: DepKind> CurrentDepGraph<K> {
11851199
#[cfg(debug_assertions)]
11861200
self.record_edge(dep_node_index, key, current_fingerprint);
11871201

1202+
#[cfg(debug_assertions)]
1203+
if let Some(ref seen_dep_nodes) = self.seen_dep_nodes {
1204+
if !seen_dep_nodes.lock().insert(key) {
1205+
panic!("Found duplicate dep-node {key:?}");
1206+
}
1207+
}
1208+
11881209
dep_node_index
11891210
}
11901211

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,8 @@ options! {
14831483
#[rustc_lint_opt_deny_field_access("use `Session::incremental_relative_spans` instead of this field")]
14841484
incremental_relative_spans: bool = (false, parse_bool, [TRACKED],
14851485
"hash spans relative to their parent item for incr. comp. (default: no)"),
1486+
incremental_verify_depnodes: bool = (false, parse_bool, [UNTRACKED],
1487+
"verify incr. comp. dep-nodes for hash collisions (default: no)"),
14861488
incremental_verify_ich: bool = (false, parse_bool, [UNTRACKED],
14871489
"verify incr. comp. hashes of green query instances (default: no)"),
14881490
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],

0 commit comments

Comments
 (0)