blob: bd6190d996132efb326859b2767ecc64d46f9bec [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"
Mehdi Aminiadc0e262016-08-23 21:30:12 +000015#include "llvm/ADT/StringExtras.h"
Peter Collingbourned7860472017-03-20 18:19:41 +000016#include "llvm/Support/Errc.h"
Peter Collingbourne80186a52016-09-23 21:33:43 +000017#include "llvm/Support/MemoryBuffer.h"
Mehdi Aminiadc0e262016-08-23 21:30:12 +000018#include "llvm/Support/Path.h"
Bob Haarmana17bd1212017-11-10 17:08:21 +000019#include "llvm/Support/Process.h"
Mehdi Aminiadc0e262016-08-23 21:30:12 +000020#include "llvm/Support/raw_ostream.h"
21
22using namespace llvm;
23using namespace llvm::lto;
24
Peter Collingbourneab76a192017-03-02 02:02:38 +000025Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
Peter Collingbourne128423f2017-03-17 00:34:07 +000026 AddBufferFn AddBuffer) {
Peter Collingbourneab76a192017-03-02 02:02:38 +000027 if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
28 return errorCodeToError(EC);
29
Peter Collingbourne80186a52016-09-23 21:33:43 +000030 return [=](unsigned Task, StringRef Key) -> AddStreamFn {
Peter Collingbourne25a17ba2017-03-20 16:41:57 +000031 // This choice of file name allows the cache to be pruned (see pruneCache()
32 // in include/llvm/Support/CachePruning.h).
Peter Collingbourne80186a52016-09-23 21:33:43 +000033 SmallString<64> EntryPath;
Peter Collingbourne25a17ba2017-03-20 16:41:57 +000034 sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
35 // First, see if we have a cache hit.
Peter Collingbourne128423f2017-03-17 00:34:07 +000036 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
37 MemoryBuffer::getFile(EntryPath);
38 if (MBOrErr) {
Teresa Johnsona344fd32018-02-20 20:21:53 +000039 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +000040 return AddStreamFn();
41 }
Mehdi Aminiadc0e262016-08-23 21:30:12 +000042
Peter Collingbourne881ba102018-06-13 18:03:14 +000043 // On Windows we can fail to open a cache file with a permission denied
44 // error. This generally means that another process has requested to delete
45 // the file while it is still open, but it could also mean that another
46 // process has opened the file without the sharing permissions we need.
47 // Since the file is probably being deleted we handle it in the same way as
48 // if the file did not exist at all.
49 if (MBOrErr.getError() != errc::no_such_file_or_directory &&
50 MBOrErr.getError() != errc::permission_denied)
Peter Collingbourne128423f2017-03-17 00:34:07 +000051 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
52 ": " + MBOrErr.getError().message() + "\n");
53
Peter Collingbourne80186a52016-09-23 21:33:43 +000054 // This native object stream is responsible for commiting the resulting
Peter Collingbourne128423f2017-03-17 00:34:07 +000055 // file to the cache and calling AddBuffer to add it to the link.
Peter Collingbourne80186a52016-09-23 21:33:43 +000056 struct CacheStream : NativeObjectStream {
Peter Collingbourne128423f2017-03-17 00:34:07 +000057 AddBufferFn AddBuffer;
Rafael Espindolaa17fca02017-11-15 19:09:22 +000058 sys::fs::TempFile TempFile;
Peter Collingbourne80186a52016-09-23 21:33:43 +000059 std::string EntryPath;
60 unsigned Task;
Mehdi Aminiadc0e262016-08-23 21:30:12 +000061
Peter Collingbourne128423f2017-03-17 00:34:07 +000062 CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
Rafael Espindolaa17fca02017-11-15 19:09:22 +000063 sys::fs::TempFile TempFile, std::string EntryPath,
Peter Collingbourne80186a52016-09-23 21:33:43 +000064 unsigned Task)
Peter Collingbourne128423f2017-03-17 00:34:07 +000065 : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
Rafael Espindolaa17fca02017-11-15 19:09:22 +000066 TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)),
67 Task(Task) {}
Peter Collingbourne80186a52016-09-23 21:33:43 +000068
69 ~CacheStream() {
Rafael Espindolaa17fca02017-11-15 19:09:22 +000070 // Make sure the stream is closed before committing it.
Peter Collingbourne80186a52016-09-23 21:33:43 +000071 OS.reset();
Peter Collingbourned0e9c162017-09-05 19:51:38 +000072
Peter Collingbourned0e9c162017-09-05 19:51:38 +000073 // Open the file first to avoid racing with a cache pruner.
74 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
Rafael Espindolaa17fca02017-11-15 19:09:22 +000075 MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName,
76 /*FileSize*/ -1,
77 /*RequiresNullTerminator*/ false);
Peter Collingbourned5fc37c2017-10-03 00:44:21 +000078 if (!MBOrErr)
79 report_fatal_error(Twine("Failed to open new cache file ") +
Rafael Espindolaa17fca02017-11-15 19:09:22 +000080 TempFile.TmpName + ": " +
Peter Collingbourned5fc37c2017-10-03 00:44:21 +000081 MBOrErr.getError().message() + "\n");
Peter Collingbourned0e9c162017-09-05 19:51:38 +000082
Bob Haarman847a77f2017-11-16 01:16:52 +000083 // On POSIX systems, this will atomically replace the destination if
84 // it already exists. We try to emulate this on Windows, but this may
85 // fail with a permission denied error (for example, if the destination
86 // is currently opened by another process that does not give us the
87 // sharing permissions we need). Since the existing file should be
88 // semantically equivalent to the one we are trying to write, we give
89 // AddBuffer a copy of the bytes we wrote in that case. We do this
90 // instead of just using the existing file, because the pruner might
91 // delete the file before we get a chance to use it.
Rafael Espindolaa17fca02017-11-15 19:09:22 +000092 Error E = TempFile.keep(EntryPath);
93 E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
94 std::error_code EC = E.convertToErrorCode();
95 if (EC != errc::permission_denied)
96 return errorCodeToError(EC);
97
98 auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
99 EntryPath);
Bob Haarmana17bd1212017-11-10 17:08:21 +0000100 MBOrErr = std::move(MBCopy);
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000101
102 // FIXME: should we consume the discard error?
103 consumeError(TempFile.discard());
104
105 return Error::success();
106 });
107
108 if (E)
Peter Collingbourned0e9c162017-09-05 19:51:38 +0000109 report_fatal_error(Twine("Failed to rename temporary file ") +
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000110 TempFile.TmpName + " to " + EntryPath + ": " +
111 toString(std::move(E)) + "\n");
Peter Collingbourned0e9c162017-09-05 19:51:38 +0000112
Teresa Johnsona344fd32018-02-20 20:21:53 +0000113 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +0000114 }
115 };
116
117 return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
118 // Write to a temporary to avoid race condition
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000119 SmallString<64> TempFilenameModel;
Peter Collingbournecb46a6b2017-03-16 18:20:06 +0000120 sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000121 Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
122 TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write);
123 if (!Temp) {
124 errs() << "Error: " << toString(Temp.takeError()) << "\n";
Peter Collingbourne80186a52016-09-23 21:33:43 +0000125 report_fatal_error("ThinLTO: Can't get a temporary file");
126 }
127
128 // This CacheStream will move the temporary file into the cache when done.
Peter Collingbournea638fe02016-09-23 23:23:23 +0000129 return llvm::make_unique<CacheStream>(
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000130 llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false),
131 AddBuffer, std::move(*Temp), EntryPath.str(), Task);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000132 };
133 };
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000134}