<html><head><style type='text/css'>p { margin: 0; }</style></head><body><div style='font-family: arial,helvetica,sans-serif; font-size: 10pt; color: #000000'><br><hr id="zwchr"><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><b>From: </b>"Daniel Berlin" <dberlin@dberlin.org><br><b>To: </b>"Ehsan A Amiri" <amehsan@ca.ibm.com><br><b>Cc: </b>"Hal Finkel" <hfinkel@anl.gov>, "llvm-commits" <llvm-commits@lists.llvm.org>, reviews+D22305+public+92ca108e50bc4651@reviews.llvm.org<br><b>Sent: </b>Thursday, July 14, 2016 5:11:20 PM<br><b>Subject: </b>Re: [PATCH] D22305: [BasicAA] Strip phi nodes, when all incoming values are the same.<br><br><div dir="ltr">Sigh. Even with all the bugs this discovers, this is unsolvable in the general case with the current way GVN is set up.<div><br></div><div>First, this bug definitely gets fixed by making sure the first GVN simplifies the phi so the second GVN run doesn't see it.<br></div><div>There are bugs in GVN that cause this to not happen right now, and fixing them makes it work.</div><div>The game turns out to be over as soon as you have this phi node as input to GVN.  However, it's sadly worse than that.</div><div><br></div><div>The iteration orders of memdep and GVN are basically the exact opposite, so GVN will always have memdep cache things that it will later simplify, and as we discussed on the bug, we can't ever get them out of the memdep cache.</div><div><br></div><div>This is about more than PRE or phis, it literally affects every memdep query it makes where the path involves something GVN will later simplify, at all, and that simplification makes it able to see a no-alias (or in some cases, a partial/must-alias).</div><div><br></div><div>For example, if you add a pointer that  alias queries follow along a backedge that requires constant folding, or *anything* basicaa does not itself process but gvn/simplifyinstruction does, it will fail in precisely the same way.</div><div><div><br></div><div>Worse than that, without the other patches (from above) to GVN to make it stop generating trivially bad IR, *GVN* itself will generate these kinds of cases.</div><div><br></div><div>So even with your alias patch, i can trivially modify the testcase in a number of ways to make it break again.</div><div><br></div></div><div>This means while your alias patch will fix the current case, there is literally no way to fix all the cases (again, in current GVN. New GVN + MemSSA will not have these issues) you are going to find that doesn't involve something like blowing away memdep cache entirely on each iteration of GVN.</div><div><br></div><div>I'm not sure how we want to paper over this bug:<br><br></div><div>1. Making first run of GVN not generate IR the second GVN will have to trivially simplify (which will fix some cases but not others)</div><div><br></div><div>2. Make BasicAA see through phi nodes (ditto, though chandler points out there are plenty of normal programs  with thousands of phi nodes that have the same arguments, and since basicaa does not cache, there is a compile time cost to doing this )</div><div><br></div><div>3. Add an API and use it to blow away memdep cache entirely between GVN iterations (which will fix all such cases at some possible compile time cost)</div><div><br></div><div id="DWT17110">Some combination of the above?<br></div></div></blockquote>I won't have a real opinion until we measure the compile-time cost of (2) and (3), but as a preliminary matter, I vote for both (2) and (3). Why?<br><br>(2) is, in theory, the right thing to do. Even if we were to consider uniform PHIs to be anti-canonical, and thus something which should be simplified, we can't simplify often enough to prevent these from blocking useful analysis work. Arbitrary uses of RAUW can create these PHIs, and we can't (and probably shouldn't) run InstCombine in between every other pass. This is a local pattern that stripPointerCasts, and similar functions, can look through.<br><br>(3) is also, in theory, the right thing to do. The memdep cache, by necessity, caches negative results. Each GVN iteration, however, performs "information revealing" operations which can make the cached results more conservative than a new query's results.<br><br>Now we actually need to measure the costs.<br><br> -Hal<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px; color: rgb(0, 0, 0); font-weight: normal; font-style: normal; text-decoration: none; font-family: Helvetica,Arial,sans-serif; font-size: 12pt;"><div dir="ltr"><div><br></div><div>(Note: i've attached a patch for 3 in case anyone wants to see the compile time cost)</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 14, 2016 at 2:12 PM, Daniel Berlin <span dir="ltr"><<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div dir="ltr">Note: This already had GVN run once on it, do you have the one before that?<br><br></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jul 14, 2016 at 1:53 PM, Ehsan A Amiri <span dir="ltr"><<a href="mailto:amehsan@ca.ibm.com" target="_blank">amehsan@ca.ibm.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><p>I can see the problem with the following command line and attached file <br><br> opt -gvn t.ll<br><br><i>(See attached file: t.ll)</i><br><br>My clang is almost a week old.<br><br><img src="cid:2__=8FBBF563DFE105498f9e8a93df938690918c8FB@" alt="Inactive hide details for Daniel Berlin ---07/14/2016 04:27:43 PM---Actually, can you please attach a .ll file and an opt comma" border="0" height="16" width="16"><font color="#424282">Daniel Berlin ---07/14/2016 04:27:43 PM---Actually, can you please attach a .ll file and an opt command line that reproduces the problem?</font><span><br><br><font color="#5f5f5f" size="2">From:        </font><font size="2">Daniel Berlin <<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>></font><br><font color="#5f5f5f" size="2">To:        </font><font size="2">Ehsan A Amiri/Toronto/IBM@IBMCA</font><br><font color="#5f5f5f" size="2">Cc:        </font><font size="2">Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>>, llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>>, <a href="mailto:reviews%2BD22305%2Bpublic%2B92ca108e50bc4651@reviews.llvm.org" target="_blank">reviews+D22305+public+92ca108e50bc4651@reviews.llvm.org</a></font><br></span><font color="#5f5f5f" size="2">Date:        </font><font size="2">07/14/2016 04:27 PM</font><span><br><font color="#5f5f5f" size="2">Subject:        </font><font size="2">Re: [PATCH] D22305: [BasicAA] Strip phi nodes, when all incoming values are the same.</font><br></span></p><hr style="color: rgb(128, 145, 165);" align="left" noshade="noshade" size="2" width="100%"><br><br><br><div><div><font size="4">Actually, can you please attach a .ll file and an opt command line that reproduces the problem?<br>The clang command line you have is very sensitive to versions, etc.</font><br><br><font size="4">I cannot get your issue to reproduce with the clang i have installed that can target powerpc-linux (and the issue does not reproduce with your testcase on x86) :)</font><br><br><font size="4">While debugging a bit, note that there is at least one obvious bug in GVN that may affect this, by inspection:</font><br><br><font size="4">When GVN splits a critical edge, it never adds the new block to the iteration order (at all), even though it inserts into it.</font><br><font size="4">So they will not get processed until the next iteration of GVN on the function, even though they have code in them.  While this is okay from a correctness standpoint, it may block optimization of certain things (including the cases you've discovered).  In practice, there is no way to perfectly solve that without pre-splitting all critical edges, but you should get the same effect if we throw the critical edge block and then it's successors (including the current blcok) into bbvect after the current block again.</font><br><br><font size="4">It is also missing a real phi simplification.</font><br><font size="4">While simplifyinstruction will check if all arguments are trivially the same, that is not the real test that should be performed.</font><br><br><font size="4">It should be doing VN.lookup on each argument and seeing if they come up with the the same value number.</font><br><br><font size="4">Once you attach the .ll file, i'll fix these and see if it fixes your testcase, and if not, debug further.</font><br><br><br><font size="4">On Thu, Jul 14, 2016 at 10:11 AM, Daniel Berlin <</font><a href="mailto:dberlin@dberlin.org" target="_blank"><u><font color="#0000ff" size="4">dberlin@dberlin.org</font></u></a><font size="4">> wrote:</font><ul><br><br><font size="4">On Thu, Jul 14, 2016 at 7:09 AM, Ehsan A Amiri <</font><a href="mailto:amehsan@ca.ibm.com" target="_blank"><u><font color="#0000ff" size="4">amehsan@ca.ibm.com</font></u></a><font size="4">> wrote:</font><br><font size="4">This is order of events:<br></font><p><font size="4">This order cannot be correct if the solution I gave (or adding simplification to *some place in GVN*) does not work.  Or GVN is broken in other ways.</font><br><font size="4"> </font><br><font size="4">1) GVN starts looking at the function. At this point the phi node has two different incoming values.<br>2) GVN performs an RAUW. The phi is converted to the one that has two identincal incoming values. </font></p><p><br><font size="4">At this point, it should now process the phi instruction again before it processes the load, because it is doing a reverse postorder traversal.</font><br><font size="4">When it did that, the phi should have been simplified</font><br><font size="4">So why did that not happen?</font><br><br><font size="4">Given the complexity of fixing the real problem,</font></p><p><br><font size="4">Look, i understand why you want to just fix this in AA and be done with it.</font><br><font size="4">Really, I do. </font><br><br><font size="4">I understand you have spent a lot of time on this bug, and I greatly appreciate that.</font><br><font size="4">But I really want to understand what is going on before we try to actually fix it.</font><br><font size="4">I have a good understanding of what happens once the bad answer gets into memdep (and thank you for that!), but i still have trouble seeing why it lived long enough to get there.</font><br><br><font size="4">To that end, so you don't have to spend more time running around for me,  i'll take over this bug, and either figure out why GVN lets this PHI live to the point it gets an AA query about it (and fix it/decide it can't be fixed), or commit the AA patch for you if we decide it can't be fixed.</font><br><br><font size="4">My ETA is by friday.</font><br><br><font size="4">I assume the testcase in the bug is the one we are still using, right?<br>(If not, if you can attach it, that would be helpful)</font><br><br></p><p></p><p></p></ul><br><br>
</div></div><p></p></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</blockquote><br><br><br>-- <br><div><span name="x"></span>Hal Finkel<br>Assistant Computational Scientist<br>Leadership Computing Facility<br>Argonne National Laboratory<span name="x"></span><br></div></div></body></html>