k register IR Register Allocation IR Instruction Scheduling n), maybe O(n 2 ), but not O(2 n ) k register code

Similar documents
Global Register Allocation via Graph Coloring

Compiler Design. Register Allocation. Hwansoo Han

CS 406/534 Compiler Construction Instruction Scheduling

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Code generation for modern processors

Code generation for modern processors

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation

Global Register Allocation via Graph Coloring The Chaitin-Briggs Algorithm. Comp 412

CS 406/534 Compiler Construction Putting It All Together

Global Register Allocation

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

fast code (preserve flow of data)

Global Register Allocation - Part 2

Instruction Scheduling

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

Today More register allocation Clarifications from last time Finish improvements on basic graph coloring concept Procedure calls Interprocedural

Low-Level Issues. Register Allocation. Last lecture! Liveness analysis! Register allocation. ! More register allocation. ! Instruction scheduling

Register Allocation. Register Allocation. Local Register Allocation. Live range. Register Allocation for Loops

Register Allocation via Hierarchical Graph Coloring

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

CS415 Compilers. Instruction Scheduling and Lexical Analysis

Register Allocation (wrapup) & Code Scheduling. Constructing and Representing the Interference Graph. Adjacency List CS2210

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University

Rematerialization. Graph Coloring Register Allocation. Some expressions are especially simple to recompute: Last Time

The C2 Register Allocator. Niclas Adlertz

Register Allocation & Liveness Analysis

Instruction Selection and Scheduling

Topic 12: Register Allocation

Lecture Overview Register Allocation

CSE P 501 Compilers. Register Allocation Hal Perkins Autumn /22/ Hal Perkins & UW CSE P-1

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006

Compiler Architecture

CSC D70: Compiler Optimization Register Allocation

Register allocation. Overview

register allocation saves energy register allocation reduces memory accesses.

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

Code Generation. CS 540 George Mason University

Lecture 18 List Scheduling & Global Scheduling Carnegie Mellon

Lecture Compiler Backend

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Lecture 7. Instruction Scheduling. I. Basic Block Scheduling II. Global Scheduling (for Non-Numeric Code)

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Topic 14: Scheduling COS 320. Compiling Techniques. Princeton University Spring Lennart Beringer

Register Allocation. Michael O Boyle. February, 2014

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

EECS 583 Class 15 Register Allocation

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Register Allocation. Preston Briggs Reservoir Labs

Intermediate Representations Part II

Instruction Selection: Preliminaries. Comp 412

April 15, 2015 More Register Allocation 1. Problem Register values may change across procedure calls The allocator must be sensitive to this

Lab 3, Tutorial 1 Comp 412

Register allocation. instruction selection. machine code. register allocation. errors

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

Introduction to Optimization. CS434 Compiler Construction Joel Jones Department of Computer Science University of Alabama

CS5363 Final Review. cs5363 1

Lecture 21 CIS 341: COMPILERS

Compiler Optimisation

Pipelining and Exploiting Instruction-Level Parallelism (ILP)

Compilers and Code Optimization EDOARDO FUSELLA

Software Pipelining by Modulo Scheduling. Philip Sweany University of North Texas

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring

Global Register Allocation Based on Graph Fusion

Global Register Allocation - 2

Chapter 4. Advanced Pipelining and Instruction-Level Parallelism. In-Cheol Park Dept. of EE, KAIST

Register Allocation. Lecture 38

Abstract Interpretation Continued

Code generation for superscalar RISCprocessors. Characteristics of RISC processors. What are RISC and CISC? Processors with and without pipelining

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Register Allocation. CS 502 Lecture 14 11/25/08

Quality and Speed in Linear-Scan Register Allocation

SSA-Form Register Allocation

Register Allocation 1

Administration CS 412/413. Instruction ordering issues. Simplified architecture model. Examples. Impact of instruction ordering

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Intermediate Representations

Characteristics of RISC processors. Code generation for superscalar RISCprocessors. What are RISC and CISC? Processors with and without pipelining

Computer Science 246 Computer Architecture

