Skip to content

Commit 0528e08

Browse files
committed
Auto merge of #153323 - nnethercote:rm-impl-QueryVTable, r=<try>
Remove `impl QueryVTable`
2 parents ec818fd + a8af604 commit 0528e08

3 files changed

Lines changed: 58 additions & 93 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_macros::HashStable;
1111
use rustc_span::{ErrorGuaranteed, Span};
1212
pub use sealed::IntoQueryParam;
1313

14-
use crate::dep_graph::{DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
14+
use crate::dep_graph::{DepKind, DepNodeIndex, SerializedDepNodeIndex};
1515
use crate::ich::StableHashingContext;
1616
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables};
1717
use crate::query::on_disk_cache::OnDiskCache;
@@ -114,7 +114,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
114114
pub cycle_error_handling: CycleErrorHandling,
115115
pub state: QueryState<'tcx, C::Key>,
116116
pub cache: C,
117-
pub will_cache_on_disk_for_key_fn: Option<fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool>,
117+
pub will_cache_on_disk_for_key_fn: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
118118

119119
/// Function pointer that calls `tcx.$query(key)` for this query and
120120
/// discards the returned value.
@@ -130,17 +130,15 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
130130
/// This should be the only code that calls the provider function.
131131
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
132132

133-
pub try_load_from_disk_fn: Option<
134-
fn(
135-
tcx: TyCtxt<'tcx>,
136-
key: &C::Key,
137-
prev_index: SerializedDepNodeIndex,
138-
index: DepNodeIndex,
139-
) -> Option<C::Value>,
140-
>,
133+
pub try_load_from_disk_fn: fn(
134+
tcx: TyCtxt<'tcx>,
135+
key: &C::Key,
136+
prev_index: SerializedDepNodeIndex,
137+
index: DepNodeIndex,
138+
) -> Option<C::Value>,
141139

142140
pub is_loadable_from_disk_fn:
143-
Option<fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool>,
141+
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
144142

145143
/// Function pointer that hashes this query's result values.
146144
///
@@ -181,49 +179,6 @@ impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
181179
}
182180
}
183181

184-
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
185-
#[inline(always)]
186-
pub fn will_cache_on_disk_for_key(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
187-
self.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
188-
}
189-
190-
#[inline(always)]
191-
pub fn try_load_from_disk(
192-
&self,
193-
tcx: TyCtxt<'tcx>,
194-
key: &C::Key,
195-
prev_index: SerializedDepNodeIndex,
196-
index: DepNodeIndex,
197-
) -> Option<C::Value> {
198-
// `?` will return None immediately for queries that never cache to disk.
199-
self.try_load_from_disk_fn?(tcx, key, prev_index, index)
200-
}
201-
202-
#[inline]
203-
pub fn is_loadable_from_disk(
204-
&self,
205-
tcx: TyCtxt<'tcx>,
206-
key: &C::Key,
207-
index: SerializedDepNodeIndex,
208-
) -> bool {
209-
self.is_loadable_from_disk_fn.map_or(false, |f| f(tcx, key, index))
210-
}
211-
212-
/// Synthesize an error value to let compilation continue after a cycle.
213-
pub fn value_from_cycle_error(
214-
&self,
215-
tcx: TyCtxt<'tcx>,
216-
cycle_error: CycleError,
217-
guar: ErrorGuaranteed,
218-
) -> C::Value {
219-
(self.value_from_cycle_error)(tcx, cycle_error, guar)
220-
}
221-
222-
pub fn construct_dep_node(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> DepNode {
223-
DepNode::construct(tcx, self.dep_kind, key)
224-
}
225-
}
226-
227182
pub struct QuerySystem<'tcx> {
228183
pub arenas: WorkerLocal<QueryArenas<'tcx>>,
229184
pub query_vtables: QueryVTables<'tcx>,

