Issue 244148
Summary [Hexagon] HVX int-to-fp expansion (ExpandHvxIntToFp) loses the rounding carry into the exponent: (float)33554431 == 16777216.0f
Labels backend:Hexagon, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```c
// clang --target=hexagon-unknown-linux-musl -mcpu=hexagonv68 -mhvx -mhvx-length=128B -O2 repro.c
// (run: qemu-hexagon -cpu v68).  HVX int->float rounding tie that carries into the exponent:
// (float)33554431 must be 33554432.0f (0x4c000000); HVX expansion gives 16777216.0f (0x4b800000).
extern int printf(const char *, ...);
typedef int v32si __attribute__((vector_size(128)));
typedef float v32sf __attribute__((vector_size(128)));
__attribute__((noinline)) v32sf conv(v32si a) { return __builtin_convertvector(a, v32sf); }
int main(void) {
  v32si a = {0};
  a[0] = 33554431; a[1] = -33554431; a[2] = 134217724; a[3] = 8190;
  v32sf f = conv(a);
  unsigned u[4]; __builtin_memcpy(u, &f, 16);
  unsigned exp[4] = {0x4c000000u, 0xcc000000u, 0x4d000000u, 0x45fff000u};
  int bad = 0;
  for (int i = 0; i < 4; i++) { printf("%llx\n", (unsigned long long)u[i]); bad |= u[i] != exp[i]; }
 printf("%llx\n", (unsigned long long)bad);
  return bad;
}
```

Resulted in this output:
```
$ clang --target=hexagon-unknown-linux-musl -mcpu=hexagonv68 -mhvx -mhvx-length=128B -O2 repro.c -o r && qemu-hexagon -cpu v68 ./r
4b800000
cb800000
4c800000
45fff000
1
```
The expected output is `4c000000 cc000000 4d000000 45fff000 0`. `(float)33554431` gives 16777216.0f, half the correct value 33554432.0f. The same happens for i16 -> f16 (`(half)8190` gives 0x6c00, expected 0x7000) and for `uitofp`, at -O0 and -O2. Scalar Hexagon is correct. The expansion uses only integer HVX instructions, so qemu's emulation of it is reliable. An exhaustive i16 -> f16 check (exhaustive-i16.ll) finds 4 wrong values for sitofp and 2 for uitofp.

Trunk: https://godbolt.org/z/War6d5TPT (llc trunk `-mtriple=hexagon -mcpu=hexagonv68 -mattr=+hvxv68,+hvx-length128b -O2` on v32.ll; the final step is `vor` of the fraction and the exponent).

Root cause: `HexagonTargetLowering::ExpandHvxIntToFp` (llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp) rounds the normalized fraction with `emitHvxShiftRightRnd`. When a round-to-even tie rounds up an all-ones fraction, the fraction carries into bit `FracWidth` (0x800000 for f32, 0x400 for f16). `Ovf` only reports the overflow of the first add, not this carry. The result is then assembled with `Flt0 = OR(Frac, Exp3)`. If the exponent's low bit is already 1, the carry is lost and the result is halved. If it is 0, the OR happens to act as an add, which is why only some ties fail.

Suggested fix: combine the fraction and the exponent with an add, so the carry propagates into the exponent (fix.diff):
```diff
--- a/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelLoweringHVX.cpp
@@ HexagonTargetLowering::ExpandHvxIntToFp(SDValue Op, SelectionDAG &DAG) const {
   SDValue Exp3 = DAG.getNode(ISD::SHL, dl, InpTy,
 {Exp2, DAG.getConstant(FracWidth, dl, InpTy)});
-  SDValue Flt0 = DAG.getNode(ISD::OR, dl, InpTy, {Frac, Exp3});
+  // The rounding of Frac may carry into the exponent field: add, not or.
+  SDValue Flt0 = DAG.getNode(ISD::ADD, dl, InpTy, {Frac, Exp3});
   SDValue Flt1 = DAG.getNode(ISD::VSELECT, dl, InpTy, {IsZero, Zero, Flt0});
```
With the fix, repro.c prints `0`. The exhaustive i16 check finds 0 mismatches against scalar Hexagon, for both sitofp and uitofp. In-tree, only the int-to-fp tests change (`vor` becomes `vadd`, plus rescheduling): autohvx/int-to-fp.ll, vector-sint-to-fp.ll and autohvx/isel-const-splat-imm.ll.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to