Author: Juan Manuel Martinez CaamaƱo Date: 2026-05-13T09:20:08+02:00 New Revision: d476ab3e85075fb83aef5ef29dafd4f032daddce
URL: https://github.com/llvm/llvm-project/commit/d476ab3e85075fb83aef5ef29dafd4f032daddce DIFF: https://github.com/llvm/llvm-project/commit/d476ab3e85075fb83aef5ef29dafd4f032daddce.diff LOG: [Support][Cache] Make `pruneCache` return an `Expected` (#191367) 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. Added: Modified: lld/COFF/LTO.cpp lld/ELF/LTO.cpp lld/MachO/LTO.cpp lld/wasm/LTO.cpp lldb/source/Core/DataFileCache.cpp llvm/include/llvm/Support/CachePruning.h llvm/lib/Debuginfod/Debuginfod.cpp llvm/lib/LTO/ThinLTOCodeGenerator.cpp llvm/lib/Support/CachePruning.cpp llvm/tools/gold/gold-plugin.cpp Removed: ################################################################################ diff --git a/lld/COFF/LTO.cpp b/lld/COFF/LTO.cpp index 445ea52e995da..aa47ff26c6b4a 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 7b0fe2001439e..27be8859a3f65 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 2c068bf518905..1f82780e65a56 100644 --- a/lld/wasm/LTO.cpp +++ b/lld/wasm/LTO.cpp @@ -250,7 +250,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/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 diff --git a/llvm/include/llvm/Support/CachePruning.h b/llvm/include/llvm/Support/CachePruning.h index a677a684a221a..ffc29f65a854c 100644 --- a/llvm/include/llvm/Support/CachePruning.h +++ b/llvm/include/llvm/Support/CachePruning.h @@ -70,9 +70,11 @@ 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. /// +/// 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,7 +83,7 @@ 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 +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..ac75ade516b2b 100644 --- a/llvm/lib/Debuginfod/Debuginfod.cpp +++ b/llvm/lib/Debuginfod/Debuginfod.cpp @@ -274,7 +274,13 @@ Expected<std::string> getCachedOrDownloadArtifact( parseCachePruningPolicy(std::getenv("DEBUGINFOD_CACHE_POLICY")); if (!PruningPolicyOrErr) return PruningPolicyOrErr.takeError(); - pruneCache(CacheDirectoryPath, *PruningPolicyOrErr); + + Expected<bool> PrunedOrErr = + pruneCache(CacheDirectoryPath, *PruningPolicyOrErr); + // Log the error but continue execution: failure to prune the cache is not + // fatal. + if (!PrunedOrErr) + logAllUnhandledErrors(PrunedOrErr.takeError(), WithColor::warning()); // 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 4bedd610ea8b2..bff6796e57129 100644 --- a/llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ b/llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -1224,7 +1224,12 @@ void ThinLTOCodeGenerator::run() { } } - pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries); + Expected<bool> PrunedOrErr = + pruneCache(CacheOptions.Path, CacheOptions.Policy, ProducedBinaries); + if (!PrunedOrErr) { + errs() << "Error: " << toString(PrunedOrErr.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..9aa3ae15ae23c 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()) @@ -278,13 +279,14 @@ bool 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(); - report_fatal_error(Twine("Can't get available size for '") + Path.str() + - "': " + EC.message()); + 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) 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; _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
