Sub topic 6
What a Hazard Is and Why It Forces a Stall
Prerequisites and the basic idea
Assume you know the CPU, registers, memory, clock cycles, and simple assembly instructions such as add, lw, sw, and conditional branches. You should also know that a pipeline divides instruction execution into stages and overlaps different instructions in the same clock cycle.
A pipeline hazard is a condition that prevents an instruction from safely entering or completing its next stage during the planned clock cycle. The pipeline must then use a remedy such as forwarding, prediction, flushing, or a stall.
A stall is a deliberate delay. The processor pauses one instruction, or inserts an empty slot called a bubble, while older instructions make the required resource or value available. A hazard does not always cause a stall. Hardware may remove some hazards through forwarding or other techniques.
Three hazard classes
| Hazard class | Cause | Typical example | Common remedy |
|---|---|---|---|
| Structural | Two stages need one hardware resource | Instruction fetch and data access share one memory | Add a resource or stall |
| Data | An instruction depends on another instruction's result | `sub` reads a register produced by `add` | Forward the result or stall |
| Control | The next instruction depends on a branch decision | A branch changes the program counter | Predict, delay, or flush |
A structural hazard occurs when hardware cannot support all requested operations in the same cycle. For example, if one memory is used for both instruction fetch and data access, a load instruction in the MEM stage may conflict with another instruction in the IF stage.
A data hazard occurs when instructions are related by their operands. The important dependency in a simple in-order pipeline is RAW, or read after write:
add r1, r2, r3 # produces r1
sub r4, r1, r5 # reads r1The sub must use the value produced by add. If the register file is not updated until the WB stage, the value may not be available when sub reaches EX.
Two other dependency names are common in more advanced pipelines:
- WAR, or write after read, occurs when a later instruction writes a location before an earlier instruction has read it.
- WAW, or write after write, occurs when two instructions write the same location in the wrong order.
WAR and WAW usually require out-of-order execution to appear. They are not normally hazards in a basic five-stage, in-order pipeline.
A control hazard occurs after a branch or jump. Until the processor knows the correct next program counter, it may fetch instructions from the wrong path. Those instructions must be discarded, or the processor must wait.
How a stall appears
Consider a five-stage pipeline:
Cycle: 1 2 3 4 5 6
add: IF ID EX MEM WB
sub: IF ID -- EX MEMThe -- represents a bubble. The sub waits because its source value is unavailable at the required time. Forwarding may remove this stall for an ordinary ALU-to-ALU dependency, but a load-use dependency often still needs one cycle.
Exercise
Classify this hazard: lw r1, 0(r2) followed immediately by add r3, r1, r4. Why can forwarding still require a stall?
Show answer
It is a RAW data hazard. The load's value becomes available only after the MEM stage, but the add needs it at the start of EX, so the pipeline usually inserts one stall cycle.