Skip to content

Commit 00de689

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 44e34e1 commit 00de689

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

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
@@ -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]
@@ -117,7 +117,7 @@ where
117117

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

123123
#[inline(always)]

compiler/rustc_query_impl/src/plumbing.rs

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

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

629+
/// Defines a `compute` function for this query, to be used as a
630+
/// function pointer in the query's vtable.
631+
mod compute_fn {
632+
use super::*;
633+
use ::rustc_middle::query::queries::$name::{Key, Value, provided_to_erased};
634+
635+
/// This function would be named `compute`, but we also want it
636+
/// to mark the boundaries of an omitted region in backtraces.
637+
#[inline(never)]
638+
pub(crate) fn __rust_begin_short_backtrace<'tcx>(
639+
tcx: TyCtxt<'tcx>,
640+
key: Key<'tcx>,
641+
) -> Erased<Value<'tcx>> {
642+
#[cfg(debug_assertions)]
643+
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
644+
645+
// Call the actual provider function for this query.
646+
let provided_value = call_provider!([$($modifiers)*][tcx, $name, key]);
647+
rustc_middle::ty::print::with_reduced_queries!({
648+
tracing::trace!(?provided_value);
649+
});
650+
651+
provided_to_erased(tcx, provided_value)
652+
}
653+
}
654+
641655
pub(crate) fn make_query_vtable<'tcx>()
642656
-> QueryVTable<'tcx, queries::$name::Storage<'tcx>>
643657
{
@@ -654,22 +668,7 @@ macro_rules! define_queries {
654668
None
655669
}),
656670
execute_query: |tcx, key| erase::erase_val(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
try_load_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
674673
Some(|tcx, key, prev_index, index| {
675674
// Check the `cache_on_disk_if` condition for this key.

0 commit comments

Comments
 (0)