Skip to content

Commit 323f5b2

Browse files
committed
Split crate_hash from index_hir.
1 parent 5530045 commit 323f5b2

File tree

7 files changed

+99
-119
lines changed

7 files changed

+99
-119
lines changed

compiler/rustc_middle/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ macro_rules! arena_types {
9191
[] predicates: rustc_middle::ty::PredicateInner<$tcx>,
9292

9393
// HIR query types
94-
[few] indexed_hir: rustc_middle::hir::map::IndexedHir<$tcx>,
94+
[few] indexed_hir: rustc_middle::hir::IndexedHir<$tcx>,
9595
[few] hir_definitions: rustc_hir::definitions::Definitions,
9696
[] hir_owner: rustc_middle::hir::Owner<$tcx>,
9797
[] hir_owner_nodes: rustc_middle::hir::OwnerNodes<$tcx>,

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

Lines changed: 13 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use crate::arena::Arena;
22
use crate::hir::map::{Entry, HirOwnerData, Map};
33
use crate::hir::{Owner, OwnerNodes, ParentedNode};
44
use crate::ich::StableHashingContext;
5-
use crate::middle::cstore::CrateStore;
65
use rustc_data_structures::fingerprint::Fingerprint;
76
use rustc_data_structures::fx::FxHashMap;
87
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9-
use rustc_data_structures::svh::Svh;
108
use rustc_hir as hir;
9+
use rustc_hir::def_id::LocalDefId;
1110
use rustc_hir::def_id::CRATE_DEF_INDEX;
12-
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
13-
use rustc_hir::definitions::{self, DefPathHash};
11+
use rustc_hir::definitions;
1412
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1513
use rustc_hir::*;
1614
use rustc_index::vec::{Idx, IndexVec};
17-
use rustc_session::{CrateDisambiguator, Session};
15+
use rustc_session::Session;
1816
use rustc_span::source_map::SourceMap;
19-
use rustc_span::{Span, Symbol, DUMMY_SP};
17+
use rustc_span::{Span, DUMMY_SP};
2018

2119
use std::iter::repeat;
2220

@@ -40,10 +38,6 @@ pub(super) struct NodeCollector<'a, 'hir> {
4038
definitions: &'a definitions::Definitions,
4139

4240
hcx: StableHashingContext<'a>,
43-
44-
// We are collecting HIR hashes here so we can compute the
45-
// crate hash from them later on.
46-
hir_body_nodes: Vec<(DefPathHash, Fingerprint)>,
4741
}
4842

4943
fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
@@ -58,34 +52,13 @@ fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V
5852

5953
fn hash_body(
6054
hcx: &mut StableHashingContext<'_>,
61-
def_path_hash: DefPathHash,
6255
item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
63-
hir_body_nodes: &mut Vec<(DefPathHash, Fingerprint)>,
6456
) -> Fingerprint {
65-
let hash = {
66-
let mut stable_hasher = StableHasher::new();
67-
hcx.while_hashing_hir_bodies(true, |hcx| {
68-
item_like.hash_stable(hcx, &mut stable_hasher);
69-
});
70-
stable_hasher.finish()
71-
};
72-
hir_body_nodes.push((def_path_hash, hash));
73-
hash
74-
}
75-
76-
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
77-
let mut upstream_crates: Vec<_> = cstore
78-
.crates_untracked()
79-
.iter()
80-
.map(|&cnum| {
81-
let name = cstore.crate_name_untracked(cnum);
82-
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
83-
let hash = cstore.crate_hash_untracked(cnum);
84-
(name, disambiguator, hash)
85-
})
86-
.collect();
87-
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
88-
upstream_crates
57+
let mut stable_hasher = StableHasher::new();
58+
hcx.while_hashing_hir_bodies(true, |hcx| {
59+
item_like.hash_stable(hcx, &mut stable_hasher);
60+
});
61+
stable_hasher.finish()
8962
}
9063

9164
impl<'a, 'hir> NodeCollector<'a, 'hir> {
@@ -96,11 +69,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
9669
definitions: &'a definitions::Definitions,
9770
mut hcx: StableHashingContext<'a>,
9871
) -> NodeCollector<'a, 'hir> {
99-
let root_mod_def_path_hash =
100-
definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX });
101-
102-
let mut hir_body_nodes = Vec::new();
103-
10472
let hash = {
10573
let Crate {
10674
ref item,
@@ -120,7 +88,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
12088
attrs: _,
12189
} = *krate;
12290

123-
hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes)
91+
hash_body(&mut hcx, item)
12492
};
12593

