4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)

Slides:



Advertisements
Similar presentations
Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Advertisements

Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
1 Optimizing compilers Managing Cache Bercovici Sivan.
Register Allocation Zach Ma.
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
Idea of Register Allocation x = m[0]; y = m[1]; xy = x*y; z = m[2]; yz = y*z; xz = x*z; r = xy + yz; m[3] = r + xz x y z xy yz xz r {} {x} {x,y} {y,x,xy}
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
Register Allocation Mooly Sagiv Schrierber Wed 10:00-12:00 html://
COMPILERS Register Allocation hussein suleman uct csc305w 2004.
1 CS 201 Compiler Construction Machine Code Generation.
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Zhiguo Ge, Weng-Fai Wong, and Hock-Beng Lim Proceedings of the Design, Automation, and Test in Europe Conference, 2007 (DATE’07) April /4/17.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Carnegie Mellon Lecture 6 Register Allocation I. Introduction II. Abstraction and the Problem III. Algorithm Reading: Chapter Before next class:
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
CS 536 Spring Run-time organization Lecture 19.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Register Allocation (via graph coloring)
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Chapter 13 Reduced Instruction Set Computers (RISC) Pipelining.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
CMPE 511 Computer Architecture A Faster Optimal Register Allocator Betül Demiröz.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
C.E. Goutis V.I.Kelefouras University of Patras Department of Electrical and Computer Engineering VLSI lab Date: 31/01/2014 Compilers for Embedded Systems.
Final Code Generation and Code Optimization.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
Register Allocation CS 471 November 12, CS 471 – Fall 2007 Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Register Allocation: Graph Coloring Compiler Baojian Hua
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Global Register Allocation Based on
Mooly Sagiv html://
The compilation process
Register Allocation Hal Perkins Autumn 2009
Run-time organization
Register Allocation Noam Rinetzky Text book:
Register Allocation Hal Perkins Autumn 2011
CS 201 Compiler Construction
ECE 463/563, Microprocessor Architecture, Prof. Eric Rotenberg
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)

4/29/09Prof. Hilfinger CS164 Lecture 382 Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring heuristics –Spilling Cache Management

4/29/09Prof. Hilfinger CS164 Lecture 383 The Memory Hierarchy Registers 1 cycle bytes Cache cycles 256k-16M Main memory cycles 32M-16G Disk M cycles 10G-1T

4/29/09Prof. Hilfinger CS164 Lecture 384 Managing the Memory Hierarchy Programs are written as if there are only two kinds of memory: main memory and disk Programmer is responsible for moving data from disk to memory (e.g., file I/O) Hardware is responsible for moving data between memory and caches Compiler is responsible for moving data between memory and registers

4/29/09Prof. Hilfinger CS164 Lecture 385 Current Trends Cache and register sizes are growing slowly Processor speed improves faster than memory speed and disk speed –The cost of a cache miss is growing –The widening gap is bridged with more caches It is very important to: –Manage registers properly –Manage caches properly Compilers are good at managing registers

4/29/09Prof. Hilfinger CS164 Lecture 386 The Register Allocation Problem Intermediate code uses as many temporaries as necessary –This complicates final translation to assembly –But simplifies code generation and optimization –Typical intermediate code uses too many temporaries The register allocation problem: –Rewrite the intermediate code to use fewer temporaries than there are machine registers –Method: assign more temporaries to a register But without changing the program behavior

4/29/09Prof. Hilfinger CS164 Lecture 387 History Register allocation is as old as intermediate code Register allocation was used in the original FORTRAN compiler in the ‘50s –Very crude algorithms A breakthrough was not achieved until 1980 when Chaitin invented a register allocation scheme based on graph coloring –Relatively simple, global and works well in practice

4/29/09Prof. Hilfinger CS164 Lecture 388 An Example Consider the program a := c + d e := a + b f := e - 1 –with the assumption that a and e die after use Temporary a can be “reused” after “a + b” Same with temporary e after “e - 1” Can allocate a, e, and f all to one register (r 1 ): r 1 := c + d r 1 := r 1 + b r 1 := r 1 - 1

4/29/09Prof. Hilfinger CS164 Lecture 389 Basic Register Allocation Idea The value in a dead temporary is not needed for the rest of the computation –A dead temporary can be reused Basic rule: –Temporaries t 1 and t 2 can share the same register if at any point in the program at most one of t 1 or t 2 is live !

