Cranelift, Part 3: Correctness in Register Allocation
cfallin.org
cfallin.org
Even compcert (the formally proven compiler with gcc -O2 competitive code generation) essentially shells out to non proven ML and then verifies the result in contrast to the rest of the compiler design.
I’ve worked on simpler approximations of both [0][1] before and can attest even the simple parts are hard to get working reliably.
I'm probably mistaken, but I thought some years ago this got reduced to polynomial time via transforming the program to SSA form and doing some other magic.
Edit: here we go: https://dl.acm.org/doi/10.1007/978-3-642-37051-9_1
https://hal-lara.archives-ouvertes.fr/hal-02102286/file/RR20...
The chordal interference graphs that arise from (not necessarily structured) programs in SSA form are optimally colorable in linear time. That approach is a solid basis for a register allocator but it's worth noting that it's not solving the same problem because the introduced phi nodes split the original live ranges at CFG join points, so you're not coloring the same interference graph the program had prior to SSA conversion. In practice a good register allocator already needs to have well-tuned heuristics for spill/fill/split/coalesce placement regardless of whether it uses SSA-based coloring, so the extra splits introduced by the SSA conversion are optimized as part of that process.
In the special case where your SSA program is structured and the code is presented to the backend in its natural order, the perfect elimination order for the chordal interference graph is just backward code order. As a result you can do optimal register coloring for such programs in a single backward pass (which can be integrated into a backward code generation pass if you're trying to go fast).