Skip to content

Commit 9327272

Browse files
committed
Try out caching the stable hash of Ty within itself
1 parent 7ccfe2f commit 9327272

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::ty::{
2424
RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy,
2525
};
2626
use rustc_ast as ast;
27+
use rustc_data_structures::fingerprint::Fingerprint;
2728
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2829
use rustc_data_structures::intern::Interned;
2930
use rustc_data_structures::memmap::Mmap;
@@ -58,6 +59,7 @@ use rustc_span::{Span, DUMMY_SP};
5859
use rustc_target::abi::{Layout, TargetDataLayout, VariantIdx};
5960
use rustc_target::spec::abi;
6061

62+
use rustc_type_ir::TypeFlags;
6163
use smallvec::SmallVec;
6264
use std::any::Any;
6365
use std::borrow::Borrow;
@@ -140,16 +142,40 @@ impl<'tcx> CtxtInterners<'tcx> {
140142
/// Interns a type.
141143
#[allow(rustc::usage_of_ty_tykind)]
142144
#[inline(never)]
143-
fn intern_ty(&self, kind: TyKind<'tcx>) -> Ty<'tcx> {
145+
fn intern_ty(
146+
&self,
147+
kind: TyKind<'tcx>,
148+
sess: &Session,
149+
resolutions: &ty::ResolverOutputs,
150+
) -> Ty<'tcx> {
144151
Ty(Interned::new_unchecked(
145152
self.type_
146153
.intern(kind, |kind| {
147154
let flags = super::flags::FlagComputation::for_kind(&kind);
148155

156+
let stable_hash = if flags.flags.intersects(TypeFlags::HAS_RE_INFER) {
157+
Fingerprint::ZERO
158+
} else {
159+
let mut hasher = StableHasher::new();
160+
let mut hcx = StableHashingContext::new(
161+
sess,
162+
&resolutions.definitions,
163+
&*resolutions.cstore,
164+
);
165+
166+
hcx.while_hashing_spans(false, |hcx| {
167+
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
168+
kind.hash_stable(hcx, &mut hasher);
169+
});
170+
});
171+
hasher.finish()
172+
};
173+
149174
let ty_struct = TyS {
150175
kind,
151176
flags: flags.flags,
152177
outer_exclusive_binder: flags.outer_exclusive_binder,
178+
stable_hash,
153179
};
154180

155181
InternedInSet(self.arena.alloc(ty_struct))
@@ -887,8 +913,12 @@ pub enum UserType<'tcx> {
887913
}
888914

889915
impl<'tcx> CommonTypes<'tcx> {
890-
fn new(interners: &CtxtInterners<'tcx>) -> CommonTypes<'tcx> {
891-
let mk = |ty| interners.intern_ty(ty);
916+
fn new(
917+
interners: &CtxtInterners<'tcx>,
918+
sess: &Session,
919+
resolutions: &ty::ResolverOutputs,
920+
) -> CommonTypes<'tcx> {
921+
let mk = |ty| interners.intern_ty(ty, sess, resolutions);
892922

893923
CommonTypes {
894924
unit: mk(Tuple(List::empty())),
@@ -1162,7 +1192,7 @@ impl<'tcx> TyCtxt<'tcx> {
11621192
s.fatal(&err);
11631193
});
11641194
let interners = CtxtInterners::new(arena);
1165-
let common_types = CommonTypes::new(&interners);
1195+
let common_types = CommonTypes::new(&interners, s, &resolutions);
11661196
let common_lifetimes = CommonLifetimes::new(&interners);
11671197
let common_consts = CommonConsts::new(&interners, &common_types);
11681198

@@ -2276,7 +2306,7 @@ impl<'tcx> TyCtxt<'tcx> {
22762306
#[allow(rustc::usage_of_ty_tykind)]
22772307
#[inline]
22782308
pub fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> {
2279-
self.interners.intern_ty(st)
2309+
self.interners.intern_ty(st, self.sess, &self.gcx.untracked_resolutions)
22802310
}
22812311

22822312
#[inline]

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use self::Variance::*;
1717
pub use adt::*;
1818
pub use assoc::*;
1919
pub use generics::*;
20+
use rustc_data_structures::fingerprint::Fingerprint;
2021
pub use vtable::*;
2122

2223
use crate::metadata::ModChild;
@@ -424,11 +425,15 @@ crate struct TyS<'tcx> {
424425
/// De Bruijn indices within the type are contained within `0..D`
425426
/// (exclusive).
426427
outer_exclusive_binder: ty::DebruijnIndex,
428+
429+
/// The stable hash of the type. This way hashing of types will not have to work
430+
/// on the address of the type anymore, but can instead just read this field
431+
stable_hash: Fingerprint,
427432
}
428433

429434
// `TyS` is used a lot. Make sure it doesn't unintentionally get bigger.
430435
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
431-
static_assert_size!(TyS<'_>, 40);
436+
static_assert_size!(TyS<'_>, 56);
432437

433438
/// Use this rather than `TyS`, whenever possible.
434439
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -442,21 +447,25 @@ static BOOL_TYS: TyS<'static> = TyS {
442447
kind: ty::Bool,
443448
flags: TypeFlags::empty(),
444449
outer_exclusive_binder: DebruijnIndex::from_usize(0),
450+
stable_hash: Fingerprint::ZERO,
445451
};
446452

447453
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Ty<'tcx> {
448454
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
449455
let TyS {
450-
ref kind,
456+
kind,
451457

452458
// The other fields just provide fast access to information that is
453459
// also contained in `kind`, so no need to hash them.
454460
flags: _,
455461

456462
outer_exclusive_binder: _,
463+
464+
stable_hash,
457465
} = self.0.0;
458466

459-
kind.hash_stable(hcx, hasher);
467+
assert_ne!(*stable_hash, Fingerprint::ZERO, "{:#?}", kind);
468+
stable_hash.hash_stable(hcx, hasher);
460469
}
461470
}
462471

src/test/ui/symbol-names/basic.legacy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN5basic4main17h611df9c6948c15f7E)
1+
error: symbol-name(_ZN5basic4main17h87acd86b3a6f1754E)
22
--> $DIR/basic.rs:8:1
33
|
44
LL | #[rustc_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(basic::main::h611df9c6948c15f7)
7+
error: demangling(basic::main::h87acd86b3a6f1754)
88
--> $DIR/basic.rs:8:1
99
|
1010
LL | #[rustc_symbol_name]

src/test/ui/symbol-names/issue-60925.legacy.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h5425dadb5b1e5fb6E)
1+
error: symbol-name(_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h8d22952c45e20d65E)
22
--> $DIR/issue-60925.rs:21:9
33
|
44
LL | #[rustc_symbol_name]
55
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h5425dadb5b1e5fb6)
7+
error: demangling(issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo::h8d22952c45e20d65)
88
--> $DIR/issue-60925.rs:21:9
99
|
1010
LL | #[rustc_symbol_name]

0 commit comments

Comments
 (0)