Skip to content

Commit 36cdce6

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 842bd5b commit 36cdce6

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
@@ -25,7 +25,7 @@ use rustc_query_system::query::{
2525
};
2626
use rustc_span::{ErrorGuaranteed, Span};
2727

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

3131
#[macro_use]
@@ -120,7 +120,7 @@ where
120120

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

126126
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

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

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

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

0 commit comments

Comments
 (0)