Skip to content

Commit 8b1ba11

Browse files
committed
Auto merge of #117116 - calebzulawski:repr-simd-packed, r=workingjubilee
Implement repr(packed) for repr(simd) This allows creating vectors with non-power-of-2 lengths that do not have padding. See rust-lang/portable-simd#319
2 parents c13187c + aa00bae commit 8b1ba11

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+8-1
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;
@@ -946,6 +946,13 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
946946
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), callee_ty.fn_sig(tcx));
947947
let arg_tys = sig.inputs();
948948

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

compiler/rustc_ty_utils/src/layout.rs

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

439453
// Compute the placement of the vector fields:
@@ -446,7 +460,7 @@ fn layout_of_uncached<'tcx>(
446460
tcx.mk_layout(LayoutS {
447461
variants: Variants::Single { index: FIRST_VARIANT },
448462
fields,
449-
abi: Abi::Vector { element: e_abi, count: e_len },
463+
abi,
450464
largest_niche: e_ly.largest_niche,
451465
size,
452466
align,

tests/ui/simd/repr_packed.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#[repr(simd)]
10+
struct FullSimd<T, const N: usize>([T; N]);
11+
12+
fn check_size_align<T, const N: usize>() {
13+
use std::mem;
14+
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
15+
assert_eq!(mem::size_of::<Simd<T, N>>() % mem::align_of::<Simd<T, N>>(), 0);
16+
}
17+
18+
fn check_ty<T>() {
19+
check_size_align::<T, 1>();
20+
check_size_align::<T, 2>();
21+
check_size_align::<T, 3>();
22+
check_size_align::<T, 4>();
23+
check_size_align::<T, 8>();
24+
check_size_align::<T, 9>();
25+
check_size_align::<T, 15>();
26+
}
27+
28+
extern "platform-intrinsic" {
29+
fn simd_add<T>(a: T, b: T) -> T;
30+
}
31+
32+
fn main() {
33+
check_ty::<u8>();
34+
check_ty::<i16>();
35+
check_ty::<u32>();
36+
check_ty::<i64>();
37+
check_ty::<usize>();
38+
check_ty::<f32>();
39+
check_ty::<f64>();
40+
41+
unsafe {
42+
// powers-of-two have no padding and work as usual
43+
let x: Simd<f64, 4> =
44+
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
45+
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
46+
47+
// non-powers-of-two have padding and need to be expanded to full vectors
48+
fn load<T, const N: usize>(v: Simd<T, N>) -> FullSimd<T, N> {
49+
unsafe {
50+
let mut tmp = core::mem::MaybeUninit::<FullSimd<T, N>>::uninit();
51+
std::ptr::copy_nonoverlapping(&v as *const _, tmp.as_mut_ptr().cast(), 1);
52+
tmp.assume_init()
53+
}
54+
}
55+
let x: FullSimd<f64, 3> =
56+
simd_add(load(Simd::<f64, 3>([0., 1., 2.])), load(Simd::<f64, 3>([2., 2., 2.])));
57+
assert_eq!(x.0, [2., 3., 4.]);
58+
}
59+
}

0 commit comments

Comments
 (0)