CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 3 - Pipelining I

Chris Fletcher
UC Berkeley, Spring 2026

"Iron Law" of Processor Performance

$$\frac{\text{Time}}{\text{Program}} = \frac{\text{Instructions}}{\text{Program}} \times \frac{\text{Cycles}}{\text{Instruction}} \times \frac{\text{Time}}{\text{Cycle}}$$

Due to Emer & Clark. Execution time is the product: improving one factor fails if another blows up more.

Putting Time-per-Cycle into Perspective

CPI for a Microcoded Machine

Inst 1: 7 cycles Inst 2: 5 cycles Inst 3: 10 cycles time →

$\text{CPI} = \dfrac{7+5+10}{3} = \dfrac{22}{3} = 7.33$

Analyzing Microcoded Machines

From Microcode to RISC

Nanocoded 68000RISC µPC µcode ROM nanoinstructionROM User PC Instruction Cache HardwiredDecode
  • VLSI: logic, RAM, ROM all MOS transistors
    • Semiconductor RAM ≈ same speed as ROM
  • Fast on-chip RAM holds the user instructions actually needed, not dead microroutines
  • Simple regular ISA = vertical microcode exposed as the ISA
  • Early 1980s: 32-bit datapath + small caches fit on one chip

Microarchitecture Styles and the Iron Law

MicroarchitectureCPICycle time
Microcoded> 1 (e.g., 7.33)Short
Single-cycle unpipelined1Long
Pipelined1 (ideal)Short

Check Your Understanding

1. Iron Law: Processor A cuts instructions/program by 20% with complex microcoded instructions but doubles CPI vs. Processor B at the same clock. Which is faster, by what factor?
B. A's time scales by $0.80 \times 2.0 = 1.60$, so A takes 1.6× longer.
2. Fill in the blank: when RAM became as fast as ROM, RISC replaced the microcode ROM with a user-visible ________ and the nanoinstruction ROM with ________.
Instruction cache; hardwired decode.

Three Timelines

Microcoded Inst 1 (7) Inst 2 (5) Inst 3 (10) Single-cycle Inst 1 Inst 2 Inst 3 Pipelined FDXMW FDXMW FDXMW one instruction finishes every short cycle time →

A 5-Stage Pipeline Does NOT Have CPI = 5

Latency, Bandwidth, Occupancy

CaseWhen it happens
Occupancy ≪ LatencyPipelined unit: 5-stage FP adder takes 5 cycles but accepts a new op every cycle
Occupancy > LatencyResult returns early, then recovery cycles (DRAM precharge after a read)
Bandwidth > 1/LatencyPipelined ($\text{BW} = 1/\text{Occupancy}$) or replicated sub-units
Bandwidth < 1/LatencyOccupancy > latency, or stalls/bubbles interrupt issue

Classic 5-Stage RISC Pipeline

FetchDecodeEXecuteMemoryWriteback PC InstructionCache Inst.Reg Registers Imm A B Imm ALU DataCache register write

Dashed lines = pipeline registers (rising edge). Assumes synchronous reads and writes for register file and memories.

Pipeline Bubbles and Hazards

Finish sequenceCPI
I1, I2, I33/3 = 1.00
I1, I2, bubble, I34/3 = 1.33
I1, bubble, I2, bubble, I35/3 = 1.67

Resolving Structural Hazards

Check Your Understanding

1. True or False: a 5-stage pipeline has ideal CPI = 5 because every instruction takes 5 cycles.
False. Latency is 5 cycles, but 5 instructions overlap, so one finishes per cycle: CPI = 1.
2. Quick computation: a memory controller returns read data in 4 cycles, then needs 2 more cycles of precharge before the next request. Latency, occupancy, peak bandwidth?
Latency 4; occupancy 6; bandwidth 1/6 ops/cycle (occupancy > latency, bandwidth < 1/latency).

Types of Data Hazards

