Repository navigation
Description
What problem does this solve?
When a small fixed-size local array is indexed by a value the compiler cannot see as constant, SROA cannot promote it and it is placed in local memory. On a CPU that is a stack slot and usually cheap. On a GPU it is a performance cliff, and there is currently no signal that it happened short of reading ptxas -v output or the PTX itself.
This cost us 31% of one of our hottest kernels, unnoticed for months. The pattern:
let ok = idx.extend(&p, b); // -> [Sai; 4], 128 bytes
if ok[c].size < min_occ { break } // `c` is a query base -- a RUNTIME index
p = ok[c];What made it hard to find is that the cost is invisible at the definition. extend itself reports a 0 byte stack frame — every index inside it is a literal, so SROA promotes it fine — and the entire penalty lands at the call sites, which is the last place you look after reading a clean callee. Restructuring so the runtime index is resolved once into four literal-index calls gave, on sm_75:
| kernel | stack frame | registers |
|---|---|---|
seed_intv_kernel |
464 -> 144 B | 138 -> 126 |
seed_intv_forward_kernel |
304 -> 272 B | 122 -> 110 |
smem_seed |
432 -> 400 B | 104 -> 92 |
and took that kernel's device time down 31.5% (3.624/3.627/3.635/3.643 s -> 2.474/2.491/2.496/2.491 s, 4 interleaved reps per arm, disjoint). It also brought the register count under the threshold for 2 blocks/SM with zero spill, which was a second win on top.
We found it by taking a full ptxas -v census across every kernel and asking which ones had nonzero stack frames — not by reading the source, which looks entirely reasonable.
Proposed API / behavior
A diagnostic, off by default or behind a flag, along the lines of:
warning: local `ok` ([Sai; 4], 128 bytes) could not be promoted to registers
--> src/walker.rs:214:9
= note: indexed by a non-constant value; the allocation is placed in local memory
Even a summary at the end of codegen — per kernel, which locals stayed in local memory and how large — would be enough to make this findable. Today it surfaces only as an unexplained N bytes stack frame line from the assembler, with no mapping back to a Rust binding.
I appreciate this is a request about diagnostics rather than a bug, and that the mapping from a surviving alloca back to a source-level binding may not be as cheap as it sounds from outside. Treat the specific rendering above as illustrative.
Alternatives considered
Reading ptxas -v per kernel and bisecting by hand, which is how we found it. That gives you a number but not a location, and the number is attributed to the kernel entry rather than to the inlined call site that caused it.
Additional context
Related in spirit to the #[launch_bounds] spill warning filed alongside this: both are cases where the register allocator lost and the only feedback is an aggregate byte count in assembler output.
Activity
feat(cuda-artifact-finalizer): parse stack-frame bytes and colon-term…
Expose ptxas resource diagnostics from native finalization (#756)
Warn when launch-bounded kernels spill registers (#757)