Skip to content

Commit 9ecb8b5

Browse files
committed
[Support][CachePruning] Disable cache pruning regression fix
borked by: rL284966 (see: https://reviews.llvm.org/D25730). Previously, Interval was unsigned (see: CachePruning.h), replacing the type with std::chrono::seconds (which is signed) causes a regression in behaviour because the c-api intends negative values to translate to large positive intervals to *effectively* disable the pruning (see comments on: setCachePruningInterval()). Differential Revision: https://reviews.llvm.org/D41231 llvm-svn: 321077
1 parent 3feaf2a commit 9ecb8b5

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ class ThinLTOCodeGenerator {
151151
/// Cache policy: interval (seconds) between two prune of the cache. Set to a
152152
/// negative value (default) to disable pruning. A value of 0 will be ignored.
153153
void setCachePruningInterval(int Interval) {
154+
static_assert(std::is_same<decltype(CacheOptions.Policy.Interval),
155+
std::chrono::seconds>::value,
156+
"ensure same types to avoid risk of overflow");
154157
if (Interval)
155-
CacheOptions.Policy.Interval = std::chrono::seconds(Interval);
158+
CacheOptions.Policy.Interval = Interval > 0 ? std::chrono::seconds(Interval)
159+
: std::chrono::seconds::max();
156160
}
157161

158162
/// Cache policy: expiration (in seconds) for an entry.

llvm/lib/Support/CachePruning.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
155155
SmallString<128> TimestampFile(Path);
156156
sys::path::append(TimestampFile, "llvmcache.timestamp");
157157
sys::fs::file_status FileStatus;
158-
const auto CurrentTime = system_clock::now();
158+
const auto CurrentTime =
159+
time_point_cast<decltype(Policy.Interval)>(system_clock::now());
159160
if (auto EC = sys::fs::status(TimestampFile, FileStatus)) {
160161
if (EC == errc::no_such_file_or_directory) {
161162
// If the timestamp file wasn't there, create one now.
@@ -168,7 +169,8 @@ bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy) {
168169
if (Policy.Interval != seconds(0)) {
169170
// Check whether the time stamp is older than our pruning interval.
170171
// If not, do nothing.
171-
const auto TimeStampModTime = FileStatus.getLastModificationTime();
172+
const auto TimeStampModTime = time_point_cast<decltype(Policy.Interval)>(
173+
FileStatus.getLastModificationTime());
172174
auto TimeStampAge = CurrentTime - TimeStampModTime;
173175
if (TimeStampAge <= Policy.Interval) {
174176
DEBUG(dbgs() << "Timestamp file too recent ("

llvm/test/ThinLTO/X86/cache.ll

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
; Verify that enabling caching is ignoring module without hash
77
; RUN: rm -Rf %t.cache && mkdir %t.cache
8-
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache
8+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache
99
; RUN: ls %t.cache/llvmcache.timestamp
1010
; RUN: ls %t.cache | count 1
1111

@@ -27,7 +27,7 @@
2727
; files matching the pattern "llvmcache-*".
2828
; RUN: rm -Rf %t.cache && mkdir %t.cache
2929
; RUN: touch -t 197001011200 %t.cache/llvmcache-foo %t.cache/foo
30-
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache
30+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache
3131
; RUN: ls %t.cache | count 4
3232
; RUN: ls %t.cache/llvmcache.timestamp
3333
; RUN: ls %t.cache/foo
@@ -36,13 +36,29 @@
3636

3737
; Verify that enabling caching is working with llvm-lto2
3838
; RUN: rm -Rf %t.cache
39-
; RUN: llvm-lto2 run -o %t.o %t2.bc %t.bc -cache-dir %t.cache \
39+
; RUN: llvm-lto2 run -o %t.o %t2.bc %t.bc -cache-dir %t.cache \
4040
; RUN: -r=%t2.bc,_main,plx \
4141
; RUN: -r=%t2.bc,_globalfunc,lx \
4242
; RUN: -r=%t.bc,_globalfunc,plx
4343
; RUN: ls %t.cache | count 2
4444
; RUN: ls %t.cache/llvmcache-* | count 2
4545

46+
; Verify that caches with a timestamp older than the pruning interval
47+
; will be pruned
48+
; RUN: rm -Rf %t.cache && mkdir %t.cache
49+
; RUN: touch -t 197001011200 %t.cache/llvmcache-foo
50+
; RUN: touch -t 197001011200 %t.cache/llvmcache.timestamp
51+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache
52+
; RUN: not ls %t.cache/llvmcache-foo
53+
54+
; Verify that specifying a negative number for the pruning interval
55+
; effectively disables the pruning
56+
; RUN: rm -Rf %t.cache && mkdir %t.cache
57+
; RUN: touch -t 197001011200 %t.cache/llvmcache-foo
58+
; RUN: touch -t 197001011200 %t.cache/llvmcache.timestamp
59+
; RUN: llvm-lto -thinlto-action=run -exported-symbol=globalfunc %t2.bc %t.bc -thinlto-cache-dir %t.cache --thinlto-cache-pruning-interval -1
60+
; RUN: ls %t.cache/llvmcache-foo
61+
4662
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
4763
target triple = "x86_64-apple-macosx10.11.0"
4864

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ static cl::opt<std::string> ThinLTOModuleId(
156156
static cl::opt<std::string>
157157
ThinLTOCacheDir("thinlto-cache-dir", cl::desc("Enable ThinLTO caching."));
158158

159+
static cl::opt<int>
160+
ThinLTOCachePruningInterval("thinlto-cache-pruning-interval", cl::desc("Set ThinLTO cache pruning interval."));
161+
159162
static cl::opt<std::string> ThinLTOSaveTempsPrefix(
160163
"thinlto-save-temps",
161164
cl::desc("Save ThinLTO temp files using filenames created by adding "
@@ -470,6 +473,7 @@ class ThinLTOProcessing {
470473
ThinGenerator.setCodePICModel(getRelocModel());
471474
ThinGenerator.setTargetOptions(Options);
472475
ThinGenerator.setCacheDir(ThinLTOCacheDir);
476+
ThinGenerator.setCachePruningInterval(ThinLTOCachePruningInterval);
473477
ThinGenerator.setFreestanding(EnableFreestanding);
474478

475479
// Add all the exported symbols to the table of symbols to preserve.

0 commit comments

Comments
 (0)