[lld] r312770 - COFF: Implement ThinLTO cache and cache pruning support.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 7 17:50:50 PDT 2017


Author: pcc
Date: Thu Sep  7 17:50:50 2017
New Revision: 312770

URL: http://llvm.org/viewvc/llvm-project?rev=312770&view=rev
Log:
COFF: Implement ThinLTO cache and cache pruning support.

Differential Revision: https://reviews.llvm.org/D37607

Added:
    lld/trunk/test/COFF/Inputs/lto-cache.ll
    lld/trunk/test/COFF/lto-cache.ll
Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/LTO.cpp
    lld/trunk/COFF/LTO.h
    lld/trunk/COFF/Options.td

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=312770&r1=312769&r2=312770&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Thu Sep  7 17:50:50 2017
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Object/COFF.h"
+#include "llvm/Support/CachePruning.h"
 #include <cstdint>
 #include <map>
 #include <set>
@@ -123,6 +124,11 @@ struct Configuration {
   // Used for /opt:lldltopartitions=N
   unsigned LTOPartitions = 1;
 
+  // Used for /opt:lldltocache=path
+  StringRef LTOCache;
+  // Used for /opt:lldltocachepolicy=policy
+  llvm::CachePruningPolicy LTOCachePolicy;
+
   // Used for /merge:from=to (e.g. /merge:.rdata=.text)
   std::map<StringRef, StringRef> Merge;
 

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=312770&r1=312769&r2=312770&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Thu Sep  7 17:50:50 2017
@@ -877,6 +877,16 @@ void LinkerDriver::link(ArrayRef<const c
   if (Args.hasArg(OPT_lldsavetemps))
     Config->SaveTemps = true;
 
+  // Handle /lldltocache
+  if (auto *Arg = Args.getLastArg(OPT_lldltocache))
+    Config->LTOCache = Arg->getValue();
+
+  // Handle /lldsavecachepolicy
+  if (auto *Arg = Args.getLastArg(OPT_lldltocachepolicy))
+    Config->LTOCachePolicy = check(
+        parseCachePruningPolicy(Arg->getValue()),
+        Twine("/lldltocachepolicy: invalid cache policy: ") + Arg->getValue());
+
   // Handle /failifmismatch
   for (auto *Arg : Args.filtered(OPT_failifmismatch))
     checkFailIfMismatch(Arg->getValue());

Modified: lld/trunk/COFF/LTO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/LTO.cpp?rev=312770&r1=312769&r2=312770&view=diff
==============================================================================
--- lld/trunk/COFF/LTO.cpp (original)
+++ lld/trunk/COFF/LTO.cpp Thu Sep  7 17:50:50 2017
@@ -18,6 +18,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/LTO/Caching.h"
 #include "llvm/LTO/Config.h"
 #include "llvm/LTO/LTO.h"
 #include "llvm/Object/SymbolicFile.h"
@@ -118,11 +119,27 @@ void BitcodeCompiler::add(BitcodeFile &F
 std::vector<StringRef> BitcodeCompiler::compile() {
   unsigned MaxTasks = LTOObj->getMaxTasks();
   Buff.resize(MaxTasks);
+  Files.resize(MaxTasks);
 
-  checkError(LTOObj->run([&](size_t Task) {
-    return llvm::make_unique<lto::NativeObjectStream>(
-        llvm::make_unique<raw_svector_ostream>(Buff[Task]));
-  }));
+  // The /lldltocache option specifies the path to a directory in which to cache
+  // native object files for ThinLTO incremental builds. If a path was
+  // specified, configure LTO to use it as the cache directory.
+  lto::NativeObjectCache Cache;
+  if (!Config->LTOCache.empty())
+    Cache = check(
+        lto::localCache(Config->LTOCache,
+                        [&](size_t Task, std::unique_ptr<MemoryBuffer> MB,
+                            StringRef Path) { Files[Task] = std::move(MB); }));
+
+  checkError(LTOObj->run(
+      [&](size_t Task) {
+        return llvm::make_unique<lto::NativeObjectStream>(
+            llvm::make_unique<raw_svector_ostream>(Buff[Task]));
+      },
+      Cache));
+
+  if (!Config->LTOCache.empty())
+    pruneCache(Config->LTOCache, Config->LTOCachePolicy);
 
   std::vector<StringRef> Ret;
   for (unsigned I = 0; I != MaxTasks; ++I) {
@@ -136,5 +153,10 @@ std::vector<StringRef> BitcodeCompiler::
     }
     Ret.emplace_back(Buff[I].data(), Buff[I].size());
   }
+
+  for (std::unique_ptr<MemoryBuffer> &File : Files)
+    if (File)
+      Ret.push_back(File->getBuffer());
+
   return Ret;
 }

Modified: lld/trunk/COFF/LTO.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/LTO.h?rev=312770&r1=312769&r2=312770&view=diff
==============================================================================
--- lld/trunk/COFF/LTO.h (original)
+++ lld/trunk/COFF/LTO.h Thu Sep  7 17:50:50 2017
@@ -49,6 +49,7 @@ public:
 private:
   std::unique_ptr<llvm::lto::LTO> LTOObj;
   std::vector<SmallString<0>> Buff;
+  std::vector<std::unique_ptr<MemoryBuffer>> Files;
 };
 }
 }

