Skip to content

Commit 3eafea8

Browse files
Rollup merge of #153120 - Zalathar:execute, r=nnethercote
Clean up some code related to `QueryVTable::execute_query_fn` This PR is an assortment of small cleanups to code that interacts with `execute_query_fn` in the query vtable. I also experimented with trying to replace the macro-generated `__rust_end_short_backtrace` functions with a single shared generic function, but I couldn't manage to avoid breaking short backtraces, so I left a note behind to document my attempt. r? nnethercote (or compiler)
2 parents 272d523 + fb42537 commit 3eafea8

4 files changed

Lines changed: 39 additions & 26 deletions

File tree

compiler/rustc_middle/src/query/inner.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,16 @@ where
3535
#[inline(always)]
3636
pub(crate) fn query_get_at<'tcx, C>(
3737
tcx: TyCtxt<'tcx>,
38-
execute_query: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
39-
query_cache: &C,
4038
span: Span,
39+
query: &'tcx QueryVTable<'tcx, C>,
4140
key: C::Key,
4241
) -> C::Value
4342
where
4443
C: QueryCache,
4544
{
46-
match try_get_cached(tcx, query_cache, &key) {
45+
match try_get_cached(tcx, &query.cache, &key) {
4746
Some(value) => value,
48-
None => execute_query(tcx, span, key, QueryMode::Get).unwrap(),
47+
None => (query.execute_query_fn)(tcx, span, key, QueryMode::Get).unwrap(),
4948
}
5049
}
5150

@@ -54,15 +53,14 @@ where
5453
#[inline]
5554
pub(crate) fn query_ensure<'tcx, C>(
5655
tcx: TyCtxt<'tcx>,
57-
execute_query: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
58-
query_cache: &C,
56+
query: &'tcx QueryVTable<'tcx, C>,
5957
key: C::Key,
6058
ensure_mode: EnsureMode,
6159
) where
6260
C: QueryCache,
6361
{
64-
if try_get_cached(tcx, query_cache, &key).is_none() {
65-
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode });
62+
if try_get_cached(tcx, &query.cache, &key).is_none() {
63+
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode });
6664
}
6765
}
6866

@@ -71,8 +69,7 @@ pub(crate) fn query_ensure<'tcx, C>(
7169
#[inline]
7270
pub(crate) fn query_ensure_error_guaranteed<'tcx, C, T>(
7371
tcx: TyCtxt<'tcx>,
74-
execute_query: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
75-
query_cache: &C,
72+
query: &'tcx QueryVTable<'tcx, C>,
7673
key: C::Key,
7774
// This arg is needed to match the signature of `query_ensure`,
7875
// but should always be `EnsureMode::Ok`.
@@ -84,10 +81,10 @@ where
8481
{
8582
assert_matches!(ensure_mode, EnsureMode::Ok);
8683

87-
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
84+
if let Some(res) = try_get_cached(tcx, &query.cache, &key) {
8885
erase::restore_val(res).map(drop)
8986
} else {
90-
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode })
87+
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Ensure { ensure_mode })
9188
.map(erase::restore_val)
9289
.map(|res| res.map(drop))
9390
// Either we actually executed the query, which means we got a full `Result`,

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,15 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
157157
/// Used when reporting query cycle errors and similar problems.
158158
pub description_fn: fn(TyCtxt<'tcx>, C::Key) -> String,
159159

160+
/// Function pointer that is called by the query methods on [`TyCtxt`] and
161+
/// friends[^1], after they have checked the in-memory cache and found no
162+
/// existing value for this key.
163+
///
164+
/// Transitive responsibilities include trying to load a disk-cached value
165+
/// if possible (incremental only), invoking the query provider if necessary,
166+
/// and putting the obtained value into the in-memory cache.
167+
///
168+
/// [^1]: [`TyCtxt`], [`TyCtxtAt`], [`TyCtxtEnsureOk`], [`TyCtxtEnsureDone`]
160169
pub execute_query_fn: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
161170
}
162171

