Skip to content

Commit 414535b

Browse files
committed
Make will_cache_on_disk_for_key_fn optional in query vtables
1 parent 746dfb9 commit 414535b

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,
@@ -38,7 +40,7 @@ pub struct DynamicQuery<'tcx, C: QueryCache> {
3840
pub query_state: usize,
3941
// Offset of this query's cache field in the QueryCaches struct
4042
pub query_cache: usize,
41-
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
43+
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
4244
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
4345
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
4446
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
@@ -74,8 +74,8 @@ where
7474
}
7575

7676
#[inline(always)]
77-
fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
78-
(self.dynamic.cache_on_disk)(tcx, key)
77+
fn will_cache_on_disk_for_key(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
78+
self.dynamic.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
7979
}
8080

8181
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

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

406406
// Record position of the cache entry.
@@ -449,7 +449,7 @@ where
449449
let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
450450
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
451451
});
452-
if query.cache_on_disk(tcx, &key) {
452+
if query.will_cache_on_disk_for_key(tcx, &key) {
453453
let _ = query.execute_query(tcx, key);
454454
}
455455
}
@@ -651,7 +651,11 @@ macro_rules! define_queries {
651651
cycle_error_handling: cycle_error_handling!([$($modifiers)*]),
652652
query_state: std::mem::offset_of!(QueryStates<'tcx>, $name),
653653
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
654-
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
654+
will_cache_on_disk_for_key_fn: should_ever_cache_on_disk!([$($modifiers)*] {
655+
Some(::rustc_middle::query::cached::$name)
656+
} {
657+
None
658+
}),
655659
execute_query: |tcx, key| erase(tcx.$name(key)),
656660
compute: |tcx, key| {
657661
#[cfg(debug_assertions)]

compiler/rustc_query_system/src/query/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub trait QueryConfig<Qcx: QueryContext>: Copy {
3636
where
3737
Qcx: 'a;
3838

39-
fn cache_on_disk(self, tcx: Qcx::DepContext, key: &Self::Key) -> bool;
39+
fn will_cache_on_disk_for_key(self, tcx: Qcx::DepContext, key: &Self::Key) -> bool;
4040

4141
// Don't use this method to compute query results, instead use the methods on TyCtxt
4242
fn execute_query(self, tcx: Qcx::DepContext, 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
@@ -630,7 +630,7 @@ where
630630
// We always expect to find a cached result for things that
631631
// can be forced from `DepNode`.
632632
debug_assert!(
633-
!query.cache_on_disk(*qcx.dep_context(), key)
633+
!query.will_cache_on_disk_for_key(*qcx.dep_context(), key)
634634
|| !qcx.dep_context().fingerprint_style(dep_node.kind).reconstructible(),
635635
"missing on-disk cache entry for {dep_node:?}"
636636
);

0 commit comments

Comments
 (0)