Skip to content

Commit c29ccfb

Browse files
committed
Precompute hash for OwnerInfo too.
1 parent d31a656 commit c29ccfb

File tree

6 files changed

+48
-27
lines changed

6 files changed

+48
-27
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use rustc_ast::visit::{self, Visitor};
4545
use rustc_ast::{self as ast, *};
4646
use rustc_attr_parsing::{AttributeParser, Late, OmitDoc};
4747
use rustc_data_structures::sorted_map::SortedMap;
48+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4849
use rustc_data_structures::tagged_ptr::TaggedRef;
4950
use rustc_data_structures::unord::ExtendUnord;
5051
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
@@ -700,16 +701,32 @@ impl<'hir> LoweringContext<'hir> {
700701
let bodies = SortedMap::from_presorted_elements(bodies);
701702

702703
// Don't hash unless necessary, because it's expensive.
703-
let rustc_middle::hir::Hashes { opt_hash_including_bodies, attrs_hash, delayed_lints_hash } =
704+
let rustc_middle::hir::Hashes { bodies_hash, attrs_hash, delayed_lints_hash } =
704705
self.tcx.hash_owner_nodes(node, &bodies, &attrs, &delayed_lints, define_opaque);
705706
let num_nodes = self.item_local_id_counter.as_usize();
706707
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
707-
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
708+
let nodes = hir::OwnerNodes { opt_hash: bodies_hash, nodes, bodies };
708709
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash, define_opaque };
709710
let delayed_lints =
710711
hir::lints::DelayedLints { lints: delayed_lints, opt_hash: delayed_lints_hash };
711712

713+
let opt_hash = if self.tcx.needs_crate_hash() {
714+
Some(self.tcx.with_stable_hashing_context(|mut hcx| {
715+
let mut stable_hasher = StableHasher::new();
716+
bodies_hash.unwrap().hash_stable(&mut hcx, &mut stable_hasher);
717+
attrs_hash.unwrap().hash_stable(&mut hcx, &mut stable_hasher);
718+
delayed_lints_hash.unwrap().hash_stable(&mut hcx, &mut stable_hasher);
719+
parenting.hash_stable(&mut hcx, &mut stable_hasher);
720+
trait_map.hash_stable(&mut hcx, &mut stable_hasher);
721+
children.hash_stable(&mut hcx, &mut stable_hasher);
722+
stable_hasher.finish()
723+
}))
724+
} else {
725+
None
726+
};
727+
712728
self.arena.alloc(hir::OwnerInfo {
729+
opt_hash,
713730
nodes,
714731
parenting,
715732
attrs,

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ impl<'tcx> AttributeMap<'tcx> {
15071507
pub struct OwnerNodes<'tcx> {
15081508
/// Pre-computed hash of the full HIR. Used in the crate hash. Only present
15091509
/// when incr. comp. is enabled.
1510-
pub opt_hash_including_bodies: Option<Fingerprint>,
1510+
pub opt_hash: Option<Fingerprint>,
15111511
/// Full HIR for the current owner.
15121512
// The zeroth node's parent should never be accessed: the owner's parent is computed by the
15131513
// hir_owner_parent query. It is set to `ItemLocalId::INVALID` to force an ICE if accidentally
@@ -1540,13 +1540,13 @@ impl fmt::Debug for OwnerNodes<'_> {
15401540
}),
15411541
)
15421542
.field("bodies", &self.bodies)
1543-
.field("opt_hash_including_bodies", &self.opt_hash_including_bodies)
1543+
.field("opt_hash", &self.opt_hash)
15441544
.finish()
15451545
}
15461546
}
15471547

15481548
/// Full information resulting from lowering an AST node.
1549-
#[derive(Debug, HashStable_Generic)]
1549+
#[derive(Debug)]
15501550
pub struct OwnerInfo<'hir> {
15511551
/// Contents of the HIR.
15521552
pub nodes: OwnerNodes<'hir>,
@@ -1562,6 +1562,8 @@ pub struct OwnerInfo<'hir> {
15621562
pub delayed_lints: DelayedLints,
15631563
/// Owners generated as side-effect by lowering.
15641564
pub children: UnordMap<LocalDefId, MaybeOwner<'hir>>,
1565+
// Only present when the crate hash is needed.
1566+
pub opt_hash: Option<Fingerprint>,
15651567
}
15661568