@@ -495,8 +504,7 @@ macro_rules! define_callbacks {
495504
(crate::query::inner::query_ensure)
496505
)(
497506
self.tcx,
498-
self.tcx.query_system.query_vtables.$name.execute_query_fn,
499-
&self.tcx.query_system.query_vtables.$name.cache,
507+
&self.tcx.query_system.query_vtables.$name,
500508
$crate::query::IntoQueryParam::into_query_param(key),
501509
$crate::query::EnsureMode::Ok,
502510
)
@@ -511,8 +519,7 @@ macro_rules! define_callbacks {
511519
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
512520
crate::query::inner::query_ensure(
513521
self.tcx,
514-
self.tcx.query_system.query_vtables.$name.execute_query_fn,
515-
&self.tcx.query_system.query_vtables.$name.cache,
522+
&self.tcx.query_system.query_vtables.$name,
516523
$crate::query::IntoQueryParam::into_query_param(key),
517524
$crate::query::EnsureMode::Done,
518525
);
@@ -540,9 +547,8 @@ macro_rules! define_callbacks {
540547

541548
erase::restore_val::<$V>(inner::query_get_at(
542549
self.tcx,
543-
self.tcx.query_system.query_vtables.$name.execute_query_fn,
544-
&self.tcx.query_system.query_vtables.$name.cache,
545550
self.span,
551+
&self.tcx.query_system.query_vtables.$name,
546552
$crate::query::IntoQueryParam::into_query_param(key),
547553
))
548554
}

compiler/rustc_query_impl/src/execution.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ fn wait_for_query<'tcx, C: QueryCache>(
270270
}
271271
}
272272

273+
/// Shared main part of both [`execute_query_incr_inner`] and [`execute_query_non_incr_inner`].
273274
#[inline(never)]
274275
fn try_execute_query<'tcx, C: QueryCache, const INCR: bool>(
275276
query: &'tcx QueryVTable<'tcx, C>,
@@ -650,8 +651,10 @@ fn check_if_ensure_can_skip_execution<'tcx, C: QueryCache>(
650651
}
651652
}
652653

654+
/// Called by a macro-generated impl of [`QueryVTable::execute_query_fn`],
655+
/// in non-incremental mode.
653656
#[inline(always)]
654-
pub(super) fn get_query_non_incr<'tcx, C: QueryCache>(
657+
pub(super) fn execute_query_non_incr_inner<'tcx, C: QueryCache>(
655658
query: &'tcx QueryVTable<'tcx, C>,
656659
tcx: TyCtxt<'tcx>,
657660
span: Span,
@@ -662,8 +665,10 @@ pub(super) fn get_query_non_incr<'tcx, C: QueryCache>(
662665
ensure_sufficient_stack(|| try_execute_query::<C, false>(query, tcx, span, key, None).0)
663666
}
664667

668+
/// Called by a macro-generated impl of [`QueryVTable::execute_query_fn`],
669+
/// in incremental mode.
665670
#[inline(always)]
666-
pub(super) fn get_query_incr<'tcx, C: QueryCache>(
671+
pub(super) fn execute_query_incr_inner<'tcx, C: QueryCache>(
667672
query: &'tcx QueryVTable<'tcx, C>,
668673
tcx: TyCtxt<'tcx>,
669674
span: Span,

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,12 @@ macro_rules! define_queries {
476476
use super::super::*;
477477
use ::rustc_middle::query::erase::{self, Erased};
478478

479-
pub(crate) mod get_query_incr {
479+
// It seems to be important that every query has its own monomorphic
480+
// copy of `execute_query_incr` and `execute_query_non_incr`.
481+
// Trying to inline these wrapper functions into their generic
482+
// "inner" helpers tends to break `tests/run-make/short-ice`.
483+
484+
pub(crate) mod execute_query_incr {
480485
use super::*;
481486

482487
// Adding `__rust_end_short_backtrace` marker to backtraces so that we emit the frames
@@ -490,7 +495,7 @@ macro_rules! define_queries {
490495
) -> Option<Erased<queries::$name::Value<'tcx>>> {
491496
#[cfg(debug_assertions)]
492497
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
493-
execution::get_query_incr(
498+
execution::execute_query_incr_inner(
494499
&tcx.query_system.query_vtables.$name,
495500
tcx,
496501
span,
@@ -500,7 +505,7 @@ macro_rules! define_queries {
500505
}
501506
}
502507

503-
pub(crate) mod get_query_non_incr {
508+
pub(crate) mod execute_query_non_incr {
504509
use super::*;
505510

506511
#[inline(never)]
@@ -510,7 +515,7 @@ macro_rules! define_queries {
510515
key: queries::$name::Key<'tcx>,
511516
__mode: QueryMode,
512517
) -> Option<Erased<queries::$name::Value<'tcx>>> {
513-
Some(execution::get_query_non_incr(
518+
Some(execution::execute_query_non_incr_inner(
514519
&tcx.query_system.query_vtables.$name,
515520
tcx,
516521
span,
@@ -605,9 +610,9 @@ macro_rules! define_queries {
605610
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
606611
description_fn: $crate::queries::_description_fns::$name,
607612
execute_query_fn: if incremental {
608-
query_impl::$name::get_query_incr::__rust_end_short_backtrace
613+
query_impl::$name::execute_query_incr::__rust_end_short_backtrace
609614
} else {
610-
query_impl::$name::get_query_non_incr::__rust_end_short_backtrace
615+
query_impl::$name::execute_query_non_incr::__rust_end_short_backtrace
611616
},
612617
}
613618
}

0 commit comments

Comments
 (0)