4/29/09Prof. Hilfinger CS164 Lecture 3810 Algorithm: Part I Compute live variables for each point: a := b + c d := -a e := d + f f := 2 * e b := d + e e := e - 1 b := f + c {b} {c,e} {b} {c,f} {b,c,e,f} {c,d,e,f} {b,c,f} {c,d,f} {a,c,f}

4/29/09Prof. Hilfinger CS164 Lecture 3811 The Register Interference Graph Two temporaries that are live simultaneously cannot be allocated in the same register We construct an undirected graph –A node for each temporary –An edge between t 1 and t 2 if they are live simultaneously at some point in the program This is the register interference graph (RIG) –Two temporaries can be allocated to the same register if there is no edge connecting them

4/29/09Prof. Hilfinger CS164 Lecture 3812 Register Interference Graph. Example. For our example: a f e d c b E.g., b and c cannot be in the same register E.g., b and d can be in the same register

4/29/09Prof. Hilfinger CS164 Lecture 3813 Register Interference Graph. Properties. It extracts exactly the information needed to characterize legal register assignments It gives a global (i.e., over the entire flow graph) picture of the register requirements After RIG construction the register allocation algorithm is architecture independent

4/29/09Prof. Hilfinger CS164 Lecture 3814 Graph Coloring. Definitions. A coloring of a graph is an assignment of colors to nodes, such that nodes connected by an edge have different colors A graph is k-colorable if it has a coloring with k colors

4/29/09Prof. Hilfinger CS164 Lecture 3815 Register Allocation Through Graph Coloring In our problem, colors = registers –We need to assign colors (registers) to graph nodes (temporaries) Let k = number of machine registers If the RIG is k-colorable then there is a register assignment that uses no more than k registers

4/29/09Prof. Hilfinger CS164 Lecture 3816 Graph Coloring. Example. Consider the sample RIG a f e d c b There is no coloring with fewer than 4 colors There are 4-colorings of this graph r4r4 r1r1 r2r2 r3r3 r2r2 r3r3

4/29/09Prof. Hilfinger CS164 Lecture 3817 Graph Coloring. Example. Under this coloring the code becomes: r 2 := r 3 + r 4 r 3 := -r 2 r 2 := r 3 + r 1 r 1 := 2 * r 2 r 3 := r 3 + r 2 r 2 := r r 3 := r 1 + r 4

4/29/09Prof. Hilfinger CS164 Lecture 3818 Computing Graph Colorings The remaining problem is to compute a coloring for the interference graph But: 1.This problem is very hard (NP-hard). No efficient algorithms are known. 2.A coloring might not exist for a given number or registers The solution to (1) is to use heuristics We’ll consider later the other problem

4/29/09Prof. Hilfinger CS164 Lecture 3819 Graph Coloring Heuristic Observation: –Pick a node t with fewer than k neighbors in RIG –Eliminate t and its edges from RIG –If the resulting graph has a k-coloring then so does the original graph Why: –Let c 1,…,c n be the colors assigned to the neighbors of t in the reduced graph –Since n < k we can pick some color for t that is different from those of its neighbors

4/29/09Prof. Hilfinger CS164 Lecture 3820 Graph Coloring Heuristic The following works well in practice: –Pick a node t with fewer than k neighbors –Push t on a stack and remove it from the RIG –Repeat until the graph has one node Then start assigning colors to nodes in the stack (starting with the last node added) –At each step pick a color different from those assigned to already colored neighbors

4/29/09Prof. Hilfinger CS164 Lecture 3821 Graph Coloring Example (1) Remove a and then d a f e d c b Start with the RIG and with k = 4: Stack: {}

4/29/09Prof. Hilfinger CS164 Lecture 3822 Graph Coloring Example (2) Now all nodes have fewer than 4 neighbors and can be removed: c, b, e, f Leaving an empty graph and stack containing {f, e, b, c, d, a} f e c b Stack: {d, a} Resulting graph:

4/29/09Prof. Hilfinger CS164 Lecture 3823 Graph Coloring Example (2) Start assigning colors to: f, e, b, c, d, a At each step, guaranteed there’s a free color b a e c r4r4 fr1r1 r2r2 r3r3 r2r2 r3r3 d

