Register Allocation via Coloring of Chordal Graphs

Slides:



Advertisements
Similar presentations
CS 336 March 19, 2012 Tandy Warnow.
Advertisements

Register Allocation COS 320 David Walker (with thanks to Andrew Myers for many of these slides)
SSA-Based Register Allocation
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
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 320 David Walker (with thanks to Andrew Myers for most of the content of these slides)
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.
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.
Bayesian Networks, Winter Yoav Haimovitch & Ariel Raviv 1.
1 NP-Complete Problems. 2 We discuss some hard problems:  how hard? (computational complexity)  what makes them hard?  any solutions? Definitions 
Agenda: Problem statement Graph Construction Relation to graph problem Special Property Problem solution References.
Last time: terminology reminder w Simple graph Vertex = node Edge Degree Weight Neighbours Complete Dual Bipartite Planar Cycle Tree Path Circuit Components.
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.
Register Allocation (Slides from Andrew Myers). Main idea Want to replace temporary variables with some fixed set of registers First: need to know which.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Register Allocation (via graph coloring)
Technion 1 Generating minimum transitivity constraints in P-time for deciding Equality Logic Ofer Strichman and Mirron Rozanov Technion, Haifa, Israel.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Technion 1 (Yet another) decision procedure for Equality Logic Ofer Strichman and Orly Meir Technion.
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.
Steiner trees Algorithms and Networks. Steiner Trees2 Today Steiner trees: what and why? NP-completeness Approximation algorithms Preprocessing.
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.
Fixed Parameter Complexity Algorithms and Networks.
An Optimistic and Conservative Register Assignment Heuristic for Chordal Graphs Philip BriskAjay K. VermaPaolo Ienne International Conference on Compilers,
Perfect Gaussian Elimination and Chordality By Shashank Rao.
1 Treewidth, partial k-tree and chordal graphs Delpensum INF 334 Institutt fo informatikk Pinar Heggernes Speaker:
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Register Allocation John Cavazos University.
ANALYSIS AND IMPLEMENTATION OF GRAPH COLORING ALGORITHMS FOR REGISTER ALLOCATION By, Sumeeth K. C Vasanth K.
Interference Graphs for Programs in Static Single Information Form are Interval Graphs Philip Brisk Processor Architecture Laboratory (LAP) EPFL Lausanne,
Data Structures & Algorithms Graphs
Graph Colouring L09: Oct 10. This Lecture Graph coloring is another important problem in graph theory. It also has many applications, including the famous.
Computing Branchwidth via Efficient Triangulations and Blocks Authors: F.V. Fomin, F. Mazoit, I. Todinca Presented by: Elif Kolotoglu, ISE, Texas A&M University.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Interval Graph Test Wen-Lian Hsu.
Register Allocation Ajay Mathew Pereira and Palsberg. Register allocation via coloring of chordal graphs. APLOS'05Register allocation via coloring of chordal.
A Graph Theoretic Approach to Cache-Conscious Placement of Data for Direct Mapped Caches Mirza Beg and Peter van Beek University of Waterloo June
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Global Register Allocation Based on
Graph Coloring and Applications
An introduction to chordal graphs and clique trees
Mooly Sagiv html://
The compilation process
Static Single Assignment
Richard Anderson Lecture 26 NP-Completeness
Linear Scan Register Allocation Massimiliano Poletto, Vivek Sarkar A Fast, Memory-Efficient Register Allocation Framework for Embedded Systems Sathyanarayanan.
Algorithms and Networks
Register Allocation Noam Rinetzky Text book:
Philip Brisk Ajay K. Verma Paolo Ienne
Optimal Polynomial-Time Interprocedural Register Allocation for High-Level Synthesis Using SSA Form Philip Brisk Ajay K. Verma Paolo Ienne csda.
Control Flow Analysis (Chapter 7)
Data Flow Analysis Compiler Design
Final Code Generation and Code Optimization
Compiler Construction
Ch09 _2 Approximation algorithm
Fall Compiler Principles Lecture 13: Summary
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
(via graph coloring and spilling)
CS 201 Compiler Construction
Presentation transcript:

Register Allocation via Coloring of Chordal Graphs Fernando M Q Pereira Jens Palsberg UCLA

95% of the interference graphs produced from Java Methods by the JoeQ compiler are Chordal. Java 1.5 library: 95.5% (23,681 methods). Public ML graphs: 94.1% (27,921 IG’s). Not considering pre-colored registers. But, what is a chordal graph?

Why is this good news? Many problems that are NP-complete for general graphs are linear time for chordal graphs. Ex. graph coloring. Simpler register allocation algorithms, but still competitive! Chordal graphs have been focus of research during the 70’s. All these problems are O(|V| + |E|)

