Salta al contenuto
Note per Studenti Pipelining and Hazards Report

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 3⋅1.0=3.03\cdot1.0=3.0 ns, but in steady state one instruction completes every 1.01.0 ns (1 GHz).

Numerical example

Delays (ns): register-file read 0.60.6, MUX B 0.20.2, function unit 0.80.8, MUX D 0.20.2, register-file write 0.60.6.

  • No pipeline: period 0.6+0.2+0.8+0.2+0.6=2.40.6+0.2+0.8+0.2+0.6=2.4 ns, i.e. 417417 MHz.
  • 3-stage pipeline (FF setup taken as 0, pipeline-register delay 0.20.2 ns): stage 1 (OF) 0.6+0.2=0.80.6+0.2=0.8 ns; stage 2 (EX) 0.2+0.8=1.00.2+0.8=1.0 ns; stage 3 (WB) 0.2+0.2+0.6=1.00.2+0.2+0.6=1.0 ns; the clock is limited by the slowest stage: T=1.0T=1.0 ns, 1 GHz.

The improvement is 2.4/1.0=2.42.4/1.0=2.4, not 3, because of the flip-flop delays and because the slowest stage sets the frequency. For NN instructions: 2.4N2.4N ns without pipeline, (3+N−1)⋅1.0(3+N-1)\cdot1.0 ns with it; for N=1000N=1000, 24002400 ns against 10021002 ns (speedup ≈2.4\approx2.4); for N=1N=1 the pipeline is slower (33 ns against 2.42.4).

Execution diagram

With kk 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 NN instructions). In the first k−1k-1 cycles not all stages are busy (the pipeline fills), likewise at the end (it empties). NN instructions need k+N−1k+N-1 cycles: 7 instructions on 4 stages take 1010 cycles. A single instruction on a 6-stage pipeline takes 66 cycles (the latency). In steady state one instruction completes per cycle, with at most kk 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

  1. R5←R1+R2R_5\leftarrow R_1+R_2
  2. R4←R3R_4\leftarrow R_3
  3. R6←R4+R5R_6\leftarrow R_4+R_5

Instruction 3 reads R4R_4 and R5R_5 in its DOF, at cycle 4. R5R_5 is written by instruction 1 at cycle 4, in the first half (reads occur in the second): available. R4R_4 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 R4R_4).

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 R4R_4 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 RWRW (no register write), BSBS (no jump) and MWMW (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 22 cycles per branch; with "not taken" prediction nothing is lost if the branch is not taken, otherwise the 22 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 (k+N−1k+N-1).

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 0.6+0.2+0.8+0.2+0.6=2.40.6+0.2+0.8+0.2+0.6=2.4 ns (417 MHz); 3 stages 0.80.8, 1.01.0, 1.01.0 ns give T=1.0T=1.0 ns (1 GHz), speedup about 2.42.4. N=1000N=1000: 24002400 ns against 10021002 ns; N=1N=1: 33 ns against 2.42.4 ns. Latency 3 stages of 1 ns =3=3 ns.
  • Cycles: k+N−1k+N-1 (7 instructions, 4 stages: 10; 1 instruction, 6 stages: 6). Steady state: one instruction per cycle, at most kk in flight.
  • Data hazard: operand read before written. R5←R1+R2R_5\leftarrow R_1+R_2; R4←R3R_4\leftarrow R_3; R6←R4+R5R_6\leftarrow R_4+R_5: hazard only at cycle 4 (R4R_4 written at cycle 5; R5R_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 RWRW, BSBS, MWMW. 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.

Teoria collegata