Skip to content

Commit 30e75b0

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 de6d33c commit 30e75b0

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
@@ -33,7 +33,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
3333
pub query_cache: usize,
3434
pub cache_on_disk: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
3535
pub execute_query: fn(tcx: TyCtxt<'tcx>, k: C::Key) -> C::Value,
36-
pub compute: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
36+
pub compute_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
3737
pub can_load_from_disk: bool,
3838
pub try_load_from_disk: fn(
3939
tcx: TyCtxt<'tcx>,

compiler/rustc_query_impl/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_query_system::query::{
2626
};
2727
use rustc_span::{ErrorGuaranteed, Span};
2828

29-
use crate::plumbing::{__rust_begin_short_backtrace, encode_all_query_results, try_mark_green};
29+
use crate::plumbing::{encode_all_query_results, try_mark_green};
3030
use crate::profiling_support::QueryKeyStringCache;
3131

3232
#[macro_use]
@@ -124,7 +124,7 @@ where
124124

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

130130
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,6 @@ macro_rules! expand_if_cached {
573573
};
574574
}
575575

576-
/// Don't show the backtrace for query system by default
577-
/// use `RUST_BACKTRACE=full` to show all the backtraces
578-
#[inline(never)]
579-
pub(crate) fn __rust_begin_short_backtrace<F, T>(f: F) -> T
580-
where
581-
F: FnOnce() -> T,
582-
{
583-
let result = f();
584-
std::hint::black_box(());
585-
result
586-
}
587-
588576
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
589577
// invoked by `rustc_with_all_queries`.
590578
macro_rules! define_queries {
@@ -642,6 +630,32 @@ macro_rules! define_queries {
642630
}
643631
}
644632

633+
/// Defines a `compute` function for this query, to be used as a
634+
/// function pointer in the query's vtable.
635+
mod compute_fn {
636+
use super::*;
637+
use ::rustc_middle::query::queries::$name::{Key, Value, provided_to_erased};
638+
639+
/// This function would be named `compute`, but we also want it
640+
/// to mark the boundaries of an omitted region in backtraces.
641+
#[inline(never)]
642+
pub(crate) fn __rust_begin_short_backtrace<'tcx>(
643+
tcx: TyCtxt<'tcx>,
644+
key: Key<'tcx>,
645+
) -> Erase<Value<'tcx>> {
646+
#[cfg(debug_assertions)]
647+
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
648+
649+
// Call the actual provider function for this query.
650+
let provided_value = call_provider!([$($modifiers)*][tcx, $name, key]);
651+
rustc_middle::ty::print::with_reduced_queries!({
652+
tracing::trace!(?provided_value);
653+
});
654+
655+
provided_to_erased(tcx, provided_value)
656+
}
657+
}
658+
645659
pub(crate) fn make_query_vtable<'tcx>()
646660
-> QueryVTable<'tcx, queries::$name::Storage<'tcx>>
647661
{
@@ -654,22 +668,7 @@ macro_rules! define_queries {
654668
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
655669
cache_on_disk: |tcx, key| ::rustc_middle::query::cached::$name(tcx, key),
656670
execute_query: |tcx, key| erase(tcx.$name(key)),
657-
compute: |tcx, key| {
658-
#[cfg(debug_assertions)]
659-
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
660-
__rust_begin_short_backtrace(||
661-
queries::$name::provided_to_erased(
662-
tcx,
663-
{
664-
let ret = call_provider!([$($modifiers)*][tcx, $name, key]);
665-
rustc_middle::ty::print::with_reduced_queries!({
666-
tracing::trace!(?ret);
667-
});
668-
ret
669-
}
670-
)
671-
)
672-
},
671+
compute_fn: self::compute_fn::__rust_begin_short_backtrace,
673672
can_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] true false),
674673
try_load_from_disk: should_ever_cache_on_disk!([$($modifiers)*] {
675674
|tcx, key, prev_index, index| {

0 commit comments

Comments
 (0)