Skip to content

fix(llvm-export): refuse a kernel parameter pointing into shared or local memory - #772

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/blackwell-conversions-verified
Aug 10, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
alejandro-soto-franco:feat/blackwell-conversions-verified

Conversation

@alejandro-soto-franco

Copy link
Copy Markdown
Contributor

Closes #771.

A kernel parameter pointing into shared memory reached the entry signature as .ptr .shared. ptxas assembles that, and the driver then refuses the module, so every kernel in it becomes unreachable. generated_intrinsics_blackwell contains such a kernel, which is why the module that example builds cannot be loaded on the architecture it pins.

Summary

Refuses a kernel parameter in a state space the host cannot supply, repairs the kernel that had one, and checks the conversions on the device rather than only compiling them.

Changes

  • crates/llvm-export/src/export/function.rs: a kernel parameter that is a pointer into shared or local memory is refused, naming the kernel, the parameter index and the space.
  • crates/rustc-codegen-cuda/examples/generated_intrinsics_blackwell/src/main.rs: compile_tma_compatibility declares its barrier as a shared static mut rather than taking one as a parameter, and the example launches the packed-FP8 and TF32 kernels and checks all 42 results against the two formats.

Rationale

Shared memory is allocated per block and local memory per thread, both by the device at launch, so the host holds no address in either to put in a parameter. Global and constant are left alone: the host allocates there, a pointer parameter in those spaces is something it can pass, and the libdevice linking tests in cuda-oxide-codegen rely on exactly that. A device function may carry any state space on its parameters, so the check is for kernels alone.

The example's kernel could never have been launched. Removing the parameter costs nothing, since a barrier has to live in shared memory anyway, and examples/barrier already declares one as a static mut inside the kernel.

Removing that kernel is what makes the module load. With it repaired, the conversion kernels run, which is what the rest of this change uses. Their results are bit-exact, so there is no tolerance to pick, and every expected value derives from the format:

value e4m3 e5m2 why
1.0 0x38 0x3C exponent 0, zero mantissa
2.0 0x40 0x40 exponent 1
-1.0 0xB8 0xBC sign bit over 1.0
448.0 0x7E 0x5F 1.75 x 2^8, the largest finite e4m3
1e30 0x7E 0x7B saturates to the largest finite value

TF32 keeps the f32 exponent and truncates the mantissa to 10 bits, so each input chosen is already representable and all ten rounding forms agree, except the four relu forms on a negative input. The relu cases pin the argument order too: cvt_rn_satfinite_relu_e4m3x2_f32(-1.0, 448.0) gives 0x7e00, which is the low lane zeroed and the high lane kept, matching the documented "first argument in the low byte".

The example stays compile-only in CI, since it pins sm_120a and no runner has that device. On one that does, the conversions are now verified rather than assumed.

Validation

RTX 5060 Laptop GPU, compute capability 12.0, CUDA 13.3, driver 610.43.03, target sm_120a.

Before this change, on that device:

DriverError(200, "device kernel image is invalid")

After it, the example reports what the device computed:

  e4m3x2       (         1,          2) = 0x4038  want 0x4038  ok
  relu_e4m3x2  (        -1,        448) = 0x7e00  want 0x7e00  ok
  e5m2x2       (      1e30,          0) = 0x007b  want 0x007b  ok
  tf32[3]      (      -3.5)             = 0x00000000  want 0x00000000  ok
  ...
PASS: every conversion matched, and the sparse MMA and TMA kernels compiled

42 checks, all passing. Changing one expected constant reports MISMATCH and exits 1, so a wrong result is caught.

The refusal was narrowed by the test suite rather than by reading: refusing every non-generic space broke three libdevice_linking tests whose kernels take a global pointer, which is legitimate.

cargo test --workspace                              exit 0, no failing test
scripts/smoketest.sh --compile-only                 191 / 191
cargo clippy -- -D warnings (the example)           clean
cargo fmt --check                                   clean

The reproducer from #771 now fails to compile with:

kernel `@compile_tma_compatibility` parameter 2 is a pointer into shared memory (address
space 3); shared memory is allocated by the device at launch, so the host has no address
to pass, and the driver refuses a module whose entry declares one

alejandro-soto-franco and others added 2 commits August 9, 2026 09:41
…ocal memory

A kernel receives its parameters in .param space, filled by the host at
launch. Shared memory is allocated per block and local memory per thread,
both by the device, so the host holds no address in either to pass. The
address space nonetheless reached the entry signature as `.ptr .shared`,
ptxas assembled it, and the driver then refused the module, taking every
other kernel in it down.

generated_intrinsics_blackwell had such a kernel, so the module that
example builds could not be loaded on the sm_120a it pins. Nothing caught
it: the example is compile-only, and no CI runner has the device.

Global and constant are left alone. The host allocates there, so a pointer
parameter in those spaces is one it can supply, and the libdevice linking
tests depend on it. Refusing every non-generic space broke three of them,
which is how the rule was narrowed. A device function may carry any state
space on its parameters, so this is checked for kernels alone.

compile_tma_compatibility now declares its barrier as a shared static mut,
the way examples/barrier already does, rather than taking one as a
parameter it could never have been given.

With the module loading, the example checks what it had only been
compiling: the packed-FP8 and TF32 kernels are launched and all 42 results
compared against the two formats, both being bit-exact. Every expected
value derives from the format rather than from a previous run, and the
relu cases pin the documented argument order, the first argument landing
in the low byte. The example stays compile-only in CI, which has no
Blackwell device to run it on.
The refusal landed without an automated test. Pin it at both layers.

error_kernel_shared_param is a new error-category example: a #[kernel]
taking *mut Barrier, which lowers to an addrspace(3) parameter. Its
smoketest verdict greps for 'is a pointer into shared memory', so an
unrelated compile failure cannot satisfy the fixture. The kernel body
deliberately does nothing but keep the parameter, so the signature
refusal is the only error it can produce. Wired into ERROR_EXAMPLES
and STATUS.md; check-error-example-status.sh and
check-example-smoketest-contract.sh both pass.

export_test.rs pins the exporter check directly: an addrspace(3) and
an addrspace(5) kernel parameter are refused by index and space name,
generic/global/constant kernel parameters stay accepted, and a device
(non-kernel) function keeps a shared parameter.

Also spell out the deliberate scope at the check itself: addrspace(4)
constant parameters stay allowed by design, and addrspace(7)
(shared::cluster) is not user-reachable as a parameter type today; it
would need a new arm if a frontend type ever surfaces one at a kernel
boundary.
@nihalpasham nihalpasham added bug Something isn't working llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate) codegen Device code-generation pipeline (Rust MIR to IR to PTX) labels Aug 10, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Verified end-to-end on sm_120:

  • the repaired generated_intrinsics_blackwell module now loads and all 42 FP8/TF32 conversion checks pass.

Pushed one fixup:

  • adding the missing regression coverage: an error_kernel_shared_param error example with the diagnostic checked in smoketest,
  • plus three export_test.rs unit tests covering the AS3/AS5 refusal and its scope.

Full workspace tests and the 192-example compile-only smoketest pass.

@nihalpasham
nihalpasham merged commit 9604db6 into NVIDIA:main Aug 10, 2026
36 checks passed
@alejandro-soto-franco
alejandro-soto-franco deleted the feat/blackwell-conversions-verified branch September 9, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working codegen Device code-generation pipeline (Rust MIR to IR to PTX) llvm-export Textual LLVM/NVVM IR exporter (llvm-export crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A kernel parameter pointing into shared memory emits an entry the driver refuses to load

2 participants