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 2 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
173 changes: 166 additions & 7 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use session::config::OutputType;
use std::cell::{Ref, RefCell};
use std::hash::Hash;
use std::rc::Rc;
use ty::TyCtxt;
use util::common::{ProfileQueriesMsg, profq_msg};

use ich::Fingerprint;
Expand All @@ -28,6 +29,7 @@ use super::edges::{self, DepGraphEdges};
use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
use super::prev::PreviousDepGraph;


#[derive(Clone)]
pub struct DepGraph {
data: Option<Rc<DepGraphData>>,
Expand Down Expand Up @@ -59,6 +61,12 @@ impl DepNodeIndex {
};
}

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

struct DepGraphData {
/// The old, initial encoding of the dependency graph. This will soon go
/// away.
Expand All @@ -74,6 +82,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 +107,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 @@ -168,6 +179,8 @@ impl DepGraph {
R: HashStable<HCX>,
{
if let Some(ref data) = self.data {
debug_assert!(!data.colors.borrow().contains_key(&key));

data.edges.borrow_mut().push_task(key);
data.current.borrow_mut().push_task(key);
if cfg!(debug_assertions) {
Expand All @@ -192,11 +205,26 @@ 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(DepNodeIndex {
legacy: dep_node_index_legacy,
new: dep_node_index_new,
})
} 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 +256,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,7 +312,7 @@ 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)
}

Expand Down Expand Up @@ -383,6 +420,128 @@ impl DepGraph {
edge_list_data,
}
}

pub fn node_color(&self, dep_node: &DepNode) -> Option<DepNodeColor> {
self.data.as_ref().and_then(|data| data.colors.borrow().get(dep_node).cloned())
}

pub fn try_mark_green(&self,
tcx: TyCtxt,
dep_node: &DepNode)
-> Option<DepNodeIndex> {
let data = self.data.as_ref().unwrap();

debug_assert!(!data.colors.borrow().contains_key(dep_node));
debug_assert!(!data.current.borrow().node_to_node_index.contains_key(dep_node));

if dep_node.kind.is_input() {
// We should only hit try_mark_green() for inputs that do not exist
// anymore in the current compilation session. Existing inputs are
// eagerly marked as either red/green before any queries are
// executed.
debug_assert!(dep_node.extract_def_id(tcx).is_none());
return None;
}

let (prev_deps, prev_dep_node_index) = match data.previous.edges_from(dep_node) {
Some(prev) => {
// This DepNode and the corresponding query invocation existed
// in the previous compilation session too, so we can try to
// mark it as green by recursively marking all of its
// dependencies green.
prev
}
None => {
// This DepNode did not exist in the previous compilation session,
// so we cannot mark it as green.
return None
}
};

let mut current_deps = Vec::new();

for &dep_dep_node in prev_deps {
let dep_dep_node = &data.previous.index_to_node(dep_dep_node);
let dep_dep_node_color = data.colors.borrow().get(dep_dep_node).cloned();
match dep_dep_node_color {
Some(DepNodeColor::Green(node_index)) => {
// This dependency has been marked as green before, we are
// still fine and can continue with checking the other
// dependencies.
current_deps.push(node_index);
}
Some(DepNodeColor::Red) => {
// We found a dependency the value of which has changed
// compared to the previous compilation session. We cannot
// mark the DepNode as green and also don't need to bother
// with checking any of the other dependencies.
return None
}
None => {
// We don't know the state of this dependency. Let's try to
// mark it green.
if let Some(node_index) = self.try_mark_green(tcx, dep_dep_node) {
current_deps.push(node_index);
} else {
// We failed to mark it green. This can have various
// reasons.
return None
}
}
}
}

// If we got here without hitting a `return` that means that all
// dependencies of this DepNode could be marked as green. Therefore we
// can also mark this DepNode as green. We do so by...

// ... allocating an entry for it in the current dependency graph and
// adding all the appropriate edges imported from the previous graph ...
let node_index_new = data.current
.borrow_mut()
.alloc_node(*dep_node,
current_deps.iter().map(|n| n.new).collect());
let dep_node_index_legacy = {
let mut legacy_graph = data.edges.borrow_mut();
legacy_graph.push_task(*dep_node);
for node_index in current_deps.into_iter().map(|n| n.legacy) {
legacy_graph.read_index(node_index);
}
legacy_graph.pop_task(*dep_node)
};

// ... copying the fingerprint from the previous graph too, so we don't
// have to recompute it ...
let fingerprint = data.previous.fingerprint_by_index(prev_dep_node_index);
assert!(self.fingerprints
.borrow_mut()
.insert(*dep_node, fingerprint)
.is_none());

let node_index = DepNodeIndex {
legacy: dep_node_index_legacy,
new: node_index_new,
};

// ... and finally storing a "Green" entry in the color map.
assert!(data.colors
.borrow_mut()
.insert(*dep_node, DepNodeColor::Green(node_index))
.is_none());

Some(node_index)
}

// Used in various assertions
pub fn is_green(&self, dep_node_index: DepNodeIndex) -> bool {
let dep_node = self.data.as_ref().unwrap().current.borrow().nodes[dep_node_index.new];
self.data.as_ref().unwrap().colors.borrow().get(&dep_node).map(|&color| {
match color {
DepNodeColor::Red => false,
DepNodeColor::Green(_) => true,
}
}).unwrap_or(false)
}
}

