Docsity

Pipelines - Computer Organization - Lecture Notes | CSCI 2500, Study notes of Computer Architecture and Organization

Toggle sidebar
bg1
Computer Science 2500
Computer Organization
Rensselaer Polytechnic Institute
Spring 2009
Topic Notes: Pipelines
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:
1. IF: instruction fetch
2. ID: register read and instruction decode
3. EX: execute or calculate address
4. MEM: memory read or write
5. WB: write back register
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.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Pipelines - Computer Organization - Lecture Notes | CSCI 2500 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

Computer Science 2500

Computer Organization

Rensselaer Polytechnic Institute Spring 2009

Topic Notes: Pipelines

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:

  1. IF : instruction fetch
  2. ID : register read and instruction decode
  3. EX : execute or calculate address
  4. MEM : memory read or write
  5. WB : write back register

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:

  1. all instructions are the same length, allowing the next IF in the pipeline to proceed immedi- ately
  2. there are very few instruction formats, allowing both register read and instruction decode to be in the ID pipeline stage
  3. the limitation on memory access to just the lw and sw instructions allows EX to combine execution and address calculation
  4. memory alignment means we always retrieve the entire instruction in a single memory access

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.

Hazards

Delayed Branching and Branch Prediction

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:

  1. If a branch leads deeper into code, assume the branch will fail.
  2. Otherwise, assume the branch will be taken.

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.

Pipelined Datapath and Control

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:

  • WB puts a result back into the register file – this is a data hazard
  • MEM may replace the PC with a branch/jump target – a control hazard

- 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

  • Figure 4.38 (bottom) shows the WB stage for a lw instruction - the value retrieved from memory, saved in the MEM/WB pipeline registers, is sent back to the register file for storing
  • Figure 4.41 adds extra values to the pipeline registers in recognition of the fact that the regis- ter number needs to be retained for the WB stage (if we don’t, we’d be using the destination register from a different instruction!)
  • Figure 4.42 highlights the parts of the datapath that are used for lw
  • Figures 4.39 and 4.40 show the completion of a sw instruction - here, we need to remember the value from the register file that is to be stored in memory. It must be passed along during the EX phase from the ID/EX registers to the EX/MEM registers - during MEM , we store the value at the location specified by the effective address, both coming from the EX/MEM pipeline registers - the WB stage does nothing for sw
  • Figure 4.43 shows a sequence of instructions in a pipeline, and Figure 4.45 shows the in- structions in execution at the fifth step of this execution sequence

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.

Dealing with Hazards

We noticed earlier that our pipelines cannot always operate at full capacity.

  • some instructions do not need to use all stages of the pipeline
  • instructions may depend on values computed in prior instructions that are still in the pipeline (data hazards)
  • instructions may begin executing before a previous jump or branch is taken (control hazards)

Data Hazards

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

  • $2 is computed by the first instruction and is used by the next 4.
  • The second and third instructions need the value of $2 before the first instruction has had a chance to store it.
  • The last two instructions are fine, remembering that values are written to the register file in the first half of the clock cycle, read in the second half.

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.

  • the value for $2 in the and instruction is in the EX/MEM pipeline registers
  • the value for $2 in the or instruction is in the MEM/WB pipeline registers

We need to detect when these conditions occur and account for them. There are two main cases to deal with:

  1. the destination register (Rd) of the instruction in the MEM phase is one of the source regis- ters (Rs or Rt) of the instruction in the EX phase
  2. the destination register (Rd) of the instruction in the WB phase is one of the source registers (Rs or Rt) of the instruction in the EX phase

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 PC is not updated
  • the IF/ID pipeline registers are not updated
  • the control entries in the ID/EX pipeline register are loaded with all 0’s, which results in the bubble/nop.

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.

Control Hazards

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 ):

  • The branch target adder is moved to ID.
  • An equality checker is added that compares the values coming out of the register file that can quickly determine the result of a beq (or bne).

Another technique that works well with short pipelines (such as our 5-stage pipeline) is the use of a branch delay slot.

  • Here, we always execute the instruction immediately following a branch or jump.
  • This way, a programmer or (hopefully) a compiler can reorder instructions so that some useful work can be done in the slot after the branch.
  • This is not always possible, so a nop instruction may need to be inserted in some cases.
  • However, it is a quite effective way to reduce the cost and frequency of control hazards. Real implementations of MIPS (among other ISAs) make use of branch delay slots.

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.