compiler/rustc_query_impl/src/execution.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ fn mk_cycle<'tcx, C: QueryCache>(
129129
match query.cycle_error_handling {
130130
CycleErrorHandling::Error => {
131131
let guar = error.emit();
132-
query.value_from_cycle_error(tcx, cycle_error, guar)
132+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
133133
}
134134
CycleErrorHandling::Fatal => {
135135
let guar = error.emit();
136136
guar.raise_fatal();
137137
}
138138
CycleErrorHandling::DelayBug => {
139139
let guar = error.delay_as_bug();
140-
query.value_from_cycle_error(tcx, cycle_error, guar)
140+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
141141
}
142142
CycleErrorHandling::Stash => {
143143
let guar = if let Some(root) = cycle_error.cycle.first()
@@ -147,7 +147,7 @@ fn mk_cycle<'tcx, C: QueryCache>(
147147
} else {
148148
error.emit()
149149
};
150-
query.value_from_cycle_error(tcx, cycle_error, guar)
150+
(query.value_from_cycle_error)(tcx, cycle_error, guar)
151151
}
152152
}
153153
}
@@ -453,7 +453,8 @@ fn execute_job_incr<'tcx, C: QueryCache>(
453453

454454
if !query.anon && !query.eval_always {
455455
// `to_dep_node` is expensive for some `DepKind`s.
456-
let dep_node = dep_node_opt.get_or_insert_with(|| query.construct_dep_node(tcx, &key));
456+
let dep_node =
457+
dep_node_opt.get_or_insert_with(|| DepNode::construct(tcx, query.dep_kind, &key));
457458

458459
// The diagnostics for this query will be promoted to the current session during
459460
// `try_mark_green()`, so we can ignore them here.
@@ -485,7 +486,8 @@ fn execute_job_incr<'tcx, C: QueryCache>(
485486
}
486487

487488
// `to_dep_node` is expensive for some `DepKind`s.
488-
let dep_node = dep_node_opt.unwrap_or_else(|| query.construct_dep_node(tcx, &key));
489+
let dep_node =
490+
dep_node_opt.unwrap_or_else(|| DepNode::construct(tcx, query.dep_kind, &key));
489491

