incr.comp.: Address review comments.

This commit is contained in:
Michael Woerister 2017-10-04 12:35:56 +02:00
parent c96d0bff94
commit 0454a41bec
2 changed files with 71 additions and 31 deletions

View File

@ -52,7 +52,7 @@ pub struct DepNodeIndex {
impl Idx for DepNodeIndex { impl Idx for DepNodeIndex {
fn new(idx: usize) -> Self { fn new(idx: usize) -> Self {
assert!((idx & 0xFFFF_FFFF) == idx); debug_assert!((idx & 0xFFFF_FFFF) == idx);
DepNodeIndex { index: idx as u32 } DepNodeIndex { index: idx as u32 }
} }
fn index(self) -> usize { fn index(self) -> usize {
@ -228,20 +228,31 @@ impl DepGraph {
let current_fingerprint = stable_hasher.finish(); let current_fingerprint = stable_hasher.finish();
assert!(self.fingerprints // Store the current fingerprint
.borrow_mut() {
.insert(key, current_fingerprint) let old_value = self.fingerprints
.is_none()); .borrow_mut()
.insert(key, current_fingerprint);
debug_assert!(old_value.is_none(),
"DepGraph::with_task() - Duplicate fingerprint \
insertion for {:?}", key);
}
let prev_fingerprint = data.previous.fingerprint_of(&key); // Determine the color of the new DepNode.
{
let prev_fingerprint = data.previous.fingerprint_of(&key);
let color = if Some(current_fingerprint) == prev_fingerprint { let color = if Some(current_fingerprint) == prev_fingerprint {
DepNodeColor::Green(dep_node_index) DepNodeColor::Green(dep_node_index)
} else { } else {
DepNodeColor::Red DepNodeColor::Red
}; };
assert!(data.colors.borrow_mut().insert(key, color).is_none()); let old_value = data.colors.borrow_mut().insert(key, color);
debug_assert!(old_value.is_none(),
"DepGraph::with_task() - Duplicate DepNodeColor \
insertion for {:?}", key);
}
(result, dep_node_index) (result, dep_node_index)
} else { } else {
@ -250,10 +261,12 @@ impl DepGraph {
let result = task(cx, arg); let result = task(cx, arg);
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
result.hash_stable(&mut hcx, &mut stable_hasher); result.hash_stable(&mut hcx, &mut stable_hasher);
assert!(self.fingerprints let old_value = self.fingerprints
.borrow_mut() .borrow_mut()
.insert(key, stable_hasher.finish()) .insert(key, stable_hasher.finish());
.is_none()); debug_assert!(old_value.is_none(),
"DepGraph::with_task() - Duplicate fingerprint \
insertion for {:?}", key);
(result, DepNodeIndex::INVALID) (result, DepNodeIndex::INVALID)
} else { } else {
(task(cx, arg), DepNodeIndex::INVALID) (task(cx, arg), DepNodeIndex::INVALID)
@ -549,16 +562,20 @@ impl DepGraph {
// ... copying the fingerprint from the previous graph too, so we don't // ... copying the fingerprint from the previous graph too, so we don't
// have to recompute it ... // have to recompute it ...
let fingerprint = data.previous.fingerprint_by_index(prev_dep_node_index); let fingerprint = data.previous.fingerprint_by_index(prev_dep_node_index);
assert!(self.fingerprints let old_fingerprint = self.fingerprints
.borrow_mut() .borrow_mut()
.insert(*dep_node, fingerprint) .insert(*dep_node, fingerprint);
.is_none()); debug_assert!(old_fingerprint.is_none(),
"DepGraph::try_mark_green() - Duplicate fingerprint \
insertion for {:?}", dep_node);
// ... and finally storing a "Green" entry in the color map. // ... and finally storing a "Green" entry in the color map.
assert!(data.colors let old_color = data.colors
.borrow_mut() .borrow_mut()
.insert(*dep_node, DepNodeColor::Green(dep_node_index)) .insert(*dep_node, DepNodeColor::Green(dep_node_index));
.is_none()); debug_assert!(old_color.is_none(),
"DepGraph::try_mark_green() - Duplicate DepNodeColor \
insertion for {:?}", dep_node);
debug!("try_mark_green({:?}) - END - successfully marked as green", dep_node.kind); debug!("try_mark_green({:?}) - END - successfully marked as green", dep_node.kind);
Some(dep_node_index) Some(dep_node_index)
@ -637,9 +654,21 @@ pub(super) struct CurrentDepGraph {
nodes: IndexVec<DepNodeIndex, DepNode>, nodes: IndexVec<DepNodeIndex, DepNode>,
edges: IndexVec<DepNodeIndex, Vec<DepNodeIndex>>, edges: IndexVec<DepNodeIndex, Vec<DepNodeIndex>>,
node_to_node_index: FxHashMap<DepNode, DepNodeIndex>, node_to_node_index: FxHashMap<DepNode, DepNodeIndex>,
anon_id_seed: Fingerprint,
task_stack: Vec<OpenTask>, task_stack: Vec<OpenTask>,
forbidden_edge: Option<EdgeFilter>, forbidden_edge: Option<EdgeFilter>,
// Anonymous DepNodes are nodes the ID of which we compute from the list of
// their edges. This has the beneficial side-effect that multiple anonymous
// nodes can be coalesced into one without changing the semantics of the
// dependency graph. However, the merging of nodes can lead to a subtle
// problem during red-green marking: The color of an anonymous node from
// the current session might "shadow" the color of the node with the same
// ID from the previous session. In order to side-step this problem, we make
// sure that anon-node IDs allocated in different sessions don't overlap.
// This is implemented by mixing a session-key into the ID fingerprint of
// each anon node. The session-key is just a random number generated when
// the DepGraph is created.
anon_id_seed: Fingerprint,
} }
impl CurrentDepGraph { impl CurrentDepGraph {

View File

@ -609,10 +609,19 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
use ty::maps::keys::Key; use ty::maps::keys::Key;
use hir::def_id::LOCAL_CRATE; use hir::def_id::LOCAL_CRATE;
// We should never get into the situation of having to force this from the DepNode. // We must avoid ever having to call force_from_dep_node() for a
// Since we cannot reconstruct the query key, we would always end up having to evaluate // DepNode::CodegenUnit:
// the first caller of this query that *is* reconstructible. This might very well be // Since we cannot reconstruct the query key of a DepNode::CodegenUnit, we
// compile_codegen_unit() in which case we'd lose all re-use. // would always end up having to evaluate the first caller of the
// `codegen_unit` query that *is* reconstructible. This might very well be
// the `compile_codegen_unit` query, thus re-translating the whole CGU just
// to re-trigger calling the `codegen_unit` query with the right key. At
// that point we would already have re-done all the work we are trying to
// avoid doing in the first place.
// The solution is simple: Just explicitly call the `codegen_unit` query for
// each CGU, right after partitioning. This way `try_mark_green` will always
// hit the cache instead of having to go through `force_from_dep_node`.
// This assertion makes sure, we actually keep applying the solution above.
debug_assert!(dep_node.kind != DepKind::CodegenUnit, debug_assert!(dep_node.kind != DepKind::CodegenUnit,
"calling force_from_dep_node() on DepKind::CodegenUnit"); "calling force_from_dep_node() on DepKind::CodegenUnit");
@ -666,6 +675,8 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
} }
}; };
// FIXME(#45015): We should try move this boilerplate code into a macro
// somehow.
match dep_node.kind { match dep_node.kind {
// These are inputs that are expected to be pre-allocated and that // These are inputs that are expected to be pre-allocated and that
// should therefore always be red or green already // should therefore always be red or green already
@ -695,13 +706,13 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::CodegenUnit | DepKind::CodegenUnit |
DepKind::CompileCodegenUnit | DepKind::CompileCodegenUnit |
// This one is just odd // These are just odd
DepKind::Null | DepKind::Null |
DepKind::WorkProduct => { DepKind::WorkProduct => {
bug!("force_from_dep_node() - Encountered {:?}", dep_node.kind) bug!("force_from_dep_node() - Encountered {:?}", dep_node.kind)
} }
// These is not a queries // These are not queries
DepKind::CoherenceCheckTrait | DepKind::CoherenceCheckTrait |
DepKind::ItemVarianceConstraints => { DepKind::ItemVarianceConstraints => {
return false return false