move global cache lookup into fn
This commit is contained in:
parent
82df0c3540
commit
ee0f20bb97
2 changed files with 62 additions and 58 deletions
|
@ -14,11 +14,11 @@ pub struct EvaluationCache<'tcx> {
|
||||||
map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>,
|
map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
pub struct CacheData<'tcx> {
|
pub struct CacheData<'tcx> {
|
||||||
pub result: QueryResult<'tcx>,
|
pub result: QueryResult<'tcx>,
|
||||||
pub proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
|
pub proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
|
||||||
pub reached_depth: usize,
|
pub additional_depth: usize,
|
||||||
pub encountered_overflow: bool,
|
pub encountered_overflow: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ impl<'tcx> EvaluationCache<'tcx> {
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
key: CanonicalInput<'tcx>,
|
key: CanonicalInput<'tcx>,
|
||||||
proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
|
proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
|
||||||
reached_depth: usize,
|
additional_depth: usize,
|
||||||
encountered_overflow: bool,
|
encountered_overflow: bool,
|
||||||
cycle_participants: FxHashSet<CanonicalInput<'tcx>>,
|
cycle_participants: FxHashSet<CanonicalInput<'tcx>>,
|
||||||
dep_node: DepNodeIndex,
|
dep_node: DepNodeIndex,
|
||||||
|
@ -40,17 +40,17 @@ impl<'tcx> EvaluationCache<'tcx> {
|
||||||
let data = WithDepNode::new(dep_node, QueryData { result, proof_tree });
|
let data = WithDepNode::new(dep_node, QueryData { result, proof_tree });
|
||||||
entry.cycle_participants.extend(cycle_participants);
|
entry.cycle_participants.extend(cycle_participants);
|
||||||
if encountered_overflow {
|
if encountered_overflow {
|
||||||
entry.with_overflow.insert(reached_depth, data);
|
entry.with_overflow.insert(additional_depth, data);
|
||||||
} else {
|
} else {
|
||||||
entry.success = Some(Success { data, reached_depth });
|
entry.success = Some(Success { data, additional_depth });
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg!(debug_assertions) {
|
if cfg!(debug_assertions) {
|
||||||
drop(map);
|
drop(map);
|
||||||
if Some(CacheData { result, proof_tree, reached_depth, encountered_overflow })
|
let expected = CacheData { result, proof_tree, additional_depth, encountered_overflow };
|
||||||
!= self.get(tcx, key, |_| false, Limit(reached_depth))
|
let actual = self.get(tcx, key, [], Limit(additional_depth));
|
||||||
{
|
if !actual.as_ref().is_some_and(|actual| expected == *actual) {
|
||||||
bug!("unable to retrieve inserted element from cache: {key:?}");
|
bug!("failed to lookup inserted element for {key:?}: {expected:?} != {actual:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,23 +63,25 @@ impl<'tcx> EvaluationCache<'tcx> {
|
||||||
&self,
|
&self,
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
key: CanonicalInput<'tcx>,
|
key: CanonicalInput<'tcx>,
|
||||||
cycle_participant_in_stack: impl FnOnce(&FxHashSet<CanonicalInput<'tcx>>) -> bool,
|
stack_entries: impl IntoIterator<Item = CanonicalInput<'tcx>>,
|
||||||
available_depth: Limit,
|
available_depth: Limit,
|
||||||
) -> Option<CacheData<'tcx>> {
|
) -> Option<CacheData<'tcx>> {
|
||||||
let map = self.map.borrow();
|
let map = self.map.borrow();
|
||||||
let entry = map.get(&key)?;
|
let entry = map.get(&key)?;
|
||||||
|
|
||||||
if cycle_participant_in_stack(&entry.cycle_participants) {
|
for stack_entry in stack_entries {
|
||||||
return None;
|
if entry.cycle_participants.contains(&stack_entry) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref success) = entry.success {
|
if let Some(ref success) = entry.success {
|
||||||
if available_depth.value_within_limit(success.reached_depth) {
|
if available_depth.value_within_limit(success.additional_depth) {
|
||||||
let QueryData { result, proof_tree } = success.data.get(tcx);
|
let QueryData { result, proof_tree } = success.data.get(tcx);
|
||||||
return Some(CacheData {
|
return Some(CacheData {
|
||||||
result,
|
result,
|
||||||
proof_tree,
|
proof_tree,
|
||||||
reached_depth: success.reached_depth,
|
additional_depth: success.additional_depth,
|
||||||
encountered_overflow: false,
|
encountered_overflow: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -90,7 +92,7 @@ impl<'tcx> EvaluationCache<'tcx> {
|
||||||
CacheData {
|
CacheData {
|
||||||
result,
|
result,
|
||||||
proof_tree,
|
proof_tree,
|
||||||
reached_depth: available_depth.0,
|
additional_depth: available_depth.0,
|
||||||
encountered_overflow: true,
|
encountered_overflow: true,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -99,7 +101,7 @@ impl<'tcx> EvaluationCache<'tcx> {
|
||||||
|
|
||||||
struct Success<'tcx> {
|
struct Success<'tcx> {
|
||||||
data: WithDepNode<QueryData<'tcx>>,
|
data: WithDepNode<QueryData<'tcx>>,
|
||||||
reached_depth: usize,
|
additional_depth: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
|
|
|
@ -134,16 +134,6 @@ impl<I: Interner> SearchGraph<I> {
|
||||||
self.mode
|
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
|
/// Pops the highest goal from the stack, lazily updating the
|
||||||
/// the next goal in the stack.
|
/// 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));
|
return Self::response_no_constraints(tcx, input, Certainty::overflow(true));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Try to fetch the goal from the global cache.
|
if let Some(result) = self.lookup_global_cache(tcx, input, available_depth, inspect) {
|
||||||
'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");
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,7 +348,10 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
|
||||||
|
|
||||||
// This is for global caching, so we properly track query dependencies.
|
// This is for global caching, so we properly track query dependencies.
|
||||||
// Everything that affects the `result` should be performed within this
|
// 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) =
|
let ((final_entry, result), dep_node) =
|
||||||
tcx.dep_graph.with_anon_task(tcx, dep_kinds::TraitSelect, || {
|
tcx.dep_graph.with_anon_task(tcx, dep_kinds::TraitSelect, || {
|
||||||
for _ in 0..FIXPOINT_STEP_LIMIT {
|
for _ in 0..FIXPOINT_STEP_LIMIT {
|
||||||
|
@ -446,6 +409,45 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
|
||||||
|
|
||||||
result
|
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> {
|
enum StepResult<'tcx> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue