Register Spilling: When Fast Registers Run Out


Modern CPUs operate on data stored in registers, tiny storage locations built directly into the processor core. These registers are orders of magnitude faster than even L1 cache, making them the primary workspace for all computation. But there’s a constraint: most architectures provide only a small number of general-purpose registers. x86-64 has 16, ARM64 has 31, and RISC-V provides 32. When a program needs to track more live values than available registers, the compiler must “spill” excess values to memory.

Register spilling happens during compilation when the register allocator determines it cannot fit all the variables a function needs into the physical registers available. The compiler inserts store instructions to write values from registers to the stack, and later inserts load instructions to bring them back when needed. This process is invisible in source code but directly impacts runtime performance.

Why Spilling Matters

The performance gap between registers and memory is substantial. A register access completes in a single cycle. An L1 cache hit takes around 4 cycles, L2 might be 12 cycles, and L3 can exceed 40 cycles. If a spilled value isn’t in cache at all, you’re looking at hundreds of cycles to fetch from main memory. Even in the best case where spills hit in L1, you’ve multiplied memory access time by 4x.

This overhead compounds in tight loops. A value that gets spilled and reloaded on every iteration transforms a handful of fast register operations into repeated memory round-trips. The loop might execute millions of times, amplifying the cost of each spill. Computation-heavy code like numerical algorithms, compression routines, and cryptographic functions are particularly sensitive because they naturally require many simultaneous values in flight.

What Triggers Spilling

Complex expressions with many temporary values are common culprits. Consider a function computing a polynomial with a dozen terms, each requiring intermediate results. Or image processing code manipulating multiple pixel channels with various coefficients. Each temporary variable is a candidate for a register, and when you exceed the available count, spilling begins.

Function calls add pressure because calling conventions typically require saving caller-saved registers to the stack. If your function was already using most registers, a call forces spills. Inlining can help by eliminating the call overhead, but it can also hurt by merging two functions’ register needs into one scope.

Loop unrolling is a double-edged optimization. Unrolling exposes more instruction-level parallelism and reduces branch overhead, but it also increases the number of live variables at any given point. An unrolled loop processing four iterations simultaneously needs four times the registers. Past a certain unroll factor, the register pressure causes spilling that negates the unrolling benefit.

Compiler Register Allocation

Compilers use sophisticated algorithms to minimize spilling. Graph coloring is a classic approach where variables are nodes and conflicts (variables live simultaneously) are edges. The compiler tries to color the graph with as many colors as there are registers. Variables that can’t be colored get spilled.

Modern compilers employ heuristics to choose which variables to spill. Frequently accessed variables in inner loops are prioritized for registers. Values used only once might be recomputed instead of spilled. The allocator may also split live ranges, keeping a variable in a register for part of its lifetime and spilling it during less critical sections.

Profile-guided optimization can improve these decisions. By knowing which code paths execute most frequently, the compiler can make more informed spill choices, keeping hot-path variables in registers while spilling values only used in cold paths.

Reducing Register Pressure

Writing code that minimizes register pressure requires understanding what creates it. Breaking large expressions into smaller functions can help by creating register allocation boundaries, though inlining can collapse those boundaries. Reducing the number of simultaneously live variables is more reliable: compute and consume values promptly rather than accumulating many temporaries before using them.

Loop structure matters. Splitting complex loop bodies into multiple passes can reduce per-iteration register needs. This trades off increased memory bandwidth for reduced spilling, which may or may not be beneficial depending on the specific bottleneck.

Architecture awareness plays a role too. x86-64’s 16 registers feel limiting compared to ARM64’s 31 or RISC-V’s 32. Code that spills heavily on x86 might compile cleanly on ARM. SIMD registers (XMM, YMM, ZMM on x86, or NEON on ARM) provide additional register space but only for specific data types and operations.

Observing Spills in Practice

Compiler output reveals spilling. Disassembly showing frequent stack pointer offsets (like mov [rsp+16], rax on x86) indicates stack spills. Profilers can attribute cycles to these memory operations. Comparing optimized builds at different optimization levels sometimes exposes the tradeoff: more aggressive optimization creates register pressure through inlining and unrolling.

Register spilling represents a fundamental constraint in CPU architecture. Registers are expensive in chip area and power, limiting how many can be provided. Compilers are quite good at managing this scarcity, but the physics of the problem remains. Understanding when and why spilling occurs helps in writing code that works with the compiler’s allocator rather than against it.