EECS 583 – Class 15 Register Allocation University of Michigan November 2, 2011.

Slides:



Advertisements
Similar presentations
Great Theoretical Ideas in Computer Science for Some.
Advertisements

Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
Register Allocation Consists of two parts: Goal : minimize spills
Target Code Generation
Compiler Support for Superscalar Processors. Loop Unrolling Assumption: Standard five stage pipeline Empty cycles between instructions before the result.
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 (
Register Allocation Zach Ma.
Register Allocation CS 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
EECS 583 – Class 13 Software Pipelining University of Michigan October 24, 2011.
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.
EECC551 - Shaaban #1 Fall 2003 lec# Pipelining and Exploiting Instruction-Level Parallelism (ILP) Pipelining increases performance by overlapping.
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.
Program Representations. Representing programs Goals.
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.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
EECC551 - Shaaban #1 Spring 2006 lec# Pipelining and Instruction-Level Parallelism. Definition of basic instruction block Increasing Instruction-Level.
1 Register Allocation Consists of two parts: –register allocation What will be stored in registers –Only unambiguous values –register assignment Which.
EECC551 - Shaaban #1 Fall 2005 lec# Pipelining and Instruction-Level Parallelism. Definition of basic instruction block Increasing Instruction-Level.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
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.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Improving Code Generation Honors Compilers April 16 th 2002.
EECC551 - Shaaban #1 Spring 2004 lec# Definition of basic instruction blocks Increasing Instruction-Level Parallelism & Size of Basic Blocks.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Register Allocation and Spilling via Graph Coloring G. J. Chaitin IBM Research, 1982.
CS745: Register Allocation© Seth Copen Goldstein & Todd C. Mowry Register Allocation.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
Dataflow IV: Loop Optimizations, Exam 2 Review EECS 483 – Lecture 26 University of Michigan Wednesday, December 6, 2006.
EECS 583 – Class 14 Modulo Scheduling Reloaded University of Michigan October 31, 2011.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
1 October 18, October 18, 2015October 18, 2015October 18, 2015 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel:
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.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 33.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
5/13/99 Ashish Sabharwal1 Pipelining and Hazards n Hazards occur because –Don’t have enough resources (ALU’s, memory,…) Structural Hazard –Need a value.
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.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Carnegie Mellon Lecture 8 Software Pipelining I. Introduction II. Problem Formulation III. Algorithm Reading: Chapter 10.5 – 10.6 M. LamCS243: Software.
Instruction Scheduling Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Global Register Allocation Based on
The compilation process
Register Allocation Hal Perkins Autumn 2009
Register Allocation Hal Perkins Autumn 2011
EECS 583 – Class 13 Software Pipelining
EECS 583 – Class 15 Register Allocation
Register Allocation Hal Perkins Autumn 2005
EECS 583 – Class 14 Modulo Scheduling Reloaded
Lecture 16: Register Allocation
EECS 583 – Class 15 Register Allocation
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

EECS 583 – Class 15 Register Allocation University of Michigan November 2, 2011

- 1 - Announcements + Reading Material v Midterm exam: Monday, Nov 14? »Could also do Wednes Nov 9 (next week!) or Wednes Nov 16 (2 wks from now) »Class vote v Today’s class reading »“Register Allocation and Spilling Via Graph Coloring,” G. Chaitin, Proc SIGPLAN Symposium on Compiler Construction, v Next class reading – More at the end of class »“Revisiting the Sequential Programming Model for Multi-Core,” M. J. Bridges, N. Vachharajani, Y. Zhang, T. Jablin, and D. I. August, Proc 40th IEEE/ACM International Symposium on Microarchitecture, December 2007.

- 2 - Homework Problem – Answers in Red latencies: add=1, mpy=3, ld = 2, st = 1, br = 1 for (j=0; j<100; j++) b[j] = a[j] * 26 1: r3 = load(r1) 2: r4 = r3 * 26 3: store (r2, r4) 4: r1 = r : r2 = r : brlc Loop Loop: LC = 99 How many resources of each type are required to achieve an II=1 schedule? For II=1, each operation needs a dedicated resource, so: 3 ALU, 2 MEM, 1 BR If the resources are non-pipelined, how many resources of each type are required to achieve II=1 Instead of 1 ALU to do the multiplies, 3 are needed, and instead of 1 MEM to do the loads, 2 are needed. Hence: 5 ALU, 3 MEM, 1 BR Assuming pipelined resources, generate the II=1 modulo schedule. See next few slides

- 3 - HW continued ,1 3,0 2,0 1,1 RecMII = 1 RESMII = 1 MII = MAX(1,1) = 1 1: r3[-1] = load(r1[0]) 2: r4[-1] = r3[-1] * 26 3: store (r2[0], r4[-1]) 4: r1[-1] = r1[0] + 4 5: r2[-1] = r2[0] + 4 remap r1, r2, r3, r4 7: brlc Loop Loop: LC = 99 Dependence graph (same as example in class) 0,0 DSA converted code below (same as example in class) Assume II=1 so resources are: 3 ALU, 2 MEM, 1 BR Priorities 1: H = 5 2: H = 3 3: H = 0 4: H = 4 5: H = 0 7: H = 0

- 4 - HW continued resources: 3 alu, 2 mem, 1 br latencies: add=1, mpy=3, ld = 2, st = 1, br = 1 1: r3[-1] = load(r1[0]) 2: r4[-1] = r3[-1] * 26 3: store (r2[0], r4[-1]) 4: r1[-1] = r1[0] + 4 5: r2[-1] = r2[0] + 4 remap r1, r2, r3, r4 7: brlc Loop Loop: LC = 99 alu0alu1m2br MRT 0 X 0 7 Rolled Schedule Unrolled Schedule m1alu2 Scheduling steps: Schedule brlc at time II-1 Schedule op1 at time 0 Schedule op4 at time 0 Schedule op2 at time 2 Schedule op3 at time 5 Schedule op5 at time 5 Schedule op7 at time XXXXX stage 1 stage 2 stage 3 stage 4 stage 5 stage 6

- 5 - HW continued r3[-1] = load(r1[0]) if p1[0]; r4[-1] = r3[-1] * 26 if p1[2]; store (r2[0], r4[-1]) if p1[5]; r1[-1] = r1[0] + 4 if p1[0]; r2[-1] = r2[0] + 4 if p1[5]; brf Loop Loop: LC = 99 The final loop consists of a single MultiOp containing 6 operations, each predicated on the appropriate staging predicate. Note register allocation still needs to be performed.

- 6 - Register Allocation: Problem Definition v Through optimization, assume an infinite number of virtual registers »Now, must allocate these infinite virtual registers to a limited supply of hardware registers »Want most frequently accessed variables in registers  Speed, registers much faster than memory  Direct access as an operand »Any VR that cannot be mapped into a physical register is said to be spilled v Questions to answer »What is the minimum number of registers needed to avoid spilling? »Given n registers, is spilling necessary »Find an assignment of virtual registers to physical registers »If there are not enough physical registers, which virtual registers get spilled?

- 7 - Live Range v Value = definition of a register v Live range = Set of operations »1 more or values connected by common uses »A single VR may have several live ranges »Very similar to the web being constructed for HW3 v Live ranges are constructed by taking the intersection of reaching defs and liveness »Initially, a live range consists of a single definition and all ops in a function in which that definition is live

- 8 - Example – Constructing Live Ranges 1: x = 2: x =3: 4: = x 5: x = 6: x = 7: = x 8: = x {x}, {5,6} {x}, {6} {}, {5} {x}, {5} {}, {1,2} {}, {1} {x}, {2} {x}, {1} {}, {5,6} {liveness}, {rdefs} LR1 for def 1 = {1,3,4} LR2 for def 2 = {2,4} LR3 for def 5 = {5,7,8} LR4 for def 6 = {6,7,8} Each definition is the seed of a live range. Ops are added to the LR where both the defn reaches and the variable is live

- 9 - Merging Live Ranges v If 2 live ranges for the same VR overlap, they must be merged to ensure correctness »LRs replaced by a new LR that is the union of the LRs »Multiple defs reaching a common use »Conservatively, all LRs for the same VR could be merged  Makes LRs larger than need be, but done for simplicity  We will not assume this r1 = = r1

Example – Merging Live Ranges 1: x = 2: x =3: 4: = x 5: x = 6: x = 7: = x 8: = x {x}, {5,6} {x}, {6} {}, {5} {x}, {5} {}, {1,2} {}, {1} {x}, {2} {x}, {1} {}, {5,6} {liveness}, {rdefs} LR1 for def 1 = {1,3,4} LR2 for def 2 = {2,4} LR3 for def 5 = {5,7,8} LR4 for def 6 = {6,7,8} Merge LR1 and LR2, LR3 and LR4 LR5 = {1,2,3,4} LR6 = {5,6,7,8}

Class Problem 1: y = 2: x = y 3: = x 6: y = 7: z = 8: x = 9: = y 10: = z 4: y = 5: = y Compute the LRs a)for each def b)merge overlapping

