<div dir="ltr">+Lang (for JIT) & Teresa (for LTO/ThinLTO).<br><br>Sounds like maybe the LinkOnlyNeeded got reused for a bit more than the original intent & maybe there should be more than one flag here - not sure.<br><br><div class="gmail_quote"><div dir="ltr">On Mon, Jun 19, 2017 at 9:16 AM Benoit Belley 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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(bcFileName, error, context));<br>
  linker.linkInModule(std::move(module),<br>
                      Linker::LinkOnlyNeeded |<br>
                      Linker::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.cpp with:<br>
<br>
  bool ModuleLinker::run() {<br>
<br>
    // ....<br>
<br>
    if (shouldImportIntrinsicGlobalVariables()) {<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("llvm.used");<br>
      addIntrinsicGlobalVariable("llvm.compiler.used");<br>
      addIntrinsicGlobalVariable("llvm.global_ctors");<br>
      addIntrinsicGlobalVariable("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>
MAIN <a href="tel:(514)%20393-1616" value="+15143931616" target="_blank">+1 514 393 1616</a><br>
DIRECT <a href="tel:(438)%20448-6304" value="+14384486304" target="_blank">+1 438 448 6304</a><br>
FAX <a href="tel:(514)%20393-0110" value="+15143930110" target="_blank">+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/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>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>