On a CPU, the main cost of unrolling and inlining is code size. On a GPU, you pay in registers. Each SIMD has a fixed-size register file shared by all the waves resident on it, so the more registers each thread uses, the fewer waves can be resident at once. The number of resident waves is called occupancy, and it’s how a GPU hides memory latency: when one wave stalls on a load, the scheduler switches to another.
This post is day 3 of Stream HPC’s Advent of Kernel Optimization, posting every weekday until the 25th of December. Check out all posts so far here.
Occupancy is a step function of register use. On an AMD Instinct MI300X (gfx942), a kernel that uses 64 VGPRs or fewer gets 8 waves per SIMD; at 102 VGPRs, it gets only 4. On the AMD Radeon RX 9070 XT (gfx1201) used below, up to 96 VGPRs gets 16 waves per SIMD, 128 gets 10, and 192 gets 8. Compiling with -Rpass-analysis=kernel-resource-usage prints these numbers for every kernel, along with its scratch and LDS usage.
Unrolling a register tile
The classic use of unrolling on a GPU is register tiling. Each thread computes a T×T tile of an outer product and keeps its accumulators in a local array.
template <int T>
__global__ void outer_product(const float* __restrict__ a, const float* __restrict__ b,
float* __restrict__ c, int k, int ld) {
const int row0 = (blockIdx.y * blockDim.y + threadIdx.y) * T;
const int col0 = (blockIdx.x * blockDim.x + threadIdx.x) * T;
float acc[T][T] = {};
for (int p = 0; p < k; ++p) {
float ra[T], rb[T];
#pragma unroll
for (int i = 0; i < T; ++i) ra[i] = a[p * ld + row0 + i];
#pragma unroll
for (int j = 0; j < T; ++j) rb[j] = b[p * ld + col0 + j];
#pragma unroll
for (int i = 0; i < T; ++i)
#pragma unroll
for (int j = 0; j < T; ++j) acc[i][j] += ra[i] * rb[j];
}
#pragma unroll
for (int i = 0; i < T; ++i)
#pragma unroll
for (int j = 0; j < T; ++j) c[(row0 + i) * ld + col0 + j] = acc[i][j];
}Everything inside the p loop, and the final store loop, is fully unrolled, so every index into acc, ra, and rb is a compile-time constant. Each iteration of p does loads and FMAs, so larger tiles reuse each loaded value more often.
Here are the results on the RX 9070 XT, with 16×16 blocks, a 3072×3072 output, and k = 512 (ROCm 10.1, mean of N runs). The Rolled build replaces every pragma with #pragma unroll 1. The VGPR, scratch, and occupancy columns are for the unrolled build; the last column shows where the rolled build puts its private arrays.
Results
| T | VGPRs | Scratch (B/lane) | Waves/SIMD | Unrolled | Rolled | Rolled: scratch / LDS |
|---|---|---|---|---|---|---|
| 2 | 18 | 0 | 16 | 3.18 ms | 6.94 ms | 0 B / 16 KiB |
| 4 | 37 | 0 | 16 | 1.55 ms | 42.98 ms | 0 B / 64 KiB |
| 8 | 85 | 0 | 16 | 1.16 ms | 20.80 ms | 272 B / 0 |
| 12 | 173 | 0 | 8 | 1.11 ms | 19.45 ms | 592 B / 0 |
| 16 | 192 | 464 | 8 | 10.13 ms | 20.89 ms | 1040 B / 0 |
Four things to notice:
- Rolled loops don’t degrade gracefully. A private array indexed by a runtime value can only stay in registers if it’s small: the compiler can turn each access into a chain of selects or an indexed register move, which is what happens to
raandrbhere. Larger arrays, such asacc, go to scratch: per-lane memory backed by VRAM. At , that’s 272 bytes per lane and an 18× slowdown compared with the unrolled loop. - Lower occupancy was fine. halves occupancy and is still the fastest. In each iteration, every thread issues 24 loads and 144 independent FMAs, so each wave hides much of its own latency. Vasily Volkov made this case in his 2010 GTC talk, Better Performance at Lower Occupancy.
- The cliff is spilling. At , the compiler runs out of registers, spills 464 bytes per lane, and runs 9× slower than . Unlike the rolled builds, this is true register spilling: the kernel metadata reports 292 spilled VGPRs.
- Scratch isn’t the only cost. Rolled reports no scratch, yet it’s the slowest of all. The compiler moved
accinto LDS, sized for a 1024-thread block (64 KiB), so every FMA becomes an LDS load, a wait, the FMA itself, and an LDS store. Adding__launch_bounds__(256)shrinks the LDS allocation to 16 KiB but barely changes the runtime, so the cost is the round trip, not occupancy. When you read the remarks, check theLDS Sizeline, not justScratchSize.
Raising the budget with launch bounds
The 192-VGPR ceiling at isn’t a hardware limit. Without __launch_bounds__, HIP assumes a block may have up to 1024 threads, and caps register use so that all the waves of such a block can be resident at once. In RDNA, a 1024-thread block is 32 waves spread across the four SIMDs of a WGP, or 8 waves per SIMD, and the SIMD’s 1536 VGPRs divided among 8 waves leaves 192 each. A 256-thread block needs only 2 waves per SIMD, so the cap rises to the per-wave maximum of 256. The GPU can only address 256 VGPRs per wave, hence the register usage is .
Our blocks have 256 threads, so adding __launch_bounds__(256) lets T=16 use all 256 VGPRs. Occupancy falls from 8 to 5 waves per SIMD, but scratch shrinks to 300 bytes per lane, and the runtime drops from 10.13 ms to 3.58 ms. That’s a 2.8× speedup from one attribute, although it still loses to . Passing --gpu-max-threads-per-block=256 to the compiler has the same effect for every kernel in the file.
Make sure the bound matches your launch: launching a block larger than __launch_bounds__ allows fails at run time.
The effect is even larger on
gfx942. Its waves are 64 lanes wide, so a 1024-thread block is 16 waves, or 4 per SIMD, which caps each wave at 128 VGPRs. Without launch bounds, the tile already spills at T=12 (228 bytes per lane) and (700 bytes per lane). With__launch_bounds__(256), neither spills: uses 178 VGPRs at 2 waves per SIMD, and uses 256 VGPRs at 1 wave per SIMD.
Why noinline doesn’t save registers
Real function calls are expensive on a GPU. The calling convention dictates which registers each side must save, and the call stack lives in scratch. That’s why AMDGPU multiplies the inlining threshold by 11, and raises it further when a call passes private arrays, which would otherwise end up in scratch.
On a CPU, the instinct is to mark a large, cold path noinline to keep it out of the hot code:
__device__ __noinline__ float rare_fixup(const float* __restrict__ p, int i) {
float v[32];
// Reads up to p[i + 31 * 64], so the buffer must be padded.
#pragma unroll
for (int j = 0; j < 32; ++j) v[j] = p[i + j * 64];
float s = 0.f;
#pragma unroll
for (int j = 0; j < 32; ++j)
#pragma unroll
for (int k = j; k < 32; ++k) s += v[j] * v[k];
return s;
}
__global__ void scale(const float* __restrict__ in, float* __restrict__ out, int fixup) {
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
float x = in[tid];
if (fixup) x += rare_fixup(in, tid);
out[tid] = 2.f * x;
}Here’s the resource usage on gfx942, compared with the same code marked __forceinline__:
| VGPRs | SGPRs | Waves/SIMD | |
|---|---|---|---|
__forceinline__ | 37 | 15 | 8 |
__noinline__ | 65 | 39 | 7 |
The launch fixes a kernel’s register count, and that count covers the kernel’s whole call graph. The callee’s registers count even if fixup is always zero, just as they would if the code were inlined. Worse, as a separate function, rare_fixup can’t assume that p points to global memory, so it falls back to flat loads, each with its own 64-bit address. That alone costs 65 VGPRs, versus 37 for the whole inlined kernel. On gfx1201, the gap is smaller (35 VGPRs inlined vs. 38 out of line, with no change in occupancy) but goes the same way. If a cold path really hurts, move it into its own kernel.
Conclusion
Unrolling and inlining are enabling optimizations, and on a GPU they’re close to mandatory: they’re what lets private arrays live in registers at all. What changes is the bill: VGPRs and LDS set occupancy, and past the budget you pay in scratch, which costs far more than extra code size does on a CPU.
The compiler’s heuristics don’t know your launch configuration or which paths are cold. Read the resource-usage remarks, including the scratch and LDS lines, set __launch_bounds__, and measure: the kernel with lower occupancy can still be the fastest.