Recap of Lecture 13 and Homework Check
- Five stages: IF ID EX MEM WB; latches between them.
- Cycles = k + n - 1; CPI → 1 if nothing stalls.
- The slowest stage + overhead sets the clock.
- Milk loop: 44 instructions → 48 cycles, ideal 3.67x.
Homework check
- 1. 6 independent instructions: cycles?
- 2. Stages 250, 150, 200, 300, 150 ps; register 25 ps: clocks and speedup?
- 3. MEM split into 2 x 150 ps: new clock, speedup?
Today's Plan (50 minutes)
0-5
Recap and homework check Lecture 13
5-8
Plan and learning outcomes
8-16
Module 1: Structural hazards one memory, register file, cost
16-27
Module 2: Data hazards RAW, WAR, WAW; stalls without forwarding
27-38
Module 3: Forwarding and load-use bill program, real clang loop, CPI
38-45
Class activity classify, count stalls, reorder
45-50
Mistakes, quiz and homework next: control hazards
Learning Outcomes (SLOs)
By the end of this lecture you will be able to:
SLO 14.1
Explain structural hazards and how the MIPS pipeline avoids them.
→ CLO3 · C2 Understand
SLO 14.2
Classify data dependences as RAW, WAR or WAW and find the ones that stall the 5-stage pipeline.
→ CLO3 · C3 Apply
SLO 14.3
Count stall cycles and CPI with and without forwarding, including load-use stalls.
→ CLO2 · C3 Apply
- CLO2: Measure and report the performance of computer systems.
- CLO3: Explain instruction set principles and advanced instruction-level and thread-level parallelism techniques.
Module 1: Structural Hazards
Two instructions, one piece of hardware
Hazards and Stalls
- pipeline CPI = 1 + stall cycles per instruction
- speedup = depth / (1 + stalls per instr.)
One machine
Washer and dryer in one box: load B must wait.
Stall = hold the instruction; a bubble (empty slot) moves down the pipe.
Structural Hazard: One Memory
lw $t0, 0($s0)
IF
ID
EX
MEM
WB
Fix 1
add $t1, $t2, $t3
IF
ID
EX
MEM
WB
Separate instruction and data memories (L1-I and L1-D caches).
sub $t4, $t5, $t6
IF
ID
EX
MEM
WB
or $t7, $t8, $t9
IF
ID
EX
MEM
WB
Cycle 4: data read (lw) and instruction fetch (or) both need memory.
Fix 2: register file
2 read + 1 write port; write in the first half of the cycle, read in the second half (WB and ID in the same cycle).
The Cost of a Structural Hazard
time per instruction (T = B's clock)
- A (hazard): (1 + 0.4 x 1) x T / 1.05 = 1.33 T
- B (none): 1 x T = 1.00 T
- B is 1.33 times faster
1.33x
When to accept one
faster without the hazard
When the hazard is rare and removing it is expensive (area, power).
even with a 5% slower clock
Module 2: Data Hazards
When the result is not ready yet
Three Kinds of Data Dependence
In-order 5-stage pipeline: reads in ID, writes in WB, in order → only RAW hazards.
WAR and WAW return with out-of-order execution (Lectures 17, 20-21).
RAW Hazards Without Forwarding
add $t1, $t2, $t3
IF
ID
EX
MEM
WB
sub $t4, $t1, $t5
IF
ID
stall
stall
EX
MEM
WB
and $t6, $t1, $t7
IF
stall
stall
ID
EX
MEM
WB
or $t8, $t1, $t9
IF
ID
EX
MEM
WB
xor $s0, $t1, $t2
IF
ID
EX
MEM
WB
11 vs 9
Write in the first half, read in the second half: ID may share WB's cycle.
cycles
2 stalls for one RAW
Dependences in a Real Program
No forwarding: 14 stalls - 28 cycles for 10 instructions.
Module 3: Forwarding and Load-Use
Use the result before it is written
Forwarding (Bypassing)
add $t1, $t2, $t3
IF
ID
EX
MEM
WB
sub $t4, $t1, $t5
IF
ID
EX
MEM
WB
and $t6, $t1, $t7
IF
ID
EX
MEM
WB
or $t8, $t1, $t9
IF
ID
EX
MEM
WB
xor $s0, $t1, $t2
IF
ID
EX
MEM
WB
Forwarding paths
Hand it over
- EX/MEM → ALU (distance 1)
- MEM/WB → ALU (distance 2)
- register file (distance 3: write-then-read)
Cook → waiter directly, not via the shelf.
The Load-Use Hazard
lw $t1, 4($s0)
IF
ID
EX
MEM
WB
sub $t2, $t0, $t1
IF
ID
stall
EX
MEM
WB
sll $t3, $t2, 3
IF
stall
ID
EX
MEM
WB
add $t4, $t3, $t2
IF
ID
EX
MEM
WB
1 stall
load-use
even with forwarding
Cannot forward back in time: the loaded value exists only at the end of MEM.
The Bill Program on Three Machines
L14_bill_sched.asm
- lw $t1, 4($s0)
- li $t5, 35 # moved up
- sub $t2, $t0, $t1
Forwarding removes most stalls; scheduling removes the load-use stall.
Real Compiler Code: the clang Loop
clang -O1 order (L14_clang.asm)
- loop:
- lw $1, 0($4)
- addu $2, $1, $2 # stall
- addiu $5, $5, -1
- addiu $4, $4, 4
- bnez $5, loop
scheduled (L14_clang_sched.asm)
- loop:
- lw $1, 0($4)
- addiu $5, $5, -1 # moved
- addu $2, $1, $2
- addiu $4, $4, 4
- bnez $5, loop
ideal cycles
with load-use stalls
after scheduling
49 instructions + 4
1 stall x 8 passes
same IC, same output
CPI with Load-Use Stalls
worked example
- CPI = 1 + loads x used-next x 1
- = 1 + 0.25 x 0.5 x 1 = 1.125
- speedup vs unpipelined = 5 / 1.125 = 4.44
- time = 1.125 x 200 ps = 225 ps per instruction
1.125
4.44
1.0625
CPI
speedup
CPI if half are scheduled
25% loads, half used next
instead of 5
compiler helps
Class Activity: Find and Count
7 min · pairs
In pairs. Use the rules on slides 11 and 15.
lw $t0, 0($s0); add $t1, $t0, $t2; sub $t0, $t3, $t4; sw $t1, 4($s0) - list every RAW, WAR and WAW. Which stall with forwarding?
No forwarding: add $t1, ... then sub $t4, $t1, ... - stalls? And with one independent instruction between them?
Loads are 30% of instructions; 40% of loads are followed by a use. CPI with forwarding?
lw $t0; lw $t1; add $t2, $t0, $t1; lw $t3; sub $t4, $t2, $t3 (forwarding): stalls? Reorder to remove them.
Common Mistakes
Wrong idea
Correct idea
✗ WAR and WAW stall the 5-stage pipeline.
✓ Only RAW: reads in ID, writes in WB, in order.
✗ Forwarding removes all data stalls.
✓ Not load-use: the value exists only after MEM - 1 stall.
✗ A stall makes the program give a wrong answer.
✓ Stalls cost time only; MARS output is the same.
✗ Structural hazards are always removed.
✓ Sometimes accepted when rare and costly to remove.
Recap Quiz
Answer on paper in 3 minutes, then we check together.
Q1
Name the three kinds of pipeline hazard.
Q2
Why does MIPS use separate instruction and data memories (caches)?
Q3
Which dependence type can stall the in-order 5-stage pipeline: RAW, WAR or WAW?
Q4
Why can forwarding not remove the load-use stall?
Q5
add $t1, $t2, $t3 then sub $t4, $t1, $t5: stalls without and with forwarding?
Homework and Next Lecture
- Read Hennessy and Patterson, Appendix C.2 (data hazards and forwarding).
- Draw both timing diagrams for: lw $t1, 0($t0); lw $t2, 4($t0); add $t3, $t1, $t2; sw $t3, 8($t0). Cycles without and with forwarding?
- Reorder the Lecture 13 milk loop to remove its load-use stall. Check the output in MARS.
- Mid-term practice (6 marks): explain forwarding and the load-use hazard with a diagram.
NEXT LECTURE
Lecture 15: Control Hazards and Pipeline Implementation Issues
Branches: stall, predict-not-taken and the delay slot. Exceptions, precise interrupts and long floating-point operations.