Repository navigation
Emit .reqntid for kernels declaring an exact launch-contract block - #514
Merged
nihalpasham merged 2 commits intoJul 28, 2026
Merged
Conversation
#[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.
Collaborator
|
Thanks, Gist: the exact block shape now reaches the artifact as Validated on 2x RTX 5090 (sm_120, CUDA 13.3):
I added one maintainer commit on top:
One note: the PR description says Related to #516, which this narrows for exact-block contracts but does not close. |
nihalpasham
approved these changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#[launch_contract(block = (x, y, z))]binds a launch to one block shape, andthat shape was known only to the host.
prepare_*checked it and returned atyped error on a mismatch, while a raw
_uncheckedlaunch, or a module whosePTX reached the driver by another route, went unchecked.
This carries the shape into the compiled artifact as
.reqntid x, y, z, whichthe 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.annotationsentries..maxntidand.reqntidcannot appear on one entry; ptxas rejects the pairwith "Conflicting directives: .maxntid and .reqntid cannot both be specified".
A kernel declaring an exact block therefore emits
.reqntidin place of thethread maximum.
.minnctapersmis independent and still emitted.KernelLaunchBoundscarries aKernelBlockGeometryenum (Exactfrom alaunch-contract block,
Maxfrom#[launch_bounds], or absent), so theexporter picks
.reqntidor.maxntidfrom the geometry's provenancerather than a bare thread count.
Correcting the emitted geometry
.maxntidbounds the productx * y * zand says nothing per axis. Everynon-1-D contract was emitting
.maxntid N, 1, 1, declaring one thread on Y forblocks 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 attributesite. 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
unsaferaw launch of a contracted kernel with a differing block shape nowfails with a driver error. The checked path already rejected that launch.
Verification
.reqntidis fail-closed: an out-of-range shape makes ptxas warn and thedriver reject every launch.
.reqntidand the.maxntidit replaces, atidentical register counts and zero spills, for all four
gemm_viewskernels.The whole cubin difference is an
EIATTR_REQNTIDrecord in place ofEIATTR_MAX_THREADS.cuda_module_contract,gemm_viewsandproof_carrying_viewsPTXharnesses assert the expected directive per entry, and that no entry declares
both.