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