Refactor try_mark_previous_green.

This commit is contained in:
Camille GILLOT 2020-11-01 16:55:13 +01:00
parent c2c59ae304
commit 91444af87a

View file

@ -488,6 +488,117 @@ impl<K: DepKind> DepGraph<K> {
}
}
fn try_mark_parent_green<Ctxt: QueryContext<DepKind = K>>(
&self,
tcx: Ctxt,
data: &DepGraphData<K>,
parent_dep_node_index: SerializedDepNodeIndex,
dep_node: &DepNode<K>,
) -> Option<()> {
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
let dep_dep_node = &data.previous.index_to_node(parent_dep_node_index);
match dep_dep_node_color {
Some(DepNodeColor::Green(_)) => {
// This dependency has been marked as green before, we are
// still fine and can continue with checking the other
// dependencies.
debug!(
"try_mark_previous_green({:?}) --- found dependency {:?} to \
be immediately green",
dep_node, dep_dep_node,
);
return Some(());
}
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.
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} was immediately red",
dep_node, dep_dep_node,
);
return None;
}
None => {}
}
// We don't know the state of this dependency. If it isn't
// an eval_always node, let's try to mark it green recursively.
if !dep_dep_node.kind.is_eval_always() {
debug!(
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
is unknown, trying to mark it green",
dep_node, dep_dep_node, dep_dep_node.hash,
);
let node_index =
self.try_mark_previous_green(tcx, data, parent_dep_node_index, dep_dep_node);
if node_index.is_some() {
debug!(
"try_mark_previous_green({:?}) --- managed to MARK dependency {:?} as green",
dep_node, dep_dep_node
);
return Some(());
}
}
// We failed to mark it green, so we try to force the query.
debug!(
"try_mark_previous_green({:?}) --- trying to force dependency {:?}",
dep_node, dep_dep_node
);
if !tcx.try_force_from_dep_node(dep_dep_node) {
// The DepNode could not be forced.
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} could not be forced",
dep_node, dep_dep_node
);
return None;
}
let dep_dep_node_color = data.colors.get(parent_dep_node_index);
match dep_dep_node_color {
Some(DepNodeColor::Green(_)) => {
debug!(
"try_mark_previous_green({:?}) --- managed to FORCE dependency {:?} to green",
dep_node, dep_dep_node
);
return Some(());
}
Some(DepNodeColor::Red) => {
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} was red after forcing",
dep_node, dep_dep_node
);
return None;
}
None => {}
}
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
}
// If the query we just forced has resulted in
// some kind of compilation error, we cannot rely on
// the dep-node color having been properly updated.
// This means that the query system has reached an
// invalid state. We let the compiler continue (by
// returning `None`) so it can emit error messages
// and wind down, but rely on the fact that this
// invalid state will not be persisted to the
// incremental compilation cache because of
// compilation errors being present.
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} resulted in compilation error",
dep_node, dep_dep_node
);
return None;
}
/// Try to mark a dep-node which existed in the previous compilation session as green.
fn try_mark_previous_green<Ctxt: QueryContext<DepKind = K>>(
&self,
@ -512,123 +623,7 @@ impl<K: DepKind> DepGraph<K> {
let prev_deps = data.previous.edge_targets_from(prev_dep_node_index);
for &dep_dep_node_index in prev_deps {
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
match dep_dep_node_color {
Some(DepNodeColor::Green(_)) => {
// This dependency has been marked as green before, we are
// still fine and can continue with checking the other
// dependencies.
debug!(
"try_mark_previous_green({:?}) --- found dependency {:?} to \
be immediately green",
dep_node,
data.previous.index_to_node(dep_dep_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.
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} was \
immediately red",
dep_node,
data.previous.index_to_node(dep_dep_node_index)
);
return None;
}
None => {
let dep_dep_node = &data.previous.index_to_node(dep_dep_node_index);
// We don't know the state of this dependency. If it isn't
// an eval_always node, let's try to mark it green recursively.
if !dep_dep_node.kind.is_eval_always() {
debug!(
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
is unknown, trying to mark it green",
dep_node, dep_dep_node, dep_dep_node.hash,
);
let node_index = self.try_mark_previous_green(
tcx,
data,
dep_dep_node_index,
dep_dep_node,
);
if node_index.is_some() {
debug!(
"try_mark_previous_green({:?}) --- managed to MARK \
dependency {:?} as green",
dep_node, dep_dep_node
);
continue;
}
}
// We failed to mark it green, so we try to force the query.
debug!(
"try_mark_previous_green({:?}) --- trying to force \
dependency {:?}",
dep_node, dep_dep_node
);
if tcx.try_force_from_dep_node(dep_dep_node) {
let dep_dep_node_color = data.colors.get(dep_dep_node_index);
match dep_dep_node_color {
Some(DepNodeColor::Green(_)) => {
debug!(
"try_mark_previous_green({:?}) --- managed to \
FORCE dependency {:?} to green",
dep_node, dep_dep_node
);
}
Some(DepNodeColor::Red) => {
debug!(
"try_mark_previous_green({:?}) - END - \
dependency {:?} was red after forcing",
dep_node, dep_dep_node
);
return None;
}
None => {
if !tcx.dep_context().sess().has_errors_or_delayed_span_bugs() {
panic!(
"try_mark_previous_green() - Forcing the DepNode \
should have set its color"
)
} else {
// If the query we just forced has resulted in
// some kind of compilation error, we cannot rely on
// the dep-node color having been properly updated.
// This means that the query system has reached an
// invalid state. We let the compiler continue (by
// returning `None`) so it can emit error messages
// and wind down, but rely on the fact that this
// invalid state will not be persisted to the
// incremental compilation cache because of
// compilation errors being present.
debug!(
"try_mark_previous_green({:?}) - END - \
dependency {:?} resulted in compilation error",
dep_node, dep_dep_node
);
return None;
}
}
}
} else {
// The DepNode could not be forced.
debug!(
"try_mark_previous_green({:?}) - END - dependency {:?} \
could not be forced",
dep_node, dep_dep_node
);
return None;
}
}
}
self.try_mark_parent_green(tcx, data, dep_dep_node_index, dep_node)?
}
// If we got here without hitting a `return` that means that all