<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;color:rgb(0,0,0);padding-top:0.5em;font-size:medium;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="color:rgb(0,0,0);font-size:medium;line-height:16.25px;display:block">
if (SD) {</span><span style="display:block"><font color="#000000" size="3"><span style="line-height:16.25px"> assert(DbgInfo-></span></font><font color="#000000" 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">http://llvm.org/viewvc/llvm-project?rev=219210&view=rev</a> )</span></font><span style="color:rgb(34,34,34);font-size:small;line-height:normal;font-family:arial;white-space:normal"><br></span></span></pre> </div><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>