Skip to content

Commit fe89f32

Browse files
committed
Address review.
1 parent 65a8681 commit fe89f32

File tree

5 files changed

+45
-55
lines changed

5 files changed

+45
-55
lines changed

compiler/rustc_incremental/src/persist/dirty_clean.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,6 @@ impl DirtyCleanVisitor<'tcx> {
391391
fn assert_clean(&self, item_span: Span, dep_node: DepNode) {
392392
debug!("assert_clean({:?})", dep_node);
393393

394-
// if the node wasn't previously evaluated and now is (or vice versa),
395-
// then the node isn't actually clean or dirty.
396394
if self.tcx.dep_graph.is_red(&dep_node) {
397395
let dep_node_str = self.dep_node_str(&dep_node);
398396
self.tcx

compiler/rustc_incremental/src/persist/save.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
3434
let dep_graph_path = dep_graph_path(sess);
3535
let staging_dep_graph_path = staging_dep_graph_path(sess);
3636

37-
join(
38-
|| sess.time("assert_dep_graph", || crate::assert_dep_graph(tcx)),
39-
|| sess.time("check_dirty_clean", || dirty_clean::check_dirty_clean_annotations(tcx)),
40-
);
37+
sess.time("assert_dep_graph", || crate::assert_dep_graph(tcx));
38+
sess.time("check_dirty_clean", || dirty_clean::check_dirty_clean_annotations(tcx));
4139

4240
if sess.opts.debugging_opts.incremental_info {
4341
tcx.dep_graph.print_incremental_info()

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,10 @@ impl<K: DepKind> DepGraph<K> {
626626

627627
// There may be multiple threads trying to mark the same dep node green concurrently
628628

629-
let dep_node_index = {
630-
// We allocating an entry for the node in the current dependency graph and
631-
// adding all the appropriate edges imported from the previous graph
632-
data.current.intern_dark_green_node(&data.previous, prev_dep_node_index)
633-
};
629+
// We allocating an entry for the node in the current dependency graph and
630+
// adding all the appropriate edges imported from the previous graph
631+
let dep_node_index =
632+
data.current.promote_node_and_deps_to_current(&data.previous, prev_dep_node_index);
634633

635634
// ... emitting any stored diagnostic ...
636635

@@ -713,7 +712,7 @@ impl<K: DepKind> DepGraph<K> {
713712
}
714713
}
715714

716-
// Returns true if the given node has been marked as green during the
715+
// Returns true if the given node has been marked as red during the
717716
// current compilation session. Used in various assertions
718717
pub fn is_red(&self, dep_node: &DepNode<K>) -> bool {
719718
self.node_color(dep_node) == Some(DepNodeColor::Red)
@@ -833,17 +832,11 @@ rustc_index::newtype_index! {
833832
/// will be populated as we run queries or tasks. We never remove nodes from the
834833
/// graph: they are only added.
835834
///
836-
/// The nodes in it are identified by a `DepNodeIndex`. Internally, this maps to
837-
/// a `HybridIndex`, which identifies which collection in the `data` field
838-
/// contains a node's data. Which collection is used for a node depends on
839-
/// whether the node was present in the `PreviousDepGraph`, and if so, the color
840-
/// of the node. Each type of node can share more or less data with the previous
841-
/// graph. When possible, we can store just the index of the node in the
842-
/// previous graph, rather than duplicating its data in our own collections.
843-
/// This is important, because these graph structures are some of the largest in
844-
/// the compiler.
835+
/// The nodes in it are identified by a `DepNodeIndex`. We avoid keeping the nodes
836+
/// in memory. This is important, because these graph structures are some of the
837+
/// largest in the compiler.
845838
///
846-
/// For the same reason, we also avoid storing `DepNode`s more than once as map
839+
/// For this reason, we avoid storing `DepNode`s more than once as map
847840
/// keys. The `new_node_to_index` map only contains nodes not in the previous
848841
/// graph, and we map nodes in the previous graph to indices via a two-step
849842
/// mapping. `PreviousDepGraph` maps from `DepNode` to `SerializedDepNodeIndex`,
@@ -939,6 +932,15 @@ impl<K: DepKind> CurrentDepGraph<K> {
939932
}
940933
}
941934

935+
#[cfg(debug_assertions)]
936+
fn record_edge(&self, dep_node_index: DepNodeIndex, key: DepNode<K>) {
937+
if let Some(forbidden_edge) = &self.forbidden_edge {
938+
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
939+
}
940+
}
941+
942+
/// Writes the node to the current dep-graph and allocates a `DepNodeIndex` for it.
943+
/// Assumes that this is a node that has no equivalent in the previous dep-graph.
942944
fn intern_new_node(
943945
&self,
944946
key: DepNode<K>,
@@ -951,9 +953,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
951953
let dep_node_index = self.encoder.borrow().send(key, current_fingerprint, edges);
952954
entry.insert(dep_node_index);
953955
#[cfg(debug_assertions)]
954-
if let Some(forbidden_edge) = &self.forbidden_edge {
955-
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
956-
}
956+
self.record_edge(dep_node_index, key);
957957
dep_node_index
958958
}
959959
}
@@ -964,37 +964,35 @@ impl<K: DepKind> CurrentDepGraph<K> {
964964
prev_graph: &PreviousDepGraph<K>,
965965
key: DepNode<K>,
966966
edges: EdgesVec,
967-
current_fingerprint: Option<Fingerprint>,
967+
fingerprint: Option<Fingerprint>,
968968
print_status: bool,
969969
) -> (DepNodeIndex, Option<(SerializedDepNodeIndex, DepNodeColor)>) {
970970
let print_status = cfg!(debug_assertions) && print_status;
971971

972972
if let Some(prev_index) = prev_graph.node_to_index_opt(&key) {
973973
// Determine the color and index of the new `DepNode`.
974-
if let Some(current_fingerprint) = current_fingerprint {
975-
if current_fingerprint == prev_graph.fingerprint_by_index(prev_index) {
974+
if let Some(fingerprint) = fingerprint {
975+
if fingerprint == prev_graph.fingerprint_by_index(prev_index) {
976976
if print_status {
977977
eprintln!("[task::green] {:?}", key);
978978
}
979979

980-
// This is a light green node: it existed in the previous compilation,
980+
// This is a green node: it existed in the previous compilation,
981981
// its query was re-executed, and it has the same result as before.
982982
let mut prev_index_to_index = self.prev_index_to_index.lock();
983983

984984
let dep_node_index = match prev_index_to_index[prev_index] {
985985
Some(dep_node_index) => dep_node_index,
986986
None => {
987987
let dep_node_index =
988-
self.encoder.borrow().send(key, current_fingerprint, edges);
988+
self.encoder.borrow().send(key, fingerprint, edges);
989989
prev_index_to_index[prev_index] = Some(dep_node_index);
990990
dep_node_index
991991
}
992992
};
993993

994994
#[cfg(debug_assertions)]
995-
if let Some(forbidden_edge) = &self.forbidden_edge {
996-
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
997-
}
995+
self.record_edge(dep_node_index, key);
998996
(dep_node_index, Some((prev_index, DepNodeColor::Green(dep_node_index))))
999997
} else {
1000998
if print_status {
@@ -1009,16 +1007,14 @@ impl<K: DepKind> CurrentDepGraph<K> {
10091007
Some(dep_node_index) => dep_node_index,
10101008
None => {
10111009
let dep_node_index =
1012-
self.encoder.borrow().send(key, current_fingerprint, edges);
1010+
self.encoder.borrow().send(key, fingerprint, edges);
10131011
prev_index_to_index[prev_index] = Some(dep_node_index);
10141012
dep_node_index
10151013
}
10161014
};
10171015

10181016
#[cfg(debug_assertions)]
1019-
if let Some(forbidden_edge) = &self.forbidden_edge {
1020-
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
1021-
}
1017+
self.record_edge(dep_node_index, key);
10221018
(dep_node_index, Some((prev_index, DepNodeColor::Red)))
10231019
}
10241020
} else {
@@ -1043,26 +1039,24 @@ impl<K: DepKind> CurrentDepGraph<K> {
10431039
};
10441040

10451041
#[cfg(debug_assertions)]
1046-
if let Some(forbidden_edge) = &self.forbidden_edge {
1047-
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
1048-
}
1042+
self.record_edge(dep_node_index, key);
10491043
(dep_node_index, Some((prev_index, DepNodeColor::Red)))
10501044
}
10511045
} else {
10521046
if print_status {
10531047
eprintln!("[task::new] {:?}", key);
10541048
}
10551049

1056-
let current_fingerprint = current_fingerprint.unwrap_or(Fingerprint::ZERO);
1050+
let fingerprint = fingerprint.unwrap_or(Fingerprint::ZERO);
10571051

10581052
// This is a new node: it didn't exist in the previous compilation session.
1059-
let dep_node_index = self.intern_new_node(key, edges, current_fingerprint);
1053+
let dep_node_index = self.intern_new_node(key, edges, fingerprint);
10601054

10611055
(dep_node_index, None)
10621056
}
10631057
}
10641058

1065-
fn intern_dark_green_node(
1059+
fn promote_node_and_deps_to_current(
10661060
&self,
10671061
prev_graph: &PreviousDepGraph<K>,
10681062
prev_index: SerializedDepNodeIndex,
@@ -1086,9 +1080,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
10861080
);
10871081
prev_index_to_index[prev_index] = Some(dep_node_index);
10881082
#[cfg(debug_assertions)]
1089-
if let Some(forbidden_edge) = &self.forbidden_edge {
1090-
forbidden_edge.index_to_node.lock().insert(dep_node_index, key);
1091-
}
1083+
self.record_edge(dep_node_index, key);
10921084
dep_node_index
10931085
}
10941086
}

compiler/rustc_query_system/src/dep_graph/query.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ impl<K: DepKind> DepGraphQuery<K> {
3232

3333
for &target in edges.iter() {
3434
let target = self.dep_index_to_index[target];
35-
// Skip missing edges.
35+
// We may miss the edges that are pushed while the `DepGraphQuery` is being accessed.
36+
// Skip them to issues.
3637
if let Some(target) = target {
3738
self.graph.add_edge(source, target, ());
3839
}

compiler/rustc_query_system/src/dep_graph/serialized.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'a, K: DepKind + Decodable<opaque::Decoder<'a>>> Decodable<opaque::Decoder<
7373
{
7474
#[instrument(skip(d))]
7575
fn decode(d: &mut opaque::Decoder<'a>) -> Result<SerializedDepGraph<K>, String> {
76-
let position = d.position();
76+
let start_position = d.position();
7777

7878
// The last 16 bytes are the node count and edge count.
7979
debug!("position: {:?}", d.position());
@@ -85,7 +85,7 @@ impl<'a, K: DepKind + Decodable<opaque::Decoder<'a>>> Decodable<opaque::Decoder<
8585
debug!(?node_count, ?edge_count);
8686

8787
debug!("position: {:?}", d.position());
88-
d.set_position(position);
88+
d.set_position(start_position);
8989
debug!("position: {:?}", d.position());
9090

9191
let mut nodes = IndexVec::with_capacity(node_count);
@@ -137,15 +137,15 @@ struct Stat<K: DepKind> {
137137
edge_counter: u64,
138138
}
139139

140-
struct EncodingStatus<K: DepKind> {
140+
struct EncoderState<K: DepKind> {
141141
encoder: FileEncoder,
142142
total_node_count: usize,
143143
total_edge_count: usize,
144144
result: FileEncodeResult,
145145
stats: Option<FxHashMap<K, Stat<K>>>,
146146
}
147147

148-
impl<K: DepKind> EncodingStatus<K> {
148+
impl<K: DepKind> EncoderState<K> {
149149
fn new(encoder: FileEncoder, record_stats: bool) -> Self {
150150
Self {
151151
encoder,
@@ -186,8 +186,9 @@ impl<K: DepKind> EncodingStatus<K> {
186186

187187
debug!(?index, ?node);
188188
let encoder = &mut self.encoder;
189-
self.result =
190-
std::mem::replace(&mut self.result, Ok(())).and_then(|()| node.encode(encoder));
189+
if self.result.is_ok() {
190+
self.result = node.encode(encoder);
191+
}
191192
index
192193
}
193194

@@ -209,7 +210,7 @@ impl<K: DepKind> EncodingStatus<K> {
209210
}
210211

211212
pub struct GraphEncoder<K: DepKind> {
212-
status: Lock<EncodingStatus<K>>,
213+
status: Lock<EncoderState<K>>,
213214
record_graph: Option<Lock<DepGraphQuery<K>>>,
214215
}
215216

@@ -225,7 +226,7 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
225226
} else {
226227
None
227228
};
228-
let status = Lock::new(EncodingStatus::new(encoder, record_stats));
229+
let status = Lock::new(EncoderState::new(encoder, record_stats));
229230
GraphEncoder { status, record_graph }
230231
}
231232

0 commit comments

Comments
 (0)