LLVM  5.0.0
Caching.cpp
Go to the documentation of this file.
1 //===-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"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/Errc.h"
19 #include "llvm/Support/Path.h"
21 
22 using namespace llvm;
23 using namespace llvm::lto;
24 
26  AddBufferFn AddBuffer) {
27  if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath))
28  return errorCodeToError(EC);
29 
30  return [=](unsigned Task, StringRef Key) -> AddStreamFn {
31  // This choice of file name allows the cache to be pruned (see pruneCache()
32  // in include/llvm/Support/CachePruning.h).
33  SmallString<64> EntryPath;
34  sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key);
35  // First, see if we have a cache hit.
37  MemoryBuffer::getFile(EntryPath);
38  if (MBOrErr) {
39  AddBuffer(Task, std::move(*MBOrErr));
40  return AddStreamFn();
41  }
42 
44  report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
45  ": " + MBOrErr.getError().message() + "\n");
46 
47  // This native object stream is responsible for commiting the resulting
48  // file to the cache and calling AddBuffer to add it to the link.
49  struct CacheStream : NativeObjectStream {
50  AddBufferFn AddBuffer;
51  std::string TempFilename;
52  std::string EntryPath;
53  unsigned Task;
54 
55  CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer,
56  std::string TempFilename, std::string EntryPath,
57  unsigned Task)
58  : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)),
59  TempFilename(std::move(TempFilename)),
60  EntryPath(std::move(EntryPath)), Task(Task) {}
61 
62  ~CacheStream() {
63  // FIXME: This code could race with the cache pruner, but it is unlikely
64  // that the cache pruner will choose to remove a newly created file.
65 
66  // Make sure the file is closed before committing it.
67  OS.reset();
68  // This is atomic on POSIX systems.
69  if (auto EC = sys::fs::rename(TempFilename, EntryPath))
70  report_fatal_error(Twine("Failed to rename temporary file ") +
71  TempFilename + ": " + EC.message() + "\n");
72 
74  MemoryBuffer::getFile(EntryPath);
75  if (!MBOrErr)
76  report_fatal_error(Twine("Failed to open cache file ") + EntryPath +
77  ": " + MBOrErr.getError().message() + "\n");
78  AddBuffer(Task, std::move(*MBOrErr));
79  }
80  };
81 
82  return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> {
83  // Write to a temporary to avoid race condition
84  int TempFD;
85  SmallString<64> TempFilenameModel, TempFilename;
86  sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o");
87  std::error_code EC =
88  sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename,
90  if (EC) {
91  errs() << "Error: " << EC.message() << "\n";
92  report_fatal_error("ThinLTO: Can't get a temporary file");
93  }
94 
95  // This CacheStream will move the temporary file into the cache when done.
96  return llvm::make_unique<CacheStream>(
97  llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true),
98  AddBuffer, TempFilename.str(), EntryPath.str(), Task);
99  };
100  };
101 }
std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition: Path.cpp:868
std::error_code getError() const
Definition: ErrorOr.h:172
Represents either an error or a value T.
Definition: ErrorOr.h:69
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:751
std::function< void(unsigned Task, std::unique_ptr< MemoryBuffer > MB)> AddBufferFn
This type defines the callback to add a pre-existing native object file (e.g.
Definition: Caching.h:33
std::function< std::unique_ptr< NativeObjectStream >unsigned Task)> AddStreamFn
This type defines the callback to add a native object that is generated on the fly.
Definition: LTO.h:174
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:465
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
This class wraps an output stream for a native object.
Definition: LTO.h:162
Expected< NativeObjectCache > localCache(StringRef CacheDirectoryPath, AddBufferFn AddBuffer)
Create a local file system cache which uses the given cache directory and file callback.
Definition: Caching.cpp:25
std::error_code rename(const Twine &from, const Twine &to)
Rename from to to.
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49