Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

layout: Avoid niches that make things worse. #63903

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 130 additions & 154 deletions src/librustc/ty/layout.rs

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/librustc_target/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,16 @@ impl Integer {
}
I8
}

/// Finds the largest integer with the given size or less.
pub fn approximate_size(wanted: Size) -> Option<Integer> {
for &candidate in &[I64, I32, I16, I8] {
if wanted >= candidate.size() {
return Some(candidate);
}
}
None
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/align-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum Align64 {
A(u32),
B(u32),
}
// CHECK: %Align64 = type { [0 x i32], i32, [15 x i32] }
// CHECK: %Align64 = type { [0 x i64], i64, [7 x i64] }

pub struct Nested64 {
a: u8,
Expand Down
11 changes: 10 additions & 1 deletion src/test/codegen/align-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ pub enum Enum64 {
A(Align64),
B(i32),
}
// CHECK: %Enum64 = type { [0 x i32], i32, [31 x i32] }
// CHECK: %Enum64 = type { [0 x i64], i64, [15 x i64] }
// CHECK: %"Enum64::A" = type { [8 x i64], %Align64, [0 x i64] }
// CHECK: %"Enum64::B" = type { [2 x i32], i32, [1 x i32] }

// CHECK-LABEL: @align64
#[no_mangle]
Expand Down Expand Up @@ -71,3 +72,11 @@ pub fn enum64(a: Align64) -> Enum64 {
let e64 = Enum64::A(a);
e64
}

// CHECK-LABEL: @enum64_b
#[no_mangle]
pub fn enum64_b(b: i32) -> Enum64 {
// CHECK: %e64 = alloca %Enum64, align 64
let e64 = Enum64::B(b);
e64
}
12 changes: 11 additions & 1 deletion src/test/ui/print_type_sizes/niche-filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<T> Default for MyOption<T> {

pub enum EmbeddedDiscr {
None,
Record { pre: u8, val: NonZeroU32, post: u16 },
Record { pre1: u8, pre2: u8, val: NonZeroU32, post: u16 },
}

impl Default for EmbeddedDiscr {
Expand All @@ -53,6 +53,15 @@ impl Default for NestedNonZero {
}
}

pub enum BadNiche {
None,
Record { pre: u8, val: NonZeroU32, post: u16 },
}

impl Default for BadNiche {
fn default() -> Self { BadNiche::None }
}

pub enum Enum4<A, B, C, D> {
One(A),
Two(B),
Expand All @@ -74,6 +83,7 @@ fn start(_: isize, _: *const *const u8) -> isize {
let _x: MyOption<NonZeroU32> = Default::default();
let _y: EmbeddedDiscr = Default::default();
let _z: MyOption<IndirectNonZero> = Default::default();
let _w: BadNiche = Default::default();
let _a: MyOption<bool> = Default::default();
let _b: MyOption<char> = Default::default();
let _c: MyOption<std::cmp::Ordering> = Default::default();
Expand Down
13 changes: 10 additions & 3 deletions src/test/ui/print_type_sizes/niche-filling.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ print-type-size type: `MyOption<IndirectNonZero>`: 12 bytes, alignment: 4 bytes
print-type-size variant `Some`: 12 bytes
print-type-size field `.0`: 12 bytes
print-type-size variant `None`: 0 bytes
print-type-size type: `EmbeddedDiscr`: 8 bytes, alignment: 4 bytes
print-type-size type: `BadNiche`: 8 bytes, alignment: 4 bytes
print-type-size discriminant: 1 bytes
print-type-size variant `Record`: 7 bytes
print-type-size field `.pre`: 1 bytes
print-type-size field `.post`: 2 bytes
print-type-size field `.val`: 4 bytes
print-type-size variant `None`: 0 bytes
print-type-size type: `EmbeddedDiscr`: 8 bytes, alignment: 4 bytes
print-type-size variant `Record`: 8 bytes
print-type-size field `.val`: 4 bytes
print-type-size field `.post`: 2 bytes
print-type-size field `.pre`: 1 bytes
print-type-size field `.pre1`: 1 bytes
print-type-size field `.pre2`: 1 bytes
print-type-size variant `None`: 0 bytes
print-type-size end padding: 1 bytes
print-type-size type: `MyOption<Union1<std::num::NonZeroU32>>`: 8 bytes, alignment: 4 bytes
print-type-size discriminant: 4 bytes
print-type-size variant `Some`: 4 bytes
Expand Down
22 changes: 10 additions & 12 deletions src/test/ui/print_type_sizes/padding.stdout
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
print-type-size type: `E1`: 12 bytes, alignment: 4 bytes
print-type-size discriminant: 1 bytes
print-type-size variant `B`: 11 bytes
print-type-size padding: 3 bytes
print-type-size field `.0`: 8 bytes, alignment: 4 bytes
print-type-size variant `A`: 7 bytes
print-type-size discriminant: 4 bytes
print-type-size variant `A`: 8 bytes
print-type-size field `.1`: 1 bytes
print-type-size padding: 2 bytes
print-type-size padding: 3 bytes
print-type-size field `.0`: 4 bytes, alignment: 4 bytes
print-type-size variant `B`: 8 bytes
print-type-size field `.0`: 8 bytes
print-type-size type: `E2`: 12 bytes, alignment: 4 bytes
print-type-size discriminant: 1 bytes
print-type-size variant `B`: 11 bytes
print-type-size padding: 3 bytes
print-type-size field `.0`: 8 bytes, alignment: 4 bytes
print-type-size variant `A`: 7 bytes
print-type-size discriminant: 4 bytes
print-type-size variant `A`: 8 bytes
print-type-size field `.0`: 1 bytes
print-type-size padding: 2 bytes
print-type-size padding: 3 bytes
print-type-size field `.1`: 4 bytes, alignment: 4 bytes
print-type-size variant `B`: 8 bytes
print-type-size field `.0`: 8 bytes
print-type-size type: `S`: 8 bytes, alignment: 4 bytes
print-type-size field `.g`: 4 bytes
print-type-size field `.a`: 1 bytes
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/print_type_sizes/repr-align.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
print-type-size type: `E`: 32 bytes, alignment: 16 bytes
print-type-size discriminant: 4 bytes
print-type-size variant `B`: 28 bytes
print-type-size padding: 12 bytes
print-type-size discriminant: 8 bytes
print-type-size variant `B`: 24 bytes
print-type-size padding: 8 bytes
print-type-size field `.0`: 16 bytes, alignment: 16 bytes
print-type-size variant `A`: 4 bytes
print-type-size field `.0`: 4 bytes
Expand Down