Skip to content

Commit c0b0295

Browse files
committed
move global cache lookup into fn
1 parent 76d9214 commit c0b0295

File tree

3 files changed

+86
-62
lines changed

3 files changed

+86
-62
lines changed

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

Lines changed: 18 additions & 16 deletions
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<'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<'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

Lines changed: 44 additions & 41 deletions
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 = "debug", 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,36 +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);
259+
if let Some(result) = self.lookup_global_cache(tcx, input, available_depth, inspect) {
299260
return result;
300261
}
301262

@@ -376,7 +337,10 @@ impl<'tcx> SearchGraph<'tcx> {
376337

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

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

439442
enum StepResult<'tcx> {

src/bootstrap/src/core/builder/tests.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,14 @@ fn check_cli<const N: usize>(paths: [&str; N]) {
6060
macro_rules! std {
6161
($host:ident => $target:ident, stage = $stage:literal) => {
6262
compile::Std::new(
63-
Compiler { host: TargetSelection::from_user(concat!(stringify!($host), "-", stringify!($host))), stage: $stage },
63+
Compiler {
64+
host: TargetSelection::from_user(concat!(
65+
stringify!($host),
66+
"-",
67+
stringify!($host)
68+
)),
69+
stage: $stage,
70+
},
6471
TargetSelection::from_user(concat!(stringify!($target), "-", stringify!($target))),
6572
)
6673
};
@@ -83,7 +90,14 @@ macro_rules! doc_std {
8390
macro_rules! rustc {
8491
($host:ident => $target:ident, stage = $stage:literal) => {
8592
compile::Rustc::new(
86-
Compiler { host: TargetSelection::from_user(concat!(stringify!($host), "-", stringify!($host))), stage: $stage },
93+
Compiler {
94+
host: TargetSelection::from_user(concat!(
95+
stringify!($host),
96+
"-",
97+
stringify!($host)
98+
)),
99+
stage: $stage,
100+
},
87101
TargetSelection::from_user(concat!(stringify!($target), "-", stringify!($target))),
88102
)
89103
};
@@ -141,10 +155,14 @@ fn check_missing_paths_for_x_test_tests() {
141155

142156
// Skip if not a test directory.
143157
if path.ends_with("tests/auxiliary") || !path.is_dir() {
144-
continue
158+
continue;
145159
}
146160

147-
assert!(tests_remap_paths.iter().any(|item| path.ends_with(*item)), "{} is missing in PATH_REMAP tests list.", path.display());
161+
assert!(
162+
tests_remap_paths.iter().any(|item| path.ends_with(*item)),
163+
"{} is missing in PATH_REMAP tests list.",
164+
path.display()
165+
);
148166
}
149167
}
150168

@@ -185,7 +203,8 @@ fn alias_and_path_for_library() {
185203
&[std!(A => A, stage = 0), std!(A => A, stage = 1)]
186204
);
187205

188-
let mut cache = run_build(&["library".into(), "core".into()], configure("doc", &["A-A"], &["A-A"]));
206+
let mut cache =
207+
run_build(&["library".into(), "core".into()], configure("doc", &["A-A"], &["A-A"]));
189208
assert_eq!(first(cache.all::<doc::Std>()), &[doc_std!(A => A, stage = 0)]);
190209
}
191210

0 commit comments

Comments
 (0)