[Bug target/122692] U16 array to U8 array saturation test fails on riscv64-linux-gnu

2025-11-14 Thread john_platts at hotmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122692

--- Comment #3 from John Platts  ---
Found a GCC 15 bug on the RISC-V target where the store of the lower 16 bits of
bits to from[i] followed by a reload of from[i] is incorrectly optimized out
from a reduced snippet at https://godbolt.org/z/ndY8G8Wcx.

Here is the snippet having a bug with GCC 15.2 on riscv64:
const uint64_t bits = rng();
__builtin_memcpy(&from[i], &bits, sizeof(T));  // not same size
expected[i] = static_cast(HWY_MIN(HWY_MAX(min, from[i]), max));

The problem is that GCC 15 fails to zero out the upper 48 bits of from[i]
before doing the comparison (as demonstrated by the snippet at
https://godbolt.org/z/ndY8G8Wcx), producing incorrect results for expected[i].

This same bug does not occur with GCC 14.3 or Clang 21 on riscv64, and both GCC
14.3 and Clang 21 correctly zero out the upper 48 bits of from[i] before doing
the comparison against 255.

[Bug target/122692] U16 array to U8 array saturation test fails on riscv64-linux-gnu

2025-11-14 Thread john_platts at hotmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122692

--- Comment #2 from John Platts  ---
Turns out that the bug is triggered by this loop (which has been separated out
into a function with the __attribute__((__noinline__)) and
__attribute__((__noinline__)) attributes):
static __attribute__((__noinline__)) NOIPA_ATTR void GenerateRandomU16x16Vals(
RandomState& rng, uint16_t* const from, uint8_t* const expected) {
  using T = uint16_t;
  using TN = uint8_t;

  const T min = 0;
  const T max = static_cast(std::numeric_limits::max());

  for (size_t i = 0; i < 16; ++i) {
const uint64_t bits = rng();
__builtin_memcpy(&from[i], &bits, sizeof(T));  // not same size
expected[i] = static_cast(HWY_MIN(HWY_MAX(min, from[i]), max));
  }
}

[Bug target/122692] U16 array to U8 array saturation test fails on riscv64-linux-gnu

2025-11-14 Thread john_platts at hotmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122692

--- Comment #1 from John Platts  ---
Created attachment 62812
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62812&action=edit
Another test program to reproduce GCC 15 bug on riscv64-linux-gnu