Skip to content

Commit 230b39e

Browse files
Auto merge of #142643 - compiler-errors:proof-tree-cache, r=<try>
[perf] Cache evaluations even when proof tree is being built 🤷
2 parents f3db639 + 93ff544 commit 230b39e

File tree

6 files changed

+65
-66
lines changed

6 files changed

+65
-66
lines changed

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,9 @@ pub trait SolverDelegateEvalExt: SolverDelegate {
147147
fn evaluate_root_goal(
148148
&self,
149149
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
150-
generate_proof_tree: GenerateProofTree,
151150
span: <Self::Interner as Interner>::Span,
152151
stalled_on: Option<GoalStalledOn<Self::Interner>>,
153-
) -> (
154-
Result<GoalEvaluation<Self::Interner>, NoSolution>,
155-
Option<inspect::GoalEvaluation<Self::Interner>>,
156-
);
152+
) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
157153

158154
/// Check whether evaluating `goal` with a depth of `root_depth` may
159155
/// succeed. This only returns `false` if the goal is guaranteed to
@@ -173,14 +169,22 @@ pub trait SolverDelegateEvalExt: SolverDelegate {
173169
fn evaluate_root_goal_raw(
174170
&self,
175171
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
176-
generate_proof_tree: GenerateProofTree,
177172
stalled_on: Option<GoalStalledOn<Self::Interner>>,
173+
) -> Result<
174+
(NestedNormalizationGoals<Self::Interner>, GoalEvaluation<Self::Interner>),
175+
NoSolution,
176+
>;
177+
178+
fn evaluate_root_goal_proof_tree(
179+
&self,
180+
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
181+
origin_span: <Self::Interner as Interner>::Span,
178182
) -> (
179183
Result<
180184
(NestedNormalizationGoals<Self::Interner>, GoalEvaluation<Self::Interner>),
181185
NoSolution,
182186
>,
183-
Option<inspect::GoalEvaluation<Self::Interner>>,
187+
inspect::GoalEvaluation<Self::Interner>,
184188
);
185189
}
186190

@@ -193,13 +197,17 @@ where
193197
fn evaluate_root_goal(
194198
&self,
195199
goal: Goal<I, I::Predicate>,
196-
generate_proof_tree: GenerateProofTree,
197200
span: I::Span,
198201
stalled_on: Option<GoalStalledOn<I>>,
199-
) -> (Result<GoalEvaluation<I>, NoSolution>, Option<inspect::GoalEvaluation<I>>) {
200-
EvalCtxt::enter_root(self, self.cx().recursion_limit(), generate_proof_tree, span, |ecx| {
201-
ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal, stalled_on)
202-
})
202+
) -> Result<GoalEvaluation<I>, NoSolution> {
203+
EvalCtxt::enter_root(
204+
self,
205+
self.cx().recursion_limit(),
206+
GenerateProofTree::No,
207+
span,
208+
|ecx| ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal, stalled_on),
209+
)
210+
.0
203211
}
204212

