Mehdi Amini | 242275b | 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 | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Peter Collingbourne | 3cd1f18 | 2017-03-20 18:19:41 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Errc.h" |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
Bob Haarman | d4f1cfb | 2017-11-10 17:08:21 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Process.h" |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
Andrew Ng | 645f3d0 | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 22 | #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 23 | #include <unistd.h> |
| 24 | #else |
| 25 | #include <io.h> |
| 26 | #endif |
| 27 | |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | using namespace llvm::lto; |
| 30 | |
Peter Collingbourne | cc5ba44 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 31 | Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 32 | AddBufferFn AddBuffer) { |
Peter Collingbourne | cc5ba44 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 33 | if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) |
| 34 | return errorCodeToError(EC); |
| 35 | |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 36 | return [=](unsigned Task, StringRef Key) -> AddStreamFn { |
Peter Collingbourne | 2291065 | 2017-03-20 16:41:57 +0000 | [diff] [blame] | 37 | // This choice of file name allows the cache to be pruned (see pruneCache() |
| 38 | // in include/llvm/Support/CachePruning.h). |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 39 | SmallString<64> EntryPath; |
Peter Collingbourne | 2291065 | 2017-03-20 16:41:57 +0000 | [diff] [blame] | 40 | sys::path::append(EntryPath, CacheDirectoryPath, "llvmcache-" + Key); |
| 41 | // First, see if we have a cache hit. |
Andrew Ng | 645f3d0 | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 42 | int FD; |
| 43 | SmallString<64> ResultPath; |
| 44 | std::error_code EC = sys::fs::openFileForRead( |
| 45 | Twine(EntryPath), FD, sys::fs::OF_UpdateAtime, &ResultPath); |
| 46 | if (!EC) { |
| 47 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 48 | MemoryBuffer::getOpenFile(FD, EntryPath, |
| 49 | /*FileSize*/ -1, |
| 50 | /*RequiresNullTerminator*/ false); |
| 51 | close(FD); |
| 52 | if (MBOrErr) { |
| 53 | AddBuffer(Task, std::move(*MBOrErr)); |
| 54 | return AddStreamFn(); |
| 55 | } |
| 56 | EC = MBOrErr.getError(); |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 57 | } |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 58 | |
Peter Collingbourne | 6965c8b | 2018-06-13 18:03:14 +0000 | [diff] [blame] | 59 | // On Windows we can fail to open a cache file with a permission denied |
| 60 | // error. This generally means that another process has requested to delete |
| 61 | // the file while it is still open, but it could also mean that another |
| 62 | // process has opened the file without the sharing permissions we need. |
| 63 | // Since the file is probably being deleted we handle it in the same way as |
| 64 | // if the file did not exist at all. |
Andrew Ng | 645f3d0 | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 65 | if (EC != errc::no_such_file_or_directory && EC != errc::permission_denied) |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 66 | report_fatal_error(Twine("Failed to open cache file ") + EntryPath + |
Andrew Ng | 645f3d0 | 2018-07-04 14:17:10 +0000 | [diff] [blame] | 67 | ": " + EC.message() + "\n"); |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 68 | |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 69 | // This native object stream is responsible for commiting the resulting |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 70 | // file to the cache and calling AddBuffer to add it to the link. |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 71 | struct CacheStream : NativeObjectStream { |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 72 | AddBufferFn AddBuffer; |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 73 | sys::fs::TempFile TempFile; |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 74 | std::string EntryPath; |
| 75 | unsigned Task; |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 76 | |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 77 | CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer, |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 78 | sys::fs::TempFile TempFile, std::string EntryPath, |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 79 | unsigned Task) |
Peter Collingbourne | ec179d7 | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 80 | : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)), |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 81 | TempFile(std::move(TempFile)), EntryPath(std::move(EntryPath)), |
| 82 | Task(Task) {} |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 83 | |
| 84 | ~CacheStream() { |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 85 | // Make sure the stream is closed before committing it. |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 86 | OS.reset(); |
Peter Collingbourne | a3886c1 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 87 | |
Peter Collingbourne | a3886c1 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 88 | // Open the file first to avoid racing with a cache pruner. |
| 89 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 90 | MemoryBuffer::getOpenFile(TempFile.FD, TempFile.TmpName, |
| 91 | /*FileSize*/ -1, |
| 92 | /*RequiresNullTerminator*/ false); |
Peter Collingbourne | eb5fecf | 2017-10-03 00:44:21 +0000 | [diff] [blame] | 93 | if (!MBOrErr) |
| 94 | report_fatal_error(Twine("Failed to open new cache file ") + |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 95 | TempFile.TmpName + ": " + |
Peter Collingbourne | eb5fecf | 2017-10-03 00:44:21 +0000 | [diff] [blame] | 96 | MBOrErr.getError().message() + "\n"); |
Peter Collingbourne | a3886c1 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 97 | |
Bob Haarman | f349107 | 2017-11-16 01:16:52 +0000 | [diff] [blame] | 98 | // On POSIX systems, this will atomically replace the destination if |
| 99 | // it already exists. We try to emulate this on Windows, but this may |
| 100 | // fail with a permission denied error (for example, if the destination |
| 101 | // is currently opened by another process that does not give us the |
| 102 | // sharing permissions we need). Since the existing file should be |
| 103 | // semantically equivalent to the one we are trying to write, we give |
| 104 | // AddBuffer a copy of the bytes we wrote in that case. We do this |
| 105 | // instead of just using the existing file, because the pruner might |
| 106 | // delete the file before we get a chance to use it. |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 107 | Error E = TempFile.keep(EntryPath); |
| 108 | E = handleErrors(std::move(E), [&](const ECError &E) -> Error { |
| 109 | std::error_code EC = E.convertToErrorCode(); |
| 110 | if (EC != errc::permission_denied) |
| 111 | return errorCodeToError(EC); |
| 112 | |
| 113 | auto MBCopy = MemoryBuffer::getMemBufferCopy((*MBOrErr)->getBuffer(), |
| 114 | EntryPath); |
Bob Haarman | d4f1cfb | 2017-11-10 17:08:21 +0000 | [diff] [blame] | 115 | MBOrErr = std::move(MBCopy); |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 116 | |
| 117 | // FIXME: should we consume the discard error? |
| 118 | consumeError(TempFile.discard()); |
| 119 | |
| 120 | return Error::success(); |
| 121 | }); |
| 122 | |
| 123 | if (E) |
Peter Collingbourne | a3886c1 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 124 | report_fatal_error(Twine("Failed to rename temporary file ") + |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 125 | TempFile.TmpName + " to " + EntryPath + ": " + |
| 126 | toString(std::move(E)) + "\n"); |
Peter Collingbourne | a3886c1 | 2017-09-05 19:51:38 +0000 | [diff] [blame] | 127 | |
Teresa Johnson | 761281e | 2018-02-20 20:21:53 +0000 | [diff] [blame] | 128 | AddBuffer(Task, std::move(*MBOrErr)); |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
| 132 | return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { |
| 133 | // Write to a temporary to avoid race condition |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 134 | SmallString<64> TempFilenameModel; |
Peter Collingbourne | aad0d39 | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 135 | sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 136 | Expected<sys::fs::TempFile> Temp = sys::fs::TempFile::create( |
| 137 | TempFilenameModel, sys::fs::owner_read | sys::fs::owner_write); |
| 138 | if (!Temp) { |
| 139 | errs() << "Error: " << toString(Temp.takeError()) << "\n"; |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 140 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 141 | } |
| 142 | |
| 143 | // This CacheStream will move the temporary file into the cache when done. |
Peter Collingbourne | 9194bd4 | 2016-09-23 23:23:23 +0000 | [diff] [blame] | 144 | return llvm::make_unique<CacheStream>( |
Rafael Espindola | 58a331a | 2017-11-15 19:09:22 +0000 | [diff] [blame] | 145 | llvm::make_unique<raw_fd_ostream>(Temp->FD, /* ShouldClose */ false), |
| 146 | AddBuffer, std::move(*Temp), EntryPath.str(), Task); |
Peter Collingbourne | 91d99c6 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 147 | }; |
| 148 | }; |
Mehdi Amini | 242275b | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 149 | } |