CE431 Parallel Computer Architecture Spring Compile-time ILP extraction Modulo Scheduling

ECE 468, Fall Midterm 2

CS415 Compilers. Intermediate Represeation & Code Generation

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Global Register Allocation

Live Variable Analysis. Work List Iterative Algorithm Rehashed

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

Naming in OOLs and Storage Layout Comp 412

Multithreaded Processors. Department of Electrical Engineering Stanford University

16 Greedy Algorithms

Register Allocation in Just-in-Time Compilers: 15 Years of Linear Scan

Linear Scan Register Allocation. Kevin Millikin

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

Dataflow analysis (ctd.)

Computer Architecture and Engineering. CS152 Quiz #5. April 23rd, Professor Krste Asanovic. Name: Answer Key

c. What are the machine cycle times (in nanoseconds) of the non-pipelined and the pipelined implementations?

Transcription:

Register Allocation Part of the compiler s back end IR Instruction Selection m register IR Register Allocation k register IR Instruction Scheduling Machine code Errors Critical properties Produce correct code that uses k (or fewer) registers Minimize added loads and stores Minimize space used to hold spilled values Operate efficiently O(n), O(n log 2 n), maybe O(n 2 ), but not O(2 n ) CMSC430 Spring 2007 1 Global Register Allocation The big picture m register code Register Allocator k register code Optimal global allocation is NP-Complete, under almost any assumptions. At each point in the code 1 Determine which values will reside in registers 2 Select a register for each such value The goal is an allocation that minimizes running time Most modern, global allocators use a graph-coloring paradigm Build a conflict graph or interference graph Find a k-coloring for the graph, or change the code to a nearby problem that it can k-color CMSC430 Spring 2007 2 1

Global Register Allocation... store r4 x This is an assignment problem, not an allocation problem! load x r1... What s harder across multiple blocks? Could replace a load with a move Good assignment would obviate the move Must build a control-flow graph to understand inter-block flow Can spend an inordinate amount of time adjusting the allocation CMSC430 Spring 2007 3 Global Register Allocation... store r4 x... store r4 x What if one block has x in a register, but the other does not? load x r1... A more complex scenario Block with multiple predecessors in the control-flow graph Must get the right values in the right registers in each predecessor In a loop, a block can be its own predecessors This adds tremendous complications CMSC430 Spring 2007 4 2

Global Register Allocation Taking a global approach Abandon the distinction between local & global Make systematic use of registers or memory Adopt a general scheme to approximate a good allocation Graph coloring paradigm (Lavrov & (later) Chaitin) 1 Build an interference graph G I for the procedure > Computing LIVE is harder than in the local case > G I is not an interval graph 2 (try to) construct a k-coloring > Minimal coloring is NP-Complete > Spill placement becomes a critical issue 3 Map colors onto physical registers CMSC430 Spring 2007 5 Graph Coloring (A Background Digression) The problem A graph G is said to be k-colorable iff the nodes can be labeled with integers 1 k so that no edge in G connects two nodes with the same label Examples 2-colorable 3-colorable Each color can be mapped to a distinct physical register CMSC430 Spring 2007 6 3

Building the Interference Graph What is an interference? (or conflict) Two values interfere if there exists an operation where both are simultaneously live If x and y interfere, they cannot occupy the same register To compute interferences, we must know where values are live The interference graph, G I Nodes in G I represent values, or live ranges Edges in G I represent individual interferences > For x, y G I, <x,y> iff x and y interfere A k-coloring of G I can be mapped into an allocation to k registers CMSC430 Spring 2007 7 Building the Interference Graph To build the interference graph 1 Discover live ranges > Build SSA form > At each φ-function, take the union of the arguments 2 Compute LIVE sets for each block > Use an iterative data-flow solver > Solve equations for LIVE over domain of live range names 3 Iterate over each block > Track the current LIVE set > At each operation, add appropriate edges & update LIVE Edge from result to each value in LIVE Remove result from LIVE Edge from each operand to each value in LIVE CMSC430 Spring 2007 8 4

