






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Rensselaer Polytechnic Institute Spring 2009
We have all seen and experienced examples of pipelining in our daily lives. The book uses a laundry analogy (Figure 4.25), but any kind of “assembly line” type of operation might be a good example.
The laundry analogy is a good one. Consider how much more quickly laundry can be finished if we take advantage of the fact that the washer and dryer (and, in the book’s example, the folder and the storer) can all operate in parallel, and each stage can start doing its work as soon as the previous stage completes its work. We don’t process a single load of laundry any more quickly, but the overlap in successive loads leads to a faster overall completion time.
Similar ideas can be used to create a pipeline of instructions being executed by a processor. MIPS instructions can be executed in phases. These phases can be used to create such a pipeline.
We’ll consider a pipeline for MIPS, which is typical of many RISC pipelines.
We have seen the instructions in our MIPS subset include five steps:
For our example, we will assume that the register access stages cost 100 time units each, while memory access and ALU operations cost 200.
Figure 4.26 in the text shows how long the different instructions in our MIPS subset will take given these latencies.
Figure 4.27 in the text shows the single-cycle and a simple pipelined execution.
Note that the speed of our pipeline in this case is limited to the speed of slowest component. A single instruction now takes as much as 900ps to complete.
Note also that the register accesses are strategically set up so that a register read takes place in the second half of a 200ps slot and the register write takes place in the first half. This will be beneficial later on.
Figure 4.28 shows a symbolic representation of the execution of an add instruction showing which components are used in each step.
The MIPS instruction set was designed with pipelines in mind, so it has features that make pipelin- ing easier:
A typical RISC system might have a 5-stage pipeline like this.
A system with a more complex instruction set may have a 12-18 stage pipeline.
Goal: 100-200 stage pipelines to get very significant speedups.
Many architectures now have multiple pipelines as well.
The original Pentium had two pipelines, and a smart compiler could keep both pipelines busy, effectively doubling the number of instructions completed in the same number of cycles.
If 2 is good, why not 4 or more? Too much duplication of hardware.
Another option: just have multiple functional units, not all stages of the pipeline.
FPUs
IF ID OD X WB X
X
X
X
ALUs LOAD STORE
This is especially useful if the execute stage takes longer anyway.
This is a superscalar processor.
We can try to minimize the effect of control hazards with two techniques: delayed branching and branch prediction.
Consider the execution of this code in a pipelined system:
j somewhere add $3,$4,$
The jump/branch is in the pipeline, but by the time we know it’s a branch, the add is already in the pipeline.
A compiler can do this on purpose – we know the add is going to happen even though we’re taking a branch before we get there.
Most modern architectures have a delayed branch of 1 or 2 instruction cycles to allow this opti- mization.
If we don’t have something to do in those delay slots , the compiler may have to fill them with nops.
This helps eliminate some bubbles in the pipeline, but when we do have nops, that’s still a bubble.
Compilers (or programmers) can also unroll loops to help eliminate branches and keep pipelines full.
This is generally a good thing anyway because branches aren’t doing useful work – just wasted time.
But what about conditional branches?
bne loop add
If we take the branch, then the add instruction should never have happened and we have to kill the instruction.
Branch prediction is very useful – try to determine which instruction is most likely to be executed after a branch in an attempt to keep the pipeline going.
Consider
if (C) S else S
Which is more likely? Programmers probably make the “then” part the more likely case.
So a compiler might want to set things up to start pipelining S1 after the condition is checked.
How about a while loop or a for loop?
while (C) S
Here, C will be false only once for the life of the while loop, so the best assumption is to predict a successful branch (another time around the loop).
The UltraSparc III actually has special branch instructions that a compiler can use when it knows a certain branch is likely to be taken the vast majority of the time.
Some rules of thumb:
This gives about an 80% success rate for human-written code.
Today’s branch prediction techniques in optimizing compilers are more intelligent and clever and can get more like 98%.
No matter how good our branch prediction is, it will sometimes fail and we need to be able to make sure instructions can be cancelled.
One possibility: allow instructions to do everything but store their result until we’re absolutely sure.
Another headache: multiple conditional branches in the pipeline.
We will now consider how to construct a data path and control to manage the 5-stage pipeline for our MIPS subset.
In Figure 4.33, we see the single-cycle data path we looked at before, redrawn to show the pipeline phases.
For the most part, information flows left-to-right in this diagram. The exceptions (in blue) represent hazards:
- the effective address stored in the EX/MEM pipeline registers is used to retrieve a value from the data memory - this value is stored in the MEM/WB pipeline registers
Augmenting control to support a pipelined control may seem daunting, but it really is not as bad as we’d expect.
We can use the same control lines as we did for the single-cycle implementation, but each stage should be using the control as set for the instruction it is executing.
Control values can be stored in the pipeline registers to make this happen.
Figure 4.46 shows the pipelined data path with the control added.
We need only store control signals at each stage that are to be used in that or in subsequent stages (Figure 4.50)
Figure 4.51 shows the complete pipelined datapath.
We noticed earlier that our pipelines cannot always operate at full capacity.
We first look at enhancements to our pipelined datapath and control that will deal with data hazards.
Figure 4.52 shows the dependencies in an unfortunate instruction sequence
Upon closer inspection, though, we see that the values needed by the second and third instructions are available in pipeline registers, as shown in Figure 4.53.
We need to detect when these conditions occur and account for them. There are two main cases to deal with:
For the first case, we need to divert a value from the EX/MEM pipeline registers as an input to the ALU. In the second, we take a value from the MEM/WB pipeline registers.
The text breaks this info four cases we can check for:
1a. EX/MEM.RegisterRd = ID/EX.RegisterRs
1b. EX/MEM.RegisterRd = ID/EX.RegisterRt
2a. MEM/WB.RegisterRd = ID/EX.RegisterRs
ForwardA = 01 if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0) and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) ForwardB = 01
Figures 4.56 and 4.57 show the data path augmented with additional lines and a forwarding unit that can resolve data hazards.
We next consider an even more unfortunate data hazard, a “load-use” hazard as shown in Figure 4.58.
This one cannot be resolved through forwarding – the value has not yet been retrieved from the data memory by the time it is needed.
In his case, we need to stall the pipeline to wait for the value to become available, as shown in Figure 4.59.
This is accomplished by adding a hazard detection unit.
Using our notation from before, we know a stall is necessary when:
ID/EX.MemRead and ((ID/EX.RegisterRt = IF/ID.RegisterRs) or (ID/EX.RegisterRt = IF/ID.RegisterRt))
Note that we are detecting this condition as early as possible – when the offending sequence in- structions are in the IF and ID stages. This makes it easier to stall.
The stall is accomplished by adding a bubble to the pipeline – an instruction that does nothing, a nop , or “no op”.
This requires that:
The data path augmented with a hazard detection unit is shown in Figure 4.60.
Stalls reduce the performance – we’re spending time executing a nop instead of meaningful in- structions. However, they are essential to ensure correct behavior.
An optimizing compiler should be aware of the details of the pipeline and can rearrange instruc- tions (in many cases) to avoid the need for a stall at execution time.
Recall that a control hazard occurs when a branch/jump instruction is being executed and subse- quent (partially-executed) instructions in the pipeline need to be cancelled since they never should have been executed.
The specifics of our pipeline mean that a conditional branch’s outcome is not known until the MEM phase.
Figure 4.61 shows a branch instruction that results in a control hazard – instructions already in the pipeline that are not to be executed are flushed (their control lines are set to 0).
Figure 4.62 shows how we can reduce the cost of a control hazard by adding hardware to determine the result of a conditional branch sooner (during ID ):
Another technique that works well with short pipelines (such as our 5-stage pipeline) is the use of a branch delay slot.
Figure 4.64 shows some examples of how code can be reordered to take advantage of branch delay slots.
We already discussed some branch prediction techniques. The text discusses some of them in a bit more detail.