<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, May 8, 2021 at 8:15 AM 张驰斌 via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">





<div lang="ZH-CN" style="word-wrap:break-word">
<div class="gmail-m_6605138497372644107WordSection1">
<p class="MsoNormal"><span lang="EN-US">LLVM Developers,</span></p>
<p class="MsoNormal"><span lang="EN-US">       Hi!</span></p>
<p class="MsoNormal"><span lang="EN-US">       There is a scenario where I want to do full program analysis, ideally after link time optimization is done and before assembly is emitted, when all symbols are in LLVM IR form and contained in a single module.
 But after some time browsing stackoverflow, lld documentation, and the mailing lists, I still have little clue how to do it</span>…<span lang="EN-US"> </span></p></div></div></blockquote><div><br></div><div>LLVM docs are _useful_ but IMAO they only provide really high level guidelines. Therefore most of the time I just read the code. Most of the LLVM code has high quality comments. </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="MsoNormal"><span lang="EN-US">I am aware of the email sent by Y Liu and titled
</span>“<span lang="EN-US">how to add my own passes to LTO pass</span>”<span lang="EN-US">. But it doesn</span>’<span lang="EN-US">t seem to work for the new pass manager. A few questions:</span></p>
<p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt">
<u></u><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><span>1.<span style="font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:normal;font-size:7pt;line-height:normal;font-family:"Times New Roman""> 
</span></span></span></span><u></u><span lang="EN-US">The legacy pass manager has an extension point, `PassManagerBuilder::EP_FullLinkTimeOptimizationLast`. Unfortunately, I can</span>’<span lang="EN-US">t find a alternative in the new pass manager. The
 `Bye` example seems to suggest using `PB.<span class="gmail-m_6605138497372644107pl-c1">registerVectorizerStartEPCallback`. I am not sure whether doing so will result in my module pass being ran at compile or link time?</span></span></p></div></div></blockquote><div><br></div><div>PassBuilder (<a href="https://llvm.org/doxygen/classllvm_1_1PassBuilder.html">https://llvm.org/doxygen/classllvm_1_1PassBuilder.html</a>) is the right place to insert EP. If you're building a pipeline for normal optimization (non-LTO) you can leverage registerOptimizerLastEPCallback. But I think right now you can't insert any EP into LTO optimization (not pre-link one) pipeline, except peephole EP (registerPeepholeEPCallback). I don't know the rationale behind this decision, maybe folks familiar with LTO can give you more details. </div><div>The callback registered with registerVectorizerStartEPC will only be executed by normal optimization pipeline (including O0)</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><u></u><u></u></span></span></p>
<p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt">
<u></u><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><span>2.<span style="font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:normal;font-size:7pt;line-height:normal;font-family:"Times New Roman""> 
</span></span></span></span><u></u><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">I wonder how does clang communicates passes that need to be run at lto to lld. </span></span></p></div></div></blockquote><div>Most of the time they're communicated via optimization level (i.e. O0 ~ O3...). Clang tells LLD what optimization level to be performed during LTO. LLD then (indirectly) asks PassBuilder to build the corresponding LTO optimization pipeline.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">The command line that I attempt to get lto running is `clang -fexperimental-new-pass-manager -fpass-plugin=libmymodulepass.so
 -flto -fuse-ld=lld </span>…<span lang="EN-US">`. I straced the command for execv calls, and expected to see libmymodulepass.so to be passed to ld.lld as an argument, but found out it wasn</span>’<span lang="EN-US">t. Could  you shed some light on how clang
 and lld cope with each other during lto?</span></span></p></div></div></blockquote><div>I think neither LLD nor the gold plugin (llvm/tools/gold) can load (NewPM) PassPlugin into their LTO optimization pipeline. Your PassPlugin will eventually be loaded, but it's unable to register your custom Pass into the LTO optimization pipeline.</div><div>The `llvm-lto` and `llvm-lto2` command line tools do support PassPlugin though -- but these tools are only intended for debug and development.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><u></u><u></u></span></span></p>
<p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt">
<u></u><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><span>3.<span style="font-style:normal;font-variant-caps:normal;font-weight:normal;font-stretch:normal;font-size:7pt;line-height:normal;font-family:"Times New Roman""> 
</span></span></span></span><u></u><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">Greping `ld.lld --help` reveals an interesting option: `--lto-newpm-passes=<value>`. Does the <value> here mean built-in pass names like `dce`/`mem2reg`, </span></span></p></div></div></blockquote><div>Yes, for all available names please refer to this file: <a href="https://github.com/llvm/llvm-project/blob/main/llvm/lib/Passes/PassRegistry.def">https://github.com/llvm/llvm-project/blob/main/llvm/lib/Passes/PassRegistry.def</a> </div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">or is it an option to insert
 our plugin like libmymodulepass.so?</span></span></p></div></div></blockquote><div>No, I don't think LLD support PassPlugin now.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="gmail-m_6605138497372644107MsoListParagraph" style="margin-left:39pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><u></u><u></u></span></span></p>
<p class="MsoNormal" style="margin-left:21pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">I’ve looking into this for a while but haven’t made much progress…Any examples / command line on how to do this are greatly appreciated!</span></span></p></div></div></blockquote><div><br></div><div>The rule of thumb here is that Clang doesn't explicitly tell LLD what Passes to run during LTO -- it only tells LLD the optimization level (for LTO) which is delegated to PassBuilder to construct a pre-defined optimization pipeline accordingly.</div><div>And unfortunately I think there are little ways to customize LTO optimization pipeline via EP. But maybe this is just a missing feature, if that's the case feel free to send a patch.</div><div><br></div><div>-Min</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div lang="ZH-CN" style="word-wrap:break-word"><div class="gmail-m_6605138497372644107WordSection1"><p class="MsoNormal" style="margin-left:21pt"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US"><u></u><u></u></span></span></p>
<p class="MsoNormal"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US"><u></u> <u></u></span></span></p>
<p class="MsoNormal"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">Best Wishes,<u></u><u></u></span></span></p>
<p class="MsoNormal"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">Chibin Zhang<u></u><u></u></span></span></p>
<p class="MsoNormal"><span class="gmail-m_6605138497372644107pl-c1"><span lang="EN-US">2021.5.8</span></span><span lang="EN-US" style="font-size:12pt;font-family:SimSun"><u></u><u></u></span></p>
</div>
</div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Min-Yih Hsu</div><div>Ph.D Student in ICS Department, University of California, Irvine (UCI).<br></div></div></div></div></div></div></div></div>