Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 1 | //===-Caching.cpp - LLVM Link Time Optimizer Cache Handling ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Caching for ThinLTO. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/LTO/Caching.h" |
| 15 | |
| 16 | #ifdef HAVE_LLVM_REVISION |
| 17 | #include "LLVMLTORevision.h" |
| 18 | #endif |
| 19 | |
| 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | #include "llvm/Support/FileSystem.h" |
| 22 | #include "llvm/Support/Path.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace llvm::lto; |
| 27 | |
| 28 | static void commitEntry(StringRef TempFilename, StringRef EntryPath) { |
| 29 | // Rename to final destination (hopefully race condition won't matter here) |
| 30 | auto EC = sys::fs::rename(TempFilename, EntryPath); |
| 31 | if (EC) { |
| 32 | // Renaming failed, probably not the same filesystem, copy and delete. |
| 33 | { |
| 34 | auto ReloadedBufferOrErr = MemoryBuffer::getFile(TempFilename); |
| 35 | if (auto EC = ReloadedBufferOrErr.getError()) |
| 36 | report_fatal_error(Twine("Failed to open temp file '") + TempFilename + |
| 37 | "': " + EC.message() + "\n"); |
| 38 | |
| 39 | raw_fd_ostream OS(EntryPath, EC, sys::fs::F_None); |
| 40 | if (EC) |
| 41 | report_fatal_error(Twine("Failed to open ") + EntryPath + |
| 42 | " to save cached entry\n"); |
| 43 | // I'm not sure what are the guarantee if two processes are doing this |
| 44 | // at the same time. |
| 45 | OS << (*ReloadedBufferOrErr)->getBuffer(); |
| 46 | } |
| 47 | sys::fs::remove(TempFilename); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | CacheObjectOutput::~CacheObjectOutput() { |
| 52 | if (EntryPath.empty()) |
| 53 | // The entry was never used by the client (tryLoadFromCache() wasn't called) |
| 54 | return; |
| 55 | // TempFilename is only set if getStream() was called, i.e. on cache miss when |
| 56 | // tryLoadFromCache() returned false. And EntryPath is valid if a Key was |
| 57 | // submitted, otherwise it has been set to CacheDirectoryPath in |
| 58 | // tryLoadFromCache. |
| 59 | if (!TempFilename.empty()) { |
| 60 | if (EntryPath == CacheDirectoryPath) |
| 61 | // The Key supplied to tryLoadFromCache was empty, do not commit the temp. |
| 62 | EntryPath = TempFilename; |
| 63 | else |
| 64 | // We commit the tempfile into the cache now, by moving it to EntryPath. |
| 65 | commitEntry(TempFilename, EntryPath); |
| 66 | } |
Teresa Johnson | 26a4628 | 2016-08-26 23:29:14 +0000 | [diff] [blame] | 67 | // Supply the cache path to the user. |
| 68 | AddBuffer(EntryPath.str()); |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Return an allocated stream for the output, or null in case of failure. |
| 72 | std::unique_ptr<raw_pwrite_stream> CacheObjectOutput::getStream() { |
| 73 | assert(!EntryPath.empty() && "API Violation: client didn't call " |
| 74 | "tryLoadFromCache() before getStream()"); |
| 75 | // Write to a temporary to avoid race condition |
| 76 | int TempFD; |
| 77 | std::error_code EC = |
| 78 | sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename); |
| 79 | if (EC) { |
| 80 | errs() << "Error: " << EC.message() << "\n"; |
| 81 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 82 | } |
| 83 | return llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true); |
| 84 | } |
| 85 | |
| 86 | // Try loading from a possible cache first, return true on cache hit. |
| 87 | bool CacheObjectOutput::tryLoadFromCache(StringRef Key) { |
| 88 | assert(!CacheDirectoryPath.empty() && |
| 89 | "CacheObjectOutput was initialized without a cache path"); |
| 90 | if (Key.empty()) { |
| 91 | // Client didn't compute a valid key. EntryPath has been set to |
| 92 | // CacheDirectoryPath. |
| 93 | EntryPath = CacheDirectoryPath; |
| 94 | return false; |
| 95 | } |
| 96 | sys::path::append(EntryPath, CacheDirectoryPath, Key); |
| 97 | return sys::fs::exists(EntryPath); |
| 98 | } |