<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, Jul 25, 2016 at 3:40 PM Finkel, Hal J. via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">



<div>
<div>
<div></div>
<div><i><font style="color:#333333">Sent from my Verizon Wireless 4G LTE DROID</font></i></div></div></div><div><div>
<div><br>
</div>
<div>On Jul 25, 2016 6:16 PM, Sean Silva <<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>> wrote:</div>
<div>></div>
<div>></div>
<div>></div>
<div>> On Mon, Jul 25, 2016 at 9:27 AM, Hal Finkel <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>> wrote:</div>
<div>>></div>
<div>>></div>
<div>>> ________________________________</div>
<div>>>></div>
<div>>>> From: "Sean Silva" <<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></div>
<div>>>> To: "Chandler Carruth" <<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>></div>
<div>>>> Cc: "Xinliang David Li" <<a href="mailto:davidxl@google.com" target="_blank">davidxl@google.com</a>>, "llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>, "Davide Italiano" <<a href="mailto:dccitaliano@gmail.com" target="_blank">dccitaliano@gmail.com</a>>,
 "Tim Amini Golling" <<a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a>>, "Hal Finkel" <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>>, "Sanjoy Das" <<a href="mailto:sanjoy@playingwithpointers.com" target="_blank">sanjoy@playingwithpointers.com</a>>,
 "Pete Cooper" <<a href="mailto:peter_cooper@apple.com" target="_blank">peter_cooper@apple.com</a>></div>
<div>>>> Sent: Friday, July 22, 2016 3:55:52 AM</div>
<div>>>> Subject: Re: [PM] I think that the new PM needs to learn about inter-analysis dependencies...</div>
<div>>>></div>
<div>>>> The more closely I look at this, the more it seems like there may be a useful incremental step in the transition to the new PM: use the new PM analysis machinery in the old PM. If this is possible, it will simplify the old PM and (hopefully) allow
 an incremental transition to the new PM instead of a flag day transition for the switch.</div>
<div>>>></div>
<div>>>> I.e., AFAICT, the new PM transition is essentially about 2 mostly orthogonal aspects of running optimization pipelines:</div>
<div>>>> 1. Analysis computation and analysis result lifetime management (including avoiding analysis recomputation)</div>
<div>>>> 2. Running transformation passes over their respective IRUnit's in some order</div>
<div>>>></div>
<div>>>> These are conflated in the old PM. In reality, the only interaction between them (with the new PM machinery for 1.) is a small number of places within 2. which need to call 'invalidate'.</div>
<div>>>></div>
<div>>>> I'm pretty sure that 2. is fairly similar in the new PM and old PM (the main difference is that the notion of "adapters" is split out in the new PM). The analysis handling seems to be what makes the old PM so difficult to understand (e.g. it is the
 cause of the multiple inheritance in the implementation). Trying to unify analyses and transformations (and some questionable (in hindsight) implementation decisions) seems to be the main "problem" with the design of the old PM AFAICT (there are other issues,
 but they are more "nice to have").</div>
<div>>>></div>
<div>>>> IMO it is an anti-pattern to think of analyses as "passes". There are just "analyses" and "transformations" and they are two separate things. In fact, the `run` method on analyses should probably be called `computeResult` or something like that to
 avoid confusion.</div>
<div>>></div>
<div>>> This makes sense to me.</div>
<div>>></div>
<div>>> We do currently have some "in between" passes, like LCSSA, which are transformations, but are required by other passes, and transform the IR but whose preservation represents properties of the IR. The particulars of how we handle LCSSA aside (e.g. I
 think we should preserve it more, perhaps everywhere), how are we planning on handling this class of things?</div>
<div>></div>
<div>></div>
<div>> The new PM doesn't currently have a concept like this. As you mentioned, it is a weird cross between a transformation and an analysis: it can be "invalidated" like an analysis, but "recomputing" it actually mutates the IR like a transformation.</div>
<div>></div>
<div>> I'd like to preface the below with the following:</div>
<div>> No matter how we ultimately address this requirement, my preference is that we do so in a way that applies to the old PM. This is a case where the old PM supports a richer set of functionality than the new PM. By incrementally refactoring the old PM
 away from its use of this extra capability and towards whatever "new" way there is to do it, we will understand better what it is that we actually need.</div>
<div>></div>
<div>> (and sorry for the brain dump in the rest of this post)</div>
<div>></div>
<div>></div>
<div>></div>
<div>> I have not seen any mention of a solution to this problem besides "we shouldn't do that", which is sort of a cop-out. Here is a strawman proposal:</div>
<div>></div>
<div>> If it isn't too expensive, one simple alternative is to have passes just make a call to a utility function to put things in LCSSA if they need it (this utility would also have to invalidate analyses).</div>
<div>> If that ends up being expensive, we can have a dummy "indicator" analysis IRIsInLCSSAForm which, if cached, means "don't bother to call the utility function". We could maybe just use the LCSSA pass directly to do the transformation. LCSSA could have IRIsInLCSSAForm as
 an member typedef `IndicatorT` so it can be accessed generically. We could then support an API like:</div>