490492
// Call the query provider.
491493
dep_graph_data.with_task(
@@ -522,7 +524,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
522524

523525
// First we try to load the result from the on-disk cache.
524526
// Some things are never cached on disk.
525-
if let Some(value) = query.try_load_from_disk(tcx, key, prev_index, dep_node_index) {
527+
if let Some(value) = (query.try_load_from_disk_fn)(tcx, key, prev_index, dep_node_index) {
526528
if std::intrinsics::unlikely(tcx.sess.opts.unstable_opts.query_dep_graph) {
527529
dep_graph_data.mark_debug_loaded_from_disk(*dep_node)
528530
}
@@ -555,15 +557,15 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
555557
// We always expect to find a cached result for things that
556558
// can be forced from `DepNode`.
557559
debug_assert!(
558-
!query.will_cache_on_disk_for_key(tcx, key)
560+
!(query.will_cache_on_disk_for_key_fn)(tcx, key)
559561
|| !tcx.key_fingerprint_style(dep_node.kind).is_maybe_recoverable(),
560562
"missing on-disk cache entry for {dep_node:?}"
561563
);
562564

563565
// Sanity check for the logic in `ensure`: if the node is green and the result loadable,
564566
// we should actually be able to load it.
565567
debug_assert!(
566-
!query.is_loadable_from_disk(tcx, key, prev_index),
568+
!(query.is_loadable_from_disk_fn)(tcx, key, prev_index),
567569
"missing on-disk cache entry for loadable {dep_node:?}"
568570
);
569571

@@ -629,7 +631,7 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
629631
// Ensuring an anonymous query makes no sense
630632
assert!(!query.anon);
631633

632-
let dep_node = query.construct_dep_node(tcx, key);
634+
let dep_node = DepNode::construct(tcx, query.dep_kind, key);
633635

634636
let dep_graph = &tcx.dep_graph;
635637
let serialized_dep_node_index = match dep_graph.try_mark_green(tcx, &dep_node) {
@@ -660,7 +662,7 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
660662
// In ensure-done mode, we can only skip execution for this key if
661663
// there's a disk-cached value available to load later if needed,
662664
// which guarantees the query provider will never run for this key.
663-
let is_loadable = query.is_loadable_from_disk(tcx, key, serialized_dep_node_index);
665+
let is_loadable = (query.is_loadable_from_disk_fn)(tcx, key, serialized_dep_node_index);
664666
EnsureCanSkip { skip_execution: is_loadable, dep_node: Some(dep_node) }
665667
}
666668
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, C, V>(
287287

288288
assert!(all_inactive(&query.state));
289289
query.cache.iter(&mut |key, value, dep_node| {
290-
if query.will_cache_on_disk_for_key(tcx, key) {
290+
if (query.will_cache_on_disk_for_key_fn)(tcx, key) {
291291
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
292292

293293
// Record position of the cache entry.
@@ -340,7 +340,7 @@ pub(crate) fn promote_from_disk_inner<'tcx, Q: GetQueryVTable<'tcx>>(
340340
dep_node.key_fingerprint
341341
)
342342
});
343-
if query.will_cache_on_disk_for_key(tcx, &key) {
343+
if (query.will_cache_on_disk_for_key_fn)(tcx, &key) {
344344
// Call `tcx.$query(key)` for its side-effect of loading the disk-cached
345345
// value into memory.
346346
(query.call_query_method_fn)(tcx, key);
@@ -523,41 +523,49 @@ macro_rules! define_queries {
523523
cycle_error_handling: cycle_error_handling!([$($modifiers)*]),
524524
state: Default::default(),
525525
cache: Default::default(),
526-
will_cache_on_disk_for_key_fn: if_cache_on_disk!([$($modifiers)*] {
527-
Some(::rustc_middle::queries::_cache_on_disk_if_fns::$name)
528-
} {
529-
None
530-
}),
526+
will_cache_on_disk_for_key_fn: if_cache_on_disk!(
527+
[$($modifiers)*]
528+
{ ::rustc_middle::queries::_cache_on_disk_if_fns::$name }
529+
{ |_, _| false }
530+
),
531531
call_query_method_fn: |tcx, key| {
532532
// Call the query method for its side-effect of loading a value
533533
// from disk-cache; the caller doesn't need the value.
534534
let _ = tcx.$name(key);
535535
},
536536
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
537-
try_load_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
538-
Some(|tcx, key, prev_index, index| {
539-
// Check the `cache_on_disk_if` condition for this key.
540-
if !::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) {
541-
return None;
542-
}
537+
try_load_from_disk_fn: if_cache_on_disk!(
538+
[$($modifiers)*]
539+
{
540+
|tcx, key, prev_index, index| {
541+
// Check the `cache_on_disk_if` condition for this key.
542+
if !::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) {
543+
return None;
544+
}
543545

544-
let value: queries::$name::ProvidedValue<'tcx> =
545-
$crate::plumbing::try_load_from_disk(tcx, prev_index, index)?;
546+
let value: queries::$name::ProvidedValue<'tcx> =
547+
$crate::plumbing::try_load_from_disk(tcx, prev_index, index)?;
546548

547-
// Arena-alloc the value if appropriate, and erase it.
548-
Some(queries::$name::provided_to_erased(tcx, value))
549-
})
550-
} {
551-
None
552-
}),
553-
is_loadable_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
554-
Some(|tcx, key, index| -> bool {
555-
::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) &&
556-
$crate::plumbing::loadable_from_disk(tcx, index)
557-
})
558-
} {
559-
None
560-
}),
549+
// Arena-alloc the value if appropriate, and erase it.
550+
Some(queries::$name::provided_to_erased(tcx, value))
551+
}
552+
}
553+
{
554+
|_tcx, _key, _prev_index, _index| None
555+
}
556+
),
557+
is_loadable_from_disk_fn: if_cache_on_disk!(
558+
[$($modifiers)*]
559+
{
560+
|tcx, key, index| -> bool {
561+
::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) &&
562+
$crate::plumbing::loadable_from_disk(tcx, index)
563+
}
564+
}
565+
{
566+
|_tcx, _key, _index| false
567+
}
568+
),
561569
value_from_cycle_error: |tcx, cycle, guar| {
562570
let result: queries::$name::Value<'tcx> =
563571
FromCycleError::from_cycle_error(tcx, cycle, guar);

0 commit comments

Comments
 (0)