Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 4/20/06 Exploiting Instruction-Level Parallelism with Software Approaches Original by Prof. David A. Patterson.

Similar presentations


Presentation on theme: "1 4/20/06 Exploiting Instruction-Level Parallelism with Software Approaches Original by Prof. David A. Patterson."— Presentation transcript:

1 1 4/20/06 Exploiting Instruction-Level Parallelism with Software Approaches Original by Prof. David A. Patterson

2 2 4/20/06 Basic Pipeline Scheduling and Loop Unrolling This code, a scalar to an array: for (i=1000; i>0; i=i–1) x[i] = x[i] + s; Assume following latency all examples –A dependent instruction must be separated from the source instruction by a distance in clock cycles equal to latency of the source instruction to avoid stall –5-stage pipeline From 柯皓仁, 交通大學

3 3 4/20/06 FP Loop: Where are the Hazards? Loop:L.DF0,0(R1);F0=array element ADD.DF4,F0,F2;add scalar from F2 S.DF4,0(R1);store result DADDUIR1,R1,#-8;decrement pointer 8Bytes BNER1,R2,Loop;branch R1!=R2 Where are the stalls? First translate into MIPS code:

4 4 4/20/06 FP Loop Showing Stalls 10 clocks: Rewrite code to minimize stalls? 1 Loop:L.DF0,0(R1);F0=vector element 2stall 3ADD.DF4,F0,F2;add scalar in F2 4stall 5stall 6 S.DF4,0(R1);store result 7 DADDUIR1,R1,#-8;decrement pointer 8Bytes 8stall 9 BNER1,R2,Loop;branch R1!=zero 10stall;delayed branch slot

5 5 4/20/06 Revised FP Loop Minimizing Stalls 6 clocks, but just 3 for execution, 3 for loop overhead; How make faster? InstructionInstructionLatency in producing resultusing result clock cycles FP ALU opAnother FP ALU op3 FP ALU opStore double2 Load doubleFP ALU op1 1 Loop:L.DF0,0(R1) 2DADDUIR1,R1,#-8 3ADD.DF4,F0,F2 4stall 5BNER1,R2,Loop;delayed branch 6 S.DF4,8(R1) ;altered & interchanged with DADDUI Swap BNE and S.D by changing address of S.D

6 6 4/20/06 Loop Unrolling Replicate the loop body multiple times and adjust the loop termination code Take n loop bodies and concatenate them into 1 basic block.

7 7 4/20/06 1 Loop:L.DF0,0(R1) 2ADD.DF4,F0,F2 3S.DF4,0(R1) ;drop DADDUI & BNE 4L.DF6,-8(R1) 5ADD.DF8,F6,F2 6S.DF8,-8(R1) ;drop DADDUI & BNE 7L.DF10,-16(R1) 8ADD.DF12,F10,F2 9S.DF12,-16(R1) ;drop DADDUI & BNE 10L.DF14,-24(R1) 11ADD.DF16,F14,F2 12S.DF16,-24(R1) 13DADDUIR1,R1,#-32;alter to 4*-8 14BNER1,R2,LOOP x (1+2)+2 = 28 clock cycles, or 7 per iteration Assumes R1 is multiple of 4 Unroll Loop Four Times (straightforward way) 2 cycles stall 1 cycle stall

8 8 4/20/06 Unrolled Loop Detail Do not usually know upper bound of loop Suppose it is n, and we would like to unroll the loop to make k copies of the body Instead of a single unrolled loop, we generate a pair of consecutive loops: –1st executes (n mod k) times and has a body that is the original loop –2nd is the unrolled body surrounded by an outer loop that iterates (n/k) times –For large values of n, most of the execution time will be spent in the unrolled loop

9 9 4/20/06 Unrolled Loop That Minimizes Stalls What assumptions made when moved code? –OK to move store past DADDUI even though changes register –OK to move loads before stores: get right data? 1 Loop:L.DF0,0(R1) 2L.DF6,-8(R1) 3L.DF10,-16(R1) 4L.DF14,-24(R1) 5ADD.DF4,F0,F2 6ADD.DF8,F6,F2 7ADD.DF12,F10,F2 8ADD.DF16,F14,F2 9S.DF4,0(R1) 10S.DF8,-8(R1) 11DADDUIR1,R1,#-32 12S.DF12,16(R1) 13BNER1,R2,LOOP 14S.DF16,8(R1); 8-32 = clock cycles, or 3.5 per iteration

