Summary
We propose an opt-in Clang feature, -fenable-readonly-thp, that implements iTHP (instruction Transparent Huge Pages) at compile and link time.
The driver arranges 2 MiB (or larger) segment alignment at link time, links a small startup object, and at process start calls madvise(MADV_COLLAPSE) on the executable text range so the kernel can promote default 4 KiB page mappings to huge pages.
A prototype patch is available; we welcome feedback on this RFC before posting the patch series to Phabricator.
Motivation
Large binaries with big instruction footprints can be frontend-bound due to iTLB pressure from 4 KiB text mappings. Linux can promote eligible executable .text regions to 2 MiB pages when alignment requirements are met and the process opts in via madvise().
Today this requires manual linker scripts and custom startup code. A compiler-managed, opt-in path makes the optimization practical for production builds.
We measured performance uplift with SPEC CPU 2026, Geekbench, and other benchmarks (CPython, database/server workloads, micro-architectural tests). Gains are workload-dependent—strongest on large .text workloads such as interpreters, simulators, and compiler-like binaries; some workloads are unchanged. We did not see broad regressions in our testing.
Kernel requirements
| Kernel version |
iTHP support |
| Linux 6.1+ (minimum) |
iTHP works on tmpfs-backed executables only (MADV_COLLAPSE, shmem THP) |
| Linux 7.2+ |
iTHP works on both tmpfs and non-tmpfs (disk-backed) executables |
Required system settings for iTHP:
/sys/kernel/mm/transparent_hugepage/enabled set to madvise or always
CONFIG_READ_ONLY_THP_FOR_FS=y in the kernel (required for non-tmpfs / file-backed THP on Linux 7.2+)
Below Linux 6.1, or when THP is disabled, behavior falls back to normal 4 KiB mappings.
Runtime behavior
With MADV_COLLAPSE, the kernel is asked to collapse eligible 4 KiB pages into huge pages immediately when madvise() is invoked on an aligned range. This differs from hint-based approaches (e.g. MADV_HUGEPAGE) where the kernel may defer promotion to a page fault or khugepaged. Promotion is not guaranteed if kernel preconditions are not met.
When the feature is active on a supported kernel, the resulting THP mappings can be observed in /proc/<pid>/smaps:
| smaps field |
Mapping type |
| ShmemPmdMapped |
shmem / tmpfs-backed executables (Linux 6.1+) |
| FilePmdMapped |
non-tmpfs / disk-backed executables (Linux 7.2+) |
Proposed interface
This proposal introduces two new Clang driver options:
-fenable-readonly-thp # default 2 MiB segment alignment
-fenable-readonly-thp=<size> # 2M, 4M, 8M, 16M, 32M, 64M, 128M, 256M
Target: x86-64 Linux ELF (initial scope)
Linkers: LLD or GNU BFD ld
Comparison with BOLT hugify
Both approaches aim to improve iTLB performance, but differ in scope, timing, and mechanism.
|
-fenable-readonly-thp |
BOLT -hugify |
| When |
Compile/link time |
Post-link (BOLT is a post-link optimizer) |
| Profile |
Not required |
Profile-driven: instrument binary, collect profile, re-link with -hugify |
| Scope |
Entire executable .text segment |
Hot code only, selected from profile |
madvise flag |
MADV_COLLAPSE — requests immediate collapse at call time on eligible ranges |
MADV_HUGEPAGE — hint only; promotion not guaranteed or immediate |
These two are complementary: BOLT targets profile-identified hot regions after link; this proposal provides a general compile-time path for whole-text promotion without instrumentation or a post-link pass.
Trade-offs
- Binary size: Padding to alignment boundaries increases
.text size; the feature remains opt-in.
- Startup cost: A one-time
madvise() during process startup; overhead is typically small.
- Kernel dependency: Requires Linux 6.1+ (tmpfs only); non-tmpfs support requires Linux 7.2+.
- Scope: Initial support is x86-64 Linux ELF with LLD or GNU BFD ld; extension to other architectures, object file formats, and linkers may follow.