StrongerXi / soc

Compiler for a subset of OCaml
1 stars 0 forks source link

Existing theoretical issue in greedy register allocator #54

Open StrongerXi opened 3 years ago

StrongerXi commented 3 years ago

A re-compilation of an issue discovered at the end of #52

Issue

The greedy register allocation algorithm can't guarantee a coloring deterministically. Consider the following case:

# 2 regs {0, 1}
# pre-color = {T0 -> 0}
T0, T1 := ... T1

If we greedily color T1 with 0, we are out of luck, because spilling wouldn't really change anything here (this is the only instruction in the program).

In fact, who knows which color it's going to pick? But the algorithm is deterministic so if it picks the wrong color, oops.

So the best we can do is

Practical win

As mentioned in another sub-issue from #52, since we only write to multiple temps in call, and those temps are pre-colored to distinct caller-saved regs, we won't have this issue in practice.

Moreover, this issue can't occur on the read end either, since any temp being read must be active (or from initial live-in, but those are arguments that are distinctly colored anyway). If they are active, they must have distinct coloring already :).

StrongerXi commented 3 years ago

55 documents the invariant that clarifies the correctness argument.