Interference v Two live ranges interfere if they share one or more ops in common »Thus, they cannot occupy the same physical register »Or a live value would be lost v Interference graph »Undirected graph where  Nodes are live ranges  There is an edge between 2 nodes if the live ranges interfere »What’s not represented by this graph  Extent of interference between the LRs  Where in the program is the interference

Example – Interference Graph 1: a = load() 2: b = load() 3: c = load() 4: d = b + c 5: e = d - 3 6: f = a * b 7: e = f + c 8: g = a + e 9: store(g) a g c f d b e lr(a) = {1,2,3,4,5,6,7,8} lr(b) = {2,3,4,6} lr(c) = {1,2,3,4,5,6,7,8,9} lr(d) = {4,5} lr(e) = {5,7,8} lr(f) = {6,7} lr{g} = {8,9}

Graph Coloring v A graph is n-colorable if every node in the graph can be colored with one of the n colors such that 2 adjacent nodes do not have the same color »Model register allocation as graph coloring »Use the fewest colors (physical registers) »Spilling is necessary if the graph is not n-colorable where n is the number of physical registers v Optimal graph coloring is NP-complete for n > 2 »Use heuristics proposed by compiler developers  “Register Allocation Via Coloring”, G. Chaitin et al, 1981  “Improvement to Graph Coloring Register Allocation”, P. Briggs et al, 1989 »Observation – a node with degree < n in the interference can always be successfully colored given its neighbors colors

