Skip to content

Commit dda5bd3

Browse files
committed
Replace TRACING_ENABLED with enter_trace_span()
Hopefully this will make tracing calls be optimized out properly when tracing is disabled
1 parent 80a2332 commit dda5bd3

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use rustc_span::Span;
1616
use rustc_span::def_id::DefId;
1717
use rustc_target::callconv::FnAbi;
1818

19+
use crate::interpret::util::EnteredTraceSpan;
20+
1921
use super::{
2022
AllocBytes, AllocId, AllocKind, AllocRange, Allocation, CTFE_ALLOC_SALT, ConstAllocation,
2123
CtfeProvenance, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, MemoryKind,
@@ -147,12 +149,6 @@ pub trait Machine<'tcx>: Sized {
147149
/// already been checked before.
148150
const ALL_CONSTS_ARE_PRECHECKED: bool = true;
149151

150-
/// Determines whether rustc_const_eval functions that make use of the [Machine] should make
151-
/// tracing calls (to the `tracing` library). By default this is `false`, meaning the tracing
152-
/// calls will supposedly be optimized out. This flag is set to `true` inside Miri, to allow
153-
/// tracing the interpretation steps, among other things.
154-
const TRACING_ENABLED: bool = false;
155-
156152
/// Whether memory accesses should be alignment-checked.
157153
fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
158154

@@ -634,6 +630,14 @@ pub trait Machine<'tcx>: Sized {
634630
/// Compute the value passed to the constructors of the `AllocBytes` type for
635631
/// abstract machine allocations.
636632
fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams;
633+
634+
/// Allows enabling/disabling tracing calls from within `rustc_const_eval` at compile time, by
635+
/// delegating the entering of [tracing::Span]s to implementors of the [Machine] trait. The
636+
/// default implementation corresponds to tracing being disabled, meaning the tracing calls will
637+
/// supposedly be optimized out completely. To enable tracing, override this trait method and
638+
/// return `span.entered()`. Also see [crate::enter_trace_span].
639+
#[must_use]
640+
fn enter_trace_span(_span: tracing::Span) -> impl EnteredTraceSpan { () }
637641
}
638642

639643
/// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines

compiler/rustc_const_eval/src/interpret/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ use self::place::{MemPlace, Place};
3838
pub use self::projection::{OffsetMode, Projectable};
3939
pub use self::stack::{Frame, FrameInfo, LocalState, StackPopCleanup, StackPopInfo};
4040
pub(crate) use self::util::create_static_alloc;
41+
pub use self::util::EnteredTraceSpan;
4142
pub use self::validity::{CtfeValidationMode, RangeSet, RefTracking};
4243
pub use self::visitor::ValueVisitor;

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@ pub(crate) fn create_static_alloc<'tcx>(
4646
interp_ok(ecx.ptr_to_mplace(Pointer::from(alloc_id).into(), layout))
4747
}
4848

49-
/// This struct is needed to enforce `#[must_use]` on [tracing::span::EnteredSpan]
50-
/// while wrapping them in an `Option`.
51-
#[must_use]
52-
pub enum MaybeEnteredSpan {
53-
Some(tracing::span::EnteredSpan),
54-
None,
55-
}
49+
/// A marker trait returned by [crate::interpret::Machine::enter_trace_span], identifying either a
50+
/// real [tracing::span::EnteredSpan] in case tracing is enabled, or the dummy type `()` when
51+
/// tracing is disabled.
52+
pub trait EnteredTraceSpan {}
53+
impl EnteredTraceSpan for () {}
54+
impl EnteredTraceSpan for tracing::span::EnteredSpan {}
5655

56+
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span].
57+
/// This is supposed to be compiled out when [crate::interpret::Machine::enter_trace_span] has the
58+
/// default implementation (i.e. when it does not actually enter the span but instead returns `()`).
59+
/// Note: the result of this macro **must be used** because the span is exited when it's dropped.
5760
#[macro_export]
5861
macro_rules! enter_trace_span {
5962
($machine:ident, $($tt:tt)*) => {
60-
if $machine::TRACING_ENABLED {
61-
$crate::interpret::util::MaybeEnteredSpan::Some(tracing::info_span!($($tt)*).entered())
62-
} else {
63-
$crate::interpret::util::MaybeEnteredSpan::None
64-
}
63+
$machine::enter_trace_span(tracing::info_span!($($tt)*))
6564
}
6665
}

0 commit comments

Comments
 (0)