Out-of-Order Execution and Register Renaming

A load is waiting for memory. Behind it stand instructions that need nothing from it. An in-order core makes them wait anyway. Letting them pass sounds easy, until two of them use the same register.

You Will Be Able To

  • Say what an out-of-order core gives up, and what it keeps.
  • Tell a dependency through a value from one that is only a shared name.
  • Rename a sequence of instructions by hand, with a rename table and a free list.
Lesson
S2.31
Time
35 min
Runs on
Your browser
Check
6 questions

S2.15 put two instructions into a cycle, in order. One slow instruction still stopped the line. Here the line is broken up.

Before You Start

You need the dependency from S2.15: an instruction reads a register that an earlier one writes.

You need forwarding from S2.8: a value can be used before it is in the register file.

Five questions this page answers
Question
1When may an instruction start on an out-of-order core?
2Which dependencies are real, and which come only from a shared register name?
3What does the rename table hold?
4In which order are the sources and the destination of an instruction renamed?
5Does ap_rv32 rename registers?

The Idea: Start When Ready, and Never Share a Place

An out-of-order core lets an instruction start as soon as its inputs are ready. Its place in the program does not hold it back.

What must be kept? Every instruction must still read the values that the program meant it to read.

One kind of dependency is about a value. An instruction reads what an earlier one wrote.

RAW is short for this kind: read after write.

A RAW dependency is real. The reader cannot start before the value exists. No trick removes it.

Two other kinds are about a name only. A later instruction writes a register that an earlier one still has to read.

WAR is short for that: write after read.

Or two instructions write the same register, and the later value must be the one that stays.

WAW is short for that: write after write.

WAR and WAW come from reuse. The program used x5 twice because registers are few. The two values have nothing to do with each other.

The cure is register renaming. The core has more registers than the instruction set names. Every new value gets a fresh one.

The real core of this track, ap_rv32, executes in order and renames nothing. What follows is a model.

New Words

New words on this page
WordMeaning
Out-of-order coreA core in which instructions start when their inputs are ready, and not in the order of the program.
RAWRead after write: an instruction reads a value that an earlier one writes. A real dependency.
WARWrite after read: an instruction writes a register that an earlier one must read first. A dependency of names.
WAWWrite after write: two instructions write the same register, and the later value must stay. A dependency of names.
Register renamingTo give every newly written value a register of its own, so that WAR and WAW disappear.
Architectural registerA register that the instruction set names: x0 to x31.
Physical registerA register that exists in the core. There are more of them than names. Written p0, p1 and so on.
Rename tableFor each architectural register, the physical register that holds its newest value.
Free listThe physical registers that hold no value anyone needs.

One Picture: Names and Places

A Table Turns Names into PlacesStep through the picture. It follows two instructions through the rename table: one that writes x5, and a later one that writes x5 again.
  • Processor
  • Control / FSM
  • Bus
  • Memory
A Table Turns Names Into PlacesStep through the picture. It follows two instructions through the rename table: one that writes x5, and a later one that writes x5 again. component Instruction names registers: x5,x8, x10 component Rename table for each name, theplace that holds itnow component Free list places nobody uses:p32, p33, p34 ... component Physicalregisters more places thannames component Execute whenready in any order 1: look up what itreads2: a new place forwhat it writesx5 is now herevalues, by place
  1. component Instructionnames registers: x5, x8, x10
    • 1: look up what it reads → Rename table
  2. component Rename tablefor each name, the place that holds it now
    • x5 is now here → Physical registers
  3. component Free listplaces nobody uses: p32, p33, p34 ...
    • 2: a new place for what it writes → Rename table
  4. component Physical registersmore places than names
    • values, by place → Execute when ready
  5. component Execute when readyin any order
  1. lw x5, 0(x8): what does it read?

    It reads x8. The table says: x8 is in p8. The instruction will read p8.

  2. lw x5: where does the result go?

    Not into the old place of x5. The free list hands out p32. The table now says: x5 is in p32.

  3. Later: addi x5, x10, 1

    It writes x5 again. The free list hands out p34. The table now says: x5 is in p34.

  4. Two values, two places

    The load result will arrive in p32. The addi result is in p34. Neither can overwrite the other.

  5. Now order does not matter

    The addi can finish long before the load. Every reader was told a place, and waits only for that place.