Coloring Algorithm v 1. While any node, x, has < n neighbors »Remove x and its edges from the graph »Push x onto a stack v 2. If the remaining graph is non-empty »Compute cost of spilling each node (live range)  For each reference to the register in the live range u Cost += (execution frequency * spill cost) »Let NB(x) = number of neighbors of x »Remove node x that has the smallest cost(x) / NB(x)  Push x onto a stack (mark as spilled) »Go back to step 1 v While stack is non-empty »Pop x from the stack »If x’s neighbors are assigned fewer than R colors, then assign x any unsigned color, else leave x uncolored

Example – Finding Number of Needed Colors A B E D C How many colors are needed to color this graph? Try n=1, no, cannot remove any nodes Try n=2, no again, cannot remove any nodes Try n=3, Remove B Then can remove A, C Then can remove D, E Thus it is 3-colorable

Example – Do a 3-Coloring a g c f d b e abcdefg cost neighbors cost/n lr(a) = {1,2,3,4,5,6,7,8} refs(a) = {1,6,8} lr(b) = {2,3,4,6} refs(b) = {2,4,6} lr(c) = {1,2,3,4,5,6,7,8,9} refs(c) = {3,4,7} lr(d) = {4,5} refs(d) = {4,5} lr(e) = {5,7,8} refs(e) = {5,7,8} lr(f) = {6,7} refs(f) = {6,7} lr{g} = {8,9} refs(g) = {8,9} Profile freqs 1,2 = 100 3,4,5 = 75 6,7 = 25 8,9 = 100 Assume each spill requires 1 operation

Example – Do a 3-Coloring (2) a g c f d b e Remove all nodes < 3 neighbors So, g can be removed a c f d b e Stack g

Example – Do a 3-Coloring (3) Now must spill a node Choose one with the smallest cost/NB  f is chosen a c d b e Stack f (spilled) g a c f d b e

Example – Do a 3-Coloring (4) a c d b Stack e f (spilled) g a c d b e Remove all nodes < 3 neighbors So, e can be removed

Example – Do a 3-Coloring (5) a d b Stack c (spilled) e f (spilled) g Now must spill another node Choose one with the smallest cost/NB  c is chosen a c d b

Example – Do a 3-Coloring (6) Stack d b a c (spilled) e f (spilled) g Remove all nodes < 3 neighbors So, a, b, d can be removed a d b Null