<div><br>
</div>
</div></div><div><div><div>I think this idea makes sense. My understanding is: There is nothing that prevents an analysis results from exposing a utility that transforms IR, and the result can certainly cache whether or not this transformation has been performed.</div></div></div></blockquote><div><br></div><div>Somewhat agreed, but I don't actually think this problem is as bad as it seems in practice.</div><div><br></div><div>We only have two places that do this (loop simplify and lcssa) and they both *can* be modeled as "check if it is form X, and if not, put it in form X" or as "check if it is form X, and if not, give up on transform". This has been discussed several times, and the direction things have been leaning for a long time has been:</div><div><br></div><div>- Make LCSSA increasingly fundamental to the IR and always present, *or* don't require LCSSA at all for transforms. Either of these solve the problem.</div><div><br></div><div>- Check for loop-simplified form if necessary, and skip the transformation if not present. Because simplified form is simple to check this seems to work well.</div><div><br></div><div>Anyways, I don't think we have to solve this problem 100% to make progress on the pass manager. AT no point have I felt particularly blocked on this.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>
<div><br>
</div>
<div><br>
</div>
<div>></div>
<div>> ```</div>
<div>> <a href="http://FooTransformation.cpp" target="_blank">FooTransformation.cpp</a>:</div>
<div>></div>
<div>> PreservedAnalyses FooTransformation::run(Function &F, AnalysisManager AM) {</div>
<div>>   // Must be called before getting analyses, as it might invalidate some.</div>
<div>>   canonicalizeIR<LCSSA>(F, AM);</div>
<div>></div>
<div>>   ...</div>
<div>> }</div>
<div>></div>
<div>></div>
<div>> include/IR/Canonicalization.h:</div>
<div>></div>
<div>> template <typename CanonicalizationT, typename IRUnitT></div>
<div>> void canonicalizeIR(IRUnitT &IR, AnalysisManager &AM) {</div>
<div>>   using IndicatorT = typename CanonicalizationT::IndicatorAnalysis;</div>
<div>>   if (<a href="http://AM.getCachedResult" target="_blank">AM.getCachedResult</a><IndicatorT>(IR))</div>
<div>>     return;</div>
<div>>   CanonicalizationT C;</div>
<div>>   PreservedAnalysis PA = <a href="http://C.run" target="_blank">C.run</a>(IR, AM);</div>
<div>>   <a href="http://AM.invalidate" target="_blank">AM.invalidate</a>(IR, PA);</div>
<div>>   (void)<a href="http://AM.getResult" target="_blank">AM.getResult</a><IndicatorT>(IR);</div>
<div>> }</div>
<div>></div>
<div>> ```</div>
<div>></div>
<div>></div>
<div>> One place that talks about this problem of "requiring a transformation" is <a href="http://llvm.org/devmtg/2014-04/PDFs/Talks/Passes.pdf" target="_blank">http://llvm.org/devmtg/2014-04/PDFs/Talks/Passes.pdf</a> on slide 17.</div>
<div>></div>
<div>> One reason it provides for "we shouldn't do that" is that if you think about these things as "canonicalize the IR into a specific form", then when you have N>1 such dependencies (like some passes do on LoopSimplify and LCSSA), one must have a subset
 of the requirements of the other. I.e. you can't have two canonicalizations that "fight" each other. Using an explicit mutation API like the strawman above is a bit less bulletproof than scheduling based on statically known interferences between canonicalizations
 (e.g. CanonicalizationA may invalidate CanonicalizationB, but not the reverse, so it would automatically know to run CanonicalizationA before CanonicalizationB), but given that we have relatively few "canonicalizations" (to give them a name) that use this
 feature of the old PM, it may be livable (at least in the middle-end, it seems like there is just LCSSA, LoopSimplify, BreakCriticalEdges, and LowerSwitch in calls to addPreservedID/addRequiredID).</div>
<div>></div>
<div>> I don't find the "Causes rampant re-running of invalidated analyses" argument in that slide convincing. If a pass needs the IR in LCSSA then it needs it. There isn't much we can do about that.</div>
<div>></div>
<div>></div>
<div>></div>
<div>></div>
<div>> One invariant I'd like to preserve in the new pass manager is that whatever pipeline is provided on the opt command line, we end up running something "valid"; so a cop-out like "if a pass needs LCSSA, you need to make sure to add LCSSA at an appropriate
 place before it in the pipeline" is not something I think is reasonable (way too error-prone).</div>
<div>></div>
<div>> Small rant:</div>
<div>></div>
<div>> We already are in this error-prone situation in the new PM with the need to call `getCachedResult` to access analyses from a larger IRUnitT (e.g. the situation I explained in the post-commit thread of r274712);</div>
<div><br>
</div>
</div></div><div><div><div>Yea, I don't like this either. I think we both agree that we need a better solution to this. I think we should fix this now and then deal with potential concurrency issues when we actually have a design for that so we know what that means.</div></div></div></blockquote><div><br></div><div>FWIW, I strongly disagree.</div><div><br></div><div>I think it would be better to iterate on this once we understand how the new pass manager works. I think exposing the fact that these things are cached is really important and useful, and it makes querying across IR unit boundaries significantly more clear at the call site.</div></div></div>