Skip to content

Commit 0f34a03

Browse files
committed
move global cache lookup into fn
1 parent ab25c3d commit 0f34a03

File tree

2 files changed

+62
-58
lines changed

2 files changed

+62
-58
lines changed

compiler/rustc_middle/src/traits/solve/cache.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub struct EvaluationCache<'tcx> {
1414
map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>,
1515
}
1616

17-
#[derive(PartialEq, Eq)]
17+
#[derive(Debug, PartialEq, Eq)]
1818
pub struct CacheData<'tcx> {
1919
pub result: QueryResult<'tcx>,
2020
pub proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
21-
pub reached_depth: usize,
21+
pub additional_depth: usize,
2222
pub encountered_overflow: bool,
2323
}
2424

@@ -29,7 +29,7 @@ impl<'tcx> EvaluationCache<'tcx> {
2929
tcx: TyCtxt<'tcx>,
3030
key: CanonicalInput<'tcx>,
3131
proof_tree: Option<&'tcx [inspect::GoalEvaluationStep<TyCtxt<'tcx>>]>,
32-
reached_depth: usize,
32+
additional_depth: usize,
3333
encountered_overflow: bool,
3434
cycle_participants: FxHashSet<CanonicalInput<'tcx>>,
3535
dep_node: DepNodeIndex,
@@ -40,17 +40,17 @@ impl<'tcx> EvaluationCache<'tcx> {
4040
let data = WithDepNode::new(dep_node, QueryData { result, proof_tree });
4141
entry.cycle_participants.extend(cycle_participants);
4242
if encountered_overflow {
43-
entry.with_overflow.insert(reached_depth, data);
43+
entry.with_overflow.insert(additional_depth, data);
4444
} else {
45-
entry.success = Some(Success { data, reached_depth });
45+
entry.success = Some(Success { data, additional_depth });
4646
}
4747

4848
if cfg!(debug_assertions) {
4949
drop(map);
50-
if Some(CacheData { result, proof_tree, reached_depth, encountered_overflow })
51-
!= self.get(tcx, key, |_| false, Limit(reached_depth))
52-
{
53-
bug!("unable to retrieve inserted element from cache: {key:?}");
50+
let expected = CacheData { result, proof_tree, additional_depth, encountered_overflow };
51+
let actual = self.get(tcx, key, [], Limit(additional_depth));
52+
if !actual.as_ref().is_some_and(|actual| expected == *actual) {
53+
bug!("failed to lookup inserted element for {key:?}: {expected:?} != {actual:?}");
5454
}
5555
}
5656
}
@@ -63,23 +63,25 @@ impl<'tcx> EvaluationCache<'tcx> {
6363
&self,
6464
tcx: TyCtxt<'tcx>,
6565
key: CanonicalInput<'tcx>,
66-
cycle_participant_in_stack: impl FnOnce(&FxHashSet<CanonicalInput<'tcx>>) -> bool,
66+
stack_entries: impl IntoIterator<Item = CanonicalInput<'tcx>>,
6767
available_depth: Limit,
6868
) -> Option<CacheData<'tcx>> {
6969
let map = self.map.borrow();
7070
let entry = map.get(&key)?;
7171

72-
if cycle_participant_in_stack(&entry.cycle_participants) {
73-
return None;
72+
for stack_entry in stack_entries {
73+
if entry.cycle_participants.contains(&stack_entry) {
74+
return None;
75+
}
7476
}
7577

7678
if let Some(ref success) = entry.success {
77-
if available_depth.value_within_limit(success.reached_depth) {
79+
if available_depth.value_within_limit(success.additional_depth) {
7880
let QueryData { result, proof_tree } = success.data.get(tcx);
7981
return Some(CacheData {
8082
result,
8183
proof_tree,
82-
reached_depth: success.reached_depth,
84+
additional_depth: success.additional_depth,
8385
encountered_overflow: false,
8486
});
8587
}
@@ -90,7 +92,7 @@ impl<'tcx> EvaluationCache<'tcx> {
9092
CacheData {
9193
result,
9294
proof_tree,
93-
reached_depth: available_depth.0,
95+
additional_depth: available_depth.0,
9496
encountered_overflow: true,
9597
}
9698
})
@@ -99,7 +101,7 @@ impl<'tcx> EvaluationCache<'tcx> {
99101

100102
struct Success<'tcx> {
101103
data: WithDepNode<QueryData<'tcx>>,
102-
reached_depth: usize,
104+
additional_depth: usize,
103105
}
104106

105107
#[derive(Clone, Copy)]

compiler/rustc_trait_selection/src/solve/search_graph.rs

+44-42
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,6 @@ impl<'tcx> SearchGraph<'tcx> {
129129
self.mode
130130
}
131131

132-
/// Update the stack and reached depths on cache hits.
133-
#[instrument(level = "trace", skip(self))]
134-
fn on_cache_hit(&mut self, additional_depth: usize, encountered_overflow: bool) {
135-
let reached_depth = self.stack.next_index().plus(additional_depth);
136-
if let Some(last) = self.stack.raw.last_mut() {
137-
last.reached_depth = last.reached_depth.max(reached_depth);
138-
last.encountered_overflow |= encountered_overflow;
139-
}
140-
}
141-
142132
/// Pops the highest goal from the stack, lazily updating the
143133
/// the next goal in the stack.
144134
///
@@ -266,37 +256,7 @@ impl<'tcx> SearchGraph<'tcx> {
266256
return Self::response_no_constraints(tcx, input, Certainty::overflow(true));
267257
};
268258

269-
// Try to fetch the goal from the global cache.
270-
'global: {
271-
let Some(CacheData { result, proof_tree, reached_depth, encountered_overflow }) =
272-
self.global_cache(tcx).get(
273-
tcx,
274-
input,
275-
|cycle_participants| {
276-
self.stack.iter().any(|entry| cycle_participants.contains(&entry.input))
277-
},
278-
available_depth,
279-
)
280-
else {
281-
break 'global;
282-
};
283-
284-
// If we're building a proof tree and the current cache entry does not
285-
// contain a proof tree, we do not use the entry but instead recompute
286-
// the goal. We simply overwrite the existing entry once we're done,
287-
// caching the proof tree.
288-
if !inspect.is_noop() {
289-
if let Some(revisions) = proof_tree {
290-
inspect.goal_evaluation_kind(
291-
inspect::WipCanonicalGoalEvaluationKind::Interned { revisions },
292-
);
293-
} else {
294-
break 'global;
295-
}
296-
}
297-
298-
self.on_cache_hit(reached_depth, encountered_overflow);
299-
debug!("global cache hit");
259+
if let Some(result) = self.lookup_global_cache(tcx, input, available_depth, inspect) {
300260
return result;
301261
}
302262

@@ -378,7 +338,10 @@ impl<'tcx> SearchGraph<'tcx> {
378338

379339
// This is for global caching, so we properly track query dependencies.
380340
// Everything that affects the `result` should be performed within this
381-
// `with_anon_task` closure.
341+
// `with_anon_task` closure. If computing this goal depends on something
342+
// not tracked by the cache key and from outside of this anon task, it
343+
// must not be added to the global cache. Notably, this is the case for
344+
// trait solver cycles participants.
382345
let ((final_entry, result), dep_node) =
383346
tcx.dep_graph.with_anon_task(tcx, dep_kinds::TraitSelect, || {
384347
for _ in 0..FIXPOINT_STEP_LIMIT {
@@ -436,6 +399,45 @@ impl<'tcx> SearchGraph<'tcx> {
436399

437400
result
438401
}
402+
403+
/// Try to fetch a previously computed result from the global cache,
404+
/// making sure to only do so if it would match the result of reevaluating
405+
/// this goal.
406+
fn lookup_global_cache(
407+
&mut self,
408+
tcx: TyCtxt<'tcx>,
409+
input: CanonicalInput<'tcx>,
410+
available_depth: Limit,
411+
inspect: &mut ProofTreeBuilder<TyCtxt<'tcx>>,
412+
) -> Option<QueryResult<'tcx>> {
413+
let CacheData { result, proof_tree, additional_depth, encountered_overflow } = self
414+
.global_cache(tcx)
415+
.get(tcx, input, self.stack.iter().map(|e| e.input), available_depth)?;
416+
417+
// If we're building a proof tree and the current cache entry does not
418+
// contain a proof tree, we do not use the entry but instead recompute
419+
// the goal. We simply overwrite the existing entry once we're done,
420+
// caching the proof tree.
421+
if !inspect.is_noop() {
422+
if let Some(revisions) = proof_tree {
423+
let kind = inspect::WipCanonicalGoalEvaluationKind::Interned { revisions };
424+
inspect.goal_evaluation_kind(kind);
425+
} else {
426+
return None;
427+
}
428+
}
429+
430+
// Update the reached depth of the current goal to make sure
431+
// its state is the same regardless of whether we've used the
432+
// global cache or not.
433+
let reached_depth = self.stack.next_index().plus(additional_depth);
434+
if let Some(last) = self.stack.raw.last_mut() {
435+
last.reached_depth = last.reached_depth.max(reached_depth);
436+
last.encountered_overflow |= encountered_overflow;
437+
}
438+
439+
Some(result)
440+
}
439441
}
440442

441443
enum StepResult<'tcx> {

0 commit comments

Comments
 (0)