<div dir="ltr"><div>For the following C code</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div>__fp16 x, y, z, w;</div><br><div><span style="color:rgb(0,0,255)">void</span> foo() {</div><div>  x = y + z;</div><div>  x = x + w;</div><div>}</div><div><br></div><div>clang produces IR that extends each operand to float and then truncates to half before assigning to x. Like this</div><div><br></div><div><div><div><span style="color:rgb(0,0,255)">define</span> <span style="color:rgb(0,128,128)">dso_local</span> <span style="color:rgb(0,128,128)">void</span> <span style="color:rgb(0,128,128)">@foo</span>() <span style="color:rgb(9,136,90)">#0</span> !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">18</span> {</div><div>  <span style="color:rgb(205,49,49)">%1</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@y</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">21</span></div><div>  <span style="color:rgb(205,49,49)">%2</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fpext</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">1</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">float</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">21</span></div><div>  <span style="color:rgb(205,49,49)">%3</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@z</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(205,49,49)">%4</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fpext</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">float</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(205,49,49)">%5</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fadd</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">23</span></div><div>  <span style="color:rgb(205,49,49)">%6</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fptrunc</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">5</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">half</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">21</span></div><div>  <span style="color:rgb(0,0,255)">store</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">6</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@x</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">24</span></div><div>  <span style="color:rgb(205,49,49)">%7</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@x</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">25</span></div><div>  <span style="color:rgb(205,49,49)">%8</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fpext</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">7</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">float</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">25</span></div><div>  <span style="color:rgb(205,49,49)">%9</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@w</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">26</span></div><div>  <span style="color:rgb(205,49,49)">%10</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fpext</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">9</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">float</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">26</span></div><div>  <span style="color:rgb(205,49,49)">%11</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fadd</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">8</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">10</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">27</span></div><div>  <span style="color:rgb(205,49,49)">%12</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fptrunc</span> <span style="color:rgb(0,128,128)">float</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">11</span> <span style="color:rgb(0,128,128)">to</span> <span style="color:rgb(0,128,128)">half</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">25</span></div><div>  <span style="color:rgb(0,0,255)">store</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">12</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@x</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">28</span></div><div>  <span style="color:rgb(0,0,255)">ret</span> <span style="color:rgb(0,128,128)">void</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">29</span></div><div>}</div></div></div><div><br></div><div>InstCombine then comes along and gets rid of all of the fpext and fptrunc. Leaving</div><div><br></div><div><div><div><span style="color:rgb(0,0,255)">define</span> <span style="color:rgb(0,128,128)">dso_local</span> <span style="color:rgb(0,128,128)">void</span> <span style="color:rgb(0,128,128)">@foo</span>() <span style="color:rgb(0,128,128)">local_unnamed_addr</span> <span style="color:rgb(9,136,90)">#0</span> !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">18</span> {</div><div>  <span style="color:rgb(205,49,49)">%1</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@y</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">21</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(205,49,49)">%2</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@z</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">26</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(205,49,49)">%3</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fadd</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">1</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">21</span></div><div>  <span style="color:rgb(205,49,49)">%4</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">load</span> <span style="color:rgb(0,128,128)">half</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@w</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">27</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(205,49,49)">%5</span> <span style="color:rgb(205,49,49)">=</span> <span style="color:rgb(0,0,255)">fadd</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span>, <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">28</span></div><div>  <span style="color:rgb(0,0,255)">store</span> <span style="color:rgb(0,128,128)">half</span> <span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">5</span>, <span style="color:rgb(0,128,128)">half</span>* <span style="color:rgb(0,128,128)">@x</span>, <span style="color:rgb(0,128,128)">align</span> <span style="color:rgb(9,136,90)">2</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">29</span>, !<span style="color:rgb(0,128,128)">tbaa</span> !<span style="color:rgb(9,136,90)">22</span></div><div>  <span style="color:rgb(0,0,255)">ret</span> <span style="color:rgb(0,128,128)">void</span>, !<span style="color:rgb(0,128,128)">dbg</span> !<span style="color:rgb(9,136,90)">30</span></div><div>}</div></div></div><div><br></div><div><br></div><div>Then SelectionDAG type legalization comes along and creates this as the final assembly</div><div><br></div><div><div><div>        <span style="color:rgb(0,0,255)">pushq</span>   <span style="color:rgb(72,100,170)">%rax</span></div><div>        <span style="color:rgb(0,0,255)">.cfi_def_cfa_offset</span> <span style="color:rgb(9,136,90)">16</span></div><div>        <span style="color:rgb(0,0,255)">movzwl</span>  <span style="color:rgb(0,128,128)">y</span>(<span style="color:rgb(72,100,170)">%rip</span>), <span style="color:rgb(72,100,170)">%edi</span></div><div>        <span style="color:rgb(0,0,255)">callq</span>   <span style="color:rgb(0,128,128)">__gnu_h2f_ieee</span></div><div>        <span style="color:rgb(0,0,255)">movss</span>   <span style="color:rgb(72,100,170)">%xmm0</span>, <span style="color:rgb(9,136,90)">4</span>(<span style="color:rgb(72,100,170)">%rsp</span>)          <span style="color:rgb(0,128,0)"># 4-byte Spill</span></div><div>        <span style="color:rgb(0,0,255)">movzwl</span>  <span style="color:rgb(0,128,128)">z</span>(<span style="color:rgb(72,100,170)">%rip</span>), <span style="color:rgb(72,100,170)">%edi</span></div><div>        <span style="color:rgb(0,0,255)">callq</span>   <span style="color:rgb(0,128,128)">__gnu_h2f_ieee</span></div><div>        <span style="color:rgb(0,0,255)">addss</span>   <span style="color:rgb(9,136,90)">4</span>(<span style="color:rgb(72,100,170)">%rsp</span>), <span style="color:rgb(72,100,170)">%xmm0</span>          <span style="color:rgb(0,128,0)"># 4-byte Folded Reload</span></div><div>        <span style="color:rgb(0,0,255)">movss</span>   <span style="color:rgb(72,100,170)">%xmm0</span>, <span style="color:rgb(9,136,90)">4</span>(<span style="color:rgb(72,100,170)">%rsp</span>)          <span style="color:rgb(0,128,0)"># 4-byte Spill</span></div><div>        <span style="color:rgb(0,0,255)">movzwl</span>  <span style="color:rgb(0,128,128)">w</span>(<span style="color:rgb(72,100,170)">%rip</span>), <span style="color:rgb(72,100,170)">%edi</span></div><div>        <span style="color:rgb(0,0,255)">callq</span>   <span style="color:rgb(0,128,128)">__gnu_h2f_ieee</span></div><div>        <span style="color:rgb(0,0,255)">addss</span>   <span style="color:rgb(9,136,90)">4</span>(<span style="color:rgb(72,100,170)">%rsp</span>), <span style="color:rgb(72,100,170)">%xmm0</span>          <span style="color:rgb(0,128,0)"># 4-byte Folded Reload</span></div><div>        <span style="color:rgb(0,0,255)">callq</span>   <span style="color:rgb(0,128,128)">__gnu_f2h_ieee</span></div><div>        <span style="color:rgb(0,0,255)">movw</span>    <span style="color:rgb(72,100,170)">%ax</span>, <span style="color:rgb(0,128,128)">x</span>(<span style="color:rgb(72,100,170)">%rip</span>)</div><div>        <span style="color:rgb(0,0,255)">popq</span>    <span style="color:rgb(72,100,170)">%rax</span></div></div></div><div><br></div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>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.</div><div><br></div><div>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?</div><div><br></div><div>Anyone else have any thoughts on this?</div><div><br></div><div><span style="background-color:rgb(255,255,255);color:rgb(34,34,34)">~Craig</span><br></div></div></div></div>