Skip to content

Commit db12346

Browse files
authored
Unrolled build for #155080
Rollup merge of #155080 - nnethercote:salvage, r=petrochenkov Simplify `try_load_from_disk_fn`. `try_load_from_disk_fn` has a single call site. We can move some of the stuff within it to its single call site, which simplifies it, and also results in all of the query profiling code ending up in the same module. Details in individual commits. r? @Zalathar
2 parents 8317fef + 4fb83f6 commit db12346

4 files changed

Lines changed: 25 additions & 34 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
9999

100100
pub will_cache_on_disk_for_key_fn: fn(key: C::Key) -> bool,
101101

102-
pub try_load_from_disk_fn: fn(
103-
tcx: TyCtxt<'tcx>,
104-
key: C::Key,
105-
prev_index: SerializedDepNodeIndex,
106-
index: DepNodeIndex,
107-
) -> Option<C::Value>,
102+
/// Function pointer that tries to load a query value from disk.
103+
///
104+
/// This should only be called after a successful check of `will_cache_on_disk_for_key_fn`.
105+
pub try_load_from_disk_fn:
106+
fn(tcx: TyCtxt<'tcx>, prev_index: SerializedDepNodeIndex) -> Option<C::Value>,
108107

109108
/// Function pointer that hashes this query's result values.
110109
///

compiler/rustc_query_impl/src/execution.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,16 +477,20 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
477477
debug_assert!(dep_graph_data.is_index_green(prev_index));
478478

479479
// First try to load the result from the on-disk cache. Some things are never cached on disk.
480-
let value;
481-
let verify;
482-
match (query.try_load_from_disk_fn)(tcx, key, prev_index, dep_node_index) {
483-
Some(loaded_value) => {
480+
let try_value = if (query.will_cache_on_disk_for_key_fn)(key) {
481+
let prof_timer = tcx.prof.incr_cache_loading();
482+
let value = (query.try_load_from_disk_fn)(tcx, prev_index);
483+
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
484+
value
485+
} else {
486+
None
487+
};
488+
let (value, verify) = match try_value {
489+
Some(value) => {
484490
if std::intrinsics::unlikely(tcx.sess.opts.unstable_opts.query_dep_graph) {
485491
dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
486492
}
487493

488-
value = loaded_value;
489-
490494
let prev_fingerprint = dep_graph_data.prev_value_fingerprint_of(prev_index);
491495
// If `-Zincremental-verify-ich` is specified, re-hash results from
492496
// the cache and make sure that they have the expected fingerprint.
@@ -495,17 +499,19 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
495499
// from disk. Re-hashing results is fairly expensive, so we can't
496500
// currently afford to verify every hash. This subset should still
497501
// give us some coverage of potential bugs.
498-
verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32)
502+
let verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32)
499503
|| tcx.sess.opts.unstable_opts.incremental_verify_ich;
504+
505+
(value, verify)
500506
}
501507
None => {
502508
// We could not load a result from the on-disk cache, so recompute. The dep-graph for
503509
// this computation is already in-place, so we can just call the query provider.
504510
let prof_timer = tcx.prof.query_provider();
505-
value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, key));
511+
let value = tcx.dep_graph.with_ignore(|| (query.invoke_provider_fn)(tcx, key));
506512
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
507513

508-
verify = true;
514+
(value, true)
509515
}
510516
};
511517

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::limit::Limit;
55
use rustc_middle::bug;
66
#[expect(unused_imports, reason = "used by doc comments")]
77
use rustc_middle::dep_graph::DepKindVTable;
8-
use rustc_middle::dep_graph::{DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex};
8+
use rustc_middle::dep_graph::{DepNode, DepNodeKey, SerializedDepNodeIndex};
99
use rustc_middle::query::erase::{Erasable, Erased};
1010
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder};
1111
use rustc_middle::query::{QueryCache, QueryJobId, QueryMode, QueryVTable, erase};
@@ -184,23 +184,14 @@ pub(crate) fn loadable_from_disk<'tcx>(tcx: TyCtxt<'tcx>, id: SerializedDepNodeI
184184
pub(crate) fn try_load_from_disk<'tcx, V>(
185185
tcx: TyCtxt<'tcx>,
186186
prev_index: SerializedDepNodeIndex,
187-
index: DepNodeIndex,
188187
) -> Option<V>
189188
where
190189
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
191190
{
192191
let on_disk_cache = tcx.query_system.on_disk_cache.as_ref()?;
193192

194-
let prof_timer = tcx.prof.incr_cache_loading();
195-
196193
// The call to `with_query_deserialization` enforces that no new `DepNodes`
197194
// are created during deserialization. See the docs of that method for more
198195
// details.
199-
let value = tcx
200-
.dep_graph
201-
.with_query_deserialization(|| on_disk_cache.try_load_query_value(tcx, prev_index));
202-
203-
prof_timer.finish_with_query_invocation_id(index.into());
204-
205-
value
196+
tcx.dep_graph.with_query_deserialization(|| on_disk_cache.try_load_query_value(tcx, prev_index))
206197
}

compiler/rustc_query_impl/src/query_impl.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,17 @@ macro_rules! define_queries {
160160
$crate::query_impl::$name::will_cache_on_disk_for_key,
161161

162162
#[cfg($cache_on_disk)]
163-
try_load_from_disk_fn: |tcx, key, prev_index, index| {
163+
try_load_from_disk_fn: |tcx, prev_index| {
164164
use rustc_middle::queries::$name::{ProvidedValue, provided_to_erased};
165165

166-
// Check the cache-on-disk condition for this key.
167-
if !$crate::query_impl::$name::will_cache_on_disk_for_key(key) {
168-
return None;
169-
}
170-
171166
let loaded_value: ProvidedValue<'tcx> =
172-
$crate::plumbing::try_load_from_disk(tcx, prev_index, index)?;
167+
$crate::plumbing::try_load_from_disk(tcx, prev_index)?;
173168

174169
// Arena-alloc the value if appropriate, and erase it.
175170
Some(provided_to_erased(tcx, loaded_value))
176171
},
177172
#[cfg(not($cache_on_disk))]
178-
try_load_from_disk_fn: |_tcx, _key, _prev_index, _index| None,
173+
try_load_from_disk_fn: |_tcx, _prev_index| None,
179174

180175
#[cfg($handle_cycle_error)]
181176
handle_cycle_error_fn: |tcx, key, cycle, err| {

0 commit comments

Comments
 (0)