Link-Time Optimization & Its Cost
Normally the compiler optimises each TU in isolation: a function defined in math.cpp can't be inlined into main.cpp, because when main.cpp is compiled, its body is unknown. Link-time optimization (LTO) changes that. With -flto, the compiler writes its intermediate representation into the object files instead of (or next to) machine code, and the real optimisation and code generation happen at link time, across the whole program.
Faster, smaller binaries at run time; a very different build at build time. This lesson looks at what changes in the build.
What an LTO object contains
mkdir -p lto && cd lto
printf 'int square(int x) { return x * x; }\n' > square.c
printf 'int square(int);\nint main(void) { return square(3) - 9; }\n' > main.c
gcc -O2 -c square.c -o square.o
gcc -O2 -flto -c square.c -o square-lto.o
echo "--- functions with machine code:"
for o in square.o square-lto.o; do echo "$o: $(objdump -d $o | grep -c '^[0-9a-f]* <')"; done
echo "--- GCC's LTO sections in square-lto.o: $(readelf -SW square-lto.o | grep -c '\.gnu\.lto_')"
gcc -O2 -flto -c main.c -o main-lto.o
gcc -O2 -flto main-lto.o square-lto.o -o app-lto
gcc -O2 main.c square.o -o app
echo "--- main() without LTO:"
objdump -d app | awk '/<main>:/,/^$/' | grep -c call
echo "--- main() with LTO:"
objdump -d app-lto | awk '/<main>:/,/^$/' | sed -n '2,4p' | awk -F'\t' '{print $3}'--- functions with machine code:
square.o: 1
square-lto.o: 0
--- GCC's LTO sections in square-lto.o: 15
--- main() without LTO:
1
--- main() with LTO:
endbr64
xor %eax,%eax
ret
- The normal object has one compiled function. The LTO object has none: just GCC's GIMPLE in
.gnu.lto_*sections. The code is generated later, by the linker plugin (lto1). - Without LTO,
maincallssquare. With LTO,square(3) - 9was inlined across files and folded toreturn 0(xor eax, eax; ret).
Clang's equivalent writes LLVM bitcode: clang -flto -c x.c produces a file that file reports as LLVM IR bitcode.
Where the time moves
| Without LTO | Full LTO (-flto) |
|
|---|---|---|
| Compile step | parse + optimise + codegen | parse + some optimisation, write IR (faster) |
| Link step | combine machine code (fast) | optimise and generate code for the whole program |
Change one .cpp |
recompile it, quick relink | recompile it, then the whole LTO link again |
| Parallelism | many compiles | one big link (partly parallel, see below) |
| Caching | ccache caches compile output | the expensive part is the link, which ccache doesn't cache |
In other words, LTO moves work from the parallel, cacheable, incremental compile step to the serial, uncacheable link step: exactly the part that sits on the critical path. A 2-second link can become a 2-minute link.
Making LTO affordable
- Parallel LTO. GCC splits the link-time work into partitions:
-flto=autouses the jobserver or the number of CPUs,-flto=8eight jobs. Plain-fltouses a running GNU make's jobserver if it finds one; otherwise GCC runs the partitions one after another and warnsusing serial compilation of N LTRANS jobs. - ThinLTO (Clang/LLVM,
-flto=thin): each module is optimised separately with a small global summary, so the link-time work is parallel and incremental. With lld,-Wl,--thinlto-cache-dir=DIRcaches per-module results between links:
mkdir -p thin && cd thin
printf 'int square(int x) { return x * x; }\n' > square.c
printf 'int cube(int x) { return x * x * x; }\n' > cube.c
printf 'int square(int); int cube(int);\nint main(void) { return square(2) + cube(1) - 5; }\n' > main.c
build() {
for f in main square cube; do clang -O2 -flto=thin -c $f.c -o $f.o; done
clang -O2 -flto=thin -fuse-ld=lld -Wl,--thinlto-cache-dir=lto-cache main.o square.o cube.o -o app
}
build && ./app && echo "first link: $(ls lto-cache | wc -l) cache entries"
build && echo "same inputs: $(ls lto-cache | wc -l) cache entries (all reused)"
printf 'int cube(int x) { return x * x * x + 0 * x; }\nint unused(void) { return 7; }\n' > cube.c
build && echo "cube.c edited: $(ls lto-cache | wc -l) cache entries"first link: 4 cache entries
same inputs: 4 cache entries (all reused)
cube.c edited: 6 cache entries
The relink after the edit created new entries only for the modules whose optimised result could change; the rest came from the cache. (The cache is pruned by policy, --thinlto-cache-policy, so it doesn't grow forever.)
- CMake:
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)or theINTERPROCEDURAL_OPTIMIZATIONtarget property, checked first withinclude(CheckIPOSupported)/check_ipo_supported(). CMake picks the right flags and thegcc-ar/llvm-ararchivers that understand LTO objects (plainarmakes archives the linker can't read with LTO). - Turn it on where it pays. Typical setup: no LTO in Debug and day-to-day builds, ThinLTO or parallel LTO in CI release builds, full LTO (plus PGO) for the final shipping build.
Test yourself
What does a GCC -flto object file mainly contain?
- Optimised machine code
- GIMPLE intermediate representation in .gnu.lto_ sections
- Preprocessed source
- Debug info only
Why does LTO often lengthen the critical path?
- Compiles get slower
- Optimisation and codegen move into the final link, a single serial step that every change re-runs
- It disables parallel builds
- It needs a new CMake
What makes ThinLTO incremental?
- It skips optimisation
- Per-module optimisation with a summary, plus a cache of module results between links
- It uses ccache
- It links only changed objects