What is a Live Range? A set LR of definitions {d 1,d 2,,d n } such that for any two definitions d i and d j in LR, there exists some use u that is reached by both d i and d j. How can we compute live ranges? > For each basic block b in the program, compute REACHESOUT(b) the set of definitions that reach the exit of basic block b d REACHESOUT(b) if there is no other definition on some path from d to the end of block b > For each basic block b, compute LIVEIN(b) the set of variables that are live on entry to b v LIVEIN(b) if there is a path from the entry of b to a use of v that contains no definition of v > At any block where control flow joins, for each live variable v, merge the live ranges associated with definitions in REACHESOUT(p), for all predecessors of b, that assign a value to v. CMSC430 Spring 2007 9 Computing LIVE Sets A value v is live at p if a path from p to some use of v along which v is not re-defined Data-flow problems are expressed as simultaneous equations LIVEOUT(b) = s succ(b) LIVEIN(s) LIVEIN(b) = (LIVEOUT(b) NOTDEF(b)) IN(b) where IN(x) is the set of names used before redefinition in block x NOTDEF(x) is the set of names not redefined in x As output, LIVEOUT(x) is the set of names live on exit from block x LIVEIN(x) is the set of names live on entry to block x CMSC430 Spring 2007 10 5

Computing LIVE Sets The compiler solve the equations with an iterative algorithm WorkList { all blocks } while ( WorkList Ø) remove a block b from WorkList Compute LIVEOUT(b) Compute LIVEIN(b) if LIVEIN(b) changed then add pred (b) to WorkList The Worklist Iterative Algorithm Why does this work? LIVEOUT, LIVEIN 2 Names IN, NOTDEF are constant for b Equations are monotone Finite chains in the lattice will reach a fixed point! Speed of convergence depends on the order in which blocks are removed & their sets recomputed This is the world s quickest introduction to data-flow analysis! CMSC430 Spring 2007 11 Observation on Coloring for Register Allocation Suppose you have k registers try to color the graph with k colors Any vertex n that has fewer than k neighbors in the interference graph (n < k) can always be colored! > Pick any color not used by its neighbors there must be one Idea for Chaitin s algorithm: > Pick any vertex n such that n < k and put it on the stack > Remove that vertex and all edges incident from the interference graph This may make some new nodes have fewer than k neighbors > At the end, if some vertex n still has k or more neighbors, then spill the live range associated with n > Otherwise successively pop vertices off the stack and color them in the lowest color not used by some neighbor CMSC430 Spring 2007 12 6

Chaitin s Algorithm 1. While there are vertices with fewer than k neighbors remaining in the interference graph G I > Pick any vertex n such that n < k and put it on the stack > Remove that vertex and all edges incident to it from G I This may cause additional vertices to have fewer than k neighbors 2. If any vertices remain in the interference graph G I (all such vertices have k or more neighbors) then: > Pick a vertex n (using some heuristic condition) and spill the live range associated with n > Remove vertex n from G I, along with all edges incident to it and put it on the stack > If this causes some vertex in G I to have fewer than k neighbors, then go to step 1; otherwise, repeat step 2 3. Successively pop vertices off the stack and color them in the lowest color not used by some neighbor CMSC430 Spring 2007 13 Chaitin s Algorithm in Practice 3 Registers 2 1 4 5 3 Stack CMSC430 Spring 2007 14 7

Chaitin s Algorithm in Practice 3 Registers 2 4 5 3 1 Stack CMSC430 Spring 2007 15 Chaitin s Algorithm in Practice 3 Registers 4 5 2 1 3 Stack CMSC430 Spring 2007 16 8

Chaitin s Algorithm in Practice 3 Registers 5 4 2 1 3 Stack CMSC430 Spring 2007 17 Chaitin s Algorithm in Practice 3 Registers Colors: 5 3 4 2 1 1: 2: 3: Stack CMSC430 Spring 2007 18 9

Chaitin s Algorithm in Practice 3 Registers 3 4 2 1 Stack 5 Colors: 1: 2: 3: CMSC430 Spring 2007 19 Chaitin s Algorithm in Practice 3 Registers Colors: 4 2 1 Stack 3 5 1: 2: 3: CMSC430 Spring 2007 20 10

