Skip to main content

Register Spilling and Occupancy Cliffs in CUDA

Explore live ranges, spill paths, whole-block occupancy cliffs, and compiler evidence through five interactive diagrams and a reproducible CUDA probe.

One more register per thread can cost an entire resident block. In the model below, moving from 64 to 65 registers at 256 threads per block drops residency from four blocks to three. That is an analytical threshold, not a measured slowdown. To explain a real kernel, separate three questions: what stays live, how many blocks fit, and what the compiled code actually does.

Live values need storage

A value is live between its definition and last use. Values with overlapping live ranges need distinct storage; non-overlapping ranges can reuse it. Switch schedules and scrub the instruction boundary to see that reuse in a small illustrative computation.

Illustrative liveness model
Schedule
5 / 8

store (a + b) × (c + d)

1
ld
2
ld
3
ld
4
ld
5
+
6
+
7
mul
8
st
a
b
c
d
p
q
r
  1. 1. a = load
  2. 2. b = load
  3. 3. c = load
  4. 4. d = load
  5. 5. p = a + b
  6. 6. q = c + d
  7. 7. r = p * q
  8. 8. store r

Boundary 5 sits before instruction 6. A value is live from its definition until the instruction that last uses it, then dies after that read.

R0
p
reused a
R1
free
R2
c
R3
d
live rangeselected boundarydead or free

Toy register reuse after the source read; addresses, predicates, and instruction temporaries are excluded from the live-value count.

Illustrative liveness for Load all four, then combine. Boundary 5, before instruction 6. Live values c, d, p. Peak simultaneous live values 4.

Source variables do not map one-to-one to machine registers. The compiler can reorder work, recompute values, or spill them. PTX virtual registers are not the final allocation in SASS. Longer live ranges can preserve independent work within a warp, but they also put pressure on the register budget.

Follow a spilled value

A spill saves a value outside its register so that register can hold something else; a reload brings the value back for use. Step through the path, then switch the reload scenario to see why a local-memory access need not reach device memory.

Illustrative default local-memory spill path
1 / 5
Reload scenario
One thread’s illustrative registers4 registers
R0
other
R1
other
R2
t
R3
other
spill store ↓
reload ↑
Thread-private local address space
slot 0empty

An address in this space is per thread. The caches and device memory below are physical storage, not the address space itself.

L1 cacheconditional

May serve a hit.

L2 cacheconditional

Can serve an L1 miss.

Device memorybacking

Not every access reaches it.

t = a * b. t occupies R2.

active value or pathidle or conditional

Illustrative service scenarios, not a cache-policy prediction or a latency scale; stores need not immediately reach device memory.

Illustrative spill path, Define t. R2 t. R3 other. Local slot empty. No reload service path highlighted.

Local memory is a thread-private address space, backed by device memory and caches. Arrays and dynamic indexing can use it without any register spill, so a local allocation alone does not establish spilling. CUDA Programming Guide: local memory

Whole blocks create occupancy cliffs

Occupancy is resident active warps divided by the architecture's maximum. Blocks consume resources in whole units, so the result changes in steps. Select 64 and 65 registers, then switch from 256 to 512 threads per block: the same per-thread change produces a larger drop. CUDA Best Practices Guide: occupancy

Occupancy explorer
64 registers
64/65 preset
Threads / block
Shared / block

Illustrative analytical model, not measured hardware occupancy.

Register capacity 65,536; whole blocks at scale1234Next whole block (dashed) does not fit the register budget16,384 regsOccupancy after all modeled limits0%25%50%75%100%1664/6596128160registers per thread64 regs: 50%
resident blockregister-fit, held by another limitunused register budgetnext block cannot fit
4 blocks fit the register budget. 4 resident blocks under all modeled limits: 32 of 64 warps, 50% occupancy. Limiting: registers.
At this block and shared-memory size:64 regs 4 resident, 50%65 regs 3 resident, 37.5%

Active warp slots

32 / 64

Resource limits

Registers
4 blocks
Shared memory
nonbinding
Warps
8 blocks
Blocks
32 blocks

ceil(64 × 32 / 256) × 256 = 2,048 regs/warp; 2,048 × 8 warps = 16,384 regs/block

Accessible data view
Occupancy at selected register counts under the fixed illustrative SM model
Registers / threadOccupancy
16100%
6450%
6537.5%
12825%
16012.5%

Illustrative points use only register, shared-memory, warp and block limits; subpartition placement, shared-memory rounding, architecture caps, and shared-spilling fallback are outside the model, as are spills and runtime.

Shared memory or another limit can stop additional blocks from becoming resident even when registers remain. More resident warps can help hide stalls; more registers can let one warp overlap independent work. Neither occupancy nor a register count alone predicts runtime.

Model assumptions, equations, and architecture differences

