# Register Spilling and Occupancy Cliffs in CUDA

> Source: <https://www.abhik.ai/articles/register-spilling-occupancy-cliffs>
> Published: 2026-12-02 00:00:00+00:00

**Recommendation:** Keep per-thread register counts at or below the block-granularity threshold (64 registers at 256 threads per block) to preserve four resident blocks; one extra register drops occupancy to 37.5%.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

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](#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.

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

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.

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](#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.

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

May serve a hit.

Can serve an L1 miss.

Not every access reaches it.

t = a * b. t occupies R2.

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](https://docs.nvidia.com/cuda/archive/12.6.3/cuda-c-programming-guide/index.html#local-memory)

## [Whole blocks create occupancy cliffs](#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](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#occupancy)

Illustrative analytical model, not measured hardware occupancy.

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 / thread | Occupancy | 
|---|---|
| 16 | 100% | 
| 64 | 50% | 
| 65 | 37.5% | 
| 128 | 25% | 
| 160 | 12.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 warps per block, registers per thread, and shared bytes per block:

and are allocated registers per warp and block. The limits count whole blocks; shared memory is nonbinding when . Occupancy is . 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](https://docs.nvidia.com/cuda/ampere-tuning-guide/index.html#occupancy)

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](#compare-two-builds-of-the-same-work)

The runnable [CUDA probe](https://github.com/abhiksark/Abhik-Portfolio/blob/main/examples/register-spilling/occupancy_probe.cu) 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](https://github.com/abhiksark/Abhik-Portfolio/blob/main/examples/register-spilling/README.md) documents the workload and reporting fields.

Keep `LIVE_VALUES=32` and change only the register cap:

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.**

Bars scale within each row.

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.

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:

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](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/structcudaFuncAttributes.html), [occupancy API](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__OCCUPANCY.html)

Selected output field names, not measured values:

## 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](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#maxrregcount-amount-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](https://docs.nvidia.com/cuda/archive/12.6.3/cuda-c-programming-guide/index.html#launch-bounds)

## [Explain a timing change with evidence](#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:

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](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html#occupancy), [CLI reference](https://docs.nvidia.com/nsight-compute/NsightComputeCli/index.html)

## 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](https://developer.nvidia.com/blog/how-to-improve-cuda-kernel-performance-with-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](https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#pragma-enable-smem-spilling)

## [Check the compiled Triton specialization](#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](https://triton-lang.org/main/getting-started/tutorials/02-fused-softmax.html)

1023 masked elements

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](#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.
