Issue 241263
Summary [InstCombine] foldFCmpFpTrunc midpoint overflows to inf for same-emax types (miscompile at -O3)
Labels new issue
Assignees
Reporter jeffchen006
    `foldFCmpFpTrunc` rewrites `fcmp (fptrunc X), C` into `fcmp X, M` with M the rounding boundary. M is computed as `(C + next(C)) / 2` in the wide type, and the sum overflows to +inf when the wide and narrow types have the same maximum exponent.

**Reproducer** (`repro.c`)
```c
/* (__bf16)x is 2^127 + 2^120 (exact in __bf16), which is greater than 2^127,
   so the program must print 1. Clang -O3 prints 0. */
int printf(const char *, ...);
volatile float x = 0x1.02p127f;
int main(void) {
  __bf16 a = x;
  printf("%d\n", a > (__bf16)0x1p127);
 return 0;
}
```

**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 repro.c -o o0
$ ./o0
1
$ clang -O3 repro.c -o o3
$ ./o3
0
```
Expected (as `-O0`): `1`. `-O3` prints `0`.

**Why the expected output is right**
All inputs are volatile, finite, and exactly representable in the narrow type, so each conversion is exact; `__bf16` compares promote to float exactly; `LDBL_MAX` is exact in fp128. No integer arithmetic, no pointers, no contraction. UBSan/ASan/MSan at -O0 are clean. Alive2 also rejects the transform on the IR below.

**Potential Root cause**
`llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp`, `foldFCmpFpTrunc`: `ExtMidValue = scalbn(ExtCValue + ExtNextCValue, -1)` in the wide type W. When W and the narrow type N have the same emax (float/bfloat, fp128/x86_fp80), the sum overflows to +inf for |C| >= 2^emax. Then the result is FLT_MAX / FP128_MAX (wrong boundary) or +inf. The infinity checks run only when `NextCValue` is infinite, so they do not catch it. (Fold from PR #99475.)

**Possible Proposed fix**
Compute the midpoint as `C + (next(C) - C)/2`: the difference is one narrow ulp, exact in the wide type, and does not overflow.
```diff
-  APFloat ExtNextCValue = ConvertFltSema(NextCValue, DestFltSema);
-  if (NextCValue.isInfinity()) {
-    APFloat PrevCValue = NextValue(*CValue, !RoundDown);
-    APFloat Bias = ConvertFltSema(*CValue - PrevCValue, DestFltSema);
-    ExtNextCValue = ExtCValue + Bias;
-  }
-  APFloat ExtMidValue =
-      scalbn(ExtCValue + ExtNextCValue, -1, APFloat::rmNearestTiesToEven);
+  APFloat Delta = NextCValue.isInfinity()
+                      ? ConvertFltSema(*CValue - NextValue(*CValue, !RoundDown),
+ DestFltSema)
+                      : ConvertFltSema(NextCValue, DestFltSema) - ExtCValue;
+  APFloat ExtMidValue =
+      ExtCValue + scalbn(Delta, -1, APFloat::rmNearestTiesToEven);
```

**Reduced IR**
```llvm
define i1 @f(float %x) {
  %t = fptrunc float %x to bfloat
 %c = fcmp ogt bfloat %t, 0xR7F00
  ret i1 %c
}
define i1 @g(float %x) {
 %t = fptrunc float %x to bfloat
  %c = fcmp oge bfloat %t, 0xR7F7F
  ret i1 %c
}
define i1 @h(fp128 %x) {
  %t = fptrunc fp128 %x to x86_fp80
  %c = fcmp oge x86_fp80 %t, 0xK7FFEFFFFFFFFFFFFFFFF
  ret i1 %c
}
define i1 @k(fp128 %x) {
  %t = fptrunc fp128 %x to x86_fp80
  %c = fcmp ogt x86_fp80 %t, 0xK7FFE8000000000000000
  ret i1 %c
}
```
`opt -passes=instcombine repro.ll` gives `fcmp ogt float %x, 0x7F7FFFFF` for `@f` (correct boundary 0x7F008000) and `fcmp oeq float %x, +inf` for `@g`.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to