Skip to content

Commit

Permalink
have on_completion record subcycles
Browse files Browse the repository at this point in the history
Rework `on_completion` method so that it removes all
provisional cache entries that are "below" a completed
node (while leaving those entries that are not below
the node).

This corrects an imprecise result that could in turn lead
to an incremental compilation failure. Under the old
scheme, if you had:

* A depends on...
     * B depends on A
     * C depends on...
         * D depends on C
     * T: 'static

then the provisional results for A, B, C, and D would all
be entangled. Thus, if A was `EvaluatedToOkModuloRegions`
(because of that final condition), then the result for C and
D would also be demoted to "ok modulo regions".

In reality, though, the result for C depends only on C and itself,
and is not dependent on regions. If we happen to evaluate the
cycle starting from C, we would never reach A, and hence the
result would be "ok".

Under the new scheme, the provisional results for C and D
are moved to the permanent cache immediately and are not affected
by the result of A.
  • Loading branch information
nikomatsakis committed May 11, 2021
1 parent 506e75c commit 8c7b7a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(drain_filter)]
#![feature(hash_drain_filter)]
#![feature(in_band_lifetimes)]
#![feature(iter_zip)]
#![feature(never_type)]
Expand Down
89 changes: 40 additions & 49 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

if let Some(result) = stack.cache().get_provisional(fresh_trait_ref) {
debug!(?result, "PROVISIONAL CACHE HIT");
stack.update_reached_depth(stack.cache().current_reached_depth());
return Ok(result);
stack.update_reached_depth(result.reached_depth);
return Ok(result.result);
}

// Check if this is a match for something already on the
Expand All @@ -661,7 +661,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
debug!(?result, "CACHE MISS");
self.insert_evaluation_cache(obligation.param_env, fresh_trait_ref, dep_node, result);

stack.cache().on_completion(stack.depth, |fresh_trait_ref, provisional_result| {
stack.cache().on_completion(stack.dfn, |fresh_trait_ref, provisional_result| {
self.insert_evaluation_cache(
obligation.param_env,
fresh_trait_ref,
Expand Down Expand Up @@ -2235,23 +2235,6 @@ struct ProvisionalEvaluationCache<'tcx> {
/// next "depth first number" to issue -- just a counter
dfn: Cell<usize>,

/// Stores the "coldest" depth (bottom of stack) reached by any of
/// the evaluation entries. The idea here is that all things in the provisional
/// cache are always dependent on *something* that is colder in the stack:
/// therefore, if we add a new entry that is dependent on something *colder still*,
/// we have to modify the depth for all entries at once.
///
/// Example:
///
/// Imagine we have a stack `A B C D E` (with `E` being the top of
/// the stack). We cache something with depth 2, which means that
/// it was dependent on C. Then we pop E but go on and process a
/// new node F: A B C D F. Now F adds something to the cache with
/// depth 1, meaning it is dependent on B. Our original cache
/// entry is also dependent on B, because there is a path from E
/// to C and then from C to F and from F to B.
reached_depth: Cell<usize>,

/// Map from cache key to the provisionally evaluated thing.
/// The cache entries contain the result but also the DFN in which they
/// were added. The DFN is used to clear out values on failure.
Expand All @@ -2275,12 +2258,13 @@ struct ProvisionalEvaluationCache<'tcx> {
#[derive(Copy, Clone, Debug)]
struct ProvisionalEvaluation {
from_dfn: usize,
reached_depth: usize,
result: EvaluationResult,
}

impl<'tcx> Default for ProvisionalEvaluationCache<'tcx> {
fn default() -> Self {
Self { dfn: Cell::new(0), reached_depth: Cell::new(usize::MAX), map: Default::default() }
Self { dfn: Cell::new(0), map: Default::default() }
}
}

Expand All @@ -2295,22 +2279,17 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
/// Check the provisional cache for any result for
/// `fresh_trait_ref`. If there is a hit, then you must consider
/// it an access to the stack slots at depth
/// `self.current_reached_depth()` and above.
fn get_provisional(&self, fresh_trait_ref: ty::PolyTraitRef<'tcx>) -> Option<EvaluationResult> {
/// `reached_depth` (from the returned value).
fn get_provisional(
&self,
fresh_trait_ref: ty::PolyTraitRef<'tcx>,
) -> Option<ProvisionalEvaluation> {
debug!(
?fresh_trait_ref,
reached_depth = ?self.reached_depth.get(),
"get_provisional = {:#?}",
self.map.borrow().get(&fresh_trait_ref),
);
Some(self.map.borrow().get(&fresh_trait_ref)?.result)
}

/// Current value of the `reached_depth` counter -- all the
/// provisional cache entries are dependent on the item at this
/// depth.
fn current_reached_depth(&self) -> usize {
self.reached_depth.get()
Some(self.map.borrow().get(&fresh_trait_ref)?.clone())
}

/// Insert a provisional result into the cache. The result came
Expand All @@ -2324,13 +2303,10 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
fresh_trait_ref: ty::PolyTraitRef<'tcx>,
result: EvaluationResult,
) {
debug!(?from_dfn, ?reached_depth, ?fresh_trait_ref, ?result, "insert_provisional");
let r_d = self.reached_depth.get();
self.reached_depth.set(r_d.min(reached_depth));

debug!(reached_depth = self.reached_depth.get());

self.map.borrow_mut().insert(fresh_trait_ref, ProvisionalEvaluation { from_dfn, result });
debug!(?from_dfn, ?fresh_trait_ref, ?result, "insert_provisional");
self.map
.borrow_mut()
.insert(fresh_trait_ref, ProvisionalEvaluation { from_dfn, reached_depth, result });
}

/// Invoked when the node with dfn `dfn` does not get a successful
Expand Down Expand Up @@ -2358,25 +2334,40 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
/// was a failure, then `on_failure` should have been invoked
/// already). The callback `op` will be invoked for each
/// provisional entry that we can now confirm.
///
/// Note that we may still have provisional cache items remaining
/// in the cache when this is done. For example, if there is a
/// cycle:
///
/// * A depends on...
/// * B depends on A
/// * C depends on...
/// * D depends on C
/// * ...
///
/// Then as we complete the C node we will have a provisional cache
/// with results for A, B, C, and D. This method would clear out
/// the C and D results, but leave A and B provisional.
///
/// This is determined based on the DFN: we remove any provisional
/// results created since `dfn` started (e.g., in our example, dfn
/// would be 2, representing the C node, and hence we would
/// remove the result for D, which has DFN 3, but not the results for
/// A and B, which have DFNs 0 and 1 respectively).
fn on_completion(
&self,
depth: usize,
dfn: usize,
mut op: impl FnMut(ty::PolyTraitRef<'tcx>, EvaluationResult),
) {
debug!(?depth, reached_depth = ?self.reached_depth.get(), "on_completion");
debug!(?dfn, "on_completion");

if self.reached_depth.get() < depth {
debug!("on_completion: did not yet reach depth to complete");
return;
}

for (fresh_trait_ref, eval) in self.map.borrow_mut().drain() {
for (fresh_trait_ref, eval) in
self.map.borrow_mut().drain_filter(|_k, eval| eval.from_dfn >= dfn)
{
debug!(?fresh_trait_ref, ?eval, "on_completion");

op(fresh_trait_ref, eval.result);
}

self.reached_depth.set(usize::MAX);
}
}

Expand Down

0 comments on commit 8c7b7a0

Please sign in to comment.