From c531d9f733e504697309ba6b85ff18ec276da162 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 28 Nov 2017 17:32:28 +0100 Subject: [PATCH] incr.comp.: Allow for recovering from missing on-disk cache entries. --- src/librustc/ty/maps/config.rs | 20 +++++++++++--------- src/librustc/ty/maps/on_disk_cache.rs | 23 ++++++++++------------- src/librustc/ty/maps/plumbing.rs | 25 ++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/librustc/ty/maps/config.rs b/src/librustc/ty/maps/config.rs index 066b80cefa4b5..ffddde557da12 100644 --- a/src/librustc/ty/maps/config.rs +++ b/src/librustc/ty/maps/config.rs @@ -31,9 +31,9 @@ pub(super) trait QueryDescription<'tcx>: QueryConfig { false } - fn load_from_disk<'a>(_: TyCtxt<'a, 'tcx, 'tcx>, - _: SerializedDepNodeIndex) - -> Self::Value { + fn try_load_from_disk<'a>(_: TyCtxt<'a, 'tcx, 'tcx>, + _: SerializedDepNodeIndex) + -> Option { bug!("QueryDescription::load_from_disk() called for unsupport query.") } } @@ -556,12 +556,14 @@ impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> { def_id.is_local() } - fn load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - id: SerializedDepNodeIndex) - -> Self::Value { - let typeck_tables: ty::TypeckTables<'tcx> = tcx.on_disk_query_result_cache - .load_query_result(tcx, id); - tcx.alloc_tables(typeck_tables) + fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, + id: SerializedDepNodeIndex) + -> Option { + let typeck_tables: Option> = tcx + .on_disk_query_result_cache + .try_load_query_result(tcx, id); + + typeck_tables.map(|tables| tcx.alloc_tables(tables)) } } diff --git a/src/librustc/ty/maps/on_disk_cache.rs b/src/librustc/ty/maps/on_disk_cache.rs index 0d09692bc3530..ca0c3bafea8df 100644 --- a/src/librustc/ty/maps/on_disk_cache.rs +++ b/src/librustc/ty/maps/on_disk_cache.rs @@ -289,21 +289,18 @@ impl<'sess> OnDiskCache<'sess> { debug_assert!(prev.is_none()); } - pub fn load_query_result<'a, 'tcx, T>(&self, - tcx: TyCtxt<'a, 'tcx, 'tcx>, - dep_node_index: SerializedDepNodeIndex) - -> T + /// Returns the cached query result if there is something in the cache for + /// the given SerializedDepNodeIndex. Otherwise returns None. + pub fn try_load_query_result<'a, 'tcx, T>(&self, + tcx: TyCtxt<'a, 'tcx, 'tcx>, + dep_node_index: SerializedDepNodeIndex) + -> Option where T: Decodable { - let result = self.load_indexed(tcx, - dep_node_index, - &self.query_result_index, - "query result"); - if let Some(result) = result { - result - } else { - bug!("Could not find query result for key {:?}", dep_node_index) - } + self.load_indexed(tcx, + dep_node_index, + &self.query_result_index, + "query result") } /// Store a diagnostic emitted during computation of an anonymous query. diff --git a/src/librustc/ty/maps/plumbing.rs b/src/librustc/ty/maps/plumbing.rs index 3121cceeea03e..fdaa13e7fd16f 100644 --- a/src/librustc/ty/maps/plumbing.rs +++ b/src/librustc/ty/maps/plumbing.rs @@ -392,12 +392,31 @@ macro_rules! define_maps { { debug_assert!(tcx.dep_graph.is_green(dep_node_index)); - let result = if tcx.sess.opts.debugging_opts.incremental_queries && - Self::cache_on_disk(key) { + // First we try to load the result from the on-disk cache + let result = if Self::cache_on_disk(key) && + tcx.sess.opts.debugging_opts.incremental_queries { let prev_dep_node_index = tcx.dep_graph.prev_dep_node_index_of(dep_node); - Self::load_from_disk(tcx.global_tcx(), prev_dep_node_index) + let result = Self::try_load_from_disk(tcx.global_tcx(), + prev_dep_node_index); + + // We always expect to find a cached result for things that + // can be forced from DepNode. + debug_assert!(!dep_node.kind.can_reconstruct_query_key() || + result.is_some(), + "Missing on-disk cache entry for {:?}", + dep_node); + result + } else { + // Some things are never cached on disk. + None + }; + + let result = if let Some(result) = result { + result } else { + // We could not load a result from the on-disk cache, so + // recompute. let (result, _ ) = tcx.cycle_check(span, Query::$name(key), || { // The diagnostics for this query have already been // promoted to the current session during