Pipeline Very Useful
Pipeline Very Useful
Dipl.-Ing. Elena Gatti (2004), slightly modified by Stefan Freinatis (2005), Uni Duisburg-Essen
What is Pipelining?
• Each step of the pipeline completes a part of an instruction. Different steps are
completing different part of different instructions
• The time required between moving an instruction one step down the pipeline is
a processor cycle
• Because all pipe stages proceed at the same time, the length of a processor
cycle is determined by the time required for the slowest pipeline
• Ideal case: the pipe stages are perfectly balanced (i.e. the length1 of every
pipe stage is the same)
• Real case:
The pipe stages will not be perfectly balanced
Pipelining involves some overhead
2) The only operations that affect memory are LOAD and STORE
3) The instruction formats are few in number, with all instructions typically
being one (= of the same) size
1
“length” means “time”
1
• RISC architectures mostly have just 3 classes of instructions:
1) ALU
2) LOAD and STORE
3) BRANCHES and JUMPS
3) EX (Execution): the ALU operates on the operands prepared in the prior cycle
5) WB (Write Back): write the results into register, whether it comes from the
memory (LOAD) or from the ALU
Hardware Implementation:
2
The 5-stage pipeline for RISC processors
• During each clock cycle the hardware will initiate a new instruction
and will be executing some part of the 5 different tasks
Clock Number
Instruction Number
1 2 3 4 5 6 7 8
Instruction i IF ID EX MEM WB
……
Problem 1: Make sure not to try to perform two or more different operations
with the same data path resource on the same clock cycle!
Fig. 3 shows the hardware implementation of the pipeline above.
Note that in CC4, Instruction i and Instruction i+3 both try to access memory.
In CC5, Instruction i and Instruction i+3 both try to access registers.
3
Time (in CC (Clock Cycles))
Instruction Number
Instruction i+3
MEM REG MEM REG
……
Possible solutions:
- Use separate instruction- and data memories
- Perform the register WRITE in the first half of the clock
cycle (for WB) and READ in the second half (for ID)
IM REG DM REG
Instruction Data
Memory Memory
Problem 2: Make sure that instructions in different stages of the pipeline do not
interfere with each other.
To solve this problem, pipeline registers (also called latches) are implemented.
At the end of a clock cycle the results from a stage are stored into the register (Fig. 5)
IM REG DM REG
5
Pipeline Hazards
• Hazards are situations that prevent the next instruction in the instruction
stream from executing during its designated clock cycle
Example:
All instructions after the ADD use the result of the ADD instruction (value of R1). But
which instructions are affected by hazards? See also Figure 6.
The ADD instruction writes the value of R1 in the WB pipe stage, but the SUB
instruction reads the value during its ID stage (data hazard!). Unless precautions are
taken to prevent it, the SUB instruction will read the wrong value.
The AND instruction is also affected by this hazard: the write of R1 does not
complete until the end of clock cycle 5. Thus, the AND instruction that reads the
registers during clock cycle 4 will receive the wrong result.
The OR instruction operates without incurring a hazard because the register READ is
performed in the second half of the cycle and the register WRITE in the first half.
The XOR instruction also operates properly because its register READ occurs in
clock cycle 6.
6
Time (in CC (Clock Cycles))
Instructions
IM REG DM REG
ADD R1, R2, R3
IM REG DM REG
SUB R4, R1, R5
IM REG DM REG
AND R6, R1, R7
IM REG DM REG
OR R8, R1, R9
Hazards in pipelines can make it necessary to stall the pipeline (the pipeline waits
until the hazard has been cleared)
There exist some techniques (ex: forwarding) that make it possible to avoid stalls.