Issue 241653
Summary [InstSimplify] `constrained.fsub(-0.0, fneg X) -> X` ignores the rounding mode and returns +0 instead of -0 under round.downward/round.dynamic
Labels new issue
Assignees
Reporter 20000419
    `simplifyFSubInst` folds `fsub -0.0, (fneg X)` to `X` for constrained calls whenever `canIgnoreSNaN` holds, without checking the rounding mode. For `X = +0.0`, `fneg X` is `-0.0`, and IEEE 754 (6.3) says the exact zero result of `-0.0 - (-0.0)` is `-0.0` under roundTowardNegative and `+0.0` otherwise. So the fold is only valid when the rounding mode cannot be toward negative (or with `nsz`). The two folds directly above it in the same function (`fsub X, +0 -> X`) already have exactly this guard; this one is missing it.

LangRef says that for `round.dynamic` "the optimizer must not assume any particular rounding mode", and for `round.downward` the mode is known to be toward negative, so in both cases returning `X` changes the sign of a zero result. `fpexcept.ignore` (or `nnan`) only allows ignoring exceptions/SNaN, which is what `canIgnoreSNaN` checks; it does not allow ignoring the sign of zero.

### Reproducer

```c
#pragma STDC FENV_ACCESS ON
#include <fenv.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

double f(double x) { return -0.0 - (-x); }

int main(void) {
  volatile double z = 0.0;
  fesetround(FE_DOWNWARD);
 feclearexcept(FE_ALL_EXCEPT);
  double r = f(z);
  int flags = fetestexcept(FE_ALL_EXCEPT);
  uint64_t u;
  memcpy(&u, &r, 8);
 printf("%016llx flags %d\n", (unsigned long long)u, flags);
  return u == 0x8000000000000000ULL ? 0 : 1;
}
```

```console
$ clang -O0 -ffp-exception-behavior=ignore -frounding-math t.c -lm && ./a.out
8000000000000000 flags 0
$ clang -O2 -ffp-exception-behavior=ignore -frounding-math t.c -lm && ./a.out
0000000000000000 flags 0
```

gcc `-O0`/`-O2 -frounding-math` prints `8000000000000000`.

IR (`opt -passes=instsimplify`):

```llvm
define double @f(double %x) strictfp {
 %n = fneg double %x
  %r = call double @llvm.experimental.constrained.fsub.f64(double -0.0, double %n, metadata !"round.dynamic", metadata !"fpexcept.ignore") strictfp
  ret double %r
}
```

becomes `ret double %x`. The same happens with `round.downward` + `fpexcept.ignore`, and with `round.dynamic`/`round.downward` + `fpexcept.strict` when the call has `nnan` (`canIgnoreSNaN` is true for `nnan` even with strict exceptions). In `-O2` the fold is applied by EarlyCSE through `simplifyInstruction`. `f32` behaves the same.

Godbolt (trunk, `-O2` vs `-O0`, with execution): https://godbolt.org/z/x8hnfWhjW

### Versions

Wrong with clang 15.0.6, 16.0.4, 17.0.6, 18.1.0, 19, 20, 21, 22, 23 and trunk (24.0.0, 8e8d8275f59d) at `-O1`, `-O2`, `-O3` and `-Os`. Correct at `-O0`. The constrained form of this fold was added in d43d9e1d5c98 ("[FPEnv][InstSimplify] Fold fsub -0.0, -X ==> X", D123396).

### Where

`llvm/lib/Analysis/InstructionSimplify.cpp`, `simplifyFSubInst`:

```cpp
// fsub -0.0, (fsub -0.0, X) ==> X
// fsub -0.0, (fneg X) ==> X
Value *X;
if (canIgnoreSNaN(ExBehavior, FMF))
  // TODO: Confirm whether m_FNegOrFSubNegZero is correct.
  if (match(Op0, m_NegZeroFP()) &&
      match(Op1, m_FNegOrFSubNegZero(m_Value(X))))
 return X;
```

compared with the fold just above it:

```cpp
// fsub X, +0 ==> X
if (canIgnoreSNaN(ExBehavior, FMF) &&
 (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
 FMF.noSignedZeros()))
```

The existing tests in `llvm/test/Transforms/InstSimplify/strictfp-fsub.ll` (`@fsub_fneg_n0_fnX_defaultenv`, `@fsub_fneg_nnan_n0_fnX_ebstrict`) only use `round.tonearest`, where the fold is correct. This is related to but different from #177467 (constant folding of `constrained.fsub(0.0, 0.0)` in `ConstantFolding`, fix in PR #184919, which does not touch `simplifyFSubInst`).

### Possible fix (not tested)

Add the same condition as the `fsub X, +0` fold: `!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) || FMF.noSignedZeros()`.

cc @kpneal @nikic

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

Assisted-by: Grok

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

Reply via email to