12694
let mut collector = NodeCollector {
@@ -131,7 +99,6 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
13199
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
132100
definitions,
133101
hcx,
134-
hir_body_nodes,
135102
map: (0..definitions.def_index_count())
136103
.map(|_| HirOwnerData { signature: None, with_bodies: None })
137104
.collect(),
@@ -147,53 +114,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
147114

148115
pub(super) fn finalize_and_compute_crate_hash(
149116
mut self,
150-
crate_disambiguator: CrateDisambiguator,
151-
cstore: &dyn CrateStore,
152-
commandline_args_hash: u64,
153-
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
117+
) -> IndexVec<LocalDefId, HirOwnerData<'hir>> {
154118
// Insert bodies into the map
155119
for (id, body) in self.krate.bodies.iter() {
156120
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
157121
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
158122
}
159-
160-
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
161-
162-
let node_hashes = self.hir_body_nodes.iter().fold(
163-
Fingerprint::ZERO,
164-
|combined_fingerprint, &(def_path_hash, fingerprint)| {
165-
combined_fingerprint.combine(def_path_hash.0.combine(fingerprint))
166-
},
167-
);
168-
169-
let upstream_crates = upstream_crates(cstore);
170-
171-
// We hash the final, remapped names of all local source files so we
172-
// don't have to include the path prefix remapping commandline args.
173-
// If we included the full mapping in the SVH, we could only have
174-
// reproducible builds by compiling from the same directory. So we just
175-
// hash the result of the mapping instead of the mapping itself.
176-
let mut source_file_names: Vec<_> = self
177-
.source_map
178-
.files()
179-
.iter()
180-
.filter(|source_file| source_file.cnum == LOCAL_CRATE)
181-
.map(|source_file| source_file.name_hash)
182-
.collect();
183-
184-
source_file_names.sort_unstable();
185-
186-
let crate_hash_input = (
187-
((node_hashes, upstream_crates), source_file_names),
188-
(commandline_args_hash, crate_disambiguator.to_fingerprint()),
189-
);
190-
191-
let mut stable_hasher = StableHasher::new();
192-
crate_hash_input.hash_stable(&mut self.hcx, &mut stable_hasher);
193-
let crate_hash: Fingerprint = stable_hasher.finish();
194-
195-
let svh = Svh::new(crate_hash.to_smaller_hash());
196-
(self.map, svh)
123+
self.map
197124
}
198125

199126
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
@@ -294,10 +221,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
294221
f: F,
295222
) {
296223
let prev_owner = self.current_dep_node_owner;
297-
298-
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
299-
300-
let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
224+
let hash = hash_body(&mut self.hcx, item_like);
301225

302226
self.current_dep_node_owner = dep_node_owner;
303227
f(self, hash);

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

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use self::collector::NodeCollector;
22

3-
use crate::hir::{Owner, OwnerNodes};
3+
use crate::hir::{HirOwnerData, IndexedHir};
4+
use crate::middle::cstore::CrateStore;
45
use crate::ty::TyCtxt;
56
use rustc_ast as ast;
7+
use rustc_data_structures::fingerprint::Fingerprint;
8+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
69
use rustc_data_structures::svh::Svh;
710
use rustc_hir::def::{DefKind, Res};
811
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -11,7 +14,6 @@ use rustc_hir::intravisit;
1114
use rustc_hir::intravisit::Visitor;
1215
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1316
use rustc_hir::*;
14-
use rustc_index::vec::IndexVec;
1517
use rustc_span::hygiene::MacroKind;
1618
use rustc_span::source_map::Spanned;
1719
use rustc_span::symbol::{kw, Ident, Symbol};
@@ -86,20 +88,6 @@ fn is_body_owner<'hir>(node: Node<'hir>, hir_id: HirId) -> bool {
8688
}
8789
}
8890

89-
#[derive(Debug)]
90-
pub(super) struct HirOwnerData<'hir> {
91-
pub(super) signature: Option<&'hir Owner<'hir>>,
92-
pub(super) with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
93-
}
94-
95-
#[derive(Debug)]
96-
pub struct IndexedHir<'hir> {
97-
/// The SVH of the local crate.
98-
pub crate_hash: Svh,
99-
100-
pub(super) map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
101-
}
102-
10391
#[derive(Copy, Clone)]
10492
pub struct Map<'hir> {
10593
pub(super) tcx: TyCtxt<'hir>,
@@ -935,19 +923,82 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
935923

936924
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
937925

