Beyond Five Stages: Deeper Pipelines and Superscalar Cores

The five-stage core finishes at most one instruction in a cycle. There are two ways to do better: make the cycle shorter, or put two instructions into it. Each way meets a wall.

You Will Be Able To

  • Work out the cycles and the time of a program on a deeper pipeline, and say why deeper is not always faster.
  • Decide which instructions a two-wide core can take in the same cycle.
  • Compute the instructions per cycle of a run, and name what holds it below the width.
Lesson
S2.15
Time
35 min
Runs on
Your browser
Check
6 questions

ap_rv32 takes one instruction in a cycle and finishes them in order. The fast cores in phones and servers do neither. This page takes the first step away from it.

Before You Start

You need the cost of a taken branch from S2.10: 2 cycles on ap_rv32.

You need the clock period as the longest path from S2.6, and forwarding from S2.8.

Five questions this page answers
Question
1What does a deeper pipeline gain, and what does it lose?
2What does superscalar mean?
3When can two instructions not go in the same cycle?
4What is IPC, and what is its upper limit on a two-wide core?
5Is ap_rv32 superscalar?

The Idea: Shorter Cycles, or More in Each Cycle

The time of a program is its cycles times the clock period. So there are two things to shrink.

First, the period. Cut each stage into smaller stages. Less logic lies between two registers, so the clock can be faster.

The number of stages is the pipeline depth. A deeper pipeline has a shorter period. But a branch is decided later in it.

So each taken branch throws away more instructions. The cycles go up while the period goes down.

Second, the cycles. Build the pipeline twice, side by side. Then two instructions can enter in the same cycle.

A core that can start more than one instruction in a cycle is superscalar.

How many it can start in one cycle is its issue width.

Its measure is IPC: instructions per cycle. A two-wide core reaches 2 at best. It rarely does.

The second instruction often needs the result of the first. That is a dependency. Then it must wait a cycle.

The real core of this track, ap_rv32, has five stages and takes one instruction in a cycle. What follows are models.

New Words

New words on this page
WordMeaning
Pipeline depthThe number of stages of a pipeline.
SuperscalarA core that can take more than one instruction in a cycle.
Issue widthThe largest number of instructions that a core can start in one cycle. A single-issue core starts one.
IPCInstructions per cycle: instructions executed, divided by cycles. The inverse of CPI.
DependencyOne instruction needs something from another: here, a register that the other one writes.
In-orderInstructions start and finish in the order of the program. ap_rv32 is in-order.
Gate levelOne logic gate on a path. Here the unit for the length of a path, and so for the period.

One Picture: Two Lanes

Two Lanes, and a Check Between ThemStep through the picture. It shows a core that takes two instructions in a cycle, and the check that sometimes sends the second one back.
  • Memory
  • Control / FSM
  • Processor
  • Bus
Two Lanes, and a Check Between ThemStep through the picture. It shows a core that takes two instructions in a cycle, and the check that sometimes sends the second one back. component Fetch two two instruction wordsin a cycle component Pair check may the second gowith the first? component Lane A the first instruction:decode, execute,write component Lane B the secondinstruction, beside it component Register file four read ports andtwo write ports component Wait the second startsthe next cycle, as afirst two instructionsthe first always goesthe second, if allowednot allowedreads and writesreads and writes
  1. component Fetch twotwo instruction words in a cycle
    • two instructions → Pair check
  2. component Pair checkmay the second go with the first?
    • the first always goes → Lane A
    • the second, if allowed → Lane B
    • not allowed → Wait
  3. component Lane Athe first instruction: decode, execute, write
    • reads and writes → Register file
  4. component Lane Bthe second instruction, beside it
    • reads and writes → Register file
  5. component Waitthe second starts the next cycle, as a first
  6. component Register filefour read ports and two write ports
  1. Two at a time

    The fetch brings two instruction words in each cycle. They are neighbours in the program.

  2. The first always goes

    The older of the two enters lane A. Nothing can stop it here.

  3. May the second go too?

    Only if it does not need the result of the first, and the first does not jump away.

  4. If not, it waits

    The second instruction starts one cycle later, as the first of the next pair. Order is never changed.

  5. Everything is doubled

    Two decoders, two ALUs, twice the ports on the register file. And forwarding between the lanes.