Chaitin s Algorithm in Practice 3 Registers 2 1 Stack 3 4 5 Colors: 1: 2: 3: CMSC430 Spring 2007 21 Chaitin s Algorithm in Practice 3 Registers 1 Stack 2 3 4 5 Colors: 1: 2: 3: CMSC430 Spring 2007 22 11

Chaitin s Algorithm in Practice 3 Registers 2 1 4 5 3 Colors: 1: 2: 3: Stack CMSC430 Spring 2007 23 Improvement in Coloring Scheme Due to Briggs, Cooper, Kennedy, and Torczon Instead of stopping at the end when all vertices have at least k neighbors, put each on the stack according to some priority > When you pop them off they may still color! 2 Registers: 2-colorable CMSC430 Spring 2007 24 12

Chaitin-Briggs Algorithm 1. While there are vertices with fewer than k neighbors remaining in the interference graph G I > Pick any vertex n such that n < k and put it on the stack > Remove that vertex and all edges incident to it from G I This may create vertices with fewer than k neighbors 2. If any vertices remain in the interference graph G I (all such vertices have k or more neighbors) then: > Pick a vertex n (using some heuristic condition), put it on the stack and remove vertex n from G I, along with all edges incident to it > If this causes some vertex in G I to have fewer than k neighbors, then go to step 1; otherwise, repeat step 2 3. Successively pop vertices off the stack and color them in the lowest color not used by some neighbor > If some vertex cannot be colored, then pick a live range to spill, spill it, and restart at step 1 CMSC430 Spring 2007 25 Chaitin Allocator (Bottom-up Coloring) renumber Build SSA, build live ranges, rename build coalesce spill costs simplify select spill Build the interference graph Fold unneeded copies LR x LR y, and < LR x,lr y > G I combine LR x & LR y Estimate cost for spilling each live range Remove nodes from the graph While stack is non-empty pop n, insert n into G I, & try to color it Spill uncolored definitions & uses while N is non-empty if n with n < k then push n onto stack else pick n to spill push n onto stack remove n from G I Chaitin s algorithm CMSC430 Spring 2007 26 13

Chaitin-Briggs Allocator (Bottom-up Coloring) renumber Build SSA, build live ranges, rename build coalesce spill costs simplify select Build the interference graph Fold unneeded copies LR x LR y, and < LR x,lr y > G I combine LR x & LR y Estimate cost for spilling each live range Remove nodes from the graph While stack is non-empty pop n, insert n into G I, & try to color it while N is non-empty if n with n < k then push n onto stack else pick n to spill push n onto stack remove n from G I spill Spill uncolored definitions & uses Briggs algorithm (1989) CMSC430 Spring 2007 27 Picking a Spill Candidate When n G I, n k, simplify must pick a spill candidate Chaitin s heuristic Minimize spill cost current degree If LR x has a negative spill cost, spill it pre-emptively > Cheaper to spill it than to keep it in a register If LR x has an infinite spill cost, it cannot be spilled > No value dies between its definition & its use Spill cost is weighted cost of loads & stores needed to spill x Bernstein et al. Suggest repeating simplify, select, & spill with several different spill choice heuristics & keeping the best CMSC430 Spring 2007 28 14

Other Improvements to Chaitin-Briggs Spilling partial live ranges Bergner introduced interference region spilling Limits spilling to regions of high demand for registers Splitting live ranges Simple idea break up one or more live ranges Lets allocator use different registers for distinct subranges Lets allocator spill subranges independently (use 1 spill location) Conservative coalescing Combining LR x LR y to form LR xy may increase register pressure Limit coalescing to case where LR xy < k Iterative form tries to coalesce before spilling CMSC430 Spring 2007 29 Chaitin-Briggs Allocator Strengths & weaknesses Precise interference graph Strong coalescing mechanism Handles register assignment well Runs fairly quickly (Bottom-up Global) Known to overspill in tight cases Interference graph has no geography Spills a live range everywhere Long blocks devolve into spilling by use counts Is improvement possible? With rising spill costs, aggressive transformations, & long blocks CMSC430 Spring 2007 30 15

