blob: 00373ddd1c4050ae46df8070c616817ec4d9ad66 [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 {
Peter Collingbourne25a17ba2017-03-20 16:41:57 +000030 // This choice of file name allows the cache to be pruned (see pruneCache()
31 // in include/llvm/Support/CachePruning.h).
Peter Collingbourne80186a52016-09-23 21:33:43 +000032 SmallString<64> EntryPath;
Peter Collingbourne25a17ba2017-03-20 16:41:57 +000033 sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
34 // First, see if we have a cache hit.
Peter Collingbourne128423f2017-03-17 00:34:07 +000035 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
36 MemoryBuffer::getFile(EntryPath);
37 if (MBOrErr) {
38 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +000039 return AddStreamFn();
40 }
Mehdi Aminiadc0e262016-08-23 21:30:12 +000041
Peter Collingbourne6bfe4d32017-03-17 21:49:09 +000042 // FIXME: Workaround for libstdc++ version mismatch bug, see D31063 review
43 // thread.
44 if ((std::errc)MBOrErr.getError().value() !=
45 std::errc::no_such_file_or_directory)
Peter Collingbourne128423f2017-03-17 00:34:07 +000046 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
47 ": " + MBOrErr.getError().message() + "\n");
48
Peter Collingbourne80186a52016-09-23 21:33:43 +000049 // This native object stream is responsible for commiting the resulting
Peter Collingbourne128423f2017-03-17 00:34:07 +000050 // file to the cache and calling AddBuffer to add it to the link.
Peter Collingbourne80186a52016-09-23 21:33:43 +000051 struct CacheStream : NativeObjectStream {
Peter Collingbourne128423f2017-03-17 00:34:07 +000052 AddBufferFn AddBuffer;
Peter Collingbourne80186a52016-09-23 21:33:43 +000053 std::string TempFilename;
54 std::string EntryPath;
55 unsigned Task;
Mehdi Aminiadc0e262016-08-23 21:30:12 +000056
Peter Collingbourne128423f2017-03-17 00:34:07 +000057 CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
Peter Collingbourne80186a52016-09-23 21:33:43 +000058 std::string TempFilename, std::string EntryPath,
59 unsigned Task)
Peter Collingbourne128423f2017-03-17 00:34:07 +000060 : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
Benjamin Kramer061f4a52017-01-13 14:39:03 +000061 TempFilename(std::move(TempFilename)),
62 EntryPath(std::move(EntryPath)), Task(Task) {}
Peter Collingbourne80186a52016-09-23 21:33:43 +000063
64 ~CacheStream() {
Peter Collingbourne128423f2017-03-17 00:34:07 +000065 // FIXME: This code could race with the cache pruner, but it is unlikely
66 // that the cache pruner will choose to remove a newly created file.
67
Peter Collingbourne80186a52016-09-23 21:33:43 +000068 // Make sure the file is closed before committing it.
69 OS.reset();
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000070 // This is atomic on POSIX systems.
71 if (auto EC = sys::fs::rename(TempFilename, EntryPath))
72 report_fatal_error(Twine("Failed to rename temporary file ") +
73 TempFilename + ": " + EC.message() + "\n");
Peter Collingbourne128423f2017-03-17 00:34:07 +000074
75 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
76 MemoryBuffer::getFile(EntryPath);
77 if (!MBOrErr)
78 report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
79 ": " + MBOrErr.getError().message() + "\n");
80 AddBuffer(Task, std::move(*MBOrErr));
Peter Collingbourne80186a52016-09-23 21:33:43 +000081 }
82 };
83
84 return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
85 // Write to a temporary to avoid race condition
86 int TempFD;
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000087 SmallString<64> TempFilenameModel, TempFilename;
88 sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
Peter Collingbourne80186a52016-09-23 21:33:43 +000089 std::error_code EC =
Peter Collingbournecb46a6b2017-03-16 18:20:06 +000090 sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
91 sys::fs::owner_read | sys::fs::owner_write);
Peter Collingbourne80186a52016-09-23 21:33:43 +000092 if (EC) {
93 errs() << "Error: " << EC.message() << "\n";
94 report_fatal_error("ThinLTO: Can't get a temporary file");
95 }
96
97 // This CacheStream will move the temporary file into the cache when done.
Peter Collingbournea638fe02016-09-23 23:23:23 +000098 return llvm::make_unique<CacheStream>(
Peter Collingbourne80186a52016-09-23 21:33:43 +000099 llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
Peter Collingbourne128423f2017-03-17 00:34:07 +0000100 AddBuffer, TempFilename.str(), EntryPath.str(), Task);
Peter Collingbourne80186a52016-09-23 21:33:43 +0000101 };
102 };
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000103}