Read the diagram as text
  • Instruction: names registers: x5, x8, x10
  • Rename table: for each name, the place that holds it now
  • Free list: places nobody uses: p32, p33, p34 ...
  • Physical registers: more places than names
  • Execute when ready: in any order
  • Instruction → Rename table: 1: look up what it reads
  • Free list → Rename table: 2: a new place for what it writes
  • Rename table → Physical registers: x5 is now here
  • Physical registers → Execute when ready: values, by place

The instruction set has 32 names. The core has more places, and gives every new value a place of its own.

Three kinds of dependency
KindExampleWhat must holdRemoved by renaming?
RAWlw x5 ... then add x6, x5, x9The add reads x5 after the lw has written itno: the value is needed
WARadd x6, x5, x9 ... then addi x5, x10, 1The addi writes x5 after the add has read the old x5yes
WAWlw x5 ... then addi x5, x10, 1The value of the addi is the one that stays in x5yes

In an in-order pipeline WAR and WAW never cause trouble: the order of the program solves them.

The Smallest Example, Worked by Hand

Six instructions, two chains
AddressInstructionChain
14lw x5, 0(x8)A: load, add, store
18add x6, x5, x9A: load, add, store
1csw x6, 4(x8)A: load, add, store
20addi x5, x10, 1B: two adds, store
24add x7, x5, x5B: two adds, store
28sw x7, 8(x8)B: two adds, store

Chain A waits for a load. Chain B needs nothing from chain A. But both use the name x5.

Start with the table: x0 is in p0, x1 in p1, up to x31 in p31. The free list holds p32, p33 and so on.

The lw at 14 reads x8: p8. It writes x5. Take p32 from the free list. The table now says x5 is in p32.

The add at 18 reads x5 and x9. Look them up now: p32 and p9. It writes x6: p33.

The sw at 1c reads x8 and x6: p8 and p33. It writes no register, so it takes nothing from the free list.

The addi at 20 reads x10: p10. It writes x5. Take p34. The table now says x5 is in p34. The value in p32 is untouched.

The add at 24 reads x5 twice. The table says p34. So it reads the result of the addi, and not of the lw.

The rename table for x5, step by step
After the instruction atx5 is inWho still reads the old place
the startp5nobody in this example
14, lw x5p32
18, add x6, x5, x9p32this add reads p32
20, addi x5, x10, 1p34the add at 18 still reads p32, whenever it runs

After the addi, both values of x5 exist at once: the load result in p32 and the new one in p34.

The Rules

  1. Rule 1: An instruction may start when the values it reads are finished.

    Not earlier, whatever its place in the program. That is the RAW dependency.

  2. Rule 2: First look up the sources in the rename table.

    The instruction will read those physical registers, even if the table changes later.

  3. Rule 3: Then give the destination a new physical register from the free list.

    Write it into the table. Later readers of that name will find the new place.

  4. Rule 4: An instruction that writes no register takes none.

    A store or a branch only looks up its sources.

  5. Rule 5: After renaming, only RAW dependencies are left.

    No two instructions in flight write the same place, so WAR and WAW cannot happen.

  6. Rule 6: x0 is never renamed.

    It reads as 0 and a write to it is dropped, as before.

Sources for These Rules

Checked in the library: Harris and Harris, Digital Design and Computer Architecture (ARM edition), sections 7.7.5 and 7.7.6. They have out-of-order execution, the three kinds of dependency with the names RAW, WAR and WAW, and register renaming. Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.10, has dynamic scheduling, antidependences and renaming. Its section 4.11 describes a real core with a map from architectural to physical registers. Both books use other processors than ours. The six instructions are from a program that the real core ap_rv32 executed. ap_rv32 itself is in-order and renames nothing. The table, the free list and the cycle counts on this page are a model. A load of 3 cycles is a choice of the model.

