Compiling C++ with -O0 -g is often the bottleneck for large projects, but a new experiment in LLVM infrastructure promises to change that. By introducing a direct code generator that compiles LLVM IR straight to machine code bytes, the project bypasses the traditional MachineFunction construction and the associated back-end passes. Early benchmarks show clang -O0 -g builds require 14% fewer instructions on x86-64 and 21% fewer on AArch64. This isn't just a theoretical win; it's a practical shift in how debug builds handle instruction selection and register allocation.

Bypassing the Traditional Back-End

The core innovation is a pass called DirectCodeGen, which sits at the front of the -O0 code generation pipeline. Instead of building a MachineFunction and running through FastISel, SelectionDAG, or RegAllocFast, this pass emits bytes directly into a private buffer. If the direct generation succeeds, the remaining passes are skipped entirely. If it failsβ€”due to unsupported constructs like atomics or complex inline assemblyβ€”the function falls back to the regular pipeline within the same object file. This per-function fallback ensures correctness while maximizing speed for the majority of functions.

Handling Debug Info Without Machine Functions

Skipping the standard pipeline creates a significant obligation: how do you generate accurate DWARF debug info without MachineInstrs? The solution involves creating a skeleton MachineFunction with placeholder instructions for each line row. This allows existing debug handlers to process the data as if the standard pipeline had run. The result is that .text sections remain identical whether debug info is enabled or not, a crucial invariant for reproducibility. However, this approach isn't without trade-offs; code size on x86-64 increases by roughly 10%, though AArch64 sizes remain comparable to the standard pipeline.

The Bottleneck Shifts to MC Layer

With the heavy lifting of instruction selection and register allocation gone, the bottleneck moves to the MC layer (Machine Code encoding and object emission). Profiling revealed that MC operations accounted for 20% of llc time on tramp3d benchmarks. To address this, several optimizations landed upstream, such as early returns in X86 finishLayout and improved CFI advance folding. The project also had to ensure that -S assembly output matches -c object output exactly, requiring careful handling of instruction encoding relaxations and label differences in exception tables.

Key Takeaways

  • Significant Speed Gains: Direct code generation reduces instruction counts by 14-21% for -O0 -g builds on x86-64 and AArch64 respectively.
  • Per-Function Fallback: The system intelligently falls back to the regular pipeline for unsupported features, ensuring no correctness regressions.
  • Debug Info Integrity: DWARF emission is preserved via skeleton MachineFunction objects, maintaining identical .text output with or without debug flags.
  • Code Size Trade-off: Expect a ~10% increase in code size on x86-64, though AArch64 impact is minimal.

The Bottom Line

This experiment proves that the traditional LLVM back-end pipeline is overkill for -O0 builds. By bypassing MachineFunction creation, we can achieve significant compile-time wins for debug builds without sacrificing correctness, provided we accept a slight increase in code size on x86-64.