namespace llvm {
namespace lto {
/// Type for client-supplied callback when a buffer is loaded from the cache.
-typedef std::function<void(std::unique_ptr<MemoryBuffer>)> AddBufferFn;
+typedef std::function<void(std::string)> AddBufferFn;
/// Manage caching on the filesystem.
///
/// Path to temporary file used to buffer output that will be committed to the
/// cache entry when this object is destroyed
SmallString<128> TempFilename;
- /// User-supplied callback, called when the buffer is pulled out of the cache
+ /// User-supplied callback, used to provide path to cache entry
/// (potentially after creating it).
AddBufferFn AddBuffer;
/// Create a CacheObjectOutput: the client is supposed to create it in the
/// callback supplied to LTO::run. The \p CacheDirectoryPath points to the
/// directory on disk where to store the cache, and \p AddBuffer will be
- /// called when the buffer is pulled out of the cache (potentially after
- /// creating it).
+ /// called when the buffer is ready to be pulled out of the cache
+ /// (potentially after creating it).
CacheObjectOutput(StringRef CacheDirectoryPath, AddBufferFn AddBuffer)
: CacheDirectoryPath(CacheDirectoryPath), AddBuffer(AddBuffer) {}
// We commit the tempfile into the cache now, by moving it to EntryPath.
commitEntry(TempFilename, EntryPath);
}
- // Load the entry from the cache now.
- auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
- if (auto EC = ReloadedBufferOrErr.getError())
- report_fatal_error(Twine("Can't reload cached file '") + EntryPath + "': " +
- EC.message() + "\n");
-
- // Supply the resulting buffer to the user.
- AddBuffer(std::move(*ReloadedBufferOrErr));
+ // Supply the cache path to the user.
+ AddBuffer(EntryPath.str());
}
// Return an allocated stream for the output, or null in case of failure.
auto &OutputName = Filenames[Task];
getOutputFileName(Filename, /*TempOutFile=*/!SaveTemps, OutputName,
MaxTasks > 1 ? Task : -1);
- IsTemporary[Task] = !SaveTemps;
+ IsTemporary[Task] = !SaveTemps && options::cache_dir.empty();
if (options::cache_dir.empty())
return llvm::make_unique<LTOOutput>(OutputName);
return llvm::make_unique<CacheObjectOutput>(
- options::cache_dir, [OutputName](std::unique_ptr<MemoryBuffer> Buffer) {
- *LTOOutput(OutputName).getStream() << Buffer->getBuffer();
- });
+ options::cache_dir,
+ [&OutputName](std::string EntryPath) { OutputName = EntryPath; });
};
check(Lto->run(AddOutput));
return llvm::make_unique<LTOOutput>(std::move(Path));
return llvm::make_unique<CacheObjectOutput>(
- CacheDir, [Path](std::unique_ptr<MemoryBuffer> Buffer) {
- *LTOOutput(Path).getStream() << Buffer->getBuffer();
+ CacheDir, [Path](std::string EntryPath) {
+ // Load the entry from the cache now.
+ auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
+ if (auto EC = ReloadedBufferOrErr.getError())
+ report_fatal_error(Twine("Can't reload cached file '") + EntryPath +
+ "': " + EC.message() + "\n");
+
+ *LTOOutput(Path).getStream() << (*ReloadedBufferOrErr)->getBuffer();
});
};