15671569
impl<'tcx> OwnerInfo<'tcx> {

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 18 additions & 8 deletions
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, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
6+
AttributeMap, BodyId, ForeignItemId, ImplItemId, ItemId, OwnerInfo, OwnerNodes, TraitItemId,
77
};
88
use crate::hir_id::ItemLocalId;
99
use crate::lints::DelayedLints;
@@ -69,25 +69,25 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId
6969
// in "DefPath Mode".
7070

7171
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
72+
#[inline]
7273
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
73-
// We ignore the `nodes` and `bodies` fields since these refer to information included in
74-
// `hash` which is hashed in the collector and used for the crate hash.
75-
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
76-
// the body satisfies the condition of two nodes being different have different
77-
// `hash_stable` results.
78-
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
79-
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
74+
// We ignore the other fields since these refer to information included in
75+
// `opt_hash` which is hashed in the collector and used for the crate hash.
76+
let OwnerNodes { opt_hash, .. } = *self;
77+
opt_hash.unwrap().hash_stable(hcx, hasher);
8078
}
8179
}
8280

8381
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for DelayedLints {
82+
#[inline]
8483
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
8584
let DelayedLints { opt_hash, .. } = *self;
8685
opt_hash.unwrap().hash_stable(hcx, hasher);
8786
}
8887
}
8988

9089
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
90+
#[inline]
9191
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
9292
// We ignore the `map` since it refers to information included in `opt_hash` which is
9393
// hashed in the collector and used for the crate hash.
@@ -96,6 +96,16 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
9696
}
9797
}
9898

99+
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerInfo<'tcx> {
100+
#[inline]
101+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
102+
// We ignore the rest since it refers to information included in `opt_hash` which is
103+
// hashed in the collector and used for the crate hash.
104+
let OwnerInfo { opt_hash, .. } = *self;
105+
opt_hash.unwrap().hash_stable(hcx, hasher);
106+
}
107+
}
108+
99109
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
100110
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
101111
hcx.hash_attr_id(self, hasher)

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,7 @@ impl<'tcx> TyCtxt<'tcx> {
169169
define_opaque: Option<&[(Span, LocalDefId)]>,
170170
) -> Hashes {
171171
if !self.needs_crate_hash() {
172-
return Hashes {
173-
opt_hash_including_bodies: None,
174-
attrs_hash: None,
175-
delayed_lints_hash: None,
176-
};
172+
return Hashes { bodies_hash: None, attrs_hash: None, delayed_lints_hash: None };
177173
}
178174

179175
self.with_stable_hashing_context(|mut hcx| {
@@ -196,19 +192,15 @@ impl<'tcx> TyCtxt<'tcx> {
196192
delayed_lints.hash_stable(&mut hcx, &mut stable_hasher);
197193
let h3 = stable_hasher.finish();
198194

199-
Hashes {
200-
opt_hash_including_bodies: Some(h1),
201-
attrs_hash: Some(h2),
202-
delayed_lints_hash: Some(h3),
203-
}
195+
Hashes { bodies_hash: Some(h1), attrs_hash: Some(h2), delayed_lints_hash: Some(h3) }
204196
})
205197
}
206198
}
207199

208200
/// Hashes computed by [`TyCtxt::hash_owner_nodes`] if necessary.
209201
#[derive(Clone, Copy, Debug)]
210202
pub struct Hashes {
211-
pub opt_hash_including_bodies: Option<Fingerprint>,
203+
pub bodies_hash: Option<Fingerprint>,
212204
pub attrs_hash: Option<Fingerprint>,
213205
pub delayed_lints_hash: Option<Fingerprint>,
214206
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,11 +1493,11 @@ impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
14931493
let bodies = Default::default();
14941494
let attrs = hir::AttributeMap::EMPTY;
14951495

1496-
let rustc_middle::hir::Hashes { opt_hash_including_bodies, .. } =
1496+
let rustc_middle::hir::Hashes { bodies_hash, .. } =
14971497
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, &[], attrs.define_opaque);
14981498
let node = node.into();
14991499
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
1500-
opt_hash_including_bodies,
1500+
opt_hash: bodies_hash,
15011501
nodes: IndexVec::from_elem_n(
15021502
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
15031503
1,

compiler/rustc_mir_transform/src/coverage/hir_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> E
6969
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
7070
let owner = hir_body.id().hir_id.owner;
7171
tcx.hir_owner_nodes(owner)
72-
.opt_hash_including_bodies
72+
.opt_hash
7373
.expect("hash should be present when coverage instrumentation is enabled")
7474
.to_smaller_hash()
7575
.as_u64()

0 commit comments

Comments
 (0)