Read the diagram as text
  • Fetch two: two instruction words in a cycle
  • Pair check: may the second go with the first?
  • Lane A: the first instruction: decode, execute, write
  • Lane B: the second instruction, beside it
  • Register file: four read ports and two write ports
  • Wait: the second starts the next cycle, as a first
  • Fetch two → Pair check: two instructions
  • Pair check → Lane A: the first always goes
  • Pair check → Lane B: the second, if allowed
  • Pair check → Wait: not allowed
  • Lane A → Register file: reads and writes
  • Lane B → Register file: reads and writes

A superscalar core is two pipelines side by side, with a check that keeps them honest.

Three pipelines for the first idea
StagesLost for a taken branchClock period, in gate levelsWhere the numbers come from
52 cycles80ap_rv32: both are measured
84 cycles55a choice of this page
127 cycles40a choice of this page

More stages: a shorter period and a later decision. The period does not halve when the stages double: every stage adds its register.

The Smallest Example, Worked by Hand

The body of a loop: five instructions
AddressInstructionWritesReads
0clw x5, 0(x8)x5x8
10add x10, x10, x5x10x10, x5
14addi x8, x8, 4x8x8
18addi x9, x9, -1x9x9
1cbne x9, x0, 0x0cno registerx9

The loop adds words of memory. x8 points to the next word, x9 counts down, x10 is the sum.

Pair them for a two-wide core, in order. Start with the lw at 0c and the add at 10.

The add reads x5. The lw writes x5 in this same cycle. The value does not exist yet. So the lw goes alone.

Next cycle: the add at 10 and the addi at 14. The addi reads x8. The add writes x10. No clash: they go together.

Next: the addi at 18 and the bne at 1c. The bne reads x9, and the addi writes it. The addi goes alone.

Next: the bne is first. It is taken, so the instruction after it in memory is not the next one. It goes alone.

One round, by hand
CycleFirstSecondWhy
1lw x5nonethe add needs x5
2add x10addi x8nothing shared
3addi x9nonethe bne needs x9
4bnenoneit leaves the line

Five instructions in four cycles: an IPC of 1.25, on a core that could do 2.

The Rules

  1. Rule 1: Cycles on a pipeline: instructions, plus stages minus 1, plus the lost cycles of every taken branch.

    With 5 stages and 2 lost cycles this is the n plus 4 plus 2 of S2.10.

  2. Rule 2: Time is cycles times the clock period.

    A deeper pipeline wins only if its period falls by more than its cycles rise.

  3. Rule 3: A two-wide core takes two neighbours of the program in one cycle.

    It never changes their order.

  4. Rule 4: The second waits when it reads the register that the first writes.

    The value is not there yet. It goes one cycle later.

  5. Rule 5: The second waits when the first is a branch or a jump that leaves the line.

    The instruction that follows it in memory is not the next one of the program.

  6. Rule 6: IPC is instructions divided by cycles. It cannot be more than the issue width.

    Dependencies and branches keep it below.

Sources for These Rules

Checked in the library: Harris and Harris, Digital Design and Computer Architecture (ARM edition), sections 7.7.1 and 7.7.4. They have deep pipelines with diminishing returns, and a two-way superscalar processor limited by dependencies. Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.10, has multiple issue. Its section 4.11 describes a real eight-stage core that takes two instructions in a cycle, in order. Both books use other processors than ours. The counts and the cycles of ap_rv32 are measured on the real core. Its logic depths, 122 and 80 gate levels, are in docs/soc/ap_rv32-blueprint.md, sections 14.1 and 14.2. The pipelines of 8 and 12 stages and their periods are a choice of this page: no such core was built. The two-wide core is a model.

Now Run It

First the depth. Two programs of S2.11 ran on the real core: 283 instructions with 114 taken branches, and 332 with 109.

A sum, printed by a lab on Icarus. The two rows with 5 stages are ap_rv32: the real core took exactly 515 and 554 cycles. The other four rows are models.
loops |  5 stages | 2 lost |  515 cycles | period 80 | time 41200
loops |  8 stages | 4 lost |  746 cycles | period 55 | time 41030
loops | 12 stages | 7 lost | 1092 cycles | period 40 | time 43680
pattern |  5 stages | 2 lost |  554 cycles | period 80 | time 44320
pattern |  8 stages | 4 lost |  775 cycles | period 55 | time 42625
pattern | 12 stages | 7 lost | 1106 cycles | period 40 | time 44240

