1 / 47

Register Allocation via Coloring of Chordal Graphs

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. But, what is a chordal graph?. Java 1.5 library: 95.5% (23,681 methods).

mandar
Download Presentation

Register Allocation via Coloring of Chordal Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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!

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

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

  6. Some terminology: A B A B • Induced subgraphs: H = G[VH] • Induced cycles: H is a cycle. • Clique: H is a complete graph. C C D E D A B A B C D E D E A B A C C D E D

  7. 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 D E D E

  8. Why are Interference graphs Chordal? • Chordal graphs are the intersection graphs of subtrees of a tree: E D F B C A

  9. 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

  10. 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.

  11. 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 :(

  12. The Proposed Algorithm. The pre-spilling version: coloring phase coalescing phase pre-spilling phase SEO The post-spilling version: post-spilling phase coalescing phase coloring phase SEO

  13. The Example. Two registers available for allocation: R1 and R2 R1 R2 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 F E D

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

  15. Simplicial Elimination Ordering R1 R2 A B C Simplicial Elimination Ordering (SEO) F E D 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

  16. 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)

  17. The Pre-Spilling Phase. coloring phase coalescing phase pre-spilling phase SEO The pre-spilling phase

  18. 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

  19. 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.

  20. 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.

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

  22. 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 )

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

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

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

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

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

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

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

  30. 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

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

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

  33. 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.

  34. 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].

  35. 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.

  36. Non-transformed Programs

  37. Post-SSA Programs

  38. 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.

  39. 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

  40. 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.

  41. 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/

  42. 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.

  43. 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.

  44. Non-1-Perfect Example 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

  45. 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

  46. 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.

More Related