Skip to content

Commit 221d6c7

Browse files
authored
Rollup merge of #140972 - Stypox:machine-tracing-flag, r=RalfJung
Add TRACING_ENABLED to Machine and add enter_trace_span!() This PR adds the necessary infrastructure to make it possible to do tracing calls from within `rustc_const_eval` when running Miri, while making sure they don't impact the performance of normal compiler execution. This is done by adding a `const` boolean to `Machine`, false by default, but that will be set to true in Miri only. The tracing macro `enter_trace_span!()` checks if it is true before doing anything, and since the value of a `const` is known at compile time, if it it false it the whole tracing call should be optimized out. I will soon open further PRs to add tracing macro calls similar to this one, so that afterwards it will be possible to learn more about Miri's time spent in the various interpretation steps: ```rs let _guard = enter_trace_span!(M, "eval_statement", "{:?}", stmt); ``` r? `@RalfJung`
2 parents 6cab15c + 28db348 commit 221d6c7

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

compiler/rustc_const_eval/src/interpret/machine.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ 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+
150156
/// Whether memory accesses should be alignment-checked.
151157
fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
152158

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,22 @@ pub(crate) fn create_static_alloc<'tcx>(
4545
assert!(ecx.memory.alloc_map.insert(alloc_id, (MemoryKind::Stack, alloc)).is_none());
4646
interp_ok(ecx.ptr_to_mplace(Pointer::from(alloc_id).into(), layout))
4747
}
48+
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+
}
56+
57+
#[macro_export]
58+
macro_rules! enter_trace_span {
59+
($machine:ident, $($tt:tt)*) => {
60+
if $machine::TRACING_ENABLED {
61+
$crate::interpret::tracing_utils::MaybeEnteredSpan::Some(tracing::info_span!($($tt)*).entered())
62+
} else {
63+
$crate::interpret::tracing_utils::MaybeEnteredSpan::None
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)