10 10 4/20/06 Steps Compiler Performed to Unroll Determine if it is legal to move the instructions and adjust the offset. Determine the unrolling loop would be useful by finding if the loop iterations were independent. Use different registers to avoid unnecessary constraints. Eliminate the extra tests and branches. Determine that the loads and stores in the unrolling loop can be interchanged. Schedule the code, preserving any dependences needed.

11 11 4/20/06 Data Dependence for the Loop Unrolling Loop:L.DF0,0(R1) ADD.DF4,F0,F2 S.DF4, 0(R1) DADDUIR1,R1,#-8 L.DF6,0(R1) ADD.DF8,F6,F2 S.DF8, 0(R1) DADDUIR1,R1,#-8 L.DF10,0(R1) ADD.DF12,F10,F2 S.DF12, 0(R1) DADDUIR1,R1,#-8 L.DF14,0(R1) ADD.DF16,F14,F2 S.DF16, 0(R1) DADDUIR1,R1,#-8 BNER1, R2, LOOP Loop unrolling and techniques affect dependences

12 12 4/20/06 Where are the name dependencies? 1 Loop:L.DF0,0(R1) 2ADD.DF4,F0,F2 3S.DF4,0(R1) ;drop DADDUI & BNE 4L.DF0,-8(R1) 5ADD.DF4,F0,F2 6S.DF4,-8(R1) ;drop DADDUI & BNE 7L.DF0,-16(R1) 8ADD.DF4,F0,F2 9S.DF4,-16(R1) ;drop DADDUI & BNE 10L.DF0,-24(R1) 11ADD.DF4,F0,F2 12S.DF4,-24(R1) 13DADDUIR1,R1,#-32;alter to 4*-8 14BNER1,R2,LOOP How can remove them?

13 13 4/20/06 Where are the name dependencies? 1 Loop:L.DF0,0(R1) 2ADD.DF4,F0,F2 3S.DF4,0(R1) ;drop DADDUI & BNE 4L.DF6,-8(R1) 5ADD.DF8,F6,F2 6S.DF8,-8(R1) ;drop DADDUI & BNE 7L.DF10,-16(R1) 8ADD.DF12,F10,F2 9S.DF12,-16(R1) ;drop DADDUI & BNE 10L.DF14,-24(R1) 11ADD.DF16,F14,F2 12S.DF16,-24(R1) 13DADDUIR1,R1,#-32;alter to 4*-8 14BNER1,R2,LOOP

14 14 4/20/06 Compiler Perspectives on Code Movement Name Dependencies are Hard to discover for Memory Accesses –Does 100(R4) = 20(R6)? –From different loop iterations, does 20(R6) = 20(R6)? Our example required compiler to know that if R1 doesn’t change then: 0(R1)  -8(R1)  -16(R1)  -24(R1) There were no dependencies between some loads and stores so they could be moved by each other

15 15 4/20/06 Limitation to Gains of Loop Unrolling A decrease in the amount of overhead amortized with each unroll –Unroll 4 times – 2 out 14 cycles are overhead  0.5 cycles per iteration –Unrolled 8 times  0.25 cycles per iteration Code size limitations –Large code size is not good for embedded computer –Large code size may increase cache miss rate Compiler limitations –Potential shortfall in registers that is created by aggressive unrolling and scheduling

16 16 4/20/06 Static Branch Prediction Simplest: Predict taken –average misprediction rate = untaken branch frequency, which for the SPEC programs is 34%. –Unfortunately, the misprediction rate ranges from not very accurate (59%) to highly accurate (9%) Predict on the basis of branch direction? –choosing backward-going branches to be taken (loop) –forward-going branches to be not taken (if) –SPEC programs, however, most forward-going branches are taken => predict taken is better Predict branches on the basis of profile information collected from earlier runs –Misprediction varies from 5% to 22%

17 17 4/20/06 Misprediction Rate for a Profile- based Predictor

18 18 4/20/06 Accuracy Comparison

19 19 4/20/06 Advanced Compiler Support for Exposing and Exploiting ILP Detecting and Enhancing loop-level parallelism Finding and eliminating dependent computations Software pipelining Global code scheduling

