Deduplicating source locations in Clang’s modular builds
Clang encodes a source location as one 32-bit integer, an offset into a single address space. Every file the compiler enters is given a contiguous range in it, sized by the file.
Diagnostics, fix-its, debug info: everything that names a line resolves through this space. It stays fast because a location is just a number.
And it is finite. About 2 GB of addresses for every character of source the compilation ever sees.
The whole talk starts from this shape: two modules, built separately, each one textually including the same header.
Each module is a separate compiler invocation writing its own self-contained PCM. So each one enters shared.h itself, and serializes its own record of everything it saw.
One space serves the whole compilation. The current TU grows from the bottom; everything loaded from PCMs is carved from the top, down.
Alone, a TU barely dents the space: include guards enter a shared header once, and few TUs hold two billion characters.
But each PCM is self-contained, so each serialized its own entries for every header it included textually. Load N modules, and the same header is live N times at once.
fatal error: ran out of source locations
Reports of exactly this kept arriving from large module builds. The space runs out and the build stops.
The duplicates meet each other at load time. So catch them there: deduplicate on the reader side.
Easy to say. The hard part is what the files on disk already assume.
B’s copy is collapsed onto A’s, so every location that pointed into it must be rewritten…
…and everything after it shifts back. A forward map translates disk → memory, an inverse map must undo it when a module is re-serialized.
The inverse of a piecewise map is not one subtraction. Getting that wrong corrupted locations on ROOT, found the hard way.
To compact the space before allocating it, the loader must read every location entry of every loaded module, up front.
| Clang’s design | an entry is materialized only when something touches it |
|---|---|
| Reader-side dedup | all of them, for all modules, at startup |
The eager scan showed up exactly where you’d expect: slower interpreter startup and higher peak memory on ROOT, regressing the fundamentals the patch has no business touching.
Saved the space, lost the fundamentals.
| address space | reclaimed, the goal was met |
|---|---|
| PCM size | unchanged, a load-time change writes nothing new to disk |
| startup & peak memory | slower start, about +15 MB peak memory |
| scan | +1.58% |
| complexity carried | forward map, inverse map, eager scan of every loaded module |
The space came back, but the fundamentals every compiler change must protect did not hold.
Keep the goal, change the place.
Two files on disk, carrying identical entries for the same header.
Clang already does something close to this: when writing a PCM, it drops module-map files that don’t affect the build. That machinery came out of builds that were hitting exactly this problem.
A module map is the file that declares which headers form a module. When one is dropped, its locations are simply never written.
Nothing written ⇒ nothing to remap later.
So generalize it: extend the same machinery from module maps to the headers themselves. Move deduplication to the writer, and make sure the second copy never exists.
Serialized locations were already {module-file, offset} pairs (0 = “this module”). The patch just lets that pair point at the module that owns the entries. The reader resolves it directly. No translation table, no fixup pass.
Two integers per input file, a few bytes each, and the per-module scan they replaced was deleted outright.
The lookup map is built once over the loaded modules, work proportional to the imports. The alternative raised in review: serialize a query table into every PCM, like HeaderFileInfo, work proportional to the module being written.
Measured on ROOT: 88 dictionaries, runtime C++ modules enabled.
Loaded source-location space down 19%, the thing builds were running out of.
| largest single savers | c++config.h 4.75 MB | features.h 2.95 MB |
|---|---|---|
| sys/cdefs.h 2.72 MB | assert.h 0.93 MB |
Exactly the shared, non-modular system headers the exhaustion reports point at.
These two have to hold, no matter what a change buys elsewhere.
The reader-side version failed exactly this test. The writer-side version passes it, which is the whole reason it exists.
SDK vendors watch these closely, and everyone benefits when they stay flat.
The scan cost is small, understood to the instruction, and paid only where the feature actually fires.
Apple’s SDKs are modularized bottom-up, so they have little duplication left to reclaim. The savings show up where duplication actually exists, and nothing regresses where it doesn’t.
Thank you. Questions welcome.