Pipelining and Hazards
In questa pagina 3
In the single-cycle computer the clock period is the sum of the delays of all blocks (Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle ControlThe 16-bit instruction word has an opcode of 7 bits plus register fields (DR, SA, SB or an immediate OP) or a branch format with a 6-bit PC-relative signed offset; the instruction set is load/store with ADD, SUB, logic, shifts, LDI, ADI, LD, ST, BRZ, BRN and JMP. In the single-cycle computer (separate instruction and data memories) a combinational instruction decoder derives MB, MD, RW, MW, PL, JB, BC from bits 15, 14, 13 and 9 and FS from bits 12 to 9, and the clock period is the sum of the delays on the critical path (9.8 ns in the example). In the multi-cycle computer (one memory) an instruction register, internal registers R8 to R15 and a sequential decoder are needed: fetch (INF) plus one or more execution states.Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle Control →), so the maximum speed is low, and it drops further when the control-unit delay is added. But the processing has precise phases, which can be separated by registers and executed in different clock periods: this is the pipeline.
The idea
A pipeline is more like a production line: each phase is separate and several phases are active in parallel, each on a different instruction. Typical datapath phases: OF (operand fetch: register read), EX (execute: function unit), WB (write back: result written). With the control unit there are four stages: IF (instruction fetch), DOF (decode and operand fetch), EX, WB.
Definition (latency and throughput).
- Latency: total duration of one instruction. It does not decrease (it grows slightly) with a pipeline.
- Throughput: instructions completed per unit time. It increases by a factor close to the number of stages.
Example. 3 stages of 1.0 ns: latency ns, but in steady state one instruction completes every ns (1 GHz).
Numerical example
Delays (ns): register-file read , MUX B , function unit , MUX D , register-file write .
- No pipeline: period ns, i.e. MHz.
- 3-stage pipeline (FF setup taken as 0, pipeline-register delay ns): stage 1 (OF) ns; stage 2 (EX) ns; stage 3 (WB) ns; the clock is limited by the slowest stage: ns, 1 GHz.
The improvement is , not 3, because of the flip-flop delays and because the slowest stage sets the frequency. For instructions: ns without pipeline, ns with it; for , ns against ns (speedup ); for the pipeline is slower ( ns against ).
Execution diagram
With stages each instruction occupies one stage per cycle and the next starts one cycle later:
| cycle | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| instruction 1 | IF | DOF | EX | WB | ||
| instruction 2 | IF | DOF | EX | WB | ||
| instruction 3 | IF | DOF | EX | WB |
Formula (cycles for instructions). In the first cycles not all stages are busy (the pipeline fills), likewise at the end (it empties). instructions need cycles: 7 instructions on 4 stages take cycles. A single instruction on a 6-stage pipeline takes cycles (the latency). In steady state one instruction completes per cycle, with at most instructions in flight.
Hazards
Definition (hazard). A hazard is a problem, in data or control, that prevents the next instruction from executing normally in the next cycle.
- Data hazard: the next instruction needs the result of a previous one, or an operand, before it is available.
- Control hazard: the program jumps to a place other than the next one, but the pipeline has already loaded the following instructions.
Data hazard: example
4-stage pipeline IF, DOF, EX, WB, with
Instruction 3 reads and in its DOF, at cycle 4. is written by instruction 1 at cycle 4, in the first half (reads occur in the second): available. is written by instruction 2 only in its WB, at cycle 5: at cycle 4 the old value is read. So there is a data hazard only at cycle 4 (two instructions one position apart, dependence on ).
Property (remedies).
- NOPs inserted by the compiler: "no operation" instructions delay the second one.
- Hardware stall (bubblean empty slot inserted in the pipeline: nothing is written or executed): the hardware inserts NOPs and freezes IF/DOF until the data is ready. Logic compares source and destination registers of the instructions in flight (signal DHS, Data Hazard Stall); clock cycles are wasted.
- Forwarding: if the needed result is already computed (at the EX output) it is sent ahead directly to the stage that needs it, without waiting for the write: no cycle is lost.
Example. For a stall delays instruction 3 by one cycle (DOF at cycle 5); with forwarding no cycle is lost.
Control hazard and branch prediction
Property. In a branch the PC changes, but in the next two cycles the pipeline has already loaded instructions that should not be executed. Solutions: insert NOPs while waiting for the branch (two cycles lost) or predict it: assume the branch is not taken and go on; if it is taken, cancel the loaded instructions by disabling (no register write), (no jump) and (no memory write): this is a bubble. Branch prediction avoids (or reduces) the losses from control hazards, not data hazards, and does not raise the clock frequency.
Example. NOPs cost cycles per branch; with "not taken" prediction nothing is lost if the branch is not taken, otherwise the loaded instructions become bubbles.
Common mistakes
- Saying the pipeline shortens a single instruction: it shortens the average time per instruction, not the latency.
- Taking the speedup equal to the number of stages: FF delays and the slowest stage must be counted.
- Confusing data hazards (operand not ready) and control hazards (branch).
- Saying branch prediction removes data hazards.
- Forgetting filling and emptying in the cycle count ().
Versione ripasso
- Pipeline: stages separated by registers (IF, DOF, EX, WB) on different instructions at once. Latency unchanged; throughput grows by less than the number of stages (FF delays, slowest stage sets the clock).
- Numbers: no pipeline ns (417 MHz); 3 stages , , ns give ns (1 GHz), speedup about . : ns against ns; : ns against ns. Latency 3 stages of 1 ns ns.
- Cycles: (7 instructions, 4 stages: 10; 1 instruction, 6 stages: 6). Steady state: one instruction per cycle, at most in flight.
- Data hazard: operand read before written. ; ; : hazard only at cycle 4 ( written at cycle 5; written in the first half of cycle 4). Fixes: compiler NOPs, hardware stall (bubble, DHS), forwarding from the EX output (no cycle lost; stall costs one).
- Control hazard: after a branch the loaded instructions must not run. NOPs cost 2 cycles; predict "not taken" and, if taken, turn them into bubbles by disabling , , . Prediction does not cure data hazards or raise the clock.
- Mistakes: speedup equal to the stage count; latency reduced; prediction for data hazards; filling and emptying forgotten.