Now Run It

The lab at the end of this page renames the six instructions. They are rows 6 to 11 of a program that the real core executed.

Printed by the lab on Icarus: a model. The six instructions, in this order, are from the trace of the real core ap_rv32.
14 | lw x5, 0(x8) | reads p8 | x5 -> p32 (was p5)
18 | add x6, x5, x9 | reads p32 p9 | x6 -> p33 (was p6)
1c | sw x6, 4(x8) | reads p8 p33 | writes no register
20 | addi x5, x10, 1 | reads p10 | x5 -> p34 (was p32)
24 | add x7, x5, x5 | reads p34 p34 | x7 -> p35 (was p7)
28 | sw x7, 8(x8) | reads p8 p35 | writes no register
without renaming: finished in cycle 3 4 5 4 5 6 | all done in cycle 6
with renaming:    finished in cycle 3 4 5 1 2 3 | all done in cycle 5

Rule 2 at work: the row at 18. It reads p32 and p9. The p32 is the place the lw was given one row earlier.

Rule 3 at work: the row at 20. x5 goes to p34, and the row says "was p32". The row at 24 reads p34 twice.

Rule 4 at work: the rows at 1c and 28 are stores. They read two places and write no register.

The last two lines ask when each of the six is finished. As many may run in a cycle as are ready. A load takes 3 cycles here.

Without renaming, the fourth instruction, the addi, is finished in cycle 4. It needs only x10. But it writes x5.

It must wait for the lw to write x5 first, and for the add at 18 to read it. Chain B stands behind chain A for nothing.

Rule 5 at work: with renaming, the addi is finished in cycle 1 and chain B in cycle 3. Both chains run side by side.

Chain A does not get faster. Its add is finished in cycle 4 both times. It waits for a value, and that is rule 1.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Start when the inputs are readyStart in program orderA slow instruction no longer blocks what does not need it. Logic that watches every waiting instruction.
Rename in program orderRename when an instruction startsEvery reader finds the right writer with one table lookup. Renaming is a stage of the pipeline.
More physical registers than namesStall on WAR and WAWIndependent chains overlap. A larger register file and a free list.
A new place for every writeRename only when a clash is seenNo clash detection at all. Places must be given back, which S2.32 does.
Sources first, then the destinationThe other orderadd x5, x5, x5 reads the old x5 and writes the new one. Nothing: the order is free.

Why can an in-order pipeline do without all this? Its instructions write in program order. A later write can never come before an earlier read.

The Trap: Expecting Renaming to Break a Real Chain

Renaming removes dependencies of names. It cannot remove a dependency of values.

Seven instructions. In the first four, each one reads the x5 that the one before it wrote. A model.
00 | addi x5, x0, 1 | reads nothing | x5 -> p32 (was p5)
04 | add x5, x5, x5 | reads p32 p32 | x5 -> p33 (was p32)
08 | add x5, x5, x5 | reads p33 p33 | x5 -> p34 (was p33)
0c | add x5, x5, x5 | reads p34 p34 | x5 -> p35 (was p34)
10 | addi x6, x0, 2 | reads nothing | x6 -> p36 (was p6)
14 | addi x7, x0, 3 | reads nothing | x7 -> p37 (was p7)
18 | add x8, x6, x7 | reads p36 p37 | x8 -> p38 (was p8)
without renaming: finished in cycle 1 2 3 4 1 1 2 | all done in cycle 4
with renaming:    finished in cycle 1 2 3 4 1 1 2 | all done in cycle 4

x5 is written four times and gets four places: p32 to p35. But every add reads the place of the add before it.

The last two lines are equal. With renaming and without, the chain is finished in cycle 4, one link in each cycle.

So an out-of-order core is not fast on every program. Code that is one long chain runs at the speed of the chain.

At Scale

This lab6 instructions, 4 new places
A wide out-of-order corehundreds of physical registers, several renamed in a cycle

