<div dir="ltr">Note: There is also a rewritten DSE that uses PostDom as well: <a href="https://reviews.llvm.org/D29624" target="_blank">https://reviews.llvm.<wbr>org/D29624</a><div><br></div><div>I will try to produce LLVM IR for you (i'm sadly finding less and less time to work on LLVM these days, so i'm mostly having other people do it :P).</div><div><br></div><div>To give you a more described example the following completely simple algorithm (which GCC and a few other compilers use variants of, see tree-ssa-dse.c) *should* work, but will give wrong answers if you ignore infinite loops.</div><div>again, i've made it not catch every case so i can do it in <50 lines. Normally, for example, there is more than one stack, instead of all possible stores on the same stack, which misses a ton of cases due to irrelevant aliasing, blah blah blah.</div><div><br></div><div><br></div><div><br></div><div>Stack StoreStack; // In realer versions of this algorithm, we have multiple stacks or other structures. In fact, in this version, there will only ever be zero or one things on the stack.</div><div>void walkPostDomChildren(<wbr>DomTreeNode *DTN)</div><div>{</div><div> BasicBlock *BB = DTN->getBlock();</div><div> if (!BB)</div><div> return;</div><div> // Ensure the that the store at the top of the stack is from something above us in our tree. Normally would be done by checking if dfs numbers of the current block is contained within the dfs in/out numbers of the top of stack. See newgvn.cpp, predicateinfo.cpp, etc for ValueStack, which does this.</div><div><br></div><div><br></div><div>Pop Store Stack until block for instruction at Stack.top() postdominates DTN.<br></div><div> </div><div> for (auto *I : reverse(BB))</div><div> if (auto *SI = dyn_cast<StoreInst>(I)) {</div><div> // If whatever is on the top of the stack is a must alias and post-dominates us, we're dead.</div><div> if (!Stack.empty() && AA.isMustAlias(Stack.top()->getPointerOperand(), I->getPointerOperand()) )</div><div> I->eraseFromParent();<br></div><div> else if (Stack.empty())</div><div> Stack.push(SI)</div><div> else</div><div> // This is really a may-def, so we must clear the stack. In this stupid version, this is true even if it noaliases top of stack.</div><div> Stack.clear()</div><div> continue;</div><div> }</div><div> if (!(getModRefInfo(I) & MRI_NoModRef))</div><div> // A use of memory, so reset our stack</div><div> Stack.clear();</div><div> for (auto * Child : DTN)<br></div><div> walkPostDomChildren(DTN)<br></div><div>}<br></div><div><br></div><div>walkPostDomChildren(PDT->getRoot())</div><div><div><br>Basically, if a store postdominates another earlier store without an intervening may-use/may-def, the earlier store is dead. There is nothing that could possibly see it happen before the later store.</div><div><br></div></div><div>Modulo transcription bugs, this algorithm is provably correct for our simplified assumptions. It will only eliminate stores that are dead.</div><div><br></div><div><br></div><div>Given the following program:</div><div><br></div><div>Block 1</div><div>store a, 5</div><div>if (foo)</div><div>{</div><div>Block 2:</div><div> call foo(load a)</div><div> goto Block 2</div><div>}</div><div>Block 3:</div><div>store a, 6</div><div><br></div><div><br></div><div>If infinite loops are not in the post-dominator tree, the tree will look like this:</div><div><br></div><div>3</div><div>|</div><div>1</div><div><br></div><div>The algorithm will visit 3, then push store a, 6 on the stack, visit 1, and use it to remove store a, 5</div><div>This is illegal. The store a, 5, is visible and used in block 2, and block 2 in fact, calls a function with value we stored.</div><div><br>Whoops!</div><div><br></div><div>If infinite loops are connected to virtual exit, the tree will look like this:</div><div><br></div><div>virtual exit</div><div> / | \</div><div>3 2 1</div><div><br></div><div><br></div><div>We will no longer eliminate store a, 5, and we will get a correct answer because at each point, nothing will be on the stack.</div><div>We will visit 3, push store a, 6. We will visit 2, pop the stack because store a, 6 is no longer in scope. Stack will stay empty throughout 2.</div><div>We will visit 1, push store a, 5. We will exit the algorithm with this store on the stack, having eliminated nothing.</div><div>We will get the correct answers no matter how many things no longer exit. This is a safe and conservative post-dominator tree for our algorithm.</div><div><br></div><div>Is this perfect? For certain, no.</div><div><br></div><div>In this example, for *this* algorithm, you can connect the infinite loop anywhere you like as long as you do not generate the post-dominator tree:</div><div>3<br></div><div>|</div><div>1</div><div>|</div><div>2</div><div><br></div><div>or</div><div>3</div><div>|</div><div>2</div><div><br></div><div>If you generate that, you will eliminate the store a, 5 again, and you should not.</div><div><br></div><div>So, for example:<br>virtual exit</div><div> | \</div><div> 3 \</div><div> | 1</div><div> 2</div><div><br></div><div>Would also give a correct answer for this algorithm, on this example. But would be generally invalid because there is no reverse path from 3 to 2, *and* there is a reverse path from 3 to 1, but 3 is not an ancestor of 1.</div><div><br></div><div><br></div><div>In fact, if the program was:<br></div><div><div>Block 1</div><div>store a, 5</div><div>if (foo)</div><div>{</div><div>Block 2:</div><div> goto Block 2<br></div><div>}</div><div>Block 3:</div><div>store a, 6</div></div><div><br></div><div>we will still generate:</div><div><div>virtual exit</div><div> / | \</div><div>3 2 1</div></div><div><br></div><div>but in practice, we could eliminate store a, 5 as it's not possible to see that it didn't happen in this example.</div><div><br></div><div>The greater problem with not connecting the infinite loops to exit is that "where it is legal to connect the loops to" (IE what doesn't break this algorithm) is now complex. Effectively, it must take into account where the memory instructions are (though the exact conditions that must be fulfilled do not have to be expressde like this)</div><div>Otherwise, i can generate an adversarial program that will give a wrong answer when you apply the above algorithm.<br></div><div>IE by placing loads and stores and calls such that you end up with the same problem as the 3->1->2 post dom tree above.</div><div><br></div><div>In particular, for the algorithm above, the places for where you connect the infinite loops must ensure either the stack gets cleared in the right place, or that the blocks are siblings instead of parent child, so the scope gets reset. (IE either we must always see the may-use in the infinite loop and clear the stack before going above it, or we must pop the stack so that we do not try to eliminate something "above" the infinite loop using something "below" the infinite loop)</div><div><br></div><div>Worse than that, that's just what works with *this* algorithm.<br></div><div><br></div><div>SSUPRE, which requires a conservative correct postdominance frontier, has worse requirements you'd have to prove are met. </div><div>So does Sreedhar and Gao's IDF algorithm (which is what IDF calculator uses), which requires the CFG and PostDom tree be in sync, and postdom have the parent and sibling property.</div><div>(This is solvable by making the real cfg have the set of fake edges you are creating, but they'd need to be walked when you call predecessors. If you connect them all to a fake exit, even if you have algorithms that must know about the edges, it's one block to special case instead of many)</div><div><br></div><div>Additionally, if you use post-dominance to prove speculation-safety, it's now not enough to know where the load/stores are, you'd have to know where every trapping instruction is to ensure the place you connect the infinite loop does not imply speculation safety where it should not.</div><div><br></div><div>Essentially: Even if you completely ignore the sibling and parent properties, connecting the infinite loops anywhere else requires enough knowledge of the algorithms being used to be able to prove that you aren't going to generate post-dom trees that make them wrong.</div><div>In practice, since at least the IDF calculator relies on the sibling and parent properties, this ends up a practical requirement for the connection points...</div><div><br></div><div>All that seems ... really fragile and difficult :)</div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 2, 2017 at 9:19 PM, Tobias Grosser <span dir="ltr"><<a href="mailto:tobias.grosser@inf.ethz.ch" target="_blank">tobias.grosser@inf.ethz.ch</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Thu, Aug 3, 2017, at 06:12, Tobias Grosser via llvm-commits wrote:<br>
> On Thu, Aug 3, 2017, at 02:19, Jakub Kuderski via Phabricator via<br>
> llvm-commits wrote:<br>
> > kuhar added a comment.<br>
> ><br>
> > I've just realized that I didn't notice the TODOs in this patch when I<br>
> > did a code review for it.<br>
> ><br>
> > // TODO: Can we change the PDT definition such that C remains part of<br>
> > the<br>
> > // CFG, at best without loosing the dominance relation D postdom<br>
> > B.<br>
> ><br>
> > I don't think we reached an agreement on the second part of the TODO, as<br>
> > the `"at best"` might imply. Could you reword the second part of the<br>
> > comment to be less ambiguous and not to suggest that?<br>
><br>
> I can just drop it.There is no need to sneak in TODOs to reach an<br>
> agreement.<br>
> Hopefully our discussion is fruit<br>
><br>
> > And a very minor nitpick: isn't it supposed to be s/loosing/losing? :)<br>
<br>
</span>r309919<br>
<br>
Best,<br>
Tobias<br>
<div class="HOEnZb"><div class="h5">><br>
> Best,<br>
> Tobias<br>
> ______________________________<wbr>_________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br></div>