Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bottom-up, Graph-Coloring, Register Allocation: Beyond the Treatment in COMP 412 COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.

Similar presentations


Presentation on theme: "Bottom-up, Graph-Coloring, Register Allocation: Beyond the Treatment in COMP 412 COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith."— Presentation transcript:

1 Bottom-up, Graph-Coloring, Register Allocation: Beyond the Treatment in COMP 412 COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use.

2 Register Allocation register allocator At each point in the code
Decide which values reside in registers Assign a particular register to each of those values Optimal global register allocation is NP-complete register allocator m register code k register COMP 512, Fall 2003

3 Register Allocation via Graph Coloring
Key ideas Build a graph to represent interferences in the code A k-coloring of that graph  a k-register allocation Use heuristics to find a k-coloring First two ideas are due to Lavrov in 1961 History in compilation Ershov’s Alpha system colored values into limited memory Cocke understood the application to register allocation Chaitin built the first coloring allocator in the PL.8 compiler Today’s lecture looks at improvements to Chaitin COMP 512, Fall 2003

4 Chaitin’s Allocator Rename
Find live ranges and systematically rename them Build Build the interference graph I n s e r t S p i l Coalesce Coalesce copies (<i,j>  G & i  j  combine i & j ) Spill Costs Simplify finds all the spills for G before inserting any spill code This creates a new graph G' that may need additional spills. Estimate cost of spill for each live range (complex) While G is non-empty if  n  n º < k, pull n out of G, & push it on stack else pick an n to spill & pull it out of G Simplify any spills? Select While stack is non-empty pop n, insert n into G, assign a color to n COMP 512, Fall 2003 *

5 Briggs’ Improvement (optimistic coloring)
Rename Find live ranges and systematically rename them Build Build the interference graph I n s e r t S p i l Coalesce Coalesce copies (<i,j>  G & i  j  combine i & j ) This simple change improves the allocation x has high º, but x’s neighbors use few colors ( colors are available for x) x is picked to spill, but spilling x does not help ( should have colored x) Makes the allocation optimistic – assume it will work out and fix things if it does not Spill Costs Estimate cost of spill for each live range (complex) While G is non-empty if  n  n º < k, pull n out of G, & push it on stack else pick an n to spill & pull it out of G Simplify While stack is non-empty pop n, insert n into G try to color n if no color is available for n, leave it uncolored uncolored nodes? Select COMP 512, Fall 2003 *

6 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b a d c b COMP 512, Fall 2003

7 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Remove a a c d COMP 512, Fall 2003

8 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d c d COMP 512, Fall 2003

9 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d, then c c COMP 512, Fall 2003

10 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d, then c Reinsert & color c c COMP 512, Fall 2003

11 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d, then c Reinsert & color c, d c d COMP 512, Fall 2003

12 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d, then c Reinsert & color c, d, and a a c d COMP 512, Fall 2003

13 Optimistic Coloring Example
Chaitin’s method No node has ° < 2 Pick a node to spill, say b Removes a, then d, then c Reinsert & color c, d, and a A color exists for b, but it has already been spilled ! a b c d COMP 512, Fall 2003

14 Optimistic Coloring Example
Briggs’ method No node has ° < 2 Pick a node to spill, say b a d c b COMP 512, Fall 2003

15 Optimistic Coloring Example
Briggs’ method No node has ° < 2 Pick a node to spill, say b Remove it & push it on the stack Now, proceed as with Chaitin Removes a, d, c Colors a, d, c a c d COMP 512, Fall 2003

16 Optimistic Coloring Example
Briggs’ method No node has ° < 2 Pick a node to spill, say b Remove it & push it on the stack Now, proceed as with Chaitin Removes a, d, c Colors a, d, c Now, reinsert b a c d COMP 512, Fall 2003

17 Optimistic Coloring Example
Briggs’ method No node has ° < 2 Pick a node to spill, say b Remove it & push it on the stack Now, proceed as with Chaitin Removes a, d, c Colors a, d, c Now, reinsert b & color it! Success ! a b c d This actually happens, in practice! COMP 512, Fall 2003

18 How do these allocators do?
Results are “pretty good” Simple procedures allocate without spills There is some room for improvement Long blocks, regions of high pressure Many implementation issues Many people have looked at improving Chaitin-Briggs Better allocations Better coloring Softer coalescing Better spilling Spilling partial live ranges Better implementations Faster graph construction Faster coalescing COMP 512, Fall 2003

