2016-08-24 05:30:12 +08:00
|
|
|
//===-Caching.cpp - LLVM Link Time Optimizer Cache Handling ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Caching for ThinLTO.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/LTO/Caching.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2017-03-21 02:19:41 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2016-08-24 05:30:12 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-09-24 05:33:43 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-08-24 05:30:12 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::lto;
|
|
|
|
|
2017-03-02 10:02:38 +08:00
|
|
|
Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath,
|
2017-03-17 08:34:07 +08:00
|
|
|
AddBufferFn AddBuffer) {
|
2017-03-02 10:02:38 +08:00
|
|
|
if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
return [=](unsigned Task, StringRef Key) -> AddStreamFn {
|
2017-03-21 00:41:57 +08:00
|
|
|
// This choice of file name allows the cache to be pruned (see pruneCache()
|
|
|
|
// in include/llvm/Support/CachePruning.h).
|
2016-09-24 05:33:43 +08:00
|
|
|
SmallString<64> EntryPath;
|
2017-03-21 00:41:57 +08:00
|
|
|
sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
|
|
|
|
// First, see if we have a cache hit.
|
2017-03-17 08:34:07 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(EntryPath);
|
|
|
|
if (MBOrErr) {
|
|
|
|
AddBuffer(Task, std::move(*MBOrErr));
|
2016-09-24 05:33:43 +08:00
|
|
|
return AddStreamFn();
|
|
|
|
}
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2017-03-21 02:19:41 +08:00
|
|
|
if (MBOrErr.getError() != errc::no_such_file_or_directory)
|
2017-03-17 08:34:07 +08:00
|
|
|
report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
|
|
|
|
": " + MBOrErr.getError().message() + "\n");
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
// This native object stream is responsible for commiting the resulting
|
2017-03-17 08:34:07 +08:00
|
|
|
// file to the cache and calling AddBuffer to add it to the link.
|
2016-09-24 05:33:43 +08:00
|
|
|
struct CacheStream : NativeObjectStream {
|
2017-03-17 08:34:07 +08:00
|
|
|
AddBufferFn AddBuffer;
|
2016-09-24 05:33:43 +08:00
|
|
|
std::string TempFilename;
|
|
|
|
std::string EntryPath;
|
|
|
|
unsigned Task;
|
2016-08-24 05:30:12 +08:00
|
|
|
|
2017-03-17 08:34:07 +08:00
|
|
|
CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
|
2016-09-24 05:33:43 +08:00
|
|
|
std::string TempFilename, std::string EntryPath,
|
|
|
|
unsigned Task)
|
2017-03-17 08:34:07 +08:00
|
|
|
: NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
|
2017-01-13 22:39:03 +08:00
|
|
|
TempFilename(std::move(TempFilename)),
|
|
|
|
EntryPath(std::move(EntryPath)), Task(Task) {}
|
2016-09-24 05:33:43 +08:00
|
|
|
|
|
|
|
~CacheStream() {
|
2017-03-17 08:34:07 +08:00
|
|
|
// FIXME: This code could race with the cache pruner, but it is unlikely
|
|
|
|
// that the cache pruner will choose to remove a newly created file.
|
|
|
|
|
2016-09-24 05:33:43 +08:00
|
|
|
// Make sure the file is closed before committing it.
|
|
|
|
OS.reset();
|
2017-03-17 02:20:06 +08:00
|
|
|
// This is atomic on POSIX systems.
|
|
|
|
if (auto EC = sys::fs::rename(TempFilename, EntryPath))
|
|
|
|
report_fatal_error(Twine("Failed to rename temporary file ") +
|
|
|
|
TempFilename + ": " + EC.message() + "\n");
|
2017-03-17 08:34:07 +08:00
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(EntryPath);
|
|
|
|
if (!MBOrErr)
|
|
|
|
report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
|
|
|
|
": " + MBOrErr.getError().message() + "\n");
|
|
|
|
AddBuffer(Task, std::move(*MBOrErr));
|
2016-09-24 05:33:43 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
|
|
|
|
// Write to a temporary to avoid race condition
|
|
|
|
int TempFD;
|
2017-03-17 02:20:06 +08:00
|
|
|
SmallString<64> TempFilenameModel, TempFilename;
|
|
|
|
sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
|
2016-09-24 05:33:43 +08:00
|
|
|
std::error_code EC =
|
2017-03-17 02:20:06 +08:00
|
|
|
sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
|
|
|
|
sys::fs::owner_read | sys::fs::owner_write);
|
2016-09-24 05:33:43 +08:00
|
|
|
if (EC) {
|
|
|
|
errs() << "Error: " << EC.message() << "\n";
|
|
|
|
report_fatal_error("ThinLTO: Can't get a temporary file");
|
|
|
|
}
|
|
|
|
|
|
|
|
// This CacheStream will move the temporary file into the cache when done.
|
2016-09-24 07:23:23 +08:00
|
|
|
return llvm::make_unique<CacheStream>(
|
2016-09-24 05:33:43 +08:00
|
|
|
llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
|
2017-03-17 08:34:07 +08:00
|
|
|
AddBuffer, TempFilename.str(), EntryPath.str(), Task);
|
2016-09-24 05:33:43 +08:00
|
|
|
};
|
|
|
|
};
|
2016-08-24 05:30:12 +08:00
|
|
|
}
|