/src/test/codegen/simd-wide-sum.rs

https://gitlab.com/rust-lang/rust · Rust · 54 lines · 25 code · 7 blank · 22 comment · 1 complexity · d7b54c683b8efe2df96b53a5c0838ddd MD5 · raw file

  1. // compile-flags: -C opt-level=3 --edition=2021
  2. // only-x86_64
  3. // ignore-debug: the debug assertions get in the way
  4. #![crate_type = "lib"]
  5. #![feature(portable_simd)]
  6. use std::simd::Simd;
  7. const N: usize = 8;
  8. #[no_mangle]
  9. // CHECK-LABEL: @wider_reduce_simd
  10. pub fn wider_reduce_simd(x: Simd<u8, N>) -> u16 {
  11. // CHECK: zext <8 x i8>
  12. // CHECK-SAME: to <8 x i16>
  13. // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
  14. let x: Simd<u16, N> = x.cast();
  15. x.reduce_sum()
  16. }
  17. #[no_mangle]
  18. // CHECK-LABEL: @wider_reduce_loop
  19. pub fn wider_reduce_loop(x: Simd<u8, N>) -> u16 {
  20. // CHECK: zext <8 x i8>
  21. // CHECK-SAME: to <8 x i16>
  22. // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
  23. let mut sum = 0_u16;
  24. for i in 0..N {
  25. sum += u16::from(x[i]);
  26. }
  27. sum
  28. }
  29. #[no_mangle]
  30. // CHECK-LABEL: @wider_reduce_iter
  31. pub fn wider_reduce_iter(x: Simd<u8, N>) -> u16 {
  32. // CHECK: zext <8 x i8>
  33. // CHECK-SAME: to <8 x i16>
  34. // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
  35. x.as_array().iter().copied().map(u16::from).sum()
  36. }
  37. // This iterator one is the most interesting, as it's the one
  38. // which used to not auto-vectorize due to a suboptimality in the
  39. // `<array::IntoIter as Iterator>::fold` implementation.
  40. #[no_mangle]
  41. // CHECK-LABEL: @wider_reduce_into_iter
  42. pub fn wider_reduce_into_iter(x: Simd<u8, N>) -> u16 {
  43. // CHECK: zext <8 x i8>
  44. // CHECK-SAME: to <8 x i16>
  45. // CHECK: call i16 @llvm.vector.reduce.add.v8i16(<8 x i16>
  46. x.to_array().into_iter().map(u16::from).sum()
  47. }