Skip to content

Commit 0ea4b47

Browse files
incr.comp.: Make sure we don't lose unused green results from the query cache.
1 parent 5802986 commit 0ea4b47

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/librustc/dep_graph/graph.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,39 @@ impl DepGraph {
659659
}).unwrap_or(false)
660660
}
661661

662+
// This method loads all on-disk cacheable query results into memory, so
663+
// they can be written out to the new cache file again. Most query results
664+
// will already be in memory but in the case where we marked something as
665+
// green but then did not need the value, that value will never have been
666+
// loaded from disk.
667+
//
668+
// This method will only load queries that will end up in the disk cache.
669+
// Other queries will not be executed.
670+
pub fn exec_cache_promotions<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) {
671+
let green_nodes: Vec<DepNode> = {
672+
let data = self.data.as_ref().unwrap();
673+
data.colors.borrow().iter().filter_map(|(dep_node, color)| match color {
674+
DepNodeColor::Green(_) => {
675+
if dep_node.cache_on_disk(tcx) {
676+
Some(*dep_node)
677+
} else {
678+
None
679+
}
680+
}
681+
DepNodeColor::Red => {
682+
// We can skip red nodes because a node can only be marked
683+
// as red if the query result was recomputed and thus is
684+
// already in memory.
685+
None
686+
}
687+
}).collect()
688+
};
689+
690+
for dep_node in green_nodes {
691+
dep_node.load_from_on_disk_cache(tcx);
692+
}
693+
}
694+
662695
pub fn mark_loaded_from_cache(&self, dep_node_index: DepNodeIndex, state: bool) {
663696
debug!("mark_loaded_from_cache({:?}, {})",
664697
self.data.as_ref().unwrap().current.borrow().nodes[dep_node_index],

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ impl<'sess> OnDiskCache<'sess> {
197197

198198
encoder.encode_tagged(PREV_DIAGNOSTICS_TAG, &diagnostics)?;
199199

200+
// Load everything into memory so we can write it out to the on-disk
201+
// cache. The vast majority of cacheable query results should already
202+
// be in memory, so this should be a cheap operation.
203+
tcx.dep_graph.exec_cache_promotions(tcx);
200204

201205
// Encode query results
202206
let mut query_result_index = EncodedQueryResultIndex::new();

src/librustc/ty/maps/plumbing.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,3 +900,58 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
900900

901901
true
902902
}
903+
904+
905+
// FIXME(#45015): Another piece of boilerplate code that could be generated in
906+
// a combined define_dep_nodes!()/define_maps!() macro.
907+
macro_rules! impl_load_from_cache {
908+
($($dep_kind:ident => $query_name:ident,)*) => {
909+
impl DepNode {
910+
// Check whether the query invocation corresponding to the given
911+
// DepNode is eligible for on-disk-caching.
912+
pub fn cache_on_disk(&self, tcx: TyCtxt) -> bool {
913+
use ty::maps::queries;
914+
use ty::maps::QueryDescription;
915+
916+
match self.kind {
917+
$(DepKind::$dep_kind => {
918+
let def_id = self.extract_def_id(tcx).unwrap();
919+
queries::$query_name::cache_on_disk(def_id)
920+
})*
921+
_ => false
922+
}
923+
}
924+
925+
// This is method will execute the query corresponding to the given
926+
// DepNode. It is only expected to work for DepNodes where the
927+
// above `cache_on_disk` methods returns true.
928+
// Also, as a sanity check, it expects that the corresponding query
929+
// invocation has been marked as green already.
930+
pub fn load_from_on_disk_cache(&self, tcx: TyCtxt) {
931+
match self.kind {
932+
$(DepKind::$dep_kind => {
933+
debug_assert!(tcx.dep_graph
934+
.node_color(self)
935+
.map(|c| c.is_green())
936+
.unwrap_or(false));
937+
938+
let def_id = self.extract_def_id(tcx).unwrap();
939+
let _ = tcx.$query_name(def_id);
940+
})*
941+
_ => {
942+
bug!()
943+
}
944+
}
945+
}
946+
}
947+
}
948+
}
949+
950+
impl_load_from_cache!(
951+
TypeckTables => typeck_tables_of,
952+
MirOptimized => optimized_mir,
953+
UnsafetyCheckResult => unsafety_check_result,
954+
BorrowCheck => borrowck,
955+
MirBorrowCheck => mir_borrowck,
956+
MirConstQualif => mir_const_qualif,
957+
);

0 commit comments

Comments
 (0)