Docsity

Software Instruction-Level Parallelism: Loop Unrolling & Code Scheduling in CMSC 411 - Pro, Study notes of Computer Science

Toggle sidebar
bg1
CMSC 411 - A. Sussman (from D. O'Leary) 1
Computer Systems Architecture
CMSC 411
Unit 4b – Software instruction-
level parallelism
Alan Sussman
April 8, 2003
CMSC 411 - Alan Sussman 2
Administrivia
• Midterms returned Thursday
• Project coming soon
– due end of last full week of semester
• Quiz 3 pushed back, probably 1 week
• Homework #4b (Chapter 4) posted by
tomorrow – due next Thursday
CMSC 411 - Alan Sussman 3
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 4
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 F4
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 F8
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 F12
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 F12
S.D F16,-24(R1)
DSUBI R1,R1,#32 point to next element
BNE R1,R2,Loop
CMSC 411 - Alan Sussman 5
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,F2
ADD.D F8,F6,F2
ADD.D F12,F10,F2
ADD.D F16,F14,F2
S.D F4,0(R1)
S.D F8,-8(R1)
DSUBI R1,R1,#32
S.D F12,16(R1)
BNE R1,R2,Loop
S.D F16, 8(R1)
CMSC 411 - Alan Sussman 6
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
pf3
pf4
pf5
pf8

Partial preview of the text

Download Software Instruction-Level Parallelism: Loop Unrolling & Code Scheduling in CMSC 411 - Pro and more Study notes Computer Science in PDF only on Docsity!

Computer Systems Architecture

CMSC 411

Unit 4b – Software instruction-

level parallelism

Alan Sussman

April 8, 2003

CMSC 411 - Alan Sussman 2

Administrivia

  • Midterms returned Thursday
  • Project coming soon
    • due end of last full week of semester
  • Quiz 3 pushed back, probably 1 week
  • Homework #4b (Chapter 4) posted by

tomorrow – due next Thursday

CMSC 411 - Alan Sussman 3

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 4

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 5

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 6

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

CMSC 411 - Alan Sussman 7

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 8

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

CMSC 411 - Alan Sussman 9

Dependences limit loop unrolling

  • In unrolling, removed intermediate DSUBI instructions to reduce the data dependence for the L.D and the control dependence for the BNEZ
  • There are also antidependences, so also made sure that later copies of the unrolled code used registers other than F0 & F4 - eliminated name dependences

Loop: L.D F0,0(R1) ADD.D F4,F0,F S.D F4,0(R1) DSUBI R1,R1,# BNE R1,R2,Loop

L.D and BNEZ depend on result of DSUBI

CMSC 411 - Alan Sussman 10

True data dependences also limit

unrolling

  • First assignment statement is an example of

a loop-carried dependence

  • Second assignment statement doesn't limit

unrolling, but makes scheduling trickier

for i=1,..., x[i+1] = x[i] + c[i] ;Uses value from previous iteration b[i+1] = d[i] + x[i+1] ;Uses the value just computed end for

CMSC 411 - Alan Sussman 11

One problem for the programmer

  • Precise exception handling becomes impossible if the compiler unrolls loops
  • The order of operations that the user assumes is completely violated, so exceptions occur at unrecognizable locations
  • Rather than precise exception handling, settle for the property that the unrolled code produces no new exceptions over the original code
  • Also possible to provide software to simulate the code's behavior around the exception to help user diagnose the problem CMSC 411 - Alan Sussman 12

Compilers need to detect data

dependences

  • Can unroll this loop, but

must be careful not to

interchange the order of the

two instructions

  • Note that a(i) never needs

to be loaded

for i=1,2,..., a(i) = b(i) + c(i); d(i) = a(i) * e(i); end for

CMSC 411 - Alan Sussman 19

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 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 20

Software pipelining

  • Another compiler technique for parallelism
  • The compiler symbolically unrolls the loop

to create one copy that interleaves

instructions from different iterations

Fig. 4.

CMSC 411 - Alan Sussman 21

Example - again

Loop: L.D F0,0(R1) ; get next element of x ADD.D F4,F0,F2 ; add s to x-element S.D F4,0(R1) ; store result DSUBI R1,R1,#8 ; point to next x-element BNE R1,R2,Loop ; test done

CMSC 411 - Alan Sussman 22

Example (cont.)

  • Three copies of the loop body, unrolled: - Software pipelined loop takes the statements in Loop: bold and interleaves them: L.D F0,0(R1) ; x[i]=x[i]+s ADD.D F4,F0,F S.D F4,0(R1)

L.D F0,0(R1);x[i-1]=x[i-1]+s ADD.D F4,F0,F S.D F4,0(R1)

L.D F0,0(R1) ;x[i-2]=x[i-2]+s ADD.D F4,F0,F S.D F4,0(R1)

Loop: S.D F4, 0(R1) ;store element i ADD.D F4,F0,F2 ;add for i- L.D F0,0(R1) ;get element i- DSUBI R1,R1,#8 ;next x BNEZ R1,Loop ;test done

  • with initialization before, and clean-up after, and 2 fewer iterations

CMSC 411 - Alan Sussman 23

Software pipelining (cont.)

  • Doesn't reduce loop overhead (like loop

unrolling does)

  • But reduces data hazards
    • similar to what hardware dynamic scheduling does, but in software (so works for VLIW, static scheduling)

