ThinLTO caching: reload cached file with mmap and drop heap-allocated memory buffer

This is reducing pressure on the OS memory system, and is NFC
when not using a cache.
I measure a 10x memory consumption reduction when linking opt
with full debug info.

From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 269682
This commit is contained in:
Mehdi Amini 2016-05-16 19:11:59 +00:00
parent 804749c942
commit 001bb41556
1 changed files with 14 additions and 5 deletions

View File

@ -484,9 +484,10 @@ public:
}
// Cache the Produced object file
void write(MemoryBufferRef OutputBuffer) {
std::unique_ptr<MemoryBuffer>
write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
if (EntryPath.empty())
return;
return OutputBuffer;
// Write to a temporary to avoid race condition
SmallString<128> TempFilename;
@ -499,7 +500,7 @@ public:
}
{
raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
OS << OutputBuffer.getBuffer();
OS << OutputBuffer->getBuffer();
}
// Rename to final destination (hopefully race condition won't matter here)
EC = sys::fs::rename(TempFilename, EntryPath);
@ -509,8 +510,16 @@ public:
if (EC)
report_fatal_error(Twine("Failed to open ") + EntryPath +
" to save cached entry\n");
OS << OutputBuffer.getBuffer();
OS << OutputBuffer->getBuffer();
}
auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
if (auto EC = ReloadedBufferOrErr.getError()) {
// FIXME diagnose
errs() << "error: can't reload cached file '" << EntryPath
<< "': " << EC.message() << "\n";
return OutputBuffer;
}
return std::move(*ReloadedBufferOrErr);
}
};
@ -943,7 +952,7 @@ void ThinLTOCodeGenerator::run() {
ExportList, GUIDPreservedSymbols, ResolvedODR, CacheOptions,
DisableCodeGen, SaveTempsDir, count);
CacheEntry.write(*OutputBuffer);
OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
ProducedBinaries[count] = std::move(OutputBuffer);
}, count);
count++;