Rule 1 at work: the program loops on 8 stages takes 283 plus 7 plus 4 times 114: 746 cycles. On 5 stages: 515.

Rule 2 at work: 746 cycles at a period of 55 is 41030. That is a little less than the 41200 of 5 stages. Twelve stages lose: 43680.

The second program has fewer taken branches for its size. There the 8 stages win clearly, and 12 stages draw level with 5.

Now the width. The lab at the end of this page replays a loop of three rounds: the 19 instructions that the real core executed.

Printed by the lab on Icarus: a model of a two-wide core. The order of the instructions is the trace of the real core ap_rv32.
 1 | 00 lui x8, 0x80000 | 04 addi x9, x0, 3
 2 | 08 addi x10, x0, 0 | 0c lw x5, 0(x8)
 3 | 10 add x10, x10, x5 | 14 addi x8, x8, 4
 4 | 18 addi x9, x9, -1 | -- it reads x9 from the first
 5 | 1c bne x9, x0, 0x0c | -- the first leaves the line
 6 | 0c lw x5, 0(x8) | -- it reads x5 from the first
 7 | 10 add x10, x10, x5 | 14 addi x8, x8, 4
 8 | 18 addi x9, x9, -1 | -- it reads x9 from the first
 9 | 1c bne x9, x0, 0x0c | -- the first leaves the line
10 | 0c lw x5, 0(x8) | -- it reads x5 from the first
11 | 10 add x10, x10, x5 | 14 addi x8, x8, 4
12 | 18 addi x9, x9, -1 | -- it reads x9 from the first
13 | 1c bne x9, x0, 0x0c | 20 sw x10, 0(x8)
instructions=19 cycles=13 pairs=6

Rule 3 at work: row 1 takes the instructions at 00 and 04 together. Row 2 takes 08 and 0c.

Rule 4 at work: row 4. The bne would read x9, which the addi before it writes. The addi goes alone.

Rule 5 at work: row 5. The bne is taken and leaves the line. Row 6 starts at 0c, the top of the loop.

Rows 6 to 9 are the round that you worked by hand: four cycles. In row 13 the bne is not taken, so the sw joins it.

Rule 6 at work: 19 instructions in 13 cycles. That is an IPC of 1.46.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
More, shorter stagesStay at fiveA faster clock. More flushed instructions for a wrong guess, more registers, more forwarding paths.
Two lanesOne lane with a faster clockMore work in a cycle at the same clock. Two of everything, and a register file with twice the ports.
Keep program orderStart whatever is readySimple control. One slow instruction holds back all behind it. S2.31 gives the order up.
The second waits on a dependencyForward inside the cycleNo path through two ALUs in one cycle. The second place of that cycle stays empty.
A good branch predictorPredict not takenIt is what makes depth affordable. Tables of counters and targets, as in S2.11 and S2.27.

Cutting a path into stages does not divide it evenly. The single-cycle ap_rv32 has a longest path of 122 gate levels.

The five-stage ap_rv32 has 80, not 122 divided by 5. The longest stage sets the clock, and logic does not cut where one would like.

The Trap: Twice the Width, the Same Program, Fewer Pairs

Width is only an offer. Whether a program takes it depends on the order of its instructions.

Swap two rows in the body of the loop. Put the count, addi x9, right behind the lw. The result of the program is the same.

The same loop with the addi of the count moved up. A model of a two-wide core.
 1 | 00 lui x8, 0x80000 | 04 addi x9, x0, 3
 2 | 08 addi x10, x0, 0 | 0c lw x5, 0(x8)
 3 | 10 addi x9, x9, -1 | 14 add x10, x10, x5
 4 | 18 addi x8, x8, 4 | 1c bne x9, x0, 0x0c
 5 | 0c lw x5, 0(x8) | 10 addi x9, x9, -1
 6 | 14 add x10, x10, x5 | 18 addi x8, x8, 4
 7 | 1c bne x9, x0, 0x0c | -- the first leaves the line
 8 | 0c lw x5, 0(x8) | 10 addi x9, x9, -1
 9 | 14 add x10, x10, x5 | 18 addi x8, x8, 4
10 | 1c bne x9, x0, 0x0c | 20 sw x10, 0(x8)
instructions=19 cycles=10 pairs=9

