Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 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" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Peter Collingbourne | d786047 | 2017-03-20 18:19:41 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Bob Haarman | a17bd121 | 2017-11-10 17:08:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Process.h" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | using namespace llvm::lto; |
| 24 | |
Peter Collingbourne | ab76a19 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 25 | Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 26 | AddBufferFn AddBuffer) { |
Peter Collingbourne | ab76a19 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 27 | if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) |
| 28 | return errorCodeToError(EC); |
| 29 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 30 | return [=](unsigned Task, StringRef Key) -> AddStreamFn { |
Peter Collingbourne | 25a17ba | 2017-03-20 16:41:57 +0000 | [diff] [blame] | 31 | // This choice of file name allows the cache to be pruned (see pruneCache() |
| 32 | // in include/llvm/Support/CachePruning.h). |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 33 | SmallString<64> EntryPath; |
Peter Collingbourne | 25a17ba | 2017-03-20 16:41:57 +0000 | [diff] [blame] | 34 | sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key); |
| 35 | // First, see if we have a cache hit. |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 36 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 37 | MemoryBuffer::getFile(EntryPath); |
| 38 | if (MBOrErr) { |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 39 | AddBuffer(Task, std::move(*MBOrErr), EntryPath); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 40 | return AddStreamFn(); |
| 41 | } |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 42 | |
Peter Collingbourne | d786047 | 2017-03-20 18:19:41 +0000 | [diff] [blame] | 43 | if (MBOrErr.getError() != errc::no_such_file_or_directory) |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 44 | report_fatal_error(Twine("Failed to open cache file ") + EntryPath + |
| 45 | ": " + MBOrErr.getError().message() + "\n"); |
| 46 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 47 | // This native object stream is responsible for commiting the resulting |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 48 | // file to the cache and calling AddBuffer to add it to the link. |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 49 | struct CacheStream : NativeObjectStream { |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 50 | AddBufferFn AddBuffer; |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 51 | sys::fs::TempFile TempFile; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 52 | std::string EntryPath; |
| 53 | unsigned Task; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 54 | |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 55 | CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer, |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 56 | sys::fs::TempFile TempFile, std::string EntryPath, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 57 | unsigned Task) |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 58 | : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)), |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 59 | TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)), |
| 60 | Task(Task) {} |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 61 | |
| 62 | ~CacheStream() { |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 63 | // Make sure the stream is closed before committing it. |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 64 | OS.reset(); |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 65 | |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 66 | // Open the file first to avoid racing with a cache pruner. |
| 67 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 68 | MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName, |
| 69 | /*FileSize*/ -1, |
| 70 | /*RequiresNullTerminator*/ false); |
Peter Collingbourne | d5fc37c | 2017-10-03 00:44:21 +0000 | [diff] [blame] | 71 | if (!MBOrErr) |
| 72 | report_fatal_error(Twine("Failed to open new cache file ") + |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 73 | TempFile.TmpName + ": " + |
Peter Collingbourne | d5fc37c | 2017-10-03 00:44:21 +0000 | [diff] [blame] | 74 | MBOrErr.getError().message() + "\n"); |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 75 | |
| 76 | // This is atomic on POSIX systems. |
Bob Haarman | a17bd121 | 2017-11-10 17:08:21 +0000 | [diff] [blame] | 77 | // On Windows, it can fail with permission denied if the destination |
| 78 | // file already exists. Since the existing file should be semantically |
| 79 | // equivalent to the one we are trying to write, we give AddBuffer |
| 80 | // a copy of the bytes we wrote in that case. We do this instead of |
| 81 | // just using the existing file, because the pruner might delete the |
| 82 | // file before we get a chance to use it. |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 83 | Error E = TempFile.keep(EntryPath); |
| 84 | E = handleErrors(std::move(E), [&](const ECError &E) -> Error { |
| 85 | std::error_code EC = E.convertToErrorCode(); |
| 86 | if (EC != errc::permission_denied) |
| 87 | return errorCodeToError(EC); |
| 88 | |
| 89 | auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(), |
| 90 | EntryPath); |
Bob Haarman | a17bd121 | 2017-11-10 17:08:21 +0000 | [diff] [blame] | 91 | MBOrErr = std::move(MBCopy); |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 92 | |
| 93 | // FIXME: should we consume the discard error? |
| 94 | consumeError(TempFile.discard()); |
| 95 | |
| 96 | return Error::success(); |
| 97 | }); |
| 98 | |
| 99 | if (E) |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 100 | report_fatal_error(Twine("Failed to rename temporary file ") + |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 101 | TempFile.TmpName + " to " + EntryPath + ": " + |
| 102 | toString(std::move(E)) + "\n"); |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 103 | |
Peter Collingbourne | d0e9c16 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 104 | AddBuffer(Task, std::move(*MBOrErr), EntryPath); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
| 108 | return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { |
| 109 | // Write to a temporary to avoid race condition |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 110 | SmallString<64> TempFilenameModel; |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 111 | sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 112 | Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create( |
| 113 | TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write); |
| 114 | if (!Temp) { |
| 115 | errs() << "Error: " << toString(Temp.takeError()) << "\n"; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 116 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 117 | } |
| 118 | |
| 119 | // This CacheStream will move the temporary file into the cache when done. |
Peter Collingbourne | a638fe0 | 2016-09-23 23:23:23 +0000 | [diff] [blame] | 120 | return llvm::make_unique<CacheStream>( |
Rafael Espindola | a17fca0 | 2017-11-15 19:09:22 +0000 | [diff] [blame^] | 121 | llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false), |
| 122 | AddBuffer, std::move(*Temp), EntryPath.str(), Task); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 123 | }; |
| 124 | }; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 125 | } |