<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Mar 1, 2017 at 3:31 PM, Sean Silva <span dir="ltr"><<a href="mailto:chisophugis@gmail.com" target="_blank">chisophugis@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="gmail-">On Wed, Mar 1, 2017 at 3:00 PM, Peter Collingbourne via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br></span><div><div class="gmail-h5"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: pcc<br>
Date: Wed Mar  1 17:00:10 2017<br>
New Revision: 296702<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=296702&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject?rev=296702&view=rev</a><br>
Log:<br>
ELF: Add ThinLTO caching support.<br>
<br>
This patch adds an option named --thinlto-cache-dir, which specifies the<br>
path to a directory in which to cache native object files for ThinLTO<br>
incremental builds.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D30509" rel="noreferrer" target="_blank">https://reviews.llvm.org/D3050<wbr>9</a><br>
<br>
Added:<br>
    lld/trunk/test/ELF/lto/Inputs/<wbr>cache.ll<br>
    lld/trunk/test/ELF/lto/cache.l<wbr>l<br>
Modified:<br>
    lld/trunk/ELF/Config.h<br>
    lld/trunk/ELF/Driver.cpp<br>
    lld/trunk/ELF/LTO.cpp<br>
    lld/trunk/ELF/LTO.h<br>
    lld/trunk/ELF/Options.td<br>
<br>
Modified: lld/trunk/ELF/Config.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=296702&r1=296701&r2=296702&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/ELF/Config.h?<wbr>rev=296702&r1=296701&r2=296702<wbr>&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Config.h (original)<br>
+++ lld/trunk/ELF/Config.h Wed Mar  1 17:00:10 2017<br>
@@ -86,6 +86,7 @@ struct Configuration {<br>
   llvm::StringRef OptRemarksFilename;<br>
   llvm::StringRef SoName;<br>
   llvm::StringRef Sysroot;<br>
+  llvm::StringRef ThinLTOCacheDir;<br>
   std::string RPath;<br>
   std::vector<<wbr>VersionDefinition> VersionDefinitions;<br>
   std::vector<llvm::StringRef> AuxiliaryList;<br>
<br>
Modified: lld/trunk/ELF/Driver.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=296702&r1=296701&r2=296702&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/ELF/Driver.cpp<wbr>?rev=296702&r1=296701&r2=29670<wbr>2&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Driver.cpp (original)<br>
+++ lld/trunk/ELF/Driver.cpp Wed Mar  1 17:00:10 2017<br>
@@ -583,6 +583,7 @@ void LinkerDriver::readConfigs(opt:<wbr>:Inpu<br>
   Config->Sysroot = getString(Args, OPT_sysroot);<br>
   Config->Target1Rel = getArg(Args, OPT_target1_rel, OPT_target1_abs, false);<br>
   Config->Target2 = getTarget2(Args);<br>
+  Config->ThinLTOCacheDir = getString(Args, OPT_thinlto_cache_dir);<br>
   Config->ThinLTOJobs = getInteger(Args, OPT_thinlto_jobs, -1u);<br>
   Config->Threads = getArg(Args, OPT_threads, OPT_no_threads, true);<br>
   Config->Trace = Args.hasArg(OPT_trace);<br>
<br>
Modified: lld/trunk/ELF/LTO.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=296702&r1=296701&r2=296702&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/ELF/LTO.cpp?re<wbr>v=296702&r1=296701&r2=296702&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/LTO.cpp (original)<br>
+++ lld/trunk/ELF/LTO.cpp Wed Mar  1 17:00:10 2017<br>
@@ -18,6 +18,7 @@<br>
 #include "llvm/ADT/StringRef.h"<br>
 #include "llvm/ADT/Twine.h"<br>
 #include "llvm/IR/DiagnosticPrinter.h"<br>
+#include "llvm/LTO/Caching.h"<br>
 #include "llvm/LTO/Config.h"<br>
 #include "llvm/LTO/LTO.h"<br>
 #include "llvm/Object/SymbolicFile.h"<br>
@@ -142,11 +143,24 @@ std::vector<InputFile *> BitcodeCompiler<br>
   std::vector<InputFile *> Ret;<br>
   unsigned MaxTasks = LTOObj->getMaxTasks();<br>
   Buff.resize(MaxTasks);<br>
+  Files.resize(MaxTasks);<br>
<br>
-  checkError(LTOObj->run([&](siz<wbr>e_t Task) {<br>
-    return llvm::make_unique<lto::NativeO<wbr>bjectStream>(<br>
-        llvm::make_unique<raw_svector_<wbr>ostream>(Buff[Task]));<br>
-  }));<br>
+  // The --thinlto-cache-dir option specifies the path to a directory in which<br>
+  // to cache native object files for ThinLTO incremental builds. If a path was<br>
+  // specified, configure LTO to use it as the cache directory.<br>
+  lto::NativeObjectCache Cache;<br>
+  if (!Config->ThinLTOCacheDir.empt<wbr>y())<br>
+    Cache = lto::localCache(Config->ThinLT<wbr>OCacheDir,<br>
+                            [&](size_t Task, StringRef Path) {<br>
+                              Files[Task] = check(MemoryBuffer::getFile(Pa<wbr>th));<br>
+                            });<br>
+<br>
+  checkError(LTOObj->run(<br>
+      [&](size_t Task) {<br>
+        return llvm::make_unique<lto::NativeO<wbr>bjectStream>(<br>
+            llvm::make_unique<raw_svector_<wbr>ostream>(Buff[Task]));<br>
+      },<br>
+      Cache));<br>
<br>
   for (unsigned I = 0; I != MaxTasks; ++I) {<br>
     if (Buff[I].empty())<br>
@@ -160,5 +174,10 @@ std::vector<InputFile *> BitcodeCompiler<br>
     InputFile *Obj = createObjectFile(MemoryBufferR<wbr>ef(Buff[I], "lto.tmp"));<br>
     Ret.push_back(Obj);<br>
   }<br>
+<br>
+  for (std::unique_ptr<MemoryBuffer> &File : Files)<br>
+    if (File)<br>
+      Ret.push_back(createObjectFile<wbr>(*File));<br>
+<br>
   return Ret;<br>
 }<br>
<br>
Modified: lld/trunk/ELF/LTO.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.h?rev=296702&r1=296701&r2=296702&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/ELF/LTO.h?rev=<wbr>296702&r1=296701&r2=296702&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/LTO.h (original)<br>
+++ lld/trunk/ELF/LTO.h Wed Mar  1 17:00:10 2017<br>
@@ -49,6 +49,7 @@ public:<br>
 private:<br>
   std::unique_ptr<llvm::lto::LT<wbr>O> LTOObj;<br>
   std::vector<SmallString<0>> Buff;<br>
+  std::vector<std::unique_ptr<Me<wbr>moryBuffer>> Files;<br>
 };<br>
 }<br>
 }<br>
