blob: 118efde6a1fee33948b001c053b41437de00d063 [file] [log] [blame]
Mehdi Aminiadc0e262016-08-23 21:30:12 +00001//===-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
25using namespace llvm;
26using namespace llvm::lto;
27
28static 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
51CacheObjectOutput::~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 }
67 // Load the entry from the cache now.
68 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
69 if (auto EC = ReloadedBufferOrErr.getError())
70 report_fatal_error(Twine("Can't reload cached file '") + EntryPath + "': " +
71 EC.message() + "\n");
72
73 // Supply the resulting buffer to the user.
74 AddBuffer(std::move(*ReloadedBufferOrErr));
75}
76
77// Return an allocated stream for the output, or null in case of failure.
78std::unique_ptr<raw_pwrite_stream> CacheObjectOutput::getStream() {
79 assert(!EntryPath.empty() && "API Violation: client didn't call "
80 "tryLoadFromCache() before getStream()");
81 // Write to a temporary to avoid race condition
82 int TempFD;
83 std::error_code EC =
84 sys::fs::createTemporaryFile("Thin", "tmp.o", TempFD, TempFilename);
85 if (EC) {
86 errs() << "Error: " << EC.message() << "\n";
87 report_fatal_error("ThinLTO: Can't get a temporary file");
88 }
89 return llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true);
90}
91
92// Try loading from a possible cache first, return true on cache hit.
93bool CacheObjectOutput::tryLoadFromCache(StringRef Key) {
94 assert(!CacheDirectoryPath.empty() &&
95 "CacheObjectOutput was initialized without a cache path");
96 if (Key.empty()) {
97 // Client didn't compute a valid key. EntryPath has been set to
98 // CacheDirectoryPath.
99 EntryPath = CacheDirectoryPath;
100 return false;
101 }
102 sys::path::append(EntryPath, CacheDirectoryPath, Key);
103 return sys::fs::exists(EntryPath);
104}