19 } } Better Coloring Optimal coloring
Use backtracking to find minimal chromatic number Took lots of compile-time Found (some) better allocations Random-walk coloring Rather than spill, color rest nodes in random order Did rather well on random graphs Neither of these ideas has been widely used (beyond the authors) Unfortunately, some codes need more than k registers Better coloring will not help these codes } Done in GCC with a Chow allocator } No real basis to believe that it helps COMP 512, Fall 2003

20 Constructive techniques used in compilers
Starting point A classic compiler solves problems by making a series of local decisions. It constructs a solution and declares victory. Individual decisions If we gave it more time, it would plant the flag and finish early ! Solution space (shape & size usually unknown) COMP 512, Fall 2003

21 Using Non-constructive techniques
The non-constructive techniques start with an approximate (bad?) solution. They improve the solution in a small number of steps. They use randomization & restart to explore different parts of the solution space. Given more time, they plant the flag in more places. They can often use more time constructively. Starting point for old method Starting point for new method COMP 512, Fall 2003

22 } Softer Coalescing Chaitin’s scheme coalesces everything that it can
Coalescing i and j can create ij º > max(i º, j °) May make ij harder to color In some contexts, this is important We can limit coalescing to conservative cases Only create ij if ij º < k In this scenario, ij is trivially colored We can also bias the color selection If i and j are connected by a copy, try to pick the same color If the other one is not yet colored, pick a color still open for it Generalize to multiple copies (but only immediate neighbors) “biased coloring with limited lookahead” } Known as conservative coalescing COMP 512, Fall 2003

23 Building on Conservative Coalescing
Iterated Coalescing If no trivially colored node remains, coalesce again Coalescing reduces degree in the graph Makes sense only if allocator uses conservative coalescing Invented for Standard ML of New Jersey Long parameter lists, passed in registers This adds many additional edges I think that they hit the (now) known bug in “NeedLoad()” Iterated coalescing cured their problem COMP 512, Fall 2003

24 Editorial Opinion Conservative coalescing is oversold
Designed to remove unproductive splits Insert “special” copies & coalesce ones that don’t help Worked pretty well for that purpose Looks great on paper Why coalesce if things get worse? Conservative coalescing never makes things worse In practice, Chaitin gets most coalesced LRs Briggs should get even more Don’t be afraid to coalesce aggressively With passive splitting & IR spilling, might even be better COMP 512, Fall 2003

25 How do these allocators do?
Results are “pretty good” Simple procedures allocate without spills There is some room for improvement Long blocks, regions of high pressure Many implementation issues Many people have looked at improving Chaitin-Briggs Better allocations Better coloring Softer coalescing Better spilling Spilling partial live ranges Better implementations Faster graph construction Faster coalescing COMP 512, Fall 2003

26 Better Spilling Clean spilling (Bernstein et al.)
Spill value once per block, if possible Avoids redundant loads & stores Best of three spilling (Bernstein et al.) Simplify/Select is cheap relative to Build/Coalesce Try it with several different heuristics Rematerialization (Briggs/Cooper 94) Recognize values that are cheaper to recreate Rather than spill them, rematerialize them Each of these helps COMP 512, Fall 2003

27 Better Spill Choice Heuristics
} Every one should do it Clean spilling Minor computations during spill insertion Mostly a matter of paying attention to details Best of three spilling Just repeat Simplify/Select with different heuristics Gets at random parts of the algorithm (NP-noise) Might get some of it by renumbering – min of seven Rematerialization Tag each value with ci , BOT, or TOP Propagate, performing meet at Ø-functions Modify spill cost computation & spill insertion } 20% idea } 20% idea COMP 512, Fall 2003

28 Rematerialization Never-killed values can be rematerialized (rather than spilled) Operands are always available Computed in a single operation Cheaper to recompute than to store & reload (the classic spill ) Allocator must Discover & mark never-killed LRs Reflect rematerialization in spill costs Use all this knowledge to generate right spills Chaitin rematerialized LRs that were entirely never-killed We can do partial LRs } Definition of “never-killed” i.e., loadI, offset + FP COMP 512, Fall 2003

