about summary refs log tree commit diff
path: root/tests/codegen/simd/packed-simd.rs
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2024-06-02 14:09:14 -0700
committerJubilee Young <workingjubilee@gmail.com>2024-06-02 20:15:15 -0700
commit9987363b7e1940273dd34e9fa63e33e03244c9c5 (patch)
tree11c94e65e8389bd0821449195eb47734742e82e7 /tests/codegen/simd/packed-simd.rs
parenteda9d7f987de76b9d61c633a6ac328936e1b94f0 (diff)
downloadrust-9987363b7e1940273dd34e9fa63e33e03244c9c5.tar.gz
rust-9987363b7e1940273dd34e9fa63e33e03244c9c5.zip
Test codegen for repr(packed,simd) -> repr(simd)
Diffstat (limited to 'tests/codegen/simd/packed-simd.rs')
-rw-r--r--tests/codegen/simd/packed-simd.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/codegen/simd/packed-simd.rs b/tests/codegen/simd/packed-simd.rs
new file mode 100644
index 00000000000..f0911b6e360
--- /dev/null
+++ b/tests/codegen/simd/packed-simd.rs
@@ -0,0 +1,44 @@
+//@ revisions:opt3 noopt
+//@[opt3] compile-flags: -Copt-level=3
+//@[noopt] compile-flags: -Cno-prepopulate-passes
+
+#![crate_type = "lib"]
+#![no_std]
+#![feature(repr_simd, core_intrinsics)]
+use core::intrinsics::simd as intrinsics;
+use core::{mem, ptr};
+
+// Test codegen for not only "packed" but also "fully aligned" SIMD types, and conversion between
+// A repr(packed,simd) type with 3 elements can't exceed its element alignment,
+// whereas the same type as repr(simd) will instead have padding.
+
+#[repr(simd, packed)]
+pub struct Simd<T, const N: usize>([T; N]);
+
+#[repr(simd)]
+#[derive(Copy, Clone)]
+pub struct FullSimd<T, const N: usize>([T; N]);
+
+// non-powers-of-two have padding and need to be expanded to full vectors
+fn load<T, const N: usize>(v: Simd<T, N>) -> FullSimd<T, N> {
+    unsafe {
+        let mut tmp = mem::MaybeUninit::<FullSimd<T, N>>::uninit();
+        ptr::copy_nonoverlapping(&v as *const _, tmp.as_mut_ptr().cast(), 1);
+        tmp.assume_init()
+    }
+}
+
+// CHECK-LABEL: square_packed
+// CHECK-SAME: ptr{{[a-z_ ]*}} sret([[RET_TYPE:[^)]+]]) [[RET_ALIGN:align (8|16)]]{{[^%]*}} [[RET_VREG:%[_0-9]*]]
+// CHECK-SAME: ptr{{[a-z_ ]*}} align 4
+#[no_mangle]
+pub fn square_packed(x: Simd<f32, 3>) -> FullSimd<f32, 3> {
+    // CHECK-NEXT: start
+    // noopt: alloca [[RET_TYPE]], [[RET_ALIGN]]
+    // CHECK: load <3 x float>
+    let x = load(x);
+    // CHECK: [[VREG:%[a-z0-9_]+]] = fmul <3 x float>
+    // CHECK-NEXT: store <3 x float> [[VREG]], ptr [[RET_VREG]], [[RET_ALIGN]]
+    // CHECK-NEXT: ret void
+    unsafe { intrinsics::simd_mul(x, x) }
+}