Deduplicating source locations in Clang’s modular builds
Clang gives every byte of source a number: one offset into a single shared space. A source location is just that integer.
Even the space gets one.
Clang passes this number around and resolves it back to a file, line, and column only when it needs to point at your code.
A textual #include is re-parsed in every build that sees it.
A module is parsed once and saved as a PCM, a serialized AST, then loaded as-is.
This is the shift large C++ codebases are making to save compilation time.
The whole space is a single range of addresses. Load a module and it takes a region of it. Inside that region, each file the module brought is one SLoc entry, a span.
One 32-bit space for the whole build, about two billion addresses and no more. Two things fill it.
| macros | each expansion spends fresh addresses (where the limit first drew attention). |
|---|---|
| modular builds | each loaded PCM carries its own copy of the files it saw, so a shared header is paid for again and again. |
fatal error: ran out of source locations
People hit this on real builds, reported on LLVM Discourse and in GitHub issues. When the space runs out, the build stops.
This patch only takes on the modular side.
Module A and module B are built separately and self-contained. Neither makes shared.h a module, they each just #include it, so each writer enters it and records its own entry.
Load both and the space holds a copy per module. Two modules, two copies, N modules, N copies. This is the main source of the waste.
Included textually, each PCM seals its own copy inside. two, and counting
The fix is a bottom-up layout. Make shared.h a module and import it, so both share the one copy. one, shared
So why isn’t every shared header a module? The ones that hurt are system and third-party headers you don’t own, plus config headers that must stay textual. Teams modularize what they can and leave the rest, which is where the duplication lives.
Goal: catch the duplicate.
Collapse a copy onto the first as it arrives.
Never write a copy that already exists.
On disk, a source location is not a raw number. It is a pair: which module owns the file, and the offset into that module’s copy.
Our token in module A. module = self means “my own copy”, and 1,420 is where it sits in that copy.
Duplicates only meet when modules are loaded together, so catch them there.
Attempt 1 keeps the pair at self and maps the offset elsewhere at load, nothing on disk is rewritten. The catch is that B’s file was written assuming its copy owns a clean, unbroken range.
B’s shared.h collapses onto A’s, so every location that pointed into it has to move.
Everything after it shifts back to fill the gap. Now a module’s offsets no longer convert by one fixed base. The map is piecewise, and you have to keep it both ways.
Before the collapse, all of B resolved with a single base. Add it to any offset and you have the location.
After B’s shared.h folds onto A’s, that one base splits by segment.
One number became three rows, and you keep the reverse of each to write the module back out. That is the piecewise map.
Measured on ROOT, the reader side reclaims a third of the address space. It just pays for it everywhere else.
Space back, everything else worse.
Compile time and peak memory regressed.
The loaded region is reserved up front, before any entry is placed. You cannot reserve, then shrink. So the duplicates must be known before allocating, which means walking every SLoc entry of every module at load.
Clang works the opposite way. An entry is built only when something touches it, and most never are. The scan throws that away, on every module, whether or not anything dedups.
And it is not just the scan. The load path carries the lookup map, the forward and inverse remap, and four per-module tables, all live at once.
Attempt 2 never writes the second copy at all. Each shared location just repoints to A’s copy instead. No gap opens, so nothing to remap, scan, or undo.
Clang already does a version of this. A module map kept only to find textual headers is never pointed at in the AST, so the writer drops its source-location space, keeping only the file record.
Generalize it from module maps to the headers themselves. When an import already owns the file, drop its entries and point the locations at that owner.
Loading B then allocates nothing for shared.h and follows the pair to A. Nothing eager, nothing to undo.
Our token is 420 into shared.h. A placed the file at 1,000, B would place it at 7,000. Same point, two bases.
Same byte as before, now counted from A’s start, 1,420 not 7,420. The reader follows the pair straight to A, no table and no fixup pass.
The writer skips a copy only if the owner is loaded as it writes, which in a normal build it almost always is.
Modules are built in dependency order, so a shared header is almost always already loaded when a module that depends on it is written. That common case is what the writer catches.
Measured on ROOT: 88 dictionaries, runtime C++ modules enabled.
Loaded source-location space down 19%, the thing those builds were running out of.
| biggest savers | c++config.h 4.75 MB | features.h 2.95 MB |
|---|---|---|
| sys/cdefs.h 2.72 MB | assert.h 0.93 MB |
These are the system headers from before, the ones with no module maps to turn them into modules, so every module carried its own copy. That is the duplication, now reclaimed.
Attempt 1 is on top, already measured. The writer side, below, reclaims less of the space and charges nothing to do it.
The reader reclaimed more because at load it sees every module at once, independent ones too, not just the chain the writer can reach while it writes.
Thank you. Questions welcome.