Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a5b58ad

Browse files
committedSep 14, 2022
Auto merge of #101307 - jyn514:simplify-storage, r=cjgillot
Simplify caching and storage for queries I highly recommend reviewing commit-by-commit; each individual commit is quite small but it can be hard to see looking at the overall diff that the behavior is the same. Each commit depends on the previous. r? `@cjgillot`
2 parents 88a1922 + 0a9d7db commit a5b58ad

File tree

7 files changed

+107
-153
lines changed

7 files changed

+107
-153
lines changed
 

‎compiler/rustc_macros/src/query.rs

Lines changed: 26 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,11 @@ struct QueryModifiers {
8686
desc: (Option<Ident>, Punctuated<Expr, Token![,]>),
8787

8888
/// Use this type for the in-memory cache.
89-
storage: Option<Type>,
89+
arena_cache: Option<Ident>,
9090

9191
/// Cache the query to disk if the `Block` returns true.
9292
cache: Option<(Option<Pat>, Block)>,
9393

94-
/// Custom code to load the query from disk.
95-
load_cached: Option<(Ident, Ident, Block)>,
96-
9794
/// A cycle error for this query aborting the compilation with a fatal error.
9895
fatal_cycle: Option<Ident>,
9996

@@ -120,8 +117,7 @@ struct QueryModifiers {
120117
}
121118

122119
fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
123-
let mut load_cached = None;
124-
let mut storage = None;
120+
let mut arena_cache = None;
125121
let mut cache = None;
126122
let mut desc = None;
127123
let mut fatal_cycle = None;
@@ -173,21 +169,8 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
173169
};
174170
let block = input.parse()?;
175171
try_insert!(cache = (args, block));
176-
} else if modifier == "load_cached" {
177-
// Parse a load_cached modifier like:
178-
// `load_cached(tcx, id) { tcx.on_disk_cache.try_load_query_result(tcx, id) }`
179-
let args;
180-
parenthesized!(args in input);
181-
let tcx = args.parse()?;
182-
args.parse::<Token![,]>()?;
183-
let id = args.parse()?;
184-
let block = input.parse()?;
185-
try_insert!(load_cached = (tcx, id, block));
186-
} else if modifier == "storage" {
187-
let args;
188-
parenthesized!(args in input);
189-
let ty = args.parse()?;
190-
try_insert!(storage = ty);
172+
} else if modifier == "arena_cache" {
173+
try_insert!(arena_cache = modifier);
191174
} else if modifier == "fatal_cycle" {
192175
try_insert!(fatal_cycle = modifier);
193176
} else if modifier == "cycle_delay_bug" {
@@ -212,8 +195,7 @@ fn parse_query_modifiers(input: ParseStream<'_>) -> Result<QueryModifiers> {
212195
return Err(input.error("no description provided"));
213196
};
214197
Ok(QueryModifiers {
215-
load_cached,
216-
storage,
198+
arena_cache,
217199
cache,
218200
desc,
219201
fatal_cycle,
@@ -262,20 +244,6 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
262244

263245
// Find out if we should cache the query on disk
264246
let cache = if let Some((args, expr)) = modifiers.cache.as_ref() {
265-
let try_load_from_disk = if let Some((tcx, id, block)) = modifiers.load_cached.as_ref() {
266-
// Use custom code to load the query from disk
267-
quote! {
268-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>>
269-
= Some(|#tcx, #id| { #block });
270-
}
271-
} else {
272-
// Use the default code to load the query from disk
273-
quote! {
274-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>>
275-
= Some(|tcx, id| tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id));
276-
}
277-
};
278-
279247
let tcx = args.as_ref().map(|t| quote! { #t }).unwrap_or_else(|| quote! { _ });
280248
// expr is a `Block`, meaning that `{ #expr }` gets expanded
281249
// to `{ { stmts... } }`, which triggers the `unused_braces` lint.
@@ -285,20 +253,13 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
285253
fn cache_on_disk(#tcx: TyCtxt<'tcx>, #key: &Self::Key) -> bool {
286254
#expr
287255
}
288-
289-
#try_load_from_disk
290256
}
291257
} else {
292-
if modifiers.load_cached.is_some() {
293-
panic!("load_cached modifier on query `{}` without a cache modifier", name);
294-
}
295258
quote! {
296259
#[inline]
297260
fn cache_on_disk(_: TyCtxt<'tcx>, _: &Self::Key) -> bool {
298261
false
299262
}
300-
301-
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>> = None;
302263
}
303264
};
304265

@@ -347,42 +308,28 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
347308

348309
let mut attributes = Vec::new();
349310

350-
// Pass on the fatal_cycle modifier
351-
if let Some(fatal_cycle) = &modifiers.fatal_cycle {
352-
attributes.push(quote! { (#fatal_cycle) });
353-
};
354-
// Pass on the storage modifier
355-
if let Some(ref ty) = modifiers.storage {
356-
let span = ty.span();
357-
attributes.push(quote_spanned! {span=> (storage #ty) });
358-
};
359-
// Pass on the cycle_delay_bug modifier
360-
if let Some(cycle_delay_bug) = &modifiers.cycle_delay_bug {
361-
attributes.push(quote! { (#cycle_delay_bug) });
362-
};
363-
// Pass on the no_hash modifier
364-
if let Some(no_hash) = &modifiers.no_hash {
365-
attributes.push(quote! { (#no_hash) });
366-
};
367-
// Pass on the anon modifier
368-
if let Some(anon) = &modifiers.anon {
369-
attributes.push(quote! { (#anon) });
370-
};
371-
// Pass on the eval_always modifier
372-
if let Some(eval_always) = &modifiers.eval_always {
373-
attributes.push(quote! { (#eval_always) });
374-
};
375-
// Pass on the depth_limit modifier
376-
if let Some(depth_limit) = &modifiers.depth_limit {
377-
attributes.push(quote! { (#depth_limit) });
378-
};
379-
// Pass on the separate_provide_extern modifier
380-
if let Some(separate_provide_extern) = &modifiers.separate_provide_extern {
381-
attributes.push(quote! { (#separate_provide_extern) });
311+
macro_rules! passthrough {
312+
( $( $modifier:ident ),+ $(,)? ) => {
313+
$( if let Some($modifier) = &modifiers.$modifier {
314+
attributes.push(quote! { (#$modifier) });
315+
}; )+
316+
}
382317
}
383-
// Pass on the remap_env_constness modifier
384-
if let Some(remap_env_constness) = &modifiers.remap_env_constness {
385-
attributes.push(quote! { (#remap_env_constness) });
318+
319+
passthrough!(
320+
fatal_cycle,
321+
arena_cache,
322+
cycle_delay_bug,
323+
no_hash,
324+
anon,
325+
eval_always,
326+
depth_limit,
327+
separate_provide_extern,
328+
remap_env_constness,
329+
);
330+
331+
if modifiers.cache.is_some() {
332+
attributes.push(quote! { (cache) });
386333
}
387334

388335
// This uses the span of the query definition for the commas,

‎compiler/rustc_middle/src/query/mod.rs

Lines changed: 49 additions & 56 deletions
Large diffs are not rendered by default.

‎compiler/rustc_middle/src/ty/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ macro_rules! query_storage {
121121
([][$K:ty, $V:ty]) => {
122122
<DefaultCacheSelector as CacheSelector<$K, $V>>::Cache
123123
};
124-
([(storage $ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
125-
<$ty as CacheSelector<$K, $V>>::Cache
124+
([(arena_cache) $($rest:tt)*][$K:ty, $V:ty]) => {
125+
<ArenaCacheSelector<'tcx> as CacheSelector<$K, $V>>::Cache
126126
};
127127
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
128128
query_storage!([$($modifiers)*][$($args)*])

‎compiler/rustc_query_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern crate rustc_middle;
1717

1818
use rustc_data_structures::sync::AtomicU64;
1919
use rustc_middle::arena::Arena;
20-
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex};
20+
use rustc_middle::dep_graph::{self, DepKindStruct};
2121
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
2222
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
2323
use rustc_middle::ty::{self, TyCtxt};

‎compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! manage the caches, and so forth.
44
55
use crate::keys::Key;
6+
use crate::on_disk_cache::CacheDecoder;
67
use crate::{on_disk_cache, Queries};
78
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
89
use rustc_data_structures::sync::{AtomicU64, Lock};
@@ -19,6 +20,7 @@ use rustc_query_system::query::{
1920
QuerySideEffects, QueryStackFrame,
2021
};
2122
use rustc_query_system::Value;
23+
use rustc_serialize::Decodable;
2224
use std::any::Any;
2325
use std::num::NonZeroU64;
2426
use thin_vec::ThinVec;
@@ -253,6 +255,18 @@ macro_rules! get_provider {
253255
};
254256
}
255257

258+
macro_rules! should_ever_cache_on_disk {
259+
([]) => {{
260+
None
261+
}};
262+
([(cache) $($rest:tt)*]) => {{
263+
Some($crate::plumbing::try_load_from_disk::<Self::Value>)
264+
}};
265+
([$other:tt $($modifiers:tt)*]) => {
266+
should_ever_cache_on_disk!([$($modifiers)*])
267+
};
268+
}
269+
256270
pub(crate) fn create_query_frame<
257271
'tcx,
258272
K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
@@ -313,6 +327,16 @@ where
313327
}
314328
}
315329

330+
pub(crate) fn try_load_from_disk<'tcx, V>(
331+
tcx: QueryCtxt<'tcx>,
332+
id: SerializedDepNodeIndex,
333+
) -> Option<V>
334+
where
335+
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
336+
{
337+
tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
338+
}
339+
316340
fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
317341
where
318342
Q: QueryDescription<QueryCtxt<'tcx>>,
@@ -418,8 +442,7 @@ macro_rules! define_queries {
418442
hash_result: hash_result!([$($modifiers)*]),
419443
handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
420444
compute,
421-
cache_on_disk,
422-
try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
445+
try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None },
423446
}
424447
}
425448

‎compiler/rustc_query_system/src/query/config.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ pub struct QueryVTable<CTX: QueryContext, K, V> {
2525
pub dep_kind: CTX::DepKind,
2626
pub eval_always: bool,
2727
pub depth_limit: bool,
28-
pub cache_on_disk: bool,
2928

3029
pub compute: fn(CTX::DepContext, K) -> V,
3130
pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
3231
pub handle_cycle_error: HandleCycleError,
32+
// NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
3333
pub try_load_from_disk: Option<fn(CTX, SerializedDepNodeIndex) -> Option<V>>,
3434
}
3535

@@ -44,18 +44,9 @@ impl<CTX: QueryContext, K, V> QueryVTable<CTX, K, V> {
4444
pub(crate) fn compute(&self, tcx: CTX::DepContext, key: K) -> V {
4545
(self.compute)(tcx, key)
4646
}
47-
48-
pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
49-
self.try_load_from_disk
50-
.expect("QueryDescription::load_from_disk() called for an unsupported query.")(
51-
tcx, index,
52-
)
53-
}
5447
}
5548

5649
pub trait QueryDescription<CTX: QueryContext>: QueryConfig {
57-
const TRY_LOAD_FROM_DISK: Option<fn(CTX, SerializedDepNodeIndex) -> Option<Self::Value>>;
58-
5950
type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
6051

6152
fn describe(tcx: CTX, key: Self::Key) -> String;

‎compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,14 @@ where
488488

489489
// First we try to load the result from the on-disk cache.
490490
// Some things are never cached on disk.
491-
if query.cache_on_disk {
491+
if let Some(try_load_from_disk) = query.try_load_from_disk {
492492
let prof_timer = tcx.dep_context().profiler().incr_cache_loading();
493493

494494
// The call to `with_query_deserialization` enforces that no new `DepNodes`
495495
// are created during deserialization. See the docs of that method for more
496496
// details.
497-
let result = dep_graph
498-
.with_query_deserialization(|| query.try_load_from_disk(tcx, prev_dep_node_index));
497+
let result =
498+
dep_graph.with_query_deserialization(|| try_load_from_disk(tcx, prev_dep_node_index));
499499

500500
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
501501

0 commit comments

Comments
 (0)
Please sign in to comment.