Skip to main content

Module kernel_args

Module kernel_args 

Source
Expand description

Typed argv lowering for the wasi:cuda launch host function.

Guests writing GPU kernels through TensorWasm’s wasi-cuda ABI pass kernel parameters as a single opaque byte buffer (args_ptr, args_len). To turn that buffer into the void**-style array CUDA’s cuLaunchKernel expects, the host needs a frozen per-argument packing format.

§Wire format

The buffer is a sequence of (tag, value) records. Each record starts with a single tag byte (ArgTag) followed by the value bytes in little-endian order. There is no inter-argument padding — the host reads exactly tag.size() bytes after each tag byte, which matches how a guest authored against this ABI would memcpy values into a &mut [u8]. (CUDA’s cuLaunchKernel itself doesn’t care about host-side padding because each arg pointer is independently addressed.)

TagTypeValue bytesWire size (incl. tag)
0x01i3245
0x02i6489
0x03f3245
0x04f6489
0x05u3245
0x06u6489
0x07ptr4 (guest ptr) + 4 (guest byte len)9

Pointer arguments carry both the guest-memory offset and the byte length the kernel will access. The host bounds-checks the [ptr, ptr+len) window against the caller’s linear memory before producing any device pointer — the same posture as the rest of the wasi-cuda host fns. On CUDA builds the resolved host pointer feeds directly into the cuLaunchKernel argv (CUDA Unified Memory makes managed host pointers valid as device pointers); on no-CUDA builds the resolved pointer is captured for inspection but never dereferenced as a device address.

§Sanity caps

Both caps exist so a malformed-but-in-bounds buffer cannot pin arbitrary host memory inside the parser. Buffers that exceed either cap surface as AbiError::KernelArgsUnsupported, reusing the existing code for “the host won’t accept this shape” — distinct from AbiError::InvalidArgs (the tag bytes are malformed) and from AbiError::InvalidPointer (the guest pointer is OOB).

See wit/wasi-cuda.wit and docs/RISKS.md for the public contract.

Structs§

KernelParamStorage
Storage backing the void** array cuLaunchKernel expects.

Enums§

ArgTag
Tag byte identifying a single kernel-argument record’s type.
LoweredArg
A single fully-parsed and bounds-checked kernel argument.
LoweredArgSnapshot
Pointer-free, freely-Send/Sync snapshot of a LoweredArg.

Constants§

MAX_KERNEL_ARGS
Hard cap on the number of argv records the host will accept per launch.
MAX_KERNEL_ARGS_BYTES
Hard cap on the total wire-format buffer length the host will parse.

Functions§

build_kernel_param_storage
Build a KernelParamStorage from a previously-parsed Vec<LoweredArg>.
encode_argv
Helper for tests and host-stub paths: encode a sequence of LoweredArg back into the wire format.
parse_argv
Parse a tagged-argv byte buffer into a host-side Vec<LoweredArg>.