Skip to content

Commit aa667a5

Browse files
authored
Rollup merge of rust-lang#74069 - erikdesjardins:bad-niche, r=nikomatsakis
Compare tagged/niche-filling layout and pick the best one Finishes up rust-lang#71045, and so fixes rust-lang#63866. cc @eddyb r? @nikomatsakis (since @eddyb wrote the first commit)
2 parents aa944aa + 3924672 commit aa667a5

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/librustc_middle/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![feature(bool_to_option)]
2828
#![feature(box_patterns)]
2929
#![feature(box_syntax)]
30+
#![feature(cmp_min_max_by)]
3031
#![feature(const_fn)]
3132
#![feature(const_panic)]
3233
#![feature(const_fn_transmute)]

src/librustc_middle/ty/layout.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,8 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
876876
.iter_enumerated()
877877
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));
878878

879+
let mut niche_filling_layout = None;
880+
879881
// Niche-filling enum optimization.
880882
if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
881883
let mut dataful_variant = None;
@@ -972,7 +974,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
972974
let largest_niche =
973975
Niche::from_scalar(dl, offset, niche_scalar.clone());
974976

975-
return Ok(tcx.intern_layout(Layout {
977+
niche_filling_layout = Some(Layout {
976978
variants: Variants::Multiple {
977979
tag: niche_scalar,
978980
tag_encoding: TagEncoding::Niche {
@@ -991,7 +993,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
991993
largest_niche,
992994
size,
993995
align,
994-
}));
996+
});
995997
}
996998
}
997999
}
@@ -1214,7 +1216,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12141216

12151217
let largest_niche = Niche::from_scalar(dl, Size::ZERO, tag.clone());
12161218

1217-
tcx.intern_layout(Layout {
1219+
let tagged_layout = Layout {
12181220
variants: Variants::Multiple {
12191221
tag,
12201222
tag_encoding: TagEncoding::Direct,
@@ -1229,7 +1231,23 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12291231
abi,
12301232
align,
12311233
size,
1232-
})
1234+
};
1235+
1236+
let best_layout = match (tagged_layout, niche_filling_layout) {
1237+
(tagged_layout, Some(niche_filling_layout)) => {
1238+
// Pick the smaller layout; otherwise,
1239+
// pick the layout with the larger niche; otherwise,
1240+
// pick tagged as it has simpler codegen.
1241+
cmp::min_by_key(tagged_layout, niche_filling_layout, |layout| {
1242+
let niche_size =
1243+
layout.largest_niche.as_ref().map_or(0, |n| n.available(dl));
1244+
(layout.size, cmp::Reverse(niche_size))
1245+
})
1246+
}
1247+
(tagged_layout, None) => tagged_layout,
1248+
};
1249+
1250+
tcx.intern_layout(best_layout)
12331251
}
12341252

12351253
// Types with no meaningful known layout.

src/test/ui/print_type_sizes/niche-filling.stdout

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ print-type-size variant `Some`: 12 bytes
88
print-type-size field `.0`: 12 bytes
99
print-type-size variant `None`: 0 bytes
1010
print-type-size type: `EmbeddedDiscr`: 8 bytes, alignment: 4 bytes
11+
print-type-size discriminant: 1 bytes
1112
print-type-size variant `Record`: 7 bytes
12-
print-type-size field `.val`: 4 bytes
13-
print-type-size field `.post`: 2 bytes
1413
print-type-size field `.pre`: 1 bytes
14+
print-type-size field `.post`: 2 bytes
15+
print-type-size field `.val`: 4 bytes
1516
print-type-size variant `None`: 0 bytes
16-
print-type-size end padding: 1 bytes
1717
print-type-size type: `MyOption<Union1<std::num::NonZeroU32>>`: 8 bytes, alignment: 4 bytes
1818
print-type-size discriminant: 4 bytes
1919
print-type-size variant `Some`: 4 bytes

src/test/ui/type-sizes.rs

+25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(never_type)]
66

77
use std::mem::size_of;
8+
use std::num::NonZeroU8;
89

910
struct t {a: u8, b: i8}
1011
struct u {a: u8, b: i8, c: u8}
@@ -102,6 +103,23 @@ enum Option2<A, B> {
102103
None
103104
}
104105

106+
// Two layouts are considered for `CanBeNicheFilledButShouldnt`:
107+
// Niche-filling:
108+
// { u32 (4 bytes), NonZeroU8 + tag in niche (1 byte), padding (3 bytes) }
109+
// Tagged:
110+
// { tag (1 byte), NonZeroU8 (1 byte), padding (2 bytes), u32 (4 bytes) }
111+
// Both are the same size (due to padding),
112+
// but the tagged layout is better as the tag creates a niche with 254 invalid values,
113+
// allowing types like `Option<Option<CanBeNicheFilledButShouldnt>>` to fit into 8 bytes.
114+
pub enum CanBeNicheFilledButShouldnt {
115+
A(NonZeroU8, u32),
116+
B
117+
}
118+
pub enum AlwaysTaggedBecauseItHasNoNiche {
119+
A(u8, u32),
120+
B
121+
}
122+
105123
pub fn main() {
106124
assert_eq!(size_of::<u8>(), 1 as usize);
107125
assert_eq!(size_of::<u32>(), 4 as usize);
@@ -145,4 +163,11 @@ pub fn main() {
145163
assert_eq!(size_of::<Option<Option<(&(), bool)>>>(), size_of::<(bool, &())>());
146164
assert_eq!(size_of::<Option<Option2<bool, &()>>>(), size_of::<(bool, &())>());
147165
assert_eq!(size_of::<Option<Option2<&(), bool>>>(), size_of::<(bool, &())>());
166+
167+
assert_eq!(size_of::<CanBeNicheFilledButShouldnt>(), 8);
168+
assert_eq!(size_of::<Option<CanBeNicheFilledButShouldnt>>(), 8);
169+
assert_eq!(size_of::<Option<Option<CanBeNicheFilledButShouldnt>>>(), 8);
170+
assert_eq!(size_of::<AlwaysTaggedBecauseItHasNoNiche>(), 8);
171+
assert_eq!(size_of::<Option<AlwaysTaggedBecauseItHasNoNiche>>(), 8);
172+
assert_eq!(size_of::<Option<Option<AlwaysTaggedBecauseItHasNoNiche>>>(), 8);
148173
}

0 commit comments

Comments
 (0)