Sorry, reverted.<br>Thanks for the kind reminding.<br><br><div class="gmail_quote">On Sat, Dec 1, 2012 at 5:10 PM, Chandler Carruth <span dir="ltr"><<a href="mailto:chandlerc@google.com" target="_blank">chandlerc@google.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="font-family:arial,helvetica,sans-serif;font-size:10pt"><div dir="ltr">Zhou, this commit was not approved in code review. Please revert and ping the code review thread until the review concludes with an explicit approval to commit. Commit after approval is really fundamental to the LLVM development process, don't circumvent it.</div>



<div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Nov 30, 2012 at 8:38 PM, Zhou Sheng <span dir="ltr"><<a href="mailto:zhousheng00@gmail.com" target="_blank">zhousheng00@gmail.com</a>></span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: sheng<br>
Date: Fri Nov 30 22:38:53 2012<br>
New Revision: 169079<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=169079&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=169079&view=rev</a><br>
Log:<br>
The patch is to improve the memory footprint of pass GlobalOpt.<br>
Also check in a case to repeat the issue, on which 'opt -globalopt' consumes 1.6GB memory.<br>
The big memory footprint cause is that current GlobalOpt one by one hoists and stores the leaf element constant into the global array, in each iteration, it recreates the global array initializer constant and leave the old initializer alone. This may result in many obsolete constants left.<br>




For example:  we have global array @rom = global [16 x i32] zeroinitializer<br>
After the first element value is hoisted and installed:   @rom = global [16 x i32] [ 1, 0, 0, ... ]<br>
After the second element value is installed:  @rom = global [16 x 32] [ 1, 2, 0, 0, ... ]        // here the previous initializer is obsolete<br>
...<br>
When the transform is done, we have 15 obsolete initializers left useless.<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/GlobalOpt/big-memory-footprint.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=169079&r1=169078&r2=169079&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=169079&r1=169078&r2=169079&view=diff</a><br>




==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Fri Nov 30 22:38:53 2012<br>
@@ -36,6 +36,7 @@<br>
 #include "llvm/ADT/DenseMap.h"<br>
 #include "llvm/ADT/SmallPtrSet.h"<br>
 #include "llvm/ADT/SmallVector.h"<br>
+#include "llvm/ADT/SetVector.h"<br>
 #include "llvm/ADT/Statistic.h"<br>
 #include "llvm/ADT/STLExtras.h"<br>
 #include <algorithm><br>
@@ -2398,7 +2399,8 @@<br>
 /// initializer.  This returns 'Init' modified to reflect 'Val' stored into it.<br>
 /// At this point, the GEP operands of Addr [0, OpNo) have been stepped into.<br>
 static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,<br>
-                                   ConstantExpr *Addr, unsigned OpNo) {<br>
+                                   ConstantExpr *Addr, unsigned OpNo,<br>
+                                   SetVector<Constant*>& Obsolete) {<br>
   // Base case of the recursion.<br>
   if (OpNo == Addr->getNumOperands()) {<br>
     assert(Val->getType() == Init->getType() && "Type mismatch!");<br>
@@ -2415,7 +2417,9 @@<br>
     ConstantInt *CU = cast<ConstantInt>(Addr->getOperand(OpNo));<br>
     unsigned Idx = CU->getZExtValue();<br>
     assert(Idx < STy->getNumElements() && "Struct index out of range!");<br>
-    Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1);<br>
+    if (Elts[Idx]->getType()->isAggregateType())<br>
+      Obsolete.insert(Elts[Idx]);<br>
+    Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1, Obsolete);<br>
<br>
     // Return the modified struct.<br>
     return ConstantStruct::get(STy, Elts);<br>
@@ -2435,8 +2439,11 @@<br>
     Elts.push_back(Init->getAggregateElement(i));<br>
<br>
   assert(CI->getZExtValue() < NumElts);<br>
+  Constant *OrigElem = Elts[CI->getZExtValue()];<br>
+  if (OrigElem->getType()->isAggregateType())<br>
+    Obsolete.insert(OrigElem);<br>
   Elts[CI->getZExtValue()] =<br>
-    EvaluateStoreInto(Elts[CI->getZExtValue()], Val, Addr, OpNo+1);<br>
+    EvaluateStoreInto(OrigElem, Val, Addr, OpNo+1, Obsolete);<br>
<br>
   if (Init->getType()->isArrayTy())<br>
     return ConstantArray::get(cast<ArrayType>(InitTy), Elts);<br>
@@ -2452,9 +2459,20 @@<br>
     return;<br>
   }<br>
<br>
+  // Collect obsolete constants created in previous CommitValueTo() invoke.<br>
+  SetVector<Constant*> Obsolete;<br>
   ConstantExpr *CE = cast<ConstantExpr>(Addr);<br>
   GlobalVariable *GV = cast<GlobalVariable>(CE->getOperand(0));<br>
-  GV->setInitializer(EvaluateStoreInto(GV->getInitializer(), Val, CE, 2));<br>
+  Constant *OrigInit = GV->getInitializer();<br>
+  if (OrigInit->getType()->isAggregateType())<br>
+    Obsolete.insert(OrigInit);<br>
+  Constant *Init = EvaluateStoreInto(OrigInit, Val, CE, 2, Obsolete);<br>
+  GV->setInitializer(Init);<br>
+<br>
+  for (unsigned i = 0; i < Obsolete.size(); ++i) {<br>
+    if (Obsolete[i]->use_empty())<br>
+      Obsolete[i]->destroyConstant();<br>
+  }<br>
 }<br>
<br>
 namespace {<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>
</blockquote></div><br>