1 // compile-flags: -C opt-level=3 --edition=2021
3 // ignore-debug: the debug assertions get in the way
6 #![feature(portable_simd)]
8 use std::simd::{Simd, SimdUint};
12 // CHECK-LABEL: @wider_reduce_simd
13 pub fn wider_reduce_simd(x: Simd<u8, N>) -> u16 {
14 // CHECK: zext <8 x i8>
15 // CHECK-SAME: to <8 x i16>
16 // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
17 let x: Simd<u16, N> = x.cast();
22 // CHECK-LABEL: @wider_reduce_loop
23 pub fn wider_reduce_loop(x: Simd<u8, N>) -> u16 {
24 // CHECK: zext <8 x i8>
25 // CHECK-SAME: to <8 x i16>
26 // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
29 sum += u16::from(x[i]);
35 // CHECK-LABEL: @wider_reduce_iter
36 pub fn wider_reduce_iter(x: Simd<u8, N>) -> u16 {
37 // CHECK: zext <8 x i8>
38 // CHECK-SAME: to <8 x i16>
39 // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
40 x.as_array().iter().copied().map(u16::from).sum()
43 // This iterator one is the most interesting, as it's the one
44 // which used to not auto-vectorize due to a suboptimality in the
45 // `<array::IntoIter as Iterator>::fold` implementation.
48 // CHECK-LABEL: @wider_reduce_into_iter
49 pub fn wider_reduce_into_iter(x: Simd<u8, N>) -> u16 {
50 // FIXME MIR inlining messes up LLVM optimizations.
51 // WOULD-CHECK: zext <8 x i8>
52 // WOULD-CHECK-SAME: to <8 x i16>
53 // WOULD-CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
54 x.to_array().into_iter().map(u16::from).sum()