move global cache lookup into fn

This commit is contained in:
lcnr 2024-05-13 15:50:59 +00:00
parent 82df0c3540
commit ee0f20bb97
2 changed files with 62 additions and 58 deletions

View file

@ -134,16 +134,6 @@ impl<I: Interner> SearchGraph<I> {
self.mode
}
/// Update the stack and reached depths on cache hits.
#[instrument(level = "trace", skip(self))]
fn on_cache_hit(&mut self, additional_depth: usize, encountered_overflow: bool) {
let reached_depth = self.stack.next_index().plus(additional_depth);
if let Some(last) = self.stack.raw.last_mut() {
last.reached_depth = last.reached_depth.max(reached_depth);
last.encountered_overflow |= encountered_overflow;
}
}
/// Pops the highest goal from the stack, lazily updating the
/// the next goal in the stack.
///
@ -276,37 +266,7 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
return Self::response_no_constraints(tcx, input, Certainty::overflow(true));
};
// Try to fetch the goal from the global cache.
'global: {
let Some(CacheData { result, proof_tree, reached_depth, encountered_overflow }) =
self.global_cache(tcx).get(
tcx,
input,
|cycle_participants| {
self.stack.iter().any(|entry| cycle_participants.contains(&entry.input))
},
available_depth,
)
else {
break 'global;
};
// If we're building a proof tree and the current cache entry does not
// contain a proof tree, we do not use the entry but instead recompute
// the goal. We simply overwrite the existing entry once we're done,
// caching the proof tree.
if !inspect.is_noop() {
if let Some(revisions) = proof_tree {
inspect.goal_evaluation_kind(
inspect::WipCanonicalGoalEvaluationKind::Interned { revisions },
);
} else {
break 'global;
}
}
self.on_cache_hit(reached_depth, encountered_overflow);
debug!("global cache hit");
if let Some(result) = self.lookup_global_cache(tcx, input, available_depth, inspect) {
return result;
}
@ -388,7 +348,10 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
// This is for global caching, so we properly track query dependencies.
// Everything that affects the `result` should be performed within this
// `with_anon_task` closure.
// `with_anon_task` closure. If computing this goal depends on something
// not tracked by the cache key and from outside of this anon task, it
// must not be added to the global cache. Notably, this is the case for
// trait solver cycles participants.
let ((final_entry, result), dep_node) =
tcx.dep_graph.with_anon_task(tcx, dep_kinds::TraitSelect, || {
for _ in 0..FIXPOINT_STEP_LIMIT {
@ -446,6 +409,45 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
result
}
/// Try to fetch a previously computed result from the global cache,
/// making sure to only do so if it would match the result of reevaluating
/// this goal.
fn lookup_global_cache(
&mut self,
tcx: TyCtxt<'tcx>,
input: CanonicalInput<'tcx>,
available_depth: Limit,
inspect: &mut ProofTreeBuilder<TyCtxt<'tcx>>,
) -> Option<QueryResult<'tcx>> {
let CacheData { result, proof_tree, additional_depth, encountered_overflow } = self
.global_cache(tcx)
.get(tcx, input, self.stack.iter().map(|e| e.input), available_depth)?;
// If we're building a proof tree and the current cache entry does not
// contain a proof tree, we do not use the entry but instead recompute
// the goal. We simply overwrite the existing entry once we're done,
// caching the proof tree.
if !inspect.is_noop() {
if let Some(revisions) = proof_tree {
let kind = inspect::WipCanonicalGoalEvaluationKind::Interned { revisions };
inspect.goal_evaluation_kind(kind);
} else {
return None;
}
}
// Update the reached depth of the current goal to make sure
// its state is the same regardless of whether we've used the
// global cache or not.
let reached_depth = self.stack.next_index().plus(additional_depth);
if let Some(last) = self.stack.raw.last_mut() {
last.reached_depth = last.reached_depth.max(reached_depth);
last.encountered_overflow |= encountered_overflow;
}
Some(result)
}
}
enum StepResult<'tcx> {