Repository navigation
Conversation
https://reviews.llvm.org/D116366 added `dontNeedIfMmap` for lld/ELF LTO memory saving (used on read-only maps) https://reviews.llvm.org/D116367. D119856 (b66b324) switched to posix_madvise to fix the Illumos build, which made `dontNeedIfMmap` a no-op on Linux, both glibc and musl. (glibc and musl return 0 for POSIX_MADV_DONTNEED without calling `madvise`, because POSIX requires posix_madvise not to change the semantics of memory access, while Linux's MADV_DONTNEED discards data (which is acceptable for the read-only map use case). Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB read-only file mapping stays resident after madvise or posix_madvise; only the next access is slower. Call madvise on Linux only, restoring the RSS reduction. Make dontNeedIfMmap a no-op elsewhere.
|
@llvm/pr-subscribers-llvm-support Author: Fangrui Song (MaskRay) Changeshttps://reviews.llvm.org/D116366 added (glibc and musl return 0 for POSIX_MADV_DONTNEED without calling Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB Call madvise on Linux only, restoring the RSS reduction. Make Full diff: https://github.com/llvm/llvm-project/pull/240071.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h
index ea911681bd7760..2811eed86b7c01 100644
--- a/llvm/include/llvm/Support/MemoryBuffer.h
+++ b/llvm/include/llvm/Support/MemoryBuffer.h
@@ -69,7 +69,7 @@ class LLVM_ABI MemoryBuffer {
/// For read-only MemoryBuffer_MMap, mark the buffer as unused in the near
/// future and the kernel can free resources associated with it. Further
/// access is supported but may be expensive. This calls
- /// madvise(MADV_DONTNEED) on read-only file mappings on *NIX systems. This
+ /// madvise(MADV_DONTNEED) on read-only file mappings on Linux. This
/// function should not be called on a writable buffer.
virtual void dontNeedIfMmap() {}
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 202f7338ca6fa5..5fee0a494f6fd3 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -924,11 +924,10 @@ void mapped_file_region::dontNeedImpl() {
assert(Mode == mapped_file_region::readonly);
if (!Mapping)
return;
-#if defined(__MVS__) || defined(_AIX)
- // If we don't have madvise, or it isn't beneficial, treat this as a no-op.
-#elif defined(POSIX_MADV_DONTNEED)
- ::posix_madvise(Mapping, Size, POSIX_MADV_DONTNEED);
-#else
+#ifdef __linux__
+ // Linux's MADV_DONTNEED unmaps the pages and reduces RSS. Other systems
+ // treat it as a hint (macOS keeps the pages resident), and glibc and musl's
+ // posix_madvise ignore POSIX_MADV_DONTNEED.
::madvise(Mapping, Size, MADV_DONTNEED);
#endif
}
|
https://reviews.llvm.org/D116366 added
dontNeedIfMmapfor lld/ELF LTOmemory saving (used on read-only maps) https://reviews.llvm.org/D116367.
D119856 (b66b324) switched to posix_madvise to fix the Illumos
build, which made
dontNeedIfMmapa no-op on Linux, both glibc andmusl.
(glibc and musl return 0 for POSIX_MADV_DONTNEED without calling
madvise, because POSIX requires posix_madvise not to change thesemantics of memory access, while Linux's MADV_DONTNEED discards data
(which is acceptable for the read-only map use case).
Other systems treat MADV_DONTNEED as a hint. On macOS 26, a 512 MiB
read-only file mapping stays resident after madvise or posix_madvise;
only the next access is slower.
Call madvise on Linux only, restoring the RSS reduction. Make
dontNeedIfMmap a no-op elsewhere.