Techniquecore
Spill code placement
Also called spill code insertion, register spilling.
Keeps a value in memory and inserts stores after its definitions and loads before its uses when it cannot have a register.
Spill code placement decides where the loads and stores of a spilled value go. When an allocator cannot give a value a register for its whole life, it keeps the value in a memory slot, the spill slot, and adds a store that copies it from a register to the slot and a load that copies it back before it is used. The simplest rule, spill everywhere from Chaitin's allocator, puts a store after every definition and a load before every use. It is always correct, but it can add far more memory traffic than needed, and a load right after a store may not even lower the register pressure. Better placement keeps the value in a register where registers are free, puts it in memory only across the part of the program where too many values are live, and puts the loads and stores at the points that run least often.
In plain words
When there are not enough registers, some value has to wait in memory. The compiler then writes it to memory and reads it back later. The easy way is to write it each time it is made and read it each time it is needed. That works, but it wastes time: if the value is needed twice in a row with plenty of free registers between, one read is enough, and if the shortage is only in one small part of the program, the value only needs to be in memory there. Reads and writes inside a loop run many times, so it pays to move them before or after the loop. Spill code placement is the work of putting these reads and writes in the cheapest places that still keep the program correct.
When it applies
- An allocator has decided to spill a value : Chaitin's allocator, optimistic coloring, linear scan or a local allocator.
- The machine computes only on registers, as in the toy IR, so each use of needs in some register at that point, and each definition writes to a register first.
- Memory and registers can be moved between with a store
mem.x = tand a loadt = mem.x. A load costs more than an instruction on registers, and its cost is multiplied by how often the point runs.
The template
Fix a spilled name with the slot mem.x. A high-pressure point is a point where more than values would be live if were kept in a register.
- Spill everywhere. After each definition of , store it; before each instruction that reads , load it into a new short temporary. This is correct, by the spill lemma in the proof file of Chaitin's allocator: at each read, the slot holds the value of that the original program would read.
- Keep the register where it is free. If a use of follows its definition, or an earlier load, in the same block with no high-pressure point between, use the register directly and drop the load. In the same way, several uses close together can share one load.
- Store only when needed. A store is needed only if the slot does not already hold the current value. A value that was just loaded from its slot, or a parameter that arrives in its slot, needs no store. A value that is never redefined needs one store after its only definition.
- Move the code to cold points. The value must be in memory across each high-pressure region it is live in. Put the store on the edges into the region and the load on the edges out of it, and choose, among the correct points, those with the lowest execution frequency. Loop nests or profile data give the frequencies.
- Split critical edges. A load or store that belongs on an edge from a block with several successors to a block with several predecessors needs a new block on that edge (critical edge splitting).
- Rebuild. In a graph-coloring allocator, the spill code changes liveness, so the interference graph is built again.
Steps 2 to 4 keep in a register over longer stretches, so they must keep each stretch free of high-pressure points. Otherwise the allocator will have to spill again.
Classic examples
One block, two registers. The program from the page register allocation problem is:
L0:
a = p + 1
b = p + 2
c = a + b
d = a * c
e = b * d
ret e
Just after c = a + b, the names , and are all live, so with one of them goes to memory. Spilling everywhere gives one store and two loads:
a = p + 1
b1 = p + 2
mem.b = b1
b2 = mem.b
c = a + b2
d = a * c
b3 = mem.b
e = b3 * d
Step 2 drops the load of b2: between the definition of and its first use, only and are live. The result has one store and one load, which is the least possible:
a = p + 1
b = p + 2
mem.b = b
c = a + b
d = a * c
b2 = mem.b
e = b2 * d
A script checked that both programs give the outcome of the original for from to , and that both interference graphs are 2-colorable. Spilling everywhere does not help at all: c1 = a + b; mem.c = c1; c2 = mem.c; d = a * c2 still has , and c2 live together. Chaitin's allocator, which uses spill everywhere, ends with 3 stores and 5 loads on this program.
A value used in a loop. Let be defined before a loop, read in every iteration, and not redefined. If the high pressure is only after the loop, spill everywhere loads in every iteration, and the cost grows with the number of iterations. Keeping in its register through the loop and storing it once on the exit edge costs one store, run once. This is a case where placement and live range splitting meet: gets a register in the loop and a slot after it.
A parameter. In the toy IR, a parameter arrives in its slot mem.p, and the entry block loads it. If is spilled everywhere, the load is followed by a store of the same value to the same slot, which can be deleted.
A constant. If is set by x = 7 and never changed, no store and no slot are needed: each load can be replaced by x = 7 itself (rematerialization).
Variations
- Spill everywhere. Chaitin (1982). It makes each spilled value a set of tiny live ranges, which helps the coloring, at the cost of many memory operations. Bouchez, Darte and Rastello (2007) proved that choosing which values to spill everywhere at least cost is NP-complete in general, even under SSA form.
- Local cleanup. After spill everywhere, a pass over each block removes loads whose value is still in a register and stores whose slot is already up to date.
- Interval splitting in linear scan. Linear scan can split an interval at the point where it runs out of registers and place a store there and a load before the next use.
- Farthest next use. Inside one block, when a register must be freed, evicting the value whose next use is farthest is the rule of Belady's algorithm for caches. It minimizes the number of loads in a block, but stores and the cost of different values make the register problem harder than paging.
- Spill slot sharing. Two spilled values that are never live at the same time can share one slot, by a coloring of the slots that works like register allocation.
- Recomputation. Rematerialization replaces the loads of a cheap value by its computation.
Pitfalls
- A load right after a store gains nothing. If the store and the load are next to each other, the value is still live across the same points, under another name. Choose the value to spill by where it can stay in memory, not only by its cost.
- Do not move a load above a redefinition. A load must read the slot after the last store that matches the definition it stands for.
- Every path needs the value. When loads move to edges, every path from a store to a use must pass a load, and every path from a definition to a load must pass a store.
- Critical edges. A load placed at the end of a block with two successors runs on both paths. Split the edge to put it on one path only.
- Spill temporaries. The short temporaries of spill code must never be spilled again; give them an infinite spill cost.
- Frequency counts. A load in a loop counts once per iteration. Ignoring this can make a placement with fewer instructions run slower.
Exercises
-
In the one-block example, spill everywhere instead of . How many stores and loads does it need, and is the interference graph 2-colorable?
Answer. is read by
c = a + bandd = a * c, so spill everywhere needs one store and two loads, intoa2beforec = a2 + band intoa3befored = a3 * c. Just after the load ofa3, the namesa3, and are live, since is read later bye = b * d. So three names are live together and the graph has a triangle. Spilling does not solve the problem; is the right choice. -
A value is defined once and read in five places in one block, with no high-pressure point between the definition and the last read. How much spill code does need?
Answer. None. With no high-pressure point in its live range, can stay in a register, and spilling it only adds cost.
-
Why can the store be dropped for a spilled parameter of the toy IR?
Answer. The parameter arrives in
mem.pand is never redefined before the entry load. After the load, the register and the slot hold the same value, so storing it again writes the value the slot already has. -
A value is defined in block and read in block . The path goes and , and only has high pressure. Where should the store and the load go?
Answer. The value must be in memory across only. Store it on the edge and load it on the edge . Along it can stay in its register. If the edges are critical, split them first. If the register differs on the two paths into , a copy on one of the edges makes them equal.
References
- Gregory J. Chaitin, Register allocation and spilling via graph coloring, Proceedings of the SIGPLAN '82 Symposium on Compiler Construction, SIGPLAN Notices 17(6), 1982, 98-105, spill everywhere, the spill cost estimate, and the rebuild of the graph after spill code is added
- Preston Briggs, Keith D. Cooper and Linda Torczon, Improvements to graph coloring register allocation, ACM Transactions on Programming Languages and Systems 16(3), 1994, 428-455, spill decisions in the optimistic allocator and the cheaper spill code given by rematerialization
- Florent Bouchez, Alain Darte and Fabrice Rastello, On the complexity of spill everywhere under SSA form, Proceedings of LCTES 2007, ACM, 2007, the complexity of choosing which values to spill everywhere, which stays NP-complete under SSA form in general
- Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 13 (Register Allocation), spill costs, spill code and the placement of loads and stores
- Andrew W. Appel with Jens Palsberg, Modern Compiler Implementation in Java, second edition, Cambridge University Press, 2002, Chapter 11 (Register Allocation), the rewrite of the program for actual spills
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag AKP7YG Edit this page History Report an error Markdown