Skip to content

Commit 09117c9

Browse files
committed
Use the query vtable in query_feed plumbing
1 parent 7d8ebe3 commit 09117c9

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

compiler/rustc_middle/src/query/inner.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
//! Helper functions that serve as the immediate implementation of
22
//! `tcx.$query(..)` and its variations.
33
4-
use std::fmt::Debug;
5-
6-
use rustc_data_structures::fingerprint::Fingerprint;
74
use rustc_query_system::dep_graph::{DepKind, DepNodeParams};
8-
use rustc_query_system::ich::StableHashingContext;
95
use rustc_query_system::query::{QueryCache, QueryMode, try_get_cached};
106
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
117

128
use crate::dep_graph;
139
use crate::query::IntoQueryParam;
1410
use crate::query::erase::{self, Erasable, Erased};
11+
use crate::query::plumbing::QueryVTable;
1512
use crate::ty::TyCtxt;
1613

1714
/// Shared implementation of `tcx.$query(..)` and `tcx.at(span).$query(..)`
@@ -84,35 +81,38 @@ where
8481
}
8582

8683
/// Common implementation of query feeding, used by `define_feedable!`.
87-
pub(crate) fn query_feed<'tcx, Cache, Value>(
84+
pub(crate) fn query_feed<'tcx, Cache>(
8885
tcx: TyCtxt<'tcx>,
8986
dep_kind: DepKind,
90-
hasher: Option<fn(&mut StableHashingContext<'_>, &Value) -> Fingerprint>,
87+
query_vtable: &QueryVTable<'tcx, Cache>,
9188
cache: &Cache,
9289
key: Cache::Key,
93-
erased: Erased<Value>,
90+
value: Cache::Value,
9491
) where
95-
Cache: QueryCache<Value = Erased<Value>>,
92+
Cache: QueryCache,
9693
Cache::Key: DepNodeParams<TyCtxt<'tcx>>,
97-
Value: Erasable + Debug,
9894
{
99-
let value = erase::restore_val::<Value>(erased);
95+
let format_value = query_vtable.format_value;
10096

97+
// Check whether the in-memory cache already has a value for this key.
10198
match try_get_cached(tcx, cache, &key) {
10299
Some(old) => {
103-
let old = erase::restore_val::<Value>(old);
104-
if let Some(hasher) = hasher {
105-
let (value_hash, old_hash): (Fingerprint, Fingerprint) = tcx
106-
.with_stable_hashing_context(|mut hcx| {
107-
(hasher(&mut hcx, &value), hasher(&mut hcx, &old))
108-
});
100+
// The query already has a cached value for this key.
101+
// That's OK if both values are the same, i.e. they have the same hash,
102+
// so now we check their hashes.
103+
if let Some(hasher_fn) = query_vtable.hash_result {
104+
let (old_hash, value_hash) = tcx.with_stable_hashing_context(|ref mut hcx| {
105+
(hasher_fn(hcx, &old), hasher_fn(hcx, &value))
106+
});
109107
if old_hash != value_hash {
110108
// We have an inconsistency. This can happen if one of the two
111109
// results is tainted by errors. In this case, delay a bug to
112110
// ensure compilation is doomed, and keep the `old` value.
113111
tcx.dcx().delayed_bug(format!(
114112
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
115-
old value: {old:?}\nnew value: {value:?}",
113+
old value: {old}\nnew value: {value}",
114+
old = format_value(&old),
115+
value = format_value(&value),
116116
));
117117
}
118118
} else {
@@ -121,14 +121,24 @@ pub(crate) fn query_feed<'tcx, Cache, Value>(
121121
// the query should not be marked `no_hash`.
122122
bug!(
123123
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
124-
old value: {old:?}\nnew value: {value:?}",
124+
old value: {old}\nnew value: {value}",
125+
old = format_value(&old),
126+
value = format_value(&value),
125127
)
126128
}
127129
}
128130
None => {
131+
// There is no cached value for this key, so feed the query by
132+
// adding the provided value to the cache.
129133
let dep_node = dep_graph::DepNode::construct(tcx, dep_kind, &key);
130-
let dep_node_index = tcx.dep_graph.with_feed_task(dep_node, tcx, &value, hasher);
131-
cache.complete(key, erased, dep_node_index);
134+
let dep_node_index = tcx.dep_graph.with_feed_task(
135+
dep_node,
136+
tcx,
137+
&value,
138+
query_vtable.hash_result,
139+
query_vtable.format_value,
140+
);
141+
cache.complete(key, value, dep_node_index);
132142
}
133143
}
134144
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, PreciseCapturingArgKind, Trait
8787
use rustc_index::IndexVec;
8888
use rustc_lint_defs::LintId;
8989
use rustc_macros::rustc_queries;
90-
use rustc_query_system::ich::StableHashingContext;
9190
use rustc_query_system::query::{QueryMode, QueryState};
9291
use rustc_session::Limits;
9392
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,6 @@ macro_rules! define_callbacks {
492492
};
493493
}
494494

495-
macro_rules! hash_result {
496-
([]) => {{
497-
Some(dep_graph::hash_result)
498-
}};
499-
([(no_hash) $($rest:tt)*]) => {{
500-
None
501-
}};
502-
([$other:tt $($modifiers:tt)*]) => {
503-
hash_result!([$($modifiers)*])
504-
};
505-
}
506-
507495
macro_rules! define_feedable {
508496
($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
509497
$(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
@@ -513,19 +501,17 @@ macro_rules! define_feedable {
513501
let key = self.key().into_query_param();
514502

515503
let tcx = self.tcx;
516-
let erased = queries::$name::provided_to_erased(tcx, value);
517-
let cache = &tcx.query_system.caches.$name;
504+
let erased_value = queries::$name::provided_to_erased(tcx, value);
518505

519506
let dep_kind: dep_graph::DepKind = dep_graph::dep_kinds::$name;
520-
let hasher: Option<fn(&mut StableHashingContext<'_>, &_) -> _> = hash_result!([$($modifiers)*]);
521507

522508
$crate::query::inner::query_feed(
523509
tcx,
524510
dep_kind,
525-
hasher,
526-
cache,
511+
&tcx.query_system.query_vtables.$name,
512+
&tcx.query_system.caches.$name,
527513
key,
528-
erased,
514+
erased_value,
529515
);
530516
}
531517
})*

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,13 @@ impl<D: Deps> DepGraph<D> {
561561
/// FIXME: If the code is changed enough for this node to be marked before requiring the
562562
/// caller's node, we suppose that those changes will be enough to mark this node red and
563563
/// force a recomputation using the "normal" way.
564-
pub fn with_feed_task<Ctxt: DepContext<Deps = D>, R: Debug>(
564+
pub fn with_feed_task<Ctxt: DepContext<Deps = D>, R>(
565565
&self,
566566
node: DepNode,
567567
cx: Ctxt,
568568
result: &R,
569569
hash_result: Option<fn(&mut StableHashingContext<'_>, &R) -> Fingerprint>,
570+
format_value_fn: fn(&R) -> String,
570571
) -> DepNodeIndex {
571572
if let Some(data) = self.data.as_ref() {
572573
// The caller query has more dependencies than the node we are creating. We may
@@ -584,7 +585,7 @@ impl<D: Deps> DepGraph<D> {
584585
result,
585586
prev_index,
586587
hash_result,
587-
|value| format!("{value:?}"),
588+
format_value_fn,
588589
);
589590

590591
#[cfg(debug_assertions)]

0 commit comments

Comments
 (0)