Modified: lld/trunk/COFF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Options.td?rev=312770&r1=312769&r2=312770&view=diff
==============================================================================
--- lld/trunk/COFF/Options.td (original)
+++ lld/trunk/COFF/Options.td Thu Sep  7 17:50:50 2017
@@ -31,6 +31,8 @@ def heap    : P<"heap", "Size of the hea
 def implib  : P<"implib", "Import library name">;
 def libpath : P<"libpath", "Additional library search path">;
 def linkrepro : P<"linkrepro", "Dump linker invocation and input files for debugging">;
+def lldltocache : P<"lldltocache", "Path to ThinLTO cached object file directory">;
+def lldltocachepolicy : P<"lldltocachepolicy", "Pruning policy for the ThinLTO cache">;
 def lldsavetemps : F<"lldsavetemps">,
     HelpText<"Save temporary files instead of deleting them">;
 def machine : P<"machine", "Specify target platform">;

Added: lld/trunk/test/COFF/Inputs/lto-cache.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/lto-cache.ll?rev=312770&view=auto
==============================================================================
--- lld/trunk/test/COFF/Inputs/lto-cache.ll (added)
+++ lld/trunk/test/COFF/Inputs/lto-cache.ll Thu Sep  7 17:50:50 2017
@@ -0,0 +1,10 @@
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+define i32 @main() {
+entry:
+  call void (...) @globalfunc()
+  ret i32 0
+}
+
+declare void @globalfunc(...)

Added: lld/trunk/test/COFF/lto-cache.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/lto-cache.ll?rev=312770&view=auto
==============================================================================
--- lld/trunk/test/COFF/lto-cache.ll (added)
+++ lld/trunk/test/COFF/lto-cache.ll Thu Sep  7 17:50:50 2017
@@ -0,0 +1,21 @@
+; REQUIRES: x86
+
+; RUN: opt -module-hash -module-summary %s -o %t.o
+; RUN: opt -module-hash -module-summary %p/Inputs/lto-cache.ll -o %t2.o
+
+; RUN: rm -Rf %t.cache && mkdir %t.cache
+; Create two files that would be removed by cache pruning due to age.
+; We should only remove files matching the pattern "llvmcache-*".
+; RUN: touch -t 197001011200 %t.cache/llvmcache-foo %t.cache/foo
+; RUN: lld-link /lldltocache:%t.cache /lldltocachepolicy:prune_after=1h /out:%t3 /entry:main %t2.o %t.o
+
+; Two cached objects, plus a timestamp file and "foo", minus the file we removed.
+; RUN: ls %t.cache | count 4
+
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+define void @globalfunc() #0 {
+entry:
+  ret void
+}




More information about the llvm-commits mailing list