TExcelTAD

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

bash · try it yourself
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, main calls square. With LTO, square(3) - 9 was inlined across files and folded to return 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=auto uses the jobserver or the number of CPUs, -flto=8 eight jobs. Plain -flto uses a running GNU make's jobserver if it finds one; otherwise GCC runs the partitions one after another and warns using 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=DIR caches per-module results between links:
bash · try it yourself
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 the INTERPROCEDURAL_OPTIMIZATION target property, checked first with include(CheckIPOSupported) / check_ipo_supported(). CMake picks the right flags and the gcc-ar/llvm-ar archivers that understand LTO objects (plain ar makes 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?

  1. Optimised machine code
  2. GIMPLE intermediate representation in .gnu.lto_ sections
  3. Preprocessed source
  4. Debug info only

Why does LTO often lengthen the critical path?

  1. Compiles get slower
  2. Optimisation and codegen move into the final link, a single serial step that every change re-runs
  3. It disables parallel builds
  4. It needs a new CMake

What makes ThinLTO incremental?

  1. It skips optimisation
  2. Per-module optimisation with a summary, plus a cache of module results between links
  3. It uses ccache
  4. It links only changed objects