Read rows 5 to 7. A round is now three cycles. The lw goes with the addi of x9, the add with the addi of x8, and the bne alone.

The whole run takes 10 cycles, not 13. Nine of them carry two instructions.

The real core shows the same effect in its own way. It ran the first order in 30 cycles and the second in 27.

Why? In the first order the add right behind the lw costs a load-use stall in every round. In the second order it does not.

So "a two-wide core" says little alone. Compilers order instructions for the core they aim at.

At Scale

ap_rv325 stages, 1 instruction in a cycle, in order
A wide out-of-order coremore than 12 stages, 4 or more in a cycle

This section looks ahead. Depth and width are the two dials of a core. Real cores turn both, and add machinery to keep them useful.

  1. A real two-wide core

    One widely used core has eight stages and takes two instructions in a cycle, in order. The textbook of Patterson and Hennessy describes it.

  2. Depth has come back down

    Some cores once had 20 to 31 stages to reach a high clock frequency. Power grows with the clock and with the registers, so newer ones are shallower.

  3. Prediction pays for depth

    A lost cycle is paid only at a wrong guess. A predictor that is right 19 times in 20 makes 7 lost cycles affordable. S2.11 to S2.28 build them.

  4. The next step

    An in-order core waits behind every slow instruction. S2.31 lets instructions overtake, and S2.32 puts the results back in order.

Try It

Run the lab. Find the rows where the second instruction waits, and read why.

The model has one switch, a localparam of one bit. Set it to 0 and run again. How many cycles now, and what is the IPC?

In load_program, set leaves[7] to 0 and run. Row 5 changes. The model now pairs across a taken branch: why is that pair wrong?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 1 | 00 lui x8, 0x80000 | 04 addi x9, x0, 3 2 | 08 addi x10, x0, 0 | 0c lw x5, 0(x8) 3 | 10 add x10, x10, x5 | 14 addi x8, x8, 4 4 | 18 addi x9, x9, -1 | -- it reads x9 from the first 5 | 1c bne x9, x0, 0x0c | -- the first leaves the line 6 | 0c lw x5, 0(x8) | -- it reads x5 from the first 7 | 10 add x10, x10, x5 | 14 addi x8, x8, 4 8 | 18 addi x9, x9, -1 | -- it reads x9 from the first 9 | 1c bne x9, x0, 0x0c | -- the first leaves the line 10 | 0c lw x5, 0(x8) | -- it reads x5 from the first 11 | 10 add x10, x10, x5 | 14 addi x8, x8, 4 12 | 18 addi x9, x9, -1 | -- it reads x9 from the first 13 | 1c bne x9, x0, 0x0c | 20 sw x10, 0(x8) instructions=19 cycles=13 pairs=6

Check Yourself

