Skip to content

Commit 329412d

Browse files
committed
Implement repr(packed) for repr(simd)
1 parent 3acb261 commit 329412d

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-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
@@ -435,7 +435,21 @@ fn layout_of_uncached<'tcx>(
435435
.size
436436
.checked_mul(e_len, dl)
437437
.ok_or_else(|| error(cx, LayoutError::SizeOverflow(ty)))?;
438-
let align = dl.vector_align(size);
438+
439+
let (abi, align) = if def.repr().packed() && !e_len.is_power_of_two() {
440+
// Non-power-of-two vectors have padding up to the next power-of-two.
441+
// If we're a packed repr, remove the padding while keeping the alignment as close
442+
// to a vector as possible.
443+
(
444+
Abi::Aggregate { sized: true },
445+
AbiAndPrefAlign {
446+
abi: Align::max_for_offset(size),
447+
pref: dl.vector_align(size).pref,
448+
},
449+
)
450+
} else {
451+
(Abi::Vector { element: e_abi, count: e_len }, dl.vector_align(size))
452+
};
439453
let size = size.align_to(align.abi);
440454

441455
// Compute the placement of the vector fields:
@@ -448,7 +462,7 @@ fn layout_of_uncached<'tcx>(
448462
tcx.mk_layout(LayoutS {
449463
variants: Variants::Single { index: FIRST_VARIANT },
450464
fields,
451-
abi: Abi::Vector { element: e_abi, count: e_len },
465+
abi,
452466
largest_niche: e_ly.largest_niche,
453467
size,
454468
align,

tests/ui/simd/repr_packed.rs

+45
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)