HazardExampleIn our 5-stage pipeline?
RAW (data dependence)$r_3 \leftarrow r_1\, op\, r_2$ then $r_5 \leftarrow r_3\, op\, r_4$Yes: the only one (reads in D, writes in W)
WAR (anti-dependence)$r_3 \leftarrow r_1\, op\, r_2$ then $r_1 \leftarrow r_4\, op\, r_5$No; matters for out-of-order (Lecture 4)
WAW (output dependence)$r_3 \leftarrow r_1\, op\, r_2$ then $r_3 \leftarrow r_6\, op\, r_7$No; arises with variable-latency pipelines

Interlocking vs. Bypassing

add x1, x3, x5
sub x2, x1, x4

Interlocked (sub reads x1 in D only after add's W):

c1c2c3c4c5c6c7c8c9
addFDXMW
subFDDDDXMW

Bypassed (X/M latch → ALU input):

c1c2c3c4c5c6
addFDXMW
subFDXMW

Red = 3 bubbles. With bypassing, x1 is ready in the X/M latch at the end of c3: 0 bubbles.

Fully Bypassed Datapath

Where Bypassing Fails: Load-Use

c1c2c3c4c5c6c7
lw x1, 0(x2)FDXMW
add x3, x1, x4FDDXMW

Check Your Understanding

1. Hazard classification: RAW, WAR, or WAW on x2? add x3, x2, x1 then sub x2, x4, x5.
WAR: the second instruction writes x2, which the first must read first.
2. What would happen if lw x1, 0(x2) is immediately followed by add x3, x1, x4 on the fully bypassed pipeline?
A 1-cycle bubble: x1 arrives at the end of lw's M stage, one cycle after add wanted to enter X.

Control Hazards

StageWhat is knownResolves
FetchPC(predict PC+4)
DecodeOpcode, immediate offsetJ/JAL (direct, unconditional); "not a branch"
EXecuteRegister values, ALU conditionBEQ etc. (direct, conditional); JALR/JR (indirect)

Pipelining Jumps and Branches

c1c2c3c4c5c6
beq (taken)FDXMW
next+4Fkilled
next+8killed
targetFDX

Branch Delay Slots

0x100  j   target
0x104  add x1, x2, x3   # delay slot: always executes
...
0x205  target: ...

Why Classic 5-Stage Pipelines Have CPI > 1

Check Your Understanding

1. Cycle count: without branch prediction, how many bubbles for a taken beq (resolved in X) vs. j (resolved in D)?
beq: 2 (FKill + DKill). j: 1 (FKill only).
2. Yes or No: do NOPs in branch delay slots speed up real programs? Use the Iron Law.
No. Fewer stall cycles lower CPI, but instructions/program rises equally, so time is unchanged or worse (I-cache misses).

Traps and Interrupts

Ii-1IiIi+1 HI1HI2…HIn trap handler, then (typically) return to Iᵢ

Precise Traps

Stop on one instruction $I_i$ so that all of $I_0 \ldots I_{i-1}$ have completed and nothing from $I_i$ onward has changed architectural state.

Exceptions in a 5-Stage Pipeline

Commit Point Exception Handling

  1. Carry Exc flags and PC down the pipeline (Exc D/E/M, PC D/E/M)
  2. Per instruction: earlier stages' exceptions override later ones
  3. Across instructions: hold exceptions until the commit point at the end of M
    • Instructions reach M in program order, so the oldest fault traps first
  4. Inject asynchronous interrupts at the commit point
  5. On trap: update Cause and EPC (from PC_M); assert Kill F, Kill D, Kill E, Kill Writeback; select handler PC

Data-memory stores and register writes happen only at or after commit.

Speculating on Exceptions

Check Your Understanding

1. Ordering: in one cycle, Instruction 1 (in M) has a data address exception and younger Instruction 2 (in X) overflows. Which trap is taken, and what happens to Instruction 2?
Instruction 1's exception reaches the commit point first (EPC = its PC_M); Instruction 2 is killed before changing architectural state.
2. Fill in the blank: exception flags are carried to the ________ at the end of M, so state changes only once an instruction cannot fault.
Commit point.