Sub topic 17
Beyond the Basic Pipeline: Superscalar and Out-of-Order Execution
This chapter assumes you know the five-stage pipeline (IF, ID, EX, MEM, WB), the three hazard classes, forwarding, and the effective CPI formula. It shows where the basic pipeline stops scaling and what real processors add. For GATE, this is a light topic: expect a conceptual question, not a long calculation. The ideas also explain why WAR and WAW hazards, which never occur in the simple in-order pipeline, matter later.
Where the basic pipeline runs out
A scalar in-order pipeline has a best-case CPI of 1. It issues one instruction per cycle, and it issues instructions strictly in program order. Two limits follow from this.
- Throughput ceiling. CPI cannot fall below 1, however well you hide stalls.
- Order dependence. If one instruction stalls, every instruction behind it stalls too, even when those instructions are independent of it.
Deeper pipelines (more stages, shorter clock period) raise the clock rate. Deeper pipelines also raise the cost of each stall and each branch misprediction, because more stages sit between fetch and the point where the branch resolves. The two techniques in this chapter attack the CPI limit and the order limit directly.
Superscalar execution
A superscalar processor fetches, decodes, and issues more than one instruction per cycle. A processor that can issue W instructions per cycle is called W-wide or W-issue. Its ideal CPI is 1/W, and its ideal IPC (instructions per cycle) is W.
The hardware needs duplicated resources:
- Several ALUs, and often separate units for integer, floating-point, load/store, and branch work.
- A wider fetch path and several decoders.
- More register file read and write ports.
- Hazard-checking logic that compares every instruction in an issue group against every other one and against the instructions already in flight.
The last item is the costly one. In a 2-wide machine, the two instructions issued together must be checked for dependences on each other. If the second reads a register the first writes, they cannot issue in the same cycle.
def superscalar_cycles(n, k, width, extra_stall_cycles=0):
# Ideal case: the first group fills the pipe in k cycles,
# then one group of `width` instructions completes each cycle.
groups = -(-n // width) # ceiling division
return k + groups - 1 + extra_stall_cycles
n = 100
print(superscalar_cycles(n, 5, 1)) # 104, scalar pipeline: k + n - 1
print(superscalar_cycles(n, 5, 2)) # 54, 2-wide
print(superscalar_cycles(n, 5, 4)) # 29, 4-wideThe 2-wide result is not 52 or 104/2 because the pipeline fill time of k - 1 cycles does not shrink with width.
Static and dynamic issue
- Static multiple issue (VLIW). The compiler packs independent operations into one long instruction word. The hardware stays simple, but the compiler must know the machine's latencies and the code is tied to that machine.
- Dynamic multiple issue (superscalar). The hardware decides at run time which instructions can issue together. The same binary runs on different widths, at the cost of more complex hardware.
Instruction-level parallelism and its limits
Instruction-level parallelism (ILP) is the amount of work in a program that can overlap because instructions are independent. A wide machine only helps if the program has enough ILP. A chain such as add r1,r1,r2 followed by add r1,r1,r3 followed by add r1,r1,r4 is a serial dependence chain, and no issue width can speed it up.
Three things limit ILP in practice: true data dependences (RAW), branches that make the instruction stream unpredictable, and memory latency, since a cache miss can take hundreds of cycles.
Out-of-order execution
An in-order machine stalls on the first blocked instruction. Consider this code, where the lw misses in the cache and takes a long time.
lw r1, 0(r2) # slow: cache miss
add r3, r1, r4 # depends on r1, must wait
sub r5, r6, r7 # independent of both
and r8, r9, r10 # independent of bothAn in-order pipeline holds sub and and behind the stalled add. An out-of-order (OoO) processor lets sub and and execute while lw and add wait. Instructions begin execution when their operands are ready, not when their turn in program order arrives.
The structure of an OoO core
- Fetch and decode in order. The front end still follows program order.
- Dispatch into a buffer. Decoded instructions wait in a reservation station or issue queue, along with the operands they already have.
- Execute when ready. An instruction leaves the queue for a functional unit once all its operands are available. Results are broadcast so that waiting instructions can capture them. This is forwarding generalized.
- Commit in order. A reorder buffer (ROB) holds finished results until every earlier instruction has finished. Then results are made architecturally visible in program order.
In-order commit is what makes exceptions precise. If an earlier instruction faults, the machine can discard all later results because none has been committed.
Register renaming: removing WAR and WAW
In the five-stage in-order pipeline, WAR and WAW hazards cannot happen. Every instruction reads in ID and writes in WB, in program order, so a later write never overtakes an earlier read or write. Out-of-order execution breaks that guarantee, and these hazards become real.
WAR and WAW are name dependences: two instructions use the same register name but pass no value between them. Only RAW is a true dependence, because a value actually flows. Name dependences can be removed by giving each write a different physical register. This is register renaming.
div r1, r2, r3 # I1: slow
add r4, r1, r5 # I2: RAW on r1 (true dependence)
sub r5, r6, r7 # I3: WAR on r5 with I2 (name dependence)
mul r1, r8, r9 # I4: WAW on r1 with I1 (name dependence)Without renaming, sub must not write r5 before add reads it, and mul must not write r1 before div does. With renaming, the hardware maps the architectural register r1 to different physical registers for div and mul, and r5 to different physical registers for add's read and sub's write. Then sub and mul can run immediately, and only the RAW dependence from div to add remains.
The table summarizes which hazards each design can have.
| Design | RAW | WAR | WAW |
|---|---|---|---|
| Five-stage in-order pipeline | Yes | No | No |
| Out-of-order without renaming | Yes | Yes | Yes |
| Out-of-order with renaming | Yes | Removed | Removed |
A well-known caveat: a basic in-order pipeline can show WAW if some instructions take more cycles in the execute stage (for example, floating-point operations that take longer than integer ones) and may finish out of order. So the "no WAW" rule holds for the standard five-stage design, and GATE questions usually state the assumption.
Branch prediction becomes essential
A wide, deep machine has many instructions in flight, so a mispredicted branch discards a lot of work. With a 4-wide machine and a 15-cycle misprediction penalty, up to 60 instruction slots can be wasted per mispredict. Out-of-order cores therefore speculate: they predict the branch, fetch and execute along the predicted path, and hold results in the ROB. If the prediction was right, results commit. If it was wrong, the ROB is flushed and fetch restarts from the correct address. This builds on the dynamic predictors from the previous chapter.
The cost of mispredictions is easy to estimate with the effective CPI formula.
def effective_cpi(base_cpi, stall_events):
return base_cpi + sum(freq * penalty for freq, penalty in stall_events)
# 4-wide machine, ideal CPI 0.25, 20% branches, 5% of branches mispredicted,
# 15-cycle penalty per misprediction
mispredict_rate_per_instr = 0.20 * 0.05
print(effective_cpi(0.25, [(mispredict_rate_per_instr, 15)])) # 0.4The result is 0.4, so mispredictions alone cost 60% over the ideal CPI of 0.25. The wider the machine, the larger the share of time lost to stalls, which is why prediction accuracy matters more as width grows.
Trade-offs and failure modes
- Complexity and power. Dependence checking grows roughly with the square of the issue width. Issue queues, the ROB, and rename tables consume area and energy.
- Diminishing returns. Real programs rarely supply more than a few independent instructions per cycle, so widths beyond about 4 to 8 give small gains.
- Memory-bound code. If most time is spent waiting on memory, extra execution units sit idle.
- Speculation side effects. Wrong-path instructions leave traces, such as cache contents. Security attacks like Spectre exploit this.
Typical conceptual questions
- A processor that issues more than one instruction per cycle is superscalar, and its CPI can be below 1.
- Out-of-order execution gives out-of-order issue and completion but in-order commit.
- Register renaming removes WAR and WAW but never RAW.
- More pipeline stages raise the clock rate but also the penalty of a stall or a misprediction.
Exercise
A 2-wide in-order superscalar machine has a 5-stage pipeline and no stalls. How many cycles does it need to run 100 instructions? Then state which of RAW, WAR and WAW register renaming eliminates.
Show answer
Two instructions finish per cycle after the pipeline fills, so there are 100 / 2 = 50 groups. Cycles = k + groups - 1 = 5 + 50 - 1 = 54. Register renaming eliminates WAR and WAW, which are name dependences. It cannot eliminate RAW, because RAW is a true dependence in which a value flows between instructions.
Exercise
A 4-wide machine has an ideal CPI of 0.25. 25% of instructions are branches, 4% of branches are mispredicted, and each misprediction costs 12 cycles. What is the effective CPI, and by what factor is it above the ideal?
Show answer
Mispredictions per instruction = 0.25 x 0.04 = 0.01. Stall cycles per instruction = 0.01 x 12 = 0.12. Effective CPI = 0.25 + 0.12 = 0.37. The factor over ideal is 0.37 / 0.25 = 1.48.
Exercise
Why can the standard five-stage in-order pipeline ignore WAR hazards, but an out-of-order core cannot?
Show answer
In the five-stage pipeline every instruction reads registers in ID and writes in WB, and instructions move through the stages in program order. A later instruction therefore cannot write a register before an earlier instruction has read it. An out-of-order core can execute a later instruction before an earlier one, so the later write may overtake the earlier read, and the hazard must be handled, usually by renaming.