Skip to content

Emit .reqntid for kernels declaring an exact launch-contract block - #514

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/reqntid-exact-block
Jul 28, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/reqntid-exact-block

Conversation

@alejandro-soto-franco

@alejandro-soto-franco alejandro-soto-franco commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

#[launch_contract(block = (x, y, z))] binds a launch to one block shape, and
that shape was known only to the host. prepare_* checked it and returned a
typed error on a mismatch, while a raw _unchecked launch, or a module whose
PTX reached the driver by another route, went unchecked.

This carries the shape into the compiled artifact as .reqntid x, y, z, which
the CUDA driver enforces per axis at every launch. Preparation and the driver
check the same requirement on independent paths.

What changes

#[launch_contract] injects __launch_contract_block_config::<X, Y, Z>()
alongside the existing contract marker. The MIR importer reads it, mir-lower
propagates reqntid_x/y/z, and llvm-export emits the matching
!nvvm.annotations entries.

.maxntid and .reqntid cannot appear on one entry; ptxas rejects the pair
with "Conflicting directives: .maxntid and .reqntid cannot both be specified".
A kernel declaring an exact block therefore emits .reqntid in place of the
thread maximum. .minnctapersm is independent and still emitted.

KernelLaunchBounds carries a KernelBlockGeometry enum (Exact from a
launch-contract block, Max from #[launch_bounds], or absent), so the
exporter picks .reqntid or .maxntid from the geometry's provenance
rather than a bare thread count.

Correcting the emitted geometry

.maxntid bounds the product x * y * z and says nothing per axis. Every
non-1-D contract was emitting .maxntid N, 1, 1, declaring one thread on Y for
blocks of 8 or 16. The driver checks only the product, so this was harmless,
and it is replaced by the real shape.

Rejecting a contradictory pair

A #[launch_bounds(max)] below the exact block's thread count is now an error.
The exact block displaces the maximum in the emitted PTX, so the maximum would
otherwise be dropped in silence and the kernel would launch at a shape its
author ruled out. #[cuda_module] already diagnosed this at the attribute
site. The check added here sits in the MIR importer, which sees both markers
regardless of attribute order, and covers standalone contracted kernels that
reach the device compiler without passing through the module path. A maximum
at or above the required count is redundant and stays allowed.

Change in behaviour

An unsafe raw launch of a contracted kernel with a differing block shape now
fails with a driver error. The checked path already rejected that launch.

Verification

  • .reqntid is fail-closed: an out-of-range shape makes ptxas warn and the
    driver reject every launch.
  • SASS is byte-identical between .reqntid and the .maxntid it replaces, at
    identical register counts and zero spills, for all four gemm_views kernels.
    The whole cubin difference is an EIATTR_REQNTID record in place of
    EIATTR_MAX_THREADS.
  • The cuda_module_contract, gemm_views and proof_carrying_views PTX
    harnesses assert the expected directive per entry, and that no entry declares
    both.

#[launch_contract(block = (x, y, z))] binds a launch to one block shape, and
that shape reached only the host. Carry it into the compiled artifact so the
CUDA driver enforces it per axis at every launch, including raw _unchecked
launches that never pass through preparation.

#[launch_contract] injects __launch_contract_block_config::<X, Y, Z>(), the
MIR importer reads it, mir-lower propagates reqntid_x/y/z, and llvm-export
emits the matching nvvm.annotations entries. An exact shape displaces the
thread maximum, since ptxas rejects an entry carrying both .maxntid and
.reqntid. .minnctapersm is independent and still emitted.

Reject a #[launch_bounds(max)] below the exact block's thread count, which
would otherwise be dropped in silence. #[cuda_module] already diagnoses this
at the attribute site, so the check added here covers standalone contracted
kernels, which reach the device compiler by another path.

Model the two directives as an enum in the exporter, so an entry cannot carry
both by construction.
…kernel entries

A #[launch_contract(block = (x, y, z))] written above #[kernel] expands
into a __launch_contract_block_config marker before the generic entry
wrapper is generated. The marker-forwarding filter did not recognize it,
so generic and explicitly instantiated kernels kept .maxntid instead of
the contracted .reqntid. Add the marker to the plain-marker arm of
is_kernel_configuration_marker so pre-expanded contracts reach the
generated entry, and flip the cuda_module_contract expectations for
generic_aligned and explicit_aligned_u32 to .reqntid.

The wrapper and the inlined helper may then both carry the marker; the
importer's conflict check already accepts equal duplicate shapes and
rejects only differing ones.
@nihalpasham nihalpasham added enhancement New feature or request safety Memory safety, soundness, or undefined behavior codegen Device code-generation pipeline (Rust MIR to IR to PTX) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) labels Jul 28, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks,

Gist: the exact block shape now reaches the artifact as .reqntid, so the driver enforces the contract per axis even on raw launches that bypass preparation.

Validated on 2x RTX 5090 (sm_120, CUDA 13.3):

  • full workspace suite (2997 tests),
  • GPU smoketests plus PTX harnesses for cuda_module_contract / gemm_views / proof_carrying_views, ptxas accepting .reqntid alongside .minnctapersm (and rejecting .maxntid+.reqntid, as the exporter enum guarantees by construction),
  • a driver-API harness confirming mismatched blocks fail with a clean CUDA_ERROR_INVALID_VALUE on every axis, and libNVVM accepting the reqntid annotations on both the modern (sm_120) and legacy LLVM7 (sm_86) dialect paths.

I added one maintainer commit on top:

  • that forwards the __launch_contract_block_config marker to generic and explicitly instantiated kernel entries, so contracts written above #[kernel] also emit .reqntid (generic_aligned and explicit_aligned_u32 expectations flipped accordingly).

One note: the PR description says max_threads becomes Option<u32>; the code (rightly) models a KernelBlockGeometry enum instead - I updated the description to match.

Related to #516, which this narrows for exact-block contracts but does not close.

@nihalpasham
nihalpasham merged commit 73301f6 into NVIDIA:main Jul 28, 2026
33 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the feat/reqntid-exact-block branch September 9, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants