Skip to content

Commit 0af8e87

Browse files
committed
Auto merge of #59722 - Zoxc:query-cache, r=eddyb
Clean up query cache code There are a couple of queries for which we do not promote saved results, which have been removed from caching here. This was likely the cause of the regression in #57293 (comment). r? @michaelwoerister
2 parents c06f80a + ede41ab commit 0af8e87

File tree

8 files changed

+133
-184
lines changed

8 files changed

+133
-184
lines changed

src/librustc/dep_graph/graph.rs

+13-24
Original file line numberDiff line numberDiff line change
@@ -842,31 +842,20 @@ impl DepGraph {
842842
// This method will only load queries that will end up in the disk cache.
843843
// Other queries will not be executed.
844844
pub fn exec_cache_promotions<'tcx>(&self, tcx: TyCtxt<'tcx>) {
845-
let green_nodes: Vec<DepNode> = {
846-
let data = self.data.as_ref().unwrap();
847-
data.colors.values.indices().filter_map(|prev_index| {
848-
match data.colors.get(prev_index) {
849-
Some(DepNodeColor::Green(_)) => {
850-
let dep_node = data.previous.index_to_node(prev_index);
851-
if dep_node.cache_on_disk(tcx) {
852-
Some(dep_node)
853-
} else {
854-
None
855-
}
856-
}
857-
None |
858-
Some(DepNodeColor::Red) => {
859-
// We can skip red nodes because a node can only be marked
860-
// as red if the query result was recomputed and thus is
861-
// already in memory.
862-
None
863-
}
845+
let data = self.data.as_ref().unwrap();
846+
for prev_index in data.colors.values.indices() {
847+
match data.colors.get(prev_index) {
848+
Some(DepNodeColor::Green(_)) => {
849+
let dep_node = data.previous.index_to_node(prev_index);
850+
dep_node.try_load_from_on_disk_cache(tcx);
864851
}
865-
}).collect()
866-
};
867-
868-
for dep_node in green_nodes {
869-
dep_node.load_from_on_disk_cache(tcx);
852+
None |
853+
Some(DepNodeColor::Red) => {
854+
// We can skip red nodes because a node can only be marked
855+
// as red if the query result was recomputed and thus is
856+
// already in memory.
857+
}
858+
}
870859
}
871860
}
872861

src/librustc/mir/interpret/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use errors::DiagnosticBuilder;
1818
use syntax_pos::{Pos, Span};
1919
use syntax::symbol::Symbol;
2020

21-
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable)]
21+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, RustcEncodable, RustcDecodable)]
2222
pub enum ErrorHandled {
2323
/// Already reported a lint or an error for this evaluation.
2424
Reported,

src/librustc/query/mod.rs

+40-23
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ty::query::QueryDescription;
22
use crate::ty::query::queries;
33
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
44
use crate::ty::subst::SubstsRef;
5-
use crate::dep_graph::SerializedDepNodeIndex;
5+
use crate::dep_graph::{RecoverKey,DepKind, DepNode, SerializedDepNodeIndex};
66
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
77
use crate::mir;
88
use crate::mir::interpret::GlobalId;
@@ -33,13 +33,13 @@ rustc_queries! {
3333
Other {
3434
/// Records the type of every item.
3535
query type_of(key: DefId) -> Ty<'tcx> {
36-
cache { key.is_local() }
36+
cache_on_disk_if { key.is_local() }
3737
}
3838

3939
/// Maps from the `DefId` of an item (trait/struct/enum/fn) to its
4040
/// associated generics.
4141
query generics_of(key: DefId) -> &'tcx ty::Generics {
42-
cache { key.is_local() }
42+
cache_on_disk_if { key.is_local() }
4343
load_cached(tcx, id) {
4444
let generics: Option<ty::Generics> = tcx.queries.on_disk_cache
4545
.try_load_query_result(tcx, id);
@@ -62,7 +62,9 @@ rustc_queries! {
6262
/// predicate gets in the way of some checks, which are intended
6363
/// to operate over only the actual where-clauses written by the
6464
/// user.)
65-
query predicates_of(_: DefId) -> &'tcx ty::GenericPredicates<'tcx> {}
65+
query predicates_of(key: DefId) -> &'tcx ty::GenericPredicates<'tcx> {
66+
cache_on_disk_if { key.is_local() }
67+
}
6668

6769
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLibrary>> {
6870
desc { "looking up the native libraries of a linked crate" }
@@ -93,7 +95,7 @@ rustc_queries! {
9395
/// of the MIR qualify_consts pass. The actual meaning of
9496
/// the value isn't known except to the pass itself.
9597
query mir_const_qualif(key: DefId) -> (u8, &'tcx BitSet<mir::Local>) {
96-
cache { key.is_local() }
98+
cache_on_disk_if { key.is_local() }
9799
}
98100

99101
/// Fetch the MIR for a given `DefId` right after it's built - this includes
@@ -115,7 +117,7 @@ rustc_queries! {
115117
/// MIR after our optimization passes have run. This is MIR that is ready
116118
/// for codegen. This is also the only query that can fetch non-local MIR, at present.
117119
query optimized_mir(key: DefId) -> &'tcx mir::Body<'tcx> {
118-
cache { key.is_local() }
120+
cache_on_disk_if { key.is_local() }
119121
load_cached(tcx, id) {
120122
let mir: Option<crate::mir::Body<'tcx>> = tcx.queries.on_disk_cache
121123
.try_load_query_result(tcx, id);
@@ -285,7 +287,9 @@ rustc_queries! {
285287

286288
TypeChecking {
287289
/// The result of unsafety-checking this `DefId`.
288-
query unsafety_check_result(_: DefId) -> mir::UnsafetyCheckResult {}
290+
query unsafety_check_result(key: DefId) -> mir::UnsafetyCheckResult {
291+
cache_on_disk_if { key.is_local() }
292+
}
289293

290294
/// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error
291295
query unsafe_derive_on_repr_packed(_: DefId) -> () {}
@@ -348,7 +352,7 @@ rustc_queries! {
348352
}
349353

350354
query typeck_tables_of(key: DefId) -> &'tcx ty::TypeckTables<'tcx> {
351-
cache { key.is_local() }
355+
cache_on_disk_if { key.is_local() }
352356
load_cached(tcx, id) {
353357
let typeck_tables: Option<ty::TypeckTables<'tcx>> = tcx
354358
.queries.on_disk_cache
@@ -360,7 +364,9 @@ rustc_queries! {
360364
}
361365

362366
Other {
363-
query used_trait_imports(_: DefId) -> &'tcx DefIdSet {}
367+
query used_trait_imports(key: DefId) -> &'tcx DefIdSet {
368+
cache_on_disk_if { key.is_local() }
369+
}
364370
}
365371

366372
TypeChecking {
@@ -372,11 +378,15 @@ rustc_queries! {
372378
}
373379

374380
BorrowChecking {
375-
query borrowck(_: DefId) -> &'tcx BorrowCheckResult {}
381+
query borrowck(key: DefId) -> &'tcx BorrowCheckResult {
382+
cache_on_disk_if { key.is_local() }
383+
}
376384

377385
/// Borrow-checks the function body. If this is a closure, returns
378386
/// additional requirements that the closure's creator must verify.
379-
query mir_borrowck(_: DefId) -> mir::BorrowCheckResult<'tcx> {}
387+
query mir_borrowck(key: DefId) -> mir::BorrowCheckResult<'tcx> {
388+
cache_on_disk_if(tcx, _) { key.is_local() && tcx.is_closure(key) }
389+
}
380390
}
381391

382392
TypeChecking {
@@ -412,9 +422,10 @@ rustc_queries! {
412422
"const-evaluating `{}`",
413423
tcx.def_path_str(key.value.instance.def.def_id())
414424
}
415-
cache { true }
416-
load_cached(tcx, id) {
417-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
425+
cache_on_disk_if(_, opt_result) {
426+
// Only store results without errors
427+
// FIXME: We never store these
428+
opt_result.map_or(true, |r| r.is_ok())
418429
}
419430
}
420431

@@ -427,9 +438,9 @@ rustc_queries! {
427438
"const-evaluating + checking `{}`",
428439
tcx.def_path_str(key.value.instance.def.def_id())
429440
}
430-
cache { true }
431-
load_cached(tcx, id) {
432-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id).map(Ok)
441+
cache_on_disk_if(_, opt_result) {
442+
// Only store results without errors
443+
opt_result.map_or(true, |r| r.is_ok())
433444
}
434445
}
435446

@@ -453,7 +464,9 @@ rustc_queries! {
453464
}
454465

455466
TypeChecking {
456-
query check_match(_: DefId) -> () {}
467+
query check_match(key: DefId) -> () {
468+
cache_on_disk_if { key.is_local() }
469+
}
457470

458471
/// Performs part of the privacy check and computes "access levels".
459472
query privacy_access_levels(_: CrateNum) -> &'tcx AccessLevels {
@@ -483,7 +496,7 @@ rustc_queries! {
483496
query symbol_name(key: ty::Instance<'tcx>) -> ty::SymbolName {
484497
no_force
485498
desc { "computing the symbol for `{}`", key }
486-
cache { true }
499+
cache_on_disk_if { true }
487500
}
488501

489502
query def_kind(_: DefId) -> Option<DefKind> {}
@@ -501,7 +514,9 @@ rustc_queries! {
501514
}
502515

503516
Codegen {
504-
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {}
517+
query codegen_fn_attrs(_: DefId) -> CodegenFnAttrs {
518+
cache_on_disk_if { true }
519+
}
505520
}
506521

507522
Other {
@@ -519,7 +534,7 @@ rustc_queries! {
519534
"const checking if rvalue is promotable to static `{}`",
520535
tcx.def_path_str(key)
521536
}
522-
cache { true }
537+
cache_on_disk_if { true }
523538
}
524539
query rvalue_promotable_map(key: DefId) -> &'tcx ItemLocalSet {
525540
desc { |tcx|
@@ -548,7 +563,7 @@ rustc_queries! {
548563
key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)
549564
) -> Vtable<'tcx, ()> {
550565
no_force
551-
cache { true }
566+
cache_on_disk_if { true }
552567
desc { |tcx|
553568
"checking if `{}` fulfills its obligations",
554569
tcx.def_path_str(key.1.def_id())
@@ -560,7 +575,9 @@ rustc_queries! {
560575
query trait_impls_of(key: DefId) -> &'tcx ty::trait_def::TraitImpls {
561576
desc { |tcx| "trait impls of `{}`", tcx.def_path_str(key) }
562577
}
563-
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {}
578+
query specialization_graph_of(_: DefId) -> &'tcx specialization_graph::Graph {
579+
cache_on_disk_if { true }
580+
}
564581
query is_object_safe(key: DefId) -> bool {
565582
desc { |tcx| "determine object safety of trait `{}`", tcx.def_path_str(key) }
566583
}

src/librustc/ty/query/config.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
5555
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
5656

5757
#[inline]
58-
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key) -> bool {
58+
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
5959
false
6060
}
6161

@@ -80,33 +80,3 @@ impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
8080
"running analysis passes on this crate".into()
8181
}
8282
}
83-
84-
macro_rules! impl_disk_cacheable_query(
85-
($query_name:ident, |$tcx:tt, $key:tt| $cond:expr) => {
86-
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
87-
#[inline]
88-
fn cache_on_disk($tcx: TyCtxt<'tcx>, $key: Self::Key) -> bool {
89-
$cond
90-
}
91-
92-
#[inline]
93-
fn try_load_from_disk(tcx: TyCtxt<'tcx>,
94-
id: SerializedDepNodeIndex)
95-
-> Option<Self::Value> {
96-
tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
97-
}
98-
}
99-
}
100-
);
101-
102-
impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
103-
def_id.is_local() && tcx.is_closure(def_id)
104-
});
105-
106-
impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local());
107-
impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
108-
impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
109-
impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
110-
impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
111-
impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
112-
impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);

src/librustc/ty/query/on_disk_cache.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,8 @@ impl<'sess> OnDiskCache<'sess> {
221221
encode_query_results::<check_match<'_>, _>(tcx, enc, qri)?;
222222
encode_query_results::<codegen_fn_attrs<'_>, _>(tcx, enc, qri)?;
223223
encode_query_results::<specialization_graph_of<'_>, _>(tcx, enc, qri)?;
224-
225-
// const eval is special, it only encodes successfully evaluated constants
226-
use crate::ty::query::QueryAccessors;
227-
let cache = const_eval::query_cache(tcx).borrow();
228-
assert!(cache.active.is_empty());
229-
for (key, entry) in cache.results.iter() {
230-
use crate::ty::query::config::QueryDescription;
231-
if const_eval::cache_on_disk(tcx, key.clone()) {
232-
if let Ok(ref value) = entry.value {
233-
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
234-
235-
// Record position of the cache entry
236-
qri.push((dep_node, AbsoluteBytePos::new(enc.position())));
237-
238-
// Encode the type check tables with the SerializedDepNodeIndex
239-
// as tag.
240-
enc.encode_tagged(dep_node, value)?;
241-
}
242-
}
243-
}
224+
encode_query_results::<const_eval<'_>, _>(tcx, enc, qri)?;
225+
// FIXME: Include const_eval_raw?
244226

245227
Ok(())
246228
})?;
@@ -1090,7 +1072,7 @@ where
10901072
let map = Q::query_cache(tcx).borrow();
10911073
assert!(map.active.is_empty());
10921074
for (key, entry) in map.results.iter() {
1093-
if Q::cache_on_disk(tcx, key.clone()) {
1075+
if Q::cache_on_disk(tcx, key.clone(), Some(&entry.value)) {
10941076
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
10951077

10961078
// Record position of the cache entry

0 commit comments

Comments
 (0)