Skip to content

Commit 099e2a6

Browse files
committed
Implement repr(packed) for repr(simd)
1 parent 54b0434 commit 099e2a6

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::value::Value;
1010
use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh, wants_wasm_eh};
1111
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
1212
use rustc_codegen_ssa::errors::{ExpectedPointerMutability, InvalidMonomorphization};
13-
use rustc_codegen_ssa::mir::operand::OperandRef;
13+
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1414
use rustc_codegen_ssa::mir::place::PlaceRef;
1515
use rustc_codegen_ssa::traits::*;
1616
use rustc_hir as hir;
@@ -945,6 +945,13 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
945945
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), callee_ty.fn_sig(tcx));
946946
let arg_tys = sig.inputs();
947947

948+
// Vectors must be immediates (non-power-of-2 #[repr(packed)] are not)
949+
for (ty, arg) in arg_tys.iter().zip(args) {
950+
if ty.is_simd() && !matches!(arg.val, OperandValue::Immediate(_)) {
951+
return_error!(InvalidMonomorphization::SimdArgument { span, name, ty: *ty });
952+
}
953+
}
954+
948955
if name == sym::simd_select_bitmask {
949956
require_simd!(
950957
arg_tys[1],

compiler/rustc_ty_utils/src/layout.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,21 @@ fn layout_of_uncached<'tcx>(
431431
.size
432432
.checked_mul(e_len, dl)
433433
.ok_or_else(|| error(cx, LayoutError::SizeOverflow(ty)))?;
434-
let align = dl.vector_align(size);
434+
435+
let (abi, align) = if def.repr().packed() && !e_len.is_power_of_two() {
436+
// Non-power-of-two vectors have padding up to the next power-of-two.
437+
// If we're a packed repr, remove the padding while keeping the alignment as close
438+
// to a vector as possible.
439+
(
440+
Abi::Aggregate { sized: true },
441+
AbiAndPrefAlign {
442+
abi: Align::max_for_offset(size),
443+
pref: dl.vector_align(size).pref,
444+
},
445+
)
446+
} else {
447+
(Abi::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
448+
};
435449
let size = size.align_to(align.abi);
436450

437451
// Compute the placement of the vector fields:
@@ -444,7 +458,7 @@ fn layout_of_uncached<'tcx>(
444458
tcx.mk_layout(LayoutS {
445459
variants: Variants::Single { index: FIRST_VARIANT },
446460
fields,
447-
abi: Abi::Vector { element: e_abi, count: e_len },
461+
abi,
448462
largest_niche: e_ly.largest_niche,
449463
size,
450464
align,

tests/ui/simd/repr_packed.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// run-pass
2+
3+
#![feature(repr_simd, platform_intrinsics)]
4+
#![allow(non_camel_case_types)]
5+
6+
#[repr(simd, packed)]
7+
struct Simd<T, const N: usize>([T; N]);
8+
9+
fn check_size_align<T, const N: usize>() {
10+
use std::mem;
11+
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
12+
assert_eq!(mem::size_of::<Simd<T, N>>() % mem::align_of::<Simd<T, N>>(), 0);
13+
}
14+
15+
fn check_ty<T>() {
16+
check_size_align::<T, 1>();
17+
check_size_align::<T, 2>();
18+
check_size_align::<T, 3>();
19+
check_size_align::<T, 4>();
20+
check_size_align::<T, 8>();
21+
check_size_align::<T, 9>();
22+
check_size_align::<T, 15>();
23+
}
24+
25+
extern "platform-intrinsic" {
26+
fn simd_add<T>(a: T, b: T) -> T;
27+
}
28+
29+
fn main() {
30+
check_ty::<u8>();
31+
check_ty::<i16>();
32+
check_ty::<u32>();
33+
check_ty::<i64>();
34+
check_ty::<usize>();
35+
check_ty::<f32>();
36+
check_ty::<f64>();
37+
38+
unsafe {
39+
// powers-of-two have no padding and work as usual
40+
// non-powers-of-two have padding and need to be expanded to full vectors
41+
let x: Simd<f64, 4> =
42+
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
43+
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
44+
}
45+
}

0 commit comments

Comments
 (0)