<div dir="ltr">Hi Benoit,<div><br></div><div>It seems to me that LinkOnlyNeeded failing to link the llvm.* special variables is a bug. I think we should probably just change the behaviour of LinkOnlyNeeded so that it links them.</div><div><br></div><div>Note that ThinLTO does not use the Linker class, it uses IRMover directly. That might not be appropriate for your use case, though, because IRMover does not follow references when linking (for example, if your module defines two functions f and g, where f calls g, it will not link g if you only ask for f).</div><div><br></div><div>Peter</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 20, 2017 at 8:38 AM, Benoit Belley via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for the hindsight.<br>
<br>
I am currently working on a patch/potential fix which introduces a new<br>
Linker::<wbr>ImportIntrinsicGlobalVariables flag. The patch includes a unit<br>
test reproducing the problem. Hopefully, that will help getting more<br>
feedback.<br>
<br>
Note that it might take a while before I am allowed to upload the patch<br>
since I need approval from Autodesk Legal department.<br>
<span class=""><br>
Cheers,<br>
Benoit<br>
<br>
Benoit Belley<br>
Sr Principal Developer<br>
M&E-Product Development Group<br>
<br>
MAIN <a href="tel:%2B1%20514%20393%201616" value="+15143931616">+1 514 393 1616</a><br>
DIRECT <a href="tel:%2B1%20438%20448%206304" value="+14384486304">+1 438 448 6304</a><br>
FAX <a href="tel:%2B1%20514%20393%200110" value="+15143930110">+1 514 393 0110</a><br>
<br>
Twitter <<a href="http://twitter.com/autodesk" rel="noreferrer" target="_blank">http://twitter.com/autodesk</a>><br>
Facebook <<a href="https://www.facebook.com/Autodesk" rel="noreferrer" target="_blank">https://www.facebook.com/<wbr>Autodesk</a>><br>
<br>
Autodesk, Inc.<br>
10 Duke Street<br>
Montreal, Quebec, Canada H3C 2L7<br>
<a href="http://www.autodesk.com" rel="noreferrer" target="_blank">www.autodesk.com</a> <<a href="http://www.autodesk.com/" rel="noreferrer" target="_blank">http://www.autodesk.com/</a>><br>
<br>
<br>
<br>
<br>
<br>
</span>From:  David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>><br>
Date:  mardi 20 juin 2017 à 11:12<br>
To:  Benoit Belley <<a href="mailto:benoit.belley@autodesk.com">benoit.belley@autodesk.com</a>>, llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>>, Lang Hames <<a href="mailto:lhames@gmail.com">lhames@gmail.com</a>>, Teresa Johnson<br>
<<a href="mailto:tejohnson@google.com">tejohnson@google.com</a>><br>
Subject:  Re: [llvm-dev] JIT, LTO and @llvm.global_ctors: Looking for<br>
advise<br>
<div><div class="h5"><br>
<br>
>+Lang (for JIT) & Teresa (for LTO/ThinLTO).<br>
><br>
>Sounds like maybe the LinkOnlyNeeded got reused for a bit more than the<br>
>original intent & maybe there should be more than one flag here - not<br>
>sure.<br>
><br>
>On Mon, Jun 19, 2017 at 9:16 AM Benoit Belley via llvm-dev<br>
><<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
><br>
>Hi Everyone,<br>
><br>
>We are looking for advise regarding the proper use of LTO in<br>
>conjunction with just-in time generated code. Our usage scenario goes<br>
>as follows.<br>
><br>
>  1. Our front-end generates an LLVM module.<br>
><br>
>  2. A small runtime support library is linked-in. The runtime<br>
>     library is distributed as bitcode. It is generated using "clang++<br>
>     -emit-llvm' and 'llvm-link'. This allows LTO to kick-in and<br>
>     functions from the runtime support library become candidates for<br>
>     inlining. This is the desired effect that we are looking for.<br>
><br>
>  3. The resulting LLVM module is compiled and dynamically loaded. We<br>
>     are currently using the MCJIT API, but are planning to move to ORC<br>
>     very soon.<br>
><br>
>Our LLVM module linking code goes roughly as follows:<br>
><br>
>  Linker linker(jittedModule);<br>
>  std::unique_ptr<llvm::Module> moduleToLink(<br>
>    getLazyIRFileModule(<wbr>bcFileName, error, context));<br>
>  linker.linkInModule(std::move(<wbr>module),<br>
>                      Linker::LinkOnlyNeeded |<br>
>                      Linker::<wbr>InternalizeLinkedSymbol);<br>
><br>
>Our issue is with the Linker::LinkOnlyNeeded flag. Using it has a huge<br>
>positive impact on link and compilation time :-). But, it causes the<br>
>@llvm.global_ctors and @llvm.global_dtors references from the<br>
>linked-in modules to be discarded :-(. AFAICT, the Linker code assumes<br>
>ThinLTO when the LinkOnlyNeeded flags is specified, and full-LTO<br>
>otherwise.<br>
><br>
>To resolve this, we have locally patched<br>
>llvm/lib/Linker/LinkModules.<wbr>cpp with:<br>
><br>
>  bool ModuleLinker::run() {<br>
><br>
>    // ....<br>
><br>
>    if (<wbr>shouldImportIntrinsicGlobalVar<wbr>iables()) {<br>
>      auto addIntrinsicGlobalVariable = [ValuesToLink,<br>
>                                         srcM](llvm::StringRef name) {<br>
>        if (GlobalValue *GV = SrcM->getGlobalVariable(name)) {<br>
>          ValuesToLink.insert(GV);<br>
>        }<br>
>      };<br>
><br>
>      // These are added here, because they must not be internalized.<br>
>      addIntrinsicGlobalVariable("<wbr>llvm.used");<br>
>      addIntrinsicGlobalVariable("<wbr>llvm.compiler.used");<br>
>      addIntrinsicGlobalVariable("<wbr>llvm.global_ctors");<br>
>      addIntrinsicGlobalVariable("<wbr>llvm.global_dtors");<br>
>    }<br>
><br>
>    // ...<br>
><br>
>  }<br>
><br>
>Questions:<br>
><br>
>   1. Is attempting to use llvm::Linker appropriate for our usage<br>
>      pattern ? Should we directly use llvm::IRMover instead ?<br>
><br>
>   2. Or, is our patch to ModuleLinker::run() the way to go ? Should<br>
>      we submit back a patch along these lines ?<br>
><br>
>   3. Other suggestions ?<br>
><br>
>[Note] We are currently using LLVM 4.0.1-rc2.<br>
><br>
>Cheers,<br>
>Benoit<br>
><br>
><br>
><br>
>Benoit Belley<br>
>Sr Principal Developer<br>
>M&E-Product Development Group<br>
><br>
</div></div>>MAIN <a href="tel:%2B1%20514%20393%201616" value="+15143931616">+1 514 393 1616</a> <tel:(514)%20393-1616><br>
>DIRECT <a href="tel:%2B1%20438%20448%206304" value="+14384486304">+1 438 448 6304</a> <tel:(438)%20448-6304><br>
>FAX <a href="tel:%2B1%20514%20393%200110" value="+15143930110">+1 514 393 0110</a> <tel:(514)%20393-0110><br>
<span class="">><br>
>Twitter <<a href="http://twitter.com/autodesk" rel="noreferrer" target="_blank">http://twitter.com/autodesk</a>><br>
>Facebook <<a href="https://www.facebook.com/Autodesk" rel="noreferrer" target="_blank">https://www.facebook.com/<wbr>Autodesk</a>><br>
><br>
>Autodesk, Inc.<br>
>10 Duke Street<br>
>Montreal, Quebec, Canada H3C 2L7<br>
</span>><a href="http://www.autodesk.com" rel="noreferrer" target="_blank">www.autodesk.com</a> <<a href="http://www.autodesk.com" rel="noreferrer" target="_blank">http://www.autodesk.com</a>> <<a href="http://www.autodesk.com/" rel="noreferrer" target="_blank">http://www.autodesk.com/</a>><br>
<div class="HOEnZb"><div class="h5">><br>
><br>
>_____________________________<wbr>__________________<br>
>LLVM Developers mailing list<br>
><a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-<wbr>bin/mailman/listinfo/llvm-dev</a><br>
<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</div>