[ELF][LTO] Call madvise(MADV_DONTNEED) on MemoryBuffer instances
authorFangrui Song <i@maskray.me>
Thu, 30 Dec 2021 19:36:57 +0000 (11:36 -0800)
committerFangrui Song <i@maskray.me>
Thu, 30 Dec 2021 19:36:58 +0000 (11:36 -0800)
commita96fe1bf3b320d62f1564ded5f7259eef1869cf9
treeb62ee747b7651586be4429dd1f9675d9788a375b
parenta6a583dae40485cacfac56811e6d9131bac6ca74
[ELF][LTO] Call madvise(MADV_DONTNEED) on MemoryBuffer instances

@tejohnson noticed that freeing MemoryBuffer instances right before
`lto->compile` can save RSS, likely because the memory can be reused by
LTO indexing (e.g. ThinLTO import/export lists).).

For ELFFileBase instances, symbol and section names are backed by MemoryBuffer,
so destroying MemoryBuffer would make some infrequent passes (parseSymbolVersion,
reportBackrefs) crash and make debugging difficult.
For a BitcodeFile, its content is completely unused, but destroying its
MemoryBuffer makes the buffer identifier inaccessible and may introduce
constraints for future changes.
This patch leverages madvise(MADV_DONTNEED) which achieves the major gain
without the latent issues.

`Maximum resident set size (kbytes): ` for a large --thinlto-index-only link:

* current behavior: 10146104KiB
* destroy MemoryBuffer instances: 8555240KiB
* madvise(MADV_DONTNEED) just bitcodeFiles  and lazyBitcodeFiles: 8737372KiB
* madvise(MADV_DONTNEED) all MemoryBuffers: 8739796KiB  (16% decrease)

Depends on D116366

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D116367
lld/ELF/Driver.cpp
lld/ELF/Driver.h
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h