# CS152 Lecture 13: OoO 2 ### Section 1: Out-of-Order Design Variants and the Unified Physical Register File (UPRF) Let's step back and look at the entire spectrum of out-of-order microarchitectures. Any Tomasulo-style pipeline must track **five distinct activities** in hardware tables: 1. **Register rename** (mapping architectural registers to internal names). 2. **Post-dispatch, pre-issue operand staging** (holding ready/waiting operands before issue). 3. **Post-completion, pre-commit result staging** (holding speculative results before commit). 4. **Post-commit result staging** (the Architectural Register File, `ARF`). 5. **Program order for in-flight instructions** (needed if we split commit from completion). Different classic designs arise simply by choosing which tables to **fuse** and how to distribute them: - **Tomasulo's Original Design:** Tracks (1) in a rename table, (2) in **distributed per-FU reservation stations**, has no (3) and no (5) because there is no separate commit, and stores (4) in the ARF. - **Data-in-ROB Design:** Tracks (1) in a rename table, (4) in the ARF, and **fuses (2), (3), and (5) inside a centralized ROB**. - **Unified Physical Register File (UPRF) Design** (used in MIPS R10K, Alpha 21264, Intel Pentium 4, and Sandy/Ivy Bridge): Tracks (1) using a **Decode Stage Register Mapping** plus a **Committed Register Mapping**, tracks (5) in a **separate ROB** (which holds *no data!*), and **fuses (2), (3), and (4) inside one centralized Unified Physical Register File (`UPRF`)**. Why do all these variants exist? Because **bits per entry** and **number of entries** differ sharply across structures. For example, a ROB is usually several times larger than the issue window/reservation stations (because completed instructions sit in the ROB waiting for an older slow instruction to commit, while unissued instructions sit in the issue window). Separating data storage (`UPRF`) from ordering (`ROB`) and scheduling allows a smaller-area design and avoids copying data from the ROB into an ARF on commit! #### How the Unified Physical Register File (UPRF) Works In a UPRF machine: - During decode, all architectural registers are renamed into indices of a single large **Physical Register File** (`P0..Pn`) that holds *both* committed and speculative values—**no register values are read at decode**. - Functional units **read operands from the UPRF at issue** and **write results to the UPRF at completion**. - **Commit only updates the Committed Register Mapping** (pointing each architectural register to its committed physical register)—**zero data movement occurs at commit!** Wait: **when can we reuse a physical register?** Be very careful here! Consider this sequence renamed from architectural registers (`x1, x3, x6...`) to physical registers (`P1, P2, P3...`): - `ld x1, (x3)` $\to$ `ld P1, (Px)` - `addi x3, x1, #4` $\to$ `addi P2, P1, #4` - `sub x6, x7, x9` $\to$ `sub P3, Py, Pz` - `add x3, x3, x6` $\to$ `add P4, P2, P3` - `ld x6, (x1)` $\to$ `ld P5, (P1)` - `add x6, x6, x3` $\to$ `add P6, P5, P4` - `sd x6, (x1)` $\to$ `sd P6, (P1)` - `ld x6, (x11)` $\to$ `ld P7, (Pw)` You might think: *"We can free `P1` as soon as `ld P1, (Px)` commits!"* **That is completely wrong!** Look at `ld P5, (P1)` and `sd P6, (P1)`—those younger instructions are still reading `P1` even after `ld P1, (Px)` commits, and if a trap happens before `P1` is overwritten, architectural `x1` *is* `P1`! Instead, **`P1` stays live until the next writer of `x1` commits**! Only when the *next* instruction that writes `x1` commits is it guaranteed that no older or future instruction can ever need the old `P1` again. #### Worked Example: Physical Register Management Step-by-Step Let's trace a complete 5-instruction sequence through a UPRF machine with four structures: - **Rename Table** (`x0..x7`): initially `x1 -> P8`, `x3 -> P7`, `x6 -> P5`, `x7 -> P6`. - **Physical Regs** (`P0..Pn`): initially `P5: , p`, `P6: , p`, `P7: , p`, `P8: , p` (`p` = data present). - **Free List**: initially `[P0, P1, P3, P2, P4]`. - **ROB**: columns `use | ex | op | p1 | PR1 | p2 | PR2 | Rd | LPRd | PRd`. Notice `LPRd` (*Last Physical Register for destination*) records the *previous* physical register mapped to `Rd` before this instruction overwrote it! (Reading `LPRd` requires a third read port on the Rename Table for each instruction.) Let's dispatch all five instructions in order: 1. **`ld x1, 0(x3)`** - Source `x3` reads `P7` from Rename Table. Since `P7` is present in Physical Regs, `p1 = p`, `PR1 = P7`. - Dest `x1` reads old mapping `LPRd = P8`, pops `P0` from Free List (`PRd = P0`), and updates Rename Table `x1 -> P0`. - ROB row 1: `use=x, op=ld, p1=p, PR1=P7, Rd=x1, LPRd=P8, PRd=P0`. 2. **`addi x3, x1, #4`** - Source `x1` reads `P0` from Rename Table (`P0` is not ready yet, so `p1` is blank, `PR1 = P0`). - Dest `x3`: `LPRd = P7`, pops `P1` (`PRd = P1`), updates Rename Table `x3 -> P1`. - ROB row 2: `use=x, op=addi, p1=_, PR1=P0, Rd=x3, LPRd=P7, PRd=P1`. 3. **`sub x6, x7, x6`** - Sources `x7 -> P6` (`p1=p, PR1=P6`) and `x6 -> P5` (`p2=p, PR2=P5`). - Dest `x6`: `LPRd = P5`, pops `P3` (`PRd = P3`), updates Rename Table `x6 -> P3`. - ROB row 3: `use=x, op=sub, p1=p, PR1=P6, p2=p, PR2=P5, Rd=x6, LPRd=P5, PRd=P3`. 4. **`add x3, x3, x6`** - Sources `x3 -> P1` (not ready) and `x6 -> P3` (not ready). - Dest `x3`: `LPRd = P1`, pops `P2` (`PRd = P2`), updates Rename Table `x3 -> P2`. - ROB row 4: `use=x, op=add, p1=_, PR1=P1, p2=_, PR2=P3, Rd=x3, LPRd=P1, PRd=P2`. 5. **`ld x6, 0(x1)`** - Source `x1 -> P0` (not ready). - Dest `x6`: `LPRd = P3`, pops `P4` (`PRd = P4`), updates Rename Table `x6 -> P4`. - ROB row 5: `use=x, op=ld, p1=_, PR1=P0, Rd=x6, LPRd=P3, PRd=P4`. Now watch **Execute & Commit** of the first two instructions: - **Execute & Commit `ld x1, 0(x3)` (ROB row 1):** - Marks `ex = x`, writes `` into `P0` and sets `p` in `P0`. - Broadcasts tag `P0` to the ROB: both `addi` (row 2) and `ld` (row 5) match `PR1 == P0` and set their `p1 = p`! - On **commit** of row 1: hardware reads `LPRd = P8`, clears/invalidates `P8`, and pushes **`P8` onto the Free List**! (Because `ld x1` just committed `P0`, nobody can ever need `P8` again.) - **Execute & Commit `addi x3, x1, #4` (ROB row 2):** - Marks `ex = x`, writes `` into `P1` and sets `p` in `P1`. - Broadcasts `P1` to the ROB: `add` (row 4) matches `PR1 == P1` and sets `p1 = p`! - On **commit** of row 2: hardware reads `LPRd = P7`, invalidates `P7`, and pushes **`P7` onto the Free List**! #### Superscalar Register Renaming Real high-performance processors rename multiple instructions per cycle (for example, `Inst 1` and `Inst 2` simultaneously). Suppose we naively connect both `Inst 1` and `Inst 2` to the `Rename Table`'s read ports and write ports in the same cycle, assigning free tags to `Inst 1.PDest` and `Inst 2.PDest`. **Does this work?** **No!** Suppose `Inst 1` writes `x1` and `Inst 2` reads `x1` (`Src1` or `Src2`). Because both instructions look up the `Rename Table` in the *same* cycle before `Inst 1` has updated it, `Inst 2` will read the *stale* physical register for `x1` instead of `Inst 1`'s newly allocated `PDest`! To fix this, we add **intra-cycle RAW hazard checking** in parallel with the rename lookup: - Comparators (`=?`) check whether `Inst 1.Dest == Inst 2.Src1` and `Inst 1.Dest == Inst 2.Src2`. - Multiplexers at `Inst 2`'s `PSrc1` and `PSrc2` outputs select `Inst 1`'s new `PDest` (from `Free Tags`) whenever a match occurs, otherwise passing through the `Rename Table` read data. (Similarly, if both write the same `Dest`, `Inst 2`'s mapping must win when updating the table.) With this parallel bypass network, processors like the **MIPS R10K** rename **4 serially-RAW-dependent instructions per cycle**. *[Editor Note: removed title slide, previous-lecture recap slide, and course-specific homework label.]* #### ✎ Check Your Understanding 1. In a Unified Physical Register File (UPRF) processor, when an instruction commits, what happens to the physical register `PRd` that it wrote, and what happens to `LPRd` (the previous physical register for that architectural destination)? **Answer:** `PRd` becomes the new committed architectural register (via the committed mapping table, with no data copying), while **`LPRd` is freed and returned to the Free List**. 2. Why does a 2-wide superscalar renamer need equality comparators (`=?`) and multiplexers between `Inst 1` and `Inst 2`? **Answer:** If `Inst 2` reads a register written by `Inst 1` in the same cycle, reading the Rename Table directly would return `Inst 1`'s *old* mapping; the comparators detect the intra-cycle RAW dependency and mux in `Inst 1`'s newly allocated `PDest` instead. --- ### Section 2: Load-Store Queue Design and Speculative Memory Disambiguation We now know how to eliminate register name hazards and track register RAW hazards out of order. After control hazards (branch prediction), **data hazards through memory** are the next most important bottleneck to superscalar performance. Remember from Lecture 12: unlike register hazards, memory addresses (`sd x1, (x2)` followed by `ld x3, (x4)`) are not known at decode—we do not know whether `x2 == x4` until execution! #### The Speculative Store Buffer Just like register updates, **stores must not modify main memory or the L1 Data Cache until the store instruction commits** (otherwise, a mis-speculated or trapping store would permanently corrupt memory). To hold speculative stores safely, we introduce a **Speculative Store Buffer**: - **At decode:** A store buffer slot is allocated in program order, with fields `V` (Valid), `S` (Speculative), `Tag` (the store's physical memory address), and `Data`. - **Splitting stores:** Stores split into two micro-operations: **"store address"** execution (which computes the address and writes `Tag`) and **"store data"** execution (which writes `Data` once the value register is ready). - **On store commit:** When a store becomes the oldest instruction in the ROB and both its address and data are available, it commits by clearing its speculative bit (`S = 0`) and eventually draining its data along the **Store Commit Path** into the `L1 Data Cache` (`Tags | Data`). - **On store abort (trap or branch misprediction):** Simply clear the valid bit (`V = 0`). Now what happens when a younger **load** executes while older uncommitted stores sit in the Speculative Store Buffer? We perform **Load Bypassing (Store-to-Load Forwarding)**: - The `Load Address` searches **both** the `Speculative Store Buffer` and the `L1 Data Cache` in parallel. - **If the address hits in both the store buffer and the cache, which should we use?** The **Speculative Store Buffer**, because it holds the newer, uncommitted value not yet written to the cache. - **If the same address appears in the store buffer twice, which should we use?** The **youngest store older than the load** (the most recent write in program order prior to the load). #### When Can a Load Execute Out of Order? Consider the classic memory-disambiguation dilemma: ``` sd x1, (x2) ld x3, (x4) ``` Suppose `x4` is ready long before `x2` (for example, `x2` depends on an earlier slow computation). **When can we execute the load?** Modern superscalars choose among three progressively more aggressive strategies: 1. **Conservative Out-of-Order Load Execution** (e.g., **MIPS R10K**, 16-entry address queue): - Execute the load ahead of older stores **only if all previous store addresses are known** and `x4 != x2`. - Each load address is compared against the addresses of all previous uncommitted stores (sometimes using a **partial conservative check**, e.g., comparing only the bottom 12 bits of the address, to save comparator hardware—if those 12 bits differ, the full addresses definitely differ!). - **Do not execute the load if any previous store address is not yet known.** This is 100% safe (zero memory mis-speculation squashes), but stalls independent loads whenever *any* older store is slow to compute its address. 2. **Address Speculation:** - **Guess that `x4 != x2`** and execute the load immediately—even before older store addresses are known! - Hardware must buffer all completed-but-uncommitted load and store addresses in program order. - When an older store eventually computes its address `x2`, it searches younger completed loads: if we subsequently find **`x4 == x2`**, the load read stale data! Hardware must **squash the load and all following instructions** and re-execute. - **Trade-off:** Huge speedups when stores and loads rarely alias, but a **large penalty for inaccurate address speculation** when they do. 3. **Memory Dependence Prediction** (e.g., **Alpha 21264**): - Start optimistic: **guess `x4 != x2` and execute loads before older stores**. - If a load later turns out to have **`x4 == x2`**, squash the load and all following instructions—*but learn from the mistake* by marking that load instruction's PC as **`store-wait`** in a predictor table. - Subsequent executions of that same load instruction will **conservatively wait for all previous stores** to complete, avoiding repeated expensive squashes! - **Periodically clear `store-wait` bits** in case program behavior or pointer aliasing changes over time. Together—virtual memory with hardware TLBs and alias-free VIPT caches at the memory interface, paired with superscalar register renaming, a unified physical register file, an in-order ROB commit point, and speculative load-store disambiguation in the core—these mechanisms form the complete foundation of a modern high-performance processor. *[Editor Note: removed acknowledgements slide.]* #### ✎ Check Your Understanding 1. If a load's address matches two entries in the Speculative Store Buffer (one older than the load, one younger, or two older entries) as well as a line in the L1 Data Cache, which data value must the load use? **Answer:** It must use the data from the **Speculative Store Buffer**, specifically from the **youngest store that is still older than the load**. 2. How does the Alpha 21264's Memory Dependence Prediction improve upon pure Address Speculation? **Answer:** Like Address Speculation, it guesses loads don't alias unknown stores, but whenever a load *does* collide (`x4 == x2`) and causes a squash, it marks that load as **`store-wait`** so future executions wait for older stores instead of repeatedly mis-speculating (with `store-wait` bits cleared periodically).