Skip to content

Commit cc2d6f8

Browse files
Rollup merge of #151736 - Zalathar:can-load-from-disk, r=nnethercote
Make some load-from-disk function pointers optional in query vtables For queries that never incremental-cache to disk, we can represent that fact with None (i.e. a null function pointer) in their vtables, and avoid having to generate stub functions that do nothing. (There is no decrease in vtable size; we just go from 3/8 padding bytes to 4/8 padding bytes.) There should be no change to compiler output.
2 parents 7d8ebe3 + e044220 commit cc2d6f8

6 files changed

Lines changed: 59 additions & 70 deletions

File tree

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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ use crate::query::{
1818
};
1919
use crate::ty::TyCtxt;
2020

21+
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
22+
23+
pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
24+
tcx: TyCtxt<'tcx>,
25+
key: &Key,
26+
prev_index: SerializedDepNodeIndex,
27+
index: DepNodeIndex,
28+
) -> Option<Value>;
29+
30+
pub type IsLoadableFromDiskFn<'tcx, Key> =
31+
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
32+
2133
/// Stores function pointers and other metadata for a particular query.
2234
///
2335
/// Used indirectly by query plumbing in `rustc_query_system`, via a trait.
@@ -31,18 +43,11 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
3143
pub query_state: usize,
3244
// Offset of this query's cache field in the QueryCaches struct
3345
pub query_cache: usize,
34-
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>>,
3547
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
3648
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
37-
pub can_load_from_disk: bool,
38-
pub try_load_from_disk: fn(
39-
tcx: TyCtxt<'tcx>,
40-
key: &C::Key,
41-
prev_index: SerializedDepNodeIndex,
42-
index: DepNodeIndex,
43-
) -> Option<C::Value>,
44-
pub loadable_from_disk:
45-
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
49+
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
50+
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
4651
pub hash_result: HashResult<C::Value>,
4752
pub value_from_cycle_error:
4853
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,

compiler/rustc_query_impl/src/lib.rs

Lines changed: 6 additions & 9 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)]
@@ -128,21 +128,18 @@ where
128128
prev_index: SerializedDepNodeIndex,
129129
index: DepNodeIndex,
130130
) -> Option<Self::Value> {
131-
if self.vtable.can_load_from_disk {
132-
(self.vtable.try_load_from_disk)(qcx.tcx, key, prev_index, index)
133-
} else {
134-
None
135-
}
131+
// `?` will return None immediately for queries that never cache to disk.
132+
self.vtable.try_load_from_disk_fn?(qcx.tcx, key, prev_index, index)
136133
}
137134

138135
#[inline]
139-
fn loadable_from_disk(
136+
fn is_loadable_from_disk(
140137
self,
141138
qcx: QueryCtxt<'tcx>,
142139
key: &Self::Key,
143140
index: SerializedDepNodeIndex,
144141
) -> bool {
145-
(self.vtable.loadable_from_disk)(qcx.tcx, key, index)
142+
self.vtable.is_loadable_from_disk_fn.map_or(false, |f| f(qcx.tcx, key, index))
146143
}
147144

148145
fn value_from_cycle_error(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 28 additions & 27 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)]
@@ -666,37 +670,34 @@ macro_rules! define_queries {
666670
)
667671
)
668672
},
669-
can_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] true false),
670-
try_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] {
671-
|tcx, key, prev_index, index| {
672-
if ::rustc_middle::query::cached::$name(tcx, key) {
673-
let value = $crate::plumbing::try_load_from_disk::<
674-
queries::$name::ProvidedValue<'tcx>
675-
>(
676-
tcx,
677-
prev_index,
678-
index,
679-
);
680-
value.map(|value| queries::$name::provided_to_erased(tcx, value))
681-
} else {
682-
None
673+
try_load_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
674+
Some(|tcx, key, prev_index, index| {
675+
// Check the `cache_on_disk_if` condition for this key.
676+
if !::rustc_middle::query::cached::$name(tcx, key) {
677+
return None;
683678
}
684-
}
679+
680+
let value: queries::$name::ProvidedValue<'tcx> =
681+
$crate::plumbing::try_load_from_disk(tcx, prev_index, index)?;
682+
683+
// Arena-alloc the value if appropriate, and erase it.
684+
Some(queries::$name::provided_to_erased(tcx, value))
685+
})
685686
} {
686-
|_tcx, _key, _prev_index, _index| None
687+
None
688+
}),
689+
is_loadable_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
690+
Some(|tcx, key, index| -> bool {
691+
::rustc_middle::query::cached::$name(tcx, key) &&
692+
$crate::plumbing::loadable_from_disk(tcx, index)
693+
})
694+
} {
695+
None
687696
}),
688697
value_from_cycle_error: |tcx, cycle, guar| {
689698
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
690699
erase::erase_val(result)
691700
},
692-
loadable_from_disk: |_tcx, _key, _index| {
693-
should_ever_cache_on_disk!([$($modifiers)*] {
694-
::rustc_middle::query::cached::$name(_tcx, _key) &&
695-
$crate::plumbing::loadable_from_disk(_tcx, _index)
696-
} {
697-
false
698-
})
699-
},
700701
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
701702
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
702703
}

compiler/rustc_query_system/src/query/dispatcher.rs

Lines changed: 2 additions & 2 deletions
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;
@@ -61,7 +61,7 @@ pub trait QueryDispatcher<'tcx>: Copy {
6161
index: DepNodeIndex,
6262
) -> Option<Self::Value>;
6363

64-
fn loadable_from_disk(
64+
fn is_loadable_from_disk(
6565
self,
6666
qcx: Self::Qcx,
6767
key: &Self::Key,

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,15 @@ 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
);
630630

631631
// Sanity check for the logic in `ensure`: if the node is green and the result loadable,
632632
// we should actually be able to load it.
633633
debug_assert!(
634-
!query.loadable_from_disk(qcx, key, prev_dep_node_index),
634+
!query.is_loadable_from_disk(qcx, key, prev_dep_node_index),
635635
"missing on-disk cache entry for loadable {dep_node:?}"
636636
);
637637

@@ -798,7 +798,7 @@ where
798798
return (false, None);
799799
}
800800

801-
let loadable = query.loadable_from_disk(qcx, key, serialized_dep_node_index);
801+
let loadable = query.is_loadable_from_disk(qcx, key, serialized_dep_node_index);
802802
(!loadable, Some(dep_node))
803803
}
804804

0 commit comments

Comments
 (0)