<div dir="ltr"><font face="arial, helvetica, sans-serif">After digging a bit more it seems we can achieve the same as gcc's -fwhole-program by simply marking the mult function as "static" which is all -fwhole-program does anyway.  From <a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__gcc.gnu.org_onlinedocs_gcc_Optimize-2DOptions.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=DK4gVXODTNyqyOhwrEdk-dmEek7Ag82Ej8VdPfydKas&s=DU0h2dJionEgs68hwnyPdjcpAp55jiRq0vaZClUxH6g&e=">https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html</a>: </font><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif"> <span style="color:rgb(0,0,0)"><i>-fwhole-program</i></span></font><dd style="color:rgb(0,0,0)"><i><font face="arial, helvetica, sans-serif"><a name="index-fwhole-program-1070"></a>Assume that the current compilation unit represents the whole program being compiled. All public functions and variables with the exception of <code>main</code> and those merged by attribute<code>externally_visible</code> become static functions and in effect are optimized more aggressively by interprocedural optimizers.</font></i></dd><div><br></div>







<p class="">So we can accomplish that for now with a simple pass on the source.  But that had me thinking, how do we accomplish the same for unused C++ classes or member functions within classes.  I figured we could accomplish that by changing the linkage type within the llvm IR.  But it turns out these already get linkonce_odr linkage.  <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_LangRef.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=DK4gVXODTNyqyOhwrEdk-dmEek7Ag82Ej8VdPfydKas&s=_kIA5RWf2mrCJCQls52Vka1PL2ZYLqaVfbY4cKs01o8&e=">http://llvm.org/docs/LangRef.html</a> states<font face="arial, helvetica, sans-serif"> "<span style="color:rgb(0,0,0);line-height:21px">Unreferenced </span><tt class="" style="color:rgb(0,0,0);line-height:21px"><span class="">linkonce</span></tt><span style="color:rgb(0,0,0);line-height:21px"> globals are allowed to be discarded" </span></font></p><p class=""><span class="">class</span><span class=""> num{</span></p>
<p class=""><span class="">  </span><span class="">private</span><span class="">:</span></p>
<p class=""><span class="">    </span><span class="">int</span><span class=""> number;</span></p>
<p class=""><span class="">  </span><span class="">public</span><span class="">:</span></p>
<p class=""><span class="">    num(</span><span class="">int</span><span class=""> n):number(n){}</span></p>
<p class=""><span class="">    </span><span class="">int</span><span class=""> mult(</span><span class="">int</span><span class=""> other){</span></p>
<p class=""><span class="">        </span><span class="">return</span><span class=""> number*other;</span></p>
<p class=""><span class="">    }</span></p>
<p class=""><span class="">};</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">int</span><span class=""> main(</span><span class="">void</span><span class="">){</span></p>
<p class=""><span class="">    </span><span class="">return</span><span class=""> </span><span class="">0</span><span class="">;</span></p>
<div>} </div></div><div><br></div><div>If I compile the above to LLVM IR there is actually no trace of the num class which kind of baffles me because what if I was compiling a library and this class was needed in the library consumer?</div><div><br></div><div>Either way with this knowledge I think we can get the results we're looking for in the short term and will follow up if we find or come up with anything that could be generally useful to others.</div><div><br></div><div>Thanks,</div><div>Ed</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 18, 2015 at 9:46 AM, <a href="mailto:ed@modk.it">ed@modk.it</a> <span dir="ltr"><<a href="mailto:ed@modk.it" target="_blank">ed@modk.it</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks Nick.  I've been pursuing Gao's technique but can't seem to get opt to remove obviously dead code from even the following trivial example:<div><br></div><div>







<p><span>int</span><span> mult(</span><span>int</span><span> a, </span><span>int</span><span> b){</span></p>
<p><span>    </span><span>return</span><span> a*b;</span></p>
<p><span>}</span></p>
<p><span></span><br></p>
<p><span>int</span><span> main(</span><span>void</span><span>){</span></p>
<p><span>    </span><span>return</span><span> </span><span>0</span><span>;</span></p>
<p><span>}</span></p><p><span><br></span></p><p>While mult is never called it still is not removed.  I just can't seem to get opt to understand it's seeing the whole program so it can remove this globally accessible function.  What am I missing?  Seems related to the missing -fwhole-program flag in clang.  Perhaps this is not even possible?  If I can't get any answers here I may repost that specific question since I didn't list [opt] in the original question subject.</p><p><br></p><p>Thanks,</p><p>Ed</p></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 17, 2015 at 1:15 AM, Nick Lewycky <span dir="ltr"><<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><a href="mailto:ed@modk.it" target="_blank">ed@modk.it</a> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>
<br>
    Is there a reason why LLVM's link-time optimization won't work for you?<br>
<br>
    <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_GoldPlugin.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=DK4gVXODTNyqyOhwrEdk-dmEek7Ag82Ej8VdPfydKas&s=tznP4wlZVjGC77P85hHCiskWhVU041iUh633t17kkyk&e=" rel="noreferrer" target="_blank">http://llvm.org/docs/GoldPlugin.html</a><br></span>
    <<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_GoldPlugin.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=rF94h73bKDdWVhxOWqRXpvw5pSMgvuHQXJ__qw8n2LU&s=PR31BXeMANGrAQP2Tt9Eg5psH82vj8Oq1WmyprGhyn8&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_GoldPlugin.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=rF94h73bKDdWVhxOWqRXpvw5pSMgvuHQXJ__qw8n2LU&s=PR31BXeMANGrAQP2Tt9Eg5psH82vj8Oq1WmyprGhyn8&e=</a>><br>
    <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_LinkTimeOptimization.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=DK4gVXODTNyqyOhwrEdk-dmEek7Ag82Ej8VdPfydKas&s=xZf4N8ZbKbOHGrsfsnU8ofjFHamrbXGJbbZzWhP5pD0&e=" rel="noreferrer" target="_blank">http://llvm.org/docs/LinkTimeOptimization.html</a><br>
    <<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_LinkTimeOptimization.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=rF94h73bKDdWVhxOWqRXpvw5pSMgvuHQXJ__qw8n2LU&s=PoqmeRXrssdG9xj6Fko_SKttwLPWqUVkxFH41dOcg4w&e=" rel="noreferrer" target="_blank">https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_docs_LinkTimeOptimization.html&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=Mfk2qtn1LTDThVkh6-oGglNfMADXfJdty4_bhmuhMHA&m=rF94h73bKDdWVhxOWqRXpvw5pSMgvuHQXJ__qw8n2LU&s=PoqmeRXrssdG9xj6Fko_SKttwLPWqUVkxFH41dOcg4w&e=</a>><span><br>
<br>
<br>
Well the primary motivation to move to LLVM is licensing which is why we<br>
also ditched binutils since we can't package gcc for iOS due to the<br>
GPL.  So in the end the gold plugin wouldn't work for licensing reasons<br>
even if we can get it to work technically but thanks for the links I'm<br>
still trying to wrap my head around the problem and any info helps.<br>
</span></blockquote>
<br>
The right future is a world where lld performs llvm lto for you.<br>
<br>
Until then, the technique in Gao's PDF is what I would recommend.<span><font color="#888888"><br>
<br>
Nick<br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>