<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I am not clear what check_1+check_2 really means. For ex: if check_2 is a subset of check_1 and check_1+check_2 implies union, then the new check_1+check_2
 may be overly conservative resulting in lost opportunities. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> llvmdev-bounces@cs.uiuc.edu [mailto:llvmdev-bounces@cs.uiuc.edu]
<b>On Behalf Of </b>Adam Nemet<br>
<b>Sent:</b> Thursday, May 21, 2015 10:23 PM<br>
<b>To:</b> Dev; Nema, Ashutosh; Hal Finkel<br>
<b>Subject:</b> [LLVMdev] Alias-based Loop Versioning<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">There is a work taking place by multiple people in this area and more is expected to happen and I’d like to make sure we’re working toward a common end goal.<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I tried to collect the use-cases for run-time memory checks and the specific memchecks required for each:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">1. Loop Vectorizer: each memory access is checked against all other memory accesses in the loop (except read vs read)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">2. Loop Distribution: only memory accesses in different partitions are checked against each other.  The loop vectorizer will add its own checks for the vectorizable distributed loops<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">3. Loop Fusion: accesses from the to-be-merged loops should be checked against each other<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">4. LICM: if hoisting a load, stores needs to be check.  When sinking a store, all accesses are checked<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">5. Load-elimination in GVN: all *intervening* stores need to be checked.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">6. Instruction scheduling for in-order targets: same as 5 <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Currently only the first two are implemented.  Ashutosh has a pending review for LICM/independent pass.  I am also working on loop-aware load-elimination requiring memchecks (5 from the above list).<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The two main approaches are whether to do this in a separate pass or to do it locally in the passes that benefit from versioning.  I tried to collect the pros and cons of each.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">1. Separate pass<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">The benefit of this approach is that current passes would not have to be modified to take advantage of the new freedom to due more independence of the memory access operations.  AA will capture the noalias annotation of inserted by this
 pass and present it to the passes.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Memchecks present an overhead at run time so one question is how we ensure that any optimization will amortize the cost of these checks.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Depending on the optimization, answering this could be pretty involved (i.e. almost like running the pass itself).  Consider the loop vectorizer.  In order to answer whether versioning the loop would make it vectorizable, you’d have to
 run most of the legality and profitability logic from the pass.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Also which accesses do we need to check?  Depending on the optimization we may not need to check each access against each other access, which being quadratic can be a significant difference.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">2. Each pass performs its own versioning<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Under this approach, each pass would make the calculation locally whether the benefit of versioning outweighs the overhead of the checks.  The current Loop Vectorizer is a good example for this.  It effectively assumes no may-alias and
 if the number of required checks are under a certain threshold it assumes that the vectorization gain will outweigh the cost of the checks.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Making decision locally is not ideal in this approach.  I.e. if we can amortize the cost of the same checks with a combination of optimization from *multiple* passes, neither pass would make the decision locally to version.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Also, it’s probably beneficial to perform a single loop versioning even if multiple passes would like to check different accesses.  E.g. rather than:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">    Checks_1</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">    /       \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">   /         \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">OrigLoop    Checks_2</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">   \         /  \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">    \       /    \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">     \ NoAlias_1 NoAlias_2</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">      \    |    /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">       \   |   /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">        \  |  /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">          Join</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">But instead:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">   Checks_1+Check_2</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">      /      \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">     /        \</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">  OrigLoop   NoAlias_2</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">     \        /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">      \      /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">       \    /</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">        Join</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">This is effectively creating a fast-path and a slow-path version of the loop.  We would probably need some metadata annotation so that subsequent passes could amend the same checking block.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">3. There are some more futuristic ideas like to always version fully, disambiguating all accesses and then have the optimizers transform both the versions of the loop.  Then a later pass would decide which checks were necessary and what
 additional optimizations were done in the speculative version of the loop .  Then finally make the cost decision of whether to keep the speculative version along with the checks or remove them.  This would probably need a fairly sophisticated set of metadata.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I think that a combination of 1 and 2 makes sense.  This is where we will effectively end up after Ashutosh’s patch.  This would hopefully give us the best of both worlds: the aggressiveness/precision of making the call locally in complex
 passes and the option of simplicity/orthogonality of the separate pass.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thoughts?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Adam<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>