Skip to content

Commit 6d8f74d

Browse files
Rollup merge of #142721 - Stypox:tracing-layout-of, r=RalfJung
Add tracing to `InterpCx::layout_of()` This PR adds tracing calls to `instantiate_from_frame_and_normalize_erasing_regions` and to `InterpCx::layout_of()`. The latter is done by shadowing `LayoutOf`'s trait method with an inherent method on `InterpCx`. <details><summary>Previous attempt by overriding the `layout_of` query (includes downloadable `.diff` patch)</summary> This PR is meant for Miri, but requires a few changes in `rustc` code, hence why it's here. It adds tracing capabilities to the `layout_of` function in `tcx` by overriding the `layout_of` query (under `local_providers`) with a wrapper that opens a tracing span and then calls the actual `layout_of`. To make this possible, I had to make `rustc_ty_utils::layout::layout_of` public. I added an assert to ensure the `providers.layout_of` value I am replacing is actually `rustc_ty_utils::layout::layout_of`, just in case. I also considered taking the previous value in `providers.layout_of` and calling that one instead, to avoid making `layout_of` public. But then the closure would not be castable to a function pointer anymore (`providers.layout_of` is a function pointer), because it would depend on the local variable storing the previous value of `providers.layout_of`. Using a global variable would work but would rely on `unsafe` or on `Mutex`es, so I wanted to avoid it. Here is some tracing output when Miri is run on `src/tools/miri/tests/pass/hello.rs`, visualizable in https://ui.perfetto.dev: [trace-1750338860374637.json](https://github.com/user-attachments/files/20820392/trace-1750338860374637.json) Another place where I could have added tracing calls is to the `rustc_middle::ty::layout::LayoutCx` struct / `spanned_layout_of()` function, however there is no simple way to disable the tracing calls with compile-time boolean constants there (since `LayoutCx::new()` is used everywhere and referenced directly), and in any case it seems like `spanned_layout_of()` just calls `tcx.layout_of()` anyway. For completeness' sake, here is tracing output for when a tracing call is added to `spanned_layout_of()`: [trace-1750340887920584.json](https://github.com/user-attachments/files/20820609/trace-1750340887920584.json) Patch to override `layout_of` query: [tracing-layout_of-query-override.diff.txt](https://github.com/user-attachments/files/20944497/tracing-layout_of-query-override.diff.txt) </details> **Note: obtaining tracing output depends on rust-lang/miri#4406, but this PR is standalone and can be merged without waiting for rust-lang/miri#4406 r? `@RalfJung`
2 parents 80f20c9 + 708dc15 commit 6d8f74d

32 files changed

+37
-33
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::DefKind;
77
use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo, ReportedErrorInfo};
88
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
99
use rustc_middle::query::TyCtxtAt;
10-
use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
10+
use rustc_middle::ty::layout::HasTypingEnv;
1111
use rustc_middle::ty::print::with_no_trimmed_paths;
1212
use rustc_middle::ty::{self, Ty, TyCtxt};
1313
use rustc_middle::{bug, throw_inval};

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
use rustc_abi::{FieldIdx, VariantIdx};
44
use rustc_middle::query::Key;
5-
use rustc_middle::ty::layout::LayoutOf;
65
use rustc_middle::ty::{self, Ty, TyCtxt};
76
use rustc_middle::{bug, mir};
87
use tracing::instrument;

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_abi::{BackendRepr, FieldIdx, VariantIdx};
22
use rustc_data_structures::stack::ensure_sufficient_stack;
33
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId, ValTreeCreationError};
4-
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
4+
use rustc_middle::ty::layout::{LayoutCx, TyAndLayout};
55
use rustc_middle::ty::{self, Ty, TyCtxt};
66
use rustc_middle::{bug, mir};
77
use rustc_span::DUMMY_SP;

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::Cow;
66
use either::{Left, Right};
77
use rustc_abi::{self as abi, ExternAbi, FieldIdx, Integer, VariantIdx};
88
use rustc_hir::def_id::DefId;
9-
use rustc_middle::ty::layout::{FnAbiOf, IntegerExt, LayoutOf, TyAndLayout};
9+
use rustc_middle::ty::layout::{FnAbiOf, IntegerExt, TyAndLayout};
1010
use rustc_middle::ty::{self, AdtDef, Instance, Ty, VariantDef};
1111
use rustc_middle::{bug, mir, span_bug};
1212
use rustc_span::sym;

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_apfloat::{Float, FloatConvert};
66
use rustc_middle::mir::CastKind;
77
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
88
use rustc_middle::ty::adjustment::PointerCoercion;
9-
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, TyAndLayout};
9+
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
1010
use rustc_middle::ty::{self, FloatTy, Ty};
1111
use rustc_middle::{bug, span_bug};
1212
use tracing::trace;

