Search

Search papers and lessons

Register Renaming

Level 2 · 14 min read

Register renaming gives each new result its own hidden physical register, so instructions that only reuse a register name stop waiting for each other.

On this page

  • Colour = topic mix
  • Systems
  • Algorithms
  • Discrete math
  • ReadReady to readNot yet

TL;DR

A program names a small set of registers, such as r1 to r8, and reuses those names all the time. Reusing a name creates waits that are not about the data: a later write to r1 must not overtake an earlier read of r1. Register renaming gives every new result its own hidden physical register and keeps a table that maps each name to the latest one. Only real data dependences remain, so the processor is free to run independent work in parallel.

Builds on: Instruction Pipelining · Leads to: Out-of-Order Execution

Where it's used

Every modern out-of-order core. Desktop, server and high-performance phone cores rename registers. The rename step sits between decoding an instruction and sending it to the execution units of a pipeline, and it is the step that makes reordering safe.

The Pentium Pro, 1995. Intel's Pentium Pro brought out-of-order execution to the x86 family. It renamed registers through its reorder buffer, a queue that holds each result until it can be made permanent in program order. Renaming let a program's register names be reused freely inside the chip.

Compiled loops. Compilers have a limited number of registers, so the same temporary register is often reused on every loop iteration. Without renaming, each iteration would have to wait for the previous one to finish with that register. Renaming lets the iterations overlap, which is why the worked example below uses a loop.

How it works

The program has three kinds of conflict between instructions that use the same register name. A read after write (RAW) is a true dependence: an instruction needs a value that an earlier one produces. A write after read (WAR) and a write after write (WAW) are false dependences: they occur only because two instructions use the same name, not because one needs the other's data.

The names r1 to r8 are architectural registers, the names that the instruction set gives the program. The chip has more storage than that: its physical registers are real storage slots, numbered P1 and up, and the program never sees them. The chip keeps a map from each name to the physical register that holds its current value.

Take this four-instruction program, using the names r1 to r8. Each instruction is an add that takes one cycle.

  • I1: r1 ← r2 + r3
  • I2: r4 ← r1 + 1 (reads the r1 that I1 wrote)
  • I3: r1 ← r5 + r6 (writes r1 again)
  • I4: r7 ← r1 + r8 (reads the r1 that I3 wrote)
  1. Start the map. Each name r1 to r8 points to a physical register of the same number, P1 to P8. The free list holds P9, P10, P11 and so on. Check: eight names are mapped and the rest are free.
  2. Rename I1. I1 writes r1, so it takes the first free register, P9, and the map now says r1 → P9. Its sources r2 and r3 still map to P2 and P3. Check: I1 reads P2 and P3 and writes P9. The old register P1 is not freed yet. It is freed when I1 commits, as step 7 explains.
  3. Rename I2. I2 reads r1, which now maps to P9, so it depends on I1 through P9. Its result goes to a new register, P10, and the map says r4 → P10. Check: the only edge so far is I1 to I2, through P9, which is a true dependence.
  4. Rename I3. I3 writes r1 again. It takes a new register, P11, and the map says r1 → P11. It reads r5 and r6, which map to P5 and P6. Check: I3 writes P11, not P9, so it cannot clobber the value that I2 reads.
  5. Rename I4. I4 reads r1, which now maps to P11, so it depends on I3. Its result goes to P12, and r7 → P12. Check: I4 depends only on I3.
  6. Read the dependences. Without renaming, I3 would have to wait for I1 (WAW) and for I2 to read r1 (WAR). After renaming, I3 depends on nothing before it. The graph below shows the difference.
flowchart LR
  subgraph Before renaming
    a1[I1] -->|true r1| a2[I2]
    a1 -.->|false: write after write| a3[I3]
    a2 -.->|false: write after read| a3
    a3 -->|true r1| a4[I4]
  end
  subgraph After renaming
    b1[I1] -->|true, via P9| b2[I2]
    b3[I3] -->|true, via P11| b4[I4]
  end
  1. Commit in order. Committing an instruction makes its result permanent. The instructions still commit in program order. When I3 commits, it frees P9, the old value of r1. This is safe, because I2, which read P9, is older than I3 and has already committed. Check: no instruction can still need P9 at that point.

Only the solid edges remain as constraints. The false edges, drawn dotted, are gone, and I1 and I3 are free to run at the same time.

The math

Definition: Rename map and free list

A rename map MM sends each architectural register name aa to a physical register M[a]M[a]. A free list FF holds the physical registers not currently in use. Renaming an instruction that writes architectural register dd and reads s1,…,sks_1, \dots, s_k does three things: it reads the current physical sources M[s1],…,M[sk]M[s_1], \dots, M[s_k]; it takes a register pp from FF and sets M[d]=pM[d] = p; and it remembers the old mapping, so that poldp_{\text{old}} can be freed later. The architectural names are what the program sees. The physical registers are invisible to it.

Step 1 in symbols: the starting map