4/29/09Prof. Hilfinger CS164 Lecture 3824 What if the Heuristic Fails? What if during simplification we get to a state where all nodes have k or more neighbors ? Example: try to find a 3-coloring of the RIG: a f e d c b

4/29/09Prof. Hilfinger CS164 Lecture 3825 What if the Heuristic Fails? Remove a and get stuck (as shown below) f e d c b Pick a node as a candidate for spilling –A spilled temporary “lives” in memory Assume that f is picked as a candidate

4/29/09Prof. Hilfinger CS164 Lecture 3826 What if the Heuristic Fails? Remove f and continue the simplification –Simplification now succeeds: b, d, e, c e d c b

4/29/09Prof. Hilfinger CS164 Lecture 3827 What if the Heuristic Fails? On the assignment phase we get to the point when we have to assign a color to f We hope that among the 4 neighbors of f we use less than 3 colors  optimistic coloring f e d c b r3r3 r1r1 r2r2 r3r3 ?

4/29/09Prof. Hilfinger CS164 Lecture 3828 Spilling Since optimistic coloring failed we must spill temporary f We must allocate a memory location as the home of f –Typically this is in the current stack frame –Call this address fa Before each operation that uses f, insert f := load fa After each operation that defines f, insert store f, fa

4/29/09Prof. Hilfinger CS164 Lecture 3829 Spilling. Example. This is the new code after spilling f a := b + c d := -a f := load fa e := d + f f := 2 * e store f, fa b := d + e e := e - 1 f := load fa b := f + c

4/29/09Prof. Hilfinger CS164 Lecture 3830 Recomputing Liveness Information The new liveness information after spilling: a := b + c d := -a f := load fa e := d + f f := 2 * e store f, fa b := d + e e := e - 1 f := load fa b := f + c {b} {c,e} {b} {c,f} {b,c,e,f} {c,d,e,f} {b,c,f} {c,d,f} {a,c,f} {c,d,f} {c,f}

4/29/09Prof. Hilfinger CS164 Lecture 3831 Recomputing Liveness Information The new liveness information is almost as before f is live only –Between a f := load fa and the next instruction –Between a store f, fa and the preceding instr. Spilling reduces the live range of f And thus reduces its interferences Which result in fewer neighbors in RIG for f

4/29/09Prof. Hilfinger CS164 Lecture 3832 Recompute RIG After Spilling The only changes are in removing some of the edges of the spilled node In our case f still interferes only with c and d And the resulting RIG is 3-colorable a f e d c b

4/29/09Prof. Hilfinger CS164 Lecture 3833 Spilling (Cont.) Additional spills might be required before a coloring is found The tricky part is deciding what to spill Possible heuristics: –Spill temporaries with most conflicts –Spill temporaries with few definitions and uses –Avoid spilling in inner loops Any heuristic is correct

4/29/09Prof. Hilfinger CS164 Lecture 3834 Caches Compilers are very good at managing registers –Much better than a programmer could be Compilers are not good at managing caches –This problem is still left to programmers –It is still an open question whether a compiler can do anything general to improve performance Compilers can, and a few do, perform some simple cache optimization

4/29/09Prof. Hilfinger CS164 Lecture 3835 Cache Optimization Consider the loop for(j = 1; j < 10; j++) for(i=1; i< ; i++) a[i] *= b[i] –This program has a terrible cache performance Why?

4/29/09Prof. Hilfinger CS164 Lecture 3836 Cache Optimization (II) Consider the program: for(i=1; i< ; i++) for(j = 1; j < 10; j++) a[i] *= b[i] –Computes the same thing –But with much better cache behavior –Might actually be more than 10x faster A compiler can perform this optimization –called loop interchange

4/29/09Prof. Hilfinger CS164 Lecture 3837 Cache Optimization (III) Other kinds of memory layout decisions possible, such as padding rows of matrix for better fit in a cache. Prefetching informs cache of anticipated future memory fetches so that they can proceed in parallel. Hardware often does this, in which case compiler may have to tell it when not to prefetch.

4/29/09Prof. Hilfinger CS164 Lecture 3838 Conclusions Register allocation is a “must have” optimization in most compilers: –Because intermediate code uses too many temporaries –Because it makes a big difference in performance Graph coloring is a powerful register allocation scheme Good cache management could give much larger payoffs, but it is difficult.