Pipeline Hazards
Pipeline Hazards
Pipeline Hazards
• There are situations, called hazards, that prevent the next instruction in the
instruction stream from executing during its designated clock cycle.
• Hazards reduce the performance from the ideal speedup gained by pipelining.
• There are three classes of hazards:
• 1. Structural hazards arise from resource conflicts when the hardware cannot
support all possible combinations of instructions simultaneously in overlapped
execution.
• 2. Data hazards arise when an instruction depends on the results of a previous
instruction in a way that is exposed by the overlapping of instructions in the pipeline.
• 3. Control hazards arise from the pipelining of branches and other instructions that
change the PC
• Hazards in pipelines can make it necessary to stall the pipeline.
• Avoiding a hazard often requires that some instructions in the
pipeline be allowed to proceed while others are delayed
Structural Hazards
• When a processor is pipelined, the overlapped execution of
instructions requires pipelining of functional units and duplication of
resources to allow all possible combinations of instructions in the
pipeline.
• If some combination of instructions cannot be accommodated
because of resource conflicts, the processor is said to have a
structural hazard.
Structural Hazards
• The most common instances of structural hazards arise
when some functional unit is not fully pipelined.
• Then a sequence of instructions using that unpipelined unit
cannot proceed at the rate of one per clock cycle.
• Another common way that structural hazards appear is
when some resource has not been duplicated enough to
allow all combinations of instructions in the pipeline to
execute.
• For example, a processor may have only one register-file
write port, but under certain circumstances, the pipeline
might want to perform two writes in a clock cycle. This will
generate a structural hazard
Structural Hazards
• When a sequence of instructions encounters this hazard, the pipeline
will stall one of the instructions until the required unit is available.
Solution 1-Structural Hazard: stall
• if forwarding hardware detects that previous ALU has written the reg
corresponding to a source for the current ALU,control logic selects the
forwarded result as the ALU input
• Generalized forwarding
-pass a result directly to the functional unit that requires it;
-forward results to not only ALU inputs but also other types of
functional units;
Data Hazards Requiring Stalls
• Unfortunately, not all potential data hazards can be handled by
bypassing.
• Consider the following sequence of instructions:
LD R1,0(R2)
DSUB R4,R1,R5
AND R6,R1,R7
OR R8,R1,R9
The LD instruction does not have the data until the end of clock cycle 4 (its MEM
cycle), while the DSUB instruction needs to have the data by the beginning of that
clock cycle.
Thus, the data hazard from using the result of a load instruction cannot be
completely eliminated with simple hardware
Solution 2 – STALL
ADAS.MCNSAKLchlk/jn
ADD R1,R2,R3
SUB R4,R1,R5
AND R6,R1,R7
OR R8,R1,R9
• ADD R1,R2,R3
• LOAD R4,8 (R1)
• STR R4 ,12(R1)
• ADD R1,R4 R3
• LOAD R1,0 (R3)
• SUB R4,R1,R5
• AND R6 R4 R1
• OR R8 R4 R1
• The IF, ID and WB stages take one clock cycle each to complete the
operation. The number of clock cycles for the EX stage depends on
the instruction. The ADD and SUB instructions need 1 clock cycle and
the MUL instruction needs 3 clock cycles in the EX stage. Operand
forwarding is used in the pipelined processor. What is the number of
clock cycles taken to complete the following sequence of instructions?
• ADD R2, R1, R0 R2 <- R0 + R1
• MUL R4, R3, R2 R4 <- R3 * R2
• SUB R6, R5, R4 R6 <- R5 - R4
• A 5-stage pipelined processor has Instruction Fetch(IF),Instruction
Decode(ID),Operand Fetch(OF),Execution (EXE)and Write
Operand(WO)stages.The IF,ID,OF and WO stages take 1 clock cycle each for any
instruction.The EXE stage takes 1 clock cycle for ADD and SUB instructions,3
clock cycles for MUL instruction,and 6 clock cycles for DIV instruction
respectively.Operand forwarding is used in the pipeline.What is the number of
clock cycles needed to execute the following sequence of instructions?