<div dir="ltr"><div dir="ltr">Thank you for summarizing!<div><br></div><div>Let's divide topics.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">1. We want isnan() for strict-FP support. The arguments are similar to why we added "fneg" as a real instruction.<br>2. We also need a bunch of other FP classify functions for strict-FP support to properly deal with SNAN: isinf, isfinite, isnormal, issubnormal, iszero, fpclassify.</blockquote><div><br></div><div>Correct. It looks like we have consensus on this topic and agree that `isnan` as well as other classification intrinsics are necessary. If it is so then we discuss only behavior of the intrinsic in fast-math mode.</div><div><br></div><div>The problem with fast-math is that it is unclear what guarantees the user provides to the compiler by using this option. It is not clearly documented anywhere. Implicit understanding is usually one of these:</div><div><br></div><div>1. Values of type `double` never are NaNs.</div><div>2. Arithmetic operations never get or produce NaN values.</div><div><br></div><div>If we adhere to the first approach, then `isnan` may be optimized out, because NaNs are not in the set of possible values for `double`. This is the understanding used in the aforementioned discussion in the GCC mail list. As follows from the discussion, this is a pretty fruitless way. It means `double` in `fast-math` compilation is a different type than regular `double`, they relate to each other like C enumerations or Pascal subranges. In this approach we must answer the questions:</div><div><br></div><div>- How would the user check data that come from a function that was compiled without `fast-math`? They cannot write `assert(isnan(x))` if `isnan` is optimized out and there is no conversion between two different flavors of `double`.</div><div>- What should return `std::numeric_limits<double>::has_quiet_NaN()`?</div><div>- What body should have this function if it is used in a program where some files are compiled with `fast-math` and some without?</div><div>- Should inlining of a function compiled with `fast-math` to a function compiled without it be prohibited in inliner?</div><div>- Should `std::isnan(std::numeric_limits<float>::quiet_NaN())` be true?</div><div><br></div><div>In the cited GCC mail discussion it was found that consistent implementation of this approach requires tremendous efforts and eventually requires changes of C standard.</div><div><br></div><div>In the second approach the guarantees are similar to those implied in integer division - we assume the divisor is not zero. It is consistent with llvm IR, where fast math flags are properties of an operation, not a type. None of the listed problems do not rise in this case. At the same time it allows optimizations like `0 * x -> 0`, which was the main motivation for this mode.There must be severe reasons to reject this approach in favour of the first.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">5. The change to the regular FP env would make clang/LLVM behave differently than GCC as noted here: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949" target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949</a> . That bug report has been cited as motivation, but IIUC, the last comment says everything is behaving as expected and the report should be closed pending a C/C++ language change.</blockquote><div><br></div><div>I know nothing about such a pending C/C++ language change. Could you please share the references?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">An argument was made that because the original patch has been in main for a few weeks, and there are no known bug reports, that it is fine as-is. I disagree with that: it's not easy to recognize the potential harm (likely small perf regression), not many users live/test continuously against trunk for FP fast-math perf, and it's not easy to file bugs against LLVM.</blockquote><div><br></div><div>Note that leaving `isnan` in the code compiled with `fast-math` is at most a missed optimization. It **does not** break semantics. Hardly it brings noticeable performance regression. It could if the code had many calls to 'isnan', but it is unlikely for real code that is compiled with `fast-math`. Quite the contrary, there are users who have to use integer arithmetics to check for NaN values, because they come from outside (memory, other modules), where `fast-math` is not used. Users expect that `isnan` behaves according to C standard but it does not due to this optimization.</div><div><br></div><div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">Thanks,<br>--Serge<br></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 1, 2021 at 10:07 PM Sanjay Patel <<a href="mailto:spatel@rotateright.com">spatel@rotateright.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"><div dir="ltr"><div>I'll take a shot at summarizing where we are. Correct as needed:</div><div>1. We want isnan() for strict-FP support. The arguments are similar to why we added "fneg" as a real instruction.<br></div><div>2. We also need a bunch of other FP classify functions for strict-FP support to properly deal with SNAN: isinf, isfinite, isnormal, issubnormal, iszero, fpclassify.</div><div>3. There's a 2nd motivation to use at least some of these functions in the regular LLVM FP environment with fast-math. For isnan(), this boils down to is "fcmp ord nnan" always true, poison, or unknown? So the root cause might really be that we shouldn't have fast-math-flags on fcmp (see <a href="https://bugs.llvm.org/show_bug.cgi?id=38086" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=38086</a> and related bugs).<br></div><div>4. The change to the regular FP env requires adding/changing documentation to the LLVM LangRef and clang (James posted a draft in a reply on 8/24).</div><div>5. The change to the regular FP env would make clang/LLVM behave differently than GCC as noted here: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949" target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949</a> . That bug report has been cited as motivation, but IIUC, the last comment says everything is behaving as expected and the report should be closed pending a C/C++ language change.<br></div><div><br></div><div>Several people have suggested reverting the original patch, so we can address this both at larger scale (so we have a clear plan for the other functions that are needed) and with smaller steps (so we don't break things).<br></div><div><br></div><div>An argument was made that because the original patch has been in main for a few weeks, and there are no known bug reports, that it is fine as-is. I disagree with that: it's not easy to recognize the potential harm (likely small perf regression), not many users live/test continuously against trunk for FP fast-math perf, and it's not easy to file bugs against LLVM.<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 25, 2021 at 7:13 AM Serge Pavlov via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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"><div dir="ltr"><div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949" rel="noreferrer" target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949</a> was mentioned as<br>motivational, but i don't see any resolution there,<br>it's not even in "confirmed" state.</blockquote><div><br></div><div>I agree, this is not at all clear evidence as to GCC's position on the matter.</div></blockquote><div><br></div><div>Sure, but that demonstrates that they are inclined to such interpretation.<br><br>GCC mail list has relevant discussion on this topic: <a href="https://gcc.gnu.org/pipermail/gcc-patches/2020-April/544622.html" target="_blank">https://gcc.gnu.org/pipermail/gcc-patches/2020-April/544622.html</a>. They tried to make the documentation about -ffinite-math-only clearer. The discussion was based on a view that if -ffinite-math-only was specified, a floating point value cannot be NaN or Inf. During the discussion an interesting observation was made:<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">… double and double under -ffinite-math-only are different types. Any function call from<br>one world to the other is dangerous. Inline functions translated in different<br>TUs compiled with different math flags violate the ODR.</blockquote><div><br></div><div>If different parts of a program are compiled with and without 