Before the program runs, M[a]=PaM[a] = P_a for each name aa from r1 to r8, and F={P9,P10,… }F = \{P_9, P_{10}, \dots\}. The number of physical registers is PP and the number of architectural names is AA, so FF starts with P−AP - A entries. In the example A=8A = 8 and the free list starts at P9P_9.

Steps 2 to 5 in symbols: the rename rule

For I1, the destination is r1 and the source map entries are M[r2]=P2M[r_2] = P_2 and M[r3]=P3M[r_3] = P_3. The new register is p=P9p = P_9, so M[r1]←P9M[r_1] \leftarrow P_9, and the old register P1P_1 is recorded. For I3, the new register is P11P_{11}, so M[r1]←P11M[r_1] \leftarrow P_{11}. I2 was renamed before I3, so it already holds P9P_9 as its source. I3 writes P11P_{11}, a different register, so P9P_9 is not overwritten while I2 still needs it.

Step 6 in symbols: the dependence test

Two instructions ii and jj with ii older must be ordered only if one reads a physical register that the other writes. After renaming, every physical register is written by exactly one instruction, so no two instructions write the same register. That removes WAW and WAR constraints. A RAW constraint remains: if an older instruction jj writes pp and a younger instruction ii reads pp, then ii must wait for jj.

Step 7 in symbols: when a register is freed

Let poldp_{\text{old}} be the register that the instruction kk displaced. It can be returned to FF when kk commits, because by then every older instruction that read poldp_{\text{old}} has committed as well. Renaming needs a free register for each new write, so the rename stage stalls when FF is empty.

A simple bound follows. At any moment, each of the AA architectural names has a current value in some physical register. Every other physical register is either free or holds a value that is waiting to be freed. So at most P−AP - A writes can be in flight at once. A design with 32 names and 96 physical registers can hold at most 96−32=6496 - 32 = 64 pending writes before the rename stage must wait.

Worked examples

Example
Tiny: two writes to the same name

Let r2 = 1, r3 = 2, r4 = 10 and r5 = 20. The program is:

  1. I1: r1 ← r2 + r3. The value is 1+2=31 + 2 = 3.
  2. I2: r1 ← r4 + r5. The value is 10+20=3010 + 20 = 30.

Without renaming, both results go to r1, so the second write must wait for the first, and the two adds finish one after the other. With renaming, I1 writes P9 and I2 writes P10. They have no data between them, so they can run in the same cycle.

  1. After both commit in program order, the map says r1 → P10, and r1 holds 30.
  2. Interpretation: the final value is 30, the same as an in-order machine gives, and renaming changed only when the work happens, not what the program computes.
Example
Standard: the four-instruction program, with cycle counts

Assume each add takes one cycle, the processor can issue two instructions per cycle, and a value is ready for use in the cycle after it is written. A write to a name may issue in the same cycle as the last read of the old value, because the new value lands a cycle later. The first value of r1 is needed by I2 and I4 in the program above.

Without renaming, I3 may not issue until I2 has read r1, because I3 overwrites it:

  1. Cycle 1: I1 issues. I2 needs r1, which is not ready. I3 must wait, because it would overwrite r1 before I2 reads the old value. Only I1 issues.
  2. Cycle 2: I2 issues, since r1 is ready, and reads r1. I3 issues in the same cycle, because its write lands only in cycle 3. I3 is ready in cycle 3.
  3. Cycle 3: I4 issues, because it needs the new r1 from I3. It is ready in cycle 4.
  4. The block finishes at cycle 4.

With renaming, the four instructions are in two independent pairs:

  1. Cycle 1: I1 and I3 both issue. They share no true dependence, so they run together.
  2. Cycle 2: I2 issues, because P9 is ready. I4 issues, because P11 is ready.
  3. The block finishes in cycle 3.

Interpretation: the same program takes 4 cycles without renaming and 3 with it. The saving comes entirely from removing the false edges. The true edges, I1 to I2 and I3 to I4, still set the minimum of two cycles of dependent work.

Example
Realistic: a loop that reuses a temporary register

A compiled loop computes sum ← sum + 2 × a[i] for each i, reading each a[i] from memory. The compiler uses one temporary register, t, for the loaded value on every iteration. The loop body is three instructions:

  • L: t ← load a[i] (takes 3 cycles)
  • M: t ← t × 2 (takes 1 cycle, reads t)
  • A: sum ← sum + t (takes 1 cycle, reads t)

Assume the index update is free, and that a load of a new value can start in the cycle after the previous load starts when renaming allows it. Run three iterations.

With renaming, each iteration gets its own physical register for t:

  1. Iteration 1: L starts at cycle 1 and is ready at cycle 4. M starts at 4 and is ready at 5. A starts at 5 and is ready at 6.
  2. Iteration 2: L starts at cycle 2, since it needs no register from iteration 1. It is ready at 5. M starts at 5 and is ready at 6. A starts at 6 and is ready at 7. The sum chain is fine: A needs the previous sum, which was ready at 6.
  3. Iteration 3: L starts at cycle 3, ready at 6. M starts at 6, ready at 7. A starts at 7, ready at 8.
  4. The three iterations finish at cycle 8.

