[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
Tue May 12 01:34:50 PDT 2026
https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/191367
>From 1a676754243d72b215456e32fadde03f8797c064 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/9] [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 84fe7ac06336a..e91bf2fc9c9d3 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -274,7 +274,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 f8c660fbccb803b818830efa9e2794f71b38f4e8 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/9] 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 6ec142fd1afc20a1ec06b0ea3e9dd55c4f81f187 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/9] 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 666c5681b7ce8d806b0a6a0650b6595e2c707eac 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/9] 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 e91bf2fc9c9d3..1c53d580a9672 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -275,7 +275,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";
>From 2f0b934326705df49363790b182879a10e783905 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 17 Apr 2026 11:18:48 +0200
Subject: [PATCH 5/9] Review: typo Peform->Perform
---
llvm/include/llvm/Support/CachePruning.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/CachePruning.h b/llvm/include/llvm/Support/CachePruning.h
index b3b3197c933c0..36155b12679d0 100644
--- a/llvm/include/llvm/Support/CachePruning.h
+++ b/llvm/include/llvm/Support/CachePruning.h
@@ -70,7 +70,7 @@ struct CachePruningPolicy {
LLVM_ABI Expected<CachePruningPolicy>
parseCachePruningPolicy(StringRef PolicyStr);
-/// Peform pruning using the supplied policy, returns true if pruning
+/// Perform pruning using the supplied policy, returns true if pruning
/// occurred, i.e. if Policy.Interval was expired.
///
/// Check whether cache pruning happens using the supplied policy, adds a
>From f418a7b09b51e4fab15769359342b936e146ec32 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 17 Apr 2026 11:19:45 +0200
Subject: [PATCH 6/9] Review: ErrOrPruned->PrunedOrErr
---
llvm/lib/Debuginfod/Debuginfod.cpp | 6 +++---
llvm/lib/LTO/ThinLTOCodeGenerator.cpp | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Debuginfod/Debuginfod.cpp b/llvm/lib/Debuginfod/Debuginfod.cpp
index 1c53d580a9672..284db473a6d8b 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -275,10 +275,10 @@ Expected<std::string> getCachedOrDownloadArtifact(
if (!PruningPolicyOrErr)
return PruningPolicyOrErr.takeError();
- Expected<bool> ErrOrPruned =
+ Expected<bool> PrunedOrErr =
pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
- if (!ErrOrPruned)
- return ErrOrPruned.takeError();
+ if (!PrunedOrErr)
+ return PrunedOrErr.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 bf16367045c4e..1da4fbffa3607 100644
--- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
+++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp
@@ -1210,10 +1210,10 @@ void ThinLTOCodeGenerator::run() {
}
}
- Expected<bool> ErrOrPruned =
+ Expected<bool> PrunedOrErr =
pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries);
- if (!ErrOrPruned) {
- errs() << "Error: " << toString(ErrOrPruned.takeError()) << "\n";
+ if (!PrunedOrErr) {
+ errs() << "Error: " << toString(PrunedOrErr.takeError()) << "\n";
report_fatal_error("ThinLTO: failure to prune cache");
}
>From 695a2ccd9559e644ca65e06f125049daf48ecc3c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 17 Apr 2026 11:25:30 +0200
Subject: [PATCH 7/9] Review: More verbose error on disk space failure
---
llvm/lib/Support/CachePruning.cpp | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Support/CachePruning.cpp b/llvm/lib/Support/CachePruning.cpp
index 1e8ba94b94ad1..9aa3ae15ae23c 100644
--- a/llvm/lib/Support/CachePruning.cpp
+++ b/llvm/lib/Support/CachePruning.cpp
@@ -279,12 +279,14 @@ llvm::pruneCache(StringRef Path, CachePruningPolicy Policy,
// Prune for size now if needed
if (Policy.MaxSizePercentageOfAvailableSpace > 0 || Policy.MaxSizeBytes > 0) {
- auto ErrOrSpaceInfo = sys::fs::disk_space(Path);
- if (!ErrOrSpaceInfo) {
- auto EC = ErrOrSpaceInfo.getError();
- return errorCodeToError(EC);
+ auto SpaceInfoOrErr = sys::fs::disk_space(Path);
+ if (!SpaceInfoOrErr) {
+ auto EC = SpaceInfoOrErr.getError();
+ return createStringError(EC,
+ "cannot get available disk space for '%s': '%s'",
+ Path.str().c_str(), EC.message().c_str());
}
- sys::fs::space_info SpaceInfo = ErrOrSpaceInfo.get();
+ sys::fs::space_info SpaceInfo = SpaceInfoOrErr.get();
auto AvailableSpace = TotalSize + SpaceInfo.free;
if (Policy.MaxSizePercentageOfAvailableSpace == 0)
>From c80aca56b2fa6bb1ba26f0cd23105844ff50be14 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 17 Apr 2026 11:34:21 +0200
Subject: [PATCH 8/9] Review: debuginfod, warn but not fail
---
llvm/lib/Debuginfod/Debuginfod.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Debuginfod/Debuginfod.cpp b/llvm/lib/Debuginfod/Debuginfod.cpp
index 284db473a6d8b..ac75ade516b2b 100644
--- a/llvm/lib/Debuginfod/Debuginfod.cpp
+++ b/llvm/lib/Debuginfod/Debuginfod.cpp
@@ -277,8 +277,10 @@ Expected<std::string> getCachedOrDownloadArtifact(
Expected<bool> PrunedOrErr =
pruneCache(CacheDirectoryPath, *PruningPolicyOrErr);
+ // Log the error but continue execution: failure to prune the cache is not
+ // fatal.
if (!PrunedOrErr)
- return PrunedOrErr.takeError();
+ logAllUnhandledErrors(PrunedOrErr.takeError(), WithColor::warning());
// Return the path to the artifact on disk.
return std::string(AbsCachedArtifactPath);
>From 90cb394a51852c00f62917be50b2639ddd9e048b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
<jmartinezcaamao at gmail.com>
Date: Fri, 17 Apr 2026 11:39:28 +0200
Subject: [PATCH 9/9] Review: move the on failure line
---
llvm/include/llvm/Support/CachePruning.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Support/CachePruning.h b/llvm/include/llvm/Support/CachePruning.h
index 36155b12679d0..ffc29f65a854c 100644
--- a/llvm/include/llvm/Support/CachePruning.h
+++ b/llvm/include/llvm/Support/CachePruning.h
@@ -73,6 +73,8 @@ parseCachePruningPolicy(StringRef PolicyStr);
/// Perform pruning using the supplied policy, returns true if pruning
/// occurred, i.e. if Policy.Interval was expired.
///
+/// On failure, it returns an Expected with the Error.
+///
/// Check whether cache pruning happens using the supplied policy, adds a
/// ThinLTO warning if cache_size_bytes or cache_size_files is too small for the
/// current link job. The warning recommends the user to consider adjusting
@@ -81,8 +83,6 @@ 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-*".
-///
-/// On failure, it returns an Error.
LLVM_ABI Expected<bool>
pruneCache(StringRef Path, CachePruningPolicy Policy,
const std::vector<std::unique_ptr<MemoryBuffer>> &Files = {});
More information about the lldb-commits
mailing list