Skip to content

Commit f41f154

Browse files
authored
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 #62958 )
2 parents a37a59f + 362c4fa commit f41f154

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

compiler/rustc_session/src/code_stats.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ pub enum SizeKind {
1919
Min,
2020
}
2121

22+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
23+
pub enum FieldKind {
24+
AdtField,
25+
Upvar,
26+
GeneratorLocal,
27+
}
28+
29+
impl std::fmt::Display for FieldKind {
30+
fn fmt(&self, w: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31+
match self {
32+
FieldKind::AdtField => write!(w, "field"),
33+
FieldKind::Upvar => write!(w, "upvar"),
34+
FieldKind::GeneratorLocal => write!(w, "local"),
35+
}
36+
}
37+
}
38+
2239
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
2340
pub struct FieldInfo {
41+
pub kind: FieldKind,
2442
pub name: Symbol,
2543
pub offset: u64,
2644
pub size: u64,
@@ -145,7 +163,7 @@ impl CodeStats {
145163
fields.sort_by_key(|f| (f.offset, f.size));
146164

147165
for field in fields {
148-
let FieldInfo { ref name, offset, size, align } = field;
166+
let FieldInfo { kind, ref name, offset, size, align } = field;
149167

150168
if offset > min_offset {
151169
let pad = offset - min_offset;
@@ -155,16 +173,16 @@ impl CodeStats {
155173
if offset < min_offset {
156174
// If this happens it's probably a union.
157175
println!(
158-
"print-type-size {indent}field `.{name}`: {size} bytes, \
176+
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
159177
offset: {offset} bytes, \
160178
alignment: {align} bytes"
161179
);
162180
} else if info.packed || offset == min_offset {
163-
println!("print-type-size {indent}field `.{name}`: {size} bytes");
181+
println!("print-type-size {indent}{kind} `.{name}`: {size} bytes");
164182
} else {
165183
// Include field alignment in output only if it caused padding injection
166184
println!(
167-
"print-type-size {indent}field `.{name}`: {size} bytes, \
185+
"print-type-size {indent}{kind} `.{name}`: {size} bytes, \
168186
alignment: {align} bytes"
169187
);
170188
}

compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cgu_reuse_tracker::CguReuseTracker;
22
use crate::code_stats::CodeStats;
3-
pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
3+
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
44
use crate::config::Input;
55
use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath};
66
use crate::errors::{

compiler/rustc_ty_utils/src/layout.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::layout::{
99
use rustc_middle::ty::{
1010
self, subst::SubstsRef, AdtDef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitable,
1111
};
12-
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
12+
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
1313
use rustc_span::symbol::Symbol;
1414
use rustc_span::DUMMY_SP;
1515
use rustc_target::abi::*;
@@ -881,6 +881,7 @@ fn variant_info_for_adt<'tcx>(
881881
let offset = layout.fields.offset(i);
882882
min_size = min_size.max(offset + field_layout.size);
883883
FieldInfo {
884+
kind: FieldKind::AdtField,
884885
name,
885886
offset: offset.bytes(),
886887
size: field_layout.size.bytes(),
@@ -960,6 +961,7 @@ fn variant_info_for_generator<'tcx>(
960961
let offset = layout.fields.offset(field_idx);
961962
upvars_size = upvars_size.max(offset + field_layout.size);
962963
FieldInfo {
964+
kind: FieldKind::Upvar,
963965
name: Symbol::intern(&name),
964966
offset: offset.bytes(),
965967
size: field_layout.size.bytes(),
@@ -983,6 +985,7 @@ fn variant_info_for_generator<'tcx>(
983985
// The struct is as large as the last field's end
984986
variant_size = variant_size.max(offset + field_layout.size);
985987
FieldInfo {
988+
kind: FieldKind::GeneratorLocal,
986989
name: state_specific_names.get(*local).copied().flatten().unwrap_or(
987990
Symbol::intern(&format!(".generator_field{}", local.as_usize())),
988991
),

tests/ui/print_type_sizes/async.stdout

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
print-type-size type: `[async fn body@$DIR/async.rs:8:36: 11:2]`: 16386 bytes, alignment: 1 bytes
22
print-type-size discriminant: 1 bytes
33
print-type-size variant `Suspend0`: 16385 bytes
4-
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
5-
print-type-size field `.arg`: 8192 bytes
6-
print-type-size field `.__awaitee`: 1 bytes
4+
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
5+
print-type-size local `.arg`: 8192 bytes
6+
print-type-size local `.__awaitee`: 1 bytes
77
print-type-size variant `Unresumed`: 8192 bytes
8-
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
8+
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
99
print-type-size variant `Returned`: 8192 bytes
10-
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
10+
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
1111
print-type-size variant `Panicked`: 8192 bytes
12-
print-type-size field `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
12+
print-type-size upvar `.arg`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
1313
print-type-size type: `std::mem::ManuallyDrop<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
1414
print-type-size field `.value`: 8192 bytes
1515
print-type-size type: `std::mem::MaybeUninit<[u8; 8192]>`: 8192 bytes, alignment: 1 bytes
+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
print-type-size type: `[generator@$DIR/generator.rs:10:5: 10:14]`: 8193 bytes, alignment: 1 bytes
22
print-type-size discriminant: 1 bytes
33
print-type-size variant `Unresumed`: 8192 bytes
4-
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
4+
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
55
print-type-size variant `Returned`: 8192 bytes
6-
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
6+
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
77
print-type-size variant `Panicked`: 8192 bytes
8-
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
8+
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
99
print-type-size variant `Suspend0`: 8192 bytes
10-
print-type-size field `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes
10+
print-type-size upvar `.array`: 8192 bytes, offset: 0 bytes, alignment: 1 bytes

tests/ui/print_type_sizes/generator_discr_placement.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ print-type-size type: `[generator@$DIR/generator_discr_placement.rs:11:13: 11:15
22
print-type-size discriminant: 1 bytes
33
print-type-size variant `Suspend0`: 7 bytes
44
print-type-size padding: 3 bytes
5-
print-type-size field `.w`: 4 bytes, alignment: 4 bytes
5+
print-type-size local `.w`: 4 bytes, alignment: 4 bytes
66
print-type-size variant `Suspend1`: 7 bytes
77
print-type-size padding: 3 bytes
8-
print-type-size field `.z`: 4 bytes, alignment: 4 bytes
8+
print-type-size local `.z`: 4 bytes, alignment: 4 bytes
99
print-type-size variant `Unresumed`: 0 bytes
1010
print-type-size variant `Returned`: 0 bytes
1111
print-type-size variant `Panicked`: 0 bytes

0 commit comments

Comments
 (0)