Skip to content

Commit e8659d7

Browse files
Rollup merge of #154591 - zetanumbers:refactor_query_fns_and_traits, r=cjgillot
Remove `will_cache_on_disk_for_key_fn` Refactors `QueryVtables` and removes `will_cache_on_disk_for_key_fn` as unnecessary indirection. Based and blocked on #154576. Expecting perf improvements.
2 parents 6eda741 + 9d82fce commit e8659d7

6 files changed

Lines changed: 32 additions & 49 deletions

File tree

compiler/rustc_middle/src/query/keys.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub trait QueryKey: Sized + QueryKeyBounds {
3636
/// [`QueryCache`]: rustc_middle::query::QueryCache
3737
type Cache<V> = DefaultCache<Self, V>;
3838

39+
type LocalQueryKey = !;
40+
3941
/// In the event that a cycle occurs, if no explicit span has been
4042
/// given for a query with key `self`, what span should we use?
4143
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
@@ -45,14 +47,12 @@ pub trait QueryKey: Sized + QueryKeyBounds {
4547
fn key_as_def_id(&self) -> Option<DefId> {
4648
None
4749
}
48-
}
49-
50-
pub trait AsLocalQueryKey: QueryKey {
51-
type LocalQueryKey;
5250

5351
/// Given an instance of this key, what crate is it referring to?
5452
/// This is used to find the provider.
55-
fn as_local_key(&self) -> Option<Self::LocalQueryKey>;
53+
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
54+
None
55+
}
5656
}
5757

