Skip to content

Commit e044220

Browse files
committed
Make will_cache_on_disk_for_key_fn optional in query vtables
1 parent d39609b commit e044220

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

compiler/rustc_macros/src/query.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,31 +280,21 @@ fn add_query_desc_cached_impl(
280280
let crate::query::Providers { #name: _, .. };
281281
};
282282

283-
// Find out if we should cache the query on disk
284-
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
283+
// Generate a function to check whether we should cache the query to disk, for some key.
284+
if let Some((args, expr)) = modifiers.cache.as_ref() {
285285
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
286286
// expr is a `Block`, meaning that `{ #expr }` gets expanded
287287
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
288288
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
289-
quote! {
289+
cached.extend(quote! {
290290
#[allow(unused_variables, unused_braces, rustc::pass_by_value)]
291291
#[inline]
292292
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::query::queries::#name::Key<'tcx>) -> bool {
293293
#ra_hint
294294
#expr
295295
}
296-
}
297-
} else {
298-
quote! {
299-
// we're taking `key` by reference, but some rustc types usually prefer being passed by value
300-
#[allow(rustc::pass_by_value)]
301-
#[inline]
302-
pub fn #name<'tcx>(_: TyCtxt<'tcx>, _: &crate::query::queries::#name::Key<'tcx>) -> bool {
303-
#ra_hint
304-
false
305-
}
306-
}
307-
};
296+
});
297+
}
308298

309299
let (tcx, desc) = &modifiers.desc;
310300
let tcx = tcx.as_ref().map_or_else(|| quote! { _ }, |t| quote! { #t });
@@ -322,10 +312,6 @@ fn add_query_desc_cached_impl(
322312
descs.extend(quote! {
323313
#desc
324314
});
325-
326-
cached.extend(quote! {
327-
#cache
328-
});
329315
}
330316

331317
pub(super) fn rustc_queries(input: TokenStream) -> TokenStream {

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use crate::query::{
1818
};
1919
use crate::ty::TyCtxt;
2020

21+
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
22+
2123
pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
2224
tcx: TyCtxt<'tcx>,
2325
key: &Key,
@@ -41,7 +43,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
4143
pub query_state: usize,
4244
// Offset of this query's cache field in the QueryCaches struct
4345
pub query_cache: usize,
44-
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
46+
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
4547
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
4648
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
4749
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,

compiler/rustc_query_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ where
8181
}
8282

8383
#[inline(always)]
84-
fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
85-
(self.vtable.cache_on_disk)(tcx, key)
84+
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
85+
self.vtable.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
8686
}
8787

8888
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
396396
assert!(query.query_state(qcx).all_inactive());
397397
let cache = query.query_cache(qcx);
398398
cache.iter(&mut |key, value, dep_node| {
399-
if query.cache_on_disk(qcx.tcx, key) {
399+
if query.will_cache_on_disk_for_key(qcx.tcx, key) {
400400
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
401401

402402
// Record position of the cache entry.
@@ -445,7 +445,7 @@ where
445445
let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
446446
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
447447
});
448-
if query.cache_on_disk(tcx, &key) {
448+
if query.will_cache_on_disk_for_key(tcx, &key) {
449449
let _ = query.execute_query(tcx, key);
450450
}
451451
}
@@ -648,7 +648,11 @@ macro_rules! define_queries {
648648
cycle_error_handling: cycle_error_handling!([$($modifiers)*]),
649649
query_state: std::mem::offset_of!(QueryStates<'tcx>, $name),
650650
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
651-
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
651+
will_cache_on_disk_for_key_fn: should_ever_cache_on_disk!([$($modifiers)*] {
652+
Some(::rustc_middle::query::cached::$name)
653+
} {
654+
None
655+
}),
652656
execute_query: |tcx, key| erase::erase_val(tcx.$name(key)),
653657
compute: |tcx, key| {
654658
#[cfg(debug_assertions)]

compiler/rustc_query_system/src/query/dispatcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub trait QueryDispatcher<'tcx>: Copy {
4646
// Don't use this method to access query results, instead use the methods on TyCtxt
4747
fn query_cache<'a>(self, tcx: Self::Qcx) -> &'a Self::Cache;
4848

49-
fn cache_on_disk(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> bool;
49+
fn will_cache_on_disk_for_key(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> bool;
5050

5151
// Don't use this method to compute query results, instead use the methods on TyCtxt
5252
fn execute_query(self, tcx: DepContextOf<'tcx, Self>, k: Self::Key) -> Self::Value;

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ where
623623
// We always expect to find a cached result for things that
624624
// can be forced from `DepNode`.
625625
debug_assert!(
626-
!query.cache_on_disk(*qcx.dep_context(), key)
626+
!query.will_cache_on_disk_for_key(*qcx.dep_context(), key)
627627
|| !qcx.dep_context().fingerprint_style(dep_node.kind).reconstructible(),
628628
"missing on-disk cache entry for {dep_node:?}"
629629
);

0 commit comments

Comments
 (0)