Skip to content

Commit ed59d57

Browse files
authored
[ThinLTO][NFC] Refactor FileCache (llvm#110463)
This is a prep for llvm#90933. - Change `FileCache` from a function to a type. - Store the cache directory in the type, which will be used when creating additional caches for two-codegen round runs that inherit this value.
1 parent dada3c3 commit ed59d57

File tree

4 files changed

+42
-14
lines changed

4 files changed

+42
-14
lines changed

llvm/include/llvm/LTO/LTO.h

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

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

llvm/include/llvm/Support/Caching.h

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,48 @@ class CachedFileStream {
4141
using AddStreamFn = std::function<Expected<std::unique_ptr<CachedFileStream>>(
4242
unsigned Task, const Twine &ModuleName)>;
4343

44-
/// This is the type of a file cache. To request an item from the cache, pass a
45-
/// unique string as the Key. For hits, the cached file will be added to the
46-
/// link and this function will return AddStreamFn(). For misses, the cache will
47-
/// return a stream callback which must be called at most once to produce
48-
/// content for the stream. The file stream produced by the stream callback will
49-
/// add the file to the link after the stream is written to. ModuleName is the
50-
/// unique module identifier for the bitcode module the cache is being checked
51-
/// for.
44+
/// This is a callable that manages file caching operations. It accepts a task
45+
/// ID \p Task, a unique key \p Key, and a module name \p ModuleName, and
46+
/// returns AddStreamFn(). This function determines whether a cache hit or miss
47+
/// occurs and handles the appropriate actions.
48+
using FileCacheFunction = std::function<Expected<AddStreamFn>(
49+
unsigned Task, StringRef Key, const Twine &ModuleName)>;
50+
51+
/// This type represents a file cache system that manages caching of files.
52+
/// It encapsulates a caching function and the directory path where the cache is
53+
/// stored. To request an item from the cache, pass a unique string as the Key.
54+
/// For hits, the cached file will be added to the link and this function will
55+
/// return AddStreamFn(). For misses, the cache will return a stream callback
56+
/// which must be called at most once to produce content for the stream. The
57+
/// file stream produced by the stream callback will add the file to the link
58+
/// after the stream is written to. ModuleName is the unique module identifier
59+
/// for the bitcode module the cache is being checked for.
5260
///
5361
/// Clients generally look like this:
5462
///
5563
/// if (AddStreamFn AddStream = Cache(Task, Key, ModuleName))
5664
/// ProduceContent(AddStream);
57-
using FileCache = std::function<Expected<AddStreamFn>(
58-
unsigned Task, StringRef Key, const Twine &ModuleName)>;
65+
///
66+
/// CacheDirectoryPath stores the directory path where cached files are kept.
67+
struct FileCache {
68+
FileCache(FileCacheFunction CacheFn, const std::string &DirectoryPath)
69+
: CacheFunction(std::move(CacheFn)), CacheDirectoryPath(DirectoryPath) {}
70+
FileCache() = default;
71+
72+
Expected<AddStreamFn> operator()(unsigned Task, StringRef Key,
73+
const Twine &ModuleName) {
74+
assert(isValid() && "Invalid cache function");
75+
return CacheFunction(Task, Key, ModuleName);
76+
}
77+
const std::string &getCacheDirectoryPath() const {
78+
return CacheDirectoryPath;
79+
}
80+
bool isValid() const { return static_cast<bool>(CacheFunction); }
81+
82+
private:
83+
FileCacheFunction CacheFunction = nullptr;
84+
std::string CacheDirectoryPath;
85+
};
5986

6087
/// This type defines the callback to add a pre-existing file (e.g. in a cache).
6188
///

llvm/lib/LTO/LTO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ class InProcessThinBackend : public ThinBackendProc {
14841484
return E;
14851485
}
14861486

1487-
if (!Cache || !CombinedIndex.modulePaths().count(ModuleID) ||
1487+
if (!Cache.isValid() || !CombinedIndex.modulePaths().count(ModuleID) ||
14881488
all_of(CombinedIndex.getModuleHash(ModuleID),
14891489
[](uint32_t V) { return V == 0; }))
14901490
// 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)