Skip to content

Commit ee0f20b

Browse files
committed
move global cache lookup into fn
1 parent 82df0c3 commit ee0f20b

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
@@ -134,16 +134,6 @@ impl<I: Interner> SearchGraph<I> {
134134
self.mode
135135
}
136136

137-
/// Update the stack and reached depths on cache hits.
138-
#[instrument(level = "trace", skip(self))]
139-
fn on_cache_hit(&mut self, additional_depth: usize, encountered_overflow: bool) {
140-
let reached_depth = self.stack.next_index().plus(additional_depth);
141-
if let Some(last) = self.stack.raw.last_mut() {
142-
last.reached_depth = last.reached_depth.max(reached_depth);
143-
last.encountered_overflow |= encountered_overflow;
144-
}
145-
}
146-
147137
/// Pops the highest goal from the stack, lazily updating the
148138
/// the next goal in the stack.
149139
///
@@ -276,37 +266,7 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
276266
return Self::response_no_constraints(tcx, input, Certainty::overflow(true));
277267
};
278268

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

@@ -388,7 +348,10 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
388348

389349
// This is for global caching, so we properly track query dependencies.
390350
// Everything that affects the `result` should be performed within this
391-
// `with_anon_task` closure.
351+
// `with_anon_task` closure. If computing this goal depends on something
352+
// not tracked by the cache key and from outside of this anon task, it
353+
// must not be added to the global cache. Notably, this is the case for
354+
// trait solver cycles participants.
392355
let ((final_entry, result), dep_node) =
393356
tcx.dep_graph.with_anon_task(tcx, dep_kinds::TraitSelect, || {
394357
for _ in 0..FIXPOINT_STEP_LIMIT {
@@ -446,6 +409,45 @@ impl<'tcx> SearchGraph<TyCtxt<'tcx>> {
446409

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

451453
enum StepResult<'tcx> {

0 commit comments

Comments
 (0)