<div dir="ltr"><div>Let's change the example to eliminate suspects:</div>  #include <math.h><br><div>  int is_nan(float x) {</div><div>    /* </div><div>      The following subclauses provide macros that are quiet (non floating-point exception raising)<br></div><div>      versions of the relational operators, and other comparison macros that facilitate writing<br></div><div>      efficient code that accounts for NaNs without suffering the ‘‘invalid’’ floating-point exception.<br></div>    */<br>    return isunordered(x, x);<br>  }<br><div><br></div><div>The comment text is from 7.12.14 of the C standard draft. I'm hoping to avoid any scenario under which it is ok to raise an exception in that code (eliminate any questions about the clang front-end behavior / FENV_ACCESS).<br></div><div><br></div><div>As IR from clang with no optimization, this becomes a bunch of load/store with:</div>  %cmp = fcmp uno double %conv, %conv1<div><br></div><div>Ok, so far? "fcmp uno" - <a href="http://llvm.org/docs/LangRef.html#fcmp-instruction" target="_blank">http://llvm.org/docs/LangRef.html#fcmp-instruction</a> :</div>uno: yields true if either operand is a QNAN.<div><br></div><div>EarlyCSE/InstCombine reduce that fcmp to:</div><div>  %cmp = fcmp uno float %x, 0.000000e+00<br></div><div><br></div><div>Still good? Same fcmp predicate, but we replaced a repeated use of "%x" with a zero constant to aid optimization.<br></div><div><br></div><div>Now, send the optimized IR to codegen:</div><div>define i32 @is_nan(float %x) {<br>  %cmp = fcmp uno float %x, 0.000000e+00<br>  %r = zext i1 %cmp to i32<br>  ret i32 %r<br>}<br></div><div><br></div><div>$ llc -o - fpexception.ll -mtriple=armv7a <br></div><div>  vmov   s0, r0<br>  mov  r0, #0</div><div>  vcmpe.f32       s0, s0<br>  vmrs APSR_nzcv, fpscr<br>     movwvs  r0, #1<br>       bx      lr<br></div><div><br></div><div>We produced "vcmpe" for code that should never cause an FP exception. ARM codegen bug?<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Oct 1, 2019 at 5:45 AM Kristof Beyls <<a href="mailto:Kristof.Beyls@arm.com" target="_blank">Kristof.Beyls@arm.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>
Hi,<br>
<br>
I’ve been investigating <a href="https://bugs.llvm.org/show_bug.cgi?id=43374" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=43374</a>, which is about clang/llvm producing code that triggers a floating point exception when x is NaN, when targeting ARM, in the
 below code example.<br>
<br>
<div>int bar(float x) {</div>
<div>  return x!=x ? 0 : 1;</div>
<div>}</div>
<br>
<div>The C99 standard states in section 7.12.14:</div>
<div><br>
</div>
<div>"""</div>
<div>The relational and equality operators support the usual mathematical relationships between numeric values. For any ordered pair of numeric values exactly one of the relationships — less, greater, and equal — is true. Relational operators may raise
 the ‘‘invalid’’ floating-point exception when argument values are NaNs.</div>
<div>"""</div>
<div><br>
</div>
<div>My interpretation of that paragraph is that it's OK for <, <=, > and >= to raise an exception when argument values are NaNs. It is not OK for == an != to raise an exception when argument values are NaNs.</div>
<br>
<div>Therefore,</div>
<br>
<div>int bar(float x) {</div>
<div>  return x!=x ? 0 : 1;</div>
<div>}</div>
<br>
should not produce an exception when x is NaN, and hence a <font face="Menlo">vcmp</font> rather than <font face="Menlo">vcmpe</font> instruction should be produced when generating ARM code for this.
<div><br>
</div>
<div><a href="http://llvm.org/viewvc/llvm-project?rev=294945&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=294945&view=rev</a> introduced support for generating <font face="Menlo">vcmp</font> instead of <font face="Menlo">vcmpe</font>
 for equality comparisons. How come <font face="Menlo">vcmpe</font> is generated for (x!=x)?</div>
