Skip to content

Commit 3eadd75

Browse files
committed
Auto merge of #50354 - varkor:initial-field-alignment-c-int, r=eddyb
Correct initial field alignment for repr(C)/repr(int) Fixes #50098 following #50098 (comment). (I wasn't sure which kind of test was best suited here — I picked run-pass simply because that was convenient, but if codegen is more appropriate, let me know and I'll change it.) r? @eddyb
2 parents d40a0b3 + 2d0d73e commit 3eadd75

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

src/librustc/ty/layout.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -940,11 +940,15 @@ impl<'a, 'tcx> LayoutCx<'tcx, TyCtxt<'a, 'tcx, 'tcx>> {
940940
// We increase the size of the discriminant to avoid LLVM copying
941941
// padding when it doesn't need to. This normally causes unaligned
942942
// load/stores and excessive memcpy/memset operations. By using a
943-
// bigger integer size, LLVM can be sure about it's contents and
943+
// bigger integer size, LLVM can be sure about its contents and
944944
// won't be so conservative.
945945

946946
// Use the initial field alignment
947-
let mut ity = Integer::for_abi_align(dl, start_align).unwrap_or(min_ity);
947+
let mut ity = if def.repr.c() || def.repr.int.is_some() {
948+
min_ity
949+
} else {
950+
Integer::for_abi_align(dl, start_align).unwrap_or(min_ity)
951+
};
948952

949953
// If the alignment is not larger than the chosen discriminant size,
950954
// don't use the alignment as the final size.

src/test/run-pass/repr_c_int_align.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -O
12+
13+
#![allow(dead_code)]
14+
15+
#[repr(C, u8)]
16+
enum ReprCu8 {
17+
A(u16),
18+
B,
19+
}
20+
21+
#[repr(u8)]
22+
enum Repru8 {
23+
A(u16),
24+
B,
25+
}
26+
27+
#[repr(C)]
28+
struct ReprC {
29+
tag: u8,
30+
padding: u8,
31+
payload: u16,
32+
}
33+
34+
fn main() {
35+
// Test `repr(C, u8)`.
36+
let r1 = ReprC { tag: 0, padding: 0, payload: 0 };
37+
let r2 = ReprC { tag: 0, padding: 1, payload: 1 };
38+
39+
let t1: &ReprCu8 = unsafe { std::mem::transmute(&r1) };
40+
let t2: &ReprCu8 = unsafe { std::mem::transmute(&r2) };
41+
42+
match (t1, t2) {
43+
(ReprCu8::A(_), ReprCu8::A(_)) => (),
44+
_ => assert!(false)
45+
};
46+
47+
// Test `repr(u8)`.
48+
let t1: &Repru8 = unsafe { std::mem::transmute(&r1) };
49+
let t2: &Repru8 = unsafe { std::mem::transmute(&r2) };
50+
51+
match (t1, t2) {
52+
(Repru8::A(_), Repru8::A(_)) => (),
53+
_ => assert!(false)
54+
};
55+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
// compile-pass
13+
14+
// This test makes sure that the tag is not grown for `repr(C)` or `repr(u8)`
15+
// variants (see https://github.com/rust-lang/rust/issues/50098 for the original bug).
16+
17+
#![feature(start)]
18+
#![allow(dead_code)]
19+
20+
#[repr(C, u8)]
21+
enum ReprCu8 {
22+
A(u16),
23+
B,
24+
}
25+
26+
#[repr(u8)]
27+
enum Repru8 {
28+
A(u16),
29+
B,
30+
}
31+
32+
#[start]
33+
fn start(_: isize, _: *const *const u8) -> isize {
34+
0
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
print-type-size type: `ReprCu8`: 4 bytes, alignment: 2 bytes
2+
print-type-size discriminant: 1 bytes
3+
print-type-size variant `A`: 3 bytes
4+
print-type-size padding: 1 bytes
5+
print-type-size field `.0`: 2 bytes, alignment: 2 bytes
6+
print-type-size variant `B`: 1 bytes
7+
print-type-size type: `Repru8`: 4 bytes, alignment: 2 bytes
8+
print-type-size discriminant: 1 bytes
9+
print-type-size variant `A`: 3 bytes
10+
print-type-size padding: 1 bytes
11+
print-type-size field `.0`: 2 bytes, alignment: 2 bytes
12+
print-type-size variant `B`: 0 bytes

0 commit comments

Comments
 (0)