Skip to content

Commit 303af36

Browse files
committed
new solver: add a separate cache for coherence
1 parent 78f97c9 commit 303af36

File tree

2 files changed

+20
-17
lines changed
  • compiler

2 files changed

+20
-17
lines changed

compiler/rustc_middle/src/ty/context.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ pub struct GlobalCtxt<'tcx> {
569569

570570
/// Caches the results of goal evaluation in the new solver.
571571
pub new_solver_evaluation_cache: solve::EvaluationCache<'tcx>,
572+
pub new_solver_coherence_evaluation_cache: solve::EvaluationCache<'tcx>,
572573

573574
/// Data layout specification for the current target.
574575
pub data_layout: TargetDataLayout,
@@ -680,10 +681,12 @@ impl<'tcx> TyCtxt<'tcx> {
680681
value.lift_to_tcx(self)
681682
}
682683

683-
/// Creates a type context and call the closure with a `TyCtxt` reference
684-
/// to the context. The closure enforces that the type context and any interned
685-
/// value (types, args, etc.) can only be used while `ty::tls` has a valid
686-
/// reference to the context, to allow formatting values that need it.
684+
/// Creates a type context. To use the context call `fn enter` which
685+
/// provides a `TyCtxt`.
686+
///
687+
/// By only providing the `TyCtxt` inside of the closure we enforce that the type
688+
/// context and any interned alue (types, args, etc.) can only be used while `ty::tls`
689+
/// has a valid reference to the context, to allow formatting values that need it.
687690
pub fn create_global_ctxt(
688691
s: &'tcx Session,
689692
lint_store: Lrc<dyn Any + sync::DynSend + sync::DynSync>,
@@ -721,6 +724,7 @@ impl<'tcx> TyCtxt<'tcx> {
721724
selection_cache: Default::default(),
722725
evaluation_cache: Default::default(),
723726
new_solver_evaluation_cache: Default::default(),
727+
new_solver_coherence_evaluation_cache: Default::default(),
724728
data_layout,
725729
alloc_map: Lock::new(interpret::AllocMap::new()),
726730
}

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

+12-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use cache::ProvisionalCache;
99
use overflow::OverflowData;
1010
use rustc_index::IndexVec;
1111
use rustc_middle::dep_graph::DepKind;
12-
use rustc_middle::traits::solve::{CanonicalInput, Certainty, MaybeCause, QueryResult};
12+
use rustc_middle::traits::solve::{
13+
CanonicalInput, Certainty, EvaluationCache, MaybeCause, QueryResult,
14+
};
1315
use rustc_middle::ty::TyCtxt;
1416
use std::{collections::hash_map::Entry, mem};
1517

@@ -58,10 +60,10 @@ impl<'tcx> SearchGraph<'tcx> {
5860
///
5961
/// We could add another global cache for coherence instead,
6062
/// but that's effort so let's only do it if necessary.
61-
pub(super) fn should_use_global_cache(&self) -> bool {
63+
pub(super) fn global_cache(&self, tcx: TyCtxt<'tcx>) -> &'tcx EvaluationCache<'tcx> {
6264
match self.mode {
63-
SolverMode::Normal => true,
64-
SolverMode::Coherence => false,
65+
SolverMode::Normal => &tcx.new_solver_evaluation_cache,
66+
SolverMode::Coherence => &tcx.new_solver_coherence_evaluation_cache,
6567
}
6668
}
6769

@@ -213,8 +215,8 @@ impl<'tcx> SearchGraph<'tcx> {
213215
inspect: &mut ProofTreeBuilder<'tcx>,
214216
mut loop_body: impl FnMut(&mut Self, &mut ProofTreeBuilder<'tcx>) -> QueryResult<'tcx>,
215217
) -> QueryResult<'tcx> {
216-
if self.should_use_global_cache() && inspect.use_global_cache() {
217-
if let Some(result) = tcx.new_solver_evaluation_cache.get(&canonical_input, tcx) {
218+
if inspect.use_global_cache() {
219+
if let Some(result) = self.global_cache(tcx).get(&canonical_input, tcx) {
218220
debug!(?canonical_input, ?result, "cache hit");
219221
inspect.cache_hit(CacheHit::Global);
220222
return result;
@@ -278,13 +280,10 @@ impl<'tcx> SearchGraph<'tcx> {
278280
// dependencies, our non-root goal may no longer appear as child of the root goal.
279281
//
280282
// See https://github.com/rust-lang/rust/pull/108071 for some additional context.
281-
let can_cache = !self.overflow_data.did_overflow() || self.stack.is_empty();
282-
if self.should_use_global_cache() && can_cache {
283-
tcx.new_solver_evaluation_cache.insert(
284-
current_goal.input,
285-
dep_node,
286-
current_goal.response,
287-
);
283+
let can_cache = inspect.use_global_cache()
284+
&& (!self.overflow_data.did_overflow() || self.stack.is_empty());
285+
if can_cache {
286+
self.global_cache(tcx).insert(current_goal.input, dep_node, current_goal.response)
288287
}
289288
}
290289

0 commit comments

Comments
 (0)