Skip to content

Commit 77d32ad

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 77d32ad

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_target::callconv::FnAbi;
1818

1919
use super::{
2020
AllocBytes, AllocId, AllocKind, AllocRange, Allocation, CTFE_ALLOC_SALT, ConstAllocation,
21-
CtfeProvenance, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, MemoryKind,
22-
Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
21+
CtfeProvenance, EnteredTraceSpan, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy,
22+
MemoryKind, Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
2323
};
2424

2525
/// Data returned by [`Machine::after_stack_pop`], and consumed by
@@ -147,12 +147,6 @@ pub trait Machine<'tcx>: Sized {
147147
/// already been checked before.
148148
const ALL_CONSTS_ARE_PRECHECKED: bool = true;
149149

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-
156150
/// Whether memory accesses should be alignment-checked.
157151
fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
158152

@@ -634,6 +628,16 @@ pub trait Machine<'tcx>: Sized {
634628
/// Compute the value passed to the constructors of the `AllocBytes` type for
635629
/// abstract machine allocations.
636630
fn get_default_alloc_params(&self) -> <Self::Bytes as AllocBytes>::AllocParams;
631+
632+
/// Allows enabling/disabling tracing calls from within `rustc_const_eval` at compile time, by
633+
/// delegating the entering of [tracing::Span]s to implementors of the [Machine] trait. The
634+
/// default implementation corresponds to tracing being disabled, meaning the tracing calls will
635+
/// supposedly be optimized out completely. To enable tracing, override this trait method and
636+
/// return `span.entered()`. Also see [crate::enter_trace_span].
637+
#[must_use]
638+
fn enter_trace_span(_span: tracing::Span) -> impl EnteredTraceSpan {
639+
()
640+
}
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
@@ -37,6 +37,7 @@ pub use self::place::{MPlaceTy, MemPlaceMeta, PlaceTy, Writeable};
3737
use self::place::{MemPlace, Place};
3838
pub use self::projection::{OffsetMode, Projectable};
3939
pub use self::stack::{Frame, FrameInfo, LocalState, StackPopCleanup, StackPopInfo};
40+
pub use self::util::EnteredTraceSpan;
4041
pub(crate) use self::util::create_static_alloc;
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)