What about Top-down Coloring? The Big Picture Use high-level priorities to rank live ranges Allocate registers for them in priority order Use coloring to assign specific registers to live ranges Use spill costs as priority function! Unconstrained must The Details receive a color! Separate constrained from unconstrained live ranges A live range is constrained if it has k neighbors in G I Color constrained live ranges first Reserve pool of local registers for spilling (or spill & iterate) Chow split live ranges before spilling them > Split into block-sized pieces > Recombine as long as < k CMSC430 Spring 2007 31 Tradeoffs in Global Allocator Design Top-down versus bottom-up Top-down uses high-level information Bottom-up uses low-level structural information Spilling Reserve registers versus iterative coloring Precise versus imprecise graph Precision allows coalescing Imprecision speeds up graph construction Big-iron precise, iterated, bottom-up JIT imprecise, reserve, b-u or t-d CMSC430 Spring 2007 32 16

Regional Approaches to Allocation Hierarchical Register Allocation (Koblenz & Callahan) Analyze control-flow graph to find hierarchy of tiles Perform allocation on individual tiles, innermost to outermost Use summary of tile to allocate surrounding tile Insert compensation code at tile boundaries (LR x LR y ) Strengths Decisions are largely local Use specialized methods on individual tiles Weaknesses Decisions are made on local information May insert too many Allocator runs in parallel copies Still, a promising idea Anecdotes suggest it is fairly effective Target machine is multi-threaded multiprocessor (Tera MTA) CMSC430 Spring 2007 33 Regional Approaches to Allocation Probabilistic Register Allocation (Proebsting & Fischer) Attempt to generalize from Best s algorithm (bottom-up, local) Generalizes furthest next use to a probability Perform an initial local allocation using estimated probabilities Follow this with a global phase > Compute a merit score for each LR as (benefit from x in a register = probability it stays in a register) > Allocate registers to LRs in priority order, by merit score, working from inner loops to outer loops > Use coloring to perform assignment among allocated LRs Little direct experience (either anecdotal or experimental) Combines top-down global with bottom-up local CMSC430 Spring 2007 34 17

Regional Approaches to Allocation Register Allocation via Fusion (Lueh, Adl-Tabatabi, Gross) Use regional information to drive global allocation Partition CFGs into regions & build interference graphs Ensure that each region is k-colorable Merge regions by fusing them along CFG edges > Maintain k-colorability by splitting along fused edge > Fuse in priority order computed during the graph partition Assign registers using int. graphs i.e., execution frequency Strengths Flexibility Fusion operator splits on low-frequency edges Weaknesses Choice of regions is critical Breaks down if region connections have many live values CMSC430 Spring 2007 35 List Scheduling Cycle 1 Ready leaves of P Active Ø while (Ready Active Ø) if (Ready Ø) then remove an op from Ready S(op) Cycle Active Active op Cycle Cycle + 1 for each op Active if (S(op) + delay(op) Cycle) then remove op from Active for each successor s of op in P if (s is ready) then Ready Ready s Removal in priority order Note: only one op per cycle op has completed execution If successor s operands are ready, put it on Ready CMSC430 Spring 2007 36 18

Detailed Scheduling Algorithm I Idea: Keep a collection of worklists W[c], one per cycle > We need MaxC = max delay + 1 such worklists Code: for each n N do begin count[n] := 0; earliest[n] = 0 end for each (n1,n2) E do begin count[n2] := count[n2] + 1; successors[n1] := successors[n1] {n2}; end for i := 0 to MaxC 1 do W[i] := ; Wcount := 0; for each n N do if count[n] = 0 then begin W[0] := W[0] {n}; Wcount := Wcount + 1; end c := 0; // c is the cycle number cw := 0;// cw is the number of the worklist for cycle c instr[c] := ; CMSC430 Spring 2007 37 Detailed Scheduling Algorithm II while Wcount > 0 do begin while W[cW] = do begin c := c + 1; instr[c] := ; cw := mod(cw+1,maxc); end nextc := mod(c+1,maxc); while W[cW] do begin Priority select and remove an arbitrary instruction x from W[cW]; if free issue units of type(x) on cycle c then begin instr[c] := instr[c] {x}; Wcount := Wcount - 1; for each y successors[x] do begin count[y] := count[y] 1; earliest[y] := max(earliest[y], c+delay(x)); if count[y] = 0 then begin loc := mod(earliest[y],maxc); W[loc] := W[loc] {y}; Wcount := Wcount + 1; end end else W[nextc] := W[nextc] {x}; end end CMSC430 Spring 2007 38 19

