Pipeline Hazards
Pipeline Hazards
(Notes based on: Computer Architecture: A Quantitative Approach, 5th Edition, John L.
Hennessy and David A. Patterson, Morgan Kaufmann, 2012)
3 categories of hazards:
(i) Structural hazards: hardware resource conflicts, i.e. two different instructions
in the pipeline need the same resource during the same clock cycle. Example:
If a unified cache, then IF (always) needs an instruction and MEM may need a
data word.
(ii) Data hazards: an instruction depends on the results of a previous instruction
that has not yet completed passing through the pipeline.
(iii) Control hazards: pipelining of branches and other instructions that change the
PC.
If a hazard occurs, then the pipeline is stalled. Instructions issued later than the stalled
instruction are also stalled, but instructions issued before that continue their execution in
the pipeline. Thus, no new instructions are issued during the stall. A stall is also called
a pipeline bubble or just a bubble, since it “floats” through the pipeline taking up space
but performing no useful work.
Data hazard example: Consider the following sequence of instructions:
DADD R1, R2, R3
DSUB R4, R1, R5
AND R6, R1, R7
OR R8, R1, R9
XOR R10, R1, R11
The result of the DADD instruction goes into R1, but not until the WB stage. The next
4 instructions all use R1 as an operand. We need to make sure that they are using the
value computed by the DADD instruction, which is what the programmer intended.
Assume that the DADD instruction IF stage occurs in cycle 1. The DSUB and AND
instructions have a data hazard, because R1 is written in (the first half of) cycle 5, but is
read by DSUB in cycle 3 and by AND in cycle 4. The OR instruction does not have a
data hazard because it reads R1 during the second half of cycle 5, whereas the write to
R1 occurs in the first half of cycle 5. There is also no hazard for the XOR instruction,
which reads R1 in cycle 6.
The solution to the above data hazards is to use forwarding. The correct result for R1 is
in a pipeline register even if it has not yet been written back to the register file. So, just
make that pipeline register value be a possible ALU input by feeding it back to MUXs
present at the ALU inputs. (It may be needed as a “left input” or a “right input”).
Specifically:
1. The ALU result from both the EX/MEM and MEM/WB pipeline registers is fed
back to each of the ALU inputs.
2. If the forwarding logic detects a data hazard, the control logic selects the
forwarded value rather than the value read from the register file (which is the old
or “stale” value).
In the previous example, the value in EX/MEM is sent to an ALU input for DSUB. The
value in MEM/WB is sent to an ALU input for AND.
Data hazards requiring stalls: Not all data hazards can be eliminated by forwarding. For
example, in a load instruction, the value from data memory is available at the end of
MEM. If the next instruction uses that as an operand (which would be in its EX stage,
which would be at the same time as MEM of the load instruction), then this cannot be
accommodated. The only possibility is to stall the pipeline for one cycle. For example,
the following timing will not work:
LD R1, 0(R2) IF ID EX MEM WB
DSUB R4, R1, R5 IF ID EX MEM WB
AND R6, R1, R7 IF ID EX MEM WB
OR R8, R1, R9 IF ID EX MEM WB