HeadlinesBriefing HeadlinesBriefing.com

Rat's Improved Register Allocator Cuts Lines, Makes Better Code

Hacker News •
×

October 7th, 2026 — rat, a small compiler backend with a semi-working C99 frontend, uses an x86-64 code generator that maps virtual registers (vregs) to physical registers or stack slots. The old linear-scan allocator grew to 1392 lines, prompting a rewrite of a priority bin-packing allocator in just 584 lines. This new allocator visits live ranges by importance and places each in the first fitting register, similar to LLVM's greedy allocator but simpler.

A value is live from its write to its last read; two values share a register only when never simultaneously live. When too many values are live, some spill to memory. The calling convention requires preserving callee-saved registers (rbx rbp r12-r15) while caller-saved registers may be clobbered.

As shown, the function h keeps y live across a call without explicit allocator logic for callee-saved placement. The allocator runs five steps: live ranges, fixed registers, coalescing, picking registers, and spilling. Each bundle keeps its register or stack slot for its full lifetime, never evicting or splitting ranges.

Source: Hacker News · Summarized by HeadlinesBriefing