Skip to content

Commit 122a72d

Browse files
authored
Unrolled build for #153120
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 69b7853 + fb42537 commit 122a72d

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

‎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
@@ -475,7 +475,12 @@ macro_rules! define_queries {
475475
use super::super::*;
476476
use ::rustc_middle::query::erase::{self, Erased};
477477

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

481486
// Adding `__rust_end_short_backtrace` marker to backtraces so that we emit the frames
@@ -489,7 +494,7 @@ macro_rules! define_queries {
489494
) -> Option<Erased<queries::$name::Value<'tcx>>> {
490495
#[cfg(debug_assertions)]
491496
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
492-
execution::get_query_incr(
497+
execution::execute_query_incr_inner(
493498
&tcx.query_system.query_vtables.$name,
494499
tcx,
495500
span,
@@ -499,7 +504,7 @@ macro_rules! define_queries {
499504
}
500505
}
501506

502-
pub(crate) mod get_query_non_incr {
507+
pub(crate) mod execute_query_non_incr {
503508
use super::*;
504509

505510
#[inline(never)]
@@ -509,7 +514,7 @@ macro_rules! define_queries {
509514
key: queries::$name::Key<'tcx>,
510515
__mode: QueryMode,
511516
) -> Option<Erased<queries::$name::Value<'tcx>>> {
512-
Some(execution::get_query_non_incr(
517+
Some(execution::execute_query_non_incr_inner(
513518
&tcx.query_system.query_vtables.$name,
514519
tcx,
515520
span,
@@ -604,9 +609,9 @@ macro_rules! define_queries {
604609
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
605610
description_fn: $crate::queries::_description_fns::$name,
606611
execute_query_fn: if incremental {
607-
query_impl::$name::get_query_incr::__rust_end_short_backtrace
612+
query_impl::$name::execute_query_incr::__rust_end_short_backtrace
608613
} else {
609-
query_impl::$name::get_query_non_incr::__rust_end_short_backtrace
614+
query_impl::$name::execute_query_non_incr::__rust_end_short_backtrace
610615
},
611616
}
612617
}

0 commit comments

Comments
 (0)