Download Leveraging Instruction-Level Parallelism: Software Approaches for Performance and more Slides Computer Science in PDF only on Docsity!
Chapter 4
Exploiting Instruction-Level
Parallelism with Software
Approaches
Basic Compiler Techniques for
Exposing
- Basic pipeline scheduling and loop unrolling
- To keep a pipeline full, parallelism among instructions
must be exploited by finding sequences of unrelated
instructions that can be overlapped in the pipeline.
- A compiler’s ability to perform such kind of scheduling
depends on both the amount of ILP available in the
program and on the latencies of the functional units in
the pipeline.
- To avoid a pipeline stall, a dependent instruction must
be separated from the source instruction by a distance
in clock cycles equal to the pipeline latency of that
source instruction..
Loop Unrolling by Compilers
for (j=1, j<= 1000, j++)
x[j]=x[j]+s;
- Assume R1 initially holds the highest address of the first
element and 8(R2) holds the last element.
Loop: L.D F0, 0(R1)
ADD.D F4, F0, F
S.D F4, 0(R1)
DADDUI R1, R1, #-
BNE R1, R2,Loop
- Performance of scheduled code with loop unrolling.
Performance of Unscheduled Code
without Loop Unrolling
Clock cycle issued
Loop: L.D F0, 0(R1) 1
stall 2
ADD.D F4, F0, F2 3 stall 4
stall 5
S.D F4, 0(R1) 6
DADDUI R1, R1, #-8 7
stall 8 BNE R1, R2,Loop 9
stall 10
- Need 10 cycles per result
Performance of Unscheduled Code with
Loop Unrolling
- Unroll the loop 4 iterations
Loop: L.D F0, 0(R1) ADD.D F4, F0, F S.D F4, 0(R1) L.D F6, -8(R1) ADD.D F8, F6, F S.D F8, -8(R1) L.D F10, -16(R1) ADD.D F12, F10, F S.D F12, -16(R1) L.D F14, -24(R1) ADD.D F16, F14, F S.D F16, -24(R1) DADDUI R1, R1, #-- BNE R1, R1, Loop
- Needs 7 cycles per result
Performance of Scheduled Code with
Loop Unrolling
Loop: L.D F0, 0(R1) L.D F6, -8(R1) L.D F10, -16(R1) L.D F14, -24(R1) ADD.D F4, F0, F ADD.D F8, F6, F ADD.D F12, F10, F ADD.D F16, F14, F S.D F4, 0(R1) S.D F8, -8(R1) DADDUI R1, R1, #-- S.D F12, 16(R1) BNE R1, R1, Loop S.D F16, 8(R1)
- Need 3.5 cycles per result
Branch Behavior in Programs
- Program behavior
- Average frequency of taken branches : 67%
- 60% of the forward branches are taken.
- 85% of the backward branches are taken
- Methods for statically branch prediction
- By examination of the program behavior
- Predict-taken (mis-prediction rate: 9%~59%).
- Predict-forward-untaken and backward taken.
- The above two approaches combined mis-prediction rate is 30%~40%.
- By the use of profile information collected from earlier
runs of the program.
The Basic VLIW Approach
- VLIW uses multiple, independent functional units.
- Multiple, independent instructions are issued by
processing a large instruction package that consists of
multiple operations.
- A VLIW instruction might include one integer/branch
instruction, two memory references, and two floating-
point operations.
- If each operation requires a 16 to 24 bits field, the length of each VLIW instruction is of 112 to 168 bits.
- Performance of VLIW
Advanced Compiler Support for Exposing
and Exploiting ILP
- Exploiting Loop-Level Parallelism
- Converting the loop-level parallelism into ILP
- Software pipelining (Symbolic loop unrolling)
- Global code scheduling
Loop-Level Parallelism
- Concepts and techniques
- Loop-level parallelism is normally analyzed at the source level while most ILP analysis is done once the instructions have been generated by the compiler.
- The analysis of loop-level parallelism focuses on determining whether data accesses in later iterations are data dependent on data values produced in earlier iterations.
- Example: for (i=1; i<=1000; i++) x[i]=x[i]+s;
- Loop-carried data dependence : Dependence exists between different iterations of the loop.
- A loop is parallel unless there is a cycle in the dependences. Therefore, a non-cycled loop-carried data dependence can be eliminated by code transformation.
Loop-Carried Data Dependence (2)
for (I=1; I<=100; I=I+1){ A[I] = A[I]+B[I]; /* S1 / B[I+1] = C[I]+D[I]; / s2 */ }
- Code transformation A[1] = A[1] +B[1]; for (I=1; I<99; I=I+1){ B[I+1] = C[I]+D[I]; /* s2 / A[I+1] = A[I+1]+B[I+1]; / S1 */ }
- Convert loop-carried data dependence into data dependence.
S 1 (^) S 2
S 1 (^) S 2
Loop-Carried Data Dependence (3)
- True loop-carried data dependence are usually in
the form of a recurrence.
For (I=2; I<=100; I++){
Y[I] = Y[I-1] + Y[I];
}
- Even true loop-carried data dependence has
parallelism.
For (I=6; I<=100; I++){
Y[I] = Y[I-5] + Y[I];
}
- The first, second, …, five iterations are parallel.
Dependence Detection Problem
- NP complete.
- GCD test heuristic
- Suppose we have stored to an array element with index value aj+b and loaded from the same array with index value ck+d, where j and k are the for-loop index variable that runs from m to n. A dependence exists if two conditions hold: - There are tow iteration indices, j and k, both within the limits of the for loop. - The loop stores into an array element indexed by aj+b and later fetches from that same array element when it is indexed by ck+d. That is, aj+b=ck+d. » Note, a,b,c, and d are generally unknown at compile time, making it impossible to tell if a dependence exists.
- A simple and sufficient test for the absence of a dependence. If a loop- carried dependence exists, then GCD(c,a) must divide (d-b). That is if GCD(c,a) does not divide (d-b), no dependence is possible (Example on page 324).
Situations where Dependence Analysis
Fails
- When objects are referenced via pointers rather
than array indices;
- When array indexing is indirect through another
array.
- When a dependence may exist for some value of
the inputs, but does not exist in actuality.