puzzle solving
play

Puzzle solving Simone Campanoni simonec@eecs.northwestern.edu - PowerPoint PPT Presentation

Puzzle solving Simone Campanoni simonec@eecs.northwestern.edu Materials Research paper: Authors: Fernando Magno Quintao Pereira, Jens Palsberg Title: Register Allocation by Puzzle Solving Conference: PLDI 2008 Ph.D. thesis


  1. Puzzle solving Simone Campanoni simonec@eecs.northwestern.edu

  2. Materials • Research paper: • Authors: Fernando Magno Quintao Pereira, Jens Palsberg • Title: Register Allocation by Puzzle Solving • Conference: PLDI 2008 • Ph.D. thesis • Author: Fernando Magno Quintao Pereira • Title: Register Allocation by Puzzle Solving • UCLA 2008

  3. Register Allocation Generated-code run time A. Spill all variables B. Puzzle solving A C. Linear scan D. Graph coloring E. Integer linear programming C Equivalent quality of graph coloring B D E ... in significantly less time! Ideal Compilation time

  4. Outline • Register allocation abstractions • From a program to a collection of puzzles • Solve puzzles • From solved puzzles to assembly code

  5. A graph-coloring register allocator f f Interference graph, f Liveness Assign colors Register allocator analysis f with Code IN, OUT var spilled Interference graph analysis colored, f Interferences Spill analysis spill( f , var, prefix) Code generation Graph Interference coloring graph In this class: f without variables and • All variables have the same type with registers • A register can store any variable

  6. Graph coloring abstraction: a problem (:MyVeryImportantFunction MyVar1 MyVar2 MyVar3 0 • MyVar1 : 64 bits MyVar1 <- 2 MyVar2 : 32 bits • MyVar2 <- 40 Software • MyVar3 : 32 bits MyVar3 <- 0 MyVar3 += MyVar1 Hardware MyVar3 += MyVar2 r8 r9 print MyVar3 Register aliasing r8 can store either one 64-bit valuel or two 32-bit values • ) r9 can store 64 bit values • Can this be obtained by the graph-coloring algorithm you learned in this class?

  7. Puzzle Abstraction • Puzzle = board + pieces (1 area = 1 register) (variables) R8 r15 • Pieces cannot overlap • Some pieces are already placed on the board • Task: fit the remaining pieces on the board (register allocation)

  8. From register file to puzzle boards • Every area of a puzzle is divided in two rows (soon will be clear why) • Registers determine the shape of the puzzle board Register aliasing determines the #columns PowerPC ARM integer registers

  9. From register file to puzzle boards • Every area of a puzzle is divided in two rows (soon will be clear why) • Registers determine the shape of the puzzle board Register aliasing determines the #columns PowerPC SPARC v8 SPARC v9 ARM integer registers ARM float registers

  10. Puzzle pieces accepted by boards Our class ->

  11. Outline • Register allocation abstractions • From a program to a collection of puzzles • Solve puzzles • From solved puzzles to assembly code

  12. From a program to puzzle pieces 1. Convert a program into an elementary program A. Transform code into SSA form 2. Map the elementary program into puzzle pieces

  13. Static Single Assignment (SSA) representation • A variable is set only by one instruction in the function body myVar1 <- 5 myVar2 <- 7 myVar3 <- 42 • A static assignment can be executed more than once

  14. SSA and not SSA example float myF (float par1, float par2, float par3){ return (par1 * par2) + par3; } float myF(float par1, float par2, float par3) { A S S myVar1 = par1 * par2 T O myVar1 = myVar1 + par3 N ret myVar1} float myF(float par1, float par2, float par3) { myVar1 = par1 * par2 myVar2 = myVar1 + par3 SSA ret myVar2}

  15. What about joins? • Add Φ functions/nodes to model joins • One argument for each incoming branch • Operationally • selects one of the arguments based on how control flow reach this node • At code generation time, need to eliminate Φ nodes b = d + 1 b2 = d + 1 b2 = d + 1 b = c + 1 b1 = c + 1 b1 = c + 1 b3=Φ(b1, b2) If (b > N) If (? > N) If (b3 > N) SSA Still not SSA Not SSA

  16. Eliminating Φ • Basic idea: Φ represents facts that value of join may come from different paths • So just set along each possible path b2 = d + 1 b1 = c + 1 b2 = d + 1 b1 = c + 1 b3 = b2 b3 = b1 b3=Φ(b1, b2) If (b3 > N) If (b3 > N) Not SSA

  17. Eliminating Φ in practice • Copies performed at Φ may not be useful • Joined value may not be used later in the program (So why leave it in?) • Use dead code elimination to kill useless Φs • Register allocation maps the variables to machine registers

  18. From a program to puzzle pieces 1. Convert a program into an elementary program A. Transform code into SSA form B. Transform A into SSI form 2. Map the elementary program into puzzle pieces

  19. Static Single Information (SSI) form In a program in SSI form: • Every basic block ends with a π-function that renames the variables that are alive going out of the basic block If (b > 1) If (b > 1) (c1, c2) = π(c) … = c * 2 … = c2 * 2 … = c + 1 … = c1 + 1 SSI Not SSI

  20. SSA and SSI code b = d + 4 b2 = d + 4 b2 = d2 + 4 b = d + 1 b1 = d + 1 b1 = d1 + 1 b3=Φ(b1, b2) b3=Φ(b1, b2) If (b3 > 1) If (b > 1) If (b3 > 1) (c1, c2) = π(c) … = c * 2 … = c * 2 … = c2 * 2 … = c + 1 … = c + 1 … = c1 + 1 SSA and SSI Not SSA and not SSI SSA but not SSI

  21. From a program to puzzle pieces 1. Convert a program into an elementary program A. Transform code into SSA form B. Transform A into SSI form C. Insert in B parallel copies between every instruction pair 2. Map the elementary program into puzzle pieces

  22. Parallel copies • Rename variables in parallel (V1, X1, Y1, Z1, A1, B1) = (V, X, Y, Z, A, B) V1 = X1 + Y1 V = X + Y Z = A + B (V2, X2, Y2, Z2, A2, B2) = (V1, X1, Y1, Z1, A1, B1) Z2 = A2 + B2

  23. From a program to puzzle pieces 1. Convert a program into an elementary program A. Transform code into SSA form B. Transform A into SSI form C. Insert in B parallel copies between every instruction pair We have obtained an elementary program!

  24. Elementary form: an example

  25. From a program to puzzle pieces 1. Convert a program into an elementary program A. Transform code into its SSA form B. Transform code into its SSI form C. Insert parallel copies between every instruction pair 2. Map the elementary program into puzzle pieces

  26. Add puzzle boards

  27. Generating puzzle pieces • For each instruction i • Create one puzzle piece for each live-in and live-out variable • If the live range ends at i, then the puzzle piece is X • If the live range begins at i, then Z-piece • Otherwise Y-piece V1 (used later) = V2 (last use) + 3 r10 = r10 + 3

  28. Example

  29. Example

  30. Outline • Register allocation abstractions • From a program to a collection of puzzles • Solve puzzles • From solved puzzles to assembly code

  31. Solving type 1 puzzles • Approach proposed: complete one area at a time • For each area: • Pad a puzzle with size-1 X- and Z-pieces until the area of puzzle pieces == board Board with 1 pre-assigned piece Padding • Solve the puzzle

  32. Solving type 1 puzzles: a visual language Puzzle solver -> Statement+ • Rule = how to complete an area Statement -> Rule | Condition • Rule composed by pattern : Condition -> (Rule : Statement) what needs to be already filled Rule -> (match/not-match an area) strategy : what type of pieces to add and where • A rule r succeeds in an area a iff Area a i. r matches a and ii. pieces of the strategy of r are available

  33. Solving type 1 puzzles: a visual language Puzzle solver -> Statement+ Puzzle solver success Statement -> Rule | Condition • A program succeeds iff all statements succeeds Condition -> (Rule : Statement) • A rule r succeeds in an area a iff Rule -> i. r matches a ii. pieces of the strategy of r are available • A condition ( r : s) succeeds iff • r succeeds or • s succeeds • All rules of a condition must have the same pattern

  34. Solving type 1 puzzles: a visual language Puzzle solver -> Statement+ Puzzle solver execution Statement -> Rule | Condition o For each statement s1, …, sn Condition -> (Rule : Statement) v For each area a such that Rule -> the pattern of si matches a q Apply si to a q If si fails, terminate and report failure

  35. Program execution: an example • A puzzle solver 1. s1 matches a1 only 2. Apply s1 to a1 succeeds and returns this puzzle s1 s2 K Q • Puzzle K 3. s2 matches a2 only a1 a2 Q 4. Apply s2 to a2 R8 r9 A. Apply first rule of s2: fails Puzzle solved! K B. Apply second rule of s2: success Q

  36. Program execution: another example • A puzzle solver 1. s1 matches a1 only 2. Apply s1 to a1 A. Apply first rule of s1: success s1 s2 a1 a2 a3 x 3 y 1 y 2 x 1 x 2 • Puzzle a1 a2 a3 y 1 y 2 x 1 x 2 x 3 3. s2 matches a2 and a3 a1 a2 a3 4. Apply s2 to a2 x 1 y 1 x 3 a1 a2 a3 x 1 y 1 x 3 x 2 y 2 Puzzle solved! 5. Apply s2 to a3

  37. Program execution: yet another example • A puzzle solver 1. s1 matches a1 only 2. Apply s1 to a1 A. Apply first rule of s1: success s1 s1 s2 s2 a1 a2 a3 x 1 x 2 x 3 y 1 y 2 • Puzzle a1 a2 a3 y 1 y 2 x 1 x 2 x 3 3. s2 matches a2 and a3 4. Apply s2 to a2: fail Finding the right puzzle solver No 1-size x pieces, we used them all in s1 is the key!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend