mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-14 21:16:50 +00:00
Rollup merge of #107533 - pnkfelix:distinguish-generator-state-in-print-type-sizes, r=compiler-errors
Extend `-Z print-type-sizes` to distinguish generator upvars+locals from "normal" fields. For example, for this code: ```rust async fn wait() {} async fn test(arg: [u8; 8192]) { wait().await; drop(arg); } async fn test_ideal(_rg: [u8; 8192]) { wait().await; // drop(arg); } fn main() { let gen_t = test([0; 8192]); let gen_i = test_ideal([0; 8192]); println!("expect {}, got: {}", std::mem::size_of_val(&gen_i), std::mem::size_of_val(&gen_t)); } ``` the `-Z print-type-sizes` output used to start with: ``` print-type-size type: `[async fn body@issue-62958-a.rs:3:32: 6:2]`: 16386 bytes, alignment: 1 bytes print-type-size discriminant: 1 bytes print-type-size variant `Suspend0`: 16385 bytes print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes print-type-size field `.arg`: 8192 bytes print-type-size field `.__awaitee`: 1 bytes ... print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes print-type-size field `.value`: 8192 bytes ... ``` but with this change, it now instead prints: ``` print-type-size type: `[async fn body@issue-62958-a.rs:3:32: 6:2]`: 16386 bytes, alignment: 1 bytes print-type-size discriminant: 1 bytes print-type-size variant `Suspend0`: 16385 bytes print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes print-type-size local `.arg`: 8192 bytes print-type-size local `.__awaitee`: 1 bytes ... print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes print-type-size field `.value`: 8192 bytes ``` (spawned off of investigation of https://github.com/rust-lang/rust/issues/62958 )
This commit is contained in:
commit
f41f154dfb
@ -19,8 +19,26 @@ pub enum SizeKind {
|
||||
Min,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub enum FieldKind {
|
||||
AdtField,
|
||||
Upvar,
|
||||
GeneratorLocal,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for FieldKind {
|
||||
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
FieldKind::AdtField => write!(w, "field"),
|
||||
FieldKind::Upvar => write!(w, "upvar"),
|
||||
FieldKind::GeneratorLocal => write!(w, "local"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub struct FieldInfo {
|
||||
pub kind: FieldKind,
|
||||
pub name: Symbol,
|
||||
pub offset: u64,
|
||||
pub size: u64,
|
||||
@ -145,7 +163,7 @@ impl CodeStats {
|
||||
fields.sort_by_key(|f| (f.offset, f.size));
|
||||
|
||||
for field in fields {
|
||||
let FieldInfo { ref name, offset, size, align } = field;
|
||||
let FieldInfo { kind, ref name, offset, size, align } = field;
|
||||
|
||||
if offset > min_offset {
|
||||
let pad = offset - min_offset;
|
||||
@ -155,16 +173,16 @@ impl CodeStats {
|
||||
if offset < min_offset {
|
||||
// If this happens it's probably a union.
|
||||
println!(
|
||||
"print-type-size {indent}field `.{name}`: {size} bytes, \
|
||||
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
|
||||
offset: {offset} bytes, \
|
||||
alignment: {align} bytes"
|
||||
);
|
||||
} else if info.packed || offset == min_offset {
|
||||
println!("print-type-size {indent}field `.{name}`: {size} bytes");
|
||||
println!("print-type-size {indent}{kind} `.{name}`: {size} bytes");
|
||||
} else {
|
||||
// Include field alignment in output only if it caused padding injection
|
||||
println!(
|
||||
"print-type-size {indent}field `.{name}`: {size} bytes, \
|
||||
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
|
||||
alignment: {align} bytes"
|
||||
);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::cgu_reuse_tracker::CguReuseTracker;
|
||||
use crate::code_stats::CodeStats;
|
||||
pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
|
||||
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
|
||||
use crate::config::Input;
|
||||
use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath};
|
||||
use crate::errors::{
|
||||
|
@ -9,7 +9,7 @@ use rustc_middle::ty::layout::{
|
||||
use rustc_middle::ty::{
|
||||
self, subst::SubstsRef, AdtDef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitable,
|
||||
};
|
||||
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
|
||||
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
|
||||
use rustc_span::symbol::Symbol;
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_target::abi::*;
|
||||
@ -881,6 +881,7 @@ fn variant_info_for_adt<'tcx>(
|
||||
let offset = layout.fields.offset(i);
|
||||
min_size = min_size.max(offset + field_layout.size);
|
||||
FieldInfo {
|
||||
kind: FieldKind::AdtField,
|
||||
name,
|
||||
offset: offset.bytes(),
|
||||
size: field_layout.size.bytes(),
|
||||
@ -960,6 +961,7 @@ fn variant_info_for_generator<'tcx>(
|
||||
let offset = layout.fields.offset(field_idx);
|
||||
upvars_size = upvars_size.max(offset + field_layout.size);
|
||||
FieldInfo {
|
||||
kind: FieldKind::Upvar,
|
||||
name: Symbol::intern(&name),
|
||||
offset: offset.bytes(),
|
||||
size: field_layout.size.bytes(),
|
||||
@ -983,6 +985,7 @@ fn variant_info_for_generator<'tcx>(
|
||||
// The struct is as large as the last field's end
|
||||
variant_size = variant_size.max(offset + field_layout.size);
|
||||
FieldInfo {
|
||||
kind: FieldKind::GeneratorLocal,
|
||||
name: state_specific_names.get(*local).copied().flatten().unwrap_or(
|
||||
Symbol::intern(&format!(".generator_field{}", local.as_usize())),
|
||||
),
|
||||
|
@ -1,15 +1,15 @@
|
||||
print-type-size type: `[async fn body@$DIR/async.rs:8:36: 11:2]`: 16386 bytes, alignment: 1 bytes
|
||||
print-type-size discriminant: 1 bytes
|
||||
print-type-size variant `Suspend0`: 16385 bytes
|
||||
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size field `.arg`: 8192 bytes
|
||||
print-type-size field `.__awaitee`: 1 bytes
|
||||
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size local `.arg`: 8192 bytes
|
||||
print-type-size local `.__awaitee`: 1 bytes
|
||||
print-type-size variant `Unresumed`: 8192 bytes
|
||||
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size variant `Returned`: 8192 bytes
|
||||
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size variant `Panicked`: 8192 bytes
|
||||
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
|
||||
print-type-size field `.value`: 8192 bytes
|
||||
print-type-size type: `std::mem::MaybeUninit<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
|
||||
|
@ -1,10 +1,10 @@
|
||||
print-type-size type: `[generator@$DIR/generator.rs:10:5: 10:14]`: 8193 bytes, alignment: 1 bytes
|
||||
print-type-size discriminant: 1 bytes
|
||||
print-type-size variant `Unresumed`: 8192 bytes
|
||||
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size variant `Returned`: 8192 bytes
|
||||
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size variant `Panicked`: 8192 bytes
|
||||
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size variant `Suspend0`: 8192 bytes
|
||||
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
|
||||
|
@ -2,10 +2,10 @@ print-type-size type: `[generator@$DIR/generator_discr_placement.rs:11:13: 11:15
|
||||
print-type-size discriminant: 1 bytes
|
||||
print-type-size variant `Suspend0`: 7 bytes
|
||||
print-type-size padding: 3 bytes
|
||||
print-type-size field `.w`: 4 bytes, alignment: 4 bytes
|
||||
print-type-size local `.w`: 4 bytes, alignment: 4 bytes
|
||||
print-type-size variant `Suspend1`: 7 bytes
|
||||
print-type-size padding: 3 bytes
|
||||
print-type-size field `.z`: 4 bytes, alignment: 4 bytes
|
||||
print-type-size local `.z`: 4 bytes, alignment: 4 bytes
|
||||
print-type-size variant `Unresumed`: 0 bytes
|
||||
print-type-size variant `Returned`: 0 bytes
|
||||
print-type-size variant `Panicked`: 0 bytes
|
||||
|
Loading…
Reference in New Issue
Block a user