<div><br>
</div>
<div>The answer is that InstCombine transforms the equality comparison into an "ordered comparison”. Before InstCombine:</div>
<div><span style="font-family:Menlo">define dso_local i32 @bar(float %x) local_unnamed_addr {</span></div>
<div><font face="Menlo">entry:</font></div>
<div><font face="Menlo">  %cmp = fcmp une float %x, %x</font></div>
<div><font face="Menlo">  %cond = select i1 %cmp, i32 0, i32 1</font></div>
<div><font face="Menlo">  ret i32 %cond</font></div>
<div><font face="Menlo">}</font></div>
<br>
After InstCombine:<br>
<div><font face="Menlo">define dso_local i32 @bar(float %x) local_unnamed_addr #0 {</font></div>
<div><font face="Menlo">entry:</font></div>
<div><font face="Menlo">  %cmp = fcmp ord float %x, 0.000000e+00</font></div>
<div><font face="Menlo">  %cond = zext i1 %cmp to i32</font></div>
<div><font face="Menlo">  ret i32 %cond</font></div>
<div><font face="Menlo">}</font></div>
<br>
Please note that on other backends like x86 or AArch64, this InstCombine doesn’t trigger floating point exception behaviour since those backends don’t seem to be producing any instructions for fcmp that raise floating point exceptions on NaNs.<br>
<br>
My question here is: how to fix this behaviour? Or: which part in the compilation flow is wrong?<br>
Reading through various standards and specifications, I’m getting confused to what the best fix would be:<br>
<br>
<ul>
<li><a href="https://llvm.org/docs/LangRef.html#floating-point-environment" target="_blank">https://llvm.org/docs/LangRef.html#floating-point-environment</a> states "<span style="font-variant-ligatures:normal;background-color:rgb(255,255,255)">The
 default LLVM floating-point environment assumes that floating-point instructions do not have side effects. Results assume the round-to-nearest rounding mode. No floating-point exception state is maintained in this environment. Therefore, there is no attempt
 to create or preserve invalid operation (SNaN) or division-by-zero exceptions.</span><span style="font-variant-ligatures:normal;background-color:rgb(255,255,255)"><font style="font-size:14px" face="Lucida Grande, Lucida Sans Unicode, Geneva, Verdana, sans-serif">”</font><br>
This suggests that if we want to retain floating point exception behaviour in the compilation flow, we shouldn’t be using the “default LLVM floating-point environment”, but rather something else. Presumably the constrained intrinsics? However, when I look at
 the constrained intrinsics definition, it seems (</span><a href="http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics" target="_blank">http://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics</a>) there is no constrained intrinsic
 for the floating point comparison operation. Should there be one?</li><li>If the default floating-point environment assumes that floating-point instructions do not have side effects, why does the Arm backend lower floating point comparison to vcmpe rather than vcmp? The revision history suggests this has been this way
 since the initial creation of the ARM backend. Should this behaviour be changed and vcmp be produced rather than vcmpe? And only later, once the generation of constrained floating point intrinsics is implemented should backends start producing signalling floating
 point comparisons for floating point comparison constrained intrinsics (assuming they’ll exist by then)?</li><li>Or alternatively, there is a good reason to keep on producing vcmpe as is today, and instcombine just shouldn’t convert “fcmp une” into “fcmp ord”?</li><li>Or as yet another alternative, instcombine is just fine converting “fcmp une” into “fcmp ord”, and it’s the ARM backend that should produce vcmp rather than vcmpe also for “unordered” comparisons, next to equality comparisons?</li></ul>
<div><br>
</div>
<div>Thanks,</div>
<div><br>
</div>
<div>Kristof</div>
</div>

</blockquote></div>