Learning Outcomes
SLO | After this lecture you can… | CLO | Bloom |
|---|---|---|---|
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 |
§1 Hazards and Structural Hazards
§1.1 Three kinds of hazard
Slide L14-S06
A hazard is a situation that stops the next instruction from starting in its planned clock cycle (hazard = danger). The pipeline then stalls: it holds the instruction and everything behind it, and a bubble (an empty slot that does no work) moves down the pipeline.
Hazard | Cause | Example | Lecture |
|---|---|---|---|
Structural | Two instructions need the same hardware in the same cycle | One memory for both instructions and data | 14 |
Data | An instruction needs a value that is not ready yet | add $t1, ... followed by sub $t4, $t1, ... | 14 |
Control | The next PC is not known until a branch is decided | beq ... | 15 |
Effect of stalls |
§1.2 One memory, one register file
Slide L14-S07
With one memory port, the lw in MEM (cycle 4) and the fourth instruction in IF (cycle 4) both need memory - a structural hazard every time a load or store reaches MEM. MIPS uses separate instruction and data memories; real processors use separate L1 instruction and data caches. The register file is used by ID (read) and WB (write) in the same cycle: it has two read ports and one write port, and it writes in the first half of the cycle and reads in the second half. Because every unit is used in only one stage, the classic 5-stage pipeline has no structural hazards. Real-world: one machine that both washes and dries - load B waits while load A is drying.
§1.3 What a structural hazard costs
Slide L14-S08
Hennessy and Patterson: 40% data references |
Designers sometimes accept a structural hazard when it is rare and removing it is expensive in chip area or power (for example a floating-point divider that is not pipelined). Real-world: a small clinic with one doctor accepts a queue.
§2 Data Hazards
§2.1 RAW, WAR and WAW
Slide L14-S10
Type | Example (earlier → later) | Meaning | Stalls the 5-stage pipeline? |
|---|---|---|---|
RAW - read after write (true dependence) | add $t1, $t2, $t3 → sub $t4, $t1, $t5 | Later must read the NEW value | Yes |
WAR - write after read (antidependence) | sub $t4, $t1, $t5 → add $t1, $t2, $t3 | Earlier must read the OLD value | No |
WAW - write after write (output dependence) | lw $t1, 0($s0) → add $t1, $t2, $t3 | Final value must be the later one | No |
In the in-order 5-stage pipeline every instruction reads its registers in ID and writes in WB, in program order, so WAR and WAW can never go wrong. Only RAW causes hazards. WAR and WAW (name dependences) matter in out-of-order processors (Lectures 17, 20-21). Real-world RAW: the bill cannot be printed before the meter reading is entered.
§2.2 Stalls without forwarding
Slide L14-S11
L14_classic.asm (MARS: $t4 = 14, $t6 = 6, $t8 = 15, $s0 = 5) |
Without forwarding a value can be used only after WB has written it. The register file writes in the first half of the cycle and reads in the second half, so the reader's ID may be in the same cycle as the writer's WB. Diagram (Hennessy and Patterson style: the stalled instruction repeats ID, the one behind it waits in IF):
Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
Distance from producer (instructions) | 1 | 2 | 3 or more |
|---|---|---|---|
Stalls without forwarding | 2 | 1 | 0 |
Total 11 cycles instead of 9. The 'and' at distance 2 needs no extra stall because sub's stalls already delayed it.
§2.3 Dependences in a real program
Slide L14-S12
L14_bill.asm (prints 3185; stores it at 0x10010008) |
# | MARS Basic instruction | Stalls, no forwarding | Stalls, forwarding |
|---|---|---|---|
1 | lui $at, 0x1001 | 0 | 0 |
2 | ori $s0, $at, 0x0 | 2 | 0 |
3 | lw $t0, 0($s0) | 2 | 0 |
4 | lw $t1, 4($s0) | 0 | 0 |
5 | sub $t2, $t0, $t1 | 2 | 1 |
6 | sll $t3, $t2, 3 | 2 | 0 |
7 | add $t4, $t3, $t2 | 2 | 0 |
8 | addiu $t5, $zero, 35 | 0 | 0 |
9 | add $t4, $t4, $t5 | 2 | 0 |
10 | sw $t4, 8($s0) | 2 | 0 |
Note that la became lui + ori, which adds a dependence of its own through $at. There is also a WAW on $t4 (two adds) - harmless in order. Without forwarding: 14 stalls, 28 cycles for 10 instructions.
§3 Forwarding and the Load-Use Hazard
§3.1 Forwarding (bypassing)
Slide L14-S14
An instruction really needs its operand at the start of EX, and an ALU instruction has its result at the end of its own EX. Forwarding adds wires and multiplexers that send the result from the EX/MEM pipeline register (distance 1) or the MEM/WB register (distance 2) straight to the ALU inputs. The forwarding unit compares the source register numbers of the instruction in EX with the destination numbers held in EX/MEM and MEM/WB. With forwarding the classic sequence has no stalls:
Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
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 |
Real-world: the cook hands the dish straight to the waiter instead of putting it on the shelf first.
§3.2 The load-use hazard
Slide L14-S15
A load has its value only at the end of MEM. If the next instruction uses it, it needs the value at the start of EX in that same cycle - forwarding cannot send a value back in time. The hazard detection unit in ID sees a load in EX whose destination is a source of the instruction in ID, and inserts one bubble. Then MEM/WB forwards the value.
Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
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 |
Producer → next instruction | No forwarding | Forwarding |
|---|---|---|
ALU instruction | 2 stalls | 0 |
Load | 2 stalls | 1 stall (load-use) |
§3.3 Three machines, one program
Slide L14-S16
Version | Stalls | Cycles (10 instructions) | Window CPI |
|---|---|---|---|
No forwarding | 14 | 28 | 2.4 |
Forwarding | 1 | 15 | 1.1 |
Forwarding + scheduled (li moved up) | 0 | 14 | 1.0 |
No forwarding, scheduled | 13 | 27 | 2.3 |
Scheduling moves an independent instruction (li $t5, 35) into the slot after the load. Same instructions, same result (3185 in MARS), same instruction count. Forwarding removes most stalls; scheduling removes the load-use stall.
§3.4 Real compiler code
Slide L14-S17
L14_clang.asm - clang -O1 MIPS loop of Lecture 12, runnable in MARS (prints 1060) |
lw $1 is used by the very next addu: one load-use stall in each of the 8 passes. MARS counts 49 instructions; ideal 53 cycles, with stalls 61, after moving 'addiu $5, $5, -1' between lw and addu 53 cycles (L14_clang_sched.asm, same output). Hardware (forwarding) and compiler (scheduling) work together; Lecture 18 covers compiler scheduling and loop unrolling.
§3.5 CPI with load-use stalls
Slide L14-S18
Worked example |
§4 Common Mistakes
Slide L14-S20
Wrong idea | Correct idea |
|---|---|
WAR and WAW stall the 5-stage pipeline. | Only RAW: reads in ID and writes in WB happen in program order. |
Forwarding removes all data stalls. | Not load-use: the value exists only at the end of MEM - 1 stall. |
A stall makes the program give a wrong answer. | Stalls cost time only; the MARS output is the same. |
Structural hazards are always removed. | Sometimes accepted when rare and costly to remove. |
Word Bank
Word | Simple meaning | Real-world example |
|---|---|---|
Hazard | Something that stops the next instruction starting on time | A red signal on the road |
Stall / bubble | A cycle in which the instruction waits; an empty slot moving down the pipe | An empty seat on a moving bus |
Structural hazard | Two instructions need the same hardware | Two customers, one ATM |
Data hazard | A value is needed before it is ready | Printing a bill before the reading is entered |
Dependence | One instruction uses a register another writes | Recipe step 2 needs the result of step 1 |
Forwarding / bypassing | Send a result straight to the ALU, skipping the register file | Hand the dish to the waiter directly |
Load-use hazard | Using a loaded value in the very next instruction | Asking for a file before it arrives from the record room |
Hazard detection unit | Hardware in ID that inserts a bubble for a load-use | A guard who stops the queue for a moment |
Scheduling | Reordering independent instructions to fill stall slots | Doing another job while the kettle boils |
Self-Check Questions
- Name the three kinds of hazard with one example each. (§1.1)
- Why does the classic MIPS pipeline have no structural hazards? (§1.2)
- Machine A: 30% data references stall 1 cycle, clock 1.1 times faster than B. Which is faster, and by how much? (§1.3)
- List all RAW, WAR and WAW in: lw $t0, 0($s0); add $t1, $t0, $t2; sub $t0, $t3, $t4; sw $t1, 4($s0). (§2.1)
- Without forwarding, how many stalls at distance 1, 2 and 3? Why is distance 3 free? (§2.2)
- Which two forwarding paths does the classic sequence use? (§3.1)
- Draw the load-use diagram and explain the one stall. (§3.2)
- Reorder lw $t0; lw $t1; add $t2, $t0, $t1; lw $t3; sub $t4, $t2, $t3 to remove all stalls. (§3.3)
- Why did the clang loop take 61 cycles instead of 53? (§3.4)
- Loads 30%, 40% used next: CPI with forwarding? (§3.5)
Answers: A = 1.3 / 1.1 = 1.18 T, so B is 1.18 times faster; list: RAW $t0, RAW $t1, WAR $t0, WAW $t0 - only lw → add stalls (9 cycles); reorder → lw $t0; lw $t1; lw $t3; add; sub (11 → 9 cycles); CPI 1.12.
Further Reading
- Hennessy and Patterson (2012): Appendix C, section C.2 (the major hurdle of pipelining - pipeline hazards).
- Patterson and Hennessy, Computer Organization and Design: Chapter 4.7 (data hazards: forwarding versus stalling).