Index: lld/trunk/ELF/Config.h =================================================================== --- lld/trunk/ELF/Config.h +++ lld/trunk/ELF/Config.h @@ -13,6 +13,7 @@ #include "llvm/ADT/MapVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/Support/CachePruning.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/ELF.h" @@ -72,6 +73,7 @@ struct Configuration { InputFile *FirstElf = nullptr; uint8_t OSABI = 0; + llvm::CachePruningPolicy ThinLTOCachePolicy; llvm::StringMap SectionStartMap; llvm::StringRef DynamicLinker; llvm::StringRef Entry; Index: lld/trunk/ELF/Driver.cpp =================================================================== --- lld/trunk/ELF/Driver.cpp +++ lld/trunk/ELF/Driver.cpp @@ -585,6 +585,9 @@ Config->Target1Rel = getArg(Args, OPT_target1_rel, OPT_target1_abs, false); Config->Target2 = getTarget2(Args); Config->ThinLTOCacheDir = getString(Args, OPT_thinlto_cache_dir); + Config->ThinLTOCachePolicy = + check(parseCachePruningPolicy(getString(Args, OPT_thinlto_cache_policy)), + "--thinlto-cache-policy: invalid cache policy"); Config->ThinLTOJobs = getInteger(Args, OPT_thinlto_jobs, -1u); Config->Threads = getArg(Args, OPT_threads, OPT_no_threads, true); Config->Trace = Args.hasArg(OPT_trace); Index: lld/trunk/ELF/LTO.cpp =================================================================== --- lld/trunk/ELF/LTO.cpp +++ lld/trunk/ELF/LTO.cpp @@ -163,6 +163,9 @@ }, Cache)); + if (!Config->ThinLTOCacheDir.empty()) + pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); + for (unsigned I = 0; I != MaxTasks; ++I) { if (Buff[I].empty()) continue; Index: lld/trunk/ELF/Options.td =================================================================== --- lld/trunk/ELF/Options.td +++ lld/trunk/ELF/Options.td @@ -394,4 +394,6 @@ def save_temps: F<"save-temps">; def thinlto_cache_dir: J<"thinlto-cache-dir=">, HelpText<"Path to ThinLTO cached object file directory">; +def thinlto_cache_policy: S<"thinlto-cache-policy">, + HelpText<"Pruning policy for the ThinLTO cache">; def thinlto_jobs: J<"thinlto-jobs=">, HelpText<"Number of ThinLTO jobs">; Index: lld/trunk/test/ELF/lto/cache.ll =================================================================== --- lld/trunk/test/ELF/lto/cache.ll +++ lld/trunk/test/ELF/lto/cache.ll @@ -3,10 +3,13 @@ ; RUN: opt -module-hash -module-summary %s -o %t.o ; RUN: opt -module-hash -module-summary %p/Inputs/cache.ll -o %t2.o -; RUN: rm -Rf %t.cache -; RUN: ld.lld --thinlto-cache-dir=%t.cache -o %t3 %t2.o %t.o +; RUN: rm -Rf %t.cache && mkdir %t.cache +; Create a file that will be removed by cache pruning due to age. +; RUN: touch -d 1970-01-01 %t.cache/foo +; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy prune_after=1h -o %t3 %t2.o %t.o -; RUN: ls %t.cache | count 2 +; Two cached objects, plus a timestamp file, minus the file we removed. +; RUN: ls %t.cache | count 3 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"