Register Allocation is Complicated… Iterated Register Coalescing [George and Appel 96] actual spill build simplify select coalesce potential spill freeze

The Proposed Algorithm. pre-spilling phase coloring phase coalescing phase SEO Simple Modular Efficient Works with non-chordal interference graphs.

Some terminology: Induced subgraphs: H = G[VH] Induced cycles: H is a cycle. Clique: H is a complete graph. A B A B C C D E D A B A B C Induced subgraph: delete some nodes, keep all the edges that existed among the original nodes. D E D E A B A C C D E D

Chordal Graphs. A graph G is chordal iff the size of the largest induced cycle is 3 (it is a triangle). non-chordal: Chordal: A B A B There are three basic ways to define a chordal graph: graphs without induced cycles with more than three nodes. intersection graphs of intervals on a tree. graphs that have a simplicial elimination ordering. Every time there is a cycle with more than three nodes, there is an edge connecting non-adjacent vertices. D E D E

Why are Interference graphs Chordal? Chordal graphs are the intersection graphs of subtrees of a tree: E D F The second definition of chordal graphs: intersections of subtrees of a tree. Intuitively, one can imagine the control flow graph as a tree, and the live ranges as subtrees. In this case, the interference graph would be chordal. B C A

But CFG’s are not trees… int m(int a, int d) { int b, c; if(a > 0) { b = 1; c = a; } else { c = 2; b = d; } return b + c; a b d c Control flow graphs are better described as series parallel graphs.

Interference graphs of programs in SSA form are chordal. Independently proved by Brisk[2005], Hack[2005], Bouchez[2005]. Intuition: The chordal graphs are the intersection graphs of subtrees of a tree. Live ranges in SSA are subtrees of the dominance tree.

Why only 95% of chordal graphs? Executable code is not in SSA form. SSA elimination. Phi-functions are abstract constructions. In executable code, phi functions are replaced by copy instructions. We call programs after SSA elimination Post-SSA programs. Some Post-SSA programs are non-chordal :(

The Proposed Algorithm. The pre-spilling version: pre-spilling phase coloring phase coalescing phase SEO The post-spilling version: Three requirements: Modularity: composed from separate building blocks; Efficiency: polynomial complexity; Produce good code. Must work for chordal and non-chordal graphs. coloring phase post-spilling phase coalescing phase SEO

The Example. Two registers available for allocation: R1 and R2 R1 R2 A 1 int B = R1; 2 int A = R2; 3 int F = 1; 4 int E = A + F; 5 int D = 0; 6 int C = D; 7 R2 = C + E; 8 R1 = B; A B C Solid lines denote interference between registers. dashed lines denote move related nodes. F E D

The Simplicial Elimination Ordering. pre-spilling phase coloring phase coalescing phase SEO Simplicial Elimination Ordering (SEO)

Simplicial Elimination Ordering B C Simplicial Elimination Ordering (SEO) F E D The most important feature we use in our algorithm is the so called simplicial elimination ordering. It is an ordering of the vertices of a graph. Every chordal graph has many simplicial elimination orderings But there are ordering that are not simplicial. Neighbors of N that precede N constitute a clique: S1 = (A, F, B, E, D, C, R2, R1) S2 = (R2, B, E, F, A, D, C, R1) But S3 = (R2, R1, D, F, E, C, A, B) is not a SEO. Why? } are SEO’s

A third definition of chordal graph. A graph G = (V, E) is chordal if, and only if, it has a simplicial elimination ordering [Dirac 61]. There exist O(|V| + |E|) algorithms to find a simplicial elimination ordering: Maximum Cardinality Search, Lexicographical Breadth First Search. Simplicial Elimination Ordering (SEO)

The Pre-Spilling Phase. coloring phase coalescing phase SEO The pre-spilling phase Most algorithms do spilling after at least one attempt of coloring the interference graph has been performed. But, with a chordal graph, it is possible to determine the minimum number of colors beforehand.

The Pre-Spilling Phase Chromatic number = size of largest clique. 1 - List all the maximal cliques in the graph. 2 - Remove nodes until all maximal cliques have K or less nodes. 2.1 - Which registers to remove? For each register r: n = number of big cliques that contain r. f = frequency of use. s = size of r’s live range. Spill factor = n * s / f The pre-spilling phase

The pre-spilling phase Only look into cliques greater than K = 2. R1 R2 A B C The pre-spilling phase F E D S1 = ( A, F, B, E, D, C, R2, R1 ) A A B B B B R2 F F E Node B is present in most of the cliques, and must be removed.

The pre-spilling phase Resulting graph: R1 R2 A C The pre-spilling phase F E D S1 = ( A, F, E, D, C, R2, R1 ) A F E B B R2 Node B is present in most of the cliques, and must be removed.

The Coloring Phase. pre-spilling phase coloring phase coalescing phase SEO The coloring phase

Coloring Chordal Graphs. Feed the greedy coloring with a simplicial elimination ordering. C The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Coloring Chordal Graphs. The coloring phase R1 R2 A F E D S1 = ( A, F, E, D, C, R2, R1 )

Register Coalescing. pre-spilling phase coloring phase coalescing SEO The coalescing phase

Register Coalescing Greedy coalescing after register allocation. Why not before graph coloring? The coalescing phase Algorithm: Register Coalescing Input: (G, color(G)) Output: (G, color’(G)) begin for every non-interfering move instruction (x := y) do let color(x) = color(y) = unused color(N(x) U N(y)); end

Register Coalescing R2 R1 The coalescing phase D A F E C

Register Coalescing R2 R1 The coalescing phase D A F E C

The Post-Spilling Phase. coloring phase post-spilling phase coalescing phase SEO The Post-Spilling Phase Remove nodes assigned same color. E.g: Remove least used color. Remove greatest color. Faster implementation, but generates worse code.

What about a Non-Chordal Graph? Coloring is no longer optimal. The number of colors will be between the optimal, and twice the optimal for almost every possible graph [Bollobas 1988].

Benchmark The Java 1.5 standard libraries. 23,681 methods. Algorithms implemented in the JoeQ framework. Two test cases: Code without any transformation: 90% chordal. Programs in Post-SSA form: 95% chordal.

Non-transformed Programs Chordal coloring 16 registers 18 registers IRC # registers / method 4.13 4.20 4.25 # spills / method 0.0055 0.0044 0.0050 Total # spill 131 105 115 Maximum # spill 17 15 16 Coalescing / moves 0.29 0.34 0.31

Post-SSA Programs Chordal coloring 16 registers 18 registers IRC # registers / method 4.12 4.13 4.17 # spills / method 0.0053 0.0040 0.0049 Total # spill 125 94 118 Maximum # spill 16 17 27 Coalescing / moves 0.68 0.72 0.70

Methods in Java 1.5 23,681 methods; 22,544 chordal methods. 85% methods could be colored with 6 regs. 99.8% could be colored with 16 regs. 28 methods demanded more than 16 regs.

Time and Complexity: G = (V, E) SEO: O(|V| + |E|); Pre-spilling: O(|V| + |E|); Coloring: O(|E|); Coalescing: O(|V|3); coloring spilling coalescing pre-spilling Largest color Least used color

Related Work. All the 27,921 public ML interference graphs are 1-perfect [Andersson 2003]. Structured programs have 1-perfect IG? Polynomial register allocation [Brisk 2005], [Hack 2005]. SSA-Interference graphs are chordal.

Conclusions Many interference graphs of structured programs are chordal; New algorithm: Modular; Efficient; Competitive; We have an extended version of the algorithm implemented on top of GCC: http://compilers.cs.ucla.edu/fernando/projects/

Are Java Interference Graphs 1-Perfect? 1-Perfect graph: minimum coloring equals largest clique. It is different of perfect graphs. All the 27,921 IG of the ML compiler compiling itself are 1-perfect [Andersson, 2003]. Not considering pre-colored registers: 94.5% of chordal graphs.

SSA and Post-SSA Graphs. SSA interference graphs: Chordal Perfect 1-Perfect Post SSA graphs: If phi functions are replaced by copy instructions, than register allocation is NP-complete.

Non-1-Perfect Example d a b e c int m(it a, int d) { int e, c; if(in() > 0) { e = 0; c = d; } else { b = 0; c = a; e = b; } return c + e; d a b e c

The Post SSA Interference Graph. d a x e2 b b = 0; c1 = d; e1 = b; e2 = 0; c2 = d; c2 c1 e = e2; c = c2; c = c1; e = e1; e e1 return c + e; c

References [Andersson 2003] Christian Andersson, Register Allocation by Optimal Graph Coloring, 12th Conference on Compiler Construction [Brisk 2005] Philip Brisk and Foad Dabiri and Jamie Macbeth and Majid Sarrafzadeh, Polynomial-Time Graph Coloring Register Allocation, 14th International Workshop on Logic and Synthesis [Hack 2005] Sebastian Hack and Daniel Grund and Gerhard Goos, Towards Register Allocation for Programs in SSA-form.