LLVM Code Generator Cuts -O0 Build Work by 14%

An experimental LLVM code generator compiles unoptimized builds without ever creating a MachineFunction, and it cuts the work by double digits. In tests on the compile-time tracker, -O0 builds with debug info needed 14% fewer instructions on x86-64, and 21% fewer on AArch64. The LLVM code generator trades some output size for that speed: x86-64 code comes out about 10% larger, while AArch64 size stays roughly the same.

Most of the code was written with Claude Code. The developer says their own part was steering the agent and editing the write-up.

Why unoptimized builds still spend so long in the back end

At -O0, clang does little optimizing, so the back end takes a large share of the time. That share runs from 13% when compiling ScalarEvolution.cpp up to 39% for sqlite3. The cost isn’t concentrated in one place. It is split between FastISel and its SelectionDAG fallback, the fast register allocator, prologue and epilogue insertion, the remaining machine passes, pass manager overhead and MC lowering.

Even an empty function pays. Ask llc -O0 to compile a function that does nothing but return, and its pass listing shows 72 entries for the function pass manager once repeated analyses are counted.

How the direct approach works

The new pass, called DirectCodeGen, runs first in the -O0 codegen pipeline and is switched on with -direct-codegen. It turns LLVM IR straight into encoded machine code bytes inside a private buffer, together with fixups, CFI points, line rows, variable slots and a table of call sites. Shared state stays untouched until the function compiles cleanly, and the IR itself is left alone.

If it works, AsmPrinter writes out the function header, bytes and tables, and the pass manager is told to bypass everything else for that function. If it fails, the function goes through the regular pipeline and lands in the same object file. Two debugging flags help here: -direct-codegen-report lists the reason for every fallback, and -direct-codegen-abort makes a fallback fail the build.

The value model is simple. Static allocas get fixed frame slots. A value with a single use inside its block never leaves a register. Every other value gets stored to a slot of its own at the point it’s defined, while the register keeps a copy that can be discarded at no cost. The generator touches only caller-saved registers, which removes save and restore code around calls, and it always builds a frame pointer. Small patterns fold away: a compare feeding only a branch merges into that branch, and so does a load feeding only the next instruction.

The size of the change

The shared part of the pass is about 2.0K lines. The x86-64 target adds 1.8K, AArch64 adds 1.7K, and AsmPrinter changes account for roughly 0.4K. A third target would need to implement 35 pure virtual hooks covering frames, slots, constants, branches, calls and one hook per instruction class, plus its own calling convention.

The developer compares this with Anthropic’s CCC, a from-scratch compiler written with AI help in February 2026 in Rust. Counted like for like, CCC’s x86-64 and AArch64 code generators and their shared layer run to roughly 30K lines, where the LLVM experiment needs about 5.9K. CCC carries its own register allocator, peepholes and inline asm, with no fallback. The LLVM experiment sends those cases to the regular pipeline and keeps DWARF, exception tables and hardening instead.

A leaner rival exists. TPDE, a single-pass back end, runs between 1.9x (sqlite3) and 3.2x (tramp3d) fewer instructions than this experiment. The direct generator offers other things: it falls back one function at a time, writes into a shared object file, handles debug info and function attributes, and supports hardening and assembly text.

What skipping the pipeline costs

Skipping passes creates debts. Passes the new code never runs are responsible for stack protectors, KCFI, fentry, XRay, patchable functions and indirect branch tracking. Each feature has to be rebuilt or handed back to the regular pipeline. When the generator meets something it doesn’t recognize, it falls back, since a slow result is better than a wrong one.

A plain allowlist would make that expensive. Sanitizers and PGO each add a module flag, and a single new upstream attribute could force every C++ inline function to fall back. The answer is a switch over enum attributes that has no default case. A newly added LLVM attribute then breaks the build until someone files it under one of three headings: irrelevant to code generation, implemented here, or left to the regular pipeline.

Several features were rebuilt inside the direct path: the stack protector, ENDBR64 for IBT, PAC and BTI using the A key, and stack clash protection for frames that need no probe loop. Compiling with distro hardening flags, 99.6% to 99.7% of sqlite3 and tramp3d functions stay direct on x86-64, against 99.2% to 99.5% on AArch64. Others still fall back, among them -pg, XRay, HWASan, MTE, shadow call stack, KCFI and retpolines.

Across the single-source programs in the llvm-test-suite, plain -O0 and PGO-instrumented builds send 2.4% of x86-64 functions back to the old pipeline. AArch64 sends about 12%, chiefly because some load types aren’t handled yet.

Keeping assembly output honest

The assembly text from clang -S has to assemble into exactly what clang -c produces. Because the compiler may still fail midway and bytes are edited after encoding, it keeps each instruction with its offset and replays them on success. The test compares llc output piped through llvm-mc against direct object output, which forces the object path to pick the same encodings the assembler would, including jump sizes.

What is still unsolved

One gap remains. A feature switched on only by a command-line option or a TargetOptions field lacks anything like the attribute switch to force a decision, so it can slip past the gates unnoticed. Until that is closed, the experiment stays an experiment, though the 14% and 21% savings give LLVM developers a concrete number to weigh against its extra code.

Source: MaskRay

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.