2023-07-11 19:43:51 -05:00
|
|
|
//@ revisions: llvm mir-opt3
|
2023-03-13 10:07:16 -05:00
|
|
|
//@ compile-flags: -C opt-level=3 -Z merge-functions=disabled --edition=2021
|
2022-04-02 16:29:41 -05:00
|
|
|
//@ only-x86_64
|
2023-07-11 19:43:51 -05:00
|
|
|
//@ [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
|
2022-04-02 16:29:41 -05:00
|
|
|
|
|
|
|
#![crate_type = "lib"]
|
|
|
|
#![feature(portable_simd)]
|
|
|
|
|
2023-11-19 18:04:06 -06:00
|
|
|
use std::simd::prelude::*;
|
2023-09-20 23:29:20 -05:00
|
|
|
const N: usize = 16;
|
2022-04-02 16:29:41 -05:00
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
// CHECK-LABEL: @wider_reduce_simd
|
|
|
|
pub fn wider_reduce_simd(x: Simd<u8, N>) -> u16 {
|
2023-09-20 23:29:20 -05:00
|
|
|
// CHECK: zext <16 x i8>
|
|
|
|
// CHECK-SAME: to <16 x i16>
|
|
|
|
// CHECK: call i16 @llvm.vector.reduce.add.v16i16(<16 x i16>
|
2022-04-02 16:29:41 -05:00
|
|
|
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 {
|
2023-09-20 23:29:20 -05:00
|
|
|
// CHECK: zext <16 x i8>
|
|
|
|
// CHECK-SAME: to <16 x i16>
|
|
|
|
// CHECK: call i16 @llvm.vector.reduce.add.v16i16(<16 x i16>
|
2022-04-02 16:29:41 -05:00
|
|
|
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 {
|
2023-09-20 23:29:20 -05:00
|
|
|
// CHECK: zext <16 x i8>
|
|
|
|
// CHECK-SAME: to <16 x i16>
|
|
|
|
// CHECK: call i16 @llvm.vector.reduce.add.v16i16(<16 x i16>
|
2022-04-02 16:29:41 -05:00
|
|
|
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 {
|
2024-03-28 08:28:32 -05:00
|
|
|
// CHECK: zext <16 x i8>
|
|
|
|
// CHECK-SAME: to <16 x i16>
|
|
|
|
// CHECK: call i16 @llvm.vector.reduce.add.v16i16(<16 x i16>
|
2022-04-02 16:29:41 -05:00
|
|
|
x.to_array().into_iter().map(u16::from).sum()
|
|
|
|
}
|