Skip to content
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

incr.comp.: Switch to red/green change tracking, remove legacy system. #44901

Merged
merged 12 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 50 additions & 7 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ impl DepNodeIndex {
};
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum DepNodeColor {
Red,
Green,
Gray
}

struct DepGraphData {
/// The old, initial encoding of the dependency graph. This will soon go
/// away.
Expand All @@ -74,6 +81,8 @@ struct DepGraphData {
/// nodes and edges as well as all fingerprints of nodes that have them.
previous: PreviousDepGraph,

colors: RefCell<FxHashMap<DepNode, DepNodeColor>>,

/// When we load, there may be `.o` files, cached mir, or other such
/// things available to us. If we find that they are not dirty, we
/// load the path to the file storing those work-products here into
Expand All @@ -97,6 +106,7 @@ impl DepGraph {
dep_node_debug: RefCell::new(FxHashMap()),
current: RefCell::new(CurrentDepGraph::new()),
previous: prev_graph,
colors: RefCell::new(FxHashMap()),
})),
fingerprints: Rc::new(RefCell::new(FxHashMap())),
}
Expand Down Expand Up @@ -192,11 +202,23 @@ impl DepGraph {
let mut stable_hasher = StableHasher::new();
result.hash_stable(&mut hcx, &mut stable_hasher);

let current_fingerprint = stable_hasher.finish();

assert!(self.fingerprints
.borrow_mut()
.insert(key, stable_hasher.finish())
.insert(key, current_fingerprint)
.is_none());

let prev_fingerprint = data.previous.fingerprint_of(&key);

let color = if Some(current_fingerprint) == prev_fingerprint {
DepNodeColor::Green
} else {
DepNodeColor::Red
};

assert!(data.colors.borrow_mut().insert(key, color).is_none());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Although I am often tempted to write an assert! like this, I tend to think that putting crucial side-effect operations inside of an assert! is bad-style. Too easy to overlook. I think I'd prefer to see something like:

let old_value = data.colors.borrow_mut().insert(key, color);
assert!(old_value.is_none(), "key {:?} executed twice", key);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


(result, DepNodeIndex {
legacy: dep_node_index_legacy,
new: dep_node_index_new,
Expand Down Expand Up @@ -228,7 +250,16 @@ impl DepGraph {
data.current.borrow_mut().push_anon_task();
let result = op();
let dep_node_index_legacy = data.edges.borrow_mut().pop_anon_task(dep_kind);
let dep_node_index_new = data.current.borrow_mut().pop_anon_task(dep_kind);
let (new_dep_node, dep_node_index_new) = data.current
.borrow_mut()
.pop_anon_task(dep_kind);
if let Some(new_dep_node) = new_dep_node {
assert!(data.colors
.borrow_mut()
.insert(new_dep_node, DepNodeColor::Red)
.is_none());
}

(result, DepNodeIndex {
legacy: dep_node_index_legacy,
new: dep_node_index_new,
Expand Down Expand Up @@ -275,10 +306,22 @@ impl DepGraph {
self.fingerprints.borrow()[dep_node]
}

pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Fingerprint {
pub fn prev_fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
self.data.as_ref().unwrap().previous.fingerprint_of(dep_node)
}

pub fn node_color(&self, dep_node: &DepNode) -> DepNodeColor {
match self.data.as_ref().unwrap().colors.borrow().get(dep_node) {
Some(&color) => {
debug_assert!(color != DepNodeColor::Gray);
color
}
None => {
DepNodeColor::Gray
}
}
}

/// Indicates that a previous work product exists for `v`. This is
/// invoked during initial start-up based on what nodes are clean
/// (and what files exist in the incr. directory).
Expand Down Expand Up @@ -485,7 +528,7 @@ impl CurrentDepGraph {
});
}

fn pop_anon_task(&mut self, kind: DepKind) -> DepNodeIndexNew {
fn pop_anon_task(&mut self, kind: DepKind) -> (Option<DepNode>, DepNodeIndexNew) {
let popped_node = self.task_stack.pop().unwrap();

if let OpenTask::Anon {
Expand Down Expand Up @@ -514,10 +557,10 @@ impl CurrentDepGraph {
};

if let Some(&index) = self.node_to_node_index.get(&target_dep_node) {
return index;
(None, index)
} else {
(Some(target_dep_node), self.alloc_node(target_dep_node, reads))
}

self.alloc_node(target_dep_node, reads)
} else {
bug!("pop_anon_task() - Expected anonymous task to be popped")
}
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/dep_graph/prev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ impl PreviousDepGraph {
.for_each(|&index| f(&self.data.nodes[index]));
}

pub fn fingerprint_of(&self, dep_node: &DepNode) -> Fingerprint {
let node_index = self.index[dep_node];
self.data.nodes[node_index].1
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
self.index
.get(dep_node)
.map(|&node_index| self.data.nodes[node_index].1)
}
}
4 changes: 2 additions & 2 deletions src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
let current_fingerprint = self.tcx.dep_graph.fingerprint_of(&dep_node);
let prev_fingerprint = self.tcx.dep_graph.prev_fingerprint_of(&dep_node);

if current_fingerprint == prev_fingerprint {
if Some(current_fingerprint) == prev_fingerprint {
let dep_node_str = self.dep_node_str(&dep_node);
self.tcx.sess.span_err(
item_span,
Expand All @@ -136,7 +136,7 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
let current_fingerprint = self.tcx.dep_graph.fingerprint_of(&dep_node);
let prev_fingerprint = self.tcx.dep_graph.prev_fingerprint_of(&dep_node);

if current_fingerprint != prev_fingerprint {
if Some(current_fingerprint) != prev_fingerprint {
let dep_node_str = self.dep_node_str(&dep_node);
self.tcx.sess.span_err(
item_span,
Expand Down