Week 4 - Pipelining
Week 4 - Pipelining
Architecture
Week 4: Pipelining
Improved Performance
• But not doubled:
– Fetch usually shorter than execution (cf. reading
and storing operands)
• Prefetch more than one instruction?
– Any jump or branch means that prefetched
instructions are not the required instructions
• Add more stages to improve performance
Two Stage Instruction Pipeline
Pipelining (six stages)
1. Fetch instruction
2. Decode instruction
3. Calculate operands (i.e., EAs)
4. Fetch operands
5. Execute instructions
6. Write result
• Structural Hazard
• Data Hazard
• Control Hazard
Pipeline Hazards (1)
• Pipeline Hazards are situations that prevent the next
instruction in the instruction stream from executing in its
designated clock cycle
• Hazards reduce the performance from the ideal speedup
gained by pipelining
• Three types of hazards
– Structural hazards
• Arise from resource conflicts when the hardware can’t support all possible
combinations of overlapping instructions
– Data hazards
• Arise when an instruction depends on the results of a previous instruction
in a way that is exposed by overlapping of instruction in pipeline
– Control hazards
• Arise from the pipelining of branches and other instructions that change
the PC (Program Counter)
Pipeline Hazards (2)
• Hazards in pipeline can make the pipeline to stall
• Eliminating a hazard often requires that some
instructions in the pipeline to be allowed to
proceed while others are delayed
– When an instruction is stalled, instructions issued
latter than the stalled instruction are stopped, while
the ones issued earlier must continue
• No new instructions are fetched during the stall
Structural Hazards (1)
• If certain combination of instructions can’t be accommodated
because of resource conflicts, the machine is said to have a
structural hazard
• It can be generated by:
– Some functional unit is not fully pipelined
– Some resources has not been duplicated enough to allow all the
combinations in the pipeline to execute
– For example: a machine may have only one register file write port, but
under certain conditions, the pipeline might want to perform two
writes in one clock cycle – this will generate structural hazard
• When a sequence of instructions encounter this hazard, the pipeline will stall
one of the instructions until the required unit is available
• Such stalls will increase the Clock cycle Per Instruction from its ideal 1 for
pipelined machines
Structural Hazards (2)
1 2 3 4 5 6 7 8 9 10
load IF ID EX MEM WB
• The use of results from ADD instruction causes hazard since the
register is not written until after those instructions read it.
Data Hazards (3)
• Eliminate the stalls for the hazard involving SUB and AND instructions
using a technique called forwarding
Data Hazards (4)
LW R1, B IF ID EX MEM WB
LW R2, C IF ID EX MEM WB
SW A, R3 IF stall ID EX MEM WB
Reducing the stall from branch hazards by moving the zero test and branch calculation into ID
phase of pipeline. It uses a separate adder to compute the branch target address during ID.
Because the branch target addition happens during ID, it will happen for all instructions. The
branch condition (Regs[IF/ID.IR6…10] op 0) will also be done for all instructions. The selection of
the sequential PC or the branch target PC will still occur during IF, but now it uses values from ID
phase, rather than from EX/MEM register. In this case, the branch instruction is done by the end
of ID phase, so EX, MEM and WB stages are not used for branch instructions anymore.