Computer Systems Architecture

CMSC 411

Unit 4b – Software instruction-

level parallelism

Alan Sussman

April 10, 2003

CMSC 411 - Alan Sussman 25

Administrivia

  • Midterms returned today
    • average:56 median:56 25%:46 75%:
  • Project coming soon
    • due end of last full week of semester
  • Quiz 3 pushed back 1 week to April 22
  • Homework #4b (Chapter 4) posted
    • due next Thursday

CMSC 411 - Alan Sussman 26

Last time

  • Loop unrolling
    • to fill pipeline stalls
    • dynamically in hardware, or statically by compiler
    • allows much greater opportunities for rescheduling, and lowers loop overheads
    • limited by register availability, instruction cache effects
    • compiler has to be pretty sophisticated to only preserve true data dependences (memory address analysis, and general bookkeeping issues)
    • examples showed how hard detecting data dependences can be – loop-carried, using GCD test
    • real problems with Fortran equivalence statements, C/C++ pointers, indirect array accesses

CMSC 411 - Alan Sussman 27

Last time

  • Software pipelining
    • symbolic loop unrolling, interleaving instructions from multiple loop iterations
    • need initialization and cleanup code to get into and out of the software pipelined loop correctly
    • reduces data hazards, but not loop control overhead - by allowing data dependent instructions to execute farther apart in time

CMSC 411 - Alan Sussman 28

Global code scheduling

  • Requires moving instructions across branches
    • e.g., for effective scheduling of a loop body
  • Want to compact a code fragment with branches (control statements) into the shortest possible sequence and preserve data and control dependences - means finding shortest sequence for critical path – longest sequence of data dependent instructions

CMSC 411 - Alan Sussman 29

Global code motion

  • Need estimates of relative frequency of

different paths through the code

  • since moving code across branches will often affect its frequency of execution
  • No guarantees that code will be faster, but if

frequency info is accurate, compiler can

decide if code is likely to be faster

CMSC 411 - Alan Sussman 30

Example – inner loop body

A[i]=A[i]+B[i]

A[i]=0?

B[i]=

C[i]=

Common path

Moving B[i]= or C[i]= requires complex analysis

CMSC 411 - Alan Sussman 37

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 38

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 39

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 40

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 41

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

Intel IA-64 Architecture and an

Implementation

CMSC 411 - Alan Sussman 43

Instruction set architecture

  • RISC-style, register-register, plus features to support compiler-based ILP - most instructions predicated using predicate registers - predicate registers set using compare or test instructions - integer registers use a register stack (like SPARC) - speculation for both control (deferring exceptions) and for memory references (load speculation) - overall, essentially a more flexible VLIW - compiler detects ILP and schedules operations, but not a single fixed format for VLIW instructions - it’s a mess, and violates several of the guidelines we’ve talked about - especially in making tradeoffs obvious, and regularity (look at the limitations on instruction issue in H&P) CMSC 411 - Alan Sussman 44

Itanium IA-64 implementation

  • Up to 6 issues per clock, including 3 branches and 2 memory references
  • 3 level cache – 2 on chip, one off
  • 9 functional units, all pipelined
  • 10 stage pipeline, in 4 major parts
    • front-end (3) – IF, branch prediction
    • instruction delivery (2) – send instructions to FUs, rename registers
    • operand delivery (2) – read registers, check predicates
    • execution (3) – also retires instructions, does writeback

CMSC 411 - Alan Sussman 45

Itanium instruction latencies

FP arithmetic 4

Integer ALU ops 0

Mispredicted branch 9

Correctly predicted 0- taken branch

Floating-point load^29

Integer load^11

Instruction Latency Figure 4.

  1. Primary cache hit
  2. Secondary cache hit

CMSC 411 - Alan Sussman 46

Fallacies

  • A simple approach for multiple issue can be found that gets both high performance and doesn’t use too many transistors or have high design complexity - no silver bullets - for simple approaches, as issue rate increases the gap between peak and sustained performance grows quickly - so more sophisticated techniques really are necessary – e.g., dynamic scheduling, hardware/software speculation, branch prediction, … - compilers getting very complex too – need sophisticated transformations, and lots of tuning