https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129155

            Bug ID: 129155
           Summary: [10/11/12/13/14/15/16/17 Regression] aarch64:
                    vcvth_n_f16_u16/u32/u64 of one const_int are folded to
                    a single ucvtf
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: aarch64-linux-gnu

Fails with trunk (gcc 17.0.0 20261129, 389e48d33719) and with 10.2.0 through
16.2.0; 8.5.0 and 9.5.0 work.
Target: aarch64-linux-gnu.

The result of an unsigned ucvtf to _Float16 depends on the source width: u16
0x8000 scaled by 2^-16 is 0.5, but u32 0xffff8000 scaled by 2^-16 is 65535.5,
which rounds to +Inf.

    #include <arm_fp16.h>

    __attribute__((noipa)) int f (void)
    {
      float16_t x = vcvth_n_f16_u16 ((uint16_t) 0x8000, 16); /* 0.5 */
      float16_t y = vcvth_n_f16_u32 (0xffff8000u, 16);       /* 65535.5 -> +Inf
*/
      return x == y;
    }

    __attribute__((noipa)) int g (void)
    {
      float16_t x = vcvth_n_f16_u32 (0x80000000u, 16);           /* 32768 */
      float16_t y = vcvth_n_f16_u64 (0xffffffff80000000ull, 16); /* 2^48 ->
+Inf */
      return x == y;
    }

    int main (void)
    {
      if (f ())
        __builtin_abort ();
      if (g ())
        __builtin_abort ();
      return 0;
    }

    aarch64-linux-gnu-gcc -O2 -march=armv8.2-a+fp16 t.c && qemu-aarch64 ./a.out

The program is expected to exit 0. On an aarch64-linux-gnu cross build of GCC
17.0.0 20261129 (experimental), git commit 389e48d33719, compiling with -O2
-march=armv8.2-a+fp16 and running the binary under qemu-aarch64 aborts. -O3 and
-Os abort as well. -O0, -O1, -O2 -fno-forward-propagate and -O2
-fno-rerun-cse-after-loop exit 0. The same -O2 command also aborts on 10.2.0
through 16.2.0 and exits 0 on 8.5.0 and 9.5.0.

At -O0 the values are 0.5 versus +Inf and 32768 versus +Inf (fp16 bits
0x3800/0x7c00 and 0x7800/0x7c00). An -O0 -fsanitize=undefined
-fno-sanitize-recover=all build exits 0.

Trunk -O2 code for f:

    movi    v31.4h, 0x80, lsl 8
    ucvtf   h31, h31, #16
    fcmp    h31, h31
    cset    w0, eq

7.5.0 also aborts at -O2 and -O3, because it ignores noipa and inlines f and g
into main; the out-of-line functions keep both conversions. -O0, -O1 and -Os on
7.5.0 do not inline and are correct.

Godbolt (trunk): https://godbolt.org/z/xs6xf7WEj

ucvtfhi3, ucvtfsihf3 and ucvtfdihf3 in config/aarch64/aarch64.md all use
UNSPEC_UCVTF with an HF result. The source width is only the mode of operand 1,
and a const_int has no mode, so 0x8000 and 0xffff8000 are both (const_int
-32768). fwprop adds the same REG_EQUAL note (unspec:HF [(const_int -32768)
(const_int 16)] UNSPEC_UCVTF) to both insns, and cse2 deletes one conversion
and turns the compare into a self-compare. Signed scvtf of a sign-extended
constant is the same value at every width and stays correct.

This is the same kind of bug as PR128310, which was fixed for i386 crc32 by
using a separate unspec per width. That fix does not cover these AArch64
patterns.

A possible direction is to give each ucvtf source width its own unspec so the
REG_EQUAL note cannot drop the mode. I have not tested a patch.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to