Skip to content

Warn when launch-bounded kernels spill registers - #757

Merged
nihalpasham merged 12 commits into
NVIDIA:mainfrom
uurl:feat/launch-bounds-spill-warning-708
Aug 10, 2026
Merged

nihalpasham merged 12 commits into
NVIDIA:mainfrom
uurl:feat/launch-bounds-spill-warning-708

Conversation

@uurl

@uurl uurl commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #756.
Please review/merge #756 first.
Refs #708.

Summary

Adds a rustc warning when a kernel compiled with #[launch_bounds] reports register spills during native cubin materialization.

This builds on the resource-reporting infrastructure introduced in #756 and addresses the warning path described in #708.

Changes

  • preserve per-kernel launch-bound metadata from the MIR importer through device codegen;
  • associate launch bounds with exported kernel names;
  • collect ptxas resource usage during cubin materialization;
  • detect kernels with non-zero spill stores or spill loads;
  • emit a span-based warning for launch-bounded kernels that spill registers;
  • include spill store/load bytes and register usage when available;
  • provide remediation guidance based on whether min_blocks_per_sm was specified.

Example diagnostic:

warning: kernel `foo` compiled with `#[launch_bounds(256, 2)]` and spills registers
note: ptxas reports 32 bytes spill stores and 32 bytes spill loads
note: ptxas allocated 64 registers per thread
help: relax `min_blocks_per_sm` or reduce register pressure

The warning intentionally does not claim that #[launch_bounds] caused the spill. It reports only the facts available from compilation: the kernel has launch bounds and ptxas reported register spilling.

Scope

The diagnostic is emitted when native cubin materialization runs, because that is the stage where ptxas resource information becomes available.

Normal PTX-only compilation does not run ptxas, so it cannot provide exact spill information.

Testing

Validated with:

cargo fmt --all
cargo check
cargo test
cargo clippy --all-targets -- -D warnings
git diff --check

@uurl
uurl force-pushed the feat/launch-bounds-spill-warning-708 branch from 198afdc to 8f3bfaf Compare August 9, 2026 10:41
@uurl
uurl force-pushed the feat/launch-bounds-spill-warning-708 branch from 8f3bfaf to e498f9b Compare August 9, 2026 10:53
nihalpasham and others added 9 commits August 10, 2026 08:52
…OPTION

Expose NvJitLinkError::is_unrecognized_option() so callers passing
optional, non-semantic options can detect an older nvJitLink that
predates them and retry without them. The result code is checked live
against the installed toolkit by a new ignored test that feeds
nvJitLinkCreate a bogus option.

Signed-off-by: Nihal Pasham <[email protected]>
…inated kernel names

Capture the 'N bytes stack frame' figure that sits on the same ptxas
'Function properties' line as the spill counters: local-memory frame
size is distinct from spill traffic and issue NVIDIA#707's census needs it.
Also unwrap kernel names in log shapes that terminate the line with a
colon after a quoted name ("Function properties for '_Z3fooPi':"),
which previously left a stray ': suffix on the parsed name.

Signed-off-by: Nihal Pasham <[email protected]>
…nized diagnostic options

Early 12.x nvJitLink versions reject options they do not know with
NVJITLINK_ERROR_UNRECOGNIZED_OPTION, which would fail the whole link
just because the caller asked for a best-effort report. When the
with-report link fails that way, retry once without the diagnostic
flags and return the linked image with an empty report. The retry runs
inside the same revalidated-tool-identity window as the first attempt,
and the plain link path is unaffected.

Signed-off-by: Nihal Pasham <[email protected]>
…ecting a resource report

Observed live on CUDA 13.3: a warm nvJitLink link serves the cubin from
its own JIT cache, skips ptxas entirely, and replays no info log, so
link_ltoir_with_report silently returned an empty resource report on
every repeat link. Add -no-cache to the cubin diagnostic option set so
the report always observes a real compile. The produced image bytes are
identical, and the flag stays out of artifact digests with the other
reporting options.

Signed-off-by: Nihal Pasham <[email protected]>
…t format

Add an ignored live test in the crate's live_tests style: build
legacy-dialect NVVM IR for a kernel that must spill (64 mutually
entangled live floats under a maxnreg=32 cap), compile it to LTOIR,
link with link_ltoir_with_report, and assert the info log is present
and parses to plausible register, stack-frame, and spill figures. The
generated IR anchors the kernel with @llvm.used exactly as cuda-oxide's
NVVM exporter does; without it nvJitLink dead-strips the kernel and the
log carries no resource lines.

On this host (CUDA 13.3, sm_86 target) the log reads:
  ptxas info    : Compiling entry function 'spill_kernel' for 'sm_86'
  ptxas info    : Function properties for spill_kernel
  ptxas         .     360 bytes stack frame, 476 bytes spill stores, 584 bytes spill loads
  ptxas info    : Used 32 registers, ...
and parses to registers=32, stack_frame=360, spills=476/584.

Signed-off-by: Nihal Pasham <[email protected]>
device_codegen.rs references mir_importer::KernelLaunchBounds, but the
explicit pipeline re-export list omitted it, so the rustc-codegen-cuda
crate (outside the root workspace) failed to build.
… warning

The launch-bounds spill warning is a raw span diagnostic, not a lint, so
#[allow] cannot suppress it. Builds that measured a spill and accepted
it need an escape hatch; presence-based parsing matches the existing
CUDA_OXIDE_NO_OPT convention.
…s only in .note.nv.tkinfo

Verified on CUDA 13.3 / sm_120: linking the same LTOIR with and without
the diagnostic options yields cubins whose sections are byte-identical
except .note.nv.tkinfo, where ptxas records its own option line (-v).
The runtime fallback in cuda-host therefore produces code-identical,
not byte-identical, images.
@nihalpasham
nihalpasham force-pushed the feat/launch-bounds-spill-warning-708 branch from e498f9b to eae5c66 Compare August 10, 2026 04:00
@nihalpasham nihalpasham added enhancement New feature or request QoL-improvement Quality-of-life improvement; not a correctness fix codegen Device code-generation pipeline (Rust MIR to IR to PTX) build-related Build scripts, toolchain discovery, or compilation setup labels Aug 10, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Gist: ptxas's --warn-on-spills diagnostic lifted to a rustc span warning, gated on #[launch_bounds], at the only stage that has the mangled name, the span, and the ptxas text all in hand, which is good; but the backend crate didn't compile as submitted, rustc-codegen-cuda sits outside the root workspace so the stated validation never built it.

I force-pushed a restack onto the updated #756 plus three commits:

  • the missing KernelLaunchBounds re-export (the compile break), a CUDA_OXIDE_NO_SPILL_WARN escape hatch since span warnings can't be allowed away, and a doc note recording that reported and
  • plain links are code-identical (SASS equal; only ptxas's self-recorded option line in .note.nv.tkinfo differs, and that section is outside the digest).
  • A fourth commit satisfies the edition-2024 fmt gate.

Verified on an RTX 5090 with CUDA 13.3:

  • a deliberately spilling launch-bounded kernel produces exactly one warning whose 428/436 spill bytes match a manual ptxas -v run;
  • the non-spilling kernel is silent; workspace, clippy, and the finalizer's live tests all green.

Merging after #756.

Partially addresses #708 within the disclosed --materialize-cubin scope; related to #707.

# Conflicts:
#	crates/cuda-artifact-finalizer/src/link.rs
@nihalpasham
nihalpasham merged commit 749b25b into NVIDIA:main Aug 10, 2026
38 checks passed
@uurl
uurl deleted the feat/launch-bounds-spill-warning-708 branch August 10, 2026 06:24
nihalpasham added a commit that referenced this pull request Aug 11, 2026
…atch (#798)

* docs(book): document the launch-bounds spill warning and its escape hatch

#757 taught the backend to warn when a `#[launch_bounds]` kernel spills
registers. The warning is user-facing and nothing in the book mentions it,
so the first time anyone meets it is in their own build output.

Two things make that worse than an ordinary documentation gap:

  * The build still succeeds. A bound that ptxas can only satisfy by
    spilling to local memory is a silent trade -- occupancy bought, then
    paid for again on every access to a spilled value -- and the warning is
    the only signal.
  * `#[allow(...)]` does not silence it. These are raw span diagnostics
    rather than lints, so the attribute a Rust user reaches for first does
    nothing, and the only escape hatch is `CUDA_OXIDE_NO_SPILL_WARN=1`,
    which appears in no documentation at all.

So the `#[launch_bounds]` section now carries the diagnostic's actual
shape, what it means, the usual responses, and the environment variable --
framed for builds that measured the spill and accepted it, which is the
intent recorded on `emit_launch_bounds_spill_warnings`. It also says
plainly that spilling is not automatically wrong, since a kernel can be
faster spilling a little at high occupancy than not spilling at low.

Every message string is quoted from `crates/rustc-codegen-cuda/src/lib.rs`
rather than paraphrased; the byte and register figures are illustrative and
labelled as such. Verified with `sphinx-build -W --keep-going`.

Note for triage: issue #708 asked for this warning and is still open even
though #757 merged it, so it looks like another squash-merge where the
`Closes` did not fire.

Signed-off-by: Vyncint Ng <[email protected]>

* docs(book): note the spill warning requires cubin materialization

The warning is emitted from write_device_artifact_object only when a
materialized cubin exists; plain PTX builds never run ptxas, so there
is no resource report to inspect. Say so, and show --materialize-cubin
in the escape-hatch example command.

---------

Signed-off-by: Vyncint Ng <[email protected]>
Co-authored-by: nihalpasham <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-related Build scripts, toolchain discovery, or compilation setup codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request QoL-improvement Quality-of-life improvement; not a correctness fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants