Skip to guide

Part 1 · Instruction pipeline

ISA, datapath, pipeline, and hazards

Connect the software-visible instruction contract to the five-stage datapath, then locate structural, data, and control hazards cycle by cycle.

Reason cycle by cycle, calculate the cost of misses and stalls, and explain why each implementation choice changes performance or correctness.

Updated July 20263 connected chaptersInteractive labs + worked examples

Instruction set architecture

The contract software sees.

An ISA defines program-visible behavior. Microarchitecture decides how a particular processor fulfills that contract.

01

Instruction set

The opcodes, encodings, and operations the processor can execute.

02

Visible registers

General-purpose and special registers software can name and manipulate.

03

Addressing modes

Immediate, register-relative, PC-relative, and other ways to identify operands.

04

Data types

Supported widths and formats, such as bytes, words, integers, and floating point.

RISC

Regular operations, pipeline-friendly control.

  • Smaller set of simpler instructions
  • Often fixed-length encodings
  • Explicit load/store memory operations
  • More sequencing responsibility for compilers
Examples: RISC-V, MIPS, and many ARM instruction sets.

CISC

Expressive instructions, more complex decode.

  • Larger and more varied instruction repertoire
  • Often variable-length encodings
  • Some instructions combine memory and computation
  • Complex work may be translated into internal micro-operations
Example: x86. Modern implementations often blend both design philosophies.

End-to-end system map

Follow one load from instruction bits to returned data.

The ISA defines what LW means. The pipeline calculates its virtual address, translation supplies a physical address, the hierarchy locates the line, and WB makes the loaded value visible in the destination register.

Classic datapath

Overlap work without changing program meaning.

A classic MIPS-style pipeline partitions instruction work across five stages. Pipeline registers hold each instruction's control and data between stages.

IF

Instruction fetch

Read the instruction at the program counter and choose the next PC.

ID

Decode and register read

Decode the opcode, identify operands, and read the register file.

EX

Execute

Run the ALU, compare a branch, or calculate an effective address.

MEM

Memory

Access the data cache for a load or store.

WB

Write back

Commit an ALU or load result into the architectural register file.

IFPC + instruction memoryIDControl + register file + sign extendEXALU + branch compareMEMData cacheWBResult mux + register write

Control signals travel with the instruction. Forwarding paths return fresh EX or MEM results to ALU inputs; branch resolution redirects the PC; WB closes the loop at the register file.

Next-PC muxPC + 4 or resolved branch targetALU-input muxRegister value or sign-extended immediateBranch decisionCompare result gates the target selectionWrite-back muxALU result or loaded memory data

Interactive pipeline timeline

See overlap, bubbles, bypasses, and flushes cycle by cycle.

5-stage RISC

Independent instructions enter on consecutive cycles. Latency remains five cycles, but steady-state throughput reaches one completed instruction per cycle.

Instruction
ADD r1,r2,r3IFIDEXMEMWB
AND r4,r5,r6IFIDEXMEMWB
OR r7,r8,r9IFIDEXMEMWB
XOR r10,r11,r12IFIDEXMEMWB
Cycle 1ADD r1,r2,r3: IF

No dependency or resource conflict requires a bubble.

Active stageForwarded valueStall or resource conflictFlushed work

Pipeline hazards

When the next instruction cannot safely advance.

A hazard is a timing condition, not automatically a bug. The interlock, bypass, and recovery machinery must preserve architectural behavior.

Resource

Structural hazard

Two stages request hardware that cannot serve both in the same cycle.

Resolve with replication, multi-porting, arbitration, or a stall.
Dependency

Data hazard

A consumer reaches its operand-use point before the producer's result is available.

Forward when timing permits; otherwise interlock and inject a bubble.
Control

Control hazard

A branch or exception makes already-fetched instructions potentially incorrect.

Predict to stay busy, then flush and redirect when the guess is wrong.
RAW dependencyADD r1, r2, r3 SUB r4, r1, r5
Load-use boundaryLW r1, 0(r2) ADD r3, r1, r4

The ADD result exists after EX and can bypass to the next EX. The load result exists only after MEM, so an adjacent consumer needs one bubble in this five-stage timing model.

Register-file edge case

Instruction i writes while i+3 reads.

The ISA only requires the SUB to observe the ADD result. The register-file circuit decides whether that same-cycle read is write-first, read-first, or unsupported. A half-cycle convention or WB-to-ID bypass avoids a bubble; a read-first single-port implementation must interlock.

Interview reasoning

Locate the boundary before naming the fix.

  1. 1

    Produce: At which stage and edge does the value or decision become valid?

  2. 2

    Consume: At which stage and edge must the younger instruction sample it?

  3. 3

    Transport: Is there a bypass or port connecting those two points in time?

  4. 4

    Hold: Which pipeline registers freeze, and where is the bubble inserted?

  5. 5

    Recover: Which younger side effects must be killed after a redirect?

Common implementation failuresForwarding from the wrong producer when two older instructions target the same registerFreezing the PC but not IF/ID, which duplicates an instructionFlushing data while a wrong-path store-enable or register-write control bit survives
Why can forwarding not completely remove a load-use hazard?

A load receives its data at the end of MEM, while the following instruction needs the operand at the beginning of EX in that same cycle. The value arrives after the consumer's sampling boundary, so the pipeline stalls once and then forwards it.

What this tests: Stage timing, not merely the definition of a RAW dependency.
How do pipeline latency and throughput differ?

Latency is the time for one instruction to pass from IF through WB. Throughput is the completion rate across many overlapped instructions. A five-stage pipeline can retain five-cycle latency while approaching one instruction per cycle after filling.

What this tests: Whether the candidate separates per-item delay from system rate.
Can ID read and WB write the same register in one cycle?

Instruction i can write r1 in WB while instruction i+3 reads r1 in ID. This is not an architectural WAR hazard. It is a same-cycle register-file timing or port conflict if the implementation returns old data or cannot perform both operations. A write-first array, first-half write plus second-half read, or explicit WB-to-ID bypass removes the stall; otherwise ID waits one cycle.

What this tests: The difference between an ISA dependency and a microarchitectural resource constraint.
Where does a one-port unified L1 cache create a structural hazard?

When a load or store reaches MEM, a younger instruction simultaneously needs the only port for IF. The processor must choose one request, normally the older data access, and hold the PC and fetch stage until the port becomes free.

What this tests: Resource occupancy across stages.

Pipeline performance

2.5 GHz benchmark

40% ALU, 25% branch, 20% load, 15% store.

Branch cost0.25 × 0.20 × 3 = 0.15 CPI
Load-use cost0.20 × 0.30 × 1 = 0.06 CPI
Effective resultCPI 1.21 · 2,066 MIPS

Continue the system

Connect the adjacent layer.