<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Hi,
<div class=""><br class="">
</div>
<div class="">The cost model for this transform is really difficult to get right. The latencies and throughputs for VRSQRTE/SQRT vary between microarchitectures but in general it is fair to say that;</div>
<div class=""><br class="">
</div>
<div class="">* There are two possible fast sequences for calculating 1/sqrt(x):</div>
<div class="">  a) (1 / x)  * sqrt(x)  (DIV, SQRT, MUL, where the DIV and SQRT are *independent* and can issue in parallel)</div>
<div class="">  b) VRSQRTE + s*(VRSQRTS + MUL + MUL) where s is the number of newton-raphson steps required - 2 for 32-bit floats and 4 for doubles.</div>
<div class=""><br class="">
</div>
<div class="">* SQRT and DIV are iterative instructions and commonly the hardware for this, because it must iterate, is not pipelined.</div>
<div class="">  * As a consequence of this, these instructions can also commonly *exit early* if the calculation converges early.</div>
<div class=""><br class="">
</div>
<div class="">* SQRT and DIV will almost always have a shorter latency than the equivalent VRSQRTE sequence due to the sheer number of instructions in that sequence and the early exit capability of SQRT/DIV.</div>
<div class=""><br class="">
</div>
<div class="">So the calculation on which to choose depends on several factors:</div>
<div class="">  1) Is the calculation throughput or latency limited? This loop is throughput limited - the result of the sqrt is not on the cyclic critical path, so we expect to be able to vectorize it or at least look ahead and have the core execute multiple
 iterations in parallel. We’d probably then want to use VRSQRTE.</div>
<div class="">     for (int i = 0; i < n; ++i)</div>
<div class="">       p[i] = 1.0 / q[i];</div>
<div class=""><br class="">
</div>
<div class="">      This loop is latency limited. Here, we don’t care about throughput as only one iteration can ever be executed in the core at once due to the critical path. We’d want to optimise for latency over anything else, so we’d use SQRT + DIV.</div>
<div class="">      for (int i = 0; i < n; ++i)</div>
<div class="">        p = 1.0 / p + q[i];</div>
<div class=""><br class="">
</div>
<div class="">  2) Can a SQRT and DIV execute in parallel on the microarchitecture? both these instructions use similar hardware, so it’s possible that they both need the same functional unit that isn’t pipelined. If so, the sqrt() sequence’s latency gets drastically
 increased and the profitability calculation changes.</div>
<div class=""><br class="">
</div>
<div class="">The major one is latency versus throughput. This is very difficult to do at the IR level, but at the MachineInstr level we have MachineTraceMetrics which is able to analyze loops and obtain their functional unit usage (“resource height”) and critical
 path length (“depth”). Using these two metrics we can determine if it’d make sense to swap the SQRT for a VRSQRTE.</div>
<div class=""><br class="">
</div>
<div class="">So in summary it is a hard problem with a difficult cost model, that can only reasonably be done at the MachineInstr level.</div>
<div class=""><br class="">
</div>
<div class="">Cheers,</div>
<div class=""><br class="">
</div>
<div class="">James</div>
<div class=""><br class="">
</div>
<div class="">
<div>
<blockquote type="cite" class="">
<div class="">On 8 Sep 2016, at 10:32, Jojo Ma <<a href="mailto:jojo.ma@linaro.org" class="">jojo.ma@linaro.org</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">
<div class="">Hi All,</div>
<div class=""><br class="">
</div>
<div class="">I'm tring to use RSQRT instructions on follow case for ARM </div>
<div class="">(now what using is sqrt):</div>
<div class="">
<pre class="gmail-bz_comment_text" id="gmail-comment_text_1" style="white-space: pre-wrap; width: 50em;">    1.0 / sqrt(x)</pre>
</div>
<div class="">The RSQRT instructions(VRSQRTE/VRSQRTS) are vector type, </div>
<div class="">but above operation is scalar type. So a transformation must be </div>
<div class="">done(transform sqrt pattern to rsqrt).</div>
<div class=""><br class="">
</div>
<div class="">I have completed a patch for this, but I made the transformation in the</div>
<div class="">backend which will leads to additional latencies.And actually it's not </div>
<div class="">reasonable doing transformation in backend.</div>
<div class="">I think it would be better done that on IR. I am a novice to llvm.I don't </div>
<div class="">know  anything about this subject. If anyone could</div>
<div class="">give me some advice would be appreciated.</div>
<div class=""><br class="">
</div>
<div class="">Thanks!</div>
<div class=""><br class="">
</div>
-- <br class="">
<div class="gmail_signature">
<div dir="ltr" class="">
<div class="">Best Regards,<br class="">
</div>
Jojo<br class="">
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose,
 or store or copy the information in any medium. Thank you.
</body>
</html>