Download Software Instruction-Level Parallelism: Loop Unrolling & Conditional Instructions - Prof. and more Study notes Computer Science in PDF only on Docsity!
Computer Systems Architecture
CMSC 411
Unit 4b – Software instruction-
level parallelism
Alan Sussman
December 7, 2004
CMSC 411 - Alan Sussman 2
Administrivia
- Project questions?
- Quiz 3 today
- Practice final posted soon
- Homework 6 posted, due Thursday
- Read Chapter 4, except 4.8 and global code scheduling in 4.
- Online course evaluation available at https://www.courses.umd.edu/online_evaluation
CMSC 411 - Alan Sussman 3
Last time
- RAID
- RAID 3 – bit-interleaved parity
- RAID 4 – block-interleaved parity – like RAID 3, but faster reads and writes
- RAID 5 – RAID 4, but stripe parity blocks across disks
- RAID 6 – use another disk to allow recovery from 2 failures
- I/O performance measures and system design
- bandwidth vs. latency
- performance analysis shows that CPU is usually not the bottleneck (most of the time is spent accessing the disk)
- Stale data
- attach I/O bus to cache vs. to memory
- DMA
- virtual vs. physical addresses CMSC 411 - Alan Sussman 4
Loop unrolling
- To improve performance of pipelines and
simple multiple issue processors
- for static issue, and for dynamic issue with static scheduling
- To fill pipeline stalls
- can be done dynamically in hardware, or statically in compiler
- Look again at an example we used before
CMSC 411 - Alan Sussman 5
Example - loop unrolling
original loop: for i=1000, 999, ..., 1 x[i] = x[i] + s;
unrolled to a depth of 4: for i=1000, 996, 992,..., 4 x[i] = x[i] + s; x[i-1] = x[i-1] + s; x[i-2] = x[i-2] + s; x[i-3] = x[i-3] + s; end for
Loop: L.D F0,0(R1) x[i] = x[i] + s ADD.D F4,F0,F2 uses F0 and F S.D F4,0(R1) L.D F6,-8(R1) x[i-1] = x[i-1] + s ADD.D F8,F6,F2 uses F6 and F S.D F8,-8(R1) L.D F10,-16(R1) x[i-2] = x[i-2] + s ADD.D F12,F10,F2 uses F10 and F S.D F12,-16(R1) L.D F14,-24(R1) x[i-3] = x[i-3] + s ADD.D F16,F14,F2 uses F10 and F S.D F16,-24(R1) DSUBI R1,R1,#32 point to next element BNE R1,R2,Loop CMSC 411 - Alan Sussman 6
And reschedule the loop
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) DSUBI R1,R1,# S.D F12,16(R1) BNE R1,R2,Loop S.D F16, 8(R1)
CMSC 411 - Alan Sussman 7
Example (cont.)
- Note: if 1000 were not divisible by 4, we
would have a loop like this plus added code
to take care of the last few elements
- How well does the unrolled code pipeline?
- uses (14 cycles)/(4 elements), instead of original code that used 6 cycles per element - assuming issue 1 instruction per cycle, and standard MIPS pipeline organization (load delays, functional unit latencies)
CMSC 411 - Alan Sussman 8
Loop unrolling (cont.)
- Limited only by:
- number of available registers
- size of instruction cache - want the unrolled loop to fit
- What is gained
- fewer pipeline stalls/bubbles
- less loop overhead - fewer DSUBI s and BNE s
- What is lost
- longer code
- many possibilities to introduce errors
- slower compilation
- more work for either the programmer or for the compiler writer
CMSC 411 - Alan Sussman 9
What did the compiler have to do?
- Determine that it was legal to move S.D after DSUBI and BNE, and adjust S.D offsets
- Determine that loop unrolling would be useful – improve performance
- Use different registers to avoid name dependences
- Eliminate extra test and branch instructions, and adjust loop termination and counter code
- Determine that loads and stores could be interchanged – the ones from different iterations are independent – requires memory address analysis
- Schedule the code, preserving true dependences
Computer Systems Architecture
CMSC 411
Unit 4b – Software instruction-
level parallelism
Alan Sussman
December 9, 2004
CMSC 411 - Alan Sussman 11
Administrivia
- Practice final posted, answers soon
- Homework 6 due today, answers posted today
- Read Chapter 4, except 4.8 and global code scheduling in 4. - practice HW questions w/answers posted today
- Extra office hours today & tomorrow, 4-5PM
- Online course evaluation available at https://www.courses.umd.edu/online_evaluation
CMSC 411 - Alan Sussman 12
Last time
- Loop unrolling
- to fill pipeline stalls, for static issue and dynamic issue with static scheduling
- limited by registers, instruction cache
- removes stalls, and loop overhead (counters, branches)
- increases code size, so makes more work for compiler and/or programmer
- compiler has to
- check legality of reordering instructions from multiple loop iterations
- rename registers
- eliminate extra branches, counter increments
- adjust loop bounds and step size
- memory address analysis – to reorder loads and stores
- schedule the code, and preserve flow dependences (RAW)
CMSC 411 - Alan Sussman 19
Fourth example
- Is there any dependence in this loop? - Does it ever store into a location and then fetch the same value later?
- Yes! Produce an example
- i=5, store 15
- i=6, fetch 15
- How to detect this in general?
for i=1,2,..., x(3i) = x(2i+3) * 5.0;
CMSC 411 - Alan Sussman 20
Mathematics to the rescue
- If store to location a*i+b and fetch from location c*i+d , then they can be equal if there are values i and I so that ai + b = cI + d
- or, equivalently, if ai - cI = d – b
- Suppose q is a divisor of a and c. Then q would also be a divisor of d-b.
- Therefore, if no divisor of a and c is also a divisor of d-b , then there can be no dependence.
- And only need to check the greatest common divisor ( gcd ) of a and c.
CMSC 411 - Alan Sussman 21
Back to the example
- a=3 , b=0 , c=2 , and d=. The greatest common divisor of a=3 and c=2 is 1, and 1 divides d-b=3 , so it is possible that loop dependences occur.
for i=1,2,..., x(3i) = x(2i+3) * 5.0; end for
CMSC 411 - Alan Sussman 22
Fifth example
- There are 5 dependences:
- S3 depends on the result of S
- S4 depends on the result of S
- There is an antidependence between S1 and S
- There is an antidependence between S3 and S
- There is an output dependence between S1 and S
for i=1,2,..., y(i) = x(i) / c; S x(i) = x(i) + c; S z(i) = y(i) + c; S y(i) = c - y(i); S end for
CMSC 411 - Alan Sussman 23
Things that may fool a compiler
regarding dependences
- Fortran equivalence statements - pointer references - for C/C++ and Java - what does *p point at? - array indexing through another array - Example: x[index[i]] - dependence exists only for some input values, but inputs may never take on those values
Real A(20,20) Real B(400) Equivalence (A,B)
- Then B(21) has the same address as A(1,2)
CMSC 411 - Alan Sussman 24
Hardware support for the compiler
- Conditional/predicated instructions
- Methods to help compiler move code past
branches
- mainly to deal with exceptions properly
- Checks for address conflicts
- to help with reordering loads and stores
CMSC 411 - Alan Sussman 25
Conditional instructions
- Condition is evaluated as part of the instruction execution - if condition true, normal execution - if condition false, instruction turned into a no-op
- Example: conditional move
- move a value from one register to another if condition is true
- can eliminate a branch in simple code sequences
CMSC 411 - Alan Sussman 26
Example: conditional move
- For code: if (A==0) { S=T; }
- Assume R1, R2, R3 hold values of A, S, T With branch: BNEZ R1, L ADDU R2, R3, R L:
With conditional move (if 3rd operand equals zero): CMOVZ R2, R3, R
- Converts the control dependence into a data dependence - for a pipeline, moves the dependence from near beginning of pipeline (branch resolution) to end (register write)
CMSC 411 - Alan Sussman 27
Superscalar execution
- Predication helps with scheduling
- Example: superscalar that can issue 1 memory reference and 1 ALU op per cycle, or just 1 branch
LW R9,0( R8 )
LW R8 ,0(R10)
BEQZ R10,L
ADD R6,R3,R
LW R1,40(R2) ADD R3,R4,R
1 st^ instruction 2 nd^ instruction 1 st^ instruction 2 nd^ instruction
LW R9,0( R8 )
BEQZ R10,L
LWC ADD R6,R3,R R8 ,0(R10),R
LW R1,40(R2) ADD R3,R4,R
LWC loads if 3 rd^ operand not 0
CMSC 411 - Alan Sussman 28
Limitations of cond. instructions
- Predicated instructions that are squashed still use processor resources - doesn’t matter if resources would have been idle anyway
- Most useful when predicate can be evaluated early
- want to avoid data hazards replacing control hazards
- Hard to do for complex control flow
- for example, moving across multiple branches
- Conditional instructions may have higher cycle count or slower clock rate than unconditional ones
CMSC 411 - Alan Sussman 29
Compiler speculation with
hardware support
- To move speculated instructions not just before branch, but before condition evaluation
- Compiler can help find instructions that can be speculatively moved and not affect program data flow
- Hard part is preserving exception behavior
- a speculated instruction that is mispredicted should not cause an exception
- 4 methods described in Section 4.5, so it can be done CMSC 411 - Alan Sussman 30
Memory reference speculation
with hardware support
- To move loads across stores, when compiler can’t be sure it is legal
- Use a speculative load instruction
- hardware saves address of memory location
- if a subsequent store changes that location before the check (to end the speculation), then the speculation failed, otherwise it succeeded
- on failure, need to redo load and re-execute all speculated instructions after the speculative load