Skip to content

Commit d8a5ccf

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 9d272a1 + eae7593 commit d8a5ccf

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
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

+48
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,14 @@ pub(super) fn encode_all_query_results<'tcx>(
189190
}
190191
}
191192

193+
pub fn query_key_hash_verify_all<'tcx>(tcx: TyCtxt<'tcx>) {
194+
tcx.sess.time("query_key_hash_verify_all", || {
195+
for verify in super::QUERY_KEY_HASH_VERIFY.iter() {
196+
verify(tcx);
197+
}
198+
})
199+
}
200+
192201
macro_rules! handle_cycle_error {
193202
([]) => {{
194203
rustc_query_system::HandleCycleError::Error
@@ -370,6 +379,34 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
370379
});
371380
}
372381

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

696740
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -730,6 +774,10 @@ macro_rules! define_queries {
730774
>
731775
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
732776

777+
const QUERY_KEY_HASH_VERIFY: &[
778+
for<'tcx> fn(TyCtxt<'tcx>)
779+
] = &[$(query_impl::$name::query_key_hash_verify),*];
780+
733781
#[allow(nonstandard_style)]
734782
mod query_callbacks {
735783
use super::*;

0 commit comments

Comments
 (0)