Lecture # Pipelining
Lecture # Pipelining
• Invalid Case:
• There cannot be a valid forwarding path from the output of the memory
access stage in the first instruction to the input of the execution stage of
the following, since that would mean going backward in time
Pipeline Hazards
Data Hazard For the code sequence
lw $s0, 20($t1)
sub $t2, $s0, $t3
• For example,
• The first instruction was a load of $s0 instead of an add.
• Data value will only be available only after the fourth stage which is too late for forwarding
• Hence, even with forwarding, we would have to stall one stage for a load-use data
hazard officially called a pipeline stall, but often given the nickname bubble.
Pipeline Hazards
Control Hazards
• Control hazard comes arises when there is need to make a
decision based on the results of one instruction while
others are executing
• For example, Branch Instruction
• The instruction next to branch instruction must be fetched on next
clock cycle
• Because the pipeline cannot possibly know what the next instruction should
be. Since it only just received the branch instruction from memory
• The decision is still to be made based on the operands of branch instruction
• There will be pipeline stall immediately after we fetch a branch
• Because waiting until the pipeline determines the outcome of the branch
and knows what instruction address to fetch from
Pipeline Hazards
Control Hazards
• If we cannot resolve the branch in the second stage
• As is often the case for longer pipelines, then we’d see an
even larger slowdown if we stall on branches
• Consequences:
• The cost of this option is too high for most computers to use
• Second Solution
• Predict: Computers do indeed use prediction to handle
branches
• One simple approach is to predict always that branches will be untaken
• When you’re right, the pipeline proceeds at full speed
• Only when branches are taken does the pipeline stall
Pipeline Hazards
Control Hazards
• A more sophisticated version of branch prediction would
have some branches predicted as taken and some as
untaken
• In the case of programming, at the bottom of loops are
branches that jump back to the top of the loop
• Since they are likely to be taken and they branch backward,
we could always predict taken for branches that jump to an
earlier address
Pipeline Hazards
Control Hazards
• Third solution: Delayed branch in computers are actually used by the MIPS
architecture to deal branches.
• The delayed branch always executes the next sequential instruction, with
the branch taking place after that one instruction delay.
• It is hidden from the MIPS assembly language programmer because the assembler
can automatically arrange the instructions to get the branch behavior desired by
the programmer.
• MIPS software will place an instruction immediately after the delayed branch
instruction that is not affected by the branch, and a taken branch changes the
address of the instruction that follows this safe instruction.
• In our example, the add instruction before the branch in Figure 4.31 does
not affect the branch and can be moved after the branch to fully hide the
branch delay. Since delayed branches are useful when the branches are
short, no processor uses a delayed branch of more than one cycle. For
longer branch delays, hardware-based branch prediction is usually used.
Pipeline Hazards
Control Hazards
• In Delayed branch example
• add instruction before the branch does not affect the branch and can be
moved after the branch to fully hide the branch delay
• Since delayed branches are useful when the branches are short, no processor
uses a delayed branch of more than one cycle
• For longer branch delays, hardware-based branch prediction is usually used