Register Allocation Ajay Mathew Pereira and Palsberg. Register allocation via coloring of chordal graphs. APLOS'05Register allocation via coloring of chordal.

Slides:



Advertisements
Similar presentations
SSA-Based Register Allocation
Advertisements

Comparison and Evaluation of Back Translation Algorithms for Static Single Assignment Form Masataka Sassa #, Masaki Kohama + and Yo Ito # # Dept. of Mathematical.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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 after Classical SSA Elimination is NP-complete Fernando M Q Pereira Jens Palsberg - UCLA - The University of California, Los Angeles.
1 Authors: Vugranam C. Sreedhar, Roy Dz-Ching Ju, David M. Gilles and Vatsa Santhanam Reader: Pushpinder Kaur Chouhan Translating Out of Static Single.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Bayesian Networks, Winter Yoav Haimovitch & Ariel Raviv 1.
1 NP-completeness Lecture 2: Jan P The class of problems that can be solved in polynomial time. e.g. gcd, shortest path, prime, etc. There are many.
1 Optimistic Register Coalescing Sobeeh Almukhaizim UC San Diego Computer Science & Engineering Based on the work of: J. Park and S. Moon School of Electrical.
1 NP-Complete Problems. 2 We discuss some hard problems:  how hard? (computational complexity)  what makes them hard?  any solutions? Definitions 
CSC5160 Topics in Algorithms Tutorial 2 Introduction to NP-Complete Problems Feb Jerry Le
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
Optimal Polynomial Time Algorithms for Register Assignment Presented at the Chinese University of Hong Kong - Fernando M. Q. Pereira - August 28 th, 2007.
Cpeg421-08S/final-review1 Course Review Tom St. John.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Approximation Algorithms
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Register Allocation (via graph coloring)
Analysis of Algorithms CS 477/677
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
CSE 421 Algorithms Richard Anderson Lecture 27 NP Completeness.
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
ECE Synthesis & Verification - Lecture 4 1 ECE 697B (667) Spring 2006 ECE 697B (667) Spring 2006 Synthesis and Verification of Digital Circuits Allocation:
Improving Code Generation Honors Compilers April 16 th 2002.
Chapter 5: Computational Complexity of Area Minimization in Multi-Layer Channel Routing and an Efficient Algorithm Presented by Md. Raqibul Hasan Std No.
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.
The Theory of NP-Completeness 1. Nondeterministic algorithms A nondeterminstic algorithm consists of phase 1: guessing phase 2: checking If the checking.
MCS312: NP-completeness and Approximation Algorithms
Complexity Classes (Ch. 34) The class P: class of problems that can be solved in time that is polynomial in the size of the input, n. if input size is.
An Optimistic and Conservative Register Assignment Heuristic for Chordal Graphs Philip BriskAjay K. VermaPaolo Ienne International Conference on Compilers,
Lecture 22 More NPC problems
MIT and James Orlin1 NP-completeness in 2005.
1 Chapter 10 Extending the Limits of Tractability Slides by Kevin Wayne Pearson-Addison Wesley. All rights reserved.
Approximation Algorithms
CSE 024: Design & Analysis of Algorithms Chapter 9: NP Completeness Sedgewick Chp:40 David Luebke’s Course Notes / University of Virginia, Computer Science.
1 Variable Elimination Graphical Models – Carlos Guestrin Carnegie Mellon University October 11 th, 2006 Readings: K&F: 8.1, 8.2, 8.3,
Interference Graphs for Programs in Static Single Information Form are Interval Graphs Philip Brisk Processor Architecture Laboratory (LAP) EPFL Lausanne,
Techniques for Proving NP-Completeness Show that a special case of the problem you are interested in is NP- complete. For example: The problem of finding.
1 Chapter 8 NP and Computational Intractability Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Data Structures & Algorithms Graphs
Unit 9: Coping with NP-Completeness
NP-COMPLETE PROBLEMS. Admin  Two more assignments…  No office hours on tomorrow.
Twenty Years of EPT Graphs: From Haifa to Rostock Martin Charles Golumbic Caesarea Rothschild Institute University of Haifa With thanks to my research.
CSE 421 Algorithms Richard Anderson Lecture 27 NP-Completeness and course wrap up.
Complete Graphs A complete graph is one where there is an edge between every two nodes A C B G.
NP-Complete problems.
Graph Coloring. Vertex Coloring problem in VLSI routing channels Standard cells Share a track Minimize channel width- assign horizontal Metal wires to.
CS 3343: Analysis of Algorithms Lecture 25: P and NP Some slides courtesy of Carola Wenk.
CSE 589 Part V One of the symptoms of an approaching nervous breakdown is the belief that one’s work is terribly important. Bertrand Russell.
Computing Branchwidth via Efficient Triangulations and Blocks Authors: F.V. Fomin, F. Mazoit, I. Todinca Presented by: Elif Kolotoglu, ISE, Texas A&M University.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 30, 2014.
CSE 421 Algorithms Richard Anderson Lecture 27 NP-Completeness Proofs.
The Theory of NP-Completeness 1. Nondeterministic algorithms A nondeterminstic algorithm consists of phase 1: guessing phase 2: checking If the checking.
Introduction to NP Instructor: Neelima Gupta 1.
1 Variable Elimination Graphical Models – Carlos Guestrin Carnegie Mellon University October 15 th, 2008 Readings: K&F: 8.1, 8.2, 8.3,
COSC 3101A - Design and Analysis of Algorithms 14 NP-Completeness.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
TU/e Algorithms (2IL15) – Lecture 10 1 NP-Completeness, II.
Graph Coloring and Applications
Optimal Polynomial-Time Interprocedural Register Allocation for High-Level Synthesis Using SSA Form Philip Brisk Ajay K. Verma Paolo Ienne csda.
Register Allocation via Coloring of Chordal Graphs
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Register Allocation Ajay Mathew Pereira and Palsberg. Register allocation via coloring of chordal graphs. APLOS'05Register allocation via coloring of chordal graphs Pereira and Palsberg. Register allocation after classical SSA elimination is NP- complete. FOSSACS'06Register allocation after classical SSA elimination is NP- complete

