Skip to content

Commit af60458

Browse files
compiler: Fix handling of repr(align(N), simd)
The attributes may not be sensible to combine in user code, but they should still work.
1 parent e2dc1a1 commit af60458

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

compiler/rustc_ty_utils/src/layout.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ fn layout_of_uncached<'tcx>(
506506
.checked_mul(e_len, dl)
507507
.ok_or_else(|| error(cx, LayoutError::SizeOverflow(ty)))?;
508508

509+
let requested_align = def.repr().align.clone();
510+
509511
let (abi, align) = if def.repr().packed() && !e_len.is_power_of_two() {
510512
// Non-power-of-two vectors have padding up to the next power-of-two.
511513
// If we're a packed repr, remove the padding while keeping the alignment as close
@@ -518,7 +520,13 @@ fn layout_of_uncached<'tcx>(
518520
},
519521
)
520522
} else {
521-
(Abi::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
523+
let natural_align = dl.vector_align(size);
524+
let align = if let Some(align) = requested_align {
525+
natural_align.max(AbiAndPrefAlign::new(align))
526+
} else {
527+
natural_align
528+
};
529+
(Abi::Vector { element: e_abi, count: e_len }, align)
522530
};
523531
let size = size.align_to(align.abi);
524532

@@ -536,7 +544,7 @@ fn layout_of_uncached<'tcx>(
536544
largest_niche: e_ly.largest_niche,
537545
size,
538546
align,
539-
max_repr_align: None,
547+
max_repr_align: requested_align,
540548
unadjusted_abi_align: align.abi,
541549
})
542550
}

tests/ui/simd/repr-align-and-simd.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ run-pass
2+
// A regression test for https://github.com/rust-lang/rust/issues/130402
3+
// Our SIMD representation did not combine correctly with the repr(align) attribute,
4+
// and this will remain a concern regardless of what we do with SIMD types.
5+
#![feature(repr_simd)]
6+
use std::mem::{size_of, align_of};
7+
8+
#[repr(simd, align(64))]
9+
struct IntelsIdeaOfWhatAvx512Means([u8; 32]);
10+
11+
#[repr(transparent)]
12+
struct DesignValidation(IntelsIdeaOfWhatAvx512Means);
13+
14+
fn main() {
15+
assert_eq!(64, size_of::<IntelsIdeaOfWhatAvx512Means>());
16+
assert_eq!(64, align_of::<IntelsIdeaOfWhatAvx512Means>());
17+
assert_eq!(64, size_of::<DesignValidation>());
18+
assert_eq!(64, align_of::<DesignValidation>());
19+
}

0 commit comments

Comments
 (0)