<div dir="ltr"><div dir="ltr"><div>On 2nd look, this really is a sqrt-specific optimization:</div><div><a href="https://reviews.llvm.org/rL357943">https://reviews.llvm.org/rL357943</a></div><div><br></div><div>I think we still want the more general fmul+fdiv canonicalization, but that transform has some risk of exposing different missing optimizations, so I'll wait a bit before trying that. <br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 5, 2019 at 9:20 AM Venkataramanan Kumar <<a href="mailto:venkataramanan.kumar.llvm@gmail.com" target="_blank">venkataramanan.kumar.llvm@gmail.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 dir="ltr"><div dir="ltr">Hi Sanjay, </div><div><br></div><div>Thanks, yes I was also thinking of looking at re association pass to see if it can catch few more cases.</div><div><br></div><div>regards,</div><div>Venkat.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 5 Apr 2019 at 19:48, Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.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 dir="ltr"><div>[adding back the mailing list] <br></div><div><br></div><div>Thanks for the example!</div><div><br></div><div><div style="color:rgb(0,0,0);background-color:rgb(255,255,254)"><div><span style="color:rgb(0,0,255)">define</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">@_Z12sqrt_squaredd</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)">)</span><span style="color:rgb(0,0,0)"> {</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%2</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">tail</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">call</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">fast</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">@llvm.sqrt.f64</span><span style="color:rgb(0,0,0)">(</span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">0</span><span style="color:rgb(0,0,0)">)</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%3</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">fdiv</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">fast</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(9,136,90)">1.000000e+00</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">2</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%4</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">=</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">fmul</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">fast</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span><span style="color:rgb(0,0,0)">, </span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">3</span></div><div><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,0,255)">ret</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(0,128,128)">double</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(205,49,49)">%</span><span style="color:rgb(9,136,90)">4</span></div><div><span style="color:rgb(0,0,0)">}</span></div><br></div></div><div>So
at first look, this has nothing to do with sqrt specifically. We are
missing a basic factorization / re-association for fmul and fdiv:<br></div><div> // (L1 / L2) * (R1 / R2) --> (L1 * R1) / (L2 * R2)<br></div><div><br></div><div>If
we do that, then existing transforms should kick in to reduce the sqrt
example. I'll try to fix this in instcombine soon. If you have
more complicated examples where we miss this, that would suggest that we
need to make an enhancement to the "reassociate" pass too.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 5, 2019 at 12:26 AM Venkataramanan Kumar <<a href="mailto:venkataramanan.kumar.llvm@gmail.com" target="_blank">venkataramanan.kumar.llvm@gmail.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 dir="ltr"><div dir="ltr"><div>Hi Sanjay, </div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 4 Apr 2019 at 21:21, Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.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 dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>1. Do you have a larger code example that shows the missed sqrt/div optimization?</div><div><br></div><div>We optimize the example you provided already in IR:</div><div>#include <math.h><br>float sqrt_squared(float x) {<br> return 1.0f/sqrtf(x) * 1.0f/sqrtf(x);<br>}<br><br></div></div></div></div></div></blockquote><div>Something like this </div><div><div style="background-color:rgb(255,255,254)"><div style="color:rgb(0,0,0)"><span style="color:rgb(0,0,255)">#include</span> <math.h></div><div style="color:rgb(0,0,0)"><span style="color:rgb(0,0,255)">double</span> sqrt_squared(<span style="color:rgb(0,0,255)">double</span> x) {</div><div style="color:rgb(0,0,0)"><span style="color:rgb(0,0,255)">double</span> y = <span style="color:rgb(9,136,90)">1.0</span>/sqrt(x) ;</div><div style="color:rgb(0,0,0)"> <span style="color:rgb(0,0,255)">double</span> z = y * y ;</div><div style="color:rgb(0,0,0)"> <span style="color:rgb(0,0,255)">return</span> z;</div><div style="color:rgb(0,0,0)">}</div><div><font color="#000000">REF: <a href="https://godbolt.org/z/2lhnJq" target="_blank">https://godbolt.org/z/2lhnJq</a></font><br></div><div><span style="background-color:rgb(255,255,255)"> </span><br></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"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div></div>$ clang -O1 -ffast-math -emit-llvm sqrt.c -S -o - <br>define float @sqrt_squared(float) local_unnamed_addr #0 {<br> %2 = fdiv fast float 1.000000e+00, %0<br> ret float %2<br>}<br><br></div><div>2. Yes, transforms like the above should work with vector types. If not, please file a bug.<br></div></div></div></div></blockquote><div>Oh! thanks let me check. </div><div> <br></div><div>regards,</div><div>Venkat.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 3, 2019 at 11:47 PM Venkataramanan Kumar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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 dir="ltr">Hi Micheal, <div><br></div><div>Thanks for the blog post. Just like to point out few things that I thought is related to FP Numerics.</div><div>LLVM could do some additional transformation with "sqrt" and "division" under fast math on X86 like 1/sqrt(x)* 1/sqrt(x) to 1/x. These are long latency instructions and could get benefit if enabled under unsafe math.</div><div><br></div><div>Also are we considering doing such FP transforms on vector floating point types?</div><div><br></div><div>regards,</div><div>Venkat.</div><div><br></div><div><br><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 1 Apr 2019 at 19:56, Cameron McInally via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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 dir="ltr"><div dir="ltr">Hey Michael,<div><br></div><div>Thank you for working on this!</div><div><br></div><div>I'd like to touch on a topic mentioned in the blog post. The constrained intrinsics work is at a road block on how to proceed with the constrained implementation in the backends, i.e. D55506. Reviews/ideas in this area would be greatly appreciated (attn: target code owners). </div><div><br></div><div>Thanks,</div><div>Cameron</div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 28, 2019 at 5:54 PM Michael Berg via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</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">All: There will be a BoF talk at the EuroLLVM conference regarding Numerics (FMF and module flags which control fp behavior and optimization).<br>
<br>
Even if you are not going to be in attendance, please reply to this thread as we are collecting open issues and ideas for future direction in all layers of LLVM for which optimizations are controlled by numerics flags.<br>
Please read over the numerics blog if you like for reference material:<br>
<br>
<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__blog.llvm.org_2019_03_llvm-2Dnumerics-2Dblog.html&d=DwIGaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=O_4M49EtSpZ_-BQYeigzGv0P4__noMcSu2RYEjS1vKs&m=CHHCGUAnxC91rtvrnhhoN0jXGXgXv8EQb8C4XiUEoxY&s=xzUyP83nqSY1h9GrDdHch4INrIFIU-Ycij4S4Tmxdko&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=http-3A__blog.llvm.org_2019_03_llvm-2Dnumerics-2Dblog.html&d=DwIGaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=O_4M49EtSpZ_-BQYeigzGv0P4__noMcSu2RYEjS1vKs&m=CHHCGUAnxC91rtvrnhhoN0jXGXgXv8EQb8C4XiUEoxY&s=xzUyP83nqSY1h9GrDdHch4INrIFIU-Ycij4S4Tmxdko&e=</a><br>
<br>
Regards,<br>
Michael<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://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwIGaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=O_4M49EtSpZ_-BQYeigzGv0P4__noMcSu2RYEjS1vKs&m=CHHCGUAnxC91rtvrnhhoN0jXGXgXv8EQb8C4XiUEoxY&s=5CVfTGSHNKAmAzx4dc0HQzDTXMucnDgx_NPimota5m8&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=https-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwIGaQ&c=slrrB7dE8n7gBJbeO0g-IQ&r=O_4M49EtSpZ_-BQYeigzGv0P4__noMcSu2RYEjS1vKs&m=CHHCGUAnxC91rtvrnhhoN0jXGXgXv8EQb8C4XiUEoxY&s=5CVfTGSHNKAmAzx4dc0HQzDTXMucnDgx_NPimota5m8&e=</a><br>
</blockquote></div>
_______________________________________________<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>
_______________________________________________<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>
</blockquote></div></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>