<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 11, 2014 at 2:16 PM, Frédéric Riss <span dir="ltr"><<a href="mailto:friss@apple.com" target="_blank">friss@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><span class=""><div>On Nov 11, 2014, at 1:43 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:</div><br></span><div><div class="h5"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Nov 11, 2014 at 1:21 PM, Frederic Riss <span dir="ltr"><<a href="mailto:friss@apple.com" target="_blank">friss@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Author: friss<br>
Date: Tue Nov 11 15:21:08 2014<br>
New Revision: 221709<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=221709&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=221709&view=rev</a><br>
Log:<br>
Totally forget deallocated SDNodes in SDDbgInfo.<br>
<br>
What would happen before that commit is that the SDDbgValues associated with<br>
a deallocated SDNode would be marked Invalidated, but SDDbgInfo would keep<br>
a map entry keyed by the SDNode pointer pointing to this list of invalidated<br>
SDDbgNodes. As the memory gets reused, the list might get wrongly associated<br>
with another new SDNode. As the SDDbgValues are cloned when they are transfered,<br>
this can lead to an exponential number of SDDbgValues being produced during<br>
DAGCombine like in <a href="http://llvm.org/bugs/show_bug.cgi?id=20893" target="_blank">http://llvm.org/bugs/show_bug.cgi?id=20893</a><br>
<br>
Note that the previous behavior wasn't really buggy as the invalidation made<br>
sure that the SDDbgValues won't be used. This commit can be considered a<br>
memory optimization and as such is really hard to validate in a unit-test.<br></blockquote><div><br>This was sort-of undefined behavior previously (comparing a previously free'd pointer to a new allocation) but is still sort-of detectable. If we added an assertion into some point of initial construction or SDNode usage, that there were zero valuse in its map - then this assertion would fire on the buggy code? (but only when the allocation got lucky and put it in the same place) But would at least fail rather than behave strangely (leakily, mind you, but strangely)<br><br>I imagine at the call to setHasDebugValue, if it previously had !getHasDebugValue, then the number of entries in the map should be 1 (this should be the first thing we've added to the map), something like changing this:<br><br><pre style="margin-top:0px;margin-bottom:0px;padding-top:0.5em;font-size:inherit;line-height:16.25px"><span style="display:block"> DbgInfo->add(DB, SD, isParameter);
</span><span style="display:block"> <span style="color:rgb(0,0,136)">if</span> (SD)
</span><span style="display:block"> SD->setHasDebugValue(<span style="color:rgb(0,0,136)">true</span>);</span></pre><br>to this:<br><pre style="margin-top:0px;margin-bottom:0px;padding-top:0.5em"><span style="font-size:inherit;line-height:16.25px;display:block">
if (SD) {</span><span style="display:block"><font size="3"><span style="line-height:16.25px"> assert(DbgInfo-></span></font><font size="3"><span style="line-height:16.25px">getSDDbgValues(SD).empty() || !SD->getHasDebugValue());
SD->setHasDebugValue(true);
}
DbgInfo->add(DB, SD, isParameter);
</span></font><span style="color:rgb(34,34,34);font-size:small;line-height:normal;font-family:arial;white-space:normal">I agree that adding a test case may still be questionable, given the need for this to end up getting a matching allocation, but maybe there are some simple cases that pretty obviously get the same memory address because they're back-to-back release-then-reallocate? (I think I added a test case like this when I fixed some similar bugs in ArgPromo or DAE, maybe... one of them has a two-pass algorithm that had some invalidity going on - </span><font face="arial"><span style="white-space:normal"><a href="http://llvm.org/viewvc/llvm-project?rev=219210&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=219210&view=rev</a> )</span></font></span></pre></div></div></div></div></div></div></div></blockquote><div><br></div><div>Sorry, I missed that part of the mail. With the assert in place, it might be possible to come up with an IR that reproduces this behavior, but as it relies on allocation order it would be quite fragile I suppose (Note that I do know absolutely nothing about SDAG allocation strategy). I’ll try to toy with it when I find some time, but no promises…</div></div></div></blockquote><div><br>Yep - see my example from ArgPromo - in that case an easy example existed due to the deallocation and immediate reallocation (of course this test case won't always exercise the problematic case - different memory allocators could easily defeat it - but it seems at least simple/plausible enough to include (adding appropriate comments & caveats to the test case in case it entirely rots, so someone knows they can remove it in that case))<br><br>I'd expect the case where the allocation is reused is a fairly simple one like the ArgPromo issue I hit, but if it doesn't fall out so easily, certainly don't spend a lot of time on it. Though I wouldn't mind if you looked at it sooner rather than later - at least enough to decide one way or the other. Unspecified times in the future are easily forgotten (but you know, doesn't have to be today - but within a week or so seems reasonable, beyond that and I imagine it'll get lost).<br><br>- David<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div>Fred</div><div><div class="h5"><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Modified:<br>
llvm/trunk/include/llvm/CodeGen/SelectionDAG.h<br>
llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/CodeGen/SelectionDAG.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SelectionDAG.h?rev=221709&r1=221708&r2=221709&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/SelectionDAG.h?rev=221709&r1=221708&r2=221709&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/CodeGen/SelectionDAG.h (original)<br>
+++ llvm/trunk/include/llvm/CodeGen/SelectionDAG.h Tue Nov 11 15:21:08 2014<br>
@@ -128,6 +128,10 @@ public:<br>
DbgValMap[Node].push_back(V);<br>
}<br>
<br>
+ /// \brief Invalidate all DbgValues attached to the node and remove<br>
+ /// it from the Node-to-DbgValues map.<br>
+ void erase(const SDNode *Node);<br>
+<br>
void clear() {<br>
DbgValMap.clear();<br>
DbgValues.clear();<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=221709&r1=221708&r2=221709&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=221709&r1=221708&r2=221709&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Tue Nov 11 15:21:08 2014<br>
@@ -687,6 +687,15 @@ void SelectionDAG::DeleteNodeNotInCSEMap<br>
DeallocateNode(N);<br>
}<br>
<br>
+void SDDbgInfo::erase(const SDNode *Node) {<br>
+ DbgValMapType::iterator I = DbgValMap.find(Node);<br>
+ if (I == DbgValMap.end())<br>
+ return;<br>
+ for (auto &Val: I->second)<br>
+ Val->setIsInvalidated();<br>
+ DbgValMap.erase(I);<br>
+}<br>
+<br>
void SelectionDAG::DeallocateNode(SDNode *N) {<br>
if (N->OperandsNeedDelete)<br>
delete[] N->OperandList;<br>
@@ -697,10 +706,9 @@ void SelectionDAG::DeallocateNode(SDNode<br>
<br>
NodeAllocator.Deallocate(AllNodes.remove(N));<br>
<br>
- // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.<br>
- ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);<br>
- for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)<br>
- DbgVals[i]->setIsInvalidated();<br>
+ // If any of the SDDbgValue nodes refer to this SDNode, invalidate<br>
+ // them and forget about that node.<br>
+ DbgInfo->erase(N);<br>
}<br>
<br>
#ifndef NDEBUG<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>
</div></blockquote></div></div></div><br></div></blockquote></div><br></div></div>