Sub topic 6
What a Hazard Is and Why It Forces a Stall
This chapter assumes you know the five stages (IF, ID, EX, MEM, WB), what a pipeline register does, and how to draw a timing diagram. It also assumes you can use the formula for total cycles, k + n - 1. Here you learn what breaks the ideal pipeline and why the hardware reacts by waiting.
The ideal pipeline and its three assumptions
An ideal pipeline starts one instruction every clock cycle, so its CPI (cycles per instruction) is 1 and n instructions finish in k + n - 1 cycles. That result depends on three assumptions:
- Every instruction finds the hardware it needs free in every cycle.
- Every instruction finds its input operands ready when it needs them.
- The address of the next instruction is known when it must be fetched.
A hazard is a situation in which the next instruction cannot execute in its designated clock cycle without giving a wrong result or fighting for hardware. Each broken assumption gives one hazard class.
| Broken assumption | Hazard class | Typical cause |
|---|---|---|
| Hardware is free | Structural | One memory port used by both IF and MEM |
| Operands are ready | Data | An instruction reads a register an earlier one has not yet written |
| Next address is known | Control | A branch whose outcome is unknown at fetch time |
A hazard is not the same as a dependence. A dependence is a property of the program: instruction B uses the result of instruction A. It becomes a hazard only when A and B are close enough together in the pipeline that B would need the value before A has delivered it. The same two instructions, placed several slots apart, cause no hazard.
Why the pipeline must stall
The pipeline issues instructions in program order, and every stage works on a different instruction at the same time. When instruction B cannot proceed, the instructions behind it cannot skip past it, because that would change program order. The instructions ahead of B are unaffected and keep moving.
A stall is a cycle in which the hardware holds an instruction, and everything behind it, in its current stage. A bubble is the empty slot the stall leaves in the downstream stages. The control logic builds it by:
- freezing the program counter and the IF/ID register, so the same instructions are fetched and decoded again;
- loading the ID/EX register with a no-operation, meaning all write-enable signals are off.
The bubble moves through EX, MEM and WB like a real instruction but changes nothing. The hardware that detects the hazard, comparing register numbers across stages, sits in ID. It must decide before the instruction leaves ID.
A worked example
Take a data hazard in a five-stage pipeline with no forwarding. Assume the register file is written in the first half of a cycle and read in the second half, so a value written in WB can be read in ID during the same cycle.
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in IDThe add reaches WB in cycle 5. The sub would normally read in cycle 3, which is too early, so it stays in ID until cycle 5.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| add r1, r2, r3 | IF | ID | EX | MEM | WB | |||
| sub r4, r1, r5 | IF | ID | ID | ID | EX | MEM | WB |
The sub repeats ID in cycles 3 and 4 without a valid read, and the read succeeds in cycle 5. EX holds a bubble in cycles 4 and 5, so the sub loses 2 cycles. Two instructions that would take 6 cycles take 8.
Counting stalls with a small simulator
The rule above can be generalized. An instruction cannot leave ID before the previous instruction does, and it cannot leave ID before every source register has been written back. The following program applies this rule without forwarding.
def total_cycles(program):
# program: list of (destination, [sources]); no forwarding,
# register file written in the first half and read in the second half.
id_cycle = 1 # ID cycle of the previous instruction (first one gets 2)
writeback = {} # register -> cycle in which its latest writer does WB
for dest, sources in program:
earliest = id_cycle + 1
for reg in sources:
earliest = max(earliest, writeback.get(reg, 0))
id_cycle = earliest
if dest:
writeback[dest] = id_cycle + 3 # EX, MEM, then WB
return id_cycle + 3 # the last instruction's WB cycle
prog = [
("r1", ["r2", "r3"]),
("r4", ["r1", "r5"]),
("r6", ["r7", "r8"]),
]
total = total_cycles(prog)
ideal = 5 + len(prog) - 1
print(total, ideal, total - ideal) # 9 7 2The independent third instruction does not cause a stall itself. It still finishes 2 cycles late because it sits behind the stalled sub.
What stalls cost
Stalls add cycles but do not change the clock period. With S total stall cycles, the time is
T = (k + n - 1 + S) × clock period
For a 5-stage pipeline running 100 instructions, 20 stall cycles give 124 cycles instead of 104. Equivalently, the effective CPI becomes 1 + S/n = 1.2 (S/n is the average stall cycles per instruction). Later chapters on forwarding, branch handling and effective CPI work to shrink this S term.
Common errors
- Stalling only one instruction. A stall holds the stalled instruction and every instruction behind it. Total time grows by the full stall count.
- Stalling the instructions ahead. Older instructions are never held by a hazard on a younger one, and they finish normally.
- Mixing up stall and bubble. The stall is the cycle the instruction waits. The bubble is the empty slot that travels down the later stages as a result.
- Treating every dependence as a hazard. Check the distance between the two instructions. In the model above, a dependent instruction 3 or more slots after its producer needs no stall.
- Ignoring the independent instruction behind. In the simulator example, instruction 3 has no dependences but is still delayed by 2 cycles.
Quick reference
- A hazard arises when one of the ideal pipeline's three assumptions fails: free hardware, ready operands, known next address.
- The pipeline responds by holding the instruction in place and inserting bubbles ahead of it.
- Each stall cycle adds exactly one cycle to total execution time and raises CPI above 1.
Exercise
A five-stage pipeline has no forwarding. The register file is written in the first half of a cycle and read in the second half. It runs these three instructions in order: (1) add r1, r2, r3; (2) add r4, r1, r5; (3) add r6, r4, r1. How many cycles do they take in total, and how many are stall cycles?
Show answer
Instruction 1 has ID in cycle 2 and WB in cycle 5. Instruction 2 needs r1, so its ID waits until cycle 5 (2 stalls), and its WB is in cycle 8. Instruction 3 needs r4 from instruction 2, so its ID waits until cycle 8 (2 more stalls), and its WB is in cycle 11. The total is 11 cycles. The ideal time is 5 + 3 - 1 = 7 cycles, so there are 4 stall cycles. The r1 dependence of instruction 3 adds nothing, because r1 is ready long before cycle 8.
Exercise
While instruction B is stalled in ID waiting for an operand, what happens to the instruction directly ahead of B, and to the instruction directly behind B?
Show answer
The instruction ahead of B has already moved into EX or later, so it continues unaffected. The instruction behind B is held in IF because the PC and the IF/ID register are frozen. It cannot advance until B leaves ID.