938-
let (map, crate_hash) = {
926+
let map = {
939927
let hcx = tcx.create_stable_hashing_context();
940928

941929
let mut collector =
942930
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
943931
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
944932

945-
let crate_disambiguator = tcx.sess.local_crate_disambiguator();
946-
let cmdline_args = tcx.sess.opts.dep_tracking_hash(true);
947-
collector.finalize_and_compute_crate_hash(crate_disambiguator, &*tcx.cstore, cmdline_args)
933+
collector.finalize_and_compute_crate_hash()
948934
};
949935

950-
tcx.arena.alloc(IndexedHir { crate_hash, map })
936+
tcx.arena.alloc(IndexedHir { map })
937+
}
938+
939+
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
940+
let mut hir_body_nodes: Vec<_> = tcx
941+
.index_hir(crate_num)
942+
.map
943+
.iter_enumerated()
944+
.filter_map(|(def_id, hod)| {
945+
let def_path_hash = tcx.definitions.def_path_hash(def_id);
946+
let hash = hod.with_bodies.as_ref()?.hash;
947+
Some((def_path_hash, hash))
948+
})
949+
.collect();
950+
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
951+
952+
let node_hashes = hir_body_nodes.iter().fold(
953+
Fingerprint::ZERO,
954+
|combined_fingerprint, &(def_path_hash, fingerprint)| {
955+
combined_fingerprint.combine(def_path_hash.0.combine(fingerprint))
956+
},
957+
);
958+
959+
let upstream_crates = upstream_crates(&*tcx.cstore);
960+
961+
// We hash the final, remapped names of all local source files so we
962+
// don't have to include the path prefix remapping commandline args.
963+
// If we included the full mapping in the SVH, we could only have
964+
// reproducible builds by compiling from the same directory. So we just
965+
// hash the result of the mapping instead of the mapping itself.
966+
let mut source_file_names: Vec<_> = tcx
967+
.sess
968+
.source_map()
969+
.files()
970+
.iter()
971+
.filter(|source_file| source_file.cnum == LOCAL_CRATE)
972+
.map(|source_file| source_file.name_hash)
973+
.collect();
974+
975+
source_file_names.sort_unstable();
976+
977+
let mut hcx = tcx.create_stable_hashing_context();
978+
let mut stable_hasher = StableHasher::new();
979+
node_hashes.hash_stable(&mut hcx, &mut stable_hasher);
980+
upstream_crates.hash_stable(&mut hcx, &mut stable_hasher);
981+
source_file_names.hash_stable(&mut hcx, &mut stable_hasher);
982+
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
983+
tcx.sess.local_crate_disambiguator().to_fingerprint().hash_stable(&mut hcx, &mut stable_hasher);
984+
985+
let crate_hash: Fingerprint = stable_hasher.finish();
986+
Svh::new(crate_hash.to_smaller_hash())
987+
}
988+
989+
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
990+
let mut upstream_crates: Vec<_> = cstore
991+
.crates_untracked()
992+
.iter()
993+
.map(|&cnum| {
994+
let name = cstore.crate_name_untracked(cnum);
995+
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
996+
let hash = cstore.crate_hash_untracked(cnum);
997+
(name, disambiguator, hash)
998+
})
999+
.collect();
1000+
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
1001+
upstream_crates
9511002
}
9521003

9531004
fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ use rustc_index::vec::IndexVec;
1919
use rustc_span::DUMMY_SP;
2020
use std::collections::BTreeMap;
2121

22+
#[derive(Debug)]
23+
struct HirOwnerData<'hir> {
24+
signature: Option<&'hir Owner<'hir>>,
25+
with_bodies: Option<&'hir mut OwnerNodes<'hir>>,
26+
}
27+
28+
#[derive(Debug)]
29+
pub struct IndexedHir<'hir> {
30+
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
31+
}
32+
2233
#[derive(Debug)]
2334
pub struct Owner<'tcx> {
2435
parent: HirId,
@@ -117,6 +128,7 @@ pub fn provide(providers: &mut Providers) {
117128
};
118129
providers.hir_crate = |tcx, _| tcx.untracked_crate;
119130
providers.index_hir = map::index_hir;
131+
providers.crate_hash = map::crate_hash;
120132
providers.hir_module_items = |tcx, id| &tcx.untracked_crate.modules[&id];
121133
providers.hir_owner = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].signature;
122134
providers.hir_owner_nodes = |tcx, id| tcx.index_hir(LOCAL_CRATE).map[id].with_bodies.as_deref();

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ rustc_queries! {
2828

2929
/// The indexed HIR. This can be conveniently accessed by `tcx.hir()`.
3030
/// Avoid calling this query directly.
31-
query index_hir(_: CrateNum) -> &'tcx map::IndexedHir<'tcx> {
31+
query index_hir(_: CrateNum) -> &'tcx crate::hir::IndexedHir<'tcx> {
3232
eval_always
3333
no_hash
3434
desc { "index HIR" }

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::dep_graph;
22
use crate::hir::exports::Export;
3-
use crate::hir::map;
43
use crate::infer::canonical::{self, Canonical};
54
use crate::lint::LintLevelMap;
65
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_data_structures::fx::FxIndexSet;
2-
use rustc_data_structures::svh::Svh;
32
use rustc_hir as hir;
43
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
54
use rustc_middle::hir::map as hir_map;
@@ -400,10 +399,6 @@ fn original_crate_name(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Symbol {
400399
tcx.crate_name
401400
}
402401

403-
fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
404-
tcx.index_hir(crate_num).crate_hash
405-
}
406-
407402
fn instance_def_size_estimate<'tcx>(
408403
tcx: TyCtxt<'tcx>,
409404
instance_def: ty::InstanceDef<'tcx>,
@@ -551,7 +546,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
551546
trait_of_item,
552547
crate_disambiguator,
553548
original_crate_name,
554-
crate_hash,
555549
instance_def_size_estimate,
556550
issue33140_self_ty,
557551
impl_defaultness,

0 commit comments

Comments
 (0)