20 20 4/20/06 Detect and Enhance LLP Loop-level parallelism (ILP) –Analyzed at the source or close to it –Most analysis is done once instructions have been generated Loop-level analysis –Determine what dependences exist among the operands in a loop across the iterations of that loop –Determine whether data accesses in later iterations are dependent on data values produced in earlier iterations »Such dependence is called a loop-carried dependence (LCD) »LCD forces successive loop iterations to execute in series –Finding loop-level parallelism involves recognizing structures such as loops, array references, and induction variable computations »The compiler can do this analysis more easily at or near the source level

21 21 4/20/06 Example 1 for (i=1; i <= 100; i=i+1) { A[i+1] = A[i] + C[i]; /* S1 */ B[i+1] = B[i] + A[i+1];/* S2 */ } Assume that A, B, and C are distinct, non-overlapping arrays S1 uses a value computed by S1 in an earlier iteration (A[i]) –Loop-carried dependence S2 uses a value computed by S2 in an earlier iteration (B[i]) –Loop-carried dependence S2 uses a value computed by S1 in the same iteration (A[i+1]) –Not loop-carried –Multiple iterations of the loop could execute in parallel, as long as each pair of statements in an iteration were kept in order

22 22 4/20/06 Example 2 for (i=1; i <= 100; i=i+1) { A[i] = A[i] + B[i]; /* S1 */ B[i+1] = C[i] + D[i]; /* S2 */ } S1 uses a value computed by S2 in an earlier iteration (B[i+1]) –Loop-carried dependence Dependence is not circular –Neither statement depends on itself, and although S1 depends on S2, S2 does not depend on S1 A loop is parallel if it can be written without a cycle in the dependences –Absence of a cycle gives a partial ordering on the statements The loop-carried dependence does not prevent parallelism

23 23 4/20/06 Example 2 (Cont.) A[1] = A[1] + B[1] for (i=1; i <= 99; i=i+1) { B[i+1] = C[i] + D[i]; A[i+1] = A[i+1] + B[i+1]; } B[101] = C[100] + D[100] Transform the previous code to conform to the partial ordering and expose the parallelism

24 24 4/20/06 Recurrence for (i=2; i<=100; i=i+1) { Y[i] = Y[i-1] + Y[i]; } for (i=6; i<=100; i=i+1) { Y[i] = Y[i-5] + Y[i]; } A recurrence is when a variable is defined based on the value of that variable in an earlier iteration A form of loop-carried dependences Dependence distance –How long the earlier instruction for a recurrence –Larger distance more ILP

25 25 4/20/06 An Example to Eliminate False Dependences for (i=1; i <= 100; i=i+1) { Y[i] = X[i] / C; /* S1 */ X[i] = X[i] + C; /* S2 */ Z[i] = Y[i] + C; /* S3 */ Y[i] = C – Y[i]; /* S4 */ } for (i=1; i <= 100; i=i+1) { T[i] = X[i] / C; /* S1 */ X1[i] = X[i] + C; /* S2 */ Z[i] = T[i] + C; /* S3 */ Y[i] = C – T[i]; /* S4 */ } True dependence: S1  S3, S1  S4 (Y[i]) Antidependence: S1  S2 (X[i]) Antidependence: S3  S4 (Y[i]) Output dependence: S1  S4 (Y[i]) Antidependence: X  X1 Antidependence: Y  T Output dependence: Y  T X has been renamed to X1  Compiler replace X by X1 -- or – Copy X1 to X

26 26 4/20/06 Finding Dependences Why we do it? –Good scheduling of code –Determining which loops might contain parallelism –Eliminating name dependences Barriers to analysis of array-oriented dependence –Reference via pointers rather than predictable array indices –Array indexing is Indirect through another array: x[y[i]] (non- affine) –False dependency General: NP hard problem –Specific cases can be done precisely –Current problem: a lot of special cases that don’t apply often –The good general heuristic is the holy grail

27 27 4/20/06 Eliminating Dependent Computations – Techniques Eliminate or reduce a dependent computation – within a basic block and within loop Algebraic simplification + Copy propagation (eliminate operations that copy values within a basic block) Tree-height reduction – increase the code parallelism DADDUI R1, R2, #4 DADDUI R1, R1, #4 DADDUI R1, R2, #8 DADDUI R1, R2, R3 DADDUI R4, R1, R6 DADDUI R8, R4, R7 DADDUI R1, R2, R3 DADDUI R4, R6, R7 DADDUI R8, R1, R4

