# CS152 Lecture 17: VLIW ### Section 1: Superscalar Scaling Limits and the VLIW Philosophy We have built a remarkably capable machine: virtual memory with hardware TLBs, register renaming, out-of-order issue, speculative load-store disambiguation, and tournament/trace-cache branch speculation. Now step back and ask a brutal hardware engineering question: **How does this out-of-order control logic scale as we make the processor wider?** Suppose our processor has **Issue Width $W$** (issuing $W$ instructions per cycle) and each instruction has an active **Lifetime $L$** cycles in the machine: - Every newly issued instruction in the $W$-wide **Issue Group** must check dependencies against all **$W \times L$ previously issued instructions** still active in the machine. - Therefore, the dependency-checking and wakeup hardware grows proportionally to: $$\text{Control Hardware Complexity} \propto W \times (W \times L) = W^2 \cdot L$$ Let's unpack this scaling law: - In an **in-order** machine, $L$ reflects fixed pipeline execution latencies, and checks happen at issue via interlocks or a scoreboard. - In an **out-of-order** machine, $L$ *also* includes the entire time instructions wait inside the instruction window and ROB, and checks require **broadcasting tags to all waiting instructions** at completion! - Worse still: as we increase issue width $W$, we *must* use a **larger instruction window** to find enough independent instructions to keep all $W$ functional units busy—which increases lifetime $L$! Because $L$ grows along with $W$, **out-of-order control logic grows faster than $W^2$—roughly as $\sim W^3$!** Look at the die photo of the **MIPS R10000 (1995)**: a huge portion of the silicon area (`IF inst`, `IF addr`, `Free List`, `Grad Unit`, `Address Queue`, `Integer Queue`, `FP Queue`, `Register Rename`) is pure **Control Logic** dedicated to figuring out what can run in parallel—not caches (`Instruction Cache`, `Data Cache`) and not actual compute (`Integer Datapath`, `FP Datapath`, `FP Mult`)! And look at the absurdity of the **Sequential ISA Bottleneck**: 1. The programmer writes sequential source code (`a = foo(b); for (i=0; ...)`). 2. The **superscalar compiler** analyzes the code, builds a dependency graph, finds independent operations, and schedules them—only to flatten them back into a **sequential machine code** stream! 3. At runtime, the **superscalar processor** must burn huge hardware power and $\sim W^3$ area to **re-check instruction dependencies** and reconstruct the exact parallel schedule the compiler already knew! #### The VLIW (Very Long Instruction Word) Solution Why not let the compiler tell the hardware *explicitly* which operations execute in parallel? That is **VLIW**—also called **SPIE (Static Parallel Instruction Execution)** by pioneer Joseph A. Fisher (who also coined *"memory disambiguation"* for the unknown memory address problem). In a VLIW architecture: - **Multiple operations are packed into one very long instruction word**—for example, a 6-slot instruction: $$[\ \text{Int Op 1}\ |\ \text{Int Op 2}\ |\ \text{Mem Op 1}\ |\ \text{Mem Op 2}\ |\ \text{FP Op 1}\ |\ \text{FP Op 2}\ ]$$ - **Each operation slot maps to a fixed functional unit** (e.g., two 1-cycle Integer Units, two 3-cycle Load/Store Units, two 4-cycle Floating-Point Units). - **Constant operation latencies are specified in the ISA.** - **The architecture guarantees two rules by compiler contract:** 1. **Parallelism within an instruction** $\implies$ **no cross-operation RAW checks** needed in hardware. 2. **No data use before data is ready** $\implies$ **no hardware data interlocks or scoreboards needed**! All of that $\sim W^3$ issue queues, CAM wakeup logic, and register renaming disappears, replaced by the **VLIW compiler**, which must: 1. Schedule operations to maximize parallel execution. 2. Guarantee intra-instruction parallelism. 3. Schedule around known latencies to avoid data hazards—typically inserting explicit **NOPs** wherever data isn't ready yet. Historical milestones showed how wide VLIWs could go: the **FPS AP120B (1976)** was the first commercial wide-instruction array processor (relying on hand-coded software pipelining and loop unrolling); the **Multiflow Trace (1987)** packed up to **28 operations into a 1024-bit instruction word** using trace scheduling; and the **Cydrome Cydra-5 (1987)** encoded **7 operations into a 256-bit word** with a rotating register file. *[Editor Note: removed title slide, previous-lecture recap slide, and Fisher's personal naming/grant-proposal anecdote.]* #### ✎ Check Your Understanding 1. Why does out-of-order superscalar control logic grow faster than $W^2$ (closer to $\sim W^3$) as issue width $W$ increases? **Answer:** Each of the $W$ issued instructions must check against $W \times L$ in-flight instructions ($\propto W^2 L$), and wider machines require deeper instruction windows to uncover enough ILP, causing lifetime $L$ to grow alongside $W$. 2. What two architectural guarantees must a VLIW compiler satisfy so hardware can eliminate dependency-checking and interlock logic? **Answer:** All operations packed into the same VLIW instruction must be mutually independent (no cross-op RAW hazards), and no instruction may read an operand before its producer's fixed architectural latency has elapsed. --- ### Section 2: Static Loop Scheduling — Loop Unrolling, Software Pipelining, and Trace Scheduling Since a VLIW processor has no dynamic hardware scheduler, all performance comes from the **compiler's static scheduling**. Let's trace a concrete loop step-by-step on our 6-slot VLIW machine (`Int1`, `Int2`, `M1`, `M2`, `FP+`, `FPx`), assuming **Int = 1 cycle**, **Mem (`fld`/`fsd`) = 3 cycles**, and **FP (`fadd`) = 4 cycles**. Consider: ```c for (i = 0; i < N; i++) B[i] = A[i] + C; ``` Compiled into scalar-style assembly (`f0` holds `C`, `x1` points to `A`, `x2` points to `B`, `x3` is the loop bound): ``` loop: fld f1, 0(x1) add x1, 8 fadd f2, f0, f1 fsd f2, 0(x2) add x2, 8 bne x1, x3, loop ``` #### Attempt 1: Naive VLIW Scheduling Look at what happens when we schedule one iteration directly onto the 6-slot VLIW grid: - **Cycle 1:** `Int1: add x1`, `M1: fld` (`fld` takes 3 cycles, so `f1` is ready at Cycle 4). - **Cycles 2–3:** **All 6 slots are empty (NOPs)!** Nothing else is ready. - **Cycle 4:** `FP+: fadd` (`fadd` takes 4 cycles, so `f2` is ready at Cycle 8). - **Cycles 5–7:** **All 6 slots are empty (NOPs)!** - **Cycle 8:** `Int1: add x2`, `Int2: bne`, `M1: fsd`. Throughput: **1 `fadd` / 8 cycles = $0.125$ FLOPS/cycle**! Out of $8 \times 6 = 48$ available operation slots, we used only 6! #### Attempt 2: Loop Unrolling (4 Ways) To fill those empty latency cycles, the compiler **unrolls the inner loop to perform 4 iterations at once** (`i += 4`, handling non-multiples of 4 with a small cleanup loop): - **Cycle 1:** `M1: fld f1` - **Cycle 2:** `M1: fld f2` - **Cycle 3:** `M1: fld f3` - **Cycle 4:** `Int1: add x1`, `M1: fld f4`, `FP+: fadd f5` (`f1` just arrived after 3 cycles!) - **Cycle 5:** `FP+: fadd f6` - **Cycle 6:** `FP+: fadd f7` - **Cycle 7:** `FP+: fadd f8` - **Cycle 8:** `M1: fsd f5` (`f5` just arrived after 4 cycles!) - **Cycle 9:** `M1: fsd f6` - **Cycle 10:** `M1: fsd f7` - **Cycle 11:** `Int1: add x2`, `Int2: bne`, `M1: fsd f8` Throughput jumps to **4 `fadds` / 11 cycles = $0.36$ FLOPS/cycle**. Better—but notice why we are still wasting 7 of the 11 cycles on `FP+`: we wait for all 4 loads and adds to finish before starting the next batch of 4 loads! #### Attempt 3: Software Pipelining Why wait to finish iteration $k$'s stores before starting iteration $k+1$'s loads? **Software pipelining** overlaps operations from *different* loop iterations so the `M1`, `M2`, and `FP+` units stay busy simultaneously! Look at our 4-way unrolled loop after software pipelining: - **Prolog (8 cycles):** Fills the pipeline—cycles 1–4 issue `fld f1..f4` for batch 0; cycles 5–8 compute `fadd f5..f8` for batch 0 while simultaneously loading `fld f1..f4` for batch 1! - **Steady-State `loop:` (4 cycles!):** Every single cycle of the steady-state loop runs **three batches at once**: - Cycle 1 of loop: `M1: fld f1` (batch $k+2$) | `M2: fsd f5` (batch $k$) | `FP+: fadd f5` (batch $k+1$) - Cycle 2 of loop: `M1: fld f2` | `M2: fsd f6` | `FP+: fadd f6` - Cycle 3 of loop: `Int2: add x2` | `M1: fld f3` | `M2: fsd f7` | `FP+: fadd f7` - Cycle 4 of loop: `Int1: add x1` | `Int2: bne` | `M1: fld f4` | `M2: fsd f8` | `FP+: fadd f8` - **Epilog (5 cycles):** Drains the final `fadd`s and `fsd`s after the loop exits. Look at the steady-state throughput: **4 `fadds` / 4 cycles = $1.0$ FLOPS/cycle**—an **$8\times$ speedup** over naive scheduling, keeping `FP+` 100% utilized! Let's see how a compiler writes a basic software-pipelined loop for the simpler 1-per-iteration case (`fld f0, 0(x1)` $\to$ `fadd f4, f0, f2` $\to$ `fsd f4, 0(x1)` $\to$ `addi x1, x1, -8` $\to$ `bne`): - **Startup:** Run the front of the first two iterations (`fld` iter 0, `fadd` iter 0, `fld` iter 1, `addi x1, x1, -16`) to prime the registers. - **Main Loop (`Loop'`):** Each iteration executes three *different* logical iterations in one pass—**store iter $i$** (`fsd f4, 16(x1)`), **add iter $i+1$** (`fadd f4, f0, f2`), and **load iter $i+2$** (`fld f0, 0(x1)`), followed by `addi` and `bne`. (Notice `fsd` comes *before* `fadd` and `fld` in the loop body so it stores the old `f4` before `fadd` overwrites `f4`, and `fadd` reads the old `f0` before `fld` overwrites `f0`!) - **Winddown:** Finish the leftover tail (`fsd` iter $N-1$, `fadd` iter $N$, `fsd` iter $N$). Compare **Software Pipelining vs. Loop Unrolling**: Loop unrolling pays startup and wind-down ramp overhead on *every* unrolled iteration (sawtooth utilization), whereas **software pipelining pays startup/wind-down costs only once per loop**, keeping execution flat at peak throughput! #### What If There Are No Loops? Trace Scheduling Real programs aren't just dense numerical loops; control-flow-intensive irregular code has frequent branches that shrink each **basic block** to only a handful of instructions, leaving little ILP inside any single block. To extract parallelism across branches in VLIW, **Trace Scheduling** (Fisher & Ellis) uses **profiling feedback** or compiler heuristics to pick a string of basic blocks—a **trace**—representing the **most frequent branch path** through the control-flow graph. The compiler treats the entire trace as one giant basic block and schedules all of its operations in parallel at once! Whenever an operation moves above or below a side branch, the compiler generates extra **fixup (compensation) code** along the off-trace branch paths to preserve correctness if execution ever leaves the trace. #### ✎ Check Your Understanding 1. In the steady-state software-pipelined `Loop'` above (`fsd f4, 16(x1)`, `fadd f4, f0, f2`, `fld f0, 0(x1)`), why does `fsd` appear *before* `fadd` and `fadd` *before* `fld`? **Answer:** Because `fsd` must read the previous iteration's sum in `f4` before `fadd` overwrites `f4`, and `fadd` must read the previous iteration's loaded value in `f0` before `fld` overwrites `f0`—each instruction belongs to a different logical loop iteration ($i$, $i+1$, $i+2$). 2. Why does Trace Scheduling require "fixup code" on branches that exit the trace? **Answer:** Because scheduling the trace as one block moves instructions across branch boundaries; if runtime execution takes an off-trace branch, fixup code must execute any skipped operations or undo speculative side effects. --- ### Section 3: Classic VLIW Pitfalls, EPIC (IA-64), Rotating Registers, and Speculation Despite its hardware simplicity, **"Classic" VLIW** suffered five major practical problems: 1. **Object-code compatibility:** Because hardware latencies and functional unit counts are baked into the instruction encoding, you must **recompile all code for every new machine**, even two models in the same processor generation! 2. **Object code size:** Empty NOP slots waste instruction memory and I-cache capacity, while loop unrolling and software pipelining replicate code. *(Three encoding schemes tried to reduce unused fields: storing a compressed format in memory and expanding on I-cache refill as in the **Multiflow Trace**—which complicates branch target addressing; marking explicit parallel groups as in **TI TMS320C6x DSPs** and **Intel IA-64**; or providing a single-op VLIW instruction like **Cydra-5 UniOp**.)* 3. **Variable-latency memory operations:** Caches and memory bank conflicts introduce latency variability that the compiler cannot predict statically. 4. **Knowing branch probabilities:** Trace scheduling requires profiling runs as a heavy extra step in the software build process. 5. **Statically unpredictable branches:** The optimal schedule changes depending on which branch path is taken at runtime. #### Intel Itanium and EPIC (IA-64) To bring VLIW into mainstream enterprise computing while fixing object-code compatibility, Intel and HP introduced **EPIC** (**Explicitly Parallel Instruction Computing**—essentially VLIW) with the **IA-64** ISA (implemented in Merced, McKinley, and up to the 8-core, 2-way multithreaded **Poulson** fetching two 128-bit bundles / 6 instructions per cycle and executing up to 12 instructions/cycle). How did IA-64 achieve **object-code compatibility**? Instead of locking operations to fixed machine slots, IA-64 packs three 41-bit instructions plus a 5-bit **Template** into a **128-bit instruction bundle**: $$[\ \text{Instruction 2}\ |\ \text{Instruction 1}\ |\ \text{Instruction 0}\ |\ \text{Template}\ ]$$ The **Template bits** encode stop bits marking **groups** of mutually independent instructions (which can span across bundle boundaries, e.g., `group i` spanning `bundle j-1`, `bundle j`, and `bundle j+1`). A narrower machine executes a large group over multiple cycles, while a wider machine executes it in one cycle—preserving binary compatibility! IA-64 also provided massive register state: **128 64-bit GPRs**, **128 64/80-bit FPRs**, and **64 1-bit Predicate Registers**. #### Three Hardware Features to Help the Compiler To overcome VLIW's loop-size, branch, and memory-latency barriers, Cydra-5 and IA-64 introduced three clever architectural features: 1. **Rotating Register Files (for Software Pipelining):** Scheduled software-pipelined loops consume lots of registers and duplicate prolog/epilog code. **Solution:** Add a **Rotating Register Base (`RRB`)** register that points to the base of the current iteration's register set: $$\text{Physical Register} = \text{Logical Register Specifier} + \text{RRB}$$ On each loop branch (`bloop`), `RRB` decrements by 1, automatically allocating a fresh set of physical registers for the new iteration without duplicating code! (Usually the register file is split into rotating and non-rotating registers.) **Worked Example:** Suppose a loop has a **3-cycle load** and a **4-cycle `fadd`**. Look at how the compiler encodes latencies *directly in the register specifier differences*: $$\texttt{ld f1, ()} \quad|\quad \texttt{fadd f5, f4, ...} \quad|\quad \texttt{sd f9, ()} \quad|\quad \texttt{bloop}$$ Notice `f4 - f1 = 3` (load latency) and `f9 - f5 = 4` (`fadd` latency)! Let's trace `RRB` counting down from `8` to `1`: - Iteration at `RRB = 8`: `ld` writes `P(1+8) = P9`. (`fadd` reads `P12`, writes `P13`; `sd` reads `P17`.) - Three iterations later (`RRB = 5`): `ld` writes `P6`, and **`fadd` reads `P(4+5) = P9`**—receiving the exact value `ld` started loading 3 cycles earlier! Meanwhile, `fadd` writes `P(5+5) = P10`. - Four iterations later (`RRB = 1`): **`sd` reads `P(9+1) = P10`**—storing the exact sum `fadd` started computing 4 cycles earlier! 2. **Predicated Execution (for Hard-to-Predict Branches):** To eliminate mispredicted branches, almost every IA-64 instruction can execute conditionally under a 1-bit predicate register (becoming a `NOP` if its predicate is false). An `if-then-else` diamond spanning **four basic blocks** (`b0: br a==b, b2`, `b1: else`, `b2: then`, `b3: join`) collapses into **one basic block**: ``` Inst 1 Inst 2 p1, p2 <- cmp(a == b) (p1) Inst 3 || (p2) Inst 5 (p1) Inst 4 || (p2) Inst 6 Inst 7 Inst 8 ``` Predication removes **$>50\%$ of branches on average** (Mahlke et al.), *Warning: it complicates bypassing!* (If `Inst 3` and `Inst 5` both target the same register under mutually exclusive predicates, hardware must check predicates before bypassing.) 3. **Control Speculation (`Load.s` / `Chk.s`) and Data Speculation (`Load.a` / `Load.c`):** - **Control Speculation:** Moving a `Load r1` *above* a branch (`br a==b, b2`) is illegal normally because if the branch isn't taken, `Load r1` might dereference a null pointer and cause a spurious page fault! IA-64 solves this by hoisting a **speculative load (`Load.s r1`)** above the branch: `Load.s` *never* raises an exception, instead setting a **"poison" bit (NaT)** on destination register `r1`. In the load's original home block, **`Chk.s r1`** checks whether the poison bit is set and jumps to fixup code only if the branch actually took that path. - **Data Speculation:** Moving `Load r1` *above* an earlier `Store` is illegal if their pointers might alias. IA-64 hoists an **advanced load (`Load.a r1`)** above the `Store`, recording its address in an associative **address check table**. Any intervening `Store` checks the table and **invalidates matching addresses**. In the original location, **`Load.c`** checks whether `r1`'s entry is still valid (and not evicted); if invalidated or missing, it jumps to fixup code to reload. *(Note: this requires associative CAM hardware in the address check table—creeping right back toward superscalar complexity!)* #### Limits of Static Scheduling Ultimately, despite massive engineering effort, **VLIW failed in general-purpose computing**—as Donald Knuth famously observed of Itanium, *"the wished-for compilers were basically impossible to write,"* and Intel formally discontinued Itanium in 2019. Statically unpredictable branches, variable cache-miss latencies, code size explosion, and compiler complexity meant complex VLIW machines approached in-order superscalars in hardware complexity with no advantage on large, irregular applications. However, **VLIW has been highly successful in the embedded DSP market**, where simpler VLIWs run in constrained environments with predictable loops and deterministic memory! *[Editor Note: removed title slide and acknowledgements slide.]* #### ✎ Check Your Understanding 1. In a Rotating Register File, suppose a `mul` has a 5-cycle latency and the loop executes `mul f2, ...` followed by `add ..., f7, ...` with `RRB` decrementing by 1 each cycle. If `RRB = 10` when `mul` executes, what physical register does `mul` write, and what will `RRB` be when `add` reads that exact physical register? **Answer:** At `RRB = 10`, `mul` writes `P(2 + 10) = P12`. Five cycles later, `RRB = 5`, and `add` reads `P(7 + 5) = P12`. 2. Why does IA-64's `Load.s` set a "poison" bit instead of immediately raising a page fault or segmentation fault? **Answer:** Because `Load.s` was hoisted speculatively above a branch; if that branch turns out not to enter the load's original block, the exception would have been spurious. Deferring the check to `Chk.s` in the home block ensures exceptions trigger only when truly needed.