[lld] [llvm] [Support][Cache] Make `pruneCache` return an `Expected` (PR #191367)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 01:56:37 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-platform-windows
@llvm/pr-subscribers-lld
@llvm/pr-subscribers-lld-macho
Author: Juan Manuel Martinez CaamaƱo (jmmartinez)
<details>
<summary>Changes</summary>
When `sys::fs::disk_space` would fail in during a call to `pruneCache`, it would report a `fatal_error`. However, a failure to prune doesn't mean the caller should fail catastrophically.
Downstream, we use LLVM's cache in the OpenCL runtime. A failure to prune the cache can be safely ignored without stopping the user's application.
---
Full diff: https://github.com/llvm/llvm-project/pull/191367.diff
9 Files Affected:
- (modified) lld/COFF/LTO.cpp (+1-1)
- (modified) lld/ELF/LTO.cpp (+2-1)
- (modified) lld/MachO/LTO.cpp (+2-1)
- (modified) lld/wasm/LTO.cpp (+2-1)
- (modified) llvm/include/llvm/Support/CachePruning.h (+3-1)
- (modified) llvm/lib/Debuginfod/Debuginfod.cpp (+4-1)
- (modified) llvm/lib/LTO/ThinLTOCodeGenerator.cpp (+5-1)
- (modified) llvm/lib/Support/CachePruning.cpp (+4-4)
- (modified) llvm/tools/gold/gold-plugin.cpp (+1-1)
``````````diff
diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp
index 2d38a13830dad..74ae7cd1089d4 100644
--- a/lld/COFF/LTO.cpp
+++ b/lld/COFF/LTO.cpp
@@ -241,7 +241,7 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
}
if (!ctx.config.ltoCache.empty())
- pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files);
+ check(pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files));
std::vector<InputFile *> ret;
bool emitASM = ctx.config.emit == EmitKind::ASM;
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 352848af1551d..81414d5f28f8f 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -377,7 +377,8 @@ SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() {
}
if (!ctx.arg.thinLTOCacheDir.empty())
- pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
+ check(
+ pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files));
if (!ctx.arg.ltoObjPath.empty()) {
saveBuffer(buf[0].second, ctx.arg.ltoObjPath);
diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index c8b7a4e797250..c989ce5f41ff5 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -278,7 +278,8 @@ std::vector<ObjFile *> BitcodeCompiler::compile() {
}
if (!config->thinLTOCacheDir.empty())
- pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
+ check(
+ pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files));
std::vector<ObjFile *> ret;
for (unsigned i = 0; i < maxTasks; ++i) {
diff --git a/lld/wasm/LTO.cpp b/lld/wasm/LTO.cpp
index 9fcb95b84b545..073d0125ffd93 100644
--- a/lld/wasm/LTO.cpp
+++ b/lld/wasm/LTO.cpp
@@ -246,7 +246,8 @@ SmallVector<InputFile *, 0> BitcodeCompiler::compile() {
}
if (!ctx.arg.thinLTOCacheDir.empty())
- pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files);
+ check(
+ pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files));
SmallVector<InputFile *, 0> ret;
for (unsigned i = 0; i != maxTasks; ++i) {
diff --git a/llvm/include/llvm/Support/CachePruning.h b/llvm/include/llvm/Support/CachePruning.h
index a677a684a221a..b3b3197c933c0 100644
--- a/llvm/include/llvm/Support/CachePruning.h
+++ b/llvm/include/llvm/Support/CachePruning.h
@@ -81,7 +81,9 @@ parseCachePruningPolicy(StringRef PolicyStr);
/// As a safeguard against data loss if the user specifies the wrong directory
/// as their cache directory, this function will ignore files not matching the
/// pattern "llvmcache-*".
-LLVM_ABI bool
+///
+/// On failure, it returns an Error.
+LLVM_ABI Expected<bool>
pruneCache(StringRef Path, CachePruningPolicy Policy,
const std::vector<std::unique_ptr<MemoryBuffer>> &Files = {});
} // namespace llvm
diff --git a/llvm/lib/Debuginfod/Debuginfod.cpp b/llvm/lib/Debuginfod/Debuginfod.cpp
index 6847dc092dfdb..46950bde9cd01 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -320,7 +320,10 @@ Expected<std::string> getCachedOrDownloadArtifact(
parseCachePruningPolicy(std::getenv("DEBUGINFOD_CACHE_POLICY"));
if (!PruningPolicyOrErr)
return PruningPolicyOrErr.takeError();
- pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
+
+ auto ErrOrPruned = pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
+ if (!ErrOrPruned)
+ return ErrOrPruned.takeError();
// Return the path to the artifact on disk.
return std::string(AbsCachedArtifactPath);
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index 93b672ae7840c..c32db1216bc99 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -1210,7 +1210,11 @@ void ThinLTOCodeGenerator::run() {
}
}
- pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
+ auto ErrOrPruned = pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries));
+ if (!ErrOrPruned) {
+ errs() << "Error: " << toString(ErrOrPruned.takeError()) << "\n";
+ report_fatal_error("ThinLTO: failure to prune cache");
+ }
// If statistics were requested, print them out now.
if (llvm::AreStatisticsEnabled())
diff --git a/llvm/lib/Support/CachePruning.cpp b/llvm/lib/Support/CachePruning.cpp
index 45d4949231fa8..1e8ba94b94ad1 100644
--- a/llvm/lib/Support/CachePruning.cpp
+++ b/llvm/lib/Support/CachePruning.cpp
@@ -142,8 +142,9 @@ llvm::parseCachePruningPolicy(StringRef PolicyStr) {
}
/// Prune the cache of files that haven't been accessed in a long time.
-bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
- const std::vector<std::unique_ptr<MemoryBuffer>> &Files) {
+Expected<bool>
+llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
+ const std::vector<std::unique_ptr<MemoryBuffer>> &Files) {
using namespace std::chrono;
if (Path.empty())
@@ -281,8 +282,7 @@ bool llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
if (!ErrOrSpaceInfo) {
auto EC = ErrOrSpaceInfo.getError();
- report_fatal_error(Twine("Can't get available size for '") + Path.str() +
- "': " + EC.message());
+ return errorCodeToError(EC);
}
sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
auto AvailableSpace = TotalSize + SpaceInfo.free;
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index ba2a1699aaea9..98aafd635651d 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -1220,7 +1220,7 @@ static ld_plugin_status cleanup_hook(void) {
// Prune cache
if (!options::cache_dir.empty()) {
CachePruningPolicy policy = check(parseCachePruningPolicy(options::cache_policy));
- pruneCache(options::cache_dir, policy);
+ check(pruneCache(options::cache_dir, policy));
}
return LDPS_OK;
``````````
</details>
https://github.com/llvm/llvm-project/pull/191367
More information about the llvm-commits
mailing list