summaryrefslogtreecommitdiffstats
path: root/tests/codegen/simd/simd-wide-sum.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:24 +0000
commit023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch)
tree60fc59477c605c72b0a1051409062ddecc43f877 /tests/codegen/simd/simd-wide-sum.rs
parentAdding debian version 1.72.1+dfsg1-1. (diff)
downloadrustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz
rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/codegen/simd/simd-wide-sum.rs')
-rw-r--r--tests/codegen/simd/simd-wide-sum.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/codegen/simd/simd-wide-sum.rs b/tests/codegen/simd/simd-wide-sum.rs
new file mode 100644
index 000000000..3116f9597
--- /dev/null
+++ b/tests/codegen/simd/simd-wide-sum.rs
@@ -0,0 +1,59 @@
+// revisions: llvm mir-opt3
+// compile-flags: -C opt-level=3 -Z merge-functions=disabled --edition=2021
+// only-x86_64
+// ignore-debug: the debug assertions get in the way
+// [mir-opt3]compile-flags: -Zmir-opt-level=3
+// [mir-opt3]build-pass
+
+// mir-opt3 is a regression test for https://github.com/rust-lang/rust/issues/98016
+
+#![crate_type = "lib"]
+#![feature(portable_simd)]
+
+use std::simd::{Simd, SimdUint};
+const N: usize = 8;
+
+#[no_mangle]
+// CHECK-LABEL: @wider_reduce_simd
+pub fn wider_reduce_simd(x: Simd<u8, N>) -> u16 {
+ // CHECK: zext <8 x i8>
+ // CHECK-SAME: to <8 x i16>
+ // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
+ let x: Simd<u16, N> = x.cast();
+ x.reduce_sum()
+}
+
+#[no_mangle]
+// CHECK-LABEL: @wider_reduce_loop
+pub fn wider_reduce_loop(x: Simd<u8, N>) -> u16 {
+ // CHECK: zext <8 x i8>
+ // CHECK-SAME: to <8 x i16>
+ // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
+ let mut sum = 0_u16;
+ for i in 0..N {
+ sum += u16::from(x[i]);
+ }
+ sum
+}
+
+#[no_mangle]
+// CHECK-LABEL: @wider_reduce_iter
+pub fn wider_reduce_iter(x: Simd<u8, N>) -> u16 {
+ // CHECK: zext <8 x i8>
+ // CHECK-SAME: to <8 x i16>
+ // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
+ x.as_array().iter().copied().map(u16::from).sum()
+}
+
+// This iterator one is the most interesting, as it's the one
+// which used to not auto-vectorize due to a suboptimality in the
+// `<array::IntoIter as Iterator>::fold` implementation.
+
+#[no_mangle]
+// CHECK-LABEL: @wider_reduce_into_iter
+pub fn wider_reduce_into_iter(x: Simd<u8, N>) -> u16 {
+ // CHECK: zext <8 x i8>
+ // CHECK-SAME: to <8 x i16>
+ // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
+ x.to_array().into_iter().map(u16::from).sum()
+}