# CS152 Lecture 12: OoO 1 ### Section 1: Complex Pipelines, Hazard Taxonomy, and In-Order Limitations Now we shift our focus inside the processor core. Simple 5-stage pipelines assume every arithmetic operation completes in 1 cycle and memory takes 1 cycle. **Pipelining becomes complex when we want high performance in the presence of:** - Long-latency or partially pipelined floating-point units (FPUs), such as an unpipelined divider (`FDiv`), multi-cycle `FAdd` and `FMul`. - Memory systems with variable access time (cache hits vs. cache misses). - Multiple arithmetic and memory units operating in parallel. To talk cleanly about these machines, let's establish four precise pipeline boundaries across the **Frontend** (instruction fetch/decode) and **Backend** (execution, `"EXE stage"`): - **Dispatch:** Instructions are *dispatched* when they leave decode and enter the **instruction pool**. - **Issue:** Instructions are *issued* when they exit the instruction pool to a functional unit. - **Complete:** Instructions are *completed* when they finish execution and produce their result/exception flags at **Writeback**. - **Commit:** (We will separate this from completion shortly) when an instruction updates permanent architectural state. #### Why Simple In-Order Complex Pipelines Struggle Suppose we attach multiple functional units (`ALU`, `Mem`, `FAdd`, `FMul`, `FDiv`) to an `Issue` stage with general-purpose (`GPRs`) and floating-point (`FPRs`) register files. Four hazards immediately appear: 1. **Structural hazards on iterative functional units** (e.g., issuing a second divide while the unpipelined `FDiv` is busy). 2. **Structural hazards on writeback** (two instructions of different latencies finishing on the same cycle and fighting for one `WB` port). 3. **WAR and WAW data hazards** (explained below). 4. **Exception handling** (a fast instruction finishing after an older slow instruction that later traps). In an **in-order complex pipeline**, we can simplify control by padding shorter pipelines to **equalize EXE cycle latency** (putting `X1, X2, X3` stages on everything) and enforcing an in-order **Commit Point** right before `W`. That eliminates WB structural hazards, WAR/WAW hazards, and imprecise exceptions. To prevent the padded writeback latency from slowing down single-cycle integer operations, we add massive **bypassing** networks—but **bypassing becomes extremely expensive with multiple padded functional units**. Can we remove the padding and let functional units run at their natural, unequal latencies without massive bypassing? If we do, we must check four conditions before issuing any instruction from a tracking table: 1. Is the required functional unit available? (Structural) 2. Is the input data available? (**RAW** hazard?) 3. Is it safe to write the destination? (**WAR**? **WAW** hazard?) 4. Is there a structural conflict at the `WB` stage? #### Review: Data Hazard Taxonomy (Register vs. Memory) Consider executing a sequence of instructions of the form $r_k \leftarrow r_i \text{ op } r_j$: 1. **Data-dependence — Read-after-Write (RAW) hazard** (a *true* dependence): $$r_3 \leftarrow r_1 \text{ op } r_2$$ $$r_5 \leftarrow r_3 \text{ op } r_4$$ The second instruction needs the value produced by the first. 2. **Anti-dependence — Write-after-Read (WAR) hazard** (a *name* dependence): $$r_3 \leftarrow r_1 \text{ op } r_2$$ $$r_1 \leftarrow r_4 \text{ op } r_5$$ The second instruction overwrites $r_1$; if it writes before the first reads $r_1$, the first reads the wrong value. 3. **Output-dependence — Write-after-Write (WAW) hazard** (a *name* dependence): $$r_3 \leftarrow r_1 \text{ op } r_2$$ $$r_3 \leftarrow r_6 \text{ op } r_7$$ Both write $r_3$; if the second finishes before the first, $r_3$ ends up with the older value instead of the newer one. Notice a crucial difference between register and memory operands: **data hazards due to register operands can be determined at the decode stage** (just by comparing register numbers), **but data hazards due to memory operands (`M[r1 + disp1] <- r2` followed by `r3 <- M[r4 + disp2]`) can be determined only after computing the effective address**—specifically, checking whether $(r_1 + \text{disp}_1) = (r_4 + \text{disp}_2)$. (We will tackle memory hazards in Lecture 13.) #### Worked Example: Dependency Graph and Out-of-Order Completion Let's trace all hazards in the following 6-instruction floating-point sequence: - $I_1$: `FDIV.D f6, f6, f4` (latency $4$) - $I_2$: `FLD f2, 45(x3)` (latency $1$) - $I_3$: `FMUL.D f0, f2, f4` (latency $3$) - $I_4$: `FDIV.D f8, f6, f2` (latency $4$) - $I_5$: `FSUB.D f10, f0, f6` (latency $1$) - $I_6$: `FADD.D f6, f8, f2` (latency $1$) Let's map every hazard: - **RAW hazards:** $I_1 \to I_4$ (`f6`), $I_1 \to I_5$ (`f6`), $I_2 \to I_3$ (`f2`), $I_2 \to I_4$ (`f2`), $I_2 \to I_6$ (`f2`), $I_3 \to I_5$ (`f0`), $I_4 \to I_6$ (`f8`). - **WAR hazards:** $I_4 \to I_6$ (`f6` is read by $I_4$ and written by $I_6$), $I_5 \to I_6$ (`f6` is read by $I_5$ and written by $I_6$). - **WAW hazards:** $I_1 \to I_6$ (both write `f6`). From this dependency graph, valid instruction orderings include in-order ($I_1, I_2, I_3, I_4, I_5, I_6$), or out-of-order schedules such as $I_2, I_1, I_3, I_4, I_5, I_6$ (swapping independent $I_1, I_2$) or $I_1, I_2, I_3, I_5, I_4, I_6$ (moving $I_5$ ahead of $I_4$ because $I_5$ doesn't depend on $I_4$). Now let's keep **in-order issue** (instructions issue one per cycle in program order) and compare **in-order completion** against **out-of-order completion** cycle-by-cycle (writing `k` for the cycle $I_k$ issues, and $\underline{k}$ for the cycle $I_k$ completes): - **In-Order Issue, In-Order Completion:** - Cycle 1: `1` ($I_1$ issues, takes 4 cycles $\to$ ready Cycle 5) - Cycle 2: `2` ($I_2$ issues, takes 1 cycle $\to$ ready Cycle 3, *must wait* to complete after $I_1$) - Cycles 3–4: stall ($I_3$ needs `f2` from $I_2$, which hasn't completed) - Cycle 5: $\underline{1}$ ($I_1$ completes) - Cycle 6: $\underline{2}$ ($I_2$ completes) - Cycle 7: `3` ($I_3$ issues, takes 3 cycles $\to$ ready Cycle 10) - Cycle 8: `4` ($I_4$ issues; `f6` and `f2` are ready! Takes 4 cycles $\to$ ready Cycle 12) - Cycle 9: stall ($I_5$ needs `f0` from $I_3$) - Cycle 10: $\underline{3}$ ($I_3$ completes) - Cycle 11: `5` ($I_5$ issues, takes 1 cycle $\to$ ready Cycle 12, *must wait* for $I_4$) - Cycle 12: $\underline{4}$ ($I_4$ completes) - Cycle 13: `6` ($I_6$ issues, takes 1 cycle $\to$ ready Cycle 14) - Cycle 14: $\underline{5}$ ($I_5$ completes) - Cycle 15: $\underline{6}$ ($I_6$ completes) — **15 cycles total.** - **In-Order Issue, Out-of-Order Completion:** - Cycle 1: `1` - Cycle 2: `2` - Cycle 3: $\underline{2}$ ($I_2$ completes immediately out of order!) - Cycle 4: `3` ($I_3$ issues $\to$ ready Cycle 7) - Cycle 5: $\underline{1}$ ($I_1$ completes) - Cycle 6: `4` ($I_4$ issues $\to$ ready Cycle 10) - Cycle 7: $\underline{3}$ ($I_3$ completes) - Cycle 8: `5` ($I_5$ issues $\to$ ready Cycle 9) - Cycle 9: $\underline{5}$ ($I_5$ completes ahead of $I_4$!) - Cycle 10: $\underline{4}$ ($I_4$ completes) - Cycle 11: `6` ($I_6$ issues) - Cycle 12: $\underline{6}$ ($I_6$ completes) — **12 cycles total.** #### Where In-Order Issue Still Fails Out-of-order completion helped above, but look what happens with a slightly different sequence when `FLD` misses in the cache (latency = `long`, say 7 cycles): - $1$: `FLD f2, 34(x2)` (latency $1$) - $2$: `FLD f4, 45(x3)` (latency `long`) - $3$: `FMULT.D f6, f4, f2` (latency $3$, RAW on $1, 2$) - $4$: `FSUB.D f8, f2, f2` (latency $1$, RAW on $1$) - $5$: `FDIV.D f4, f2, f8` (latency $4$, RAW on $1, 4$; **WAR on $3$ (`f4`)**) - $6$: `FADD.D f10, f6, f4` (latency $1$, RAW on $3, 5$) Under **in-order issue**: `1 2,1_ . . . . . . 2_ 3 4 4_ 3_ 5 . . . 5_ 6 6_` (20 cycles). Notice that on Cycle 3, instruction $4$ (`FSUB.D f8, f2, f2`) already has its operand `f2` ready! Yet **the in-order issue restriction prevents instruction $4$ from being issued** simply because instruction $3$ ahead of it is stuck waiting on the long load ($2$). To hide long-latency operations, we must allow independent younger instructions to issue out of order. *[Editor Note: removed title slide, previous-lecture recap slide, and roadmap slide.]* #### ✎ Check Your Understanding 1. What are the three types of data hazards, and which one represents a *true* dependency as opposed to a *name* dependency? **Answer:** **RAW** (Read-after-Write, true data-dependence), **WAR** (Write-after-Read, anti-dependence), and **WAW** (Write-after-Write, output-dependence). Only **RAW** is a true dependency; WAR and WAW are name dependencies. 2. In the second 6-instruction trace above, why did instruction $4$ (`FSUB.D f8, f2, f2`) sit idle for 8 cycles under in-order issue even though `f2` became available on Cycle 2? **Answer:** Because instruction $3$ (`FMULT.D`) was stalled waiting on the long-latency load ($2$), and in-order issue forbids issuing instruction $4$ before all preceding instructions have issued. --- ### Section 2: Out-of-Order Issue and Why Register Renaming Is Essential To break the bottleneck of in-order issue, we place an **Instruction Pool** between `Dispatch` and `Issue`: - The pool holds multiple instructions waiting to issue. - **Dispatch** adds the next instruction in program order to the pool if there is space and the instruction does not cause a **WAR** or **WAW** hazard. *(Note: WAR becomes possible again! Under in-order issue, if an instruction registered its input operands at its functional unit upon issue, no younger instruction could write before the older instruction read. With out-of-order issue, a younger instruction in the pool could issue and overwrite a register before an older waiting instruction reads it!)* - **Issue** selects *any* instruction in the pool whose **RAW** hazards are satisfied (for now, at most one issue per cycle). On writeback (`WB`), waiting instructions are enabled. Let's apply naive **Out-of-Order Issue (without renaming)** to our exact 6-instruction example with the long load ($2$): - Cycle 1: `1` ($I_1$ issues) - Cycle 2: `2`, $\underline{1}$ ($I_2$ issues; $I_1$ completes) - Cycle 3: `4` ($I_4$ issues out of order ahead of $I_3$!) - Cycle 4: $\underline{4}$ ($I_4$ completes!) - Cycles 5–8: `. . . .` — **Stuck!** Why can't instruction $5$ (`FDIV.D f4, f2, f8`) issue even though `f2` and `f8` are both ready? Because instruction $3$ (`FMULT.D f6, f4, f2`) has not read `f4` yet, creating a **WAR hazard on `f4`**! - Cycle 9: $\underline{2}$ ($I_2$ completes) - Cycle 10: `3` ($I_3$ issues) - Cycle 13: $\underline{3}$ ($I_3$ completes) - Cycle 14: `5` ($I_5$ issues) - Cycle 18: $\underline{5}$ ($I_5$ completes) - Cycle 19: `6` ($I_6$ issues) - Cycle 20: $\underline{6}$ ($I_6$ completes). Look at that result: **Out-of-order execution did not allow any significant improvement!** It still took 20 cycles! And this is not a contrived example—it is **typical in loops**. Consider: ```c while (...) { A[i] += x; } ``` Compiled into assembly: ``` L1: FLD f4, 0(x3) ADDD f6, f4, f2 FST 0(x3), f6 ADD x3, x3, 8 j L1 ``` Every single iteration reuses architectural registers `f4` and `f6`. Even if the first `FLD` misses in cache, the second iteration cannot start its `FLD` into `f4` because of WAR/WAW hazards on `f4` and `f6`! Thus, **the number of registers in an ISA limits the number of instructions that can be in the pipeline**, and **out-of-order issue by itself does not provide any significant performance improvement!** #### Register Renaming to the Rescue Floating-point pipelines often cannot be kept filled with a small number of architectural registers (for instance, the IBM 360 had only 4 floating-point registers). Can a microarchitecture use *more* registers than specified by the ISA without breaking ISA compatibility? Yes! Robert Tomasulo of IBM suggested an ingenious solution in 1967 using on-the-fly **register renaming**. Any anti-dependence (WAR) or output-dependence (WAW) can be eliminated by giving the new write a **fresh storage location (name)**. Look at what happens to our 6-instruction example when hardware renames `f4` in instructions $5$ and $6$ to a fresh internal register `f4'`: - $3$: `FMULT.D f6, f4, f2` - $5$: `FDIV.D f4', f2, f8` - $6$: `FADD.D f10, f6, f4'` The WAR edge between $3$ and $5$ disappears! Now watch the execution schedule: - Cycle 1: `1` - Cycle 2: `2`, $\underline{1}$ - Cycle 3: `4` - Cycle 4: $\underline{4}$ - Cycle 5: `5` (**$I_5$ issues right away**, overlapping its 4-cycle divide with the long load $I_2$!) - Cycle 9: $\underline{2}$ ($I_2$ completes) - Cycle 10: `3`, $\underline{5}$ ($I_3$ issues and $I_5$ finishes!) - Cycle 11: $\underline{3}$ ($I_3$ completes; compressed in diagram notation) - Cycle 12: `6` ($I_6$ issues) - Cycle 13: $\underline{6}$ ($I_6$ completes) — **down from 20 cycles to ~13 cycles!** In the pipeline, **Decode performs register renaming and adds instructions to the issue-stage Reorder Buffer (ROB)** (our instruction pool). Because every write gets a unique renamed destination, **renaming makes WAR and WAW hazards impossible**. Any instruction in the ROB whose **RAW** hazards are satisfied can be issued immediately, yielding true **out-of-order (dataflow) execution**. #### ✎ Check Your Understanding 1. Why did naive out-of-order issue fail to speed up the 6-instruction sequence when `FLD f4` had a long latency? **Answer:** Instruction $5$ (`FDIV.D f4, f2, f8`) had a **WAR hazard** on `f4` with the stalled instruction $3$ (`FMULT.D f6, f4, f2`), blocking $5$ and $6$ from executing ahead of $3$. 2. Which two hazard types are completely eliminated by register renaming, and which one remains? **Answer:** **WAR** and **WAW** hazards are completely eliminated; only **RAW** hazards (true data dependences) remain. --- ### Section 3: Data-in-ROB Microarchitecture and Separating Completion from Commit How do we build register renaming and out-of-order execution in hardware? Let's examine the **"Data-in-ROB" design** first. #### Renaming Structures (Data-in-ROB) There are two central tables: 1. **Renaming Table & Architectural Register File (Regfile):** One entry per architectural register (`f1`, `f2`, ...), containing a presence bit `p` and a `data / tag` field: - If `p = 1` (present), the architectural register holds the valid, up-to-date data value. - If `p = 0` (busy), the field holds a **tag** ($t_i$) pointing to the ROB slot of the instruction that will produce that register's value. 2. **Reorder Buffer (ROB):** A FIFO queue of instruction templates with slots indexed by tags $t_1, t_2, \dots, t_n$ (`Head` = oldest, `Tail` = newest). Each ROB entry tracks: - `i` (issued bit), `Opcode` (`op`) - Source 1: `p1` (presence bit), `Tag / Src1` (either the ready value if `p1=1`, or producer tag $t_k$ if `p1=0`) - Source 2: `p2`, `Tag / Src2` - Destination: `p_dst` (whether result is ready), `Reg` (architectural destination register), `Result` (holds completed value before commit), and `Except?` (exception flags). Let's trace the four lifecycle events of an instruction in this machine: 1. **Dispatch (at Decode, in program order):** - Allocate the next ROB slot at `Tail` (its index $t$ is this instruction's **tag**). - Read source operands from the architectural regfile/rename table. If an operand is non-busy (`p=1`), copy its value to `Src1`/`Src2` with `p=1`. If it is busy (`p=0`), read `` from the producing ROB slot at that tag index (bypassing if just completed); if still not ready, copy the producer's tag $t_k$ into `Src` and set `p=0`. - Write the destination architectural register entry in the rename table with `` so all *future* readers point to this instruction's tag. 2. **Issue (out of order):** - Whenever an entry has `i=0` and both `p1=1` and `p2=1` (and its FU is free), set `i=1` and dispatch the operation and values to the Functional Unit. 3. **Completion (out of order):** - The FU broadcasts `` onto a common bus. - The ROB writes `Result` and `Except?` into slot `t` and sets `p_dst = 1`. - Simultaneously, **all waiting `Src1` and `Src2` fields in the ROB are searched associatively for tag `t`**; on a match, the hardware sets `p=1` and copies `result` into `Src`. *(Note: replacing the tag by its value via associative CAM broadcast across every ROB source slot is an expensive hardware operation!)* 4. **Commit (in order, at `Head`):** - Wait until the oldest instruction at `Head` has `p_dst = 1`. - Check `Except?`. If no trap, copy `Result` into the architectural register file: if the rename table's tag for `Reg` still matches `Head` (no younger writer overwrote the mapping), set `<1, _, Result>`; otherwise (a younger instruction already renamed `Reg`), update the committed value while leaving the rename table's busy tag intact (`<0, _, Result>`). Then dequeue `Head`. - **When can a tag/name be reused?** Only **whenever an instruction commits** (freeing its ROB slot). #### Why Separate Completion from Commit? In early out-of-order machines (like Tomasulo's original IBM 360/91), **completion equaled commit**: as soon as an instruction finished executing, it immediately updated architectural registers. That creates two severe problems: 1. **WAW hazards** if a younger instruction writes before an older one if not carefully gated. 2. **Imprecise traps/exceptions:** If instruction $I_4$ finishes and overwrites `f8` *before* older instruction $I_1$ finishes and discovers a divide-by-zero or page fault, the architectural state is already corrupted when the trap fires! Imprecise traps make debugging and OS virtual-memory restarts nearly impossible. By **separating completion from commit**, the ROB holds completed values temporarily (**out-of-order completion**) and updates architectural state strictly in program order (**in-order commit**). Look at how a snapshot of the ROB moves from `Cycle t` to `Cycle t + 1`: - At `Cycle t`, the ROB holds active decoded-but-uncommitted instructions from oldest (`ld x1, (x3)` at `Head`) down to `add x6, x6, x3`. Younger instructions (`sd x6, (x1)`, `ld x6, (x1)`) are still being fetched. - At `Cycle t + 1`, `ld x1, (x3)` **commits** (leaving the ROB), the middle instructions continue to **execute** (and dependents can bypass completed values straight out of the ROB before commit), and the newer instructions are **fetched** and enter the bottom of the ROB. When a **Trap** is detected at the **Commit** point: - Hardware **kills** all younger instructions in the ROB, Decode, and Fetch, resets the Rename Table (`All p=1`), sets `free = oldest`, and **injects the trap handler PC** into Fetch. Because no younger instruction has touched architectural state yet, the trap is 100% **precise**. What if we need to flush only *part* of the ROB without taking a full trap—such as on a **branch misprediction**? Repairing the rename table on a partial squash is non-trivial because every enqueued instruction after the branch modified the rename table. Two solutions exist: - **Rename unwind:** Walk backward from the tail of the ROB to the branch, restoring old mappings. - **Rename table snapshots:** Save a copy of the rename table per branch (or per instruction) so it can be restored in a single cycle on misprediction. #### Historical Perspective: Why OoO Faded Until the 1990s Tomasulo's 1967 IBM 360/91 FPU pioneered distributed reservation stations (3 entries for the Adder, 2 for the Multiplier, 6 load buffers, 3 store buffers) connected by a **Common Data Bus (``)** that broadcast results to all waiting entries simultaneously. Yet despite being built commercially in the 1960s, out-of-order execution largely disappeared until the 1990s for two architectural and two economic reasons: - **Two missing architectural pieces:** **Precise traps** (without a ROB, early OoO machines had imprecise traps) and **branch prediction** (without accurate speculation, control hazards starved the instruction window of ILP). - **Technology scaling:** Simpler in-order designs in newer semiconductor technology beat complicated multi-board OoO machines in older technology—fitting the processor and caches onto a single chip gave microprocessors an era of ~1%/week performance scaling before OoO became necessary again. *[Editor Note: removed blank transition slide and acknowledgements slide.]* #### ✎ Check Your Understanding 1. In a Data-in-ROB machine, when are source tags replaced by actual data values, and when can an instruction's ROB tag/slot be reused? **Answer:** Source tags are replaced by data **whenever a functional unit produces data** (via associative broadcast on completion). A ROB tag/slot can only be reused **whenever that instruction commits** in order at the ROB head. 2. Why did separating *completion* from *commit* solve the problem of imprecise traps? **Answer:** Out-of-order completion writes results and exception flags only into temporary ROB buffers; architectural state is updated strictly in program order at commit, so if an older instruction traps, all younger instructions can be cleanly killed before they alter architectural state.