205213
fn root_goal_may_hold_with_depth(
@@ -220,21 +228,37 @@ where
220228
fn evaluate_root_goal_raw(
221229
&self,
222230
goal: Goal<I, I::Predicate>,
223-
generate_proof_tree: GenerateProofTree,
224231
stalled_on: Option<GoalStalledOn<I>>,
225-
) -> (
226-
Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution>,
227-
Option<inspect::GoalEvaluation<I>>,
228-
) {
232+
) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
229233
EvalCtxt::enter_root(
230234
self,
231235
self.cx().recursion_limit(),
232-
generate_proof_tree,
236+
GenerateProofTree::No,
233237
I::Span::dummy(),
234238
|ecx| {
235239
ecx.evaluate_goal_raw(GoalEvaluationKind::Root, GoalSource::Misc, goal, stalled_on)
236240
},
237241
)
242+
.0
243+
}
244+
245+
#[instrument(level = "debug", skip(self))]
246+
fn evaluate_root_goal_proof_tree(
247+
&self,
248+
goal: Goal<I, I::Predicate>,
249+
origin_span: I::Span,
250+
) -> (
251+
Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution>,
252+
inspect::GoalEvaluation<I>,
253+
) {
254+
let (result, proof_tree) = EvalCtxt::enter_root(
255+
self,
256+
self.cx().recursion_limit(),
257+
GenerateProofTree::Yes,
258+
origin_span,
259+
|ecx| ecx.evaluate_goal_raw(GoalEvaluationKind::Root, GoalSource::Misc, goal, None),
260+
);
261+
(result, proof_tree.unwrap())
238262
}
239263
}
240264

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{
1414
};
1515
use rustc_next_trait_solver::delegate::SolverDelegate as _;
1616
use rustc_next_trait_solver::solve::{
17-
GenerateProofTree, GoalEvaluation, GoalStalledOn, HasChanged, SolverDelegateEvalExt as _,
17+
GoalEvaluation, GoalStalledOn, HasChanged, SolverDelegateEvalExt as _,
1818
};
1919
use rustc_span::Span;
2020
use thin_vec::ThinVec;
@@ -106,14 +106,11 @@ impl<'tcx> ObligationStorage<'tcx> {
106106
self.overflowed.extend(
107107
ExtractIf::new(&mut self.pending, |(o, stalled_on)| {
108108
let goal = o.as_goal();
109-
let result = <&SolverDelegate<'tcx>>::from(infcx)
110-
.evaluate_root_goal(
111-
goal,
112-
GenerateProofTree::No,
113-
o.cause.span,
114-
stalled_on.take(),
115-
)
116-
.0;
109+
let result = <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
110+
goal,
111+
o.cause.span,
112+
stalled_on.take(),
113+
);
117114
matches!(result, Ok(GoalEvaluation { has_changed: HasChanged::Yes, .. }))
118115
})
119116
.map(|(o, _)| o),
@@ -207,14 +204,7 @@ where
207204
continue;
208205
}
209206

