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" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
| 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) { |
| 39 | AddBuffer(Task, std::move(*MBOrErr)); |
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; |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 51 | std::string TempFilename; |
| 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, |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 56 | std::string TempFilename, std::string EntryPath, |
| 57 | unsigned Task) |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 58 | : NativeObjectStream(std::move(OS)), AddBuffer(std::move(AddBuffer)), |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 59 | TempFilename(std::move(TempFilename)), |
| 60 | EntryPath(std::move(EntryPath)), Task(Task) {} |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 61 | |
| 62 | ~CacheStream() { |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 66 | // Make sure the file is closed before committing it. |
| 67 | OS.reset(); |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 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"); |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 72 | |
| 73 | ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = |
| 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)); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
| 82 | return [=](size_t Task) -> std::unique_ptr<NativeObjectStream> { |
| 83 | // Write to a temporary to avoid race condition |
| 84 | int TempFD; |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 85 | SmallString<64> TempFilenameModel, TempFilename; |
| 86 | sys::path::append(TempFilenameModel, CacheDirectoryPath, "Thin-%%%%%%.tmp.o"); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 87 | std::error_code EC = |
Peter Collingbourne | cb46a6b | 2017-03-16 18:20:06 +0000 | [diff] [blame] | 88 | sys::fs::createUniqueFile(TempFilenameModel, TempFD, TempFilename, |
| 89 | sys::fs::owner_read | sys::fs::owner_write); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 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. |
Peter Collingbourne | a638fe0 | 2016-09-23 23:23:23 +0000 | [diff] [blame] | 96 | return llvm::make_unique<CacheStream>( |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 97 | llvm::make_unique<raw_fd_ostream>(TempFD, /* ShouldClose */ true), |
Peter Collingbourne | 128423f | 2017-03-17 00:34:07 +0000 | [diff] [blame] | 98 | AddBuffer, TempFilename.str(), EntryPath.str(), Task); |
Peter Collingbourne | 80186a5 | 2016-09-23 21:33:43 +0000 | [diff] [blame] | 99 | }; |
| 100 | }; |
Mehdi Amini | adc0e26 | 2016-08-23 21:30:12 +0000 | [diff] [blame] | 101 | } |