Skip to content

Commit eaff0cb

Browse files
committed
Use fewer intermediate functions for short backtraces in queries
If we make sure that `compute_fn` in the query's vtable is actually named `__rust_begin_short_backtrace`, we can avoid the need for some additional intermediate functions and stack frames. This is similar to how the `get_query_incr` and `get_query_non_incr` functions are actually named `__rust_end_short_backtrace`.
1 parent 2c89741 commit eaff0cb

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
4545
pub query_cache: usize,
4646
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
4747
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
48-
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
48+
pub compute_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
4949
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
5050
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
5151
pub hash_result: HashResult<C::Value>,

compiler/rustc_query_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_query_system::query::{
2929
};
3030
use rustc_span::{ErrorGuaranteed, Span};
3131

32-
use crate::plumbing::{__rust_begin_short_backtrace, encode_all_query_results, try_mark_green};
32+
use crate::plumbing::{encode_all_query_results, try_mark_green};
3333
use crate::profiling_support::QueryKeyStringCache;
3434

3535
#[macro_use]
@@ -123,7 +123,7 @@ where
123123

124124
#[inline(always)]
125125
fn compute(self, qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
126-
(self.vtable.compute)(qcx.tcx, key)
126+
(self.vtable.compute_fn)(qcx.tcx, key)
127127
}
128128

129129
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -566,18 +566,6 @@ macro_rules! expand_if_cached {
566566
};
567567
}
568568

569-
/// Don't show the backtrace for query system by default
570-
/// use `RUST_BACKTRACE=full` to show all the backtraces
571-
#[inline(never)]
572-
pub(crate) fn __rust_begin_short_backtrace<F, T>(f: F) -> T
573-
where
574-
F: FnOnce() -> T,
575-
{
576-
let result = f();
577-
std::hint::black_box(());
578-
result
579-
}
580-
581569
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
582570
// invoked by `rustc_with_all_queries`.
583571
macro_rules! define_queries {
@@ -636,6 +624,32 @@ macro_rules! define_queries {
636624
}
637625
}
638626

627+
/// Defines a `compute` function for this query, to be used as a
628+
/// function pointer in the query's vtable.
629+
mod compute_fn {
630+
use super::*;
631+
use ::rustc_middle::queries::$name::{Key, Value, provided_to_erased};
632+
633+
/// This function would be named `compute`, but we also want it
634+
/// to mark the boundaries of an omitted region in backtraces.
635+
#[inline(never)]
636+
pub(crate) fn __rust_begin_short_backtrace<'tcx>(
637+
tcx: TyCtxt<'tcx>,
638+
key: Key<'tcx>,
639+
) -> Erased<Value<'tcx>> {
640+
#[cfg(debug_assertions)]
641+
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
642+
643+
// Call the actual provider function for this query.
644+
let provided_value = call_provider!([$($modifiers)*][tcx, $name, key]);
645+
rustc_middle::ty::print::with_reduced_queries!({
646+
tracing::trace!(?provided_value);
647+
});
648+
649+
provided_to_erased(tcx, provided_value)
650+
}
651+
}
652+
639653
pub(crate) fn make_query_vtable<'tcx>()
640654
-> QueryVTable<'tcx, queries::$name::Storage<'tcx>>
641655
{
@@ -652,22 +666,7 @@ macro_rules! define_queries {
652666
None
653667
}),
654668
execute_query: |tcx, key| erase::erase_val(tcx.$name(key)),
655-
compute: |tcx, key| {
656-
#[cfg(debug_assertions)]
657-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
658-
__rust_begin_short_backtrace(||
659-
queries::$name::provided_to_erased(
660-
tcx,
661-
{
662-
let ret = call_provider!([$($modifiers)*][tcx, $name, key]);
663-
rustc_middle::ty::print::with_reduced_queries!({
664-
tracing::trace!(?ret);
665-
});
666-
ret
667-
}
668-
)
669-
)
670-
},
669+
compute_fn: self::compute_fn::__rust_begin_short_backtrace,
671670
try_load_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
672671
Some(|tcx, key, prev_index, index| {
673672
// Check the `cache_on_disk_if` condition for this key.

0 commit comments

Comments
 (0)