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" |
| 16 | #include "llvm/Support/FileSystem.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" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::lto; |
| 23 | |
Peter Collingbourne | ab76a19 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 24 | Expected<NativeObjectCache> lto::localCache(StringRef CacheDirectoryPath, |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 25 | AddBufferFn AddBuffer) { |
Peter Collingbourne | ab76a19 | 2017-03-02 02:02:38 +0000 | [diff] [blame] | 26 | if (std::error_code EC = sys::fs::create_directories(CacheDirectoryPath)) |
| 27 | return errorCodeToError(EC); |
| 28 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 29 | return [=](unsigned Task, StringRef Key) -> AddStreamFn { |
| 30 | // First, see if we have a cache hit. |
| 31 | SmallString<64> EntryPath; |
| 32 | sys::path::append(EntryPath, CacheDirectoryPath, Key); |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 33 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 34 | MemoryBuffer::getFile(EntryPath); |
| 35 | if (MBOrErr) { |
| 36 | AddBuffer(Task, std::move(*MBOrErr)); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 37 | return AddStreamFn(); |
| 38 | } |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 39 | |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 40 | if (MBOrErr.getError() != std::errc::no_such_file_or_directory) |
| 41 | report_fatal_error(Twine("Failed to open cache file ") + EntryPath + |
| 42 | ": " + MBOrErr.getError().message() + "\n"); |
| 43 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 44 | // This native object stream is responsible for commiting the resulting |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 45 | // 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] | 46 | struct CacheStream : NativeObjectStream { |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 47 | AddBufferFn AddBuffer; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 48 | std::string TempFilename; |
| 49 | std::string EntryPath; |
| 50 | unsigned Task; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 51 | |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 52 | CacheStream(std::unique_ptr<raw_pwrite_stream> OS, AddBufferFn AddBuffer, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 53 | std::string TempFilename, std::string EntryPath, |
| 54 | unsigned Task) |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 55 | : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)), |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 56 | TempFilename(std::move(TempFilename)), |
| 57 | EntryPath(std::move(EntryPath)), Task(Task) {} |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 58 | |
| 59 | ~CacheStream() { |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 60 | // FIXME: This code could race with the cache pruner, but it is unlikely |
| 61 | // that the cache pruner will choose to remove a newly created file. |
| 62 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 63 | // Make sure the file is closed before committing it. |
| 64 | OS.reset(); |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 65 | // This is atomic on POSIX systems. |
| 66 | if (auto EC = sys::fs::rename(TempFilename, EntryPath)) |
| 67 | report_fatal_error(Twine("Failed to rename temporary file ") + |
| 68 | TempFilename + ": " + EC.message() + "\n"); |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 69 | |
| 70 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 71 | MemoryBuffer::getFile(EntryPath); |
| 72 | if (!MBOrErr) |
| 73 | report_fatal_error(Twine("Failed to open cache file ") + EntryPath + |
| 74 | ": " + MBOrErr.getError().message() + "\n"); |
| 75 | AddBuffer(Task, std::move(*MBOrErr)); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 76 | } |
| 77 | }; |
| 78 | |
| 79 | return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { |
| 80 | // Write to a temporary to avoid race condition |
| 81 | int TempFD; |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 82 | SmallString<64> TempFilenameModel, TempFilename; |
| 83 | sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 84 | std::error_code EC = |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 85 | sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename, |
| 86 | sys::fs::owner_read | sys::fs::owner_write); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 87 | if (EC) { |
| 88 | errs() << "Error: " << EC.message() << "\n"; |
| 89 | report_fatal_error("ThinLTO: Can't get a temporary file"); |
| 90 | } |
| 91 | |
| 92 | // This CacheStream will move the temporary file into the cache when done. |
Peter Collingbourne | a638fe0 | 2016-09-23 23:23:23 +0000 | [diff] [blame] | 93 | return llvm::make_unique<CacheStream>( |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 94 | llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true), |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame^] | 95 | AddBuffer, TempFilename.str(), EntryPath.str(), Task); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 96 | }; |
| 97 | }; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 98 | } |