| Issue |
241652
|
| Summary |
[AggressiveInstCombine] foldSqrt drops `strictfp` from a `sqrt` libcall, so `sqrt(2.0)` under FENV_ACCESS is constant-folded with round-to-nearest
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
In a function compiled with `#pragma STDC FENV_ACCESS ON` (or `-ffp-model=strict`), `sqrt(2.0)` is a `strictfp` libcall. `AggressiveInstCombine`'s `foldSqrt` replaces it with `llvm.sqrt` via `IRBuilder::CreateIntrinsic`, which does not carry over the `strictfp` call attribute. The new call is then constant-folded by `SimplifyInstructionsInBlock`, because `canConstantFoldCallTo` checks `Call->isStrictFP()` and the new call is not strictfp. The host folds with round-to-nearest-even, so under `FE_DOWNWARD` the program gets the wrong last bit and no `FE_INEXACT`.
### Reproducer
```c
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#pragma STDC FENV_ACCESS ON
int main(void) {
fesetround(FE_DOWNWARD);
feclearexcept(FE_ALL_EXCEPT);
double s = sqrt(2.0);
unsigned long long u;
memcpy(&u, &s, 8);
printf("%016llx %d\n", u, fetestexcept(FE_ALL_EXCEPT));
return 0;
}
```
```console
$ clang -O0 -ffp-model=strict -frounding-math t.c -lm && ./a.out
3ff6a09e667f3bcc 32
$ clang -O2 -ffp-model=strict -frounding-math t.c -lm && ./a.out
3ff6a09e667f3bcd 0
```
The correctly rounded downward result is `0x3ff6a09e667f3bcc` with `FE_INEXACT` set (that is what libm returns at `-O0`, and what gcc `-O2 -frounding-math` prints). `-O2` stores the round-to-nearest constant. The pragma alone (without `-ffp-model=strict`) fails the same way. `sqrtf(2.0f)` under `FE_UPWARD` is the same fold (`3fb504f4` vs `3fb504f3`).
`opt -passes=aggressive-instcombine` alone is enough. Before:
```llvm
; in a strictfp function
%6 = call double @sqrt(double noundef 2.000000e+00) #4 ; #4 = { nounwind strictfp }
```
After:
```llvm
store double f0x3FF6A09E667F3BCD, ptr %2, align 8
```
`opt -passes=instcombine` alone keeps the strictfp libcall. With `-opt-bisect-limit`, the change happens at `aggressive-instcombine on main`.
Godbolt (trunk, `-O2` vs `-O0`, with execution): https://godbolt.org/z/o1T3M3YEc
### Versions
Wrong with clang 17.0.6, 18.1.0, 19, 20, 21, 22, 23 and trunk (24.0.0, 8e8d8275f59d) at `-O2`, `-O3` and `-Os`; clang 16.0.4 only at `-O3`; clang 15.0.6 is correct. `-O0` and `-O1` are correct on all versions (`-O1` does not run AggressiveInstCombine). The libcall-to-intrinsic rewrite in `foldSqrt` came with e079bf655832 / e3205b87655f (clang 16, then `-O3` only). With `-fno-math-errno` clang emits `llvm.experimental.constrained.sqrt` instead and the result is correct.
### Where
- `llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp`, `foldSqrt`: the rewrite `Builder.CreateIntrinsic(Intrinsic::sqrt, Ty, Arg, Call, "sqrt")` has no `Call->isStrictFP()` check. `CreateIntrinsic` copies only fast-math flags, not the `strictfp` call attribute.
- `llvm/lib/Analysis/ConstantFolding.cpp`, `canConstantFoldCallTo` asks `Call->isStrictFP()`, which is now false, so `Intrinsic::sqrt` is folded with the host rounding mode.
The ordinary libcall simplifier already has the guard: `LibCallSimplifier::optimizeFloatingPointLibCall` (`SimplifyLibCalls.cpp`) returns early `if (CI->isStrictFP())`, which is why `pow`, `log`, `exp` and `sin` in the same function stay correct.
The open PRs #201433 (math intrinsics in strictfp functions) and #190478 (constant folding of non-constrained intrinsics in strictfp functions) do not touch `AggressiveInstCombine.cpp`; here the original call is strictfp and the attribute is lost by the rewrite.
### Possible fix (not tested)
In `foldSqrt`, return false when `Call->isStrictFP()` (or the parent function is strictfp), like `optimizeFloatingPointLibCall` does.
cc @rotateright @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