Skip to content

Commit 1d68600

Browse files
Revert "Compare tagged/niche-filling layout and pick the best one"
1 parent 8ad7bc3 commit 1d68600

File tree

4 files changed

+7
-51
lines changed

4 files changed

+7
-51
lines changed

src/librustc_middle/lib.rs

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

src/librustc_middle/ty/layout.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,6 @@ 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-
881879
// Niche-filling enum optimization.
882880
if !def.repr.inhibit_enum_layout_opt() && no_explicit_discriminants {
883881
let mut dataful_variant = None;
@@ -974,7 +972,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
974972
let largest_niche =
975973
Niche::from_scalar(dl, offset, niche_scalar.clone());
976974

977-
niche_filling_layout = Some(Layout {
975+
return Ok(tcx.intern_layout(Layout {
978976
variants: Variants::Multiple {
979977
tag: niche_scalar,
980978
tag_encoding: TagEncoding::Niche {
@@ -993,7 +991,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
993991
largest_niche,
994992
size,
995993
align,
996-
});
994+
}));
997995
}
998996
}
999997
}
@@ -1216,7 +1214,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12161214

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

1219-
let tagged_layout = Layout {
1217+
tcx.intern_layout(Layout {
12201218
variants: Variants::Multiple {
12211219
tag,
12221220
tag_encoding: TagEncoding::Direct,
@@ -1231,23 +1229,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12311229
abi,
12321230
align,
12331231
size,
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)
1232+
})
12511233
}
12521234

12531235
// 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
1211
print-type-size variant `Record`: 7 bytes
13-
print-type-size field `.pre`: 1 bytes
14-
print-type-size field `.post`: 2 bytes
1512
print-type-size field `.val`: 4 bytes
13+
print-type-size field `.post`: 2 bytes
14+
print-type-size field `.pre`: 1 bytes
1615
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,7 +5,6 @@
55
#![feature(never_type)]
66

77
use std::mem::size_of;
8-
use std::num::NonZeroU8;
98

109
struct t {a: u8, b: i8}
1110
struct u {a: u8, b: i8, c: u8}
@@ -103,23 +102,6 @@ enum Option2<A, B> {
103102
None
104103
}
105104

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-
123105
pub fn main() {
124106
assert_eq!(size_of::<u8>(), 1 as usize);
125107
assert_eq!(size_of::<u32>(), 4 as usize);
@@ -163,11 +145,4 @@ pub fn main() {
163145
assert_eq!(size_of::<Option<Option<(&(), bool)>>>(), size_of::<(bool, &())>());
164146
assert_eq!(size_of::<Option<Option2<bool, &()>>>(), size_of::<(bool, &())>());
165147
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);
173148
}

0 commit comments

Comments
 (0)