blob: e6338b2e18d5e260f4251fea7cadc1db1637c0be [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) {
Peter Collingbourned0e9c162017-09-05 19:51:38 +000039 AddBuffer(Task, std::move(*MBOrErr), EntryPath);
Peter Collingbourne80186a52016-09-23 21:33:43 +000040 return AddStreamFn();
41 }
Mehdi Aminiadc0e262016-08-23 21:30:12 +000042
Peter Collingbourned7860472017-03-20 18:19:41 +000043 if (MBOrErr.getError() != errc::no_such_file_or_directory)
Peter Collingbourne128423f2017-03-17 00:34:07 +000044 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
45 ": " + MBOrErr.getError().message() + "\n");
46
Peter Collingbourne80186a52016-09-23 21:33:43 +000047 // This native object stream is responsible for commiting the resulting
Peter Collingbourne128423f2017-03-17 00:34:07 +000048 // file to the cache and calling AddBuffer to add it to the link.
Peter Collingbourne80186a52016-09-23 21:33:43 +000049 struct CacheStream : NativeObjectStream {
Peter Collingbourne128423f2017-03-17 00:34:07 +000050 AddBufferFn AddBuffer;
Rafael Espindolaa17fca02017-11-15 19:09:22 +000051 sys::fs::TempFile TempFile;
Peter Collingbourne80186a52016-09-23 21:33:43 +000052 std::string EntryPath;
53 unsigned Task;
Mehdi Aminiadc0e262016-08-23 21:30:12 +000054
Peter Collingbourne128423f2017-03-17 00:34:07 +000055 CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
Rafael Espindolaa17fca02017-11-15 19:09:22 +000056 sys::fs::TempFile TempFile, std::string EntryPath,
Peter Collingbourne80186a52016-09-23 21:33:43 +000057 unsigned Task)
Peter Collingbourne128423f2017-03-17 00:34:07 +000058 : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
Rafael Espindolaa17fca02017-11-15 19:09:22 +000059 TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)),
60 Task(Task) {}
Peter Collingbourne80186a52016-09-23 21:33:43 +000061
62 ~CacheStream() {
Rafael Espindolaa17fca02017-11-15 19:09:22 +000063 // Make sure the stream is closed before committing it.
Peter Collingbourne80186a52016-09-23 21:33:43 +000064 OS.reset();
Peter Collingbourned0e9c162017-09-05 19:51:38 +000065
Peter Collingbourned0e9c162017-09-05 19:51:38 +000066 // Open the file first to avoid racing with a cache pruner.
67 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
Rafael Espindolaa17fca02017-11-15 19:09:22 +000068 MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName,
69 /*FileSize*/ -1,
70 /*RequiresNullTerminator*/ false);
Peter Collingbourned5fc37c2017-10-03 00:44:21 +000071 if (!MBOrErr)
72 report_fatal_error(Twine("Failed to open new cache file ") +
Rafael Espindolaa17fca02017-11-15 19:09:22 +000073 TempFile.TmpName + ": " +
Peter Collingbourned5fc37c2017-10-03 00:44:21 +000074 MBOrErr.getError().message() + "\n");
Peter Collingbourned0e9c162017-09-05 19:51:38 +000075
76 // This is atomic on POSIX systems.
Bob Haarmana17bd1212017-11-10 17:08:21 +000077 // On Windows, it can fail with permission denied if the destination
78 // file already exists. Since the existing file should be semantically
79 // equivalent to the one we are trying to write, we give AddBuffer
80 // a copy of the bytes we wrote in that case. We do this instead of
81 // just using the existing file, because the pruner might delete the
82 // file before we get a chance to use it.
Rafael Espindolaa17fca02017-11-15 19:09:22 +000083 Error E = TempFile.keep(EntryPath);
84 E = handleErrors(std::move(E), [&](const ECError &E) -> Error {
85 std::error_code EC = E.convertToErrorCode();
86 if (EC != errc::permission_denied)
87 return errorCodeToError(EC);
88
89 auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(),
90 EntryPath);
Bob Haarmana17bd1212017-11-10 17:08:21 +000091 MBOrErr = std::move(MBCopy);
Rafael Espindolaa17fca02017-11-15 19:09:22 +000092
93 // FIXME: should we consume the discard error?
94 consumeError(TempFile.discard());
95
96 return Error::success();
97 });
98
99 if (E)
Peter Collingbourned0e9c162017-09-05 19:51:38 +0000100 report_fatal_error(Twine("Failed to rename temporary file ") +
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000101 TempFile.TmpName + " to " + EntryPath + ": " +
102 toString(std::move(E)) + "\n");
Peter Collingbourned0e9c162017-09-05 19:51:38 +0000103
Peter Collingbourned0e9c162017-09-05 19:51:38 +0000104 AddBuffer(Task, std::move(*MBOrErr), EntryPath);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000105 }
106 };
107
108 return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
109 // Write to a temporary to avoid race condition
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000110 SmallString<64> TempFilenameModel;
Peter Collingbournecb46a6b2017-03-16 18:20:06 +0000111 sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000112 Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create(
113 TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write);
114 if (!Temp) {
115 errs() << "Error: " << toString(Temp.takeError()) << "\n";
Peter Collingbourne80186a52016-09-23 21:33:43 +0000116 report_fatal_error("ThinLTO: Can't get a temporary file");
117 }
118
119 // This CacheStream will move the temporary file into the cache when done.
Peter Collingbournea638fe02016-09-23 23:23:23 +0000120 return llvm::make_unique<CacheStream>(
Rafael Espindolaa17fca02017-11-15 19:09:22 +0000121 llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false),
122 AddBuffer, std::move(*Temp), EntryPath.str(), Task);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000123 };
124 };
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000125}