L2 Intermediate · Module 6: GEMM and memory optimization

6.3Register pressure, spilling and the occupancy trade-off

Registers are the fastest storage on a GPU, and every thread on an SM draws from one fixed budget. Using more per thread means fewer threads fit; using fewer than the code needs makes the compiler spill to slow local memory. This chapter measures both sides of that trade-off on one kernel, and the result is lopsided.

GPU required (Colab or a rented GPU)~20 min

By the end you will be able to

  • ✓Compute how many blocks fit per SM from registers per thread
  • ✓Read register and spill counts from -Xptxas -v and cudaFuncGetAttributes
  • ✓Use __launch_bounds__ and -maxrregcount, and predict when they cause spills
  • ✓Decide between higher occupancy and more work per thread

An Ampere SM has 65,536 32-bit registers, shared by every thread resident on it, and at most 255 per thread. The register-tiled matmul in 6.2 wanted 64 accumulators per thread plus inputs and addresses. Where does that budget go, and what happens when it runs out?

Registers decide occupancy

Occupancy is the fraction of the SM’s thread slots (1,536 on this GPU) that are filled (2.5). The register file is one of its limits:

blocks per SM≤⌊65,536registers per thread×threads per block⌋\text{blocks per SM} \le \left\lfloor \frac{65{,}536}{\text{registers per thread} \times \text{threads per block}} \right\rfloor

(rounded, because registers are allocated in chunks of 256 per warp). At 256 threads per block:

Registers per threadBlocks per SMOccupancy (of 1,536 threads)
64467%
80350%
128233%
209117%

Shared memory per block and the hardware block limit cap occupancy too. Whichever limit is tightest wins.

Seeing the numbers

Two ways to find out what a kernel uses:

Terminal window
nvcc -O2 -arch=native -Xptxas -v register_pressure.cu ...
# ptxas info : Used 127 registers, used 1 barriers, 8192 bytes smem, 380 bytes cmem[0]
# ptxas info : Used 64 registers, 512 bytes cumulative stack size, ...
# 512 bytes stack frame, 1372 bytes spill stores, 1272 bytes spill loads

or at run time with cudaFuncGetAttributes(&attr, kernel), which reports attr.numRegs and attr.localSizeBytes, plus cudaOccupancyMaxActiveBlocksPerMultiprocessor for blocks per SM. The lab prints all three.

Spilling

If a thread needs more registers than it is allowed, the compiler spills: it stores some values to local memory and reloads them when needed. Local memory is private to the thread but physically lives in DRAM, cached in L1 and L2. A spilled value costs a load and a store where a register cost nothing.

You can cap registers in two ways:

// per kernel: "I will launch 256 threads and want at least 3 blocks per SM"
__global__ void __launch_bounds__(256, 3) mm(...);
Terminal window
nvcc -maxrregcount=64 ... # every kernel in the file

__launch_bounds__(256, 3) tells the compiler to fit 3×2563 \times 256 threads, so it caps registers at about 65,536/768≈8565{,}536 / 768 \approx 85, and spills whatever doesn’t fit.

Measured: one kernel, seven builds

The lab builds the 6.2 kernel with different per-thread tiles and different bounds:

output (RTX 3050 6GB Laptop)
4096 x 4096 FP32 matmul, one kernel built seven ways:
per-thread tile, bound regs spilled blocks/SM occupancy time GFLOP/s
4x4, no bound 78 0 B 3 50% 52.99 ms 2594
8x4, no bound 96 0 B 2 33% 44.44 ms 3093
8x8, no bound 127 0 B 2 33% 35.46 ms 3876
8x8, min 2 blocks/SM 127 0 B 2 33% 35.22 ms 3903
8x8, min 3 blocks/SM 80 168 B 3 50% 161.65 ms 850
8x8, min 4 blocks/SM 64 512 B 4 67% 1319.28 ms 104
16x8, no bound 209 0 B 2 17% 39.64 ms 3467
cuBLAS 30.17 ms 4555

Read it in two directions.

More work per thread wins, up to a point. From a 4×4 to an 8×8 thread tile, registers grow from 78 to 127 and occupancy falls from 50% to 33%, yet the kernel gets 1.5× faster. Each loaded value feeds more FMAs, and the extra independent accumulators give each warp its own latency hiding (6.6). At 16×8 the tile is too large: 209 registers, a 128-thread block, 17% occupancy, and it is slower again.

Forcing occupancy up is a disaster. Asking for 3 blocks per SM caps the 8×8 kernel at 80 registers, which spills 168 bytes per thread and makes it 4.6× slower. At 4 blocks per SM, 512 bytes spill (the whole accumulator array goes to local memory) and it is 37× slower, even though occupancy doubled. In a loop that does 64 FMAs per few loads, adding a local-memory load and store to each FMA destroys everything register tiling bought.

Where registers go

Common reasons a kernel uses more registers than you expect:

  • Unrolled loops keep many values alive at once. That is usually the point, but full unrolling of a long loop can overshoot.
  • 64-bit addresses. Every pointer takes two registers, and every precomputed address another two. Recomputing addresses from one base pointer can save many.
  • Arrays indexed at run time don’t live in registers at all; they go to local memory even with no spill reported in the “spill” line, and show up in the stack frame size.
  • Inlined device functions and math library calls (expf, sinf) bring their temporaries with them.

Lab: the occupancy trade-off 🔴

Terminal window
cd labs/m06/6.3 && nvcc -O2 -arch=native -DUSE_CUBLAS -Xptxas -v register_pressure.cu -o register_pressure -lcublas && ./register_pressure

Sources

book · FREGLYAI Systems Performance EngineeringChris FreglyRegister pressure, spilling, launch bounds and the occupancy trade-off.
repo · BGEBEFORE-GPU-ENGINEERINGBEFORE-GPU-ENGINEERINGPhase 3, Ch 36–51: registers as the top of the memory hierarchy.

Check your understanding

0 / 4 answered
  1. 1.A kernel uses 96 registers per thread with 256-thread blocks. How many blocks fit in 65,536 registers?

  2. 2.What does __launch_bounds__(256, 4) ask the compiler to do?

  3. 3.In the lab, raising occupancy from 33% to 67% made the matmul 37× slower. Why?

  4. 4.Where does spilled data live?