]> git.lizzy.rs Git - rust.git/blob - src/librustc_incremental/calculate_svh/mod.rs
incr.comp.: Hide concrete hash algorithm used for ICH
[rust.git] / src / librustc_incremental / calculate_svh / mod.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Calculation of the (misnamed) "strict version hash" for crates and
12 //! items. This hash is used to tell when the HIR changed in such a
13 //! way that results from previous compilations may no longer be
14 //! applicable and hence must be recomputed. It should probably be
15 //! renamed to the ICH (incremental compilation hash).
16 //!
17 //! The hashes for all items are computed once at the beginning of
18 //! compilation and stored into a map. In addition, a hash is computed
19 //! of the **entire crate**.
20 //!
21 //! Storing the hashes in a map avoids the need to compute them twice
22 //! (once when loading prior incremental results and once when
23 //! saving), but it is also important for correctness: at least as of
24 //! the time of this writing, the typeck passes rewrites entries in
25 //! the dep-map in-place to accommodate UFCS resolutions. Since name
26 //! resolution is part of the hash, the result is that hashes computed
27 //! at the end of compilation would be different from those computed
28 //! at the beginning.
29
30 use syntax::ast;
31 use std::cell::RefCell;
32 use std::hash::{Hash, Hasher};
33 use rustc::dep_graph::DepNode;
34 use rustc::hir;
35 use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
36 use rustc::hir::intravisit as visit;
37 use rustc::ty::TyCtxt;
38 use rustc_data_structures::fnv::FnvHashMap;
39 use rustc::util::common::record_time;
40 use rustc::session::config::DebugInfoLevel::NoDebugInfo;
41
42 use self::def_path_hash::DefPathHashes;
43 use self::svh_visitor::StrictVersionHashVisitor;
44 use self::caching_codemap_view::CachingCodemapView;
45 use self::hasher::IchHasher;
46
47 mod def_path_hash;
48 mod svh_visitor;
49 mod caching_codemap_view;
50 mod hasher;
51
52 pub struct IncrementalHashesMap {
53     hashes: FnvHashMap<DepNode<DefId>, u64>,
54
55     // These are the metadata hashes for the current crate as they were stored
56     // during the last compilation session. They are only loaded if
57     // -Z query-dep-graph was specified and are needed for auto-tests using
58     // the #[rustc_metadata_dirty] and #[rustc_metadata_clean] attributes to
59     // check whether some metadata hash has changed in between two revisions.
60     pub prev_metadata_hashes: RefCell<FnvHashMap<DefId, u64>>,
61 }
62
63 impl IncrementalHashesMap {
64     pub fn new() -> IncrementalHashesMap {
65         IncrementalHashesMap {
66             hashes: FnvHashMap(),
67             prev_metadata_hashes: RefCell::new(FnvHashMap()),
68         }
69     }
70
71     pub fn insert(&mut self, k: DepNode<DefId>, v: u64) -> Option<u64> {
72         self.hashes.insert(k, v)
73     }
74
75     pub fn iter<'a>(&'a self) -> ::std::collections::hash_map::Iter<'a, DepNode<DefId>, u64> {
76         self.hashes.iter()
77     }
78
79     pub fn len(&self) -> usize {
80         self.hashes.len()
81     }
82 }
83
84 impl<'a> ::std::ops::Index<&'a DepNode<DefId>> for IncrementalHashesMap {
85     type Output = u64;
86
87     fn index(&self, index: &'a DepNode<DefId>) -> &u64 {
88         &self.hashes[index]
89     }
90 }
91
92
93 pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
94                                                     -> IncrementalHashesMap {
95     let _ignore = tcx.dep_graph.in_ignore();
96     let krate = tcx.map.krate();
97     let hash_spans = tcx.sess.opts.debuginfo != NoDebugInfo;
98     let mut visitor = HashItemsVisitor {
99         tcx: tcx,
100         hashes: IncrementalHashesMap::new(),
101         def_path_hashes: DefPathHashes::new(tcx),
102         codemap: CachingCodemapView::new(tcx),
103         hash_spans: hash_spans,
104     };
105     record_time(&tcx.sess.perf_stats.incr_comp_hashes_time, || {
106         visitor.calculate_def_id(DefId::local(CRATE_DEF_INDEX),
107                                  |v| visit::walk_crate(v, krate));
108         krate.visit_all_items(&mut visitor);
109     });
110
111     tcx.sess.perf_stats.incr_comp_hashes_count.set(visitor.hashes.len() as u64);
112
113     record_time(&tcx.sess.perf_stats.svh_time, || visitor.compute_crate_hash());
114     visitor.hashes
115 }
116
117 struct HashItemsVisitor<'a, 'tcx: 'a> {
118     tcx: TyCtxt<'a, 'tcx, 'tcx>,
119     def_path_hashes: DefPathHashes<'a, 'tcx>,
120     codemap: CachingCodemapView<'tcx>,
121     hashes: IncrementalHashesMap,
122     hash_spans: bool,
123 }
124
125 impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
126     fn calculate_node_id<W>(&mut self, id: ast::NodeId, walk_op: W)
127         where W: for<'v> FnMut(&mut StrictVersionHashVisitor<'v, 'a, 'tcx>)
128     {
129         let def_id = self.tcx.map.local_def_id(id);
130         self.calculate_def_id(def_id, walk_op)
131     }
132
133     fn calculate_def_id<W>(&mut self, def_id: DefId, mut walk_op: W)
134         where W: for<'v> FnMut(&mut StrictVersionHashVisitor<'v, 'a, 'tcx>)
135     {
136         assert!(def_id.is_local());
137         debug!("HashItemsVisitor::calculate(def_id={:?})", def_id);
138         let mut state = IchHasher::new();
139         walk_op(&mut StrictVersionHashVisitor::new(&mut state,
140                                                    self.tcx,
141                                                    &mut self.def_path_hashes,
142                                                    &mut self.codemap,
143                                                    self.hash_spans));
144         let item_hash = state.finish();
145         self.hashes.insert(DepNode::Hir(def_id), item_hash);
146         debug!("calculate_item_hash: def_id={:?} hash={:?}", def_id, item_hash);
147
148         let bytes_hashed = self.tcx.sess.perf_stats.incr_comp_bytes_hashed.get() +
149                            state.bytes_hashed();
150         self.tcx.sess.perf_stats.incr_comp_bytes_hashed.set(bytes_hashed);
151     }
152
153     fn compute_crate_hash(&mut self) {
154         let krate = self.tcx.map.krate();
155
156         let mut crate_state = IchHasher::new();
157
158         let crate_disambiguator = self.tcx.sess.local_crate_disambiguator();
159         "crate_disambiguator".hash(&mut crate_state);
160         crate_disambiguator.len().hash(&mut crate_state);
161         crate_disambiguator.hash(&mut crate_state);
162
163         // add each item (in some deterministic order) to the overall
164         // crate hash.
165         {
166             let def_path_hashes = &mut self.def_path_hashes;
167             let mut item_hashes: Vec<_> =
168                 self.hashes.iter()
169                            .map(|(item_dep_node, &item_hash)| {
170                                // convert from a DepNode<DefId> tp a
171                                // DepNode<u64> where the u64 is the
172                                // hash of the def-id's def-path:
173                                let item_dep_node =
174                                    item_dep_node.map_def(|&did| Some(def_path_hashes.hash(did)))
175                                                 .unwrap();
176                                (item_dep_node, item_hash)
177                            })
178                            .collect();
179             item_hashes.sort(); // avoid artificial dependencies on item ordering
180             item_hashes.hash(&mut crate_state);
181         }
182
183         {
184             let mut visitor = StrictVersionHashVisitor::new(&mut crate_state,
185                                                             self.tcx,
186                                                             &mut self.def_path_hashes,
187                                                             &mut self.codemap,
188                                                             self.hash_spans);
189             visitor.hash_attributes(&krate.attrs);
190         }
191
192         let crate_hash = crate_state.finish();
193         self.hashes.insert(DepNode::Krate, crate_hash);
194         debug!("calculate_crate_hash: crate_hash={:?}", crate_hash);
195     }
196 }
197
198
199 impl<'a, 'tcx> visit::Visitor<'tcx> for HashItemsVisitor<'a, 'tcx> {
200     fn visit_item(&mut self, item: &'tcx hir::Item) {
201         self.calculate_node_id(item.id, |v| v.visit_item(item));
202         visit::walk_item(self, item);
203     }
204
205     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem) {
206         self.calculate_node_id(item.id, |v| v.visit_foreign_item(item));
207         visit::walk_foreign_item(self, item);
208     }
209 }
210