Instruction Scheduling (revisited) In an earlier lecture, we introduced list scheduling Efficient, greedy, local heuristic Technique of choice for more than 20 years How can the compiler improve on local list scheduling? Different priority functions & tie breakers Use forward & backward list scheduling (Figures 12.4 & 12.5) Increase size of region fed to scheduler (classic answer) Try other algorithms Little success with other algorithms on code written by humans Some compiler generated code defeats the list scheduler CMSC430 Spring 2007 39 What About OOO Execution? Out-of-order microprocessors should simplify scheduling Processor looks at a window in the instruction stream Processor executes operations as they are ready Processor (typically) renames registers for correctness Does this eliminate the need for instruction scheduling? For any finite window, a worst case schedule Depth-first schedule! > 100 operation window 110 for FU 1, 110 for FU 2 Schedule need not be perfect, but must be not bad OOO can compensate for mild variations in latency > Cache miss, infrequent stall, CMSC430 Spring 2007 40 20

Other Priority Functions Computing Ranks Maximum path length containing it > Favors critical path; tends towards depth-first Number of immediate successors in P Better with > 1 FU > Favors longer ready queue; tends toward breadth-first Total number of descendants in P > Favors heavily used values; tends toward breadth-first Add latency to node s rank > Favors long operations to cover their latencies Increment rank of if node contains a last use > Tends to shorten live ranges & decrease register pressure These can be used as priorities or as tie-breakers > Use randomization & repetition Decreases register use CMSC430 Spring 2007 41 Forward versus Backward List Scheduling Folk wisdom has long suggested doing both Some blocks amenable to forward scheduling Some blocks amenable to backward scheduling Conventional approach is to try both & keep best result Does it matter? Takes dependence graph that is both wide & deep Depends on detailed knowledge of the specific block See Figures 12.4 & 12.5 in EAC for a real example Advice Use several forward & several backward passes Use different priorities & tie breakers, or use randomization CMSC430 Spring 2007 42 21

How Well Does List Scheduling Do? Non-optimal list schedules (%) versus available parallelism 1 functional unit, randomly generated blocks of 10, 20, 50 ops Percent non-optimal ls 18 16 14 12 10 8 6 4 2 0 1 1.3 1.6 1.9 2.2 2.5 2.8 3.1 50 ops. 20 ops. 10 ops. At the peak, compiler should apply other techniques Measure parallelism in list scheduler Invoke stronger techniques when high-probability of payoff 3.4 3.7 Available Parallelism 4 4.3 4.6 4.9 5.2 Most codes fall here, unless the compiler transforms them for ILP. If the compiler transforms the code, it should avoid this area! CMSC430 Spring 2007 43 Scheduling over Larger Scopes Basic idea is simple Longer sequence of operations more opportunities Pick a multi-block path & treat it as a single block Add compensation code for other exits (& entries) Several distinct scopes Extended basic blocks > A sequence b 1, b 2,, b n where b i has 1 predecessor, 1 < i n Traces > An arbitrary acyclic path, usually chosen from trace data Loops > Think of source-language loop, can find arbitrary loops CMSC430 Spring 2007 44 22