Without renaming, each load must wait until the previous iteration has finished reading t. The same rule as in the standard example applies: a write may start in the cycle when the previous reader starts.

  1. Iteration 1 finishes its read of t at cycle 5, when A starts.
  2. Iteration 2's L may start at cycle 5, so it is ready at 8. Its M starts at 8 and is ready at 9. Its A starts at 9 and is ready at 10.
  3. Iteration 3's L must wait for A in iteration 2, which starts at 9. So L starts at 9, is ready at 12, M starts at 12 and is ready at 13, and A starts at 13 and is ready at 14.
  4. The three iterations finish at cycle 14.

Interpretation: renaming cuts the loop from 14 cycles to 8, with the same instructions and the same arithmetic. Each load is still 3 cycles long, but the loads now overlap.

Sizing check: with 32 architectural names and 96 physical registers, at most 64 writes can be pending. A loop that writes one register per iteration could therefore keep about 64 iterations in flight in principle, although the window, not the register file, usually limits the real number first.

Common mistakes and tradeoffs

It is tempting to think that renaming makes true dependences disappear. In fact a value that is needed before it is computed still forces a wait. A chain of loads, where each load's address is the result of the one before, runs at the speed of the chain whatever the renaming. Renaming removes only the false conflicts.

It is tempting to think that the compiler's register allocation already does this job. In fact the compiler has a fixed number of architectural registers, decides which values can share them with dataflow analysis, and so it creates the false conflicts in the first place. Hardware renaming works on the reused names at run time, with more physical registers than the program can name. A compiler can also give each value its own name at compile time, the idea behind static single assignment form. Most compiled code still reuses names, so the hardware cannot rely on the compiler having removed the conflicts.

It is tempting to think that renaming reorders instructions by itself. In fact it only makes reordering possible. The instructions still have to be sent to the execution units in a different order, which is the job of Out-of-Order Execution.

It is tempting to think that renaming costs nothing when a branch is guessed wrong. In fact the instructions after a wrong guess (see Branch Prediction) have already been renamed, so the rename map has to be restored to its state before the branch, and their physical registers returned to the free list. This is part of the cost of Speculative Execution.

It is tempting to think that more physical registers are always better. In fact each extra register costs area and power, and the register file grows harder to read as it gets larger. The free list also has to be large enough that the rename stage rarely runs dry.

Use renaming when a core runs general code with many writes to a few names and needs to overlap them. It is not worth the cost in a small in-order core, where the instructions issue in program order and no reordering takes place. Alternatives include asking the compiler to spread its use of registers over more names, which reduces the false conflicts without hardware, although only as far as the architectural register count allows.

History

Robert Tomasulo's 1967 design for the floating-point unit of the IBM System/360 Model 91 is the first widely known scheme that removed false register conflicts while running instructions out of order. Instead of a rename table, it tagged each pending value with the unit that would produce it, which gave the same effect: a later write never waited for an earlier read of the same name.

Explicit rename tables and physical register files became the usual design in the years after that, as processors grew wider and needed more instructions in flight. The Pentium Pro in 1995 brought the approach to x86, and it has been part of high-performance cores ever since. The idea caught on because it removes a whole class of stalls without changing the instruction set that programs see.

Recap

  • A program's reuse of register names creates false dependences, write after read and write after write, that are not about data.
  • Renaming gives each new result a fresh physical register and updates a map from names to registers.
  • After renaming, only true data dependences constrain the order of execution.
  • A register is freed when the instruction that displaced it commits, so older readers are finished first.
  • Renaming makes out-of-order execution safe, and it does nothing for true dependence chains by itself.

What to read next: Out-of-Order Execution, which uses renaming to issue instructions out of order, and Speculative Execution, which uses the same machinery to run past branches that are not yet decided.

Check yourself

QuestionRecall: name the two kinds of false dependence that renaming removes, and say which kind remains after renaming.Show answer

Write after read (WAR) and write after write (WAW) are the false dependences. Read after write (RAW), a true dependence where an instruction needs a value produced earlier, remains after renaming.

QuestionComputation: an architectural design has 32 register names and 128 physical registers. What is the most writes that can be pending at once, under the bound in this lesson?Show answer

The bound is P−A=128−32=96P - A = 128 - 32 = 96 pending writes. The design can hold at most 96 writes that are waiting for their old registers to be freed.

QuestionWhy: why does a register freed at commit, not at the last read, remain safe?Show answer

A register holds the old value of a name until the instruction that displaced it commits. Every older instruction that reads the old value has committed before that point, because commit happens in program order. So by the time the register is freed, no instruction that could still need its value remains.

QuestionApplied: a small embedded core issues instructions strictly in program order and has only a few registers. Should it add register renaming? Explain.Show answer

Probably not. Renaming helps only when instructions are reordered, so that a later independent instruction can run ahead of a waiting one. An in-order core never does that, so the renamed registers would sit idle while the core spent area and power on the rename table and free list.

Sources