compiler/rustc_const_eval/src/interpret/discriminant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functions for reading and writing discriminants of multi-variant layouts (enums and coroutines).
22
33
use rustc_abi::{self as abi, FieldIdx, TagEncoding, VariantIdx, Variants};
4-
use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
4+
use rustc_middle::ty::layout::{PrimitiveExt, TyAndLayout};
55
use rustc_middle::ty::{self, CoroutineArgsExt, ScalarInt, Ty};
66
use rustc_middle::{mir, span_bug};
77
use tracing::{instrument, trace};

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use rustc_hir::def_id::DefId;
77
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
88
use rustc_middle::query::TyCtxtAt;
99
use rustc_middle::ty::layout::{
10-
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers, TyAndLayout,
10+
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
11+
TyAndLayout,
1112
};
1213
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypingEnv, Variance};
1314
use rustc_middle::{mir, span_bug};
@@ -21,7 +22,7 @@ use super::{
2122
MemPlaceMeta, Memory, OpTy, Place, PlaceTy, PointerArithmetic, Projectable, Provenance,
2223
err_inval, interp_ok, throw_inval, throw_ub, throw_ub_custom,
2324
};
24-
use crate::{ReportErrorExt, fluent_generated as fluent, util};
25+
use crate::{ReportErrorExt, enter_trace_span, fluent_generated as fluent, util};
2526

2627
pub struct InterpCx<'tcx, M: Machine<'tcx>> {
2728
/// Stores the `Machine` instance.
@@ -91,6 +92,20 @@ impl<'tcx, M: Machine<'tcx>> LayoutOfHelpers<'tcx> for InterpCx<'tcx, M> {
9192
}
9293
}
9394

95+
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
96+
/// This inherent method takes priority over the trait method with the same name in LayoutOf,
97+
/// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
98+
/// See [LayoutOf::layout_of] for the original documentation.
99+
#[inline]
100+
pub fn layout_of(
101+
&self,
102+
ty: Ty<'tcx>,
103+
) -> <InterpCx<'tcx, M> as LayoutOfHelpers<'tcx>>::LayoutOfResult {
104+
let _span = enter_trace_span!(M, "InterpCx::layout_of", "ty = {:?}", ty.kind());
105+
LayoutOf::layout_of(self, ty)
106+
}
107+
}
108+
94109
impl<'tcx, M: Machine<'tcx>> FnAbiOfHelpers<'tcx> for InterpCx<'tcx, M> {
95110
type FnAbiOfResult = Result<&'tcx FnAbi<'tcx, Ty<'tcx>>, InterpErrorKind<'tcx>>;
96111

@@ -284,6 +299,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
284299
frame: &Frame<'tcx, M::Provenance, M::FrameExtra>,
285300
value: T,
286301
) -> Result<T, ErrorHandled> {
302+
let _span = enter_trace_span!(
303+
M,
304+
"instantiate_from_frame_and_normalize_erasing_regions",
305+
"{}",
306+
frame.instance
307+
);
287308
frame
288309
.instance
289310
.try_instantiate_mir_and_normalize_erasing_regions(

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_abi::Size;
88
use rustc_apfloat::ieee::{Double, Half, Quad, Single};
99
use rustc_hir::def_id::DefId;
1010
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic};
11-
use rustc_middle::ty::layout::{LayoutOf as _, TyAndLayout, ValidityRequirement};
11+
use rustc_middle::ty::layout::{TyAndLayout, ValidityRequirement};
1212
use rustc_middle::ty::{GenericArgsRef, Ty, TyCtxt};
1313
use rustc_middle::{bug, ty};
1414
use rustc_span::{Symbol, sym};

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_abi as abi;
88
use rustc_abi::{BackendRepr, HasDataLayout, Size};
99
use rustc_hir::def::Namespace;
1010
use rustc_middle::mir::interpret::ScalarSizeMismatch;
11-
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
11+
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, TyAndLayout};
1212
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
1313
use rustc_middle::ty::{ConstInt, ScalarInt, Ty, TyCtxt};
1414
use rustc_middle::{bug, mir, span_bug, ty};

compiler/rustc_const_eval/src/interpret/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_abi::Size;
33
use rustc_apfloat::{Float, FloatConvert};
44
use rustc_middle::mir::NullOp;
55
use rustc_middle::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
6-
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
6+
use rustc_middle::ty::layout::TyAndLayout;
77
use rustc_middle::ty::{self, FloatTy, ScalarInt, Ty};
88
use rustc_middle::{bug, mir, span_bug};
99
use rustc_span::sym;

0 commit comments

Comments
 (0)