1 //! Code to save/load the dep-graph from files.
3 use rustc::dep_graph::{PreviousDepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
4 use rustc::session::Session;
5 use rustc::ty::query::OnDiskCache;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_serialize::opaque::Decoder;
9 use rustc_serialize::Decodable as RustcDecodable;
13 use super::file_format;
15 use super::work_product;
17 pub fn dep_graph_tcx_init(tcx: TyCtxt<'_>) {
18 if !tcx.dep_graph.is_fully_enabled() {
22 tcx.allocate_metadata_dep_nodes();
25 type WorkProductMap = FxHashMap<WorkProductId, WorkProduct>;
27 pub enum LoadResult<T> {
30 Error { message: String },
33 impl LoadResult<(PreviousDepGraph, WorkProductMap)> {
34 pub fn open(self, sess: &Session) -> (PreviousDepGraph, WorkProductMap) {
36 LoadResult::Error { message } => {
40 LoadResult::DataOutOfDate => {
41 if let Err(err) = delete_all_session_dir_contents(sess) {
43 "Failed to delete invalidated or incompatible \
44 incremental compilation session directory contents `{}`: {}.",
45 dep_graph_path(sess).display(),
51 LoadResult::Ok { data } => data,
56 fn load_data(report_incremental_info: bool, path: &Path) -> LoadResult<(Vec<u8>, usize)> {
57 match file_format::read_file(report_incremental_info, path) {
58 Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
60 // The file either didn't exist or was produced by an incompatible
61 // compiler version. Neither is an error.
62 LoadResult::DataOutOfDate
64 Err(err) => LoadResult::Error {
65 message: format!("could not load dep-graph from `{}`: {}", path.display(), err),
70 fn delete_dirty_work_product(sess: &Session, swp: SerializedWorkProduct) {
71 debug!("delete_dirty_work_product({:?})", swp);
72 work_product::delete_workproduct_files(sess, &swp.work_product);
75 /// Either a result that has already be computed or a
76 /// handle that will let us wait until it is computed
77 /// by a background thread.
78 pub enum MaybeAsync<T> {
80 Async(std::thread::JoinHandle<T>),
82 impl<T> MaybeAsync<T> {
83 pub fn open(self) -> std::thread::Result<T> {
85 MaybeAsync::Sync(result) => Ok(result),
86 MaybeAsync::Async(handle) => handle.join(),
91 pub type DepGraphFuture = MaybeAsync<LoadResult<(PreviousDepGraph, WorkProductMap)>>;
93 /// Launch a thread and load the dependency graph in the background.
94 pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
95 // Since `sess` isn't `Sync`, we perform all accesses to `sess`
96 // before we fire the background thread.
98 let prof = sess.prof.clone();
100 if sess.opts.incremental.is_none() {
101 // No incremental compilation.
102 return MaybeAsync::Sync(LoadResult::Ok { data: Default::default() });
105 let _timer = sess.prof.generic_activity("incr_comp_prepare_load_dep_graph");
107 // Calling `sess.incr_comp_session_dir()` will panic if `sess.opts.incremental.is_none()`.
108 // Fortunately, we just checked that this isn't the case.
109 let path = dep_graph_path_from(&sess.incr_comp_session_dir());
110 let report_incremental_info = sess.opts.debugging_opts.incremental_info;
111 let expected_hash = sess.opts.dep_tracking_hash();
113 let mut prev_work_products = FxHashMap::default();
115 // If we are only building with -Zquery-dep-graph but without an actual
116 // incr. comp. session directory, we skip this. Otherwise we'd fail
117 // when trying to load work products.
118 if sess.incr_comp_session_dir_opt().is_some() {
119 let work_products_path = work_products_path(sess);
120 let load_result = load_data(report_incremental_info, &work_products_path);
122 if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
123 // Decode the list of work_products
124 let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos);
125 let work_products: Vec<SerializedWorkProduct> =
126 RustcDecodable::decode(&mut work_product_decoder).unwrap_or_else(|e| {
128 "Error decoding `work-products` from incremental \
129 compilation session directory: {}",
135 for swp in work_products {
136 let mut all_files_exist = true;
137 for &(_, ref file_name) in swp.work_product.saved_files.iter() {
138 let path = in_incr_comp_dir_sess(sess, file_name);
140 all_files_exist = false;
142 if sess.opts.debugging_opts.incremental_info {
144 "incremental: could not find file for work \
153 debug!("reconcile_work_products: all files for {:?} exist", swp);
154 prev_work_products.insert(swp.id, swp.work_product);
156 debug!("reconcile_work_products: some file for {:?} does not exist", swp);
157 delete_dirty_work_product(sess, swp);
163 MaybeAsync::Async(std::thread::spawn(move || {
164 let _prof_timer = prof.generic_activity("incr_comp_load_dep_graph");
166 match load_data(report_incremental_info, &path) {
167 LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
168 LoadResult::Error { message } => LoadResult::Error { message },
169 LoadResult::Ok { data: (bytes, start_pos) } => {
170 let mut decoder = Decoder::new(&bytes, start_pos);
171 let prev_commandline_args_hash = u64::decode(&mut decoder)
172 .expect("Error reading commandline arg hash from cached dep-graph");
174 if prev_commandline_args_hash != expected_hash {
175 if report_incremental_info {
177 "[incremental] completely ignoring cache because of \
178 differing commandline arguments"
181 // We can't reuse the cache, purge it.
182 debug!("load_dep_graph_new: differing commandline arg hashes");
184 // No need to do any further work
185 return LoadResult::DataOutOfDate;
188 let dep_graph = SerializedDepGraph::decode(&mut decoder)
189 .expect("Error reading cached dep-graph");
191 LoadResult::Ok { data: (PreviousDepGraph::new(dep_graph), prev_work_products) }
197 pub fn load_query_result_cache(sess: &Session) -> OnDiskCache<'_> {
198 if sess.opts.incremental.is_none() || !sess.opts.debugging_opts.incremental_queries {
199 return OnDiskCache::new_empty(sess.source_map());
202 let _prof_timer = sess.prof.generic_activity("incr_comp_load_query_result_cache");
204 match load_data(sess.opts.debugging_opts.incremental_info, &query_cache_path(sess)) {
205 LoadResult::Ok { data: (bytes, start_pos) } => OnDiskCache::new(sess, bytes, start_pos),
206 _ => OnDiskCache::new_empty(sess.source_map()),