You are on page 1of 31

The Processor

Lecture # 12
Course Instructor: Dr. Afshan Jamil
Outline
• Revisiting hazards
• Data Hazard
• Forwarding paths
• Forwarding hardware
• Double data hazard
• Datapath with forwarding
• Load use data hazard
• Datapath with hazard detection unit
• Branch hazard
• Data hazard for branches
• Dynamic branch prediction
Revisiting Hazards

• So far, our datapath and control have


ignored hazards.
• We shall revisit data hazards and control
hazards and enhance our datapath and
control to handle them in hardware…
Data Hazards and Forwarding
• Problem with starting an instruction before previous
are finished:
– data dependencies that go backward in time –
called data hazards
Hardware Solution: Forwarding

• Idea: use intermediate data, do not wait


for result to be finally written to the
destination register. Two steps:
1. Detect data hazard
2. Forward intermediate data to resolve
hazard
Data Hazards in R-Type Instructions
• Consider this sequence:
• sub x2, x1, x3
• and x12, x2, x5
• or x13, x6, x2
• add x14, x2, x2
• sd x15, 100(x2)
• We can resolve hazards with forwarding
– How do we detect when to forward?
Detecting the Need to Forward
• Pass register numbers along pipeline
– e.g., ID/EX.RegisterRs1 = register number for
Rs1 sitting in ID/EX pipeline register
• ALU operand register numbers in EX stage are
given by
– ID/EX.RegisterRs1, ID/EX.RegisterRs2
Detecting the Need to Forward
• Data hazards when
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs1
1b. EX/MEM.RegisterRd = ID/EX.RegisterRs2
2a. MEM/WB.RegisterRd = ID/EX.RegisterRs1
2b. MEM/WB.RegisterRd = ID/EX.RegisterRs2

Fwd from Fwd from


EX/MEM MEM/WB
pipeline reg pipeline reg
Detecting the Need to Forward
• But only if forwarding instruction will write to
a register!
– EX/MEM.RegWrite, MEM/WB.RegWrite
• And only if Rd for that instruction is not x0
– EX/MEM.RegisterRd ≠ 0,
MEM/WB.RegisterRd ≠ 0
Forwarding Conditions
• EX hazard
– if ((EX/MEM.RegWrite) and
(EX/MEM.RegisterRd ≠ 0) and
(EX/MEM.RegisterRd = ID/EX.RegisterRs1))
ForwardA = 10
– if ((EX/MEM.RegWrite) and
(EX/MEM.RegisterRd ≠ 0) and
(EX/MEM.RegisterRd = ID/EX.RegisterRs2))
ForwardB = 10
Forwarding Conditions
• MEM hazard
– if ((MEM/WB.RegWrite) and
(MEM/WB.RegisterRd ≠ 0) and
(MEM/WB.RegisterRd = ID/EX.RegisterRs1))
ForwardA = 01
– if ((MEM/WB.RegWrite) and
(MEM/WB.RegisterRd ≠ 0) and
(MEM/WB.RegisterRd = ID/EX.RegisterRs2))
ForwardB = 01
No Forwarding
Forwarding paths
Forwarding Hardware: Multiplexor
Control
Double Data Hazard

• Consider the sequence:


• add x1, x1, x2
• add x1, x1, x3
• add x1, x1, x4
• Both hazards occur
– Want to use the most recent
• Revise MEM hazard condition
– Only fwd if EX hazard condition isn’t true
Revised Forwarding Condition
• MEM hazard
– if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRs1))
and (MEM/WB.RegisterRd = ID/EX.RegisterRs1))
ForwardA = 01
– if (MEM/WB.RegWrite and (MEM/WB.RegisterRd ≠ 0)
and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd ≠ 0)
and (EX/MEM.RegisterRd = ID/EX.RegisterRs2))
and (MEM/WB.RegisterRd = ID/EX.RegisterRs2))
ForwardB = 01
Datapath with forwarding
Data Hazards and stalls
• In addition to a forwarding unit, we need a hazard
detection unit.
• It operates during the ID stage so that it can insert
the stall between the load and the instruction
dependent on it.
• Control for the hazard detection unit is this single
condition:
CONTD…

• if (ID/EX.MemRead and((ID/EX.RegisterRd =
IF/ID.RegisterRs1) or (ID/EX.RegisterRd =
IF/ID.RegisterRs2)))
stall the pipeline
How to Stall the Pipeline
• Force control values in ID/EX register to 0
– EX, MEM and WB do nop (no-operation)
• Prevent update of PC and IF/ID register
– Using instruction is decoded again
– Following instruction is fetched again
– 1-cycle stall allows MEM to read data for ld
• Can subsequently forward to EX stage
Stall in the pipeline
Datapath with Hazard detection
Control Hazard: Reducing Branch Delay
• If we move the conditional branch execution
earlier in the pipeline, then fewer instructions
need be flushed.
• Moving the branch decision up requires two
actions to occur earlier:
– computing the branch target address and
– evaluating the branch decision.
• Move hardware to determine outcome to ID stage
– Target address adder
– Register comparator
Reducing Branch Delay
• Example: branch taken
• 36 sub x10, x4, x8
• 40 beq x1, x3, 16
• 44 and x12, x2, x5
• 48 or x13, x2, x6
• 52 add x14, x4, x2
• 56 sub x15, x6, x7
• . . .
• 72 ld x4, 50(x7)
Example: Branch Taken
Example: Branch Taken
Data Hazards for Branches
• If a comparison register is a destination of 2nd
or 3rd preceding ALU instruction
IF ID EX MEM WB
add x1, x2, x3
IF ID EX MEM WB
add x4, x5, x6
IF ID EX MEM WB

IF ID EX MEM WB
beq x1, x4, target

◼ Can resolve using forwarding


Data Hazards for Branches
• If a comparison register is a destination of preceding
ALU instruction or 2nd preceding load instruction
– Need 1 stall cycle

ld x1, addr IF ID EX MEM WB

add x4, $5, $6 IF ID EX MEM WB

beq stalled IF ID

beq x1, x4, target ID EX MEM WB


Data Hazards for Branches

• If a comparison register is a destination of immediately


preceding load instruction
– Need 2 stall cycles

ld x1, addr IF ID EX MEM WB

beq stalled IF ID

beq stalled ID

beq x1, x0, target ID EX MEM WB


Dynamic Branch Prediction
• In deeper and superscalar pipelines, branch penalty is
more significant
• Use dynamic prediction
– Branch prediction buffer (branch history table)
– Indexed by recent branch instruction addresses
– Stores outcome (taken/not taken)
– To execute a branch
• Check table, expect the same outcome
• Start fetching from fall-through or target
• If wrong, flush pipeline and flip prediction

You might also like