blob: 3f10c154683d983624d90a1e3caab7c7aabf6aad [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"
Mehdi Aminiadc0e262016-08-23 21:30:12 +000017#include "llvm/Support/FileSystem.h"
Peter Collingbourne80186a52016-09-23 21:33:43 +000018#include "llvm/Support/MemoryBuffer.h"
Mehdi Aminiadc0e262016-08-23 21:30:12 +000019#include "llvm/Support/Path.h"
20#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;
Peter Collingbourne80186a52016-09-23 21:33:43 +000051 std::string TempFilename;
52 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,
Peter Collingbourne80186a52016-09-23 21:33:43 +000056 std::string TempFilename, std::string EntryPath,
57 unsigned Task)
Peter Collingbourne128423f2017-03-17 00:34:07 +000058 : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
Benjamin Kramer061f4a52017-01-13 14:39:03 +000059 TempFilename(std::move(TempFilename)),
60 EntryPath(std::move(EntryPath)), Task(Task) {}
Peter Collingbourne80186a52016-09-23 21:33:43 +000061
62 ~CacheStream() {
63 // Make sure the file is closed before committing it.
64 OS.reset();
Peter Collingbourned0e9c162017-09-05 19:51:38 +000065
66#ifdef _WIN32
67 // Rename the file first on Windows because we cannot rename an open
68 // file on that platform using the sys::fs::rename function.
69 // FIXME: This code could race with the cache pruner, but it is unlikely
70 // that the cache pruner will choose to remove a newly created file.
71 // We should look at using the SetFileInformationByHandle function to
72 // rename the file while it is open.
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000073 if (auto EC = sys::fs::rename(TempFilename, EntryPath))
74 report_fatal_error(Twine("Failed to rename temporary file ") +
75 TempFilename + ": " + EC.message() + "\n");
Peter Collingbourne128423f2017-03-17 00:34:07 +000076
77 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
78 MemoryBuffer::getFile(EntryPath);
Peter Collingbourned0e9c162017-09-05 19:51:38 +000079#else
80 // Open the file first to avoid racing with a cache pruner.
81 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
82 MemoryBuffer::getFile(TempFilename);
83
84 // This is atomic on POSIX systems.
85 if (auto EC = sys::fs::rename(TempFilename, EntryPath))
86 report_fatal_error(Twine("Failed to rename temporary file ") +
87 TempFilename + ": " + EC.message() + "\n");
88#endif
89
Peter Collingbourne128423f2017-03-17 00:34:07 +000090 if (!MBOrErr)
91 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
92 ": " + MBOrErr.getError().message() + "\n");
Peter Collingbourned0e9c162017-09-05 19:51:38 +000093 AddBuffer(Task, std::move(*MBOrErr), EntryPath);
Peter Collingbourne80186a52016-09-23 21:33:43 +000094 }
95 };
96
97 return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
98 // Write to a temporary to avoid race condition
99 int TempFD;
Peter Collingbournecb46a6b2017-03-16 18:20:06 +0000100 SmallString<64> TempFilenameModel, TempFilename;
101 sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
Peter Collingbourne80186a52016-09-23 21:33:43 +0000102 std::error_code EC =
Peter Collingbournecb46a6b2017-03-16 18:20:06 +0000103 sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
104 sys::fs::owner_read | sys::fs::owner_write);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000105 if (EC) {
106 errs() << "Error: " << EC.message() << "\n";
107 report_fatal_error("ThinLTO: Can't get a temporary file");
108 }
109
110 // This CacheStream will move the temporary file into the cache when done.
Peter Collingbournea638fe02016-09-23 23:23:23 +0000111 return llvm::make_unique<CacheStream>(
Peter Collingbourne80186a52016-09-23 21:33:43 +0000112 llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
Peter Collingbourne128423f2017-03-17 00:34:07 +0000113 AddBuffer, TempFilename.str(), EntryPath.str(), Task);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000114 };
115 };
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000116}