Example – Do a 3-Coloring (7) Stack d b a c (spilled) e f (spilled) g a g c f d b e Have 3 colors: red, green, blue, pop off the stack assigning colors only consider conflicts with non-spilled nodes already popped off stack d  red b  green (cannot choose red) a  blue (cannot choose red or green) c  no color (spilled) e  green (cannot choose red or blue) f  no color (spilled) g  red (cannot choose blue)

Example – Do a 3-Coloring (8) 1: blue = load() 2: green = load() 3: spill1 = load() 4: red = green + spill1 5: green = red - 3 6: spill2 = blue * green 7: green = spill2 + spill1 8: red = blue + green 9: store(red) d  red b  green a  blue c  no color e  green f  no color g  red Notes: no spills in the blocks executed 100 times. Most spills in the block executed 25 times. Longest lifetime (c) also spilled

Homework Problem 1: y = 2: x = y 3: = x 6: y = 7: z = 8: x = 9: = y 10: = z 4: y = 5: = y do a 2-coloring compute cost matrix draw interference graph color graph

It’s not that easy – Iterative Coloring 1: blue = load() 2: green = load() 3: spill1 = load() 4: red = green + spill1 5: green = red - 3 6: spill2 = blue * green 7: green = spill2 + spill1 8: red = blue + green 9: store(red) You can’t spill without creating more live ranges - Need regs for the stack ptr, value spilled, [offset] Can’t color before taking this into account

Iterative Coloring (1) 1: a = load() 2: b = load() 3: c = load() 10: store(c, sp) 11: i = load(sp) 4: d = b + i 5: e = d - 3 6: f = a * b 12: store(f, sp + 4) 13: j = load(sp + 4) 14: k = load(sp) 7: e = k + j 8: g = a + e 9: store(g) 1.After spilling, assign variables to a stack location, insert loads/stores 0: c = 15: store(c, sp)

Iterative Coloring (2) 1: a = load() 2: b = load() 3: c = load() 10: store(c, sp) 11: i = load(sp) 4: d = b + i 5: e = d - 3 6: f = a * b 12: store(f, sp + 4) 13: j = load(sp + 4) 14: k = load(sp) 7: e = k + j 8: g = a + e 9: store(g) 2.Update live ranges - Don’t need to recompute! 0: c = 15: store(c, sp) lr(a) = {1,2,3,4,5,6,7,8,10,11,12,13,14} refs(a) = {1,6,8} lr(b) = {2,3,4,6,10,11} lr(c) = {3,10} (This was big) lr(d) = … lr(e) = … lr(f) = … lr(g) = … lr(i) = {4,11} lr(j) = {7,13,14} lr(k) = {7,14} lr(sp) = …

Iterative Coloring (3) 3.Update interference graph -Nuke edges between spilled LRs a g c f d b e i j k

Iterative Coloring (4) a g c f d b e i j k 3.Add edges for new/spilled LRs -Stack ptr (almost) always interferes with everything so ISAs usually just reserve a reg for it. 4. Recolor and repeat until no new spill is generated

Time to Switch Gears – Research Topics!

Topics We Will Cover v 1. Automatic Parallelization v 2. Optimizing Streaming Applications for Multicore/GPUs v 3. Automatic SIMDization v 4. TBD

Paper Reviews v 1 per class for the rest of the semester v Paper review purpose »Read the paper before class – up to this point reading has not been a point of emphasis, now it is! »Put together a list of non-trivial observations – think about it! »Have something interesting to say in class v Review content – 2 parts » line summary of paper »2. Your thoughts/observations – it can be any of the following:  An interesting observation on some aspect of the paper  Raise one or more questions that a cursory reader would not think of  Identify a weakness of the approach and explain why  Propose an extension to the idea that fills some hole in the work  Pick a difficult part of the paper, decipher it, and explain it in your own words

Paper Reviews – continued v Review format »Plain text only -.txt file »½ page is sufficient v Reviews are due by the start of each lecture »Copy file to andrew.eecs.umich.edu:/y/submit »Put uniquename_classXX.txt v First reading – due Monday Nov 7 (pdf on the website) »“Revisiting the Sequential Programming Model for Multi-Core,” M. J. Bridges, N. Vachharajani, Y. Zhang, T. Jablin, and D. I. August, Proc 40th IEEE/ACM International Symposium on Microarchitecture, December 2007.