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