Skip to content

Commit 434f8e6

Browse files
authored
Unrolled build for rust-lang#125311
Rollup merge of rust-lang#125311 - calebzulawski:repr-packed-simd-intrinsics, r=workingjubilee Make repr(packed) vectors work with SIMD intrinsics In rust-lang#117116 I fixed `#[repr(packed, simd)]` by doing the expected thing and removing padding from the layout. This should be the last step in providing a solution to rust-lang/portable-simd#319
2 parents 8bec878 + 5c32f84 commit 434f8e6

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed

compiler/rustc_codegen_llvm/src/intrinsic.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,60 @@ impl<'ll, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'_, 'll, 'tcx> {
482482
}
483483

484484
_ if name.as_str().starts_with("simd_") => {
485+
// Unpack non-power-of-2 #[repr(packed, simd)] arguments.
486+
// This gives them the expected layout of a regular #[repr(simd)] vector.
487+
let mut loaded_args = Vec::new();
488+
for (ty, arg) in arg_tys.iter().zip(args) {
489+
loaded_args.push(
490+
// #[repr(packed, simd)] vectors are passed like arrays (as references,
491+
// with reduced alignment and no padding) rather than as immediates.
492+
// We can use a vector load to fix the layout and turn the argument
493+
// into an immediate.
494+
if ty.is_simd()
495+
&& let OperandValue::Ref(place) = arg.val
496+
{
497+
let (size, elem_ty) = ty.simd_size_and_type(self.tcx());
498+
let elem_ll_ty = match elem_ty.kind() {
499+
ty::Float(f) => self.type_float_from_ty(*f),
500+
ty::Int(i) => self.type_int_from_ty(*i),
501+
ty::Uint(u) => self.type_uint_from_ty(*u),
502+
ty::RawPtr(_, _) => self.type_ptr(),
503+
_ => unreachable!(),
504+
};
505+
let loaded =
506+
self.load_from_place(self.type_vector(elem_ll_ty, size), place);
507+
OperandRef::from_immediate_or_packed_pair(self, loaded, arg.layout)
508+
} else {
509+
*arg
510+
},
511+
);
512+
}
513+
514+
let llret_ty = if ret_ty.is_simd()
515+
&& let abi::Abi::Aggregate { .. } = self.layout_of(ret_ty).layout.abi
516+
{
517+
let (size, elem_ty) = ret_ty.simd_size_and_type(self.tcx());
518+
let elem_ll_ty = match elem_ty.kind() {
519+
ty::Float(f) => self.type_float_from_ty(*f),
520+
ty::Int(i) => self.type_int_from_ty(*i),
521+
ty::Uint(u) => self.type_uint_from_ty(*u),
522+
ty::RawPtr(_, _) => self.type_ptr(),
523+
_ => unreachable!(),
524+
};
525+
self.type_vector(elem_ll_ty, size)
526+
} else {
527+
llret_ty
528+
};
529+
485530
match generic_simd_intrinsic(
486-
self, name, callee_ty, fn_args, args, ret_ty, llret_ty, span,
531+
self,
532+
name,
533+
callee_ty,
534+
fn_args,
535+
&loaded_args,
536+
ret_ty,
537+
llret_ty,
538+
span,
487539
) {
488540
Ok(llval) => llval,
489541
Err(()) => return Ok(()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ compile-flags: -Cno-prepopulate-passes
2+
3+
#![crate_type = "lib"]
4+
#![feature(repr_simd, core_intrinsics)]
5+
// make sure that codegen emits correctly-aligned loads and stores for repr(packed, simd) types
6+
// the alignment of a load should be no less than T, and no more than the size of the vector type
7+
use std::intrinsics::simd as intrinsics;
8+
9+
#[derive(Copy, Clone)]
10+
#[repr(packed, simd)]
11+
struct f32x3([f32; 3]);
12+
13+
#[derive(Copy, Clone)]
14+
#[repr(packed, simd)]
15+
struct f32x4([f32; 4]);
16+
17+
// CHECK-LABEL: load_f32x3
18+
#[no_mangle]
19+
pub fn load_f32x3(floats: &f32x3) -> f32x3 {
20+
// FIXME: Is a memcpy really the best we can do?
21+
// CHECK: @llvm.memcpy.{{.*}}ptr align 4 {{.*}}ptr align 4
22+
*floats
23+
}
24+
25+
// CHECK-LABEL: load_f32x4
26+
#[no_mangle]
27+
pub fn load_f32x4(floats: &f32x4) -> f32x4 {
28+
// CHECK: load <4 x float>, ptr %{{[a-z0-9_]*}}, align {{4|8|16}}
29+
*floats
30+
}
31+
32+
// CHECK-LABEL: add_f32x3
33+
#[no_mangle]
34+
pub fn add_f32x3(x: f32x3, y: f32x3) -> f32x3 {
35+
// CHECK: load <3 x float>, ptr %{{[a-z0-9_]*}}, align 4
36+
unsafe { intrinsics::simd_add(x, y) }
37+
}
38+
39+
// CHECK-LABEL: add_f32x4
40+
#[no_mangle]
41+
pub fn add_f32x4(x: f32x4, y: f32x4) -> f32x4 {
42+
// CHECK: load <4 x float>, ptr %{{[a-z0-9_]*}}, align {{4|8|16}}
43+
unsafe { intrinsics::simd_add(x, y) }
44+
}

tests/ui/simd/repr_packed.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#[repr(simd, packed)]
77
struct Simd<T, const N: usize>([T; N]);
88

9-
#[repr(simd)]
10-
struct FullSimd<T, const N: usize>([T; N]);
11-
129
fn check_size_align<T, const N: usize>() {
1310
use std::mem;
1411
assert_eq!(mem::size_of::<Simd<T, N>>(), mem::size_of::<[T; N]>());
@@ -39,21 +36,15 @@ fn main() {
3936
check_ty::<f64>();
4037

4138
unsafe {
42-
// powers-of-two have no padding and work as usual
39+
// powers-of-two have no padding and have the same layout as #[repr(simd)]
4340
let x: Simd<f64, 4> =
4441
simd_add(Simd::<f64, 4>([0., 1., 2., 3.]), Simd::<f64, 4>([2., 2., 2., 2.]));
4542
assert_eq!(std::mem::transmute::<_, [f64; 4]>(x), [2., 3., 4., 5.]);
4643

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.]);
44+
// non-powers-of-two should have padding (which is removed by #[repr(packed)]),
45+
// but the intrinsic handles it
46+
let x: Simd<f64, 3> = simd_add(Simd::<f64, 3>([0., 1., 2.]), Simd::<f64, 3>([2., 2., 2.]));
47+
let arr: [f64; 3] = x.0;
48+
assert_eq!(arr, [2., 3., 4.]);
5849
}
5950
}

0 commit comments

Comments
 (0)