Skip to content

Commit 8455f36

Browse files
committed
Auto merge of rust-lang#122227 - Zoxc:query-hash-verify, r=<try>
Verify that query keys result in unique dep nodes This implements checking that query keys result into unique dep nodes as mentioned in rust-lang#112469. We could do a perf check to see how expensive this is. r? `@michaelwoerister`
2 parents 094a620 + 8099206 commit 8455f36

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

compiler/rustc_interface/src/queries.rs

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ impl Compiler {
321321
}
322322

323323
self.sess.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
324+
325+
gcx.enter(rustc_query_impl::query_key_hash_verify_all);
324326
}
325327

326328
// The timer's lifetime spans the dropping of `queries`, which contains

compiler/rustc_query_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_span::{ErrorGuaranteed, Span};
4141

4242
#[macro_use]
4343
mod plumbing;
44-
pub use crate::plumbing::QueryCtxt;
44+
pub use crate::plumbing::{query_key_hash_verify_all, QueryCtxt};
4545

4646
mod profiling_support;
4747
pub use self::profiling_support::alloc_self_profile_query_strings;

compiler/rustc_query_impl/src/plumbing.rs

+50
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::rustc_middle::dep_graph::DepContext;
66
use crate::rustc_middle::ty::TyEncoder;
77
use crate::QueryConfigRestored;
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
910
use rustc_data_structures::sync::Lock;
1011
use rustc_errors::DiagInner;
@@ -189,6 +190,16 @@ pub(super) fn encode_all_query_results<'tcx>(
189190
}
190191
}
191192

193+
pub fn query_key_hash_verify_all<'tcx>(tcx: TyCtxt<'tcx>) {
194+
if tcx.sess().opts.unstable_opts.incremental_verify_ich || cfg!(debug_assertions) {
195+
tcx.sess.time("query_key_hash_verify_all", || {
196+
for verify in super::QUERY_KEY_HASH_VERIFY.iter() {
197+
verify(tcx);
198+
}
199+
})
200+
}
201+
}
202+
192203
macro_rules! handle_cycle_error {
193204
([]) => {{
194205
rustc_query_system::HandleCycleError::Error
@@ -370,6 +381,34 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
370381
});
371382
}
372383

384+
pub(crate) fn query_key_hash_verify<'tcx>(
385+
query: impl QueryConfig<QueryCtxt<'tcx>>,
386+
qcx: QueryCtxt<'tcx>,
387+
) {
388+
let _timer =
389+
qcx.profiler().generic_activity_with_arg("query_key_hash_verify_for", query.name());
390+
391+
let mut map = FxHashMap::default();
392+
393+
let cache = query.query_cache(qcx);
394+
cache.iter(&mut |key, _, _| {
395+
let node = DepNode::construct(qcx.tcx, query.dep_kind(), key);
396+
if let Some(other_key) = map.insert(node, *key) {
397+
bug!(
398+
"query key:\n\
399+
`{:?}`\n\
400+
and key:\n\
401+
`{:?}`\n\
402+
mapped to the same dep node:\n\
403+
{:?}",
404+
key,
405+
other_key,
406+
node
407+
);
408+
}
409+
});
410+
}
411+
373412
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
374413
where
375414
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -691,6 +730,13 @@ macro_rules! define_queries {
691730
)
692731
}
693732
}}
733+
734+
pub fn query_key_hash_verify<'tcx>(tcx: TyCtxt<'tcx>) {
735+
$crate::plumbing::query_key_hash_verify(
736+
query_impl::$name::QueryType::config(tcx),
737+
QueryCtxt::new(tcx),
738+
)
739+
}
694740
})*}
695741

696742
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -730,6 +776,10 @@ macro_rules! define_queries {
730776
>
731777
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
732778

779+
const QUERY_KEY_HASH_VERIFY: &[
780+
for<'tcx> fn(TyCtxt<'tcx>)
781+
] = &[$(query_impl::$name::query_key_hash_verify),*];
782+
733783
#[allow(nonstandard_style)]
734784
mod query_callbacks {
735785
use super::*;

compiler/rustc_session/src/options.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,9 @@ options! {
16741674
"print high-level information about incremental reuse (or the lack thereof) \
16751675
(default: no)"),
16761676
incremental_verify_ich: bool = (false, parse_bool, [UNTRACKED],
1677-
"verify incr. comp. hashes of green query instances (default: no)"),
1677+
"verify extended properties for incr. comp. (default: no):
1678+
- hashes of green query instances
1679+
- hash collisions of query keys"),
16781680
inline_in_all_cgus: Option<bool> = (None, parse_opt_bool, [TRACKED],
16791681
"control whether `#[inline]` functions are in all CGUs"),
16801682
inline_llvm: bool = (true, parse_bool, [TRACKED],

0 commit comments

Comments
 (0)