Index: include/llvm/LTO/legacy/ThinLTOCodeGenerator.h =================================================================== --- include/llvm/LTO/legacy/ThinLTOCodeGenerator.h +++ include/llvm/LTO/legacy/ThinLTOCodeGenerator.h @@ -151,8 +151,12 @@ /// Cache policy: interval (seconds) between two prune of the cache. Set to a /// negative value (default) to disable pruning. A value of 0 will be ignored. void setCachePruningInterval(int Interval) { - if (Interval) - CacheOptions.Policy.Interval = std::chrono::seconds(Interval); + static_assert(std::is_same::value, + "ensure same types to avoid risk of overflow"); + if (Interval) + CacheOptions.Policy.Interval = Interval > 0 ? std::chrono::seconds(Interval) + : std::chrono::seconds::max(); } /// Cache policy: expiration (in seconds) for an entry. Index: lib/Support/CachePruning.cpp =================================================================== --- lib/Support/CachePruning.cpp +++ lib/Support/CachePruning.cpp @@ -169,7 +169,8 @@ // Check whether the time stamp is older than our pruning interval. // If not, do nothing. const auto TimeStampModTime = FileStatus.getLastModificationTime(); - auto TimeStampAge = CurrentTime - TimeStampModTime; + auto TimeStampAge = + duration_cast(CurrentTime - TimeStampModTime); if (TimeStampAge <= Policy.Interval) { DEBUG(dbgs() << "Timestamp file too recent (" << duration_cast(TimeStampAge).count() Index: test/ThinLTO/X86/cache.ll =================================================================== --- test/ThinLTO/X86/cache.ll +++ test/ThinLTO/X86/cache.ll @@ -5,7 +5,7 @@ ; Verify that enabling caching is ignoring module without hash ; RUN: rm -Rf %t.cache && mkdir %t.cache -; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache ; RUN: ls %t.cache/llvmcache.timestamp ; RUN: ls %t.cache | count 1 @@ -27,7 +27,7 @@ ; files matching the pattern "llvmcache-*". ; RUN: rm -Rf %t.cache && mkdir %t.cache ; RUN: touch -t 197001011200 %t.cache/llvmcache-foo %t.cache/foo -; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache ; RUN: ls %t.cache | count 4 ; RUN: ls %t.cache/llvmcache.timestamp ; RUN: ls %t.cache/foo @@ -36,13 +36,29 @@ ; Verify that enabling caching is working with llvm-lto2 ; RUN: rm -Rf %t.cache -; RUN: llvm-lto2 run -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ +; RUN: llvm-lto2 run -o %t.o %t2.bc %t.bc -cache-dir %t.cache \ ; RUN: -r=%t2.bc,_main,plx \ ; RUN: -r=%t2.bc,_globalfunc,lx \ ; RUN: -r=%t.bc,_globalfunc,plx ; RUN: ls %t.cache | count 2 ; RUN: ls %t.cache/llvmcache-* | count 2 +; Verify that caches with a timestamp older than the pruning interval +; will be pruned +; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: touch -t 197001011200 %t.cache/llvmcache-foo +; RUN: touch -t 197001011200 %t.cache/llvmcache.timestamp +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache +; RUN: not ls %t.cache/llvmcache-foo + +; Verify that specifying a negative number for the pruning interval +; effectively disables the pruning +; RUN: rm -Rf %t.cache && mkdir %t.cache +; RUN: touch -t 197001011200 %t.cache/llvmcache-foo +; RUN: touch -t 197001011200 %t.cache/llvmcache.timestamp +; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-pruning-interval -1 +; RUN: ls %t.cache/llvmcache-foo + target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" Index: tools/llvm-lto/llvm-lto.cpp =================================================================== --- tools/llvm-lto/llvm-lto.cpp +++ tools/llvm-lto/llvm-lto.cpp @@ -156,6 +156,9 @@ static cl::opt ThinLTOCacheDir("thinlto-cache-dir", cl::desc("Enable ThinLTO caching.")); +static cl::opt + ThinLTOCachePruningInterval("thinlto-cache-pruning-interval", cl::desc("Set ThinLTO cache pruning interval.")); + static cl::opt ThinLTOSaveTempsPrefix( "thinlto-save-temps", cl::desc("Save ThinLTO temp files using filenames created by adding " @@ -470,6 +473,7 @@ ThinGenerator.setCodePICModel(getRelocModel()); ThinGenerator.setTargetOptions(Options); ThinGenerator.setCacheDir(ThinLTOCacheDir); + ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval); ThinGenerator.setFreestanding(EnableFreestanding); // Add all the exported symbols to the table of symbols to preserve. Index: tools/lto/lto.cpp =================================================================== --- tools/lto/lto.cpp +++ tools/lto/lto.cpp @@ -573,7 +573,7 @@ void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg, int interval) { - return unwrap(cg)->setCachePruningInterval(interval); + return unwrap(cg)->setCachePruningInterval(interval ); } void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,