Issue 233383
Summary [clang] [InstCombine] foldSPFofSPF folds MAX(MAX(A,B),B) → MAX(A,B) for FP select patterns with different NaN behaviour - clamp(NaN) returns NaN instead of the bound (miscompile from plain C, -O1+)
Labels backend:X86, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    ## C reproducer — `repro.c`
```c
#include <math.h>
#include <stdio.h>
__attribute__((noinline)) float clamp_low(float x) {
  float m = (x < 1.0f) ? 1.0f : x;     /* NaN -> m = NaN   */
  return (1.0f < m) ? m : 1.0f;         /* NaN -> 1.0        */
}
int main(void) {
  float r = clamp_low(NAN);
  printf("clamp_low(NaN) = %f (%s)\n", r, r == 1.0f ? "correct" : "WRONG: must be 1.0");
  return r != 1.0f;
}
```
| compiler | output |
|---|---|
| `clang -O0` | `1.000000` (correct) |
| `clang -O1` / `-O2` / `-O3` | **`nan` (WRONG)** |
| `gcc -O2` | `1.000000` (correct) |
| `clang -O2` with the fix below | `1.000000` |

Trunk: https://godbolt.org/z/qzvaqjfd1

Both comparisons are ordinary C `<`; no fast-math is involved. For `x = NaN`, `x < 1.0f` is false, so `m = x = NaN`;
then `1.0f < NaN` is false, so the function must return `1.0f`.

## Minimal IR — `min.ll`
```llvm
define float @f(float %b) {
  %c1 = fcmp ogt float 1.0, %b
  %m = select i1 %c1, float 1.0, float %b        ; NaN -> %b (NaN)
  %c2 = fcmp olt float 1.0, %m
  %r = select i1 %c2, float %m, float 1.0        ; NaN -> 1.0
  ret float %r
}
```
`opt -passes=instcombine` returns `%m`, dropping the second select. llubi on `run.ll` gives `1.000000` before and
`nan` after.

## Root cause
`InstCombinerImpl::foldSPFofSPF` (InstCombineSelect.cpp):
```cpp
  if (C == A || C == B) {
    // MAX(MAX(A, B), B) -> MAX(A, B)
    if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
      return replaceInstUsesWith(Outer, Inner);
  }
```
It is called from `visitSelectInst` with only the *flavors* of the two select patterns. For floating point,
`matchSelectPattern` also reports a **NaN behaviour**: `SPNB_RETURNS_NAN`, `SPNB_RETURNS_OTHER` or
`SPNB_RETURNS_ANY`. Here both selects are `SPF_FMAXNUM`, but:
- the inner `b < 1 ? 1 : b` returns the NaN operand;
- the outer `1 < m ? m : 1` returns the *other* operand.

Absorption `max(max(A,B),B) = max(A,B)` holds only when both have the same NaN behaviour. With mixed behaviour the
NaN that the inner select passes through is replaced by the outer select's non-NaN operand, which the fold drops.

**Fix (validated):** pass the full `SelectPatternResult`s and require equal `NaNBehavior` for FP types. With it,
`repro.c`, `min.ll` and the fuzz case are correct and the TV assertion is silent:
```cpp
      SelectPatternResult SPRL = matchSelectPattern(LHS, LHS2, RHS2);
      if (SPRL.Flavor && (!SelType->isFPOrFPVectorTy() || SPRL.NaNBehavior == SPR.NaNBehavior))
        if (Instruction *R = foldSPFofSPF(...))
```
(the same for the RHS operand).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to