-ffinite-math-only doubles of different flavors can intermix. In the code compiled with -ffast-math a user cannot check assumption about the value by calling "assert(!isnan(x));" because `isnan` is replaced with expected value due to optimizations. The only usable solution in this case could be UBSAN, which is a much heavier solution. <br><br>Two different flavors of double require different mangling. Template specializations also must be different, in particular, specializations of `std::numeric_limits<T>` must be different for these two double types, the methods `has_infinity` and `has_quite_NaN` must return different values.<br><br>They agree that it is profitable to treat NaNs and Infs separately. In this case there would be 4 different double types. It is not clear what to do with constexpr expressions, should the compiler treat using NaN/Inf as undefined behavior even if the ultimate result is finite?<br></div><div><br></div><div>Participants agree that such optimizations are not good:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> … the example of simplifying x * 0 to 0 is about preserving NaNs<br>during expression simplification when the FPU would.  I think these<br>kind of optimizations are what originally was intended to be allowed<br>with -ffinite-math-only - that we started to simplify isnan(x) to false<br>was extending the scope and that change wasn't uncontested since<br>it makes -ffinite-math-only less useful to some people.</blockquote><br>Eventually they came to conclusion:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">… if we want a version of<br>-ffinite-math-only that's well-behaved in language terms (including<br>in terms of the macros that are defined and in the values of<br>numeric_limits), perhaps this should be an official (optional) C/C++<br>extension that defines what the rules are.</blockquote></div><div><br></div><div><div>Thanks,<br></div><div dir="ltr">--Serge<br></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Aug 25, 2021 at 4:25 AM James Y Knight <<a href="mailto:jyknight@google.com" target="_blank">jyknight@google.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"><div dir="ltr"><div dir="ltr">On Tue, Aug 24, 2021 at 1:53 PM Roman Lebedev via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Regardless of everything, i would like to see a patch that restores<br>
the -ffast-math handling, and *then* the RFC on what the is-nan check<br>
should do when -ffast-math is present.<br>
It is more than possible that the RFC will succeed,<br>
but i don't think a change like that should happen the way it did.</blockquote><div><br></div><div>I find the rationale to be convincing, as to the need for a change. But, the scope of the proposal is too narrow. We cannot discuss fast-math semantics changes <i>only</i> for "isnan", it needs to be in the context of the desired behavior for all operations -- the RFC should cover the entire set of changes we want to eventually make, even if isnan is the only thing implemented so far. Discussing this greater scope could result in a different desired implementation, rather than simply adding "llvm.isnan" intrinsic.</div><div><br></div><div>Yet, even with that expanded scope, the two halves of the proposal are still going to be closely linked, so I suspect it still makes sense to discuss both the strict-fp and fast-math changes in a single RFC.</div><div><br></div><div>Anyhow, for the fast-math section, I believe the proposed semantics ought to be:</div><div>  The -ffinite-math-only and -fno-signed-zeros options do not impact the ability to accurately load, store, copy, or pass or return such values from general function calls. They also do not impact any of the "non-computational" and "quiet-computational" IEEE-754 operations, which includes classification functions (fpclassify, signbit, isinf/isnan/etc), sign-modification (copysign, fabs, and negation `-(x)`), as well as the totalorder and totalordermag functions. Those correctly handle NaN, Inf, and signed zeros even when the flags are in effect. These flags <i>do</i> affect the behavior of other expressions and math standard-library calls, as well as comparison operations.</div><div><br></div><div>I would not expect this to have an actual negative impact on the performance benefit of those flags, since the optimization benefits mainly arise from comparisons and the general computation instructions which are unchanged.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949" rel="noreferrer" target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84949</a> was mentioned as<br>
motivational, but i don't see any resolution there,<br>
it's not even in "confirmed" state.</blockquote><div><br></div><div>I agree, this is not at all clear evidence as to GCC's position on the matter.</div></div></div>
</blockquote></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div></div>