28 28 4/20/06 Eliminating Dependent Computations – Techniques (Cont.) Most compilers require that optimizations that rely on associativity (e.g. tree-height reduction) be explicitly enabled –Integer/FP arithmetic (range and precision) may lead to round-error Optimization related to recurrence –Recurrence: expressions whose value on one iteration is given by a function that depends on previous iteration –When a loop with a recurrence is unrolled, we may be able to algebraically optimized the unrolled loop, so that the recurrence need only be evaluated once per unrolled iteration »sum = sum + x  sum = sum + x1 + x2 + x3 + x4 + x5 (5 dependent operations)  sum = ((sum + x1) + (x2 + x3)) + (x4 + x5) (3 dependent operations)

29 29 4/20/06 Software Pipelining Observation: if iterations from loops are independent, then can get more ILP by taking instructions from different iterations Software pipelining: reorganizes loops so that each iteration is made from instructions chosen from different iterations of the original loop (~ Tomasulo in SW)

30 30 4/20/06 Software Pipelining Example Before: Unrolled 3 times 1 L.DF0,0(R1) 2 ADD.DF4,F0,F2 3 S.DF4,0(R1) 4 L.DF6,-8(R1) 5 ADD.DF8,F6,F2 6 S.DF8,-8(R1) 7 L.DF10,-16(R1) 8 ADD.DF12,F10,F2 9 S.DF12,-16(R1) 10 DADDUIR1,R1,# BNER1,R2,LOOP After: Software Pipelined 1 S.DF4,16(R1) ;Stores M[i] 2 ADD.DF4,F0,F2 ;Adds to M[i-1] 3 L.DF0,0(R1);Loads M[i-2] 4 DADDUIR1,R1,#-8 5 BNER1,R2,LOOP Symbolic Loop Unrolling – Maximize result-use distance – Less code space than unrolling – Fill & drain pipe only once per loop vs. once per each unrolled iteration in loop unrolling SW Pipeline Loop Unrolled overlapped ops Time 5 cycles per iteration Assume that DADDUI is scheduled before ADD.D, and L.D (with an adjusted offset) is placed in the branch delay slot

31 31 4/20/06 Hardware Support for Exposing More Parallelism at Compile-Time Conditional or Predicated Instructions –Discussed before in context of branch prediction –Conditional instruction execution Two-issue superscalar –First instruction slotSecond instruction slot LW R1,40(R2)ADD R3,R4,R5 ADD R6,R3,R7 BEQZ R10,L LW R8,0(R10) LW R9,0(R8) –Waste slot since 3rd LW dependent on result of 2nd LW

32 32 4/20/06 Hardware Support for Exposing More Parallelism at Compile-Time Use predicated version load word (LWC)? –load occurs unless the third operand is 0 First instruction slotSecond instruction slot LW R1,40(R2)ADD R3,R4,R5 LWC R8,0(R10),R10ADD R6,R3,R7 BEQZ R10,L LW R9,0(R8) If the sequence following the branch were short, the entire block of code might be converted to predicated execution, and the branch eliminated

33 33 4/20/06 Hardware Support for Memory Reference Speculation To move loads across stores, when it cannot be absolutely certain that such a movement is correct, a special instruction to check for address conflicts –The special instruction is left at the original location of the load and the load is moved up across stores –When a speculated load is executed, the hardware saves the address of the accessed memory location –If a subsequent store changes the location before the check instruction, then the speculation has failed –If only load instruction was speculated, then it suffices to redo the load at the point of the check instruction

34 34 4/20/06 What if Can Chance Instruction Set? Superscalar processors decide on the fly how many instructions to issue –HW complexity of Number of instructions to issue O(n 2 ) Why not allow compiler to schedule instruction level parallelism explicitly? Format the instructions in a potential issue packet so that HW need not check explicitly for dependences

35 35 4/20/06 VLIW: Very Large Instruction Word Each “instruction” has explicit coding for multiple operations –In IA-64, grouping called a “packet” –In Transmeta, grouping called a “molecule” (with “atoms” as ops) Tradeoff instruction space for simple decoding –The long instruction word has room for many operations –By definition, all the operations the compiler puts in the long instruction word are independent => execute in parallel –E.g., 2 integer operations, 2 FP ops, 2 Memory refs, 1 branch »16 to 24 bits per field => 7*16 or 112 bits to 7*24 or 168 bits wide –Need compiling technique that schedules across several branches

