Skip to content

Commit

Permalink
Auto merge of rust-lang#130821 - lcnr:nalgebra-hang-2, r=<try>
Browse files Browse the repository at this point in the history
caching? CACHING!

Fixes the new minimization of the hang in nalgebra and nalgebra itself :3

this is a bit iffy, especially the cache in `TypeRelating`. I believe all the caches are correct, but would like to provide an easily verifiable abstraction for caching visitors if possible. Don't think I can extend that one to `TypeRelating` however.

The first commit removes region uniquification, reintroducing the ICE from rust-lang/trait-system-refactor-initiative#27. This does not affect coherence and I would like to fix this by introducing OR-region constraints

- [x] add test of the new nalgebra minimization, it's different from this
- [ ] add an abstraction for caching type visitors

r? `@compiler-errors`
  • Loading branch information
bors committed Sep 27, 2024
2 parents a3f76a2 + e87dee8 commit 417bd4f
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 132 deletions.
15 changes: 14 additions & 1 deletion compiler/rustc_infer/src/infer/relate/type_relating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_middle::ty::relate::{
};
use rustc_middle::ty::{self, Ty, TyCtxt, TyVar};
use rustc_span::Span;
use rustc_type_ir::data_structures::DelayedSet;
use tracing::{debug, instrument};

use super::combine::CombineFields;
Expand All @@ -16,6 +17,7 @@ pub struct TypeRelating<'combine, 'a, 'tcx> {
fields: &'combine mut CombineFields<'a, 'tcx>,
structurally_relate_aliases: StructurallyRelateAliases,
ambient_variance: ty::Variance,
cache: DelayedSet<(ty::Variance, Ty<'tcx>, Ty<'tcx>)>,
}

impl<'combine, 'infcx, 'tcx> TypeRelating<'combine, 'infcx, 'tcx> {
Expand All @@ -24,7 +26,12 @@ impl<'combine, 'infcx, 'tcx> TypeRelating<'combine, 'infcx, 'tcx> {
structurally_relate_aliases: StructurallyRelateAliases,
ambient_variance: ty::Variance,
) -> TypeRelating<'combine, 'infcx, 'tcx> {
TypeRelating { fields: f, structurally_relate_aliases, ambient_variance }
TypeRelating {
fields: f,
structurally_relate_aliases,
ambient_variance,
cache: Default::default(),
}
}
}

Expand Down Expand Up @@ -78,6 +85,10 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
let a = infcx.shallow_resolve(a);
let b = infcx.shallow_resolve(b);

if self.cache.contains(&(self.ambient_variance, a, b)) {
return Ok(a);
}

match (a.kind(), b.kind()) {
(&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
match self.ambient_variance {
Expand Down Expand Up @@ -160,6 +171,8 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
}
}

assert!(self.cache.insert((self.ambient_variance, a, b)));

Ok(a)
}

Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_infer/src/infer/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc_middle::bug;
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFolder, TypeSuperFoldable};
use rustc_middle::ty::visit::TypeVisitableExt;
use rustc_middle::ty::{self, Const, InferConst, Ty, TyCtxt, TypeFoldable};
use rustc_type_ir::data_structures::DelayedMap;

use super::{FixupError, FixupResult, InferCtxt};

Expand All @@ -15,12 +16,13 @@ use super::{FixupError, FixupResult, InferCtxt};
/// points for correctness.
pub struct OpportunisticVarResolver<'a, 'tcx> {
infcx: &'a InferCtxt<'tcx>,
cache: DelayedMap<Ty<'tcx>, Ty<'tcx>>,
}

impl<'a, 'tcx> OpportunisticVarResolver<'a, 'tcx> {
#[inline]
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
OpportunisticVarResolver { infcx }
OpportunisticVarResolver { infcx, cache: Default::default() }
}
}

Expand All @@ -33,9 +35,13 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for OpportunisticVarResolver<'a, 'tcx> {
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
if !t.has_non_region_infer() {
t // micro-optimize -- if there is nothing in this type that this fold affects...
} else if let Some(&ty) = self.cache.get(&t) {
return ty;
} else {
let t = self.infcx.shallow_resolve(t);
t.super_fold_with(self)
let shallow = self.infcx.shallow_resolve(t);
let res = shallow.super_fold_with(self);
assert!(self.cache.insert(t, res));
res
}
}

Expand Down
15 changes: 13 additions & 2 deletions compiler/rustc_middle/src/ty/fold.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_hir::def_id::DefId;
use rustc_type_ir::data_structures::DelayedMap;
pub use rustc_type_ir::fold::{
FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable, shift_region, shift_vars,
};
Expand Down Expand Up @@ -164,11 +165,13 @@ struct BoundVarReplacer<'tcx, D> {
current_index: ty::DebruijnIndex,

delegate: D,

cache: DelayedMap<(ty::DebruijnIndex, Ty<'tcx>), Ty<'tcx>>,
}

impl<'tcx, D: BoundVarReplacerDelegate<'tcx>> BoundVarReplacer<'tcx, D> {
fn new(tcx: TyCtxt<'tcx>, delegate: D) -> Self {
BoundVarReplacer { tcx, current_index: ty::INNERMOST, delegate }
BoundVarReplacer { tcx, current_index: ty::INNERMOST, delegate, cache: Default::default() }
}
}

Expand Down Expand Up @@ -197,7 +200,15 @@ where
debug_assert!(!ty.has_vars_bound_above(ty::INNERMOST));
ty::fold::shift_vars(self.tcx, ty, self.current_index.as_u32())
}
_ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
_ if t.has_vars_bound_at_or_above(self.current_index) => {
if let Some(&ty) = self.cache.get(&(self.current_index, t)) {
return ty;
}

let res = t.super_fold_with(self);
assert!(self.cache.insert((self.current_index, t), res));
res
}
_ => t,
}
}
Expand Down
Loading

0 comments on commit 417bd4f

Please sign in to comment.