Register Allocation –Recap Interference graph Graph coloring NP Complete- Chaitin’s proof Heuristics- priority coloring, Kempe’s method Some examples and graphics are borrowed from the ASPLOS’05 paper

George and Appel’s iterative spilling algorithm

Drawback of George and Appel’s iterative spilling algorithm- spilling and coalescing very complex. The interference relations b/w temporary variables can form any possible graph- Chaitin

Chordal Graph Chord- an edge which is not part of the cycle but which connects two vertices on the cycle. A graph is chordal if every cycle with four or more edges has a chord.

useful properties Minimum coloring-- O(E + V ) time maximum clique maximum independent set minimum covering by cliques All these NP complete problems in general graphs are now solvable in polynomial time.

Simplicial Elimination Ordering A vertex v is called simplicial if its neighborhood in G is a clique. A SEO of G is a bijection V (G) -->{1…V}, such that every vertex v i is a simplicial vertex in the subgraph induced by {v 1,…. v i }

{b; a; c; d} is a simplicial elimination ordering.

An undirected graph without self-loops is chordal if and only if it has a simplicial elimination ordering. (Dirac)

Algorithm

The maximum cardinality search algorithm(MCS)

The greedy coloring algorithm

Algorithm

Post spilling polynomial algorithm-- O(V K ) the greedy coloring tends to use the lower colors first.

