Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ed3c8e8

Browse files
committed
Hash during lowering.
1 parent 457de08 commit ed3c8e8

File tree

4 files changed

+35
-28
lines changed

4 files changed

+35
-28
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use rustc_ast::visit;
4141
use rustc_ast::{self as ast, *};
4242
use rustc_ast_pretty::pprust;
4343
use rustc_data_structures::captures::Captures;
44+
use rustc_data_structures::fingerprint::Fingerprint;
4445
use rustc_data_structures::fx::FxHashSet;
46+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4547
use rustc_data_structures::sync::Lrc;
4648
use rustc_errors::{struct_span_err, Applicability};
4749
use rustc_hir as hir;
@@ -467,7 +469,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
467469
}
468470
}
469471

470-
hir::OwnerInfo { node, attrs, bodies, trait_map }
472+
let (hash, node_hash) = self.hash_body(node, &bodies);
473+
474+
hir::OwnerInfo { hash, node_hash, node, attrs, bodies, trait_map }
475+
}
476+
477+
/// Hash the HIR node twice, one deep and one shallow hash. This allows to differentiate
478+
/// queries which depend on the full HIR tree and those which only depend on the item signature.
479+
fn hash_body(
480+
&mut self,
481+
node: hir::OwnerNode<'hir>,
482+
bodies: &IndexVec<hir::ItemLocalId, Option<&'hir hir::Body<'hir>>>,
483+
) -> (Fingerprint, Fingerprint) {
484+
let mut hcx = self.resolver.create_stable_hashing_context();
485+
let mut stable_hasher = StableHasher::new();
486+
hcx.with_hir_bodies(true, node.def_id(), bodies, |hcx| {
487+
node.hash_stable(hcx, &mut stable_hasher)
488+
});
489+
let full_hash = stable_hasher.finish();
490+
let mut stable_hasher = StableHasher::new();
491+
hcx.with_hir_bodies(false, node.def_id(), bodies, |hcx| {
492+
node.hash_stable(hcx, &mut stable_hasher)
493+
});
494+
let node_hash = stable_hasher.finish();
495+
(full_hash, node_hash)
471496
}
472497

473498
/// This method allocates a new `HirId` for the given `NodeId` and stores it in

compiler/rustc_hir/src/hir.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast::{Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, TraitObject
99
pub use rustc_ast::{BorrowKind, ImplPolarity, IsAuto};
1010
pub use rustc_ast::{CaptureBy, Movability, Mutability};
1111
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
12+
use rustc_data_structures::fingerprint::Fingerprint;
1213
use rustc_data_structures::fx::FxHashMap;
1314
use rustc_index::vec::IndexVec;
1415
use rustc_macros::HashStable_Generic;
@@ -670,6 +671,10 @@ pub struct OwnerInfo<'hir> {
670671
/// Map indicating what traits are in scope for places where this
671672
/// is relevant; generated by resolve.
672673
pub trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
674+
/// Pre-computed hash of the full HIR.
675+
pub hash: Fingerprint,
676+
/// Pre-computed hash of the top node.
677+
pub node_hash: Fingerprint,
673678
}
674679

675680
/// The top-level data structure that stores the entire contents of

compiler/rustc_middle/src/hir/map/collector.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use crate::arena::Arena;
22
use crate::hir::map::Map;
33
use crate::hir::{IndexedHir, OwnerNodes, ParentedNode};
4-
use rustc_data_structures::fingerprint::Fingerprint;
54
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
75
use rustc_hir as hir;
86
use rustc_hir::def_id::LocalDefId;
97
use rustc_hir::def_id::CRATE_DEF_ID;
108
use rustc_hir::definitions;
119
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1210
use rustc_hir::*;
1311
use rustc_index::vec::{Idx, IndexVec};
14-
use rustc_query_system::ich::StableHashingContext;
1512
use rustc_session::Session;
1613
use rustc_span::source_map::SourceMap;
1714
use rustc_span::{Span, DUMMY_SP};
@@ -37,8 +34,6 @@ pub(super) struct NodeCollector<'a, 'hir> {
3734
current_dep_node_owner: LocalDefId,
3835

3936
definitions: &'a definitions::Definitions,
40-
41-
hcx: StableHashingContext<'a>,
4237
}
4338

4439
fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
@@ -51,27 +46,12 @@ fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V
5146
map[k] = Some(v);
5247
}
5348

54-
fn hash_body<'s, 'hir: 's>(
55-
hcx: &mut StableHashingContext<'s>,
56-
item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
57-
hash_bodies: bool,
58-
owner: LocalDefId,
59-
bodies: &'hir IndexVec<ItemLocalId, Option<&'hir Body<'hir>>>,
60-
) -> Fingerprint {
61-
let mut stable_hasher = StableHasher::new();
62-
hcx.with_hir_bodies(hash_bodies, owner, bodies, |hcx| {
63-
item_like.hash_stable(hcx, &mut stable_hasher)
64-
});
65-
stable_hasher.finish()
66-
}
67-
6849
impl<'a, 'hir: 'a> NodeCollector<'a, 'hir> {
6950
pub(super) fn root(
7051
sess: &'a Session,
7152
arena: &'hir Arena<'hir>,
7253
krate: &'hir Crate<'hir>,
7354
definitions: &'a definitions::Definitions,
74-
hcx: StableHashingContext<'a>,
7555
) -> NodeCollector<'a, 'hir> {
7656
let mut collector = NodeCollector {
7757
arena,
@@ -80,7 +60,6 @@ impl<'a, 'hir: 'a> NodeCollector<'a, 'hir> {
8060
parent_node: hir::CRATE_HIR_ID,
8161
current_dep_node_owner: CRATE_DEF_ID,
8262
definitions,
83-
hcx,
8463
map: IndexVec::from_fn_n(|_| None, definitions.def_index_count()),
8564
parenting: FxHashMap::default(),
8665
};
@@ -97,10 +76,10 @@ impl<'a, 'hir: 'a> NodeCollector<'a, 'hir> {
9776
let mut nodes = IndexVec::new();
9877
nodes.push(Some(ParentedNode { parent: ItemLocalId::new(0), node: node.into() }));
9978

100-
let bodies = &self.krate.owners[owner].as_ref().unwrap().bodies;
101-
102-
let hash = hash_body(&mut self.hcx, node, true, owner, bodies);
103-
let node_hash = hash_body(&mut self.hcx, node, false, owner, bodies);
79+
let info = self.krate.owners[owner].as_ref().unwrap();
80+
let hash = info.hash;
81+
let node_hash = info.node_hash;
82+
let bodies = &info.bodies;
10483

10584
debug_assert!(self.map[owner].is_none());
10685
self.map[owner] = Some(self.arena.alloc(OwnerNodes { hash, node_hash, nodes, bodies }));

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,11 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx IndexedHir<'tc
10711071
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
10721072

10731073
// We can access untracked state since we are an eval_always query.
1074-
let hcx = tcx.create_stable_hashing_context();
10751074
let mut collector = NodeCollector::root(
10761075
tcx.sess,
10771076
&**tcx.arena,
10781077
tcx.untracked_crate,
10791078
&tcx.untracked_resolutions.definitions,
1080-
hcx,
10811079
);
10821080
let top_mod = tcx.untracked_crate.module();
10831081
collector.visit_mod(top_mod, top_mod.inner, CRATE_HIR_ID);

0 commit comments

Comments
 (0)