Lecture-6 (Pipeline Hazards)CS422-Spring 2018
Biswa@CSE-IITK
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 2
Hazards
• Limits to pipelining: Hazards prevent next instruction from executing during its designated clock cycle
–Structural hazards: HW cannot support this combination of instructions (single person to fold and put clothes away)
–Data hazards: Instruction depends on result of prior instruction still in the pipeline (missing sock)
–Control hazards: Caused by delay between the fetching of instructions and decisions about changes in control flow (branches and jumps).
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 3
One Memory Port/Structural Hazards
Instr.
Order
Time (clock cycles)
Load
Instr 1
Instr 2
Instr 3
Instr 4
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 6 Cycle 7Cycle 5
Reg
ALU
DMemIfetch Reg
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 4
Why Separate Data and Instruction Caches?
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 5
Bubble
Instr.
Order
Time (clock cycles)
Load
Instr 1
Instr 2
Stall
Instr 3
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 6 Cycle 7Cycle 5
Reg
ALU
DMemIfetch Reg
Bubble Bubble Bubble BubbleBubble
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 6
Data Hazards
Instr.
Order
add r1,r2,r3
sub r4,r1,r3
and r6,r1,r7
or r8,r1,r9
xor r10,r1,r11
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Time (clock cycles)
IF ID/RF EX MEM WB
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 7
RAW
• Read After Write (RAW)InstrJ tries to read operand before InstrI writes it
• Caused by a “Dependence” (in compiler nomenclature). This hazard results from an actual need for communication.
I: add r1,r2,r3
J: sub r4,r1,r3
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 8
WAR
• Write After Read (WAR)InstrJ writes operand before InstrI reads it
• Called an “anti-dependence” by compiler writers.This results from reuse of the name “r1”.
• Can’t happen in MIPS 5 stage pipeline because:
– All instructions take 5 stages, and
– Reads are always in stage 2, and
– Writes are always in stage 5
I: sub r4,r1,r3 J: add r1,r2,r3K: mul r6,r1,r7
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 9
WAW
• Write After Write (WAW)InstrJ writes operand before InstrI writes it.
• Called an “output dependence” by compiler writersThis also results from the reuse of name “r1”.
• Can’t happen in MIPS 5 stage pipeline because:
– All instructions take 5 stages, and
– Writes are always in stage 5
• Will see WAR and WAW in more complicated pipes
I: sub r1,r4,r3 J: add r1,r2,r3K: mul r6,r1,r7
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 10
Data ForwardingTime (clock cycles)
Instr.
Order
add r1,r2,r3
sub r4,r1,r3
and r6,r1,r7
or r8,r1,r9
xor r10,r1,r11
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 11
Hardware Change
ME
M/W
R
ID/E
X
EX
/ME
M Data
Memory
ALU
mux
mux
Registe
rs
NextPC
Immediate
mux
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 12
Forwarding to Avoid LW-SW ? Time (clock cycles)
Instr.
Order
add r1,r2,r3
lw r4, 0(r1)
sw r4,12(r1)
or r8,r6,r9
xor r10,r9,r11
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 13
Even With Forwarding ?Time (clock cycles)
Instr.
Order
lw r1, 0(r2)
sub r4,r1,r6
and r6,r1,r7
or r8,r1,r9
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 14
Control Hazard on Branches with 3-stage Stall
10: beq r1,r3,36
14: and r2,r3,r5
18: or r6,r1,r7
22: add r8,r1,r9
36: xor r10,r1,r11
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
Reg
ALU
DMemIfetch Reg
What do you do with the 3 instructions in between?How do you do it?Where is the “commit”?
Conditional
• the target address is close to the current PC location
• branch distance from the incremented PC value fits into the immediate field
• for example: loops, if statements
Unconditional (jumps)
• transfers of control
• the target address is far away from the current PC location
• for example: subroutine calls
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 15
Branches
CS422: Spring 2018 Biswabandan Panda, CSE@IITK 16
Branches
Syntax: BEQ $1, $2, 12
Action: If ($1 != $2), PC = PC + 4
Zero-extend or sign-extend immediate field?
Action: If ($1 == $2), PC = PC + 4 + 48
Immediate field codes # words, not # bytes.Why is this encoding a good idea?
Why is this extension method a good idea?
Increases branch range to 128 KB.
Supports forward and backward branches.
Sign-extend.