The model has 65,536 32-bit registers per SM (256 KiB of storage), 64 resident warps, 32 resident blocks, 32 threads per warp, 256-register allocation units per warp, and 96 KiB shared memory. These are fixed illustrative budgets, not a hardware occupancy calculator.

For WW warps per block, rr registers per thread, and SS shared bytes per block:

Rw=256⌈32r256⌉Rb=WRwBR=⌊65536Rb⌋BS=⌊98304S⌋BW=⌊64W⌋B=min⁡(BR,BS,BW,32)\begin{aligned} R_w &= 256\left\lceil \frac{32r}{256} \right\rceil \\ R_b &= W R_w \\ B_R &= \left\lfloor \frac{65536}{R_b} \right\rfloor \\ B_S &= \left\lfloor \frac{98304}{S} \right\rfloor \\ B_W &= \left\lfloor \frac{64}{W} \right\rfloor \\ B &= \min(B_R, B_S, B_W, 32) \end{aligned}

RwR_w and RbR_b are allocated registers per warp and block. The limits count whole blocks; shared memory is nonbinding when S=0S=0. Occupancy is BW/64BW/64. At 65 registers per thread, rounding raises 2,080 registers per warp to 2,304. At 256 threads, that means 18,432 registers per block and three resident blocks, or 37.5% occupancy.

The model omits SM subpartition placement, architecture-specific allocation details, shared-memory rounding, and per-block resource caps. It predicts neither spills nor latency. Ampere CC8.0 supports 64 resident warps and 32 blocks per SM; CC8.6 supports 48 warps and 16 blocks. Both have 65,536 32-bit registers. Ampere tuning guide

Allocation granularity also varies. The best-practices guide's CC7.0 example uses 37 registers per thread: 128-thread blocks allow 12 resident blocks and 75% occupancy, while 320-thread blocks allow four and 62.5%. Different block sizes turn the same per-thread allocation into different whole-block costs.

Compare two builds of the same work

The runnable CUDA probe processes 65,536 outputs. Each output sums LIVE_VALUES recurrence states; each state runs 64 dependent updates, while different states' recurrences are independent. The example README documents the workload and reporting fields.

Keep LIVE_VALUES=32 and change only the register cap:

code
nvcc -O3 -lineinfo -std=c++17 -arch=sm_80 -Xptxas=-v -DLIVE_VALUES=32 examples/register-spilling/occupancy_probe.cu -o probe nvcc -O3 -lineinfo -std=c++17 -arch=sm_80 -Xptxas=-v -DLIVE_VALUES=32 --maxrregcount=32 examples/register-spilling/occupancy_probe.cu -o probe_capped

Set -arch for your GPU. Use an optimized build without -G, which changes compilation. The figure records the compiler reports from these builds with CUDA 12.3.52; select a report field to compare its values. No GPU execution or timing is recorded here.

Compiler cap evidence
Config
Row
Recorded compiler evidence, not runtime measurementsCUDA 12.3.52sm_80LIVE_VALUES=32

Bars scale within each row.

Registers / threadreg
Uncapped40 registers
Cap 3232 registers
Stack frameB
Uncapped0 bytes
Cap 3248 bytes
Spill storesB
Uncapped0 bytes
Cap 3276 bytes
Spill loadsB
Uncapped0 bytes
Cap 3280 bytes

Registers, stack, and spill rows are compiler report fields. This figure does not add the byte rows, query runtime attributes, or read them as local-memory traffic.

Uncapped reports 40 registers on the Registers / thread row. The other report shows 32 registers.

UncappedCap 32

Reproduce with the article’s nvcc command; another toolkit or target can produce different output.

The cap reduced the recorded allocation and introduced reported spills. It does not establish a speedup or slowdown. Compiler spill bytes are static accounting, not bytes transferred per launch. Another toolkit or target can produce different output.

Run both binaries with identical inputs, block size, and repetitions:

code
./probe 256 100 ./probe_capped 256 100 nvidia-smi --query-gpu=name,driver_version,memory.total --format=csv nvcc --version

The arguments are threads per block (128, 256, or 512; default 256) and repetitions (default 100). The probe validates every output against a CPU reference, runs three warmups, and times a group of launches with CUDA events. It reports the average GPU timeline interval per launch: allocation, transfers, and validation are excluded, but idle gaps between host-submitted launches can remain. Keep block and grid sizes fixed between the two builds.

Workload and reported resource fields

The kernel reads coalesced grid-stride input and uses constant-index, unrolled element loops over a fixed-size per-thread array. LIVE_VALUES accepts 8, 16, or 32. Changing it changes the workload; those timings would not compare equal work. The source uses no launch bounds and does not force a 64- or 65-register allocation. The compiler decides what stays live, spills, or is recomputed. Every CUDA call is checked; numerical validation must pass before timing.