Coalescing For each instruction a := b, look for a color c not used in N(a) [ N(b), If such a color exists, then the temporaries a and b are coalesced into a single register with the color c.

Coalescing

Pre-spilling Maintains k-colorable property removes nodes to bring the size of the largest clique down to the number of available colors

iterated register coalescing algorithm. New algorithm

Caveats entire run-time library of the standard Java 1.5 distribution Loop variables – spilling ? JoeQ compiler(John Whaley) IR- a set of instructions (quads- operator + 4 operands) organized into a control flow graph control flow can potentially exit from the middle of a basic block

Register Allocation after Classical SSA Elimination is NP-complete Sumit Kumar Jha Some examples and graphics are borrowed from the FOSSACS’06 paper and talk by the author Fernando M Q Pereira.

Talk Outline Background on “old” complexity results in register allocation SSA form and the register allocation problem Circular Graphs and Post-SSA Circular Graphs The Reduction of coloring Circular graphs to register allocation after SSA elimination. The Big Picture

Core register allocation problem Instance: a program P and a number N of available registers. Problem: Can each of the temporaries of P be mapped to one of the N registers such that temporary variables with interfering live ranges are assigned to different registers? NP Complete

Chaitin’s Proof Chaitin et al. showed in 1981 that the core register allocation problem is NP-complete They used a reduction from the graph coloring problem. –The essence of Chaitin et al.'s proof is that every graph is the interference graph of some program.

Static Single Assignment (SSA) SSA form. Static single assignment (SSA) form is an intermediate representation used in many compilers like gcc 4. If a program is in SSA form, then every variable is assigned exactly once, and each use refers to exactly one definition.

Register Allocation for SSA Programs Bouchez and Hack (2006) proved the result that strict programs in SSA form have chordal interference graphs. Chordal graphs can be colored in polynomial time! Strict program: Every path from the initial block to the use of a variable v passes through a definition of v.

SSA and Chordal interference graphs The core register allocation problem is NP- complete. [Chaitin 1981] Also, a compiler can transform a given program into SSA form in cubic time. We can color a chordal graph in linear time so we can solve the core register allocation problem for programs in SSA form in linear time. A contradiction!! Not really.

Register Allocation after conversion to SSA may be easier. Given a program P, its SSA-form version P0, and a number of registers K, the core register allocation problem (P,K) is not equivalent to (P0,K). –we can map a (P;K)-solution to a (P0;K)- solution, –we can not necessarily map a (P0;K)-solution to a (P;K)-solution. The SSA transformation splits the live ranges of temporaries in P in such a way that P0 may need fewer registers than P.

Why Chatin’s proof does not work after SSA elimination? (a) Chaitin et al.'s program to represent C4. (b) The interference graph of the original program

Chatin’s proof does not work after SSA elimination

What we learnt till now… Core Register Allocation is NP Complete. Chatin’s NP completeness proof does not work after classical SSA elimination. The solution obtained by analyzing a SSA form program [chordal graphs] in polynomial time can not be mapped back to the original program. So, the question if Register Allocation after SSA elimination is NP complete remains open.

The current approach The authors identify a subset of graphs called circular graphs such that –They are NP-hard to color –It is possible to write a program P(C) such that is core register allocation uses N+a registers after SSA elimination if and only if the circular graph C is N-colorable. The authors introduce an intermediate step SSA-Circular graphs to move from the circular graph C to the program P(C)

The current approach Circular-arc graph Post-SSA graph Simple Post-SSA Program Register Assignment Register To colors Arcs to arcs int m(int a1,int e1, int t1,int i1){ int a = a1; int e = e1; int t = t1; int i = i1; while(i < 100) { int i2 = i + 1; > i = i2; a = a2; t = t2; e = e2; } return a; }

Circular Graph A circular graph is an undirected graph given by a finite set of vertices V \subset N X N, such that We sometimes refer to a vertex (d, u) as an interval, and we call d, u as extreme points. Two vertices (d,u), (d’,u’) are connected by an edge if and only if b(d,u) \cap b(d’,u’) \neq \phi.

Circular Graphs and SSA-Circular Graphs - I Theorem: Finding a minimal coloring for a circular-arc graph is NP-complete.

Circular Graphs and SSA-Circular Graphs - II d 1 = … ; c 1 = … ; a = c; b = d; c 2 = a+1; d 2 = b+1; d c d 1,d 2 c 1,c 2 = c b c d b c 2 d 2 a a c 2 d 1 d 1 d d 2 c 1 c 1 SSA-form

SSA and Chordal interference graphs (a) C5 represented as a set of intervals. (b) The set of intervals that represent W = F(C5; 3). (c) W represented as a graph.

Circular Graphs and SSA-Circular Graphs - III 1) int d1 = … ; 2) int c1 = … ; 3) d = d1; 4) c = c1; 5) while ( … ) { 6) int a = c; 7) int b = d; 8) c2 = a+1; 9) d2 = b+1; 10) d = d2; 11) c = c2; 12) } b c2c2 b c a a c=c 2 d2d2 d d=d 2 Post-SSA program Post-SSA graph

Circular Graphs and SSA-Circular Graphs - IV Result In Paper: Circular-arc graph has N coloring iff SSA-graph has N coloring.

The Reduction - I int m(int a,int e,int i){ while(i < 100) { i = i + 1; if (e > 10) break; int b = i + 11; if (a > 11) break; int c = i + 12; if(b > 12) break; int d = i + 13; if(c > 13) break; e = i + 14; if(d > 14) break; a = i + 15; } return a; } Given a circular graph, can we find a program such that the program needs K registers iff the circular graph is k-colorable?

The Reduction - II int m(int a,int e, int t,int i){ while(i < 100) { i = i + 1; if(t > 9) break; if(e > 10) break; int b = i + 11; if (a > 11) break; int c = i + 12; if(b > 12) break; int d = i + 13; if(c > 13) break; e = i + 14; if(d > 14) break; a = i + 15; t = i + 16; } return a; }

After SSA-elimination … int m(int a1, int e1, int t1, int i1) { int a = a1; int e = e1; int t = t1; int i = i1; while(i > 10) { int i2 = i + 1; > i = i2; a = a2; t = t2; e = e2; } return a; } N + a register assignment  N coloring

Current View – Register Allocation Target program SSA-form Code ’ NP Complete (Chatin 1981) Polynomial RA NP Complete (Current Work) Classical SSA Elimination

Questions?

Thanks

The Reduction Given a circular graph, can we find a program such that the program needs K registers iff the circular graph is k-colorable?

Static Single Assignment (SSA) An equation such as V = M, where V is a n- dimensional vector, and M is a n*m matrix, contains n -functions such as a i = Φ(a i1,a i2, ………a im ). The live ranges of temporaries in the same column of a matrix overlap, while the live ranges of temporaries in the same row do not overlap. The classical approach to SSA-elimination replaces the Φ-functions with copy instructions.

Control flow of simple SSA programs The grammar for simple post- SSA programs.

SSA and Chordal interference graphs An SSA-circular graph W is a circular graph with two additional properties: Property (1) says that for each interval in W l there is an interval in W z so that these intervals share an extreme point y.

Circular to SSA Circular Graphs