36 36 4/20/06 Recall that Unrolled Loop That Minimizes Stalls 1 Loop:L.DF0,0(R1) 2L.DF6,-8(R1) 3L.DF10,-16(R1) 4L.DF14,-24(R1) 5ADD.DF4,F0,F2 6ADD.DF8,F6,F2 7ADD.DF12,F10,F2 8ADD.DF16,F14,F2 9S.DF4,0(R1) 10S.DF8,-8(R1) 11DADDUIR1,R1,#-32 12S.DF12,16(R1) 13BNER1,R2,LOOP 14S.DF16,8(R1); 8-32 = clock cycles, or 3.5 per iteration

37 37 4/20/06 Loop Unrolling in VLIW Memory MemoryFPFPInt. op/Clock reference 1reference 2operation 1 op. 2 branch L.D F0,0(R1)L.D F6,-8(R1)1 L.D F10,-16(R1)L.D F14,-24(R1)2 L.D F18,-32(R1)L.D F22,-40(R1)ADD.D F4,F0,F2ADD.D F8,F6,F23 L.D F26,-48(R1)ADD.D F12,F10,F2ADD.D F16,F14,F24 ADD.D F20,F18,F2ADD.D F24,F22,F25 S.D F4,0(R1)S.D F8,-8(R1)ADD.D F28,F26,F26 S.D F12,-16(R1)S.D F16,-24(R1)7 S.D F20,24(R1)S.D F24,16(R1)DADDUI R1,R1,#-568 S.D F28,8(R1)BNE R1,R2,LOOP9 Unrolled 7 times to avoid delays 7 results in 9 clocks, or 1.3 clocks per iteration Average: 2.5 ops per clock Note: Need more registers in VLIW

38 38 4/20/06 Trace Scheduling Parallelism across IF branches vs. LOOP branches? Two steps: –Trace Selection »Find likely sequence of basic blocks (trace) of (statically predicted or profile predicted) long sequence of straight-line code –Trace Compaction »Squeeze trace into few VLIW instructions »Need bookkeeping code in case prediction is wrong This is a form of compiler-generated speculation –Compiler must generate “fixup” code to handle cases in which trace is not the taken branch –Needs extra registers: undoes bad guess by discarding

39 39 4/20/06 Advantages of HW (Tomasulo) vs. SW (VLIW) Speculation HW advantages: –HW better at memory disambiguation since knows actual addresses –HW better at branch prediction since lower overhead –HW maintains precise exception model –HW does not execute bookkeeping instructions –Same software works across multiple implementations –Smaller code size (not as many nops filling blank instructions) SW advantages: –Window of instructions that is examined for parallelism much higher –Much less hardware involved in VLIW (unless you are Intel…!) –More involved types of speculation can be done more easily –Speculation can be based on large-scale program behavior, not just local information

40 40 4/20/06 Superscalar vs. VLIW Smaller code size Binary compatibility across generations of hardware Simplified Hardware for decoding, issuing instructions No Interlock Hardware (compiler checks?) More registers, but simplified Hardware for Register Ports

41 41 4/20/06 Summary#1: Hardware versus Software Speculation Mechanisms To speculate extensively, must be able to disambiguate memory references –Much easier in HW than in SW for code with pointers HW-based speculation works better when control flow is unpredictable, and when HW-based branch prediction is superior to SW-based branch prediction done at compile time –Mispredictions mean wasted speculation HW-based speculation maintains precise exception model even for speculated instructions HW-based speculation does not require compensation or bookkeeping code

42 42 4/20/06 Summary#2: Hardware versus Software Speculation Mechanisms Compiler-based approaches may benefit from the ability to see further in the code sequence, resulting in better code scheduling HW-based speculation with dynamic scheduling does not require different code sequences to achieve good performance for different implementations of an architecture –may be the most important in the long run?

43 43 4/20/06 Summary #3: Software Scheduling Instruction Level Parallelism (ILP) found either by compiler or hardware. Loop level parallelism is easiest to see –SW dependencies/compiler sophistication determine if compiler can unroll loops –Memory dependencies hardest to determine => Memory disambiguation –Very sophisticated transformations available Trace Sceduling to Parallelize If statements Superscalar and VLIW: CPI 1) –Dynamic issue vs. Static issue –More instructions issue at same time => larger hazard penalty –Limitation is often number of instructions that you can successfully fetch and decode per cycle

44 44 4/20/06 Homework 4.6, 4.8, 4.10


Download ppt "1 4/20/06 Exploiting Instruction-Level Parallelism with Software Approaches Original by Prof. David A. Patterson."

Similar presentations


Ads by Google