29 { Rematerialization Big Picture
Use SSA to break LR into component values Tag each component with a value Use Wegman & Zadeck SCCP to propagate tags Split off never-killed parts from rest of LR Use a “special” copy operation Special copies get coalesced with conservative coalescing Use tags to compute spill costs & to insert spill code Rely on conservative coalescing and biased coloring to remove unproductive splits (as before) { TOP defined by COPY or Ø inst never-killed op ( ptr) BOT defined by other op COMP 512, Fall 2003

30 Spilling Partial Live Ranges
} No data on how this does with Chaitin-Briggs Bottom-up splitting (Chow) Break troubled live range into basic blocks Recombine them when it does not increase degree Aggressive splitting (Briggs) Split aggressively based on the CFG Undo non-productive splits Interference region spilling (Bergner) Spill just region that conflicts with colored nodes Run in competition with default spilling Passive splitting (Simpson) Use directed interference graph to identify splits Run Classic, IR spilling, and Passive splitting in competition } Improvements ran from + 4x to - 4x spill ops } Improvements of ~36% in spill ops vs. Briggs } Sometimes wins big vs. Bergner, sometimes loses Up to 75% vs Chaitin Up to 78% vs Chaitin -34% to +73% COMP 512, Fall 2003 *

31 Interference Region Spilling
Simple idea: Find region where i & j are both live Spill i around this interference region (IR) Can reduce total cost of spilling i Fits entirely in “Insert Spills” phase of a Briggs allocator The implementation Take colored subgraph and list of uncolored nodes For each uncolored LR, find subranges that can be colored Rest of LR is its IR Compare cost of spilling IR versus cost of spilling entire LR Take cheaper alternative COMP 512, Fall 2003

32 Passive Splitting Passive Splitting x   y   x  y
Key observation x   y   x  y x   y    y x x   y  x  y spilling x does not help with y spilling x helps with y The containment graph captures this effect It is just a directed analog of the interference graph x y COMP 512, Fall 2003

33 Passive Splitting x y and not y x suggests splitting x around y
To split x around y store x before each definition of y load x after each death of y What does it cost? one memory op at each border of the overlap region may (or may not) be cheaper than spilling x everywhere This is the base case In practice, we may need to split around several live ranges COMP 512, Fall 2003

34 Building the Interference Graph
Need two representations Bit matrix Fast test for specific interference Need upper (lower) diagonal submatrix Takes fair amount of space & time Adjacency lists Fast iteration over neighbors Needed to find colors, count degree Must tightly bound space to make it practical Both Chaitin & Briggs recommend two passes First pass builds bit matrix and sizes adjacency vectors Second pass builds adjacency vectors into perfect-sized arrays COMP 512, Fall 2003

35 Building the Interference Graph
Split the graph into disjoint register classes Separate GPRs from FPRs Others may make sense (CCs, predicates) Graph is still n2, but n is smaller In practice, GPR/FPR split is significant Build adjacency lists in a single pass Block allocate adjacency lists (30 edges per block) Reduce amount of wasted space & pointer overhead Simple time-space tradeoff Already had a pointer, to implement coalescing Significance: 75% of space (Chaitin-Briggs) with one fewer pass 70% of time (Chaitin-Briggs) for whole allocation * COMP 512, Fall 2003

36 Building the Interference Graph
Hash table implementation If graph is sparse, replace bit-matrix with hash table Chaitin tried it and discarded the idea George & Appel claim it beat the bit matrix in space & time Our experience Finding a good hash function takes care Universal hash function from Cormen, Leiserson, & Rivest Multiplicative hash function from Knuth Takes graphs with many thousands of LRs to overtake split bit-matrix implementation Significance: 199% to 656% space versus Chaitin-Briggs 124% to 4500% allocation time versus Chaitin-Briggs * COMP 512, Fall 2003

37 Some interesting observations here …
Faster Coalescing Some interesting observations here … COMP 512, Fall 2003

38 Bibliography Briggs, Cooper, & Torczon, “Improvements to Graph Coloring Register Allocation,” ACM TOPLAS 16(3), May, 1994. Bernstein, Goldin, Golumbic, Krawczyk, Mansour, Nashon, & Pinter, “Spill Code Minimization Techniques for Optimizing Compilers,” Proceedings of PLDI 89, SIGPLAN Notices 24(7), July 1989. George & Appel, “Iterated Register Coalescing,” ACM TOPLAS 18(3), May, 1996. Bergner, Dahl, Engebretsen, & O’Keefe, “Spill Code Minimization via Interference Region Spilling,” Proceedings of PLDI 97, SIGPLAN Notices 32(6), June 1997. Cooper, Harvey, & Torczon, “How to Build an Interference Graph,” Software–Practice and Experience, 28(4), April, 1998 Cooper & Simpson, “Live-range splitting in a graph coloring register allocator,” Proceedings of the 1998 International Conference on Compiler Construction, LNCS 1381 (Springer), March/April 1998. COMP 512, Fall 2003


Download ppt "Bottom-up, Graph-Coloring, Register Allocation: Beyond the Treatment in COMP 412 COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith."

Similar presentations


Ads by Google