Professional Documents
Culture Documents
7 Hazard
7 Hazard
(computer Archiceture)
Hazard in computer Archiceture
8
Inter-Instruction Dependences
Data dependence True
r3 r1 op r2 Read-after-Write Dependency
r5 r3 op r4 (RAW)
Anti-dependence
r3 r1 op r2 Write-after-Read Name
r1 r4 op r5 (WAR) dependences
or
Output dependence False
Dependency
r3 r1 op r2 Write-after-Write
r5 r3 op r4 (WAW)
r3 r6 op r7
Control dependence
9
Data Dependency
• Data dependency is a situation in which a program
statement (Instruction) refers to the data of a preceeding
statement.
• An instruction j is a data dependent on instruction i if either
of the following holds:
• Instruction i produces a result that may be used by
instruction j .
• Instruction j is data dependent on instruction k, and
instruction k is data dependent on instruction i.
• True dependency arises when one instruction use the value
produced by an earlier instruction
1.A=3
2.B=A
3. C=B
1.B=3
N.B2=B
2.A=B2+1
3. B=7
Output Dependency
An output Dependency also known as write after write(WAW) occurs
when the ordering of instructions will affect the final output value of a
variable.
1.B=3
2.A=B+1
3. B=7
Example:
A: bre $s1.$s2,Label
B: add $s3,$s4,$s5
C: Label sub $t0,$t1,$t2
Q. Find out all types dependencies for the following
MIPS instructions
1.DADD R1,R2,R3
2.LD R4,0(R1) : R4<-M[R1+0]
3.DADDUI R5,R1,#10
4.SD R4,0(R5) : M[R5]<-R4
5.OR R4,R3,R5
6.AND R1,R6,R7
Solution
given.
1.DADD R1,R2,R3
2.LD R4,0(R1)
3.DAADUI R5,R1,#10
4.SD R4,0(R5)
5.OR R4,R3,R5
6.AND R1,R6,R7
The use of the result of the SUB instruction in the next three instructions causes a
data hazard, since the register $2 is not written until after those instructions read it.
Pipeline Hazards
Execution Order is: Read After Write (RAW)
InstrI
InstrJ InstrJ tries to read operand before InstrI writes it
I: add r1,r2,r3
J: sub r4,r1,r3
Pipeline Hazards
Execution Order is: Write After Read (WAR)
InstrI
InstrJ InstrJ tries to write operand before InstrI reads i
– Gets wrong operand
I: sub r4,r1,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Pipeline Hazards
Execution Order is: Write After Write (WAW)
InstrI
InstrJ InstrJ tries to write operand before InstrI writes it
– Leaves wrong result ( InstrI not InstrJ )
I: sub r1,r4,r3
J: add r1,r2,r3
K: mul r6,r1,r7
Pipeline Hazards
Data Hazard Detection in MIPS
Read after Write
Time (in clock cycles)
Value of CC 1 CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9
register $2: 10 10 10 10 10/– 20 – 20 – 20 – 20 – 20
Program
execution
IF/ID ID/EX EX/MEM MEM/WB
order
(in
instruction Reg
sub $2, $1, $3 IM Reg DM
s)
Pipeline Hazards
Data Hazards
Pipeline Hazards
Handling data hazard by S/W(stalling)
1 2 3 4 5 6 7 8 9
W
add$s0,$t0,$t1 IF ID EX MEM s0 $ s0
writte n
here
S T ALL
BUBBLEBUBBLEBUBBLEBUBBLEBUBBLE
S T ALL
BUBBLEBUBBLEBUBBLEBUBBLEBUBBLE
sub$t2$,s0,$t3 R
IF s0 EX MEM WB
$ s0 re a d
here
Pipeline Hazards
Forwarding
• Generally speaking:
– Forwarding occurs when a result is passed
directly to the functional unit that requires
it.
– Result goes from output of one pipeline
stage to input of another.
Forwarding
1 2 3 4 5 6 7 8 9
ID W
add $ s0,$ t0,$ IF ID EX MEM s0
t1
new value
• of s0
R
sub $ t2,$ s0,$ IF s0 EX MEM WB
t3
1 2 3 4 5 6 7 8 9
0
ID W
lw$s0,20($t1) IF ID EX MEM s0
ne w va lue
of s 0
ST ALL
BUBBLEBUBBLEBUBBLEBUBBLEBUBBLE
R
s u b $ t$2 s, 0, $ t 3 IF s0 EX MEM WB
Pipeline Hazards
Instruction Reordering
• ADD R1 , R2 , R3
• SUB R4 , R1 , R5 Before
• XOR R8 , R6 , R7
• AND R9 , R10 , R11
• ADD R1 , R2 , R3
• XOR R8 , R6 , R7 After
• AND R9 , R10 , R11
• SUB R4 , R1 , R5
Structural Hazards
• Attempt to use the same resource by two or more
instructions at the same time
• Example: Single Memory for instructions and data
– Accessed by IF stage
– Accessed at same time by MEM stage
• Solutions
– Delay the second access by one clock cycle, OR
– Provide separate memories for instructions & data
» This is called a “Harvard Architecture”
Pipeline Hazards
Pipelined Example - Executing Multiple
Instructions
Pipeline Hazards
Alternative View - Multicycle Diagram
lw $ r0 , 10 ($ r1 IM REG ALU DM RE G
)
Pipeline Hazards
Alternative View - Multicycle
Diagram
CC1 CC2 CC3 CC4 CC5 CC6 CC7 CC8
lw $ r0 , 10 ($ r1 IM REG ALU DM RE G
) Memory Conflict
Pipeline Hazards
One Memory Port Structural
Hazards
Time (clock cycles)
Cycle 1 Cycle 2Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
AL
U
n
s
t Instr 1 Ifetch Reg DMem Reg
AL
U
r.
Instr 2 Ifetch Reg DMem Reg
AL
U
O
r
Stall Bubble Bubble Bubble Bubble Bubble
d
Instr 3 Ifetch Reg DMem Reg
AL
U
e
Pipeline Hazards
r
solution to Structural Hazards
Dealing with Structural Hazards
Stall
• low cost, simple
• Increases CPI
• use for rare case
since stalling
Pipeline hardwarehas resource
• performance effect
useful for multi-cycle resources
• good performance
•
Replicate resource
• good performance
• increases cost (+ maybe interconnect delay)
• useful for cheap or divisible resources
Pipeline Hazards
NUMERICALS
1. Calculate the no. of stall without operand forwarding and with
forwarding
DADD R1, R2, R3
LD R4, 0(R1)
SD R4, 12(R1)
2. Calculate the no. of stall without operand forwarding and with
forwarding
DADD R1, R2, R3
DSUB R4, R1, R5
AND R6, R1, R7
OR R8, R1, R9
XOR R10, R1, R11
3. Calculate the no. of stall without operand
forwarding and with forwarding
LD R1, 0(R2)
DSUB R4, R1, R5
AND R6, R1, R7
OR R8, R1, R9
4.
Control Hazards
A control hazard is when we need to find the
destination of a branch, and can’t fetch any new
instructions until we know that destination.
A branch is either
– Taken: PC <= PC + 4 + Immediate
– Not Taken: PC <= PC + 4
Pipeline Hazards
How branches impact pipelined instructions
Pipeline Hazards
control Hazard Solutions
• Stall - stop fetching instr. until result is
available
– Significant performance penalty
– Hardware required to stall
• Predict - assume an outcome and continue
fetching (undo if prediction is wrong)
– Performance penalty only when guess wrong
– lose cycle only when misprediction
• Delayed branch - specify in architecture that
following instruction is always executed
– Compiler re-orders instructions into delay slot
– Insert "NOP" (no-op) operations when can't use (~50%)
Pipeline Hazards
Stall pipeline
• Control hazards can cause a greater performance loss for pipeline than data
hazards. When a branch is executed, it may or may not change the PC
(program counter) to something other than its current value plus 4.
• If a branch changes the PC to its target address, it is a taken branch;
• if it falls through, it is not taken.
• If instruction i is a taken branch, then the PC is normally not changed until
the end of MEM stage, after the completion of the address calculation and
comparison
• The simplest method of dealing with branches is to stall the pipeline as soon
as the branch is detected until we reach the MEM stage, which determines
the new PC. T
• Stall pipeline
• The simplest scheme to handle branches is to freeze or flush the pipeline,
holding or deleting any instructions after the branch until the branch
destination is known.
• Advantage: simple both to software and hardware
• The pipeline behavior looks like :
Branch IF ID EX MEM WB
Branch successor IF(stall) stall stall IF ID EX MEM WB
Branch successor+1 IF ID EX MEM WB
Branch Prediction Schemes
• There are many methods to deal with the pipeline stalls caused
by branch delay.
• compile-time schemes in which predictions are static - they are
fixed for each branch during the entire execution, and the
predictions are compile-time guesses.
• Predict taken
• Predict not taken
• Delayed branch
Predict Not Taken
• A higher performance, and only slightly
more complex, scheme is to predict the
branch as not taken, simply allowing the
hardware to continue as if the branch were
not executed. Care must be taken not to
change the machine state until the branch
outcome is definitely known.
The pipeline with this scheme implemented behaves as shown
below:
Branch instr
sequential successor 1
sequential successor 2
.....
sequential successor n
Branch target if taken
Pipeline Hazards
• Three branch-scheduling schemes in branch delay slot