clang v19+: unexpected behavior under -fno-honor-infinities and -fno-honor-nans
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
I closed the [issue](https://github.com/llvm/llvm-project/issues/191630) which was reporting **bug** of floating optimizations.
As [originally](https://github.com/llvm/llvm-project/issues/191630#issuecomment-4230663149) posted:
> But I think clang v19+ is going to optimize it too aggressively under `-ffno-inite-math-only`.
>
> [GCC doc](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
> > `-ffinite-math-only`
> > Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs.
>
> [clang doc](https://clang.llvm.org/docs/UsersManual.html)
> > `-ffinite-math-only`
> > Allow floating-point optimizations that assume arguments and results are not NaNs or +-Inf. `-ffinite-math-only` implies:
> > `-fno-honor-infinities`
> > `-fno-honor-nans`
>
> With this flag on, compilers are allowed to optimize expressions with the assumption that no NaNs or +-Infs occurs in the computation. But in my opinion, "totally get values optimized away" is not acceptable. Should only allow "simplify the expressions with the no-honoring assumptions".
>
> It is dangerous if allow compiler to totally **delete** expressions like `__builtin_inf()`, even without any warnings/errors diagnosis. In such a way, the flag `-ffinite-math-only` is too dangerous and so is `-ffast-math`, which will narrowing the usage of these flags.
The behavior of "remove nan/inf expressions" is not restricted on builtin functions. see [godbolt](https://godbolt.org/z/Mdae9qPG8).
```c++
struct Math
{
static float inf()
{
union { unsigned i; float f; } x = {0x7f800000};
return (x.f); //return __builtin_inf();
}
};
//clang v19+ will produce just a "ret" instruction with '-O1 -ffinite-math-only'
//this is unexpected: gcc and clang<=18 does not have this behavior
float asm_inf()
{
return Math::inf();
}
```
Contributor guide
Assessment
This issue has not been assessed yet.