<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 11, 2015, at 6:05 AM, Jens Persson via swift-dev &lt;<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Correction: The test I'm running is actually using V4&lt;V4&lt;Float&gt;&gt;.<div class="">Manually unrolling the loop makes adding V4&lt;V4&lt;Float&gt;&gt; as fast as adding SIMD float4x4.</div><div class="">Using the (un-unrolled) for loop will be about 4 times slower.</div><div class="">My question is still: Shouldn't the optimizer be able to handle that for loop / make my manual unrolling unnecessary?</div><div class="">/Jens</div></div><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class="gmail_quote">On Fri, Dec 11, 2015 at 8:28 AM, Jens Persson<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:jens@bitcycle.com" target="_blank" class="">jens@bitcycle.com</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="ltr" class=""><div class=""><div class="">I've been doing a lot of performance testing related to generic value types and SIMD lately, and I've built Swift from sources in order to get an idea of what's coming up optimizerwise. Things have improved and the optimizer is impressive overall. But I still see no improvement in the case exemplified below.</div><div class=""><br class=""></div><div class="">Manually unrolling the simple for loop will make it ~ 4 times faster (and exactly the same as when SIMD float4):<br class=""></div><div class=""><br class=""></div><div class="">struct V4&lt;T&gt; {</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var elements: (T, T, T, T)</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>/.../</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>subscript(index: Int) -&gt; T { /.../ }</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>/.../</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>func addedTo(other: V4) -&gt; V4 {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>var r = V4()</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>// Manually unrolling makes code ~ 4 times faster:</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>// for i in 0 ..&lt; 4 { r[i] = self[i] + other[i] }</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>r[0] = self[0] + other[0]</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>r[1] = self[1] + other[1]</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>r[2] = self[2] + other[2]</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>r[3] = self[3] + other[3]</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>return r</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>}</div><div class="">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span>/.../</div><div class="">}</div><div class=""><br class=""></div><div class="">Shouldn't the optimizer be able to handle that for loop and make the manual unrolling unnecessary?</div></div></div></blockquote></div></div></div></blockquote><div><br class=""></div>In theory, yes. In practice there are some fairly complex phase ordering issues in the SIL optimizer, and certain optimizations (like general loop unrolling) that are only done in the LLVM optimizer. The LLVM optimizer runs after all the SIL-level optimizations, which may mean that SIL-level optimization opportunities are exposed by the LLVM optimizer but by then it is too late to do anything about them.</div><div><br class=""></div><div><blockquote type="cite" class=""><div class=""><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="ltr" class=""><div class=""><div class="">(compiled the test with -O -whole-module-optimizations, also tried -Ounchecked but with same results.)</div></div></div></blockquote></div></div></div></blockquote><div><br class=""></div><div>Would you mind opening an issue on <a href="https://bugs.swift.org" class="">https://bugs.swift.org</a>&nbsp;will a small stand-alone test case that compiles successfully, and report your results there?</div><div><br class=""></div><div>Mark</div></div><br class=""></body></html>