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.
| Question | |
|---|---|
| 1 | What does a deeper pipeline gain, and what does it lose? |
| 2 | What does superscalar mean? |
| 3 | When can two instructions not go in the same cycle? |
| 4 | What is IPC, and what is its upper limit on a two-wide core? |
| 5 | Is 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
| Word | Meaning |
|---|---|
| Pipeline depth | The number of stages of a pipeline. |
| Superscalar | A core that can take more than one instruction in a cycle. |
| Issue width | The largest number of instructions that a core can start in one cycle. A single-issue core starts one. |
| IPC | Instructions per cycle: instructions executed, divided by cycles. The inverse of CPI. |
| Dependency | One instruction needs something from another: here, a register that the other one writes. |
| In-order | Instructions start and finish in the order of the program. ap_rv32 is in-order. |
| Gate level | One logic gate on a path. Here the unit for the length of a path, and so for the period. |
One Picture: Two Lanes
- Memory
- Control / FSM
- Processor
- Bus
- Two at a time
The fetch brings two instruction words in each cycle. They are neighbours in the program.
- The first always goes
The older of the two enters lane A. Nothing can stop it here.
- May the second go too?
Only if it does not need the result of the first, and the first does not jump away.
- If not, it waits
The second instruction starts one cycle later, as the first of the next pair. Order is never changed.
- 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.
| Stages | Lost for a taken branch | Clock period, in gate levels | Where the numbers come from |
|---|---|---|---|
| 5 | 2 cycles | 80 | ap_rv32: both are measured |
| 8 | 4 cycles | 55 | a choice of this page |
| 12 | 7 cycles | 40 | a 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
| Address | Instruction | Writes | Reads |
|---|---|---|---|
| 0c | lw x5, 0(x8) | x5 | x8 |
| 10 | add x10, x10, x5 | x10 | x10, x5 |
| 14 | addi x8, x8, 4 | x8 | x8 |
| 18 | addi x9, x9, -1 | x9 | x9 |
| 1c | bne x9, x0, 0x0c | no register | x9 |
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.
| Cycle | First | Second | Why |
|---|---|---|---|
| 1 | lw x5 | none | the add needs x5 |
| 2 | add x10 | addi x8 | nothing shared |
| 3 | addi x9 | none | the bne needs x9 |
| 4 | bne | none | it leaves the line |
Five instructions in four cycles: an IPC of 1.25, on a core that could do 2.
The Rules
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.
Rule 2: Time is cycles times the clock period.
A deeper pipeline wins only if its period falls by more than its cycles rise.
Rule 3: A two-wide core takes two neighbours of the program in one cycle.
It never changes their order.
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.
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.
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.
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 44240Rule 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.
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=6Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| More, shorter stages | Stay at five | A faster clock. More flushed instructions for a wrong guess, more registers, more forwarding paths. |
| Two lanes | One lane with a faster clock | More work in a cycle at the same clock. Two of everything, and a register file with twice the ports. |
| Keep program order | Start whatever is ready | Simple control. One slow instruction holds back all behind it. S2.31 gives the order up. |
| The second waits on a dependency | Forward inside the cycle | No path through two ALUs in one cycle. The second place of that cycle stays empty. |
| A good branch predictor | Predict not taken | It 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.
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=9Read 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
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.
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.
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.
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.
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?
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=6Check Yourself
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.