| Issue |
240241
|
| Summary |
[DAGCombiner] `X/sqrt(X) -> sqrt(X)` without `nsz` turns `0/sqrt(0) == 0` from false into true
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
#165011 relaxed the `nsz` requirement of the `X/Sqrt(X) -> Sqrt(X)` fold in `visitFDIV` to "`nsz` or all users ignore the sign of a zero" (`canIgnoreSignBitOfZeroResult`, after #232762). For this fold the difference at zero is not only the sign: `±0 / sqrt(±0)` is NaN while `sqrt(±0)` is `±0`. A user that ignores the sign of zero, such as `fcmp oeq x, 0.0`, still sees NaN vs zero. With only `reassoc` on the divide, `x / sqrt(x) == 0.0` becomes true for `x = ±0`.
```llvm
; Only reassoc. No nsz, nnan, ninf, arcp, afn.
define i32 @f(double %x) {
%s = call reassoc double @llvm.sqrt.f64(double %x)
%q = fdiv reassoc double %x, %s
%c = fcmp oeq double %q, 0.000000e+00
%r = zext i1 %c to i32
ret i32 %r
}
declare double @llvm.sqrt.f64(double)
```
`llc -O2 -mtriple=x86_64` (trunk) drops the division:
```asm
f:
sqrtsd xmm0, xmm0
xorpd xmm1, xmm1
cmpeqsd xmm1, xmm0
movq rax, xmm1
and eax, 1
ret
```
`llc -O0` keeps `sqrtsd` + `divsd` + `ucomisd`. Godbolt: https://godbolt.org/z/fa5fPsvWx
>From C (only `reassociate(on)` in the pragma, `-fno-math-errno` so `sqrt` becomes `llvm.sqrt`):
```c
#include <math.h>
#include <stdio.h>
__attribute__((noinline)) int f(double x) {
double q;
{
#pragma clang fp reassociate(on) contract(off)
q = x / sqrt(x);
}
return q == 0.0;
}
int main(void) {
volatile double z = 0.0, n = -0.0, p = 4.0;
int az = f(z), an = f(n), ap = f(p);
printf("expect z 0 n 0 p 0\nactual z %d n %d p %d\n", az, an, ap);
return (az == 0 && an == 0 && ap == 0) ? 0 : 1;
}
```
```console
$ clang -O0 -fno-math-errno t.c -lm && ./a.out # actual z 0 n 0 p 0
$ clang -O2 -fno-math-errno t.c -lm && ./a.out # actual z 1 n 1 p 0
```
Godbolt (clang trunk f2823c28d856, built 2026-11-03, i.e. after #232762, executed): https://godbolt.org/z/9T5rdaqTx
Versions: wrong with clang 22, 23 and trunk at `-O1`, `-O2`, `-O3` and `-Os`; correct with clang 15 to 17 and 19 to 21 at every level, and with gcc (also with gcc `-fassociative-math -fno-signed-zeros -fno-trapping-math`). (18 not tested.) `llc-21 -O2` keeps the `divsd`, so this is the backend change from #165011 (merged 2025-12-09).
Where: `DAGCombiner::visitFDIV`, `llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp` line 20467 at 605fe44a3:
```cpp
// Fold X/Sqrt(X) -> Sqrt(X)
if ((Flags.hasNoSignedZeros() ||
DAG.canIgnoreSignBitOfZeroResult(SDValue(N, 0))) &&
Flags.hasAllowReassociation())
if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0))
return N1;
```
`canIgnoreSignBitOfZero*` returns true for an `ISD::SETCC` user, which is right for "is the sign of a zero observable", but this rewrite also changes NaN (from `0/0`) into a zero, so that predicate is not a sufficient condition here.
Possible fix (not tested): for this fold, go back to requiring `nsz` (as before #165011), or additionally require `nnan` when relying on `canIgnoreSignBitOfZeroResult`. The other folds listed in #165011 (`fadd x, +0.0`, `fsub x, -0.0`, `fsub +0.0, x`) really only differ in the sign of zero, so they are not affected.
cc @guy-david
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