<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p></p>
<div>Had some discussion with @rkennke. A pre-loop could help for alignment.</div>
<div><br>
</div>
<div>For example, lets say we have</div>
<div>```</div>
<div>fn copy(byte[] src, byte[] dst)</div>
<div> for (i = 1; i < src.length; ++i)</div>
<div> dst[i] = src[i]</div>
<div>```</div>
<div><br>
</div>
<div>Lets say we want to align vector load/stores to words of 8 bytes. We can</div>
<div>assume that src/dst are word aligned. Since we start at the 1'th index, we</div>
<div>know the first few iterations are not word aligned and can put them in the</div>
<div>beginning loop. We can transform it into:</div>
<div><br>
</div>
<div>```</div>
<div>// pre-loop</div>
<div>for (i = 1; i < 8; i++) { dst[i] = src[i] }</div>
<div>// main-loop</div>
<div>for (i = 8; i < src.length - (src.length % 16); i+=16) {</div>
<div> // 128 bit / 16 byte / 2 word vector load-store</div>
<div> dst[i:i+16] = src[i:i+16]</div>
<div>}</div>
<div>// post-loop, remainder iterations</div>
<div>for (i; i < src.length; i++) { dst[i] = src[i] }</div>
<div>```</div>
<div><br>
</div>
<div>I found that clang also generates a pre-loop for the same example. They call it</div>
<div>“prolog/main/epilog” loop https://godbolt.org/z/KrEfj7z56.</div>
<div><br>
</div>
I would like to hear if there are any other reasons for a pre-loop.
<p></p>
<p><span style="color: rgb(29, 28, 29); font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; font-variant-ligatures: none; white-space-collapse: preserve; background-color: rgba(29, 28, 29, 0.04);"></span></p>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Cao, Joshua<br>
<b>Sent:</b> Wednesday, February 7, 2024 10:53:09 AM<br>
<b>To:</b> hotspot-compiler-dev@openjdk.org<br>
<b>Subject:</b> What is the motivation for pre/main/post loops?</font>
<div> </div>
</div>
<div>
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p>I can't find any notes on this. The closest I can find is <a href="https://wiki.openjdk.org/pages/viewpage.action?pageId=20415918" class="OWAAutoLink" id="LPlnk894820">https://wiki.openjdk.org/pages/viewpage.action?pageId=20415918</a><i></i></p>
<p><br>
</p>
<p><span style="font-size: 12pt;">> </span><span style="color: rgb(68, 68, 68); font-family: "Bitstream Vera Sans", "Luxi Sans", Helvetica, Arial, san-serif; font-size: 12pt;">clone loop body and create pre-, main- and post- loops</span></p>
<p><span style="color: rgb(68, 68, 68); font-family: "Bitstream Vera Sans", "Luxi Sans", Helvetica, Arial, san-serif; font-size: 12pt;"><br>
</span></p>
<p><span style="color: rgb(68, 68, 68); font-size: 12pt;">I can guess why a post loop is useful. We can use it for the remainder iterations after loop unrolling.</span></p>
<p><span style="color: rgb(68, 68, 68); font-size: 12pt;">However, I cannot think of scenarios where a pre-loop is useful. What are some cases where a</span></p>
<p><span style="color: rgb(68, 68, 68); font-size: 12pt;">pre-loop is useful?</span></p>
<p></p>
</div>
</div>
</body>
</html>