<div dir="ltr"><div dir="ltr">On Wed, Jul 14, 2021 at 12:45 AM Hongtao Liu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-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">> ><br>
> Set excess_precision_type to FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16 to<br>
> round after each operation could keep semantics right.<br>
> And I'll document the behavior difference between soft-fp and<br>
> AVX512FP16 instruction for exceptions.<br>
I got some feedback from my colleague who's working on supporting<br>
_Float16 for llvm.<br>
The LLVM side wants to set FLT_EVAL_METHOD_PROMOTE_TO_FLOAT for<br>
soft-fp so that codes can be more efficient.<br>
i.e.<br>
_Float16 a, b, c, d;<br>
d = a + b + c;<br>
<br>
would be transformed to<br>
float tmp, tmp1, a1, b1, c1;<br>
a1 = (float) a;<br>
b1 = (float) b;<br>
c1 = (float) c;<br>
tmp = a1 + b1;<br>
tmp1 = tmp + c1;<br>
d = (_Float16) tmp;<br>
<br>
so there's only 1 truncation in the end.<br>
<br>
if users want to round back after every operation. codes should be<br>
explicitly written as<br>
_Float16 a, b, c, d, e;<br>
e = a + b;<br>
d = e + c;<br>
<br>
That's what Clang does, quote from [1]<br>
_Float16 arithmetic will be performed using native half-precision<br>
support when available on the target (e.g. on ARMv8.2a); otherwise it<br>
will be performed at a higher precision (currently always float) and<br>
then truncated down to _Float16. Note that C and C++ allow<br>
intermediate floating-point operands of an expression to be computed<br>
with greater precision than is expressible in their type, so Clang may<br>
avoid intermediate truncations in certain cases; this may lead to<br>
results that are inconsistent with native arithmetic.<br></blockquote><div><br></div><div>Clang for AArch64 promotes each individual operation and rounds immediately afterwards. <a href="https://godbolt.org/z/qzGfv6nvo">https://godbolt.org/z/qzGfv6nvo</a> note the fcvts between the two fadd operations. It's implemented in the LLVM backend where we can't see what was originally a single expression.</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">
<br>
and so does arm gcc<br>
quote from arm.c<br>
<br>
/* We can calculate either in 16-bit range and precision or<br>
32-bit range and precision. Make that decision based on whether<br>
we have native support for the ARMv8.2-A 16-bit floating-point<br>
instructions or not. */<br>
return (TARGET_VFP_FP16INST<br>
? FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16<br>
: FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);<br>
<br>
<br>
[1]<a href="https://clang.llvm.org/docs/LanguageExtensions.html" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/LanguageExtensions.html</a><br>
> > --<br>
> > Joseph S. Myers<br>
> > <a href="mailto:joseph@codesourcery.com" target="_blank">joseph@codesourcery.com</a><br>
><br>
><br>
><br>
> --<br>
> BR,<br>
> Hongtao<br>
<br>
<br>
<br>
-- <br>
BR,<br>
Hongtao<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>