2019-12-22 16:42:04 -06:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
|
|
|
use rustc_data_structures::sync::Lock;
|
2018-09-08 17:16:45 -05:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2016-11-15 10:48:07 -06:00
|
|
|
use std::cmp::{self, Ordering};
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct VariantInfo {
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub kind: SizeKind,
|
|
|
|
pub size: u64,
|
|
|
|
pub align: u64,
|
|
|
|
pub fields: Vec<FieldInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2017-03-12 19:12:13 -05:00
|
|
|
pub enum SizeKind {
|
|
|
|
Exact,
|
|
|
|
Min,
|
|
|
|
}
|
2016-11-15 10:48:07 -06:00
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct FieldInfo {
|
|
|
|
pub name: String,
|
|
|
|
pub offset: u64,
|
|
|
|
pub size: u64,
|
|
|
|
pub align: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum DataTypeKind {
|
|
|
|
Struct,
|
|
|
|
Union,
|
|
|
|
Enum,
|
|
|
|
Closure,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub struct TypeSizeInfo {
|
|
|
|
pub kind: DataTypeKind,
|
|
|
|
pub type_description: String,
|
|
|
|
pub align: u64,
|
|
|
|
pub overall_size: u64,
|
2018-02-04 05:10:28 -06:00
|
|
|
pub packed: bool,
|
2016-11-15 10:48:07 -06:00
|
|
|
pub opt_discr_size: Option<u64>,
|
|
|
|
pub variants: Vec<VariantInfo>,
|
|
|
|
}
|
|
|
|
|
2019-11-10 10:48:47 -06:00
|
|
|
#[derive(Default)]
|
2016-11-15 10:48:07 -06:00
|
|
|
pub struct CodeStats {
|
2019-11-10 10:48:47 -06:00
|
|
|
type_sizes: Lock<FxHashSet<TypeSizeInfo>>,
|
2016-11-15 10:48:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CodeStats {
|
2019-12-22 16:42:04 -06:00
|
|
|
pub fn record_type_size<S: ToString>(
|
|
|
|
&self,
|
|
|
|
kind: DataTypeKind,
|
|
|
|
type_desc: S,
|
|
|
|
align: Align,
|
|
|
|
overall_size: Size,
|
|
|
|
packed: bool,
|
|
|
|
opt_discr_size: Option<Size>,
|
|
|
|
mut variants: Vec<VariantInfo>,
|
|
|
|
) {
|
2019-04-11 21:18:23 -05:00
|
|
|
// Sort variants so the largest ones are shown first. A stable sort is
|
|
|
|
// used here so that source code order is preserved for all variants
|
|
|
|
// that have the same size.
|
2019-12-22 16:42:04 -06:00
|
|
|
variants.sort_by(|info1, info2| info2.size.cmp(&info1.size));
|
2016-11-15 10:48:07 -06:00
|
|
|
let info = TypeSizeInfo {
|
2017-07-03 13:19:51 -05:00
|
|
|
kind,
|
2016-11-15 10:48:07 -06:00
|
|
|
type_description: type_desc.to_string(),
|
2018-09-08 17:16:45 -05:00
|
|
|
align: align.bytes(),
|
2016-11-15 10:48:07 -06:00
|
|
|
overall_size: overall_size.bytes(),
|
2018-02-04 05:10:28 -06:00
|
|
|
packed: packed,
|
2016-11-15 10:48:07 -06:00
|
|
|
opt_discr_size: opt_discr_size.map(|s| s.bytes()),
|
2017-07-03 13:19:51 -05:00
|
|
|
variants,
|
2016-11-15 10:48:07 -06:00
|
|
|
};
|
2019-11-10 10:48:47 -06:00
|
|
|
self.type_sizes.borrow_mut().insert(info);
|
2016-11-15 10:48:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_type_sizes(&self) {
|
2019-11-10 10:48:47 -06:00
|
|
|
let type_sizes = self.type_sizes.borrow();
|
|
|
|
let mut sorted: Vec<_> = type_sizes.iter().collect();
|
2016-11-15 10:48:07 -06:00
|
|
|
|
|
|
|
// Primary sort: large-to-small.
|
|
|
|
// Secondary sort: description (dictionary order)
|
|
|
|
sorted.sort_by(|info1, info2| {
|
|
|
|
// (reversing cmp order to get large-to-small ordering)
|
|
|
|
match info2.overall_size.cmp(&info1.overall_size) {
|
|
|
|
Ordering::Equal => info1.type_description.cmp(&info2.type_description),
|
|
|
|
other => other,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for info in &sorted {
|
2019-12-22 16:42:04 -06:00
|
|
|
println!(
|
|
|
|
"print-type-size type: `{}`: {} bytes, alignment: {} bytes",
|
|
|
|
info.type_description, info.overall_size, info.align
|
|
|
|
);
|
2016-11-15 10:48:07 -06:00
|
|
|
let indent = " ";
|
|
|
|
|
|
|
|
let discr_size = if let Some(discr_size) = info.opt_discr_size {
|
2019-12-22 16:42:04 -06:00
|
|
|
println!("print-type-size {}discriminant: {} bytes", indent, discr_size);
|
2016-11-15 10:48:07 -06:00
|
|
|
discr_size
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
// We start this at discr_size (rather than 0) because
|
|
|
|
// things like C-enums do not have variants but we still
|
|
|
|
// want the max_variant_size at the end of the loop below
|
|
|
|
// to reflect the presence of the discriminant.
|
|
|
|
let mut max_variant_size = discr_size;
|
|
|
|
|
|
|
|
let struct_like = match info.kind {
|
|
|
|
DataTypeKind::Struct | DataTypeKind::Closure => true,
|
|
|
|
DataTypeKind::Enum | DataTypeKind::Union => false,
|
|
|
|
};
|
|
|
|
for (i, variant_info) in info.variants.iter().enumerate() {
|
|
|
|
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
|
|
|
|
let indent = if !struct_like {
|
|
|
|
let name = match name.as_ref() {
|
2018-07-27 04:11:18 -05:00
|
|
|
Some(name) => name.to_owned(),
|
|
|
|
None => i.to_string(),
|
2016-11-15 10:48:07 -06:00
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
println!(
|
|
|
|
"print-type-size {}variant `{}`: {} bytes",
|
|
|
|
indent,
|
|
|
|
name,
|
|
|
|
size - discr_size
|
|
|
|
);
|
2016-11-15 10:48:07 -06:00
|
|
|
" "
|
|
|
|
} else {
|
|
|
|
assert!(i < 1);
|
|
|
|
" "
|
|
|
|
};
|
|
|
|
max_variant_size = cmp::max(max_variant_size, size);
|
|
|
|
|
|
|
|
let mut min_offset = discr_size;
|
2016-11-26 13:37:15 -06:00
|
|
|
|
Fix `-Z print-type-sizes`'s handling of zero-sized fields.
Currently, the type `struct S { x: u32, y: u32, tag: () }` is
incorrectly described like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.tag`: 0 bytes, offset: 0 bytes, alignment: 1 bytes
print-type-size padding: 4 bytes
print-type-size field `.y`: 4 bytes, alignment: 4 bytes
```
Specifically:
- The `padding` line is wrong. (There is no padding.)
- The `offset` and `alignment` on the `.tag` line shouldn't be printed.
The problem is that multiple fields can end up with the same offset, and
the printing code doesn't handle this correctly.
This commit fixes it by adjusting the field sorting so that zero-sized fields
are dealt with before non-zero-sized fields. With that in place, the
printing code works correctly.
The commit also corrects the "something is very wrong" comment.
The new output looks like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.tag`: 0 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.y`: 4 bytes
```
2019-12-10 21:17:41 -06:00
|
|
|
// We want to print fields by increasing offset. We also want
|
|
|
|
// zero-sized fields before non-zero-sized fields, otherwise
|
|
|
|
// the loop below goes wrong; hence the `f.size` in the sort
|
|
|
|
// key.
|
2016-11-26 13:37:15 -06:00
|
|
|
let mut fields = fields.clone();
|
Fix `-Z print-type-sizes`'s handling of zero-sized fields.
Currently, the type `struct S { x: u32, y: u32, tag: () }` is
incorrectly described like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.tag`: 0 bytes, offset: 0 bytes, alignment: 1 bytes
print-type-size padding: 4 bytes
print-type-size field `.y`: 4 bytes, alignment: 4 bytes
```
Specifically:
- The `padding` line is wrong. (There is no padding.)
- The `offset` and `alignment` on the `.tag` line shouldn't be printed.
The problem is that multiple fields can end up with the same offset, and
the printing code doesn't handle this correctly.
This commit fixes it by adjusting the field sorting so that zero-sized fields
are dealt with before non-zero-sized fields. With that in place, the
printing code works correctly.
The commit also corrects the "something is very wrong" comment.
The new output looks like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.tag`: 0 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.y`: 4 bytes
```
2019-12-10 21:17:41 -06:00
|
|
|
fields.sort_by_key(|f| (f.offset, f.size));
|
2016-11-26 13:37:15 -06:00
|
|
|
|
|
|
|
for field in fields.iter() {
|
2016-11-15 10:48:07 -06:00
|
|
|
let FieldInfo { ref name, offset, size, align } = *field;
|
|
|
|
|
2018-02-04 05:10:28 -06:00
|
|
|
if offset > min_offset {
|
|
|
|
let pad = offset - min_offset;
|
2019-12-22 16:42:04 -06:00
|
|
|
println!("print-type-size {}padding: {} bytes", indent, pad);
|
2018-02-04 05:10:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset < min_offset {
|
Fix `-Z print-type-sizes`'s handling of zero-sized fields.
Currently, the type `struct S { x: u32, y: u32, tag: () }` is
incorrectly described like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.tag`: 0 bytes, offset: 0 bytes, alignment: 1 bytes
print-type-size padding: 4 bytes
print-type-size field `.y`: 4 bytes, alignment: 4 bytes
```
Specifically:
- The `padding` line is wrong. (There is no padding.)
- The `offset` and `alignment` on the `.tag` line shouldn't be printed.
The problem is that multiple fields can end up with the same offset, and
the printing code doesn't handle this correctly.
This commit fixes it by adjusting the field sorting so that zero-sized fields
are dealt with before non-zero-sized fields. With that in place, the
printing code works correctly.
The commit also corrects the "something is very wrong" comment.
The new output looks like this:
```
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.tag`: 0 bytes
print-type-size field `.x`: 4 bytes
print-type-size field `.y`: 4 bytes
```
2019-12-10 21:17:41 -06:00
|
|
|
// If this happens it's probably a union.
|
2019-12-22 16:42:04 -06:00
|
|
|
println!(
|
|
|
|
"print-type-size {}field `.{}`: {} bytes, \
|
2018-02-04 05:10:28 -06:00
|
|
|
offset: {} bytes, \
|
|
|
|
alignment: {} bytes",
|
2019-12-22 16:42:04 -06:00
|
|
|
indent, name, size, offset, align
|
|
|
|
);
|
2018-02-04 05:10:28 -06:00
|
|
|
} else if info.packed || offset == min_offset {
|
2019-12-22 16:42:04 -06:00
|
|
|
println!("print-type-size {}field `.{}`: {} bytes", indent, name, size);
|
2018-02-04 05:10:28 -06:00
|
|
|
} else {
|
|
|
|
// Include field alignment in output only if it caused padding injection
|
2019-12-22 16:42:04 -06:00
|
|
|
println!(
|
|
|
|
"print-type-size {}field `.{}`: {} bytes, \
|
2018-02-04 05:10:28 -06:00
|
|
|
alignment: {} bytes",
|
2019-12-22 16:42:04 -06:00
|
|
|
indent, name, size, align
|
|
|
|
);
|
2016-11-15 10:48:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
min_offset = offset + size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
assert!(
|
|
|
|
max_variant_size <= info.overall_size,
|
|
|
|
"max_variant_size {} !<= {} overall_size",
|
|
|
|
max_variant_size,
|
|
|
|
info.overall_size
|
|
|
|
);
|
2016-11-15 10:48:07 -06:00
|
|
|
if max_variant_size < info.overall_size {
|
2019-12-22 16:42:04 -06:00
|
|
|
println!(
|
|
|
|
"print-type-size {}end padding: {} bytes",
|
|
|
|
indent,
|
|
|
|
info.overall_size - max_variant_size
|
|
|
|
);
|
2016-11-15 10:48:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|