One copy is enough. Deduplicating source locations in Clang modules
1 / 21
→ advance   ← back

One copy is enough

Deduplicating source locations in Clang’s modular builds

the source-location address space
shared.h
mod1.h
shared.h ↩ reuses the first
mod2.h
02³¹
Ayokunle Amodu
C++ Alliance Fellowship, final presentation
Groundwork · 1 of 4

A source location is an address

Clang gives every byte of source a number: one offset into a single shared space. A source location is just that integer.

in shared.h, one line:   int add(int a, int b);

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.

Groundwork · 2 of 4

A module is compiled once into a PCM

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.

shared.h
types.h
util.h
parse once →
M.pcm
serialized AST on disk
→ load, no reparse

This is the shift large C++ codebases are making to save compilation time.

Groundwork · 3 of 4

An entry is a file’s span in the space

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.

the whole address space
module M
0 2³¹
1,420
shared.h
types.h
util.h
1,000
5,890
inside module M, each file is one entry
the entry, one per file
SLocEntry {
  file: shared.h
  begins at: 1,000
  length: 4,890
}
a location is a point inside it
1,420
many per file. one entry claims the span.
Groundwork · 4 of 4

The space is finite, and it fills

One 32-bit space for the whole build, about two billion addresses and no more. Two things fill it.

macroseach expansion spends fresh addresses (where the limit first drew attention).
modular buildseach 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.

Why modules strain it

The same header, copied into every module

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.

module A
#include "shared.h"
int a_fn();
module B
#include "shared.h"
int b_fn();
shared.h · textually included by both
local, this file
module A
module B
tu.cc + headers
free
shared.h
others
shared.h
others
02³¹

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.

Why modules strain it · the cause

It only happens when a shared header is not its own module

shared.h · built once as its own module, imported by both
A.pcm
shared.h copy
others
B.pcm
shared.h copy
others

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.

Two places to intervene

The duplicate can be stopped at either end

Goal: catch the duplicate.

Attempt 1 · reader side
at load

Collapse a copy onto the first as it arrives.

Attempt 2 · writer side
at write

Never write a copy that already exists.

The one thing both attempts rewrite

A stored location is a pair

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.

module
self
offset
1,420

Our token in module A. module = self means “my own copy”, and 1,420 is where it sits in that copy.

Attempt 1 · at load

Fix it when the modules meet

Duplicates only meet when modules are loaded together, so catch them there.

loaded-files map
shared.h→module A, offset 1,000
types.h→module A, offset 5,890
1As modules load, record each file’s first copy in a map.
2Loading B, shared.h is already in the map from A, so give B’s copy no new space.
3Remap B’s shared.h locations to land in A’s copy.

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.

Attempt 1 · what reuse forced

One clean base becomes a piecewise map

already loaded, module A
shared.h
mod1
module B’s copy collapses onto A’s, and everything after it shifts back
mod2
shared.h
diags.h
free

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.

forwardon-disk offset→where it lives nowto resolve on load
inversewhere it lives now→on-disk offsetto write the module back out
Attempt 1 · the map, by example

One base becomes three

Before the collapse, all of B resolved with a single base. Add it to any offset and you have the location.

before   location = base + offset   one add, the whole module

After B’s shared.h folds onto A’s, that one base splits by segment.

before shared.h
+0
unchanged
shared.h
−6,000
onto A’s copy, 7,000 → 1,000
after shared.h
−4,890
back by shared.h’s width

One number became three rows, and you keep the reverse of each to write the module back out. That is the piecewise map.

Attempt 1 · measured

It works, and it costs too much

Measured on ROOT, the reader side reclaims a third of the address space. It just pays for it everywhere else.

address space
−33%
space won back
module writing
+6.1%
retired instructions
peak memory
+5.1%
more to hold at load
PCM size
~0%
nothing saved on disk

Space back, everything else worse.

Attempt 1 · the cost that killed it

To find the duplicates, you scan everything

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 · writer side

Prevent the copy instead of repairing it

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.

drop the SLoc entries  // never referenced in the AST
keep a file record  // for dependency scanning

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.

Attempt 2 · the steps

Writing module B, step by step

A.pcm, already written, imported by B, owns shared.h
shared.h
mod1
B.pcm, being written now
mod2
shared.h
diags.h
1Check the map of files our imports already own. shared.h is there, A owns it
2Write no SLoc entries for shared.h. the copy never exists
3Point the pair at A’s copy and re-base the offset into it. same byte, A’s numbering

Loading B then allocates nothing for shared.h and follows the pair to A. Nothing eager, nothing to undo.

Attempt 2 · the redirect, up close

What the pair becomes

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.

A’s copy, the one we keep
1,420
1,0005,890
B’s copy, dropped
7,420
7,00011,890
base shift = 7,000 − 1,000 = 6,000
7,420 − 6,000 = 1,420 → { module: A, offset: 1,420 }

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.

Attempt 2 · how far it reaches

It reaches along the import chain

The writer skips a copy only if the owner is loaded as it writes, which in a normal build it almost always is.

the chain: A imports B imports C
C.pcm · owns shared.h
shared.h
others
B.pcm · imports C
→ C
others
A.pcm · imports B
→ C
others
one copy · the writer prevents the rest

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.

Results · the win

The address space comes back

Measured on ROOT: 88 dictionaries, runtime C++ modules enabled.

baseline
75.33 MB
patched
61.02 MB

Loaded source-location space down 19%, the thing those builds were running out of.

biggest saversc++config.h  4.75 MBfeatures.h  2.95 MB
sys/cdefs.h  2.72 MBassert.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.

Results · what held

And the fundamentals held

Attempt 1 is on top, already measured. The writer side, below, reclaims less of the space and charges nothing to do it.

address space
module writing
peak memory
PCM size
reader sideattempt 1
−33%
+6.1%
+5.1%
~0%
writer sideshipped
−19%
+0.006%
−3.7%
−4.4%

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.

A note on tooling

How I used AI on this

Seeing the invisible
In school the compiler was taught in slides and diagrams, so I could always see what it was doing. Here, even after reading the code, the picture would not come, so I had it drawn out until I could see it.
Tracing the patches
It pulled up the related patches and how module maps got to where they are. That is how I found the change mine builds on.
A sounding board
Brainstorming out loud. We would float ideas and push back on each other, bouncing until something held.
Prototyping fast
Standing up a rough version quickly to see if an idea holds, before committing to it.
Testing, and never the first answer
I would describe a case and it would write the test, with variants I never think of. A test is how I found out if either of us was talking rubbish. Always validate!

One copy is enough

ProblemEvery module carries its own copy of shared headers, and the 32-bit source-location space runs out.
ApproachNever write the second copy. When writing a module, redirect a shared file’s locations to the module that already owns them.
Result−19% loaded location space on ROOT, with compile time and memory steady and one small, understood scan cost.
StatusPR #209795, approved. One cross-module correctness follow-up still under review.

Thank you. Questions welcome.