<div dir="ltr">Softening them in type legalization? We still need a chain to order the libcall don't we?<div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Dec 10, 2019 at 3:44 PM Eli Friedman <<a href="mailto:efriedma@quicinc.com">efriedma@quicinc.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 lang="EN-US">
<div class="gmail-m_5202774751670211812WordSection1">
<p class="MsoNormal">strict fp_to_f16 is influenced by the rounding mode… but only in the case where it isn’t exact.  So you could assume that in strict mode, any bitcast/store has an exact operand, and use a random chain, I guess.  That’s pretty fragile, though;
 probably simpler to change legalization to soften them.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">-Eli<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> Craig Topper <<a href="mailto:craig.topper@gmail.com" target="_blank">craig.topper@gmail.com</a>> <br>
<b>Sent:</b> Tuesday, December 10, 2019 3:35 PM<br>
<b>To:</b> Eli Friedman <<a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a>><br>
<b>Cc:</b> llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Tim Northover <<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>><br>
<b>Subject:</b> [EXT] Re: TypePromoteFloat loses intermediate rounding operations<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Thanks Eli.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I forgot to bring up the strict FP questions which I was working on when I found this. If we're in a strict FP function, do the fp_to_f16/f16_to_fp emitted by promoting load/store/bitcast need to be strict versions of fp_to_f16/f16_to_fp.
 And if so where do we get the chain, especially for the bitcast case which isn't a chained node.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<div>
<p class="MsoNormal">~Craig<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Tue, Dec 10, 2019 at 3:18 PM Eli Friedman <<a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-top:none;border-right:none;border-bottom:none;border-left:1pt solid rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">We could fix the legalization without touching the other handling by just inserting an fp_to_f16/f16_to_fp pair after each arithmetic operation that requires it.  One advantage
 to that approach is that it’s easier to take the obvious shortcut for fast-math.<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">The “promote-to-larger” strategy doesn’t really round correctly in general, but it works for specific pairs of operator/operation.  For example, for f16 fadd in the default rounding
 mode, “a+b” is exactly equivalent to “(_Float16)((float)a+(float)b)”.  Not sure if this works for all f16 operations, and not sure how much we care if it doesn’t.<u></u><u></u></p>
<p class="MsoNormal">There aren’t any calling convention implications here for ARM targets; not sure about other targets.  On 32-bit ARM, clang explicitly coerces half values to a legal type.  And half
 is always legal on AArch64 (unless you force soft-float, but at that point we don’t care).<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<p class="MsoNormal">-Eli<u></u><u></u></p>
<p class="MsoNormal"> <u></u><u></u></p>
<div style="border-top:none;border-right:none;border-bottom:none;border-left:1.5pt solid blue;padding:0in 0in 0in 4pt">
<div>
<div style="border-right:none;border-bottom:none;border-left:none;border-top:1pt solid rgb(225,225,225);padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> Craig Topper <<a href="mailto:craig.topper@gmail.com" target="_blank">craig.topper@gmail.com</a>>
<br>
<b>Sent:</b> Tuesday, December 10, 2019 12:18 PM<br>
<b>To:</b> llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Eli Friedman <<a href="mailto:efriedma@quicinc.com" target="_blank">efriedma@quicinc.com</a>>; Tim Northover <<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>><br>
<b>Subject:</b> [EXT] TypePromoteFloat loses intermediate rounding operations<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"> <u></u><u></u></p>
<div>
<div>
<p class="MsoNormal">For the following C code<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"> <u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">__fp16 x, y, z, w;</span><u></u><u></u></p>
</div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">void</span><span style="color:black"> foo() {</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">x = y + z;</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">x = x + w;</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">}</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">clang produces IR that extends each operand to float and then truncates to half before assigning to x. Like this</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">define</span><span style="color:black"> </span><span style="color:teal">dso_local</span><span style="color:black">
</span><span style="color:teal">void</span><span style="color:black"> </span><span style="color:teal">@foo</span><span style="color:black">()
</span><span style="color:rgb(9,136,90)">#0</span><span style="color:black"> !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">18</span><span style="color:black"> {</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%1</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@y</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">21</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%2</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fpext</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">1</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">21</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%3</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@z</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%4</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fpext</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%5</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fadd</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span><span style="color:black">,
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">23</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%6</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fptrunc</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">5</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">21</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">store</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">6</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@x</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">24</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%7</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@x</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">25</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%8</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fpext</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">7</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">25</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%9</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@w</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">26</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%10</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fpext</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">9</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">26</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%11</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fadd</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">8</span><span style="color:black">,
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">10</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">27</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%12</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fptrunc</span><span style="color:black"> </span><span style="color:teal">float</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">11</span><span style="color:black">
</span><span style="color:teal">to</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">25</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">store</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">12</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@x</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">28</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">ret</span><span style="color:black"> </span><span style="color:teal">void</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">29</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">}</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">InstCombine then comes along and gets rid of all of the fpext and fptrunc. Leaving</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">define</span><span style="color:black"> </span><span style="color:teal">dso_local</span><span style="color:black">
</span><span style="color:teal">void</span><span style="color:black"> </span><span style="color:teal">@foo</span><span style="color:black">()
</span><span style="color:teal">local_unnamed_addr</span><span style="color:black">
</span><span style="color:rgb(9,136,90)">#0</span><span style="color:black"> !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">18</span><span style="color:black"> {</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%1</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@y</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">21</span><span style="color:black">,
 !</span><span style="color:teal">tbaa</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%2</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@z</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">26</span><span style="color:black">,
 !</span><span style="color:teal">tbaa</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%3</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fadd</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">1</span><span style="color:black">,
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">21</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%4</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">load</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@w</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">27</span><span style="color:black">,
 !</span><span style="color:teal">tbaa</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(205,49,49)">%5</span><span style="color:black"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:black">
