Skip to content

[BUG]: enum with a bool payload overlapping another variant's payload fails device codegen ("refusing to expose non-canonical bool bits", mir-lower enum slot map) #1889

Backlog

Description

@ubmaxxer

Description

A data-carrying enum in which one variant holds a bool and another variant holds a wider field at the same offset fails device codegen with enum slot map: E scalar bool field N overlaps storage other than one exact i8 byte; refusing to expose non-canonical bool bits. This happens for every explicit repr (#[repr(C)], #[repr(i32)], …) and also for plain repr(Rust) enums whenever rustc's layout places the bool over another variant's integer, for example enum E { V0((u8, i64), E0), V1(i32, bool), V2 }. The same program compiles and runs on the host. Both device routes fail, since the check is in mir-lower's enum slot map before route selection. The guard is sound (it refuses rather than read non-0/1 bits as a bool), but it rejects ordinary safe Rust; the bool leaf could be routed through the byte-level fallback (load the i8, icmp ne 0) instead. Reproduced on main at a412e09; the affected code is unchanged at the current main (8b91bb7).

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: `EI` scalar bool field 1 overlaps storage other than one exact i8 byte; refusing to expose non-canonical bool bits

Shapes checked, one kernel per build:

enum device build
enum ER { A(u32, bool), B(i32, i32) } (repr(Rust)) ok (rustc happens to keep the bool clear of the i32)
#[repr(i32)] enum EI { A(u32, bool), B(i32, i32) } fails
#[repr(C)] enum EC { A(u32, bool), B(i32, i32) } fails
enum E { V0((u8, i64), E0), V1(i32, bool), V2 } (repr(Rust), E0 a two-variant enum) fails
enum EB { A(bool), B(u32) } ok
enum EO { A(bool), B } ok

Expected behavior

The kernel compiles and prints the value the host computes (6 for the reproducer below). A bool stored in bytes that another variant uses for an integer is still a valid bool whenever the discriminant says the bool variant is active, so the field can be loaded as one byte and compared to zero.

Minimal reproduction example

use cuda_core::{CudaContext, DeviceBuffer, LaunchConfig};
use cuda_device::{DisjointSlice, cuda_module, kernel, thread};

#[derive(Clone, Copy)]
#[repr(i32)]
pub enum EI { A(u32, bool), B(i32, i32) }

#[inline(never)]
pub fn fi(e: EI) -> u64 {
    match e { EI::A(x, b) => x as u64 + b as u64, EI::B(x, y) => (x as u64) ^ (y as u64) }
}

#[cuda_module]
mod kernels {
    use super::*;
    #[kernel]
    pub fn k(e: EI, mut out: DisjointSlice<u64>) {
        if let Some(s) = out.get_mut(thread::index_1d()) { *s = fi(e); }
    }
}

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, EI::A(5, true), &mut out) }.unwrap();
    println!("{}", out.to_host_vec(&stream).unwrap()[0]); // host: 6
}

// 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 mir-lower.

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 pipeline stops in mir-lower's enum slot map (cuda-oxide/crates/mir-lower/src/convert/types/enum_layout.rs, the "non-canonical bool bits" guard, at a412e09). With a primitive or C repr the payloads form a union after the tag, so A.1: bool shares bytes with B.1: i32; the slot map gives each leaf a typed slot and refuses when a bool leaf's byte is covered by a wider slot of another type. repr(Rust) enums hit the same guard whenever rustc's layout produces that overlap, which is common once a variant carries a nested enum or a tuple before the bool.

Suggested fix. Treat such a bool leaf like any other byte-addressed field: load the i8 at its offset through the memory round-trip the slot map already uses for colliding slots, and materialize the bool with icmp ne i8 %b, 0 (the active-variant check already guarantees the byte is 0 or 1 when it is read as a bool).

This issue was found with a fuzzer; the reproducer above was reduced from its test case. The report was written with LLM assistance.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions