Modern processors do not execute one instruction at a time. They overlap them in an assembly line — a pipeline — so that while one instruction is being decoded the previous one is already being executed and the one before that is writing its result back to a register. On a five-stage RISC pipeline the stages are Fetch → Decode → Execute → Memory → Write-back.
This overlap is why a 3 GHz chip can retire billions of operations per second. But the assembly-line metaphor has a catch: sometimes one worker needs the output of the worker two steps behind them, or the whole line needs to change direction mid-stream. These collisions are called hazards, and they come in three flavours:
- Data hazards — an instruction needs a value that has not been written yet by an earlier instruction still in the pipe. The classic case is a RAW (Read-After-Write) dependency: instruction tries to read a register that instruction is still computing.
- Control hazards — a branch instruction changes the program counter, but the next one or two instructions after it have already been fetched. Those instructions may be wrong.
- Structural hazards — two instructions need the same hardware resource (say, a single memory port) at the same cycle.
Without any countermeasures the pipeline must insert idle cycles — bubbles (also called stalls or NOPs) — to let the dependency resolve. Forwarding (also called bypassing) short-circuits those waits by routing a result directly from one pipeline stage to the input of an earlier stage. The gain is striking: a RAW that costs two stall cycles with no forwarding costs zero stall cycles with forwarding.
Comments
Loading comments...