<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Nov 12, 2013 at 4:59 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<div><div class="h5">On Tue, Nov 12, 2013 at 4:46 PM, Manman Ren <span dir="ltr"><<a href="mailto:manman.ren@gmail.com" target="_blank">manman.ren@gmail.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"><div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">
<div><div>On Tue, Nov 12, 2013 at 4:38 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div><div><br><div class="gmail_quote">
On Tue, Nov 12, 2013 at 4:29 PM, Manman Ren <span dir="ltr"><<a href="mailto:manman.ren@gmail.com" target="_blank">manman.ren@gmail.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"><div style="font-size:12px;font-family:Helvetica">Hi All,</div>
<div style="font-size:12px;font-family:Helvetica"><br></div>


<div style="font-size:12px;font-family:Helvetica">
In LTO, we load in the source modules and link the source modules into a destination module.</div><div style="font-size:12px;font-family:Helvetica">Lots of MDNodes are only used by the source modules, for example Xalan used 649MB for MDNodes after loading and linking, but the actual destination module only has 393MB of MDNodes. There are 649-393MB (40% of 649MB) not used.</div>




<div style="font-size:12px;font-family:Helvetica"><br></div><div style="font-size:12px;font-family:Helvetica">MDNodes belong to the Context, deleting modules will not release the MDNodes.</div>
<div style="font-size:12px;font-family:Helvetica"><br></div><div style="font-size:12px;font-family:Helvetica">One possible solution is:</div><div style="font-size:12px;font-family:Helvetica">
<br></div><div style="font-size:12px;font-family:Helvetica">In LLVMContext, add “removeUnusedMDNodes" function</div><div style="font-size:12px;font-family:Helvetica">  It goes through OwnedModules and check if a MDNode is used by any of the modules, if not remove it.</div>




<div style="font-size:12px;font-family:Helvetica">  One implementation is to mark a visited MDNode used when traversing the module. After done traversing all modules, we can delete MDNodes in MDNodeSet that are not marked.</div>




<div style="font-size:12px;font-family:Helvetica"><br></div><div style="font-size:12px;font-family:Helvetica">In LTOCodeGenerator, add a vector of source modules that are added (these source modules will be linked with DestroySource mode).</div>




<div style="font-size:12px;font-family:Helvetica">In LTOCodeGenerator:: compile_to_file, delete all source modules that are linked in, then call LLVMContext::removeUnusedMDNodes</div><div style="font-size:12px;font-family:Helvetica">




—> I can’t find a better place to call the function. When we call compile_to_file, we should have done linking in all source modules.</div><div style="font-size:12px;font-family:Helvetica">Another possibility is to add a lto API so the linker can delete the source modules and call the API to release MDNodes.</div>




<div style="font-size:12px;font-family:Helvetica"><br></div><div style="font-size:12px;font-family:Helvetica">Other options are:</div><div style="font-size:12px;font-family:Helvetica">
1> Using a different LLVMContext for the destination module, but it didn’t work out since Linker was not designed to work with different LLVMContexts for source vs destination.</div><div style="font-size:12px;font-family:Helvetica">




2> removeUnusedMDNodes checks if a MDNode is used in a different way  (i.e use_empty() && !hasValueHandler()), but it does not remove MDNodes that form cycles.</div></blockquote></div><br></div></div>3) Make the MDNode be owned by the module that uses it?</div>


</div></blockquote><div> </div></div></div><div>MDNode is shared among modules so multiple modules can use it, if we specify an owner for a MDNode, that will prevent sharing.</div></div></div></div></blockquote><div><br>

</div></div></div><div>From your stats (40% stuck in the old module) it doesn't sound like this is buying us anything...</div></div></div></div></blockquote><div><br></div><div>Hi Chandler,</div><div><br></div><div>I don't quite get why you think sharing is not buying us anything...</div>
<div>It reduces the memory footprint of the source modules (there is sharing among the source modules) and the number of MDNodes created for the destination module (we do not need to re-create the MDNodes that can be shared).</div>
<div><br></div><div>The amount of sharing may not be that much but it still exists.</div><div><br></div><div>I had some experiments earlier on building clang with "-flto -g", if we dis-allow sharing between source modules and destination module, the memory footprint for MDNodes will increase by 15%.</div>
<div>If we disallow sharing among the source modules, the memory footprint for MDNodes will be even larger.</div><div><br></div><div>Thanks,</div><div>Manman</div><div><br></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">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </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">

<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span><font color="#888888"><div><br></div><div>Manman </div></font></span></div><br></div></div>
</blockquote></div><br></div></div>
</blockquote></div><br></div></div>