<div dir="ltr">Yes, formally this is UB. But removal of `isnan` is also a standard violation, because `isnan` has definite semantics, which is not preserved in this case. We already are outside the standard behavior with the `isnan` removal, so things cannot become worse.<div><br></div><div>Anyway this is a workaround for emulation only, for the (hypothetical) case, when performance dropped noticeably due to execution of the checks. It is now clear if such cases really exist.<br><div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Thanks,<br>--Serge<br></div></div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 17, 2021 at 12:56 AM Aaron Ballman <<a href="mailto:aaron@aaronballman.com">aaron@aaronballman.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, Sep 16, 2021 at 1:31 PM Serge Pavlov via cfe-dev<br>
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
><br>
> On Thu, Sep 16, 2021 at 10:02 PM Cranmer, Joshua <<a href="mailto:joshua.cranmer@intel.com" target="_blank">joshua.cranmer@intel.com</a>> wrote:<br>
>><br>
>> I think you are not adequately summing up the risks of your approach; there are three other issues I see.<br>
>><br>
>><br>
>><br>
>> First, redefining `isnan` as a macro is expressly undefined behavior in C (see section 7.1.3, clauses 2 and 3—it’s undefined behavior to define a macro as a same name as reserved identifier in a standard library header). Conditionally redefining an `isnan` macro is therefore not a permissible solution.<br>
><br>
><br>
> Defining things like `isnan` is the job of libc, which often is not a part of the compiler.<br>
<br>
The standard makes no real distinction between what's the job of the<br>
compiler and what's the job of the library; it's all "the<br>
implementation" as far as the standard is concerned. FWIW, there are<br>
plenty of libc things which are produced by the compiler (see<br>
<a href="https://github.com/llvm/llvm-project/tree/main/clang/lib/Headers" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/tree/main/clang/lib/Headers</a> for<br>
all the standard library interfaces provided by Clang).<br>
<br>
> It does not cause undefined behavior by itself.<br>
<br>
A user-defined macro named `isnan` is UB per 7.1.3p1 if the user<br>
includes <math.h> in the TU.<br>
<br>
> Redefining macro defined in system headers may be harmful if the new macro is inconsistent with other libc implementation (`errno` comes to mind). So this looks like a kind of legal disclaimer.<br>
<br>
I would hope so; the standard says explicitly that redefining that<br>
macro is UB. :-)<br>
<br>
~Aaron<br>
<br>
> As with `-ffinite-math-only` the redefinition is used at your own risk. For our task to remove the call of pure function the risk is negligible. And it is needed only to reproduce the old behavior.<br>
><br>
>><br>
>><br>
>><br>
>> The second thing that has been repeatedly brought up that is missing is the fact that `isnan` may still be inconsistently optimized out. `isnan(x)` would only be retained in the program if the compiler cannot deduce that `x` is the result of a nnan arithmetic operation. If it can deduce that—the simplest case being the somewhat questionable `isnan(x + 0)` example, but it’s also possible that, e.g., you’re calling `isnan(sum)` on the result of a summation, which would be the result of an arithmetic expression post-mem2reg/SROA—then the compiler would still elide it. It could be that this is less surprising to users than unconditionally optimizing array `isnan(x)`, but it should still be admitted that there is a potential for surprise here.<br>
><br>
><br>
> Regarding your example, this thread already contains consideration of this case:<br>
><br>
> On Tue, Sep 14, 2021 at 12:50 AM Serge Pavlov <<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>> wrote:<br>
>><br>
>> On Mon, Sep 13, 2021 at 11:46 PM Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>> wrote:<br>
>>><br>
>>> … is guaranteed to work, and I read that fast-math enables the compiler to reason about constructs like `x + 0` being equal to `x`, then I’m going to be very confused when:<br>
>><br>
>> You are right, this was a bad idea. Compiler may optimize out `isnan` but only when it deduces that the value cannot be NaN, but not due to the user's promise. It is especially important for `isinf`. Addition of two finite values may produce infinity and there is no universal way to predict it. It is probably not an issue for types like float or double, but ML cores use halfs or even minifloats, where overflow is much more probable. If in the code:<br>
>> ```<br>
>> float r = a + b;<br>
>> if (isinf(r)) {...<br>
>> ```<br>
>> `isinf` were optimized out just because -ffinite-math-only is in effect, the user cannot check if overflow did not occur.<br>
><br>
><br>
> Rules proposed by Richard are also formulated using arguments, not results. Now there is no intention to optimize such a case.<br>
><br>
>><br>
>><br>
>> A final point is that the potential optimization benefits of eliding `isnan` are not limited to the cost of running the function itself (which are likely to be negligible), but also include the benefits of deleting any subsequent code that is attempting to handle NaN values, which may be fairly large blocks. A salient example is complex multiplication and division, where the actual expansion of the multiplication and division code itself is dwarfed by the recalculation code if the result turns out to be a NaN.<br>
><br>
><br>
> The code intended for handling NaNs won't be executed in -ffinite-math-only mode, if the mode is used correctly. So expenses are only the check itself and the associated jump. For the code that does intensive calculation they must be negligible. Anyway, the user can redefine `isnan`, it is as safe as `-ffinite-math-only` itself.<br>
><br>
>><br>
>><br>
>> From: llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> On Behalf Of Serge Pavlov via llvm-dev<br>
>> Sent: Thursday, September 16, 2021 1:37<br>
>> To: Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>><br>
>> Cc: Arthur O'Dwyer <<a href="mailto:arthur.j.odwyer@gmail.com" target="_blank">arthur.j.odwyer@gmail.com</a>>; <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>; <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
>> Subject: Re: [llvm-dev] [cfe-dev] Should isnan be optimized out in fast-math mode?<br>
>><br>
>><br>
>><br>
>> Let me make some summary. I omit references for brevity, they are spread in the thread.<br>
>><br>
>> Treatment of `isnan` with `-ffinite-math-only` has issues:<br>
>> - There are many users' complaints and disagreement expressed in GCC bug tracker and forums about the treatment.<br>
>> - There are legitimate use cases when `isnan` needs to be called in `-ffinite-math-only` mode.<br>
>> - Users have to invent workarounds to get functionality of `isnan`, which results in portability and performance loss.<br>
>> - There is inconsistency with the behavior of libc, which always does a real check, and the compiler, which omits it.<br>
>> Preserving `isnan` in the code would solve all of them.<br>
>><br>
>> What is the risk?<br>
>><br>
>> `-ffinite-math-only` is an optimization option, so preserving `isnan` cannot break the behavior of correct programs. The only possible negative impact is some loss of performance. It is unlikely that a real program spends so much time in `isnan` calls that it has noticeable effect, but if it does, a user can conditionally redefine `isnan` macro.<br>
>><br>
>> Preserving `isnan` in `-ffinite-math-only` mode is safe and makes the compiler more reliable and user-friendly.<br>
>><br>
>><br>
>><br>
>> Thanks,<br>
>> --Serge<br>
><br>
> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>