<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Hi,<br class="">
<br class="">
I’ve been investigating <a href="https://bugs.llvm.org/show_bug.cgi?id=43374" class="">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 class="">
<br class="">
<div class="">int bar(float x) {</div>
<div class="">  return x!=x ? 0 : 1;</div>
<div class="">}</div>
<br class="">
<div class="">The C99 standard states in section 7.12.14:</div>
<div class=""><br class="">
</div>
<div class="">"""</div>
<div class="">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 class="">"""</div>
<div class=""><br class="">
</div>
<div class="">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 class="">
<div class="">Therefore,</div>
<br class="Apple-interchange-newline">
<div class="">int bar(float x) {</div>
<div class="">  return x!=x ? 0 : 1;</div>
<div class="">}</div>
<br class="">
should not produce an exception when x is NaN, and hence a <font face="Menlo" class="">vcmp</font> rather than <font face="Menlo" class="">vcmpe</font> instruction should be produced when generating ARM code for this.
<div class=""><br class="">
</div>
<div class=""><a href="http://llvm.org/viewvc/llvm-project?rev=294945&view=rev" class="">http://llvm.org/viewvc/llvm-project?rev=294945&view=rev</a> introduced support for generating <font face="Menlo" class="">vcmp</font> instead of <font face="Menlo" class="">vcmpe</font>
 for equality comparisons. How come <font face="Menlo" class="">vcmpe</font> is generated for (x!=x)?</div>
<div class=""><br class="">
</div>
<div class="">The answer is that InstCombine transforms the equality comparison into an "ordered comparison”. Before InstCombine:</div>
<div class=""><span style="font-family: Menlo;" class="">define dso_local i32 @bar(float %x) local_unnamed_addr {</span></div>
<div class=""><font face="Menlo" class="">entry:</font></div>
<div class=""><font face="Menlo" class="">  %cmp = fcmp une float %x, %x</font></div>
<div class=""><font face="Menlo" class="">  %cond = select i1 %cmp, i32 0, i32 1</font></div>
<div class=""><font face="Menlo" class="">  ret i32 %cond</font></div>
<div class=""><font face="Menlo" class="">}</font></div>
<br class="">
After InstCombine:<br class="">
<div class=""><font face="Menlo" class="">define dso_local i32 @bar(float %x) local_unnamed_addr #0 {</font></div>
<div class=""><font face="Menlo" class="">entry:</font></div>
<div class=""><font face="Menlo" class="">  %cmp = fcmp ord float %x, 0.000000e+00</font></div>
<div class=""><font face="Menlo" class="">  %cond = zext i1 %cmp to i32</font></div>
<div class=""><font face="Menlo" class="">  ret i32 %cond</font></div>
<div class=""><font face="Menlo" class="">}</font></div>
<br class="">
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 class="">
<br class="">
My question here is: how to fix this behaviour? Or: which part in the compilation flow is wrong?<br class="">
Reading through various standards and specifications, I’m getting confused to what the best fix would be:<br class="">
<br class="">
<ul class="MailOutline">
<li class=""><a href="https://llvm.org/docs/LangRef.html#floating-point-environment" class="">https://llvm.org/docs/LangRef.html#floating-point-environment</a> states "<span style="font-variant-ligatures: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255);" class="">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; orphans: 2; widows: 2; background-color: rgb(255, 255, 255);" class=""><font face="Lucida Grande, Lucida Sans Unicode, Geneva, Verdana, sans-serif" style="font-size: 14px;" class="">”</font><br class="">
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" class="">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 class="">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 class="">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 class="">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 class=""><br class="">
</div>
<div class="">Thanks,</div>
<div class=""><br class="">
</div>
<div class="">Kristof</div>
</body>
</html>