Sub topic 9
Stalls and Bubbles: Resolving Hazards by Waiting
Assume that you know the five pipeline stages: instruction fetch (IF), instruction decode (ID), execute (EX), memory access (MEM), and write-back (WB). This chapter also assumes an in-order, single-issue pipeline, where one instruction enters the pipeline per clock cycle.
Why a Pipeline Must Sometimes Wait
A pipeline normally starts one instruction every cycle. A hazard is a condition that prevents an instruction from safely entering its next stage. If the processor cannot solve the hazard by forwarding, duplicating a resource, or predicting correctly, it must wait.
A stall is a pause in instruction progress. A bubble is an empty pipeline slot created by a stall. The processor usually holds the dependent instruction in place and inserts a bubble into a later stage.
For example:
add r1, r2, r3 # produces r1
sub r4, r1, r5 # consumes r1The sub instruction needs the value produced by add. If the pipeline has no forwarding path, sub must wait until add writes r1 in the WB stage.
Inserting a Bubble
Consider a five-stage pipeline without forwarding. Assume that the register file writes during the first half of a clock cycle and reads during the second half.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| `add r1, r2, r3` | IF | ID | EX | MEM | WB | ||
| `sub r4, r1, r5` | IF | ID | Stall | Stall | EX | MEM | |
| Bubble |
The sub instruction reaches ID in cycle 3. It cannot proceed to EX because r1 is not written until WB in cycle 5. It therefore remains in ID during cycles 4 and 5. The pipeline control inserts two empty cycles before sub reaches EX in cycle 6.
The exact stall count depends on the timing assumptions. Always check:
- The stage where the producer makes its result available.
- The stage where the consumer needs that result.
- Whether forwarding exists.
- Whether register writes and reads can occur in the same cycle.
What a Stall Does to Performance
For an ideal pipeline, the first instruction takes k cycles to leave a k-stage pipeline, and each later instruction completes one cycle later:
\[ T_{\text{ideal}} = k + n - 1 \]
If hazards add s stall cycles:
\[ T = k + n - 1 + s \]
A stall increases total execution time and lowers throughput. It does not usually change the latency of the instruction already in the pipeline. It delays instructions behind the hazard.
A common mistake is to count an instruction’s entire pipeline latency as a stall. Only the extra waiting cycles count. The normal IF, ID, EX, MEM, and WB stages are already included in the ideal pipeline formula.
> callout note > A bubble is counted as a cycle in the pipeline schedule, but it is not an instruction. Do not count it as an additional completed instruction.
A Systematic Counting Method
For a hazard problem:
- Draw the ideal stage positions for each instruction.
- Identify when each source operand is produced.
- Identify when the dependent instruction needs that operand.
- Apply the stated forwarding and register-file rules.
- Insert the minimum number of bubbles.
- Recount the final completion cycle.
Exercise
In a five-stage pipeline without forwarding, add r1, r2, r3 enters IF in cycle 1 and sub r4, r1, r5 enters IF in cycle 2. If add writes r1 in WB during cycle 5 and the register file permits a same-cycle write followed by read, in which cycle does sub enter EX?
Show answer
sub enters EX in cycle 5. It waits in ID during cycles 3 and 4, then reads the value written during the first half of cycle 5 and executes during the second half.