Lecture 4: Pipelining 2
Section 1: Deeper Pipelines and Compiler Scheduling — The MIPS R4000
Moving beyond the classic 5-stage pipeline, single-thread processors exploit more Instruction-Level Parallelism (ILP) across four major pipeline categories:
- Super-pipelined: Slicing each stage into smaller sub-stages to crank up clock frequency (
Time/Cycle). - Decoupled: Splitting integer/address generation from floating-point execution via queues.
- Out-of-order: Feeding instructions from an in-order Frontend into an Instruction Pool, then issuing independent instructions out-of-order into a parallel Backend.
- Superscalar: Widening both Frontend and Backend to fetch and execute multiple instructions per cycle.
Deeper Pipelines: The 8-Stage MIPS R4000
Suppose Instruction Memory and Data Memory caches are too large and slow to access in a single short clock cycle. The MIPS R4000 solves this by super-pipelining cache access into an 8-stage pipeline:
IF— Instruction cache fetch (first half)IS— Instruction cache fetch (second half; instruction bits are actually ready at the end ofIS, before the tag check finishes!)RF— Register Fetch and Instruction cache tag check (speculating that a direct-mapped I$ hit allows us to decode and fetch registers in parallel with checking the tag!)EX— ALU execution (and branch condition evaluation)DF— Data cache fetch (first half)DS— Data cache fetch (second half; data bits ready at end ofDS)TC— Data cache tag check (Commit Point sits right afterTC, because we cannot write loaded data into the register file until we know the cache hit!)WB— Register Writeback
How does deepening the pipeline from 5 to 8 stages hurt branch and load-use hazards?
- R4000 Branches (3-cycle branch delay):
BEQZevaluates its condition inEX(Cycle 4). Meanwhile,Instruction 1(Cycle 2),Instruction 2(Cycle 3), andInstruction 3(Cycle 4) have already enteredIFbeforeTargetcan fetch in Cycle 5—producing a 3-cycle branch delay! - R4000 Load-Use Delay (2-cycle delay):
LD R1reads direct-mapped D$ data at the end ofDS(Cycle 6), and speculatively bypasses it directly toEXof a consumerADDD R2, R1in Cycle 7 beforeTCcompletes (ifTCmisses, the pipeline stalls/recovers). Even with that aggressive bypass, two instructions (Instruction 1in Cycle 2,Instruction 2in Cycle 3) sit betweenLDandADDD(IF=Cycle 4,IS=Cycle 5,RF=Cycle 6,EX=Cycle 7), creating a 2-cycle load-use delay!
Compiler Techniques: Pipeline Scheduling and Loop Unrolling
To keep deeper pipelines busy without stalling, compilers rearrange code. Consider the vector-vector add for (i = 0; i < N; i++) A[i] = B[i] + C[i]; translated naively into RISC-V assembly:
loop: fld f0, 0(x2) // x2 -> B[i]
fld f1, 0(x3) // x3 -> C[i]
fadd.d f2, f0, f1 // stalls waiting for f1!
fsd f2, 0(x1) // x1 -> A[i]; stalls waiting for f2!
addi x1, x1, 8
addi x2, x2, 8
addi x3, x3, 8
bne x1, x4, loop // x4 = end pointer
Simple Pipeline Scheduling: Move the independent pointer-bump instructions (
addi x3, x3, 8andaddi x2, x2, 8) betweenfld f1andfadd.d, and moveaddi x1, x1, 8betweenfadd.dandfsd(adjusting the store offset to-8(x1)becausex1was already bumped by 8!). Still, long load and floating-point latencies quickly run out of independent work inside a single loop iteration.Loop Unrolling: Duplicate the loop body (e.g., 2 iterations at once using distinct registers
f0/f1andf10/f11), bumping pointers once by16and storing at-16(x1)and-8(x1):loop: fld f0, 0(x2) fld f1, 0(x3) fld f10, 8(x2) fld f11, 8(x3) addi x3, x3, 16 addi x2, x2, 16 fadd.d f2, f0, f1 fadd.d f12, f10, f11 addi x1, x1, 16 fsd f2, -16(x1) fsd f12, -8(x1) bne x1, x4, loopUnrolling cuts loop-overhead instructions (
addi,bne) in half and spaces out dependentfld\rightarrowfadd.d\rightarrowfsdchains. However, unrolling hits four walls: it is limited by the number of architectural registers, expands the instruction cache footprint, requires complex compiler pointer-aliasing analysis (provingA,B, andCdon’t overlap), and still cannot hide unpredictable cache misses.
To overcome what static compiler scheduling cannot predict at compile time, architects turned to dynamic hardware scheduling.
✎ Check Your Understanding
Pipeline delay check: On the 8-stage MIPS R4000, why is the load-use delay 2 cycles instead of 3 cycles even though the
TC(tag check) stage does not finish until Cycle 7? Answer: Because the R4000 uses a direct-mapped data cache, allowing the loaded data fromDS(Cycle 6) to bypass speculatively into the consumer’sEXstage in Cycle 7 beforeTCfinishes.Code check: When scheduling
addi x1, x1, 8beforefsd f2, 0(x1)in the vector-add loop, why did the store instruction change tofsd f2, -8(x1)? Answer: Becauseaddialready advancedx1by +8 bytes toward the next element, so-8(x1)restores the target address of the current elementA[i].
Section 2: Dynamic Scheduling, Scoreboarding, and Multi-Unit In-Order Pipelines
In 1964, Seymour Cray built the CDC 6600—widely regarded as the first supercomputer—and pioneered dynamic hardware scheduling to keep 10 parallel, unpipelined functional units (FP adder, 2 FP multipliers, divider, integer adder, 2 incrementers, etc.) busy at a 10\text{ MHz} clock against a 32-bank central memory.
The CDC 6600 Load/Store ISA
Long before “RISC” existed, Cray designed the CDC 6600 ISA specifically to simplify high-performance pipelined hardware:
- Three explicit register banks:
8 x 60-bitdata operand registers (X0–X7),8 x 18-bitaddress registers (A0–A7), and8 x 18-bitindex registers (B0–B7). - 3-address register-register ALU:
opcode(6b) | i(3b) | j(3b) | k(3b)computing R_i \leftarrow R_j \text{ op } R_k. Only 3-bit register fields need dependency checking—zero implicit accumulator or stack dependencies! - Decoupled implicit Load/Store: Writing an address into
A1–A5automatically initiates a memory load into the matchingX1–X5; writing intoA6–A7initiates a store fromX6–X7. For example, a vector-add loop setsA0 <- B0 + a0(triggers loadX0),A1 <- B0 + b0(triggers loadX1), computesX6 <- X0 + X1, and setsA6 <- B0 + c0(triggers storeX6). By decoupling settingArfrom readingXr, software and hardware can overlap multiple outstanding memory requests with independent work! (Its successor, the CDC 7600, pipelined those functional units, directly foreshadowing RISC.)
Complex Pipeline Hazards and the CDC 6600 Scoreboard
In any multi-unit machine—split into a Frontend (fetch/decode) that dispatches instructions into an Instruction Pool, and a Backend that issues them to parallel execution units before Writeback (WB)—four new hazards emerge:
- Structural conflicts at execution if an FPU/memory unit is unpipelined and busy for multiple cycles.
- Structural conflicts at the
WBport when fast and slow units finish in the same cycle. - Out-of-order write hazards (WAW and WAR) when a younger fast instruction overtakes an older slow instruction.
- Imprecise exceptions when younger instructions finish before older ones fault.
The CDC 6600 Scoreboard (the control logic gating entry/exit of the Instruction Pool) solved these dynamically:
- Dispatch (in-order): Instructions enter the scoreboard in program order until the scoreboard is full.
- Issue (out-of-order): An instruction exits the scoreboard to an execution unit only when it has no RAW hazards, no WAW hazards (checked against all older executing instructions), and no structural hazards (on the target EXE unit or register writeback).
- Stalling WAR before Writeback: Notice the subtle WAR rule! A younger instruction can start executing early even if it writes a register (
r1) that an older stalled instruction hasn’t read yet—because it stalls in theEXEstage immediately before register-file writeback until the older instruction finishes readingr1(though stalled outputs waiting to write back can create a pileup blocking that functional unit).
In-Order Multi-Unit and Superscalar Alternatives
Not every processor wanted the complexity of out-of-order scoreboarding. An In-Order Multi-Unit Pipeline enforces fixed timing instead:
- All short/pipelined units (Integer ALU + Data Mem,
FAdd,FMul) are padded to the exact same latency toW(X1, X2, X3to the Commit Point right beforeW). Because one instruction enters per cycle and all take 3 execution stages, writeback ports are never oversubscribed (1in,1out per cycle) and exceptions remain strictly in-order! - Long-latency operations like an unpipelined
FDivor cache miss simply stall the pipeline. - How do we keep 3-stage padding from slowing down single-cycle integer instructions? Bypassing from the end of
X1directly back toX1inputs lets back-to-back integer ops run with zero bubbles while still waiting untilWto commit!
To push throughput past \text{CPI} = 1 (reaching \text{IPC} > 1) without out-of-order hardware, an In-Order Superscalar Pipeline (such as the 1992 Alpha 21064 or 1996 MIPS R5000) fetches 2 instructions per cycle into a Dual Decode, issuing both simultaneously whenever one is integer/memory and the other is floating-point. Extending this to 4-wide issue (UltraSPARC, Alpha 21164) by duplicating functional units works conceptually, but register-file read/write ports and bypassing multiplexer wiring grow explosively.
[Editor Note: removed supercomputer folklore, CDC 6600 weight/floor-area/cooling/price stats, and the Watson/Cray memo slides.]
✎ Check Your Understanding
Scoreboard rule check: Which two data hazards prevent an instruction from issuing out of the CDC 6600 Scoreboard, and how does the Scoreboard handle the third data hazard (WAR)? Answer: RAW and WAW hazards (plus structural hazards) block issue out of the scoreboard; WAR hazards are allowed to issue and execute, then stall right before register-file writeback until the older reader finishes.
Design check: In the complex in-order pipeline where Integer ALU,
FAdd, andFMulall wait until stageX3to reachW, why don’t consecutive integer instructions stall for 2 cycles? Answer: Bypassing forwards the integer ALU result immediately afterX1to the next instruction’sX1input without waiting forW.
Section 3: Advanced In-Order Techniques — Decoupled Execution and Dynamic ALU Scheduling
Even without full out-of-order scoreboarding, architects developed two clever in-order mechanisms to hide memory latency: Decoupled Architectures and Dynamic ALU Scheduling.
Decoupled (Lookahead / Runahead) Execution
Look back at our vector-add loop:
loop: fld f0, 0(x2) // control/address (red)
fld f1, 0(x3) // control/address (red)
fadd.d f2, f0, f1 // data computation (green)
fsd f2, 0(x1) // address (red) + data (green)
addi x1, x1, 8 // control/address (red)
addi x2, x2, 8 // control/address (red)
addi x3, x3, 8 // control/address (red)
bne x1, x4, loop // control/address (red)
Notice a structural split: none of the control or memory-address instructions (addi, bne, fld address, fsd address) depend on the floating-point data computations (fadd.d)!
A Simple Decoupled Machine exploits this by connecting two in-order pipelines through FIFO queues:
- The Integer Pipeline (
F D X M W) runs far ahead (“lookahead”): it executesaddiandbne, computes load addresses (checking eachLoad Addressagainst pending entries in theStore Address Queueso loads never bypass an older store to the same address!), sends loads to memory, and pushes floating-pointµOps (Load Data Writeback µOp,Compute µOp,Store Data Read µOp) into aµOp Queue. - Memory returns loaded words in order into a
Load Data Queue. - The Floating-Point Pipeline (
R X1 X2 X3 W) runs at its own pace, pulling micro-ops from theµOp Queueand operands from theLoad Data Queue, computingfadd.d, and placing results into theStore Data Queueto pair with addresses waiting in theStore Address Queue.
Because the Integer Pipeline can race several loop iterations ahead, many writes to f0 can be queued in flight at the same time, cleanly ordered by the FIFO queues without WAW/WAR corruption!
Dynamic ALU Scheduling (Motorola MC68060 Style)
Finally, consider a clever in-order pipeline trick used in the Motorola MC68060 (and adapted in modern in-order RISC cores): placing two ALU stages—one before memory (EA) and one after memory (ALU): \text{EA (Effective Address)} \longrightarrow \text{MEM (Data Cache)} \longrightarrow \text{ALU (Integer Execute)}
Originally built so CISC memory-register instructions (add x1, x1, 24(x2)) could calculate x2 + 24 in EA, read M[x2 + 24] in MEM, and add x1 in ALU all in one pass, this EA -> MEM -> ALU structure also lets pure RISC instructions bypass load-use stalls dynamically! Trace all five instructions in the MC68060 example (using RISC-V-style syntax):
add x1, x1, 24(x2): Cycle 1EAcomputesx2 + 24; Cycle 2MEMreadsM[x2 + 24]; Cycle 3ALUcomputesx1 + M[x2 + 24].add x3, x1, x6: Needsx1from Instruction 1’s lateALUstage (Cycle 3)! Because it needs no memory access, it sails pastEA(Cycle 2) andMEM(Cycle 3), acceptsx1bypassed directly from Cycle 3’sALUinto Cycle 4’sALU(x1 + x6), and finishes with zero stalls!addi x5, x2, 12: Needsx2(already ready) and no memory access, so it executes early in Cycle 3’sEAstage (x2 + 12).lw x4, 16(x5): Needsx5from Instruction 3 immediately for address calculation—and getsx5bypassed straight from Cycle 3’sEAinto Cycle 4’sEA(x5 + 16), hittingMEMin Cycle 5 with zero stalls!lw x8, 16(x3): Needsx3from Instruction 2’s lateALU(Cycle 4)—and getsx3bypassed directly from Cycle 4’sALUinto Cycle 5’sEA(x3 + 16), hittingMEMin Cycle 6 with zero stalls!
By placing an ALU both before and after MEM, producers run early (EA) when feeding load addresses and late (ALU) when consuming loaded data—achieving zero-bubble execution on code that would stall on a single-ALU 5-stage pipeline.
[Editor Note: removed duplicate Simple Decoupled Machine diagram slide.]
✎ Check Your Understanding
Queue safety check: In the Simple Decoupled Machine, why must every
Load Addressgenerated by the Integer Pipeline be checked against theStore Address Queue? Answer: Because the Integer Pipeline runs ahead and sends loads to memory while older floating-point store data may still be queued; if a load matches a pending store address, forwarding or waiting prevents reading stale data from memory.Stage assignment check: In an
EA -> MEM -> ALUpipeline, why doesaddi x5, x2, 12(followed immediately bylw x4, 16(x5)) execute inEArather than the laterALUstage? Answer: ExecutingaddiinEAproducesx5two cycles earlier, allowing it to bypass directly into the immediately followinglw’sEAaddress-calculation stage with zero stalls.