From 3afb618592a7ecf9e755d439a644508a39dfe26d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 10 Apr 2026 13:47:27 +1000 Subject: [PATCH] Move the `cache_on_disk` check out of `try_load_from_disk_fn`. It doesn't need to be in there, it can instead be at the single call site. Removing it eliminates one parameter, makes `define_queries!` smaller (which is always good), and also enables the next commit which tidies up profiling. This commit also changes how `value` and `verify` are initialized, because I don't like the current way of doing it after the declaration. --- compiler/rustc_middle/src/query/plumbing.rs | 4 +++- compiler/rustc_query_impl/src/execution.rs | 21 ++++++++++++--------- compiler/rustc_query_impl/src/query_impl.rs | 9 ++------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index b0c6a8a508cc..cdcb58e8b2b9 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -99,9 +99,11 @@ pub struct QueryVTable<'tcx, C: QueryCache> { pub will_cache_on_disk_for_key_fn: fn(key: C::Key) -> bool, + /// Function pointer that tries to load a query value from disk. + /// + /// This should only be called after a successful check of `will_cache_on_disk_for_key_fn`. pub try_load_from_disk_fn: fn( tcx: TyCtxt<'tcx>, - key: C::Key, prev_index: SerializedDepNodeIndex, index: DepNodeIndex, ) -> Option, diff --git a/compiler/rustc_query_impl/src/execution.rs b/compiler/rustc_query_impl/src/execution.rs index 44995f3f9826..5695ce6d90e0 100644 --- a/compiler/rustc_query_impl/src/execution.rs +++ b/compiler/rustc_query_impl/src/execution.rs @@ -477,16 +477,17 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>( debug_assert!(dep_graph_data.is_index_green(prev_index)); // First try to load the result from the on-disk cache. Some things are never cached on disk. - let value; - let verify; - match (query.try_load_from_disk_fn)(tcx, key, prev_index, dep_node_index) { - Some(loaded_value) => { + let try_value = if (query.will_cache_on_disk_for_key_fn)(key) { + (query.try_load_from_disk_fn)(tcx, prev_index, dep_node_index) + } else { + None + }; + let (value, verify) = match try_value { + Some(value) => { if std::intrinsics::unlikely(tcx.sess.opts.unstable_opts.query_dep_graph) { dep_graph_data.mark_debug_loaded_from_disk(*dep_node) } - value = loaded_value; - let prev_fingerprint = dep_graph_data.prev_value_fingerprint_of(prev_index); // If `-Zincremental-verify-ich` is specified, re-hash results from // the cache and make sure that they have the expected fingerprint. @@ -495,17 +496,19 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>( // from disk. Re-hashing results is fairly expensive, so we can't // currently afford to verify every hash. This subset should still // give us some coverage of potential bugs. - verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32) + let verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32) || tcx.sess.opts.unstable_opts.incremental_verify_ich; + + (value, verify) } None => { // We could not load a result from the on-disk cache, so recompute. The dep-graph for // this computation is already in-place, so we can just call the query provider. let prof_timer = tcx.prof.query_provider(); - value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, key)); + let value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, key)); prof_timer.finish_with_query_invocation_id(dep_node_index.into()); - verify = true; + (value, true) } }; diff --git a/compiler/rustc_query_impl/src/query_impl.rs b/compiler/rustc_query_impl/src/query_impl.rs index 101bf2c4e80f..ab84451c1cd2 100644 --- a/compiler/rustc_query_impl/src/query_impl.rs +++ b/compiler/rustc_query_impl/src/query_impl.rs @@ -160,14 +160,9 @@ pub(crate) fn make_query_vtable<'tcx>(incremental: bool) $crate::query_impl::$name::will_cache_on_disk_for_key, #[cfg($cache_on_disk)] - try_load_from_disk_fn: |tcx, key, prev_index, index| { + try_load_from_disk_fn: |tcx, prev_index, index| { use rustc_middle::queries::$name::{ProvidedValue, provided_to_erased}; - // Check the cache-on-disk condition for this key. - if !$crate::query_impl::$name::will_cache_on_disk_for_key(key) { - return None; - } - let loaded_value: ProvidedValue<'tcx> = $crate::plumbing::try_load_from_disk(tcx, prev_index, index)?; @@ -175,7 +170,7 @@ pub(crate) fn make_query_vtable<'tcx>(incremental: bool) Some(provided_to_erased(tcx, loaded_value)) }, #[cfg(not($cache_on_disk))] - try_load_from_disk_fn: |_tcx, _key, _prev_index, _index| None, + try_load_from_disk_fn: |_tcx, _prev_index, _index| None, #[cfg($handle_cycle_error)] handle_cycle_error_fn: |tcx, key, cycle, err| {