Skip to content

[BUG]: ptr::read_unaligned of a bool (or a struct containing one) fails device codegen ("Transmute via memory round-trip: source aggregate … is not byte-faithful", mir-lower) #1899

Backlog

Description

@ubmaxxer

Description

core::ptr::read_unaligned of a value whose type contains a bool fails device codegen in mir-lower with Transmute via memory round-trip: source aggregate llvm.struct <{ builtin.integer i1 }> is not byte-faithful. read_unaligned copies the bytes into a MaybeUninit<T> and then assume_inits it; that transmute is lowered as a store/load round trip through memory, and the path refuses any aggregate holding an LLVM i1, because the memory image of an i1 is not the byte image of a Rust bool. core::ptr::read of the same value takes a typed load and works; read_unaligned of a u32 works. read_unaligned is the idiomatic way to pull a record out of a byte buffer, so any record with a flag field is affected. The same program compiles and runs on the host; both device routes fail since the failure is in the lowering shared by both. 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.
       Transmute via memory round-trip: source aggregate llvm.struct <{ builtin.integer i1 } : Unpacked> is not byte-faithful (for example, it may contain bool/i1 or implicit padding)
shape device build
read_unaligned(addr_of!(b)), b: bool (ru_bool below) fails
read_unaligned(addr_of!(s)), s: struct { a: u32, b: bool } fails (<{ <{ i32, i1, [3 x i8] }> }>)
read(addr_of!(b)), b: bool ok, correct
read_unaligned(addr_of!(x)), x: u32 ok, correct

Expected behavior

The kernel compiles and prints 1, as the host does. A bool copied through memory should be moved as its byte (i8), as the byte-faithful packed paths already do, or MaybeUninit<T>::assume_init should lower to a typed load of T.

Minimal reproduction example

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

#[device]
#[inline(never)]
pub fn ru_bool(b: bool) -> u64 {
    let v = unsafe { core::ptr::read_unaligned(core::ptr::addr_of!(b)) };   // fails
    // let v = unsafe { core::ptr::read(core::ptr::addr_of!(b)) };          // ok
    v as u64
}

#[cuda_module]
mod kernels {
    use super::*;
    #[kernel]
    pub fn k(b: bool, mut out: DisjointSlice<u64>) {
        let h = ru_bool(b);
        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, true, &mut out) }.unwrap();
    println!("{}", out.to_host_vec(&stream).unwrap()[0]); // host: 1
}

// 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 message comes from mir-lower's memory round-trip transmute path. The same message appears for other shapes that move an i1 through memory without read_unaligned, such as an Option wrapping a #[repr(packed)] struct that holds an enum, so the limitation is the i1-carrying aggregate in the round trip rather than read_unaligned itself; the probe above is the smallest case.

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