/// A "work product" is an intermediate result that we save into the
Expand Down Expand Up @@ -485,7 +644,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 +673,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
2 changes: 1 addition & 1 deletion src/librustc/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod serialized;

pub use self::dep_tracking_map::{DepTrackingMap, DepTrackingMapConfig};
pub use self::dep_node::{DepNode, DepKind, DepConstructor, WorkProductId};
pub use self::graph::{DepGraph, WorkProduct, DepNodeIndex};
pub use self::graph::{DepGraph, WorkProduct, DepNodeIndex, DepNodeColor};
pub use self::prev::PreviousDepGraph;
pub use self::query::DepGraphQuery;
pub use self::safe::AssertDepGraphSafe;
Expand Down
38 changes: 26 additions & 12 deletions src/librustc/dep_graph/prev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,33 @@ impl PreviousDepGraph {
PreviousDepGraph { data, index }
}

pub fn with_edges_from<F>(&self, dep_node: &DepNode, mut f: F)
where
F: FnMut(&(DepNode, Fingerprint)),
{
let node_index = self.index[dep_node];
self.data
.edge_targets_from(node_index)
.into_iter()
.for_each(|&index| f(&self.data.nodes[index]));
#[inline]
pub fn edges_from(&self,
dep_node: &DepNode)
-> Option<(&[SerializedDepNodeIndex], SerializedDepNodeIndex)> {
self.index
.get(dep_node)
.map(|&node_index| {
(self.data.edge_targets_from(node_index), node_index)
})
}

pub fn fingerprint_of(&self, dep_node: &DepNode) -> Fingerprint {
let node_index = self.index[dep_node];
self.data.nodes[node_index].1
#[inline]
pub fn index_to_node(&self, dep_node_index: SerializedDepNodeIndex) -> DepNode {
self.data.nodes[dep_node_index].0
}

#[inline]
pub fn fingerprint_of(&self, dep_node: &DepNode) -> Option<Fingerprint> {
self.index
.get(dep_node)
.map(|&node_index| self.data.nodes[node_index].1)
}

#[inline]
pub fn fingerprint_by_index(&self,
dep_node_index: SerializedDepNodeIndex)
-> Fingerprint {
self.data.nodes[dep_node_index].1
}
}
1 change: 0 additions & 1 deletion src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use rustc_back::PanicStrategy;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::StableVec;
use std::cell::{RefCell, Cell};

use std::ops::Deref;
use std::rc::Rc;
Expand Down
Loading