Skip to content

[BUG]: device function returning a zero-sized enum fails verification ("MirReturnOp operand count must match function result count", mir-importer) #1890

Backlog

Description

@ubmaxxer

Description

A device function whose return type is a zero-sized enum (a single unit variant, such as enum Z { V0 }) fails device codegen with Verification failed for '<crate>::<fn>': MirReturnOp operand count must match function result count. Passing the same enum as an argument, matching on it, or building it as a constant all compile; only returning it fails. The dialect-mir signature keeps the ZST result while the body's return is emitted without an operand, so the verifier's count check fails. The same program compiles and runs on the host; both device routes fail since the importer runs before route selection. Zero-sized enums arise from generic code once a type parameter is instantiated with a unit-like type, and from #[derive]d marker enums. Reproduced on main at a412e09; the importer is unchanged at the current main (8b91bb7).

Error / actual behavior

$ cargo oxide run repro
error: [rustc_codegen_cuda] Device codegen failed: PTX generation failed: Verification failed for 'repro::id_z':
         ["src/main.rs": line: 7, column: 1] Compilation error: verification failed.
       MirReturnOp operand count must match function result count
shape device build
device fn returning Z (id_z below) fails
Z::V0 as an argument, match z { Z::V0 => … } (use_z below, kernel k_use) ok, correct

Expected behavior

The kernel compiles and prints 42. A zero-sized return carries no data, so either the signature should drop the result or the return should carry a synthesized unit value, as zero-sized struct returns already do.

Minimal reproduction example

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

#[derive(Clone, Copy)]
pub enum Z { V0 }

#[inline(never)]
pub fn id_z(z: Z, x: u32) -> Z { let _ = x; z }          // fails: returns the ZST enum

#[inline(never)]
pub fn use_z(z: Z, x: u32) -> u32 { match z { Z::V0 => x.wrapping_add(1) } }

#[cuda_module]
mod kernels {
    use super::*;
    #[kernel]
    pub fn k_ret(x: u32, mut out: DisjointSlice<u32>) {
        let z = id_z(Z::V0, x);
        if let Some(s) = out.get_mut(thread::index_1d()) { *s = use_z(z, x); }
    }
    // Compiles and prints 42 when `k_ret` and `id_z` are removed:
    // #[kernel] pub fn k_use(x: u32, mut out: DisjointSlice<u32>) {
    //     if let Some(s) = out.get_mut(thread::index_1d()) { *s = use_z(Z::V0, x); }
    // }
}

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::<u32>::zeroed(&stream, 1).unwrap();
    unsafe { module.k_ret(&stream, cfg, 41u32, &mut out) }.unwrap();
    println!("{}", out.to_host_vec(&stream).unwrap()[0]); // host: 42
}

// 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

The failure is unchanged with CUDA_OXIDE_DEBUG=full, which skips the MIR preparation passes, so the mismatched function comes out of the importer itself; the message is the MirReturnOp verifier in dialect-mir, run on the module immediately before lowering (cuda-oxide-codegen/src/pipeline.rs), before any route-specific stage.

Related: #1886 (zero-sized constant synthesis lacks the Array and Tuple cases). The same helper also lacks an Enum case: a zero-sized enum folded into a constant inside a sized aggregate fails with Unsupported construct: Zero-sized constant synthesis does not support type MirEnumType { … variant_names: ["V0"], … total_size: 0 … }. The two together mean a unit-variant enum can be used only as a plain value in a kernel today.

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