<br>
Modified: lld/trunk/ELF/Options.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=296702&r1=296701&r2=296702&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/ELF/Options.td<wbr>?rev=296702&r1=296701&r2=29670<wbr>2&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/ELF/Options.td (original)<br>
+++ lld/trunk/ELF/Options.td Wed Mar  1 17:00:10 2017<br>
@@ -392,4 +392,6 @@ def opt_remarks_filename: S<"opt-remarks<br>
 def opt_remarks_with_hotness: F<"opt-remarks-with-hotness">,<br>
   HelpText<"Include hotness informations in the optimization remarks file">;<br>
 def save_temps: F<"save-temps">;<br>
+def thinlto_cache_dir: J<"thinlto-cache-dir=">,<br>
+  HelpText<"Path to ThinLTO cached object file directory">;<br>
 def thinlto_jobs: J<"thinlto-jobs=">, HelpText<"Number of ThinLTO jobs">;<br>
<br>
Added: lld/trunk/test/ELF/lto/Inputs/<wbr>cache.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/Inputs/cache.ll?rev=296702&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/test/ELF/lto/<wbr>Inputs/cache.ll?rev=296702&<wbr>view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/test/ELF/lto/Inputs/<wbr>cache.ll (added)<br>
+++ lld/trunk/test/ELF/lto/Inputs/<wbr>cache.ll Wed Mar  1 17:00:10 2017<br>
@@ -0,0 +1,10 @@<br>
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32<wbr>:64-S128"<br>
+target triple = "x86_64-unknown-linux-gnu"<br>
+<br>
+define i32 @main() {<br>
+entry:<br>
+  call void (...) @globalfunc()<br>
+  ret i32 0<br>
+}<br>
+<br>
+declare void @globalfunc(...)<br>
<br>
Added: lld/trunk/test/ELF/lto/cache.l<wbr>l<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/lto/cache.ll?rev=296702&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-pr<wbr>oject/lld/trunk/test/ELF/lto/<wbr>cache.ll?rev=296702&view=auto</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- lld/trunk/test/ELF/lto/cache.l<wbr>l (added)<br>
+++ lld/trunk/test/ELF/lto/cache.l<wbr>l Wed Mar  1 17:00:10 2017<br>
@@ -0,0 +1,15 @@<br>
+; RUN: opt -module-hash -module-summary %s -o %t.o<br>
+; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.o<br>
+<br>
+; RUN: rm -Rf %t.cache && mkdir %t.cache<br>
+; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3.o %t2.o %t.o<br></blockquote><div><br></div></div></div><div>I find this a bit confusing. The argument to `-o` is a .o file, but -r is not passed.</div></div></div></div></blockquote><div><br></div><div>Renamed to %t3 in r296728.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Also, there doesn't seem to be a definition of _start in any of the input files?</div></div></div></div></blockquote><div><br></div><div>I believe that we will accept an undefined entry symbol in executables with a warning (that is what bfd and gold do). I don't know how important it is to accept that, though. Rui, Rafael?</div><div><br></div><div>For now I've renamed the main symbol to _start.</div><div><br></div><div>Thanks,</div></div>-- <br><div class="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</div></div>