</span><span style="color:blue">fadd</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span><span style="color:black">,
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">28</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">store</span><span style="color:black"> </span><span style="color:teal">half</span><span style="color:black">
</span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">5</span><span style="color:black">,
</span><span style="color:teal">half</span><span style="color:black">* </span><span style="color:teal">@x</span><span style="color:black">,
</span><span style="color:teal">align</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">2</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">29</span><span style="color:black">,
 !</span><span style="color:teal">tbaa</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">22</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">ret</span><span style="color:black"> </span><span style="color:teal">void</span><span style="color:black">, !</span><span style="color:teal">dbg</span><span style="color:black"> !</span><span style="color:rgb(9,136,90)">30</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">}</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">Then SelectionDAG type legalization comes along and creates this as the final assembly</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">pushq</span><span style="color:black"> </span><span style="color:rgb(72,100,170)">%rax</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">.cfi_def_cfa_offset</span><span style="color:black"> </span>
<span style="color:rgb(9,136,90)">16</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movzwl</span><span style="color:black"> </span><span style="color:teal">y</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rip</span><span style="color:black">),
</span><span style="color:rgb(72,100,170)">%edi</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">callq</span><span style="color:black"> </span><span style="color:teal">__gnu_h2f_ieee</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movss</span><span style="color:black"> </span><span style="color:rgb(72,100,170)">%xmm0</span><span style="color:black">,
</span><span style="color:rgb(9,136,90)">4</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rsp</span><span style="color:black">)
</span><span style="color:green"># 4-byte Spill</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movzwl</span><span style="color:black"> </span><span style="color:teal">z</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rip</span><span style="color:black">),
</span><span style="color:rgb(72,100,170)">%edi</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">callq</span><span style="color:black"> </span><span style="color:teal">__gnu_h2f_ieee</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">addss</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">4</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rsp</span><span style="color:black">),
</span><span style="color:rgb(72,100,170)">%xmm0</span><span style="color:black"> </span>
<span style="color:green"># 4-byte Folded Reload</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movss</span><span style="color:black"> </span><span style="color:rgb(72,100,170)">%xmm0</span><span style="color:black">,
</span><span style="color:rgb(9,136,90)">4</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rsp</span><span style="color:black">)
</span><span style="color:green"># 4-byte Spill</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movzwl</span><span style="color:black"> </span><span style="color:teal">w</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rip</span><span style="color:black">),
</span><span style="color:rgb(72,100,170)">%edi</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">callq</span><span style="color:black"> </span><span style="color:teal">__gnu_h2f_ieee</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">addss</span><span style="color:black"> </span><span style="color:rgb(9,136,90)">4</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rsp</span><span style="color:black">),
</span><span style="color:rgb(72,100,170)">%xmm0</span><span style="color:black"> </span>
<span style="color:green"># 4-byte Folded Reload</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">callq</span><span style="color:black"> </span><span style="color:teal">__gnu_f2h_ieee</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">movw</span><span style="color:black"> </span><span style="color:rgb(72,100,170)">%ax</span><span style="color:black">,
</span><span style="color:teal">x</span><span style="color:black">(</span><span style="color:rgb(72,100,170)">%rip</span><span style="color:black">)</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:blue">popq</span><span style="color:black"> </span><span style="color:rgb(72,100,170)">%rax</span><u></u><u></u></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">I assumed SelectionDAG should produce something equivalent to the original clang code with 4 total extends to f32 and 2 truncates. Instead we got 3 extends and 1 truncate. So we lost the intermediate rounding between the 2 adds that
 was in the original clang IR.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">I believe this occurs because the TypePromoteFloat legalization converts all arithmetic operations to their f32 equivalents, but does not place conversions to/from half around them. Instead fp_to_f16 and f16_to_fp nodes are only generated
 at loads, stores, bitcasts, and a probably a few other places. Basically only the place where the 16-bit size is needed to make the operation possible. Basically what we have is a very similar implementation to promoting integers, but that doesn't work for
 FP because we lose out on intermediate rounding.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">It seems like what we should instead do is insert fp16_to_fp and fp_to_fp16 in the libcall and arithmetic op handling. And use i16 to connect the legalized pieces together. Similar to how we use integer types when softening operations.
 I'm not sure if there would still be rounding issues with this, but it seems closer to matching the IR.</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">Unfortunately, I think this would have the side effect of changing half arguments and return types to i16 instead of float, which would be an ABI change. At least on some targets __fp16 can't be used as an argument or return type so
 maybe that won't be a real problem?</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black">Anyone else have any thoughts on this?</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:black"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal" style="background:rgb(255,255,254)">
<span style="color:rgb(34,34,34);background:white">~Craig</span><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>

</blockquote></div>