Extended Basic Blocks An example b 1 Extended Basic Blocks b 1, b 2, b 4 b 1, b 3 b 2 b 4 b 5 b 3 b 5 b 6 Both b 5 and b 6 have > 1 predecessors b 6 Scheduling EBBs Treat EBB as a block Moving an operation across boundary can necessitate compensation code Can restrict motion to zero growth case with 12 to 13% improvement [LCTES 98] CMSC430 Spring 2007 45 Traces Technique developed for the Multiflow Computer b 2 b 1 b 3 1 Identify high-frequency path 2 Schedule it as if a single block 3 Insert compensation code 4 Schedule next important path Results b 4 b 5 b 6 b 1 Fast trip through common path Off-path quality declines Compensation code growth b 2 b 3 b 4 b 5 b 6 CMSC430 Spring 2007 46 23

Loop Scheduling (Software Pipelining) Loops deserve special attention Their bodies execute frequently They do most of the work in time-critical computations They often contain major holes & interlocks The ideas Schedule multiple iterations together Run several iterations concurrently Shorten initiation interval for overall speed > Cycles between initiation of different iterations > = length of computation kernel CMSC430 Spring 2007 47 Example loadi r0,0 r1 loadi r0,400 r2 floadai r0,c fr1 l0 floadai r1,a fr2 l1 fadd fr2,fr1 fr2 l2 fstoreai fr2 r1,b l3 addi r1,8 r1 l4 c mp_le r1,r2 r3 l5 cbr r1 l0,l6 2 cycle delay 3 cycle delay How fast (in cycles per iteration) can we execute this loop? CMSC430 Spring 2007 48 24

Minimum Number of Cycles in Kernel Machine resource constraint: > N u is the number of units of type u > I u is the number of instructions requiring a unit of type u > I u / N u is the minimum number of cycles required for one iteration of the loop based on unit u > max u I u / N u is the minimum number of cycles required for all units Slope constraint > If the loop computes a recurrence over k r cycles > And the total delay along the recurrence cycle is d r > Then each iteration is going to require d r / k r cycles to execute > max r d r / k r is the minimum number of cycles per iteration to compute recurrences CMSC430 Spring 2007 49 Example loadi r0,0 r1 loadi r0,400 r2 floadai r0,c fr1 l0 floadai r1,a fr2 l1 fadd fr2,fr1 fr2 l2 fstoreai fr2 r1,b l3 addi r1,8 r1 l4 c mp_le r1,r2 r3 l5 cbr r3 l0,l6 2 cycle delay 3 cycle delay Floating Pt Unit: Load/Store Unit: Integer Unit: 1 instruction 2 instructions 3 instructions 3 cycles minimum CMSC430 Spring 2007 50 25

Loop Scheduling Mechanics Determine lower bound on initiation interval > Number of issue slots Register constraint, too > Longest dependence chain Lay out a schedule of appropriate length Use list scheduling with a modulo cycle count > Could fail just try with one more cycle per iteration Add a pre-loop & a post-loop to fill & drain the pipeline It gets pretty intricate! Conditional control flow complicates matters even more CMSC430 Spring 2007 51 Example loadi r0,0 r1 loadi r0,400 r2 floadai r0,c fr1 l0 floadai r1,a fr2 l1 fadd fr2,fr1 fr2 l2 fstoreai fr2 r1,b l3 addi r1,8 r1 l4 c mp_le r1,r2 r3 l5 cbr r1 l0,l6 2 cycle delay 3 cycle delay Load/Store Unit floadai r1,a fr2 fstoreai fr3 r1,b-16 Integer Unit addir1,8 r1 cmp_le r1,r2 r3 cbr r3 l0,l6 Floating Point Unit fadd fr2,fr1 fr3 CMSC430 Spring 2007 52 26

Final Code ld r1,0 ld r2,400 fld fr1, c p1 fld fr2,a(r1); ai r1,r1,8 p2 comp r1,r2 p3 beq e1; fadd fr3,fr2,fr1 k1 fld fr2,a(r1); ai r1,r1,8 k2 comp r1,r2 k3 fstfr3,b-16(r1); blek1; fadd fr3,fr2,fr1 e1 nop e2 nop e3 fst fr3,b-8(r1) CMSC430 Spring 2007 53 27