Skip to content

Commit ba6f93c

Browse files
incr.comp.: Make the StableHashingContext mostly independent of the tcx.
1 parent e567afb commit ba6f93c

File tree

5 files changed

+61
-39
lines changed

5 files changed

+61
-39
lines changed

src/librustc/ich/hcx.rs

+41-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
// except according to those terms.
1010

1111
use hir;
12-
use hir::def_id::DefId;
12+
use hir::def_id::{DefId, DefIndex};
1313
use hir::map::DefPathHash;
1414
use ich::{self, CachingCodemapView};
1515
use session::config::DebugInfoLevel::NoDebugInfo;
1616
use ty::{self, TyCtxt, fast_reject};
17+
use session::Session;
1718

1819
use std::cmp::Ord;
1920
use std::hash as std_hash;
@@ -42,6 +43,7 @@ thread_local!(static IGNORED_ATTR_NAMES: RefCell<FxHashSet<Symbol>> =
4243
/// things (e.g. each DefId/DefPath is only hashed once).
4344
pub struct StableHashingContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
4445
tcx: TyCtxt<'a, 'gcx, 'tcx>,
46+
body_resolver: BodyResolver<'gcx>,
4547
hash_spans: bool,
4648
hash_bodies: bool,
4749
overflow_checks_enabled: bool,
@@ -59,6 +61,20 @@ pub enum NodeIdHashingMode {
5961
HashDefPath,
6062
}
6163

64+
/// The BodyResolver allows to map a BodyId to the corresponding hir::Body.
65+
/// We could also just store a plain reference to the hir::Crate but we want
66+
/// to avoid that the crate is used to get untracked access to all of the HIR.
67+
#[derive(Clone, Copy)]
68+
struct BodyResolver<'hir>(&'hir hir::Crate);
69+
70+
impl<'hir> BodyResolver<'hir> {
71+
// Return a reference to the hir::Body with the given BodyId.
72+
// DOES NOT DO ANY TRACKING, use carefully.
73+
fn body(self, id: hir::BodyId) -> &'hir hir::Body {
74+
self.0.body(id)
75+
}
76+
}
77+
6278
impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
6379

6480
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Self {
@@ -74,8 +90,11 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
7490
}
7591
});
7692

93+
let body_resolver = BodyResolver(tcx.dep_graph.with_ignore(|| tcx.hir.krate()));
94+
7795
StableHashingContext {
7896
tcx,
97+
body_resolver,
7998
caching_codemap: None,
8099
raw_codemap: tcx.sess.codemap(),
81100
hash_spans: hash_spans_initial,
@@ -85,6 +104,11 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
85104
}
86105
}
87106

107+
#[inline]
108+
pub fn sess(&self) -> &'gcx Session {
109+
self.tcx.sess
110+
}
111+
88112
pub fn force_span_hashing(mut self) -> Self {
89113
self.hash_spans = true;
90114
self
@@ -121,13 +145,13 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
121145
}
122146

123147
#[inline]
124-
pub fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx> {
125-
self.tcx
148+
pub fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
149+
self.tcx.def_path_hash(def_id)
126150
}
127151

128152
#[inline]
129-
pub fn def_path_hash(&mut self, def_id: DefId) -> DefPathHash {
130-
self.tcx.def_path_hash(def_id)
153+
pub fn local_def_path_hash(&self, def_index: DefIndex) -> DefPathHash {
154+
self.tcx.hir.definitions().def_path_hash(def_index)
131155
}
132156

133157
#[inline]
@@ -221,6 +245,16 @@ impl<'a, 'gcx, 'lcx> StableHashingContextProvider for ty::TyCtxt<'a, 'gcx, 'lcx>
221245
}
222246
}
223247

