Skip to content

Commit 67c6882

Browse files
committed
[NFC] Refactor FileCache
- Turn it into a type from a function. - Store the cache directory for the future use.
1 parent da550fc commit 67c6882

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

llvm/include/llvm/LTO/LTO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class LTO {
351351
///
352352
/// The client will receive at most one callback (via either AddStream or
353353
/// Cache) for each task identifier.
354-
Error run(AddStreamFn AddStream, FileCache Cache = nullptr);
354+
Error run(AddStreamFn AddStream, FileCache Cache = {});
355355

356356
/// Static method that returns a list of libcall symbols that can be generated
357357
/// by LTO but might not be visible from bitcode symbol table.

llvm/include/llvm/Support/Caching.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,29 @@ using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>(
5454
///
5555
/// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName))
5656
/// ProduceContent(AddStream);
57-
using FileCache = std::function<Expected<AddStreamFn>(
57+
using FileCacheFunction = std::function<Expected<AddStreamFn>(
5858
unsigned Task, StringRef Key, const Twine &ModuleName)>;
5959

60+
struct FileCache {
61+
FileCache(FileCacheFunction CacheFn, const std::string &DirectoryPath)
62+
: CacheFunction(std::move(CacheFn)), CacheDirectoryPath(DirectoryPath) {}
63+
FileCache() = default;
64+
65+
Expected<AddStreamFn> operator()(unsigned Task, StringRef Key,
66+
const Twine &ModuleName) {
67+
assert(isValid() && "Invalid cache function");
68+
return CacheFunction(Task, Key, ModuleName);
69+
}
70+
const std::string &getCacheDirectoryPath() const {
71+
return CacheDirectoryPath;
72+
}
73+
bool isValid() const { return static_cast<bool>(CacheFunction); }
74+
75+
private:
76+
FileCacheFunction CacheFunction = nullptr;
77+
std::string CacheDirectoryPath;
78+
};
79+
6080
/// This type defines the callback to add a pre-existing file (e.g. in a cache).
6181
///
6282
/// Buffer callbacks must be thread safe.

llvm/lib/LTO/LTO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ class InProcessThinBackend : public ThinBackendProc {
14291429
return E;
14301430
}
14311431

1432-
if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1432+
if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
14331433
all_of(CombinedIndex.getModuleHash(ModuleID),
14341434
[](uint32_t V) { return V == 0; }))
14351435
// Cache disabled or no entry for this module in the combined index or

llvm/lib/Support/Caching.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
3737
TempFilePrefixRef.toVector(TempFilePrefix);
3838
CacheDirectoryPathRef.toVector(CacheDirectoryPath);
3939

40-
return [=](unsigned Task, StringRef Key,
41-
const Twine &ModuleName) -> Expected<AddStreamFn> {
40+
auto Func = [=](unsigned Task, StringRef Key,
41+
const Twine &ModuleName) -> Expected<AddStreamFn> {
4242
// This choice of file name allows the cache to be pruned (see pruneCache()
4343
// in include/llvm/Support/CachePruning.h).
4444
SmallString<64> EntryPath;
@@ -167,4 +167,5 @@ Expected<FileCache> llvm::localCache(const Twine &CacheNameRef,
167167
Task);
168168
};
169169
};
170+
return FileCache(Func, CacheDirectoryPathRef.str());
170171
}

0 commit comments

Comments
 (0)