11. Advanced ILP: Superscalar, VLIW, Loop Unrolling, Multithreading
Description
Superscalar processors issue several instructions per cycle using multiple execution units. The hardware finds independent instructions at run time (dynamic multiple issue, usually with out-of-order execution).
VLIW (Very Long Instruction Word) moves that job to the compiler: it packs independent operations into one long instruction (a bundle) with fixed slots (e.g. integer, memory, floating point). The hardware is simple, but code gets bloated with NOPs when slots cannot be filled, and the binary depends on the exact hardware. Intel's Itanium (EPIC) is the well-known VLIW-style design and was commercially unsuccessful.
| Superscalar | VLIW | |
|---|---|---|
| Who finds parallelism | Hardware (run time) | Compiler (compile time) |
| Hardware complexity | High | Lower |
| Code compatibility | Same binary across generations | Tied to hardware layout |
| Unpredictable events (cache misses) | Handled dynamically | Stalls the whole bundle |
| Code size | Normal | Larger (NOPs) |
Software techniques that expose ILP:
- Loop unrolling: replicate the loop body several times to cut branch/counter overhead and give the scheduler more independent instructions (needs extra registers; works when iterations are independent).
- Instruction scheduling: reorder instructions to hide latencies.
- Software pipelining and trace scheduling (compiler techniques for VLIW).
Multithreading fills idle issue slots with work from another thread:
- Fine-grained: switch threads every cycle.
- Coarse-grained: switch only on long stalls.
- SMT (simultaneous multithreading, "hyper-threading" on Intel): issue from several threads in the same cycle on one core, sharing the execution units.
Limits of ILP: true dependences, branch mispredictions, cache misses and the power cost of wide issue windows give diminishing returns, which is why performance growth moved to multicore (TLP), SIMD (DLP) and specialization.
Maximizing processor performance: balance clock speed (power/heat), ILP, the memory hierarchy and energy efficiency.
Key Formulas
IPC = 1 / CPI Peak IPC = issue width
Efficiency = achieved IPC / peak IPC
Loop unrolling by k: loop overhead per element drops from (overhead) to (overhead / k)
CPU time = IC × CPI / f (ILP lowers CPI; wider issue can push CPI below 1)
Steps
Unroll and schedule a loop: (1) check iterations are independent, (2) replicate the body k times, (3) rename registers for each copy, (4) combine counter/branch updates into one, (5) reorder so loads come early and stores late to hide latency.
Pack a VLIW bundle: (1) build the dependence graph, (2) place independent operations in slots of the same bundle, (3) fill unused slots with NOPs.
Worked Example
A. IPC. 4-issue core, achieved IPC = 1.6: CPI = 1/1.6 = 0.625, efficiency = 1.6 / 4 = 40% of peak.
B. Unrolling a loop for (i...) x[i] = x[i] + s; on a single-issue in-order core. Assumed latencies: load→FP op needs 1 stall, FP op→store needs 2 stalls, branch has no stall.
Loop: fld f0, 0(x1) # load x[i]
fadd.d f4, f0, f2 # + s
fsd f4, 0(x1) # store
addi x1, x1, -8
bne x1, x2, Loop
| Version | Cycles per iteration | Cycles per element |
|---|---|---|
| Original (with 3 stalls: 1 after load, 2 after fadd) | 8 | 8 |
| Rescheduled (addi moved into a stall slot, store offset adjusted) | 7 | 7 |
| Unrolled ×4 and scheduled | 14 | 3.5 |
Unrolled ×4 and scheduled, no stalls:
Loop: fld f0, 0(x1)
fld f6, -8(x1)
fld f8, -16(x1)
fld f10,-24(x1)
fadd.d f4, f0, f2
fadd.d f12, f6, f2
fadd.d f14, f8, f2
fadd.d f16, f10, f2
fsd f4, 0(x1)
fsd f12,-8(x1)
fsd f14,-16(x1)
fsd f16,-24(x1)
addi x1, x1, -32
bne x1, x2, Loop
14 instructions, 0 stalls → 3.5 cycles per element, a 2.3× improvement over the original 8. It uses more registers and makes the code longer.
C. Issue width and scheduling. Five instructions: i1: a = load (latency 2); i2: b = load (2); i3: c = a + b (1); i4: d = e × f (3); i5: g = d + c (1). Results can be used latency cycles after issue.
| Machine | Cycles to finish |
|---|---|
| 1-wide, in order | 8 |
| 2-wide, in order | 6 |
| 2-wide, out of order | 5 (i4 issues early while i3 waits for loads) |
D. VLIW bundle. Slots [integer | memory | FP]. fld f0,0(x1) (mem) and addi x3,x3,4 (int) and an independent fadd.d (FP) fit in one bundle; if no FP op is ready the slot becomes a NOP, wasting space.
E. Loop-carried dependence. a[i] = a[i-1] + 1 cannot be unrolled for parallelism: each iteration needs the previous result (a true RAW across iterations).
F. SMT. One thread on a 4-wide core might reach IPC ≈ 1.2 because of stalls; two SMT threads fill idle slots and raise total throughput, but typically by well under 2× because they share caches and execution units.
Python / Assembly
def schedule(instrs, width, ooo):
"""instrs: (name, dest, [srcs], latency); returns issue cycles and total cycles"""
ready, done, pending, cycle = {}, [None]*len(instrs), list(range(len(instrs))), 0
while pending:
cycle += 1; n = 0
for idx in list(pending):
name, dest, srcs, lat = instrs[idx]
if n < width and all(ready.get(s, 0) < cycle for s in srcs):
ready[dest] = cycle + lat - 1
done[idx] = cycle; pending.remove(idx); n += 1
elif not ooo:
break # in-order: a stalled instruction blocks the rest
return done, max(done[i] + instrs[i][3] - 1 for i in range(len(instrs)))
prog = [("i1","a",[],2), ("i2","b",[],2), ("i3","c",["a","b"],1),
("i4","d",["e","f"],3), ("i5","g",["d","c"],1)]
print(schedule(prog, 1, False)[1], # 8
schedule(prog, 2, False)[1], # 6
schedule(prog, 2, True)[1]) # 5
(The unrolled RISC-V loop above is the assembly example for this topic.)
Notes
- Itanium: the compiler combines instructions, and Itanium called its variant EPIC (explicitly parallel instruction computing).
- Loop unrolling reduces branching overhead and increases ILP; the cost is code size and register pressure.
- Hyper-threading is Intel's name for SMT; it shares one core's execution resources between two hardware threads.
- ILP hardware is transparent to programmers; SIMD and threads require explicit software support.
- Quick facts: superscalar executes several instructions per cycle; hyper-threading lets one core run several threads; clock speed raises performance but also power and heat; VLIW has the compiler pack instructions.
References: [HP6] Ch. 3 (3.2 loop unrolling, 3.7 multiple issue/VLIW, 3.10 limits of ILP, 3.11 multithreading); [PH-RV] Ch. 4 (4.10-4.11);
All rights remain with the original author (Algo-holics). Publishing here does not grant a license to copy or reuse this text. Copyright & license. If you noticed a mistake or copyright issues, please contact: lebroskiofficial@proton.me