248+
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::BodyId {
249+
fn hash_stable<W: StableHasherResult>(&self,
250+
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
251+
hasher: &mut StableHasher<W>) {
252+
if hcx.hash_bodies() {
253+
hcx.body_resolver.body(*self).hash_stable(hcx, hasher);
254+
}
255+
}
256+
}
257+
224258
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::HirId {
225259
#[inline]
226260
fn hash_stable<W: StableHasherResult>(&self,
@@ -250,7 +284,7 @@ impl<'a, 'gcx, 'tcx> ToStableHashKey<StableHashingContext<'a, 'gcx, 'tcx>> for h
250284
fn to_stable_hash_key(&self,
251285
hcx: &StableHashingContext<'a, 'gcx, 'tcx>)
252286
-> (DefPathHash, hir::ItemLocalId) {
253-
let def_path_hash = hcx.tcx().hir.definitions().def_path_hash(self.owner);
287+
let def_path_hash = hcx.local_def_path_hash(self.owner);
254288
(def_path_hash, self.local_id)
255289
}
256290
}
@@ -378,10 +412,9 @@ pub fn hash_stable_trait_impls<'a, 'tcx, 'gcx, W, R>(
378412
}
379413

380414
{
381-
let tcx = hcx.tcx();
382415
let mut keys: AccumulateVec<[_; 8]> =
383416
non_blanket_impls.keys()
384-
.map(|k| (k, k.map_def(|d| tcx.def_path_hash(d))))
417+
.map(|k| (k, k.map_def(|d| hcx.def_path_hash(d))))
385418
.collect();
386419
keys.sort_unstable_by(|&(_, ref k1), &(_, ref k2)| k1.cmp(k2));
387420
keys.len().hash_stable(hcx, hasher);

src/librustc/ich/impls_hir.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'a, 'gcx, 'tcx> ToStableHashKey<StableHashingContext<'a, 'gcx, 'tcx>> for D
3434

3535
#[inline]
3636
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a, 'gcx, 'tcx>) -> DefPathHash {
37-
hcx.tcx().def_path_hash(*self)
37+
hcx.def_path_hash(*self)
3838
}
3939
}
4040

@@ -995,16 +995,6 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::B
995995
}
996996
}
997997

998-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::BodyId {
999-
fn hash_stable<W: StableHasherResult>(&self,
1000-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
1001-
hasher: &mut StableHasher<W>) {
1002-
if hcx.hash_bodies() {
1003-
hcx.tcx().hir.body(*self).hash_stable(hcx, hasher);
1004-
}
1005-
}
1006-
}
1007-
1008998
impl<'a, 'gcx, 'tcx> ToStableHashKey<StableHashingContext<'a, 'gcx, 'tcx>> for hir::BodyId {
1009999
type KeyType = (DefPathHash, hir::ItemLocalId);
10101000

@@ -1119,7 +1109,7 @@ for hir::def_id::DefIndex {
11191109
fn hash_stable<W: StableHasherResult>(&self,
11201110
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
11211111
hasher: &mut StableHasher<W>) {
1122-
hcx.tcx().hir.definitions().def_path_hash(*self).hash_stable(hcx, hasher);
1112+
hcx.local_def_path_hash(*self).hash_stable(hcx, hasher);
11231113
}
11241114
}
11251115

@@ -1129,7 +1119,7 @@ for hir::def_id::DefIndex {
11291119

11301120
#[inline]
11311121
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a, 'gcx, 'tcx>) -> DefPathHash {
1132-
hcx.tcx().hir.definitions().def_path_hash(*self)
1122+
hcx.local_def_path_hash(*self)
11331123
}
11341124
}
11351125

src/librustc/ich/impls_syntax.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,11 @@ fn hash_token<'a, 'gcx, 'tcx, W: StableHasherResult>(token: &token::Token,
324324
// in a stable way, in addition to the HIR.
325325
// Since this is hardly used anywhere, just emit a
326326
// warning for now.
327-
if hcx.tcx().sess.opts.debugging_opts.incremental.is_some() {
327+
if hcx.sess().opts.debugging_opts.incremental.is_some() {
328328
let msg = format!("Quasi-quoting might make incremental \
329329
compilation very inefficient: {:?}",
330330
non_terminal);
331-
hcx.tcx().sess.span_warn(error_reporting_span, &msg[..]);
331+
hcx.sess().span_warn(error_reporting_span, &msg[..]);
332332
}
333333

334334
std_hash::Hash::hash(non_terminal, hasher);

src/librustc/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Typeck
736736
krate: local_id_root.krate,
737737
index: closure_expr_id,
738738
};
739-
(hcx.tcx().def_path_hash(var_owner_def_id),
739+
(hcx.def_path_hash(var_owner_def_id),
740740
var_id.local_id,
741-
hcx.tcx().def_path_hash(closure_def_id))
741+
hcx.def_path_hash(closure_def_id))
742742
});
743743

744744
closure_tys.hash_stable(hcx, hasher);

src/librustc_incremental/calculate_svh/mod.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl<'a> ::std::ops::Index<&'a DepNode> for IncrementalHashesMap {
9090
}
9191

