Skip to content

Commit

Permalink
allow reordering of the last field of a MaybeUnsized struct if it's a…
Browse files Browse the repository at this point in the history
… ZST
  • Loading branch information
erikdesjardins committed Aug 30, 2020
1 parent d3b9ece commit e5d85f9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions compiler/rustc_middle/src/ty/layout.rs
Expand Up @@ -289,25 +289,32 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {

let optimize = !repr.inhibit_struct_field_reordering_opt();
if optimize {
let end =
if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
let optimizing = &mut inverse_memory_index[..end];
let field_align = |f: &TyAndLayout<'_>| {
if let Some(pack) = pack { f.align.abi.min(pack) } else { f.align.abi }
};
match kind {
StructKind::AlwaysSized | StructKind::MaybeUnsized => {
optimizing.sort_by_key(|&x| {
StructKind::AlwaysSized => {
inverse_memory_index.sort_by_key(|&x| {
// Place ZSTs first to avoid "interesting offsets",
// especially with only one or two non-ZST fields.
let f = &fields[x as usize];
(!f.is_zst(), cmp::Reverse(field_align(f)))
});
}
StructKind::MaybeUnsized => {
// Sort in descending alignment, except for the last field,
// which may be accessed through an unsized type.
inverse_memory_index[..fields.len() - 1]
.sort_by_key(|&x| cmp::Reverse(field_align(&fields[x as usize])));
// Place ZSTs first to avoid "interesting offsets".
// This will reorder the last field if it is a ZST, which is okay because
// there's nothing in memory that could be accessed through an unsized type.
inverse_memory_index.sort_by_key(|&x| !fields[x as usize].is_zst());
}
StructKind::Prefixed(..) => {
// Sort in ascending alignment so that the layout stay optimal
// regardless of the prefix
optimizing.sort_by_key(|&x| field_align(&fields[x as usize]));
inverse_memory_index.sort_by_key(|&x| field_align(&fields[x as usize]));
}
}
}
Expand Down

0 comments on commit e5d85f9

Please sign in to comment.