210-
let result = delegate
211-
.evaluate_root_goal(
212-
goal,
213-
GenerateProofTree::No,
214-
obligation.cause.span,
215-
stalled_on,
216-
)
217-
.0;
207+
let result = delegate.evaluate_root_goal(goal, obligation.cause.span, stalled_on);
218208
self.inspect_evaluated_obligation(infcx, &obligation, &result);
219209
let GoalEvaluation { certainty, has_changed, stalled_on } = match result {
220210
Ok(result) => result,

compiler/rustc_trait_selection/src/solve/fulfill/derive_errors.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use rustc_middle::traits::query::NoSolution;
1111
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1212
use rustc_middle::ty::{self, Ty, TyCtxt};
1313
use rustc_middle::{bug, span_bug};
14-
use rustc_next_trait_solver::solve::{
15-
GenerateProofTree, GoalEvaluation, SolverDelegateEvalExt as _,
16-
};
14+
use rustc_next_trait_solver::solve::{GoalEvaluation, SolverDelegateEvalExt as _};
1715
use tracing::{instrument, trace};
1816

1917
use crate::solve::delegate::SolverDelegate;
@@ -90,15 +88,11 @@ pub(super) fn fulfillment_error_for_stalled<'tcx>(
9088
root_obligation: PredicateObligation<'tcx>,
9189
) -> FulfillmentError<'tcx> {
9290
let (code, refine_obligation) = infcx.probe(|_| {
93-
match <&SolverDelegate<'tcx>>::from(infcx)
94-
.evaluate_root_goal(
95-
root_obligation.as_goal(),
96-
GenerateProofTree::No,
97-
root_obligation.cause.span,
98-
None,
99-
)
100-
.0
101-
{
91+
match <&SolverDelegate<'tcx>>::from(infcx).evaluate_root_goal(
92+
root_obligation.as_goal(),
93+
root_obligation.cause.span,
94+
None,
95+
) {
10296
Ok(GoalEvaluation { certainty: Certainty::Maybe(MaybeCause::Ambiguity), .. }) => {
10397
(FulfillmentErrorCode::Ambiguity { overflow: None }, true)
10498
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_middle::ty::{TyCtxt, VisitorResult, try_visit};
2020
use rustc_middle::{bug, ty};
2121
use rustc_next_trait_solver::resolve::eager_resolve_vars;
2222
use rustc_next_trait_solver::solve::inspect::{self, instantiate_canonical_state};
23-
use rustc_next_trait_solver::solve::{GenerateProofTree, MaybeCause, SolverDelegateEvalExt as _};
23+
use rustc_next_trait_solver::solve::{MaybeCause, SolverDelegateEvalExt as _};
2424
use rustc_span::Span;
2525
use tracing::instrument;
2626

@@ -248,9 +248,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
248248
// considering the constrained RHS, and pass the resulting certainty to
249249
// `InspectGoal::new` so that the goal has the right result (and maintains
250250
// the impression that we don't do this normalizes-to infer hack at all).
251-
let (nested, proof_tree) =
252-
infcx.evaluate_root_goal_raw(goal, GenerateProofTree::Yes, None);
253-
let proof_tree = proof_tree.unwrap();
251+
let (nested, proof_tree) = infcx.evaluate_root_goal_proof_tree(goal, span);
254252
let nested_goals_result = nested.and_then(|(nested, _)| {
255253
normalizes_to_term_hack.constrain_and(
256254
infcx,
@@ -284,9 +282,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
284282
// into another candidate who ends up with different inference
285283
// constraints, we get an ICE if we already applied the constraints
286284
// from the chosen candidate.
287-
let proof_tree = infcx
288-
.probe(|_| infcx.evaluate_root_goal(goal, GenerateProofTree::Yes, span, None).1)
289-
.unwrap();
285+
let proof_tree = infcx.probe(|_| infcx.evaluate_root_goal_proof_tree(goal, span).1);
290286
InspectGoal::new(infcx, self.goal.depth + 1, proof_tree, None, source)
291287
}
292288
}
@@ -488,13 +484,8 @@ impl<'tcx> InferCtxt<'tcx> {
488484
depth: usize,
489485
visitor: &mut V,
490486
) -> V::Result {
491-
let (_, proof_tree) = <&SolverDelegate<'tcx>>::from(self).evaluate_root_goal(
492-
goal,
493-
GenerateProofTree::Yes,
494-
visitor.span(),
495-
None,
496-
);
497-
let proof_tree = proof_tree.unwrap();
487+
let (_, proof_tree) =
488+
<&SolverDelegate<'tcx>>::from(self).evaluate_root_goal_proof_tree(goal, visitor.span());
498489
visitor.visit_goal(&InspectGoal::new(self, depth, proof_tree, None, GoalSource::Misc))
499490
}
500491
}

compiler/rustc_type_ir/src/search_graph/global_cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ impl<X: Cx> GlobalCache<X> {
5757
let with_overflow = WithOverflow { nested_goals, result };
5858
let prev = entry.with_overflow.insert(required_depth, with_overflow);
5959
if let Some(prev) = &prev {
60-
assert!(cx.evaluation_is_concurrent());
61-
assert_eq!(cx.get_tracked(&prev.result), origin_result);
60+
let prev_result = cx.get_tracked(&prev.result);
61+
assert_eq!(prev_result, origin_result);
6262
}
6363
} else {
6464
let prev = entry.success.replace(Success { required_depth, nested_goals, result });
6565
if let Some(prev) = &prev {
66-
assert!(cx.evaluation_is_concurrent());
67-
assert_eq!(cx.get_tracked(&prev.result), origin_result);
66+
let prev_result = cx.get_tracked(&prev.result);
67+
assert_eq!(prev_result, origin_result);
6868
}
6969
}
7070
}

compiler/rustc_type_ir/src/search_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
652652
// Do not try to move a goal into the cache again if we're testing
653653
// the global cache.
654654
assert_eq!(result, expected, "input={input:?}");
655-
} else if D::inspect_is_noop(inspect) {
655+
} else {
656656
self.insert_global_cache(cx, final_entry, result, dep_node)
657657
}
658658
} else if D::ENABLE_PROVISIONAL_CACHE {

0 commit comments

Comments
 (0)