blob: d8b91c48ee306c2f97d99ef272b54db9a9d6a7f3 [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"
16#include "llvm/Support/FileSystem.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"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22using namespace llvm::lto;
23
Peter Collingbourneab76a192017-03-02 02:02:38 +000024Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
Peter Collingbourne128423f2017-03-17 00:34:07 +000025 AddBufferFn AddBuffer) {
Peter Collingbourneab76a192017-03-02 02:02:38 +000026 if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
27 return errorCodeToError(EC);
28
Peter Collingbourne80186a52016-09-23 21:33:43 +000029 return [=](unsigned Task, StringRef Key) -> AddStreamFn {
30 // First, see if we have a cache hit.
31 SmallString<64> EntryPath;
32 sys::path::append(EntryPath, CacheDirectoryPath, Key);
Peter Collingbourne128423f2017-03-17 00:34:07 +000033 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
34 MemoryBuffer::getFile(EntryPath);
35 if (MBOrErr) {
36 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +000037 return AddStreamFn();
38 }
Mehdi Aminiadc0e262016-08-23 21:30:12 +000039
Peter Collingbourne128423f2017-03-17 00:34:07 +000040 if (MBOrErr.getError() != std::errc::no_such_file_or_directory)
41 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
42 ": " + MBOrErr.getError().message() + "\n");
43
Peter Collingbourne80186a52016-09-23 21:33:43 +000044 // This native object stream is responsible for commiting the resulting
Peter Collingbourne128423f2017-03-17 00:34:07 +000045 // file to the cache and calling AddBuffer to add it to the link.
Peter Collingbourne80186a52016-09-23 21:33:43 +000046 struct CacheStream : NativeObjectStream {
Peter Collingbourne128423f2017-03-17 00:34:07 +000047 AddBufferFn AddBuffer;
Peter Collingbourne80186a52016-09-23 21:33:43 +000048 std::string TempFilename;
49 std::string EntryPath;
50 unsigned Task;
Mehdi Aminiadc0e262016-08-23 21:30:12 +000051
Peter Collingbourne128423f2017-03-17 00:34:07 +000052 CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
Peter Collingbourne80186a52016-09-23 21:33:43 +000053 std::string TempFilename, std::string EntryPath,
54 unsigned Task)
Peter Collingbourne128423f2017-03-17 00:34:07 +000055 : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
Benjamin Kramer061f4a52017-01-13 14:39:03 +000056 TempFilename(std::move(TempFilename)),
57 EntryPath(std::move(EntryPath)), Task(Task) {}
Peter Collingbourne80186a52016-09-23 21:33:43 +000058
59 ~CacheStream() {
Peter Collingbourne128423f2017-03-17 00:34:07 +000060 // FIXME: This code could race with the cache pruner, but it is unlikely
61 // that the cache pruner will choose to remove a newly created file.
62
Peter Collingbourne80186a52016-09-23 21:33:43 +000063 // Make sure the file is closed before committing it.
64 OS.reset();
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000065 // This is atomic on POSIX systems.
66 if (auto EC = sys::fs::rename(TempFilename, EntryPath))
67 report_fatal_error(Twine("Failed to rename temporary file ") +
68 TempFilename + ": " + EC.message() + "\n");
Peter Collingbourne128423f2017-03-17 00:34:07 +000069
70 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
71 MemoryBuffer::getFile(EntryPath);
72 if (!MBOrErr)
73 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
74 ": " + MBOrErr.getError().message() + "\n");
75 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +000076 }
77 };
78
79 return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
80 // Write to a temporary to avoid race condition
81 int TempFD;
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000082 SmallString<64> TempFilenameModel, TempFilename;
83 sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
Peter Collingbourne80186a52016-09-23 21:33:43 +000084 std::error_code EC =
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000085 sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
86 sys::fs::owner_read | sys::fs::owner_write);
Peter Collingbourne80186a52016-09-23 21:33:43 +000087 if (EC) {
88 errs() << "Error: " << EC.message() << "\n";
89 report_fatal_error("ThinLTO: Can't get a temporary file");
90 }
91
92 // This CacheStream will move the temporary file into the cache when done.
Peter Collingbournea638fe02016-09-23 23:23:23 +000093 return llvm::make_unique<CacheStream>(
Peter Collingbourne80186a52016-09-23 21:33:43 +000094 llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
Peter Collingbourne128423f2017-03-17 00:34:07 +000095 AddBuffer, TempFilename.str(), EntryPath.str(), Task);
Peter Collingbourne80186a52016-09-23 21:33:43 +000096 };
97 };
Mehdi Aminiadc0e262016-08-23 21:30:12 +000098}