﻿# Register spilling: when thread-local values move to device memory

Register spilling can trade a smaller register footprint for device-memory traffic; evaluate the compiled kernel rather than counting source variables.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/learn-register-spill-local-memory/
Language: en

Thread-local state is a programming concept, not a guarantee that every value stays in physical registers. A compiler may place some values in local memory when register allocation or addressability requires it. That local memory is backed by device memory, making register pressure a possible source of additional memory instructions.

## A source variable has a lifetime

A value needs storage from the point where it becomes live until its final use. Many simultaneously live values increase allocation pressure, while a compiler may reuse a register after an earlier value dies. Counting declared variables therefore misses the central relationship. Arrays indexed in a way that needs addressable storage can also affect placement. The compiled kernel, including optimization and specialization, determines which storage instructions actually execute.

Primary reference: [CUDA Programming Guide: Writing SIMT Kernels](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/writing-cuda-kernels.html).

## Unrolling can enlarge the live set

Imagine a kernel that accumulates several partial results while unrolling an inner loop. More independent arithmetic can expose instruction parallelism, but the retained partial results need storage. Past a resource boundary, extra loads and stores may appear. This hypothetical example explains why a transformation can remove loop overhead and still lose time elsewhere. Its complete cost includes instruction scheduling, register allocation and the memory operations introduced by the compiler.

Primary reference: [CUDA C++ Best Practices Guide](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/).

## Local memory is not shared memory

Local memory belongs logically to an individual thread, but that ownership says nothing about on-chip residency. Shared memory is a cooperative scratchpad with a different allocation and addressing model. Moving data between these forms requires an algorithmic design; renaming a variable does not move it into faster storage. Cache behavior may influence local-memory cost, so observed latency should not be equated automatically with a full external-memory round trip for every access.

Primary reference: [Nsight Compute Profiling Guide](https://docs.nvidia.com/nsight-compute/ProfilingGuide/index.html).

## A register limit changes several outcomes

Constraining registers can allow more blocks to fit while making each block execute more memory work. Allowing more registers can reverse that tradeoff. Neither setting is a general optimization rule. Inspect the compiled resource count and relevant local-memory instructions for each variant, then connect them with observed work. A higher occupancy result is only part of the evidence when the method used to obtain it changes the kernel’s instruction stream.

## Preserve the numerical operation

Compare alternative loop organizations on identical inputs and check the accepted numerical result. Separate changes in arithmetic order from changes in allocation pressure. Record compiler options, generated resources and execution evidence, because a different build can make a different placement decision. The resulting explanation should identify which live values or addressing patterns caused the expensive path, allowing a focused redesign instead of a blind reduction of every local variable.

Related reading: [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/); [Reproducible inference stacks: preserving a GPU pilot’s result](/journal/software-stack-reproducibility/).