cudaFuncGetAttributes reports numRegs per thread, localSizeBytes per thread, and sharedSizeBytes of static shared memory per block, excluding dynamic shared memory. The probe also reports maxThreadsPerBlock and calls cudaOccupancyMaxActiveBlocksPerMultiprocessor for the compiled function, selected block size, and zero dynamic shared bytes. That API accounts for static kernel resources. Function attributes, occupancy API

Selected output field names, not measured values:

code
device_name compute_capability cuda_runtime_version block_threads grid_blocks repetitions kernel_num_regs kernel_local_size_bytes kernel_static_shared_size_bytes kernel_max_threads_per_block theoretical_active_blocks_per_sm_from_api validation timing_average_kernel_interval_ms
Register caps and launch bounds are different controls

--maxrregcount bounds register usage subject to the ABI minimum. It does not promise an exact allocation. A smaller budget may add recomputation, instructions, or spills, and can reduce instruction-level parallelism. nvcc: maxrregcount

__launch_bounds__(maxThreadsPerBlock, minBlocksPerMultiprocessor) supplies compiler constraints, not a residency guarantee. Exceeding the maximum launch block size fails. The compiler may increase registers up to the derived limit to preserve instruction-level parallelism. It ignores --maxrregcount on kernels with launch bounds, and __maxnreg__ cannot combine with launch bounds. Keep launch bounds out of this comparison; test them separately. CUDA Programming Guide: launch bounds

Explain a timing change with evidence

Follow the evidence from the build to execution:

  1. Compiler report: save -Xptxas=-v output, the command, toolkit version, and target.
  2. Machine code: inspect cuobjdump -sass probe for changed loads, recomputation, and control flow.
  3. Loaded function: query resource attributes and the occupancy API instead of substituting the illustrative model.
  4. Profile: inspect resource limiters, active and eligible warps, and local-memory traffic together.

Discover available sections with ncu --list-sections and metrics with ncu --query-metrics. Skip the validation launch and three warmups, then collect one matching launch:

code
ncu --kernel-name regex:OccupancyProbeKernel --launch-skip 4 --launch-count 1 --section LaunchStats --section Occupancy --section SchedulerStats --section MemoryWorkloadAnalysis ./probe 256 100

Theoretical occupancy is a resource upper bound; achieved occupancy describes execution. Active warps may be stalled; eligible warps are ready to issue. Profiling replay, cache control, and clocks can change timing, so benchmark separately and use the profile to explain the result. Nsight Compute profiling guide, CLI reference

CUDA 13 can opt into shared-memory spilling

CUDA 13.0 added .pragma enable_smem_spilling for whole-program compilation. The compiler can place spills in shared memory before falling back to local memory. That shared-memory reservation can itself limit residency. The default-path diagram and CUDA 12.3 build comparison above do not enable this feature. NVIDIA: shared-memory register spilling

PTX ISA 9.5 documents introduction in PTX 9.0, a requirement of sm_75 or newer, and restrictions involving per-function and separate compilation, device-debug, recursive and EWP modes, dynamic shared memory, and setmaxnreg. Explicit launch bounds are recommended. Check the toolkit and target before using the pragma. PTX ISA: enable_smem_spilling

Check the compiled Triton specialization

Triton's fused-softmax tutorial pads each row to the next power of two. Select 1024 and 1025 logical elements to see the padded extent change. This policy illustrates a shape boundary; it does not predict a register count. Triton fused softmax tutorial

Triton tile padding
Logical width
Illustrative next-power-of-two tile policyFixed scale: 0 to 2048 elements
Logical extent1025 elements
Padded tile2048 elements

1023 masked elements

01024 boundary2048
Valid logical elementsMasked padding elements1024 boundary

Illustrative tile policy only; not all Triton kernels use it, and elements are not registers, threads, instructions, or memory traffic.

Warm up the specialization, inspect n_regs and metadata.shared, and inspect the generated code. Tile shape, num_warps, and num_stages can change resources without a monotonic effect on occupancy or time. Tensor elements do not correspond one-to-one to registers. Validate and benchmark each configuration separately.

Keep the comparison reproducible

Record the GPU and compute capability; driver, toolkit, and profiler versions; source revision and build flags; LIVE_VALUES; block, grid, and shared-memory sizes; validation; queried resources; and the timing boundary. For profiles, also retain sections, launch count, replay mode, and clock settings.

Keep analytical calculations, compiler observations, runtime resource queries, and timing in separate columns. The example README provides the commands and field definitions needed to rebuild the comparison.

Abhik Sarkar

Abhik Sarkar

Machine Learning Consultant specializing in Computer Vision and Deep Learning. Leading ML teams and building innovative solutions.

Share this article

If you found this article helpful, consider sharing it with your network

Mastodon