Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 8251e8c

Browse files
mehdi_aminijoker-eph
authored andcommitted
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 <[email protected]> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269682 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b2fcf6d commit 8251e8c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ class ModuleCacheEntry {
514514
}
515515

516516
// Cache the Produced object file
517-
void write(MemoryBufferRef OutputBuffer) {
517+
std::unique_ptr<MemoryBuffer>
518+
write(std::unique_ptr<MemoryBuffer> OutputBuffer) {
518519
if (EntryPath.empty())
519-
return;
520+
return OutputBuffer;
520521

521522
// Write to a temporary to avoid race condition
522523
SmallString<128> TempFilename;
@@ -529,7 +530,7 @@ class ModuleCacheEntry {
529530
}
530531
{
531532
raw_fd_ostream OS(TempFD, /* ShouldClose */ true);
532-
OS << OutputBuffer.getBuffer();
533+
OS << OutputBuffer->getBuffer();
533534
}
534535
// Rename to final destination (hopefully race condition won't matter here)
535536
EC = sys::fs::rename(TempFilename, EntryPath);
@@ -539,8 +540,16 @@ class ModuleCacheEntry {
539540
if (EC)
540541
report_fatal_error(Twine("Failed to open ") + EntryPath +
541542
" to save cached entry\n");
542-
OS << OutputBuffer.getBuffer();
543+
OS << OutputBuffer->getBuffer();
544+
}
545+
auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
546+
if (auto EC = ReloadedBufferOrErr.getError()) {
547+
// FIXME diagnose
548+
errs() << "error: can't reload cached file '" << EntryPath
549+
<< "': " << EC.message() << "\n";
550+
return OutputBuffer;
543551
}
552+
return std::move(*ReloadedBufferOrErr);
544553
}
545554
};
546555

@@ -926,7 +935,7 @@ void ThinLTOCodeGenerator::run() {
926935
ExportList, GUIDPreservedSymbols, ResolvedODR, CacheOptions,
927936
DisableCodeGen, SaveTempsDir, count);
928937

929-
CacheEntry.write(*OutputBuffer);
938+
OutputBuffer = CacheEntry.write(std::move(OutputBuffer));
930939
ProducedBinaries[count] = std::move(OutputBuffer);
931940
}, count);
932941
count++;

0 commit comments

Comments
 (0)