Skip to content

Commit d31ac77

Browse files
committed
Compute hir hash lazily
1 parent 99fdeda commit d31ac77

File tree

5 files changed

+29
-41
lines changed

5 files changed

+29
-41
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+2-30
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ use std::sync::Arc;
4747
use rustc_ast::node_id::NodeMap;
4848
use rustc_ast::{self as ast, *};
4949
use rustc_attr_parsing::{AttributeParser, OmitDoc};
50-
use rustc_data_structures::fingerprint::Fingerprint;
5150
use rustc_data_structures::sorted_map::SortedMap;
52-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5351
use rustc_data_structures::tagged_ptr::TaggedRef;
5452
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle, StashKey};
5553
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5654
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5755
use rustc_hir::{
5856
self as hir, ConstArg, GenericArg, HirId, ItemLocalMap, LangItem, ParamName, TraitCandidate,
5957
};
60-
use rustc_index::{Idx, IndexSlice, IndexVec};
58+
use rustc_index::{Idx, IndexVec};
6159
use rustc_macros::extension;
6260
use rustc_middle::span_bug;
6361
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
@@ -408,29 +406,6 @@ fn index_crate<'a>(
408406
}
409407
}
410408

411-
/// Compute the hash for the HIR of the full crate.
412-
/// This hash will then be part of the crate_hash which is stored in the metadata.
413-
fn compute_hir_hash(
414-
tcx: TyCtxt<'_>,
415-
owners: &IndexSlice<LocalDefId, hir::MaybeOwner<'_>>,
416-
) -> Fingerprint {
417-
let mut hir_body_nodes: Vec<_> = owners
418-
.iter_enumerated()
419-
.filter_map(|(def_id, info)| {
420-
let info = info.as_owner()?;
421-
let def_path_hash = tcx.hir_def_path_hash(def_id);
422-
Some((def_path_hash, info))
423-
})
424-
.collect();
425-
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
426-
427-
tcx.with_stable_hashing_context(|mut hcx| {
428-
let mut stable_hasher = StableHasher::new();
429-
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
430-
stable_hasher.finish()
431-
})
432-
}
433-
434409
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
435410
let sess = tcx.sess;
436411
// Queries that borrow `resolver_for_lowering`.
@@ -460,10 +435,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
460435
drop(ast_index);
461436
sess.time("drop_ast", || drop(krate));
462437

463-
// Don't hash unless necessary, because it's expensive.
464-
let opt_hir_hash =
465-
if tcx.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
466-
hir::Crate { owners, opt_hir_hash }
438+
hir::Crate { owners }
467439
}
468440

469441
#[derive(Copy, Clone, PartialEq, Debug)]

Diff for: compiler/rustc_hir/src/hir.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,6 @@ impl<'tcx> MaybeOwner<'tcx> {
14251425
#[derive(Debug)]
14261426
pub struct Crate<'hir> {
14271427
pub owners: IndexVec<LocalDefId, MaybeOwner<'hir>>,
1428-
// Only present when incr. comp. is enabled.
1429-
pub opt_hir_hash: Option<Fingerprint>,
14301428
}
14311429

14321430
#[derive(Debug, Clone, Copy, HashStable_Generic)]

Diff for: compiler/rustc_hir/src/stable_hash_impls.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_span::def_id::DefPathHash;
33

44
use crate::HashIgnoredAttrId;
55
use crate::hir::{
6-
AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
6+
AttributeMap, BodyId, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
77
};
88
use crate::hir_id::{HirId, ItemLocalId};
99

@@ -111,13 +111,6 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
111111
}
112112
}
113113

114-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
115-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
116-
let Crate { owners: _, opt_hir_hash } = self;
117-
opt_hir_hash.unwrap().hash_stable(hcx, hasher)
118-
}
119-
}
120-
121114
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
122115
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
123116
hcx.hash_attr_id(self, hasher)

Diff for: compiler/rustc_middle/src/hir/map.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1010
use rustc_hir::intravisit::Visitor;
1111
use rustc_hir::*;
1212
use rustc_hir_pretty as pprust_hir;
13+
use rustc_index::IndexSlice;
1314
use rustc_span::def_id::StableCrateId;
1415
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, sym, with_metavar_spans};
1516

@@ -1127,9 +1128,32 @@ impl<'tcx> pprust_hir::PpAnn for TyCtxt<'tcx> {
11271128
}
11281129
}
11291130

1131+
/// Compute the hash for the HIR of the full crate.
1132+
/// This hash will then be part of the crate_hash which is stored in the metadata.
1133+
fn compute_hir_hash(
1134+
tcx: TyCtxt<'_>,
1135+
owners: &IndexSlice<LocalDefId, MaybeOwner<'_>>,
1136+
) -> Fingerprint {
1137+
let mut hir_body_nodes: Vec<_> = owners
1138+
.iter_enumerated()
1139+
.filter_map(|(def_id, info)| {
1140+
let info = info.as_owner()?;
1141+
let def_path_hash = tcx.hir_def_path_hash(def_id);
1142+
Some((def_path_hash, info))
1143+
})
1144+
.collect();
1145+
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
1146+
1147+
tcx.with_stable_hashing_context(|mut hcx| {
1148+
let mut stable_hasher = StableHasher::new();
1149+
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
1150+
stable_hasher.finish()
1151+
})
1152+
}
1153+
11301154
pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
11311155
let krate = tcx.hir_crate(());
1132-
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
1156+
let hir_body_hash = compute_hir_hash(tcx, &krate.owners);
11331157

11341158
let upstream_crates = upstream_crates(tcx);
11351159

Diff for: compiler/rustc_middle/src/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ rustc_queries! {
151151
query hir_crate(key: ()) -> &'tcx Crate<'tcx> {
152152
arena_cache
153153
eval_always
154+
no_hash
154155
desc { "getting the crate HIR" }
155156
}
156157

0 commit comments

Comments
 (0)