9292
struct ComputeItemHashesVisitor<'a, 'tcx: 'a> {
93+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
9394
hcx: StableHashingContext<'a, 'tcx, 'tcx>,
9495
hashes: IncrementalHashesMap,
9596
}
@@ -101,14 +102,14 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
101102
item_like: T)
102103
where T: HashStable<StableHashingContext<'a, 'tcx, 'tcx>>
103104
{
104-
if !hash_bodies && !self.hcx.tcx().sess.opts.build_dep_graph() {
105+
if !hash_bodies && !self.tcx.sess.opts.build_dep_graph() {
105106
// If we just need the hashes in order to compute the SVH, we don't
106107
// need have two hashes per item. Just the one containing also the
107108
// item's body is sufficient.
108109
return
109110
}
110111

111-
let def_path_hash = self.hcx.tcx().hir.definitions().def_path_hash(def_index);
112+
let def_path_hash = self.hcx.local_def_path_hash(def_index);
112113

113114
let mut hasher = IchHasher::new();
114115
self.hcx.while_hashing_hir_bodies(hash_bodies, |hcx| {
@@ -125,14 +126,12 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
125126
debug!("calculate_def_hash: dep_node={:?} hash={:?}", dep_node, item_hash);
126127
self.hashes.insert(dep_node, item_hash);
127128

128-
let tcx = self.hcx.tcx();
129129
let bytes_hashed =
130-
tcx.sess.perf_stats.incr_comp_bytes_hashed.get() +
131-
bytes_hashed;
132-
tcx.sess.perf_stats.incr_comp_bytes_hashed.set(bytes_hashed);
130+
self.tcx.sess.perf_stats.incr_comp_bytes_hashed.get() + bytes_hashed;
131+
self.tcx.sess.perf_stats.incr_comp_bytes_hashed.set(bytes_hashed);
133132

134133
if hash_bodies {
135-
let in_scope_traits_map = tcx.in_scope_traits_map(def_index);
134+
let in_scope_traits_map = self.tcx.in_scope_traits_map(def_index);
136135
let mut hasher = IchHasher::new();
137136
in_scope_traits_map.hash_stable(&mut self.hcx, &mut hasher);
138137
let dep_node = def_path_hash.to_dep_node(DepKind::InScopeTraits);
@@ -141,12 +140,11 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
141140
}
142141

143142
fn compute_crate_hash(&mut self) {
144-
let tcx = self.hcx.tcx();
145-
let krate = tcx.hir.krate();
143+
let krate = self.tcx.hir.krate();
146144

147145
let mut crate_state = IchHasher::new();
148146

149-
let crate_disambiguator = tcx.sess.local_crate_disambiguator();
147+
let crate_disambiguator = self.tcx.sess.local_crate_disambiguator();
150148
"crate_disambiguator".hash(&mut crate_state);
151149
crate_disambiguator.as_str().len().hash(&mut crate_state);
152150
crate_disambiguator.as_str().hash(&mut crate_state);
@@ -221,7 +219,7 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
221219

222220
fn compute_and_store_ich_for_trait_impls(&mut self, krate: &'tcx hir::Crate)
223221
{
224-
let tcx = self.hcx.tcx();
222+
let tcx = self.tcx;
225223

226224
let mut impls: Vec<(DefPathHash, Fingerprint)> = krate
227225
.trait_impls
@@ -266,7 +264,7 @@ impl<'a, 'tcx: 'a> ComputeItemHashesVisitor<'a, 'tcx> {
266264

267265
impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for ComputeItemHashesVisitor<'a, 'tcx> {
268266
fn visit_item(&mut self, item: &'tcx hir::Item) {
269-
let def_index = self.hcx.tcx().hir.local_def_id(item.id).index;
267+
let def_index = self.tcx.hir.local_def_id(item.id).index;
270268
self.compute_and_store_ich_for_item_like(def_index,
271269
false,
272270
item);
@@ -276,7 +274,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for ComputeItemHashesVisitor<'a, 'tcx>
276274
}
277275

278276
fn visit_trait_item(&mut self, item: &'tcx hir::TraitItem) {
279-
let def_index = self.hcx.tcx().hir.local_def_id(item.id).index;
277+
let def_index = self.tcx.hir.local_def_id(item.id).index;
280278
self.compute_and_store_ich_for_item_like(def_index,
281279
false,
282280
item);
@@ -286,7 +284,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for ComputeItemHashesVisitor<'a, 'tcx>
286284
}
287285

288286
fn visit_impl_item(&mut self, item: &'tcx hir::ImplItem) {
289-
let def_index = self.hcx.tcx().hir.local_def_id(item.id).index;
287+
let def_index = self.tcx.hir.local_def_id(item.id).index;
290288
self.compute_and_store_ich_for_item_like(def_index,
291289
false,
292290
item);
@@ -304,6 +302,7 @@ pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
304302
let krate = tcx.hir.krate();
305303

306304
let mut visitor = ComputeItemHashesVisitor {
305+
tcx,
307306
hcx: StableHashingContext::new(tcx),
308307
hashes: IncrementalHashesMap::new(),
309308
};

0 commit comments

Comments
 (0)