5858
impl QueryKey for () {
@@ -96,13 +96,11 @@ impl<'tcx> QueryKey for ty::LitToConstInput<'tcx> {
9696
impl QueryKey for CrateNum {
9797
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
9898

99+
type LocalQueryKey = LocalCrate;
100+
99101
fn default_span(&self, _: TyCtxt<'_>) -> Span {
100102
DUMMY_SP
101103
}
102-
}
103-
104-
impl AsLocalQueryKey for CrateNum {
105-
type LocalQueryKey = LocalCrate;
106104

107105
#[inline(always)]
108106
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -136,6 +134,7 @@ impl QueryKey for LocalDefId {
136134

137135
impl QueryKey for DefId {
138136
type Cache<V> = DefIdCache<V>;
137+
type LocalQueryKey = LocalDefId;
139138

140139
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141140
tcx.def_span(*self)
@@ -145,10 +144,6 @@ impl QueryKey for DefId {
145144
fn key_as_def_id(&self) -> Option<DefId> {
146145
Some(*self)
147146
}
148-
}
149-
150-
impl AsLocalQueryKey for DefId {
151-
type LocalQueryKey = LocalDefId;
152147

153148
#[inline(always)]
154149
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -197,13 +192,11 @@ impl QueryKey for (LocalDefId, LocalDefId, Ident) {
197192
}
198193

199194
impl QueryKey for (CrateNum, DefId) {
195+
type LocalQueryKey = DefId;
196+
200197
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
201198
self.1.default_span(tcx)
202199
}
203-
}
204-
205-
impl AsLocalQueryKey for (CrateNum, DefId) {
206-
type LocalQueryKey = DefId;
207200

208201
#[inline(always)]
209202
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -212,13 +205,11 @@ impl AsLocalQueryKey for (CrateNum, DefId) {
212205
}
213206

214207
impl QueryKey for (CrateNum, SimplifiedType) {
208+
type LocalQueryKey = SimplifiedType;
209+
215210
fn default_span(&self, _: TyCtxt<'_>) -> Span {
216211
DUMMY_SP
217212
}
218-
}
219-
220-
impl AsLocalQueryKey for (CrateNum, SimplifiedType) {
221-
type LocalQueryKey = SimplifiedType;
222213

223214
#[inline(always)]
224215
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::LocalDefId;
33
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
44
pub use self::into_query_key::IntoQueryKey;
55
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
6-
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
6+
pub use self::keys::{LocalCrate, QueryKey};
77
pub use self::plumbing::{
88
ActiveKeyStatus, Cycle, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable, TyCtxtAt,
99
TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::dep_graph::{DepKind, DepNodeIndex, QuerySideEffect, SerializedDepNode
1414
use crate::ich::StableHashState;
1515
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
1616
use crate::query::on_disk_cache::OnDiskCache;
17-
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryStackFrame};
17+
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryKey, QueryStackFrame};
1818
use crate::ty::{self, TyCtxt};
1919

2020
/// For a particular query, keeps track of "active" keys, i.e. keys whose
@@ -86,6 +86,9 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
8686
/// True if this query has the `feedable` modifier.
8787
pub feedable: bool,
8888

89+
pub cache_on_disk_local: bool,
90+
pub separate_provide_extern: bool,
91+
8992
pub dep_kind: DepKind,
9093
pub state: QueryState<'tcx, C::Key>,
9194
pub cache: C,
@@ -97,11 +100,9 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
97100
/// This should be the only code that calls the provider function.
98101
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
99102

100-
pub will_cache_on_disk_for_key_fn: fn(key: C::Key) -> bool,
101-
102103
/// Function pointer that tries to load a query value from disk.
103104
///
104-
/// This should only be called after a successful check of `will_cache_on_disk_for_key_fn`.
105+
/// This should only be called after a successful check of [`Self::will_cache_on_disk_for_key`].
105106
pub try_load_from_disk_fn:
106107
fn(tcx: TyCtxt<'tcx>, prev_index: SerializedDepNodeIndex) -> Option<C::Value>,
107108

@@ -133,6 +134,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
133134
pub execute_query_fn: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
134135
}
135136

137+
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
138+
pub fn will_cache_on_disk_for_key(&self, key: C::Key) -> bool {
139+
self.cache_on_disk_local && (!self.separate_provide_extern || key.as_local_key().is_some())
140+
}
141+
}
142+
136143
impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
137144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138145
// When debug-printing a query vtable (e.g. for ICE or tracing),
@@ -328,7 +335,7 @@ macro_rules! define_callbacks {
328335
/// This query has the `separate_provide_extern` modifier.
329336
#[cfg($separate_provide_extern)]
330337
pub type LocalKey<'tcx> =
331-
<Key<'tcx> as $crate::query::AsLocalQueryKey>::LocalQueryKey;
338+
<Key<'tcx> as $crate::query::QueryKey>::LocalQueryKey;
332339
/// Key type used by provider functions in `local_providers`.
333340
#[cfg(not($separate_provide_extern))]
334341
pub type LocalKey<'tcx> = Key<'tcx>;

compiler/rustc_query_impl/src/execution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
498498
debug_assert!(dep_graph_data.is_index_green(prev_index));
499499

500500
// First try to load the result from the on-disk cache. Some things are never cached on disk.
501-
let try_value = if (query.will_cache_on_disk_for_key_fn)(key) {
501+
let try_value = if query.will_cache_on_disk_for_key(key) {
502502
let prof_timer = tcx.prof.incr_cache_loading();
503503
let value = (query.try_load_from_disk_fn)(tcx, prev_index);
504504
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
@@ -603,7 +603,7 @@ fn ensure_can_skip_execution<'tcx, C: QueryCache>(
603603
// needed, which guarantees the query provider will never run
604604
// for this key.
605605
EnsureMode::Done => {
606-
(query.will_cache_on_disk_for_key_fn)(key)
606+
query.will_cache_on_disk_for_key(key)
607607
&& loadable_from_disk(tcx, serialized_dep_node_index)
608608
}
609609
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn encode_query_values_inner<'a, 'tcx, C, V>(
9393

9494
assert!(all_inactive(&query.state));
9595
query.cache.for_each(&mut |key, value, dep_node| {
96-
if (query.will_cache_on_disk_for_key_fn)(*key) {
96+
if query.will_cache_on_disk_for_key(*key) {
9797
encoder.encode_query_value::<V>(dep_node, &erase::restore_val::<V>(*value));
9898
}
9999
});
@@ -152,7 +152,7 @@ pub(crate) fn promote_from_disk_inner<'tcx, C: QueryCache>(
152152

153153
// If the recovered key isn't eligible for cache-on-disk, then there's no
154154
// value on disk to promote.
155-
if !(query.will_cache_on_disk_for_key_fn)(key) {
155+
if !query.will_cache_on_disk_for_key(key) {
156156
return;
157157
}
158158

compiler/rustc_query_impl/src/query_impl.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::queries::TaggedQueryKey;
22
use rustc_middle::query::erase::{self, Erased};
3-
use rustc_middle::query::{AsLocalQueryKey, QueryMode, QueryVTable};
3+
use rustc_middle::query::{QueryKey, QueryMode, QueryVTable};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66

@@ -127,20 +127,6 @@ macro_rules! define_queries {
127127
}
128128
}
129129

130-
fn will_cache_on_disk_for_key<'tcx>(
131-
_key: rustc_middle::queries::$name::Key<'tcx>,
132-
) -> bool {
133-
cfg_select! {
134-
// If a query has both `cache_on_disk` and `separate_provide_extern`, only
135-
// disk-cache values for "local" keys, i.e. things in the current crate.
136-
all($cache_on_disk, $separate_provide_extern) => {
137-
AsLocalQueryKey::as_local_key(&_key).is_some()
138-
}
139-
all($cache_on_disk, not($separate_provide_extern)) => true,
140-
not($cache_on_disk) => false,
141-
}
142-
}
143-
144130
pub(crate) fn make_query_vtable<'tcx>(incremental: bool)
145131
-> QueryVTable<'tcx, rustc_middle::queries::$name::Cache<'tcx>>
146132
{
@@ -153,12 +139,11 @@ macro_rules! define_queries {
153139
dep_kind: rustc_middle::dep_graph::DepKind::$name,
154140
state: Default::default(),
155141
cache: Default::default(),
142+
cache_on_disk_local: $cache_on_disk,
143+
separate_provide_extern: $separate_provide_extern,
156144

157145
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
158146

159-
will_cache_on_disk_for_key_fn:
160-
$crate::query_impl::$name::will_cache_on_disk_for_key,
161-
162147
#[cfg($cache_on_disk)]
163148
try_load_from_disk_fn: |tcx, prev_index| {
164149
use rustc_middle::queries::$name::{ProvidedValue, provided_to_erased};

0 commit comments

Comments
 (0)