Repository navigation
Description
Description
An Option<P> where P is a #[repr(packed)] struct whose fields include a niche carrier (here an Option<bool> field) fails device codegen in mir-lower's enum slot map with enum slot map: Option lowered to 24 bytes but rustc says 21. rustc gives Option<P> the packed struct's own size (21 bytes, niche in the Option<bool> field); the slot map builds the enum's storage from the natural (padded, aligned) layout of the payload, gets 24, and fails closed on the mismatch. Option of a packed struct without a niche, an Option field inside a packed(2) struct, and Option of an ordinary struct with the same fields all compile and run. The same program compiles and runs on the host; both device routes fail. Reproduced on main at a412e09; mir-lower is unchanged at the current main (b9e9c24).
Error / actual behavior
$ cargo oxide run repro
error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Lowering failed: Compilation error: invalid input program.
enum slot map: `Option` lowered to 24 bytes but rustc says 21
| shape | device build |
|---|---|
Option<P2>, P2 = #[repr(C, packed)] { Option<i64>, u16, i16, Option<bool> } (21 bytes, niche) — opt_packed_niche below |
fails |
Option<P1>, P1 = #[repr(packed)] { u8, u32 } (no niche: tag + payload) |
ok, correct |
Option<u32> as a field of a #[repr(packed(2))] struct |
ok, correct |
Option<S>, S a plain struct with a bool field (niche) |
ok, correct |
Expected behavior
The kernel compiles and prints the payload's f1 (5), as the host does. The enum storage for a packed payload should follow rustc's layout (21 bytes, align 1, niche in the payload), as the by-value packed struct paths already do.
Minimal reproduction example
use cuda_core::{CudaContext, DeviceBuffer, LaunchConfig};
use cuda_device::{DisjointSlice, cuda_module, device, kernel, thread};
#[derive(Clone, Copy)]
#[repr(C, packed)]
pub struct P2 { pub f0: Option<i64>, pub f1: u16, pub f2: i16, pub f3: Option<bool> } // 21 bytes, align 1
#[device]
#[inline(never)]
pub fn opt_packed_niche(o: Option<P2>) -> u64 { // rustc: Option<P2> is 21 bytes (niche in f3)
match o { Some(p) => { let f1 = p.f1; f1 as u64 } None => 7 }
}
#[cuda_module]
mod kernels {
use super::*;
#[kernel]
pub fn k(x: u32, mut out: DisjointSlice<u64>) {
let o = if x > 1 { Some(P2 { f0: Some(3), f1: x as u16, f2: 1, f3: Some(true) }) } else { None };
let h = opt_packed_niche(o);
if let Some(s) = out.get_mut(thread::index_1d()) { *s = h; }
}
}
fn main() {
let ctx = CudaContext::new(0).unwrap();
let stream = ctx.default_stream();
let module = kernels::load(&ctx).unwrap();
let cfg = LaunchConfig { grid_dim: (1, 1, 1), block_dim: (1, 1, 1), shared_mem_bytes: 0 };
let mut out = DeviceBuffer::<u64>::zeroed(&stream, 1).unwrap();
unsafe { module.k(&stream, cfg, 5u32, &mut out) }.unwrap();
println!("{}", out.to_host_vec(&stream).unwrap()[0]); // host: 5
}
// Standalone example crate; Cargo.toml as examples/vecadd. Run with `cargo oxide run <crate>`.Operating System
Ubuntu 24.04.3 LTS (Linux 6.14.0-29-generic)
GPU and GPU Architecture
NVIDIA GeForce RTX 4070 (sm_89). Not GPU-dependent: the failure is at build time, in the importer's verifier.
CUDA driver version
580.178.04
cargo oxide doctor
cargo-oxide environment check
==============================
Rust toolchain... ✓ rustc 1.99.1 (58cf8f9c3 2026-10-13)
rust-toolchain.toml... ✓ channel 1.99.1
Pinned toolchain active... ✓ 1.99.1-x86_64-unknown-linux-gnu
Required rustup components... ✓ rust-src, rustc-dev, rust-analyzer, clippy, rustfmt, llvm-tools
Codegen backend... ✓ <repo>/cuda-oxide/crates/rustc-codegen-cuda/target/x86_64-unknown-linux-gnu/debug/librustc_codegen_cuda.so
Project config (.cargo/cuda-oxide.toml)... - not present (using defaults)
CUDA headers (cuda.h)... ✓ /usr/local/cuda-13.3/include/cuda.h (discovered; neither CUDA_TOOLKIT_PATH nor CUDA_HOME is set)
CUDA toolkit (nvcc)... ✓ Cuda compilation tools, release 12.0, V12.0.140 (/bin/nvcc)
libNVVM (libnvvm.so)... ✓ libNVVM 2.0
nvJitLink (libnvJitLink.so)... ✓ nvJitLink 13.3
libdevice (libdevice.10.bc)... ✓ /usr/local/cuda/nvvm/libdevice/libdevice.10.bc
llc (LLVM)... ✓ LLVM version 23.1.1-rust-1.99.1-stable ($RUSTUP_HOME/toolchains/1.99.1-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/llc)
clang / libclang resource dir... ✓ /usr/lib/llvm-18/lib/clang/18 (via clang)
NVIDIA driver / GPU... ✓ NVIDIA GeForce RTX 4070 (compute capability 8.9, driver 580.178.04)
cuda-gdb (optional)... ✓ NVIDIA (R) CUDA Debugger (/bin/cuda-gdb)
compute-sanitizer (optional)... ✓ Version 2022.4.1 (/bin/compute-sanitizer)
wild linker (optional)... - not found (only needed for CUDA_OXIDE_LINKER=wild)
✅ Environment looks good!
rustc version
rustc 1.99.1 (58cf8f9c3 2026-10-13), host x86_64-unknown-linux-gnu, LLVM version 23.1.1
llc version
LLVM version 23.1.1-rust-1.99.1-stable (rustup llvm-tools component)
Additional context
No .ll or .ptx is produced; the check is the enum slot map in mir-lower's enum layout. Related shapes produce sibling messages from the same checker on other programs: `Option` lowered with alignment 8 but rustc requires 2; explicit over-aligned enum storage is not yet supported (Option of a packed(2) struct holding a u64), `Option` field 0 requires alignment 8 but rustc offset 4 is not aligned; packed enum payload access is not yet supported (Option<u64> as a field of a packed struct), and `Option` carrier (size 8, align 8) cannot sit at byte 2 of 18. The reproducer above is the one reduced to a single function; the others are listed so the fix can cover the family.
This issue was found with a fuzzer; the reproducer above was reduced from its test case. The report was written with LLM assistance.
Activity