@@ -18,7 +18,7 @@ use tracing::warn;
1818
1919use crate :: dep_graph:: { DepNode , DepNodeIndex } ;
2020use crate :: job:: { QueryJobInfo , QueryJobMap , find_cycle_in_stack, report_cycle} ;
21- use crate :: plumbing:: { current_query_job, next_job_id, start_query} ;
21+ use crate :: plumbing:: { current_query_job, loadable_from_disk , next_job_id, start_query} ;
2222use crate :: query_impl:: for_each_query_vtable;
2323
2424#[ inline]
@@ -488,81 +488,59 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
488488
489489 debug_assert ! ( dep_graph_data. is_index_green( prev_index) ) ;
490490
491- // First we try to load the result from the on-disk cache.
492- // Some things are never cached on disk.
493- if let Some ( value) = ( query. try_load_from_disk_fn ) ( tcx, key, prev_index, dep_node_index) {
494- if std:: intrinsics:: unlikely ( tcx. sess . opts . unstable_opts . query_dep_graph ) {
495- dep_graph_data. mark_debug_loaded_from_disk ( * dep_node)
491+ // First try to load the result from the on-disk cache. Some things are never cached on disk.
492+ let value;
493+ let verify;
494+ match ( query. try_load_from_disk_fn ) ( tcx, key, prev_index, dep_node_index) {
495+ Some ( loaded_value) => {
496+ if std:: intrinsics:: unlikely ( tcx. sess . opts . unstable_opts . query_dep_graph ) {
497+ dep_graph_data. mark_debug_loaded_from_disk ( * dep_node)
498+ }
499+
500+ value = loaded_value;
501+
502+ let prev_fingerprint = dep_graph_data. prev_value_fingerprint_of ( prev_index) ;
503+ // If `-Zincremental-verify-ich` is specified, re-hash results from
504+ // the cache and make sure that they have the expected fingerprint.
505+ //
506+ // If not, we still seek to verify a subset of fingerprints loaded
507+ // from disk. Re-hashing results is fairly expensive, so we can't
508+ // currently afford to verify every hash. This subset should still
509+ // give us some coverage of potential bugs.
510+ verify = prev_fingerprint. split ( ) . 1 . as_u64 ( ) . is_multiple_of ( 32 )
511+ || tcx. sess . opts . unstable_opts . incremental_verify_ich ;
496512 }
513+ None => {
514+ // We could not load a result from the on-disk cache, so recompute. The dep-graph for
515+ // this computation is already in-place, so we can just call the query provider.
516+ let prof_timer = tcx. prof . query_provider ( ) ;
517+ value = tcx. dep_graph . with_ignore ( || ( query. invoke_provider_fn ) ( tcx, key) ) ;
518+ prof_timer. finish_with_query_invocation_id ( dep_node_index. into ( ) ) ;
497519
498- let prev_fingerprint = dep_graph_data. prev_value_fingerprint_of ( prev_index) ;
499- // If `-Zincremental-verify-ich` is specified, re-hash results from
500- // the cache and make sure that they have the expected fingerprint.
501- //
502- // If not, we still seek to verify a subset of fingerprints loaded
503- // from disk. Re-hashing results is fairly expensive, so we can't
504- // currently afford to verify every hash. This subset should still
505- // give us some coverage of potential bugs though.
506- let try_verify = prev_fingerprint. split ( ) . 1 . as_u64 ( ) . is_multiple_of ( 32 ) ;
507- if std:: intrinsics:: unlikely (
508- try_verify || tcx. sess . opts . unstable_opts . incremental_verify_ich ,
509- ) {
510- incremental_verify_ich (
511- tcx,
512- dep_graph_data,
513- & value,
514- prev_index,
515- query. hash_value_fn ,
516- query. format_value ,
517- ) ;
520+ verify = true ;
518521 }
522+ } ;
519523
520- return value;
524+ if verify {
525+ // Verify that re-running the query produced a result with the expected hash.
526+ // This catches bugs in query implementations, turning them into ICEs.
527+ // For example, a query might sort its result by `DefId` - since `DefId`s are
528+ // not stable across compilation sessions, the result could get up getting sorted
529+ // in a different order when the query is re-run, even though all of the inputs
530+ // (e.g. `DefPathHash` values) were green.
531+ //
532+ // See issue #82920 for an example of a miscompilation that would get turned into
533+ // an ICE by this check
534+ incremental_verify_ich (
535+ tcx,
536+ dep_graph_data,
537+ & value,
538+ prev_index,
539+ query. hash_value_fn ,
540+ query. format_value ,
541+ ) ;
521542 }
522543
523- // We always expect to find a cached result for things that
524- // can be forced from `DepNode`.
525- debug_assert ! (
526- !( query. will_cache_on_disk_for_key_fn) ( tcx, key)
527- || !tcx. key_fingerprint_style( dep_node. kind) . is_maybe_recoverable( ) ,
528- "missing on-disk cache entry for {dep_node:?}"
529- ) ;
530-
531- // Sanity check for the logic in `ensure`: if the node is green and the result loadable,
532- // we should actually be able to load it.
533- debug_assert ! (
534- !( query. is_loadable_from_disk_fn) ( tcx, key, prev_index) ,
535- "missing on-disk cache entry for loadable {dep_node:?}"
536- ) ;
537-
538- // We could not load a result from the on-disk cache, so
539- // recompute.
540- let prof_timer = tcx. prof . query_provider ( ) ;
541-
542- // The dep-graph for this computation is already in-place.
543- // Call the query provider.
544- let value = tcx. dep_graph . with_ignore ( || ( query. invoke_provider_fn ) ( tcx, key) ) ;
545-
546- prof_timer. finish_with_query_invocation_id ( dep_node_index. into ( ) ) ;
547-
548- // Verify that re-running the query produced a result with the expected hash
549- // This catches bugs in query implementations, turning them into ICEs.
550- // For example, a query might sort its result by `DefId` - since `DefId`s are
551- // not stable across compilation sessions, the result could get up getting sorted
552- // in a different order when the query is re-run, even though all of the inputs
553- // (e.g. `DefPathHash` values) were green.
554- //
555- // See issue #82920 for an example of a miscompilation that would get turned into
556- // an ICE by this check
557- incremental_verify_ich (
558- tcx,
559- dep_graph_data,
560- & value,
561- prev_index,
562- query. hash_value_fn ,
563- query. format_value ,
564- ) ;
565-
566544 value
567545}
568546
@@ -624,7 +602,8 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
624602 // In ensure-done mode, we can only skip execution for this key if
625603 // there's a disk-cached value available to load later if needed,
626604 // which guarantees the query provider will never run for this key.
627- let is_loadable = ( query. is_loadable_from_disk_fn ) ( tcx, key, serialized_dep_node_index) ;
605+ let is_loadable = ( query. will_cache_on_disk_for_key_fn ) ( tcx, key)
606+ && loadable_from_disk ( tcx, serialized_dep_node_index) ;
628607 EnsureCanSkip { skip_execution : is_loadable, dep_node : Some ( dep_node) }
629608 }
630609 }
0 commit comments