Predict the outputSeven instructions run on the two-wide model. The first four form a chain through x5. The last three are two loads of constants and an add of them. What does the last line say?
// Seven instructions, two in a cycle. What does the last line say?
// Test code. It replays the instructions that the real program executed, through a model of another kind of core.
// The real core ap_rv32 takes one instruction in a cycle and finishes them in program order. It has none of this logic.
module tb;
  // ---- The program. One row for each instruction: what it writes, what it reads, and its kind.
  localparam int ALU = 0, LOAD = 1, STORE = 2, BRANCH = 3, JUMP = 4, STOP = 5;
  localparam int ROWS = 8;
  int s_rd [0:7], s_rs1 [0:7], s_rs2 [0:7], s_kind [0:7], s_target [0:7];
  function automatic string tx(input int row);   // the text of a row
    case (row)
      0: return "addi x5, x0, 1";
      1: return "add x5, x5, x5";
      2: return "add x5, x5, x5";
      3: return "add x5, x5, x5";
      4: return "addi x6, x0, 2";
      5: return "addi x7, x0, 3";
      6: return "add x8, x6, x7";
      7: return "ebreak";
      default: return "?";
    endcase
  endfunction
  function automatic string ad(input int row);   // the address of a row, as two hex digits
    return $sformatf("%02h", 8'(4 * row));
  endfunction
  // The recorded run of the real program: the rows in the order they were executed. leaves[n] is 1 when the
  // instruction was a branch or a jump that did not go on to the next address.
  localparam int COUNT = 7;
  int seq [0:6];
  bit leaves [0:6];
  task automatic load_program;
    s_rd[0] = 5;  s_rs1[0] = 0;  s_rs2[0] = 0;  s_kind[0] = ALU;  s_target[0] = 'h00;
    s_rd[1] = 5;  s_rs1[1] = 5;  s_rs2[1] = 5;  s_kind[1] = ALU;  s_target[1] = 'h00;
    s_rd[2] = 5;  s_rs1[2] = 5;  s_rs2[2] = 5;  s_kind[2] = ALU;  s_target[2] = 'h00;
    s_rd[3] = 5;  s_rs1[3] = 5;  s_rs2[3] = 5;  s_kind[3] = ALU;  s_target[3] = 'h00;
    s_rd[4] = 6;  s_rs1[4] = 0;  s_rs2[4] = 0;  s_kind[4] = ALU;  s_target[4] = 'h00;
    s_rd[5] = 7;  s_rs1[5] = 0;  s_rs2[5] = 0;  s_kind[5] = ALU;  s_target[5] = 'h00;
    s_rd[6] = 8;  s_rs1[6] = 6;  s_rs2[6] = 7;  s_kind[6] = ALU;  s_target[6] = 'h00;
    s_rd[7] = 0;  s_rs1[7] = 0;  s_rs2[7] = 0;  s_kind[7] = STOP;  s_target[7] = 'h00;
    seq[0] = 0;  seq[1] = 1;  seq[2] = 2;  seq[3] = 3;  seq[4] = 4;  seq[5] = 5;  seq[6] = 6;
    leaves[0] = 0;  leaves[1] = 0;  leaves[2] = 0;  leaves[3] = 0;  leaves[4] = 0;  leaves[5] = 0;  leaves[6] = 0;
  endtask

  // ---- A core that takes two instructions in a cycle, in program order.
  // The second one waits for the next cycle in two cases. WIDE = 0 gives a core that takes one in a cycle.
  localparam bit WIDE = 1;
  int n, cycles, pairs, first, second;
  string why;
  initial begin
    load_program();
    n = 0;  cycles = 0;  pairs = 0;
    while (n < COUNT) begin
      cycles++;
      first = seq[n];  why = "";
      if (!WIDE) why = "one at a time";
      else if (n + 1 >= COUNT) why = "the program ends";
      else if (leaves[n]) why = "the first leaves the line";                 // the next instruction is somewhere else
      else begin
        second = seq[n + 1];
        if (s_rd[first] != 0 && (s_rs1[second] == s_rd[first] || s_rs2[second] == s_rd[first]))
          why = $sformatf("it reads x%0d from the first", s_rd[first]);       // the value does not exist yet
      end
      if (why == "") begin $display("%2d | %s %s | %s %s", cycles, ad(first), tx(first), ad(seq[n + 1]), tx(seq[n + 1]));  pairs++;  n += 2; end
      else begin $display("%2d | %s %s | -- %s", cycles, ad(first), tx(first), why);  n += 1; end
    end
    $display("instructions=%0d cycles=%0d pairs=%0d", COUNT, cycles, pairs);
  end
endmodule
ConceptWhat does a deeper pipeline gain, and what does it lose?
ConceptWhat does superscalar mean?
ConceptWhen must the second instruction of a pair wait?
ConceptA two-wide core executes 60 instructions in 40 cycles. What is its IPC, and what is the most it could reach?
ConceptIs ap_rv32 superscalar?

Common Mistakes

Wrong Expect a core with twice the stages to have twice the clock frequency.

Each stage adds a register, and logic does not cut evenly. ap_rv32 went from 122 gate levels to 80 with five stages.

Fix Measure the longest stage. It sets the period.

Wrong Multiply the speed of a core by its issue width.

Dependencies and taken branches leave places empty. The loop here reached 1.46 of 2.

Fix Count instructions and cycles for the real program.

Wrong Call ap_rv32 superscalar because it has a pipeline.

A pipeline overlaps instructions. It still starts one in each cycle.

Fix ap_rv32 is in-order and single-issue.

Remember

  • Time is cycles times period. A deeper pipeline shortens the period and raises the cost of each taken branch.
  • A superscalar core takes several instructions in a cycle. A two-wide in-order core takes two neighbours.
  • The second waits when it reads what the first writes, or when the first leaves the line.
  • IPC is instructions divided by cycles. ap_rv32 is single-issue and in-order: its IPC is below 1.

Practice

Report a problem with this page