<br><br><div class="gmail_quote"><div dir="ltr">On Fri, Jun 24, 2016, 5:16 PM Gerolf Hoflehner <<a href="mailto:ghoflehner@apple.com">ghoflehner@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Gerolf added a comment.<br>
<br>
I'll take a more in depth look into the load and store merge routines also. At a first glance it seems one could just add a few MSSA hooks rather than copy-paste-modify the code base.<br></blockquote></div><div>It is possible, but th<span style="font-size:13px">e other version is o(n^3), the memory SSA version is o(n), so this is deliberate. It is not possible to make the other version less than n^2 without memory versions.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><br></span></div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I also take a (late :-() look at the core MSSA design. Could you outline the high-level MSSA design in more detail than <a href="http://reviews.llvm.org/D7864?Eg" rel="noreferrer" target="_blank">http://reviews.llvm.org/D7864?Eg</a>. </blockquote></div><div><br></div><div>There are also a bunch of mailing list threads and the comment in memoryssa.h, but happy to expand on whatever for you :)</div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">>how does it compare/relate to </span><a href="http://www.airs.com/dnovillo/Papers/mem-ssa.pdf" rel="noreferrer" target="_blank" style="font-size:13px">http://www.airs.com/dnovillo/Papers/mem-ssa.pdf</a><span style="font-size:13px">? Thanks!</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">Sure.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">This paper covers what was the first version of memoryssa for GCC. Over time, we significantly simplified the design after getting years of experience with clients. GCC's current design is now identical to the llvm one.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">Compared to the paper, in both implementations, there are no kills anymore, and no partitioning. All loads and stores use and define a single memoryssa variable instead of multiple ones representing heap partitions. There is a long discussion of why in one of the threads, but it boils down to: it's not worth making it more precise for the cost you pay in time or memory or reasoning or complexity of code, for what clients really ended up wanting out of it. </span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">This pass is a good example. Having multiple variables would not make it faster or more precise for load or store merging and may in fact make it much slower. For passes that care, the walker interface provides a way to further try to disambiguate def def and def use chains.</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><br></span></div><div><br></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><br></span></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-Gerolf<br>
<br>
<br>
================<br>
Comment at: lib/Transforms/Scalar/MergedLoadStoreMotion.cpp:346<br>
@@ -271,1 +345,3 @@<br>
HoistedInst->insertBefore(HoistPt);<br>
+ if (MSSA) {<br>
+ // We also hoist operands of loads using this function, so check to see if<br>
----------------<br>
Ok, it would probably not be possible foresee all use cases. I certainly agree to the iterative approach you propose. I'm surprised though that a simple replacement of a load has not come up elsewhere.<br></blockquote></div><div><br></div><div>This is the first major pass conversion outside of earlycse. I am working pass by pass to preserve it for gvn.</div><div><br></div><div>Note that this is also not simple load replacement, it's merging multiple loads into a single one by insertion of a new load and removing. However, the operations performed are exactly what is happening to the non memoryssa ir, which is that you are inserting a new load, and removing the two old ones.</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
================<br>
Comment at: lib/Transforms/Scalar/MergedLoadStoreMotion.cpp:1032<br>
@@ +1031,3 @@<br>
+<br>
+void MergedLoadStoreMotion::updateMemorySSAForSunkStores(<br>
+ BasicBlock *SinkLocation, StoreInst *S0, StoreInst *S1, StoreInst *SNew) {<br>
----------------<br>
<br>
What got me here is that I expected the update to be simple.</blockquote></div><div><br></div><div>It would be if the stores were not may defs. Because they are, there may be more of them below in the same block that you are not touching.</div><div><br></div><div>Ie. </div><div>1=memorydef(0)</div><div>Store a </div><div>2= memorydef(1)</div><div>Store b</div><div><br></div><div>When you move a, you must update b's defining access to 0</div><div><br></div><div>It is not possible to avoid this in all cases. Even if you had multiple memoryssa variables, there is no perfect partitioning of the heap that guarantees you won't have a use.</div><div><br></div><div>If you removed the defining access of may defs, you would not easily know how far a given memorydef version is valid for, or be able to tell the may defs that might kill your store. In the above design, finding the lowest sink points and uses for a given store is easy, because you can walk the use chains. This makes a number of optimization algorithms that are otherwise quadratic, linear.</div><div><br></div><div><br></div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Why is here more involved than replacing two MD's with one, possibly removing a MP? </blockquote></div><div><br></div><div>In the simple case, where there are no stores below you, it is in fact, that :)</div><div><br></div><div>The other complication is that a mp may already exist in the block below you (imagine our store a, store b case in both sides of the diamond. A phi will exist in the merge block and your insertion point is after the phi)</div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From the clients perspective updates like this look too complicated to get right. </blockquote></div><div>Fwiw: I disagree, but I admit I've written the memoryssa update routines for a ton of both gcc and llvm passes. This is not materially different from the ir update you have to perform if you sink stores and their operands with pre.</div><div>This is pretty much as complicated as it gets as well, because it's code motion, not just replacement with existing values.</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Maybe there is verifier in place, but in the code I don't even see a single assertion but could help debugging issues should something go wrong.</blockquote></div><div>There is a verifier that is run in both the updating unit tests and the test cases for this pass that verify the updating is correct.</div><div>Past that, removal and insertion routines will assert if you do something that is obviously wrong, much more than the ir does The verifier in both cases is used to catch screwups that cost too much to verify at all times.</div><div>The verifier can be called at any time, and I'm happy to add a call to it here under xdebug if you like.</div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> Perhaps we can come up with some utility routines that make simpler to grasp: when two memory or more memory operation get moved, this is the blueprint for updating MSSA and guarantee it stays consistent.<br></blockquote></div><div><br></div><div>I think we could in general, but probably not for this kind of specialized code motion.</div><div><br></div><div>The simple cse case is already pretty simple (for loads, you do nothing but remove the dead load. For stores , replacealluseswith your definingacess, Remove the dead store).</div><div><br></div><div>Note that only stores are ever complex to reason about when updating. Because loads are just uses, they never require much because doing things do them can't change the reaching versions or require new phi nodes (unless you sink loads instead of hoist them, because both memoryssa and the normal ir are pruned ssa, so they eliminate phi nodes that have no uses. We could just turn this back off if we wanted to guarantee no load sinking would ever require a new phi node, but I have not hit a pass that wants to sink loads)</div><div><br></div><div>For stores, general downward code motion can get complex enough that it may be simpler and easier to not try to preserve the analysis. This is true of normal ssa as well. This pass is pretty much the limit of what I'd try to preserve rather than have it just recalculate memoryssa. The set of update cases is fixed and formally provable.</div><div><br></div><div>But thoughts definitely welcome.</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
================<br>
Comment at: lib/Transforms/Scalar/MergedLoadStoreMotion.cpp:1099<br>
@@ +1098,3 @@<br>
+<br>
+ BasicBlock *MD0Block = MD0->getBlock();<br>
+ for (auto UI = MD0->use_begin(), UE = MD0->use_end(); UI != UE;) {<br>
----------------<br>
There could be a lambda or function that takes care of the two loops..<br></blockquote></div><div><br></div><div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<a href="http://reviews.llvm.org/D8688" rel="noreferrer" target="_blank">http://reviews.llvm.org/D8688</a><br>
<br>
<br>
<br>
</blockquote></div>