Skip to content

Commit df7b441

Browse files
committed
Precompute hash for OwnerInfo too.
1 parent bfdaf14 commit df7b441

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
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, 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};
@@ -674,16 +675,32 @@ impl<'hir> LoweringContext<'hir> {
674675
let bodies = SortedMap::from_presorted_elements(bodies);
675676

676677
// Don't hash unless necessary, because it's expensive.
677-
let (opt_hash_including_bodies, attrs_hash, delayed_lints_hash) =
678+
let (bodies_hash, attrs_hash, delayed_lints_hash) =
678679
self.tcx.hash_owner_nodes(node, &bodies, &attrs, &delayed_lints, define_opaque);
679680
let num_nodes = self.item_local_id_counter.as_usize();
680681
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
681-
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
682+
let nodes = hir::OwnerNodes { opt_hash: bodies_hash, nodes, bodies };
682683
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash, define_opaque };
683684
let delayed_lints =
684685
hir::lints::DelayedLints { lints: delayed_lints, opt_hash: delayed_lints_hash };
685686

687+
let opt_hash = if self.tcx.needs_crate_hash() {
688+
Some(self.tcx.with_stable_hashing_context(|mut hcx| {
689+
let mut stable_hasher = StableHasher::new();
690+
nodes.hash_stable(&mut hcx, &mut stable_hasher);
691+
parenting.hash_stable(&mut hcx, &mut stable_hasher);
692+
attrs.hash_stable(&mut hcx, &mut stable_hasher);
693+
trait_map.hash_stable(&mut hcx, &mut stable_hasher);
694+
delayed_lints.hash_stable(&mut hcx, &mut stable_hasher);
695+
children.hash_stable(&mut hcx, &mut stable_hasher);
696+
stable_hasher.finish()
697+
}))
698+
} else {
699+
None
700+
};
701+
686702
self.arena.alloc(hir::OwnerInfo {
703+
opt_hash,
687704
nodes,
688705
parenting,
689706
attrs,

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ impl<'tcx> AttributeMap<'tcx> {
14791479
pub struct OwnerNodes<'tcx> {
14801480
/// Pre-computed hash of the full HIR. Used in the crate hash. Only present
14811481
/// when incr. comp. is enabled.
1482-
pub opt_hash_including_bodies: Option<Fingerprint>,
1482+
pub opt_hash: Option<Fingerprint>,
14831483
/// Full HIR for the current owner.
14841484
// The zeroth node's parent should never be accessed: the owner's parent is computed by the
14851485
// hir_owner_parent query. It is set to `ItemLocalId::INVALID` to force an ICE if accidentally
@@ -1512,13 +1512,13 @@ impl fmt::Debug for OwnerNodes<'_> {
15121512
}),
15131513
)
15141514
.field("bodies", &self.bodies)
1515-
.field("opt_hash_including_bodies", &self.opt_hash_including_bodies)
1515+
.field("opt_hash", &self.opt_hash)
15161516
.finish()
15171517
}
15181518
}
15191519

15201520
/// Full information resulting from lowering an AST node.
1521-
#[derive(Debug, HashStable_Generic)]
1521+
#[derive(Debug)]
15221522
pub struct OwnerInfo<'hir> {
15231523
/// Contents of the HIR.
15241524
pub nodes: OwnerNodes<'hir>,
@@ -1534,6 +1534,8 @@ pub struct OwnerInfo<'hir> {
15341534
pub delayed_lints: DelayedLints,
15351535
/// Owners generated as side-effect by lowering.
15361536
pub children: UnordMap<LocalDefId, MaybeOwner<'hir>>,
1537+
// Only present when the crate hash is needed.
1538+
pub opt_hash: Option<Fingerprint>,
15371539
}
15381540

15391541
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::{HirId, ItemLocalId};
99
use crate::lints::DelayedLints;
@@ -92,25 +92,25 @@ impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId
9292
// in "DefPath Mode".
9393

9494
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
95+
#[inline]
9596
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
96-
// We ignore the `nodes` and `bodies` fields since these refer to information included in
97-
// `hash` which is hashed in the collector and used for the crate hash.
98-
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
99-
// the body satisfies the condition of two nodes being different have different
100-
// `hash_stable` results.
101-
let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
102-
opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
97+
// We ignore the other fields since these refer to information included in
98+
// `opt_hash` which is hashed in the collector and used for the crate hash.
99+
let OwnerNodes { opt_hash, .. } = *self;
100+
opt_hash.unwrap().hash_stable(hcx, hasher);
103101
}
104102
}
105103

106104
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for DelayedLints {
105+
#[inline]
107106
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
108107
let DelayedLints { opt_hash, .. } = *self;
109108
opt_hash.unwrap().hash_stable(hcx, hasher);
110109
}
111110
}
112111

113112
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
113+
#[inline]
114114
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
115115
// We ignore the `map` since it refers to information included in `opt_hash` which is
116116
// hashed in the collector and used for the crate hash.
@@ -119,6 +119,16 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap
119119
}
120120
}
121121

122+
impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerInfo<'tcx> {
123+
#[inline]
124+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
125+
// We ignore the rest since it refers to information included in `opt_hash` which is
126+
// hashed in the collector and used for the crate hash.
127+
let OwnerInfo { opt_hash, .. } = *self;
128+
opt_hash.unwrap().hash_stable(hcx, hasher);
129+
}
130+
}
131+
122132
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
123133
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
124134
hcx.hash_attr_id(self, hasher)

compiler/rustc_middle/src/ty/context.rs

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

1356-
let (opt_hash_including_bodies, _, _) =
1356+
let (opt_hash, _, _) =
13571357
self.tcx.hash_owner_nodes(node, &bodies, &attrs.map, &[], attrs.define_opaque);
13581358
let node = node.into();
13591359
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
1360-
opt_hash_including_bodies,
1360+
opt_hash,
13611361
nodes: IndexVec::from_elem_n(
13621362
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
13631363
1,

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn extract_hir_info<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ExtractedHir
327327
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx hir::Body<'tcx>) -> u64 {
328328
// FIXME(cjgillot) Stop hashing HIR manually here.
329329
let owner = hir_body.id().hir_id.owner;
330-
tcx.hir_owner_nodes(owner).opt_hash_including_bodies.unwrap().to_smaller_hash().as_u64()
330+
tcx.hir_owner_nodes(owner).opt_hash.unwrap().to_smaller_hash().as_u64()
331331
}
332332

333333
fn extract_hole_spans_from_hir<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &hir::Body<'tcx>) -> Vec<Span> {

0 commit comments

Comments
 (0)