<div dir="ltr"><div>If we cannot come to a solution, it could be a good chance to get things moving.</div><div>It is not clear to me how this topic can be interesting for standardization and would the people discuss it.</div><div>But if there is such a possibility, I would be happy to use it.</div><div><br></div><div dir="ltr"><div><div dir="ltr" data-smartmail="gmail_signature">Thanks,<br>--Serge<br></div></div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Sep 14, 2021 at 12:09 AM Aaron Ballman 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">Sorry for fat-fingering my previous attempt at a response.<br>
<br>
If it would be helpful, I am happy to put people in touch with the<br>
WG14 C Floating Point study group. Then these questions can be asked<br>
of a wider audience of compiler vendors to see if there's some common<br>
thoughts on the subject, even if WG14 won't have an "official" stance<br>
because the behavior is wholly a matter of QoI. I'd request that we<br>
nominate one person from the LLVM community to hold that discussion<br>
and report back so we don't throw the study group into the deep end of<br>
our pool (and given my lack of domain knowledge, I'd appreciate it if<br>
that someone was not me).<br>
<br>
~Aaron<br>
<br>
On Mon, Sep 13, 2021 at 12:46 PM Chris Tetreault via cfe-dev<br>
<<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Honestly, we can do this until the end of time. I think we both agree, that for either scheme, there exists workarounds. The question is which workarounds are more palatable, which is a matter of opinion. I think we’ve come to an impasse, so let me just state that my opinion on the question “Should isnan be optimized out in fast-math mode?” is “Yes”, which is what you asked to get in your original message. I think that the implementation of fast-math will be cleaner if we don’t special case a bunch of random constructs in order to do what the user meant instead of what they said. I think fast-math is a notorious footgun, and any attempts to mitigate this will only reduce the effectiveness of the tool, while not really improving the user experience.<br>
><br>
><br>
><br>
> As a user, if I read that:<br>
><br>
><br>
><br>
> ```<br>
><br>
> if (isnan(x)) {<br>
><br>
> ```<br>
><br>
><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>
><br>
><br>
> ```<br>
><br>
> if (isnan(x + 0)) {<br>
><br>
> ```<br>
><br>
><br>
><br>
> … does not also work. I’m going to open a bug and complain, and the slide down the slippery slope will continue. You and I understand the difference, and the technical reason why `isnan(x)` is supported but `isnan(x + 0)` isn’t, but Joe Coder just trying to figure out why he’s got NaN in his matrices despite his careful NaN handling code. Joe is not a compiler expert, and on the face of it, it seems like a silly limitation. This will never end until fast-math is gutted.<br>
><br>
><br>
><br>
> Thanks,<br>
><br>
> Chris Tetreault<br>
><br>
><br>
><br>
> From: Serge Pavlov <<a href="mailto:sepavloff@gmail.com" target="_blank">sepavloff@gmail.com</a>><br>
> Sent: Friday, September 10, 2021 9:21 PM<br>
> To: Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>><br>
> Cc: Richard Smith <<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</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>
> WARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.<br>
><br>
> On Sat, Sep 11, 2021 at 2:39 AM Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>> wrote:<br>
><br>
> The problem is that math code is often templated, so `template <typename T> MyMatrixT<T> safeMul(const MyMatrixT<T> & lhs …` is going to be in a header.<br>
><br>
><br>
><br>
> No problem, the user can write:<br>
><br>
> ```<br>
><br>
> #ifdef __FAST_MATH__<br>
><br>
> #undef isnan<br>
> #define isnan(x) false<br>
><br>
> #endif<br>
><br>
> ```<br>
> and put it somewhere in the headers.<br>
><br>
><br>
><br>
> On Sat, Sep 11, 2021 at 2:39 AM Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>> wrote:<br>
><br>
> Regardless, my position isn’t “there is no NaN”. My position is “you cannot count on operations on NaN working”.<br>
><br>
><br>
><br>
> Exactly. Attempts to express the condition of -ffast-math as restrictions on types are not fruitful. I think it is the reason why GCC documentation does not use simple and clear "there is no NaN" but prefers more complicated wording about arithmetic.<br>
><br>
><br>
><br>
> On Sat, Sep 11, 2021 at 2:39 AM Chris Tetreault <<a href="mailto:ctetreau@quicinc.com" target="_blank">ctetreau@quicinc.com</a>> wrote:<br>
><br>
> I think working around these sorts of issues is something that C and C++ developers are used to. These sorts of “inconsistent” between compilers behaviors is something we accept because we know it comes with improved performance. In this case, the fix is easy, so I don’t think this corner case is worth supporting. Especially when the fix is also just one line:<br>
> ```<br>
> #define myIsNan(x) (reinterpret_cast<uint32_t>(x) == THE_BIT_PATTERN_OF_MY_SENTINEL_NAN)<br>
> ```<br>
><br>
><br>
><br>
> It won't work in this way. If `x == 5.0`, then `reinterpret_cast<uint32_t>(x) == 5`. What you need there is a bitcast. Standard C does not have such. To emulate it a reinterpret_cast of memory can be used: `*reinterpret_cast<int *>(&x)`. Another way is to use a union. Both these solutions require operations with memory, which is not good for performance, especially on GPU and ML cores. Of course, a smart compiler can eliminate memory operation, but it does not have to do it always, as it is only optimization. Moving a value between float and integer pipelines also may incur a performance penalty. At the same time this check often may be done with a single instruction.<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>
_______________________________________________<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></div>