[Lldb-commits] [lld] [lldb] [llvm] [Support][Cache] Make `pruneCache` return an `Expected` (PR #191367)
Juan Manuel Martinez CaamaƱo via lldb-commits
lldb-commits at lists.llvm.org
Thu Apr 16 02:12:09 PDT 2026
https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/191367
>From fa12e21583bbdfbce2b224465413600e4c2c4904 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 10 Apr 2026 10:25:09 +0200
Subject: [PATCH 1/4] [Support][Cache] Make `pruneCache` return an `Expected`
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.
---
lld/COFF/LTO.cpp | 2 +-
lld/ELF/LTO.cpp | 3 ++-
lld/MachO/LTO.cpp | 3 ++-
lld/wasm/LTO.cpp | 3 ++-
llvm/include/llvm/Support/CachePruning.h | 4 +++-
llvm/lib/Debuginfod/Debuginfod.cpp | 5 ++++-
llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 6 +++++-
llvm/lib/Support/CachePruning.cpp | 8 ++++----
llvm/tools/gold/gold-plugin.cpp | 2 +-
9 files changed, 24 insertions(+), 12 deletions(-)
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;
>From d25239feb6d3cf5f011dcebf3785842f70b8c9b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 10 Apr 2026 11:07:06 +0200
Subject: [PATCH 2/4] Missing LLDB side
---
lldb/source/Core/DataFileCache.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/lldb/source/Core/DataFileCache.cpp b/lldb/source/Core/DataFileCache.cpp
index 9109269711231..c35ee372a2e0a 100644
--- a/lldb/source/Core/DataFileCache.cpp
+++ b/lldb/source/Core/DataFileCache.cpp
@@ -46,7 +46,12 @@ llvm::CachePruningPolicy DataFileCache::GetLLDBIndexCachePolicy() {
DataFileCache::DataFileCache(llvm::StringRef path, llvm::CachePruningPolicy policy) {
m_cache_dir.SetPath(path);
- pruneCache(path, policy);
+ llvm::Expected<bool> err_or_pruned = pruneCache(path, policy);
+ if (!err_or_pruned) {
+ Log *log = GetLog(LLDBLog::Modules);
+ LLDB_LOG_ERROR(log, err_or_pruned.takeError(),
+ "failed to prune lldb index cache directory: {0}");
+ }
// This lambda will get called when the data is gotten from the cache and
// also after the data was set for a given key. We only need to take
>From 0a121bb11e82ae526dbd50b24179a8000f7203a2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 10 Apr 2026 11:08:53 +0200
Subject: [PATCH 3/4] Extra )
---
llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index c32db1216bc99..48c19863470cd 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -1210,7 +1210,8 @@ void ThinLTOCodeGenerator::run() {
}
}
- auto ErrOrPruned = 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");
>From 888cc1602a236b26595fe58696a07d9b70378515 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Thu, 16 Apr 2026 11:11:25 +0200
Subject: [PATCH 4/4] Review: no-auto, use Expected<bool>
---
llvm/lib/Debuginfod/Debuginfod.cpp | 3 ++-
llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Debuginfod/Debuginfod.cpp b/llvm/lib/Debuginfod/Debuginfod.cpp
index 46950bde9cd01..f811e4de9168c 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -321,7 +321,8 @@ Expected<std::string> getCachedOrDownloadArtifact(
if (!PruningPolicyOrErr)
return PruningPolicyOrErr.takeError();
- auto ErrOrPruned = pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
+ Expected<bool> ErrOrPruned =
+ pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
if (!ErrOrPruned)
return ErrOrPruned.takeError();
diff --git a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
index 48c19863470cd..bf16367045c4e 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -1210,7 +1210,7 @@ void ThinLTOCodeGenerator::run() {
}
}
- auto ErrOrPruned =
+ Expected<bool> ErrOrPruned =
pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
if (!ErrOrPruned) {
errs() << "Error: " << toString(ErrOrPruned.takeError()) << "\n";
More information about the lldb-commits
mailing list