[PATCH] D116367: [ELF][LTO] Call madvise(MADV_DONTNEED) on BitcodeFile buffers

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 29 11:52:53 PST 2021


MaskRay updated this revision to Diff 396562.
MaskRay retitled this revision from "[ELF][LTO] --thinlto-index-only: call madvise(MADV_DONTNEED)" to "[ELF][LTO] Call madvise(MADV_DONTNEED) on BitcodeFile buffers".
MaskRay added a comment.

decrease memory for in-process LTO as well.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116367/new/

https://reviews.llvm.org/D116367

Files:
  lld/ELF/Driver.cpp
  lld/ELF/InputFiles.cpp
  lld/ELF/InputFiles.h


Index: lld/ELF/InputFiles.h
===================================================================
--- lld/ELF/InputFiles.h
+++ lld/ELF/InputFiles.h
@@ -407,6 +407,7 @@
 
 std::string replaceThinLTOSuffix(StringRef path);
 
+extern SmallVector<std::unique_ptr<MemoryBuffer>> memoryBuffers;
 extern SmallVector<ArchiveFile *, 0> archiveFiles;
 extern SmallVector<BinaryFile *, 0> binaryFiles;
 extern SmallVector<BitcodeFile *, 0> bitcodeFiles;
Index: lld/ELF/InputFiles.cpp
===================================================================
--- lld/ELF/InputFiles.cpp
+++ lld/ELF/InputFiles.cpp
@@ -43,6 +43,7 @@
 bool InputFile::isInGroup;
 uint32_t InputFile::nextGroupId;
 
+SmallVector<std::unique_ptr<MemoryBuffer>> elf::memoryBuffers;
 SmallVector<ArchiveFile *, 0> elf::archiveFiles;
 SmallVector<BinaryFile *, 0> elf::binaryFiles;
 SmallVector<BitcodeFile *, 0> elf::bitcodeFiles;
@@ -122,9 +123,8 @@
     return None;
   }
 
-  std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
-  MemoryBufferRef mbref = mb->getMemBufferRef();
-  make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership
+  MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();
+  memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership
 
   if (tar)
     tar->append(relativeToRoot(path), mbref.getBuffer());
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -87,6 +87,7 @@
 
     inputSections.clear();
     outputSections.clear();
+    memoryBuffers.clear();
     archiveFiles.clear();
     binaryFiles.clear();
     bitcodeFiles.clear();
@@ -1987,6 +1988,23 @@
   return symtab->addSymbol(sym);
 }
 
+// MemoryBuffers backing BitcodeFiles are unused from now on. Mark input files
+// as MADV_DONTNEED so that these pages can be reused by the expensive thin
+// link, saving memory.
+//
+// With --thinlto-index-only, for ELFFileBase files, their MemoryBuffers are
+// nearly unused as well, but the saving is typically small because
+// in LTO links bitcode files typically dominate.
+static void markBuffersAsDontNeed() {
+  DenseSet<const char *> bufs;
+  for (BitcodeFile *file : elf::bitcodeFiles)
+    bufs.insert(file->mb.getBufferStart());
+
+  for (MemoryBuffer &mb : llvm::make_pointee_range(memoryBuffers))
+    if (bufs.count(mb.getBufferStart()))
+      mb.dontNeedIfMmap();
+}
+
 // This function is where all the optimizations of link-time
 // optimization takes place. When LTO is in use, some input files are
 // not in native object file format but in the LLVM bitcode format.
@@ -2001,6 +2019,9 @@
   for (BitcodeFile *file : bitcodeFiles)
     lto->add(*file);
 
+  if (!bitcodeFiles.empty())
+    markBuffersAsDontNeed();
+
   for (InputFile *file : lto->compile()) {
     auto *obj = cast<ObjFile<ELFT>>(file);
     obj->parse(/*ignoreComdats=*/true);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116367.396562.patch
Type: text/x-patch
Size: 2868 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211229/f7e26d85/attachment.bin>


More information about the llvm-commits mailing list