This section looks ahead. Six instructions and a handful of places show the idea. A real core renames hundreds of instructions in flight.

  1. Several in one cycle

    A wide core renames a group of instructions together. The second must see the new place that the first has just been given. That is common practice.

  2. Places run out

    This lab never gives a place back. A real core frees the old place of a register when the newer write can no longer be undone. S2.32 shows when.

  3. The table must be repairable

    After a wrong branch guess, the table holds entries of instructions that should never have run. S2.32 puts it back.

  4. Asked in interviews

    The three names RAW, WAR and WAW, and which of them renaming removes, are a standard question about processors.

Try It

Run the lab. For each row, check the places it reads against the rows above it.

In load_program, change s_rd[8] from 5 to 12, and s_rs1[9] and s_rs2[9] from 5 to 12. Chain B now uses x12. What changes in the two last lines, and why?

Change s_kind[5] from LOAD to ALU. The load is now as fast as an add. How much does renaming still gain?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 14 | lw x5, 0(x8) | reads p8 | x5 -> p32 (was p5) 18 | add x6, x5, x9 | reads p32 p9 | x6 -> p33 (was p6) 1c | sw x6, 4(x8) | reads p8 p33 | writes no register 20 | addi x5, x10, 1 | reads p10 | x5 -> p34 (was p32) 24 | add x7, x5, x5 | reads p34 p34 | x7 -> p35 (was p7) 28 | sw x7, 8(x8) | reads p8 p35 | writes no register without renaming: finished in cycle 3 4 5 4 5 6 | all done in cycle 6 with renaming: finished in cycle 3 4 5 1 2 3 | all done in cycle 5

Check Yourself

