| Issue |
244096
|
| Summary |
[InstCombine] `sqrt(X) * sqrt(Y) -> sqrt(X * Y)` without `ninf` turns a finite result into +Inf
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
`foldFMulReassoc` (`llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp`) rewrites `sqrt(X) * sqrt(Y)` to `sqrt(X * Y)` when the `fmul` has `reassoc` and `nnan`. It does not check `ninf`. When `X * Y` overflows, the new form returns `+Inf` where the original was finite.
```llvm
define float @f(float %x, float %y) {
%sx = call reassoc nnan float @llvm.sqrt.f32(float %x)
%sy = call reassoc nnan float @llvm.sqrt.f32(float %y)
%m = fmul reassoc nnan float %sx, %sy
ret float %m
}
```
`opt -passes=instcombine` gives `%1 = fmul reassoc nnan float %x, %y` followed by `%m = call reassoc nnan float @llvm.sqrt.f32(float %1)`. For `x = y = FLT_MAX`, the original returns `0x7f7ffffe` and the folded form returns `+Inf` (`0x7f800000`). The fold fires the same way when the `sqrt` calls have no fast-math flags.
>From C:
```c
#include <stdio.h>
#include <math.h>
#include <stdint.h>
int main(void) {
volatile float x = 3.4028234663852886e38f; /* FLT_MAX */
volatile float y = 3.4028234663852886e38f;
float r = sqrtf(x) * sqrtf(y);
uint32_t b;
__builtin_memcpy(&b, &r, 4);
printf("bits=%08x\n", b);
return 0;
}
```
`clang -O2 -fno-math-errno -fassociative-math -fno-honor-nans -fno-signed-zeros t.c -lm` prints `bits=7f800000`. `-O0` prints `bits=7f7ffffe`. The `fmul` and both `sqrt` calls carry `reassoc nnan nsz` and no `ninf`.
Godbolt (clang trunk, `-O2` with the flags above and `-O0`): https://godbolt.org/z/nWjbPb5Ge
```cpp
// sqrt(X) * sqrt(Y) -> sqrt(X * Y)
// nnan disallows the possibility of returning a number if both operands are
// negative (in that case, we should return NaN).
if (I.hasNoNaNs() && match(Op0, m_OneUse(m_Sqrt(m_Value(X)))) &&
match(Op1, m_OneUse(m_Sqrt(m_Value(Y))))) {
```
The `nnan` check covers the case where both inputs are negative. The overflow case is not covered. The constant reassociations in the same function (`(X / C1) * C`, `(C1 / X) * C`, ...) check `isNormalFP()` on the folded constant, so they do not create an infinity. This fold creates one from finite inputs without `ninf`. Open PR #174291 changes this fold to also require `reassoc` on the two `sqrt` calls, which the C example above still has. The problem seems to be that this fold does not consider overflow of `X * Y` when `ninf` is absent.
Versions (C, flags above): `-O0` correct; `-O1`, `-O2`, `-O3` and `-Os` wrong with clang 15.0.6, 16.0.4, 17.0.6, 18.1.8, 19, 20, 21, 22, 23 and 24 (`d5a22c0d1c26`). Compiler Explorer clang trunk (`c80ecb8de257`) prints `bits=7f800000` too. The fold is unchanged on main (`1559c018809d`).
cc @nikic @dtcxzyw
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