Lecture 7: Memory 3
Section 1: Reducing Hit Time and Miss Penalty — Victim Caches, Way Prediction, Critical Word First, and Non-Blocking Caches
Recalling \text{AMAT} = \text{Hit time} + \text{Miss rate} \times \text{Miss penalty}, we now examine five architectural techniques targeting Hit time, Miss penalty, and conflict Miss rate.
1. Victim Caches (Reducing Conflict Misses Without Hurting Hit Time)
Direct-mapped L1 caches have the fastest Hit time (no way-mux!), but suffer high conflict misses. In the HP 7200, architects added a tiny Victim Cache—a 4-block fully-associative backup cache holding lines recently evicted from the direct-mapped L1:
- First, look up in the direct-mapped L1 (fast 1-cycle hit!).
- If L1 misses, check the 4-entry Victim Cache (
VC). - On a
VChit: Swap the hit line fromVCwith the newly evicted victim line from L1, returning data quickly without touching L2! - On a
VCmiss: Fetch from L2 into L1, move L1’s evicted line intoVC, and pushVC’s evicted line out to L2 (if dirty/needed). This delivers the fast hit time of direct-mapped L1 with the low conflict rate of associativity. (Pseudo-associative caches achieve a related effect inside a single direct-mapped array by probing a primary index first and re-hashing to a secondary slot on a miss.)
2. Way-Predicting Caches (Reducing Set-Associative Hit Time)
In a set-associative cache (like the MIPS R10000 L2), reading and comparing all ways takes time and energy. A Way-Predicting Cache maintains a small prediction table indexed by the processor address that guesses which way holds the line:
- Look only in the predicted way first. If it matches: fast
HIT! - If it misses in the predicted way, check the remaining way(s). If another way matches:
SLOW HIT(return data and update the prediction table). If all ways miss:MISS(fetch block from next level). - In the Alpha 21264-like Way-Predicting Instruction Cache, each I-cache line stores two next-way predictions—a
Sequential Wayand aBranch Target Wayselected byJump control—enabling wide multi-instruction fetch per cycle at direct-mapped speed.
3. Early Restart and Critical Word First (Reducing Miss Penalty of Long Blocks)
Long cache lines (e.g., 64 bytes) save tag area, but waiting for the entire block to arrive word-by-word from DRAM increases Miss penalty. Instead of waiting for all 4 words (Word 0, Word 1, Word 2, Word 3) when the CPU asked for Word 2:
- Early Restart: Fetch words in normal order (
0, 1, 2, 3), and as soon asWord 2arrives, forward it immediately to restart the CPU whileWord 3finishes loading. - Critical Word First: Request the missed word
Word 2first from memory, send it straight to the CPU on arrival, and fill the remaining words in wrap-around order (Word 3, Word 0, Word 1) in the background. Because long blocks are universal today, Critical Word First is widely used.
4. Non-Blocking (Lockup-Free) Caches (Increasing Bandwidth & Hiding Miss Penalty)
A simple blocking cache freezes the entire CPU on any D-cache miss—even if the very next instruction hits in the D-cache! A Non-Blocking (Lockup-Free) Cache lets the D-cache keep servicing hits during an outstanding miss:
- Hit under miss: Requires Full/Empty bits on registers (in-order scoreboard) or out-of-order execution so independent instructions can run past the stalled consumer.
- Hit under multiple miss / Miss under miss: Overlaps multiple concurrent misses to lower effective miss penalty, which dramatically increases controller complexity—it must track multiple in-flight misses, merge secondary misses to a line already being fetched, and talk to a pipelined or banked memory system (e.g., Pentium Pro supported 4 outstanding misses; the Cray X1E vector supercomputer supported 2,048).
- Empirical SPEC92 measurements (
8 KBDM D$,32 Bblock,16-cycle miss) reveal who benefits most: Floating-Point programs (with lots of independent array streaming) saw AMAT plunge across0->1 -> 1->2 -> 2->64overlapping misses (0.68 -> 0.52 -> 0.34 -> 0.26), whereas Integer programs (heavily pointer-dependent) saturated almost immediately after 1 miss (0.24 -> 0.20 -> 0.19 -> 0.19).
✎ Check Your Understanding
Protocol check: In a direct-mapped L1 paired with a Victim Cache (
VC), what happens to the evicted L1 line when a requested address misses in L1 but hits in theVC? Answer: The two lines are swapped: the hit line moves from theVCinto L1, and the line evicted from L1 moves into that slot in theVC.Workload check: Why does increasing a non-blocking cache from
1outstanding miss to64outstanding misses slash AMAT dramatically for floating-point code (0.52 -> 0.26) while barely affecting integer code (0.20 -> 0.19)? Answer: Floating-point code contains long loops of independent array accesses that can overlap many memory misses in parallel, whereas integer code has tight pointer/control dependencies that quickly stall before many misses can accumulate.
Section 2: Reducing Miss Rate — Hardware/Software Prefetching and Compiler Locality Optimizations
Our final group of optimizations attacks Miss rate—specifically compulsory and capacity misses—by either fetching data before the CPU asks for it (Prefetching) or restructuring loops so data stays hot in cache (Compiler Optimizations).
Prefetching: Hardware and Software
Prefetching speculates on future instruction and data accesses and brings them into caches ahead of demand (instructions are much easier to predict than data). Every prefetcher must balance three issues:
- Usefulness: Prefetched blocks should actually turn into hits.
- Timeliness: Not too late (miss penalty not hidden) and not too early (evicted before use).
- Cache and bandwidth pollution: Speculative fetches consume memory bandwidth and can evict useful lines!
Hardware Instruction Prefetching (Alpha AXP 21064): On an I-cache miss to line i, fetch two lines: place i into L1I, and place `i+1` into a small separate **Stream Buffer** (avoiding polluting L1I if a branch jumps away!). If the next fetch misses L1I$ but hits the Stream Buffer, move i+1 into L1I$ and prefetch i+2.
Hardware Data Prefetching Schemes:
- Prefetch-on-miss: Prefetch block
b+1upon a miss tob. - One-Block Lookahead (OBL): Initiate prefetch for
b+1wheneverbis accessed (hit or miss; generalizes to N-block lookahead). How does OBL differ from simply doubling the cache block size? With OBL,bandb+1remain separate cache blocks (or sit in a stream buffer), avoiding extra conflict misses and allowing unused lookahead lines to be replaced independently without wasting space inside an oversized block! - Strided prefetch: Observe accesses to
b, b+N, b+2N\rightarrow prefetchb+3N, etc. - Spatial Memory Streaming (SMS): Fix a memory region size R; observe offsets
a, b, caccessed inside one region; later whena+Ris touched in a new region, immediately prefetchb+Randc+R. - Memory-dependent / Pointer-chasing (CDP, DMP): Observe pointer dereferences
p, *p, **p\rightarrow prefetch***p, ****p(modern Apple M-series chips combine multiple stride prefetchers, an SMS prefetcher, and a “stateless” data-memory-dependent prefetcher—though reading memory contents to drive prefetch addresses can also leak cryptographic secrets if not carefully isolated!).
Software Prefetching: Inserts explicit non-blocking prefetch(&a[i + P]); instructions into loops:
for (i = 0; i < N; i++) {
prefetch(&a[i + P]);
prefetch(&b[i + P]);
SUM = SUM + a[i] * b[i];
}Here, timing—not predictability—is the hardest problem: setting lookahead distance P too small arrives too late; setting P too large pollutes the cache; and guessing P is hard because DRAM/L2 latency varies dynamically—on top of the instruction-count overhead of executing extra prefetch instructions!
Compiler Optimizations for Locality
Compilers can eliminate misses with zero hardware cost by restructuring loops (or marking single-use streaming data with “no-allocate” hints / dead-line replacement so streaming data doesn’t evict reusable data):
Loop Interchange (Improves Spatial Locality): Suppose a 2D array
x[i][j]is stored in row-major order (wherex[i][0], x[i][1], ...sit in the same cache line):// Before: inner loop jumps down columns (stride N*sizeof(elem))! for (j = 0; j < N; j++) for (i = 0; i < M; i++) x[i][j] = 2 * x[i][j]; // After: inner loop walks across rows (stride 1 = full spatial locality!) for (i = 0; i < M; i++) for (j = 0; j < N; j++) x[i][j] = 2 * x[i][j];Swapping the loops turns strided column misses into sequential unit-stride cache hits.
Loop Fusion (Improves Temporal Locality):
// Before: first loop writes all of a[0..N-1], evicting early elements before loop 2! for (i = 0; i < N; i++) a[i] = b[i] * c[i]; for (i = 0; i < N; i++) d[i] = a[i] * c[i]; // After: a[i] and c[i] are reused immediately while still hot in cache/registers! for (i = 0; i < N; i++) { a[i] = b[i] * c[i]; d[i] = a[i] * c[i]; }Matrix Multiply Cache Tiling / Blocking (Improves Temporal Locality): In naive matrix multiply
x[i][j] += y[i][k] * z[k][j], each(i, j)innerk-loop sweeps across an entire row ofyand down an entire column ofz. For large N, all N^2 elements ofzexceed cache capacity, so every single iteration ofire-fetches the entire matrixzfrom DRAM! Cache Tiling breaks the computation into B \times B blocks (choosing B so the active B \times B tiles ofx,y, andzfit inside the cache at once):for (jj = 0; jj < N; jj = jj + B) for (kk = 0; kk < N; kk = kk + B) for (i = 0; i < N; i++) for (j = jj; j < min(jj + B, N); j++) { r = 0; for (k = kk; k < min(kk + B, N); k++) r = r + y[i][k] * z[k][j]; x[i][j] = x[i][j] + r; }Now the B \times B sub-matrix of
z(and tiles ofyandx) stays resident in cache across all N rows ofi, cutting DRAM traffic by roughly a factor of B!
[Editor Note: removed repeated roadmap slides, GoFetch administrative banner, and acknowledgements.]
✎ Check Your Understanding
Concept check: Why is a hardware One-Block Lookahead (
OBL) prefetcher using a stream buffer better than simply doubling the cache block size? Answer: Doubling the block size cuts the number of cache lines in half (increasing conflict misses) and wastes bandwidth when only part of a block is used, whereas OBL keeps smaller blocks and puts prefetched blocks in a stream buffer without polluting the cache.Locality classification: Identify which type of locality (spatial or temporal) is primarily improved by Loop Interchange, Loop Fusion, and Cache Tiling. Answer: Loop Interchange primarily improves spatial locality (unit-stride row access); Loop Fusion and Cache Tiling primarily improve temporal locality (reusing
a[i]/c[i]or a B \times B matrix tile while it remains in cache).