Predict the outputSix instructions write x5 three times, each with a constant. The last one adds the third x5 and x6. Nothing is a load. What does the last line say?
// Six instructions that write x5 three times. Which physical register does the last add read?
// 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 = 7;
  int s_rd [0:6], s_rs1 [0:6], s_rs2 [0:6], s_kind [0:6], s_target [0:6];
  function automatic string tx(input int row);   // the text of a row
    case (row)
      0: return "addi x5, x0, 1";
      1: return "addi x6, x5, 1";
      2: return "addi x5, x0, 2";
      3: return "addi x7, x5, 1";
      4: return "addi x5, x0, 3";
      5: return "add x8, x5, x6";
      6: 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 = 6;
  int seq [0:5];
  bit leaves [0:5];
  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] = 6;  s_rs1[1] = 5;  s_rs2[1] = 0;  s_kind[1] = ALU;  s_target[1] = 'h00;
    s_rd[2] = 5;  s_rs1[2] = 0;  s_rs2[2] = 0;  s_kind[2] = ALU;  s_target[2] = 'h00;
    s_rd[3] = 7;  s_rs1[3] = 5;  s_rs2[3] = 0;  s_kind[3] = ALU;  s_target[3] = 'h00;
    s_rd[4] = 5;  s_rs1[4] = 0;  s_rs2[4] = 0;  s_kind[4] = ALU;  s_target[4] = 'h00;
    s_rd[5] = 8;  s_rs1[5] = 5;  s_rs2[5] = 6;  s_kind[5] = ALU;  s_target[5] = 'h00;
    s_rd[6] = 0;  s_rs1[6] = 0;  s_rs2[6] = 0;  s_kind[6] = STOP;  s_target[6] = 'h00;
    seq[0] = 0;  seq[1] = 1;  seq[2] = 2;  seq[3] = 3;  seq[4] = 4;  seq[5] = 5;
    leaves[0] = 0;  leaves[1] = 0;  leaves[2] = 0;  leaves[3] = 0;  leaves[4] = 0;  leaves[5] = 0;
  endtask

  // ---- The rename table: for each register of the instruction set, the physical register that holds it now.
  int map [0:31];          // x0 to x31 start in p0 to p31
  int next_free;           // the free physical registers are p32, p33, ... in this order
  int start [0:5], finish [0:5];
  int last_write [0:31];
  // When is each instruction finished? A load takes 3 cycles, everything else 1. An instruction starts when
  // the values it reads are finished. RENAME = 0: every register has one place only. Then an instruction that
  // writes x also waits for the writer of x before it, and for every earlier reader of x.
  task automatic timing(input bit RENAME);
    int row, lat, s, all;
    string text;
    for (int r = 0; r < 32; r++) last_write[r] = -1;
    text = "";  all = 0;
    for (int n = 0; n < COUNT; n++) begin
      row = seq[n];  lat = (s_kind[row] == LOAD) ? 3 : 1;  s = 1;
      if (s_rs1[row] != 0 && last_write[s_rs1[row]] >= 0 && finish[last_write[s_rs1[row]]] + 1 > s) s = finish[last_write[s_rs1[row]]] + 1;
      if (s_rs2[row] != 0 && last_write[s_rs2[row]] >= 0 && finish[last_write[s_rs2[row]]] + 1 > s) s = finish[last_write[s_rs2[row]]] + 1;
      if (!RENAME && s_rd[row] != 0) begin
        if (last_write[s_rd[row]] >= 0 && finish[last_write[s_rd[row]]] + 2 - lat > s) s = finish[last_write[s_rd[row]]] + 2 - lat;
        for (int j = last_write[s_rd[row]] + 1; j < n; j++)       // the readers of the old value
          if ((s_rs1[seq[j]] == s_rd[row] || s_rs2[seq[j]] == s_rd[row]) && start[j] - lat + 1 > s) s = start[j] - lat + 1;
      end
      start[n] = s;  finish[n] = s + lat - 1;
      if (s_rd[row] != 0) last_write[s_rd[row]] = n;
      text = {text, $sformatf(" %0d", finish[n])};
      if (finish[n] > all) all = finish[n];
    end
    $display("%s finished in cycle%s | all done in cycle %0d", RENAME ? "with renaming:   " : "without renaming:", text, all);
  endtask
  int row, old;
  string reads, writes;
  initial begin
    load_program();
    for (int r = 0; r < 32; r++) map[r] = r;
    next_free = 32;
    for (int n = 0; n < COUNT; n++) begin
      row = seq[n];
      reads = "";
      if (s_rs1[row] != 0) reads = {reads, $sformatf(" p%0d", map[s_rs1[row]])};       // look the sources up first
      if (s_rs2[row] != 0) reads = {reads, $sformatf(" p%0d", map[s_rs2[row]])};
      if (reads == "") reads = " nothing";
      if (s_rd[row] != 0) begin                                                        // then give the result a new place
        old = map[s_rd[row]];  map[s_rd[row]] = next_free;  next_free++;
        writes = $sformatf("x%0d -> p%0d (was p%0d)", s_rd[row], map[s_rd[row]], old);
      end
      else writes = "writes no register";
      $display("%s | %s | reads%s | %s", ad(row), tx(row), reads, writes);
    end
    timing(0);
    timing(1);
  end
endmodule
ConceptWhen may an instruction start on an out-of-order core?
ConceptWhich dependencies come only from a shared register name?
ConceptWhat does the rename table hold?
ConceptThe table says x5 is in p40. The free list starts with p41. How is sub x5, x5, x6 renamed, with x6 in p6?
ConceptDoes ap_rv32 rename registers?

Common Mistakes

Wrong Give the destination a new place first, and then look up the sources.

add x5, x5, x5 would read its own unwritten result.

Fix Look up the sources, then rename the destination.

Wrong Count WAR and WAW as reasons why code is slow.

They are names only. A core with renaming does not see them.

Fix Look for the RAW chains. They set the speed.

Wrong Say that ap_rv32 renames registers because it forwards.

Forwarding carries a value past the register file. The registers keep one place each.

Fix ap_rv32 is in-order and has no rename table.

Remember

  • An out-of-order core starts an instruction when its inputs are ready.
  • RAW is a dependency of values and stays. WAR and WAW are dependencies of names.
  • Renaming: look up the sources in the table, then give the destination a new physical register.
  • After renaming, independent chains overlap. A real chain is as slow as before.

Practice

Report a problem with this page