[cfe-dev] [llvm-dev] Should isnan be optimized out in fast-math mode?

Chris Tetreault via cfe-dev cfe-dev at lists.llvm.org
Wed Sep 8 15:16:25 PDT 2021

Maybe not, but it will simplify the implementation of clang, and eliminating even 1 instruction is technically a speedup. If the check is in an assert, then it would ideally be removed in a release build and not matter anyways. If the check is in a branch, then that's a whole branch that can get eliminated as dead code, which may be huge if it's deep in the hot render loop.

But really, "a check for NaN" is an operation, so by the documented behavior of -ffast-math, it should assume that it does not receive NaN as an argument. Absent a compelling use case, I think consistent behavior is a very valuable thing to have. By turning on fast math, as a developer you are saying "My code doesn't have NaN, so feel free to optimizing assuming this". To then go ahead and have code that expects checks for NaN to work is kind of silly. If the user wants this behavior, they should pass -funsafe-math-optimizations (or whatever subset of the flags of fast math that they really wanted). After all, what is the point of checking for NaN if "you don't have NaN"?

Really, the problem is that `-ffast-math` is the flag that everybody knows about, so they use it and get upset when it doesn't do what they want. This is a problem of education, not something the compiler should be working around. Now, if we want to issue warnings about misuse of things like isnan or isfinite in the presence of fast math, then that would be great.

   Chris Tetreault

-----Original Message-----
From: cfe-dev <cfe-dev-bounces at lists.llvm.org> On Behalf Of Joerg Sonnenberger via cfe-dev
Sent: Wednesday, September 8, 2021 1:58 PM
To: llvm-dev at lists.llvm.org; cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] [llvm-dev] Should isnan be optimized out in fast-math mode?

WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.

On Wed, Sep 08, 2021 at 06:04:08PM +0000, Chris Tetreault via llvm-dev wrote:
> As a developer (who always reads the docs and generally makes good 
> life choices), if I turn on -ffast-math, I want the compiler to 
> produce the fastest possible floating point math code possible, 
> floating point semantics be darned. Given this viewpoint, my opinion 
> on this topic is that the compiler should do whatever it wants, given 
> the constraints of the documented behavior of NaN.

There is a huge different between optimisations that assume NaN is not present and breaking checks for them. I'm not convinced at all that constant-folding isnan to false will actually speed up real world code.

cfe-dev mailing list
cfe-dev at lists.llvm.org

More information about the cfe-dev mailing list