diff options
Diffstat (limited to 'contrib/llvm/lib/LTO/Caching.cpp')
-rw-r--r-- | contrib/llvm/lib/LTO/Caching.cpp | 82 |
1 files changed, 42 insertions, 40 deletions
diff --git a/contrib/llvm/lib/LTO/Caching.cpp b/contrib/llvm/lib/LTO/Caching.cpp index fd5bdb0..e32e46c 100644 --- a/contrib/llvm/lib/LTO/Caching.cpp +++ b/contrib/llvm/lib/LTO/Caching.cpp @@ -13,6 +13,7 @@ #include "llvm/LTO/Caching.h" #include "llvm/ADT/StringExtras.h" +#include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -21,70 +22,71 @@ using namespace llvm; using namespace llvm::lto; -static void commitEntry(StringRef TempFilename, StringRef EntryPath) { - // Rename to final destination (hopefully race condition won't matter here) - auto EC = sys::fs::rename(TempFilename, EntryPath); - if (EC) { - // Renaming failed, probably not the same filesystem, copy and delete. - // FIXME: Avoid needing to do this by creating the temporary file in the - // cache directory. - { - auto ReloadedBufferOrErr = MemoryBuffer::getFile(TempFilename); - if (auto EC = ReloadedBufferOrErr.getError()) - report_fatal_error(Twine("Failed to open temp file '") + TempFilename + - "': " + EC.message() + "\n"); +Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, + AddBufferFn AddBuffer) { + if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) + return errorCodeToError(EC); - raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None); - if (EC) - report_fatal_error(Twine("Failed to open ") + EntryPath + - " to save cached entry\n"); - // I'm not sure what are the guarantee if two processes are doing this - // at the same time. - OS << (*ReloadedBufferOrErr)->getBuffer(); - } - sys::fs::remove(TempFilename); - } -} - -NativeObjectCache lto::localCache(std::string CacheDirectoryPath, - AddFileFn AddFile) { return [=](unsigned Task, StringRef Key) -> AddStreamFn { - // First, see if we have a cache hit. + // This choice of file name allows the cache to be pruned (see pruneCache() + // in include/llvm/Support/CachePruning.h). SmallString<64> EntryPath; - sys::path::append(EntryPath, CacheDirectoryPath, Key); - if (sys::fs::exists(EntryPath)) { - AddFile(Task, EntryPath); + sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key); + // First, see if we have a cache hit. + ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = + MemoryBuffer::getFile(EntryPath); + if (MBOrErr) { + AddBuffer(Task, std::move(*MBOrErr)); return AddStreamFn(); } + if (MBOrErr.getError() != errc::no_such_file_or_directory) + report_fatal_error(Twine("Failed to open cache file ") + EntryPath + + ": " + MBOrErr.getError().message() + "\n"); + // This native object stream is responsible for commiting the resulting - // file to the cache and calling AddFile to add it to the link. + // file to the cache and calling AddBuffer to add it to the link. struct CacheStream : NativeObjectStream { - AddFileFn AddFile; + AddBufferFn AddBuffer; std::string TempFilename; std::string EntryPath; unsigned Task; - CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddFileFn AddFile, + CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer, std::string TempFilename, std::string EntryPath, unsigned Task) - : NativeObjectStream(std::move(OS)), AddFile(AddFile), - TempFilename(TempFilename), EntryPath(EntryPath), Task(Task) {} + : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)), + TempFilename(std::move(TempFilename)), + EntryPath(std::move(EntryPath)), Task(Task) {} ~CacheStream() { + // FIXME: This code could race with the cache pruner, but it is unlikely + // that the cache pruner will choose to remove a newly created file. + // Make sure the file is closed before committing it. OS.reset(); - commitEntry(TempFilename, EntryPath); - AddFile(Task, EntryPath); + // This is atomic on POSIX systems. + if (auto EC = sys::fs::rename(TempFilename, EntryPath)) + report_fatal_error(Twine("Failed to rename temporary file ") + + TempFilename + ": " + EC.message() + "\n"); + + ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = + MemoryBuffer::getFile(EntryPath); + if (!MBOrErr) + report_fatal_error(Twine("Failed to open cache file ") + EntryPath + + ": " + MBOrErr.getError().message() + "\n"); + AddBuffer(Task, std::move(*MBOrErr)); } }; return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { // Write to a temporary to avoid race condition int TempFD; - SmallString<64> TempFilename; + SmallString<64> TempFilenameModel, TempFilename; + sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); std::error_code EC = - sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename); + sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename, + sys::fs::owner_read | sys::fs::owner_write); if (EC) { errs() << "Error: " << EC.message() << "\n"; report_fatal_error("ThinLTO: Can't get a temporary file"); @@ -93,7 +95,7 @@ NativeObjectCache lto::localCache(std::string CacheDirectoryPath, // This CacheStream will move the temporary file into the cache when done. return llvm::make_unique<CacheStream>( llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true), - AddFile, TempFilename.str(), EntryPath.str(), Task); + AddBuffer, TempFilename.str(), EntryPath.str(), Task); }; }; } |