Skip to content

Commit 3587d4c

Browse files
committed
say what kind of cache hit
1 parent a2050ba commit 3587d4c

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ use super::{CanonicalInput, Certainty, Goal, NoSolution, QueryInput, QueryResult
22
use crate::ty;
33
use std::fmt::{Debug, Write};
44

5+
#[derive(Eq, PartialEq, Hash, HashStable)]
6+
pub enum CacheHit {
7+
Provisional,
8+
Global,
9+
}
10+
511
#[derive(Eq, PartialEq, Hash, HashStable)]
612
pub struct GoalEvaluation<'tcx> {
713
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
@@ -12,7 +18,7 @@ pub struct GoalEvaluation<'tcx> {
1218
/// is represented as an entry in this vec.
1319
pub evaluation_steps: Vec<GoalEvaluationStep<'tcx>>,
1420

15-
pub cache_hit: bool,
21+
pub cache_hit: Option<CacheHit>,
1622

1723
pub result: Option<QueryResult<'tcx>>,
1824
}
@@ -92,8 +98,9 @@ impl ProofTreeFormatter<'_, '_> {
9298
writeln!(f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?;
9399

94100
match goal.cache_hit {
95-
true => writeln!(f, "CACHE HIT: {:?}", goal.result),
96-
false => {
101+
Some(CacheHit::Global) => writeln!(f, "GLOBAL CACHE HIT: {:?}", goal.result),
102+
Some(CacheHit::Provisional) => writeln!(f, "PROVISIONAL CACHE HIT: {:?}", goal.result),
103+
None => {
97104
for (n, step) in goal.evaluation_steps.iter().enumerate() {
98105
let f = &mut *self.f;
99106
writeln!(f, "REVISION {n}: {:?}", step.result.unwrap())?;

compiler/rustc_trait_selection/src/solve/inspect/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub trait InspectSolve<'tcx> {
2323
goal: Goal<'tcx, ty::Predicate<'tcx>>,
2424
) -> Box<dyn InspectSolve<'tcx> + 'tcx>;
2525
fn canonicalized_goal(&mut self, canonical_goal: CanonicalInput<'tcx>);
26-
fn cache_hit(&mut self);
26+
fn cache_hit(&mut self, cache_hit: CacheHit);
2727
fn goal_evaluation(&mut self, goal_evaluation: Box<dyn InspectSolve<'tcx> + 'tcx>);
2828

2929
fn new_goal_evaluation_step(
@@ -59,7 +59,7 @@ impl<'tcx> InspectSolve<'tcx> for () {
5959
Box::new(())
6060
}
6161
fn canonicalized_goal(&mut self, _canonical_goal: CanonicalInput<'tcx>) {}
62-
fn cache_hit(&mut self) {}
62+
fn cache_hit(&mut self, _cache_hit: CacheHit) {}
6363
fn goal_evaluation(&mut self, _goal_evaluation: Box<dyn InspectSolve<'tcx> + 'tcx>) {}
6464

6565
fn new_goal_evaluation_step(
@@ -104,7 +104,7 @@ impl<'tcx> InspectSolve<'tcx> for DebugSolver<'tcx> {
104104
uncanonicalized_goal: goal,
105105
canonicalized_goal: None,
106106
evaluation_steps: vec![],
107-
cache_hit: false,
107+
cache_hit: None,
108108
result: None,
109109
}))
110110
}
@@ -117,9 +117,11 @@ impl<'tcx> InspectSolve<'tcx> for DebugSolver<'tcx> {
117117
_ => unreachable!(),
118118
}
119119
}
120-
fn cache_hit(&mut self) {
120+
fn cache_hit(&mut self, cache_hit: CacheHit) {
121121
match self {
122-
DebugSolver::GoalEvaluation(goal_evaluation) => goal_evaluation.cache_hit = true,
122+
DebugSolver::GoalEvaluation(goal_evaluation) => {
123+
goal_evaluation.cache_hit = Some(cache_hit)
124+
}
123125
_ => unreachable!(),
124126
};
125127
}

compiler/rustc_trait_selection/src/solve/search_graph/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod cache;
22
mod overflow;
33

44
pub(super) use overflow::OverflowHandler;
5+
use rustc_middle::traits::solve::inspect::CacheHit;
56

67
use self::cache::ProvisionalEntry;
78
use cache::ProvisionalCache;
@@ -89,11 +90,12 @@ impl<'tcx> SearchGraph<'tcx> {
8990
/// Tries putting the new goal on the stack, returning an error if it is already cached.
9091
///
9192
/// This correctly updates the provisional cache if there is a cycle.
92-
#[instrument(level = "debug", skip(self, tcx), ret)]
93+
#[instrument(level = "debug", skip(self, tcx, inspect), ret)]
9394
fn try_push_stack(
9495
&mut self,
9596
tcx: TyCtxt<'tcx>,
9697
input: CanonicalInput<'tcx>,
98+
inspect: &mut dyn InspectSolve<'tcx>,
9799
) -> Result<(), QueryResult<'tcx>> {
98100
// Look at the provisional cache to check for cycles.
99101
let cache = &mut self.provisional_cache;
@@ -120,6 +122,8 @@ impl<'tcx> SearchGraph<'tcx> {
120122
// Finally we can return either the provisional response for that goal if we have a
121123
// coinductive cycle or an ambiguous result if the cycle is inductive.
122124
Entry::Occupied(entry_index) => {
125+
inspect.cache_hit(CacheHit::Provisional);
126+
123127
let entry_index = *entry_index.get();
124128

125129
let stack_depth = cache.depth(entry_index);
@@ -212,12 +216,12 @@ impl<'tcx> SearchGraph<'tcx> {
212216
if self.should_use_global_cache() {
213217
if let Some(result) = tcx.new_solver_evaluation_cache.get(&canonical_input, tcx) {
214218
debug!(?canonical_input, ?result, "cache hit");
215-
inspect.cache_hit();
219+
inspect.cache_hit(CacheHit::Global);
216220
return result;
217221
}
218222
}
219223

220-
match self.try_push_stack(tcx, canonical_input) {
224+
match self.try_push_stack(tcx, canonical_input, inspect) {
221225
Ok(()) => {}
222226
// Our goal is already on the stack, eager return.
223227
Err(response) => return response,

0 commit comments

Comments
 (0)