How nvlabs/cuda-oxide works
1. Repository shape
- This is a Rust workspace centered on cuda-oxide, a custom rustc codegen backend for CUDA.
- The workspace splits into a compiler pipeline, device-side APIs, host-side runtime APIs, build tooling, fuzzing infrastructure, and documentation/examples.
2. Primary end-to-end flow
- The user writes host and device code in one Rust source file.
- #[cuda_module] and #[kernel] mark device entry points.
- At build time, the backend compiles kernel code through a Rust-to-PTX pipeline, embeds the resulting device artifact into the host binary, and generates typed host launch wrappers.
3. Compiler pipeline boundary
- rustc-codegen-cuda is the entry point for code generation.
- mir-importer translates Rust MIR into the project’s internal dialects.
- mir-lower lowers the MIR dialect into LLVM-like dialects.
- dialect-mir, dialect-llvm, and dialect-nvvm model the intermediate representations and CUDA/NVVM intrinsics used during compilation.
- The pipeline ultimately exports LLVM IR / PTX and supports device LTOIR paths for newer CUDA toolchains.
4. Host runtime layer
- cuda-core is the main synchronous runtime and resource-management layer.
- It owns CUDA context, stream, event, memory, and launch abstractions such as CudaContext, DeviceBuffer, PinnedHostBuffer, and related launch/module types.
- cuda-host provides typed module loading and launch helpers, plus support for loading embedded device artifacts and LTOIR workflows.
5. Async execution layer
- cuda-async sits above the core runtime and wraps GPU work as lazy composable operations.
- Its main abstraction is DeviceOperation, which delays execution until synchronization or await.
- This crate is the boundary for the asynchronous pipeline used by examples like async_mlp and async_vecadd.
6. Device programming surface
- cuda-device is the device-side standard library for kernels.
- It provides intrinsics and safety-oriented abstractions for threads, warps, grids, shared memory, barriers, atomics, clusters, TMA, WGMMA, and other GPU primitives.
- The crate is organized by CUDA execution and synchronization concepts rather than by host/runtime concerns.
7. Procedural macros and generated APIs
- cuda-macros contains proc macros that reshape Rust code into CUDA-friendly forms.
- It is responsible for the #[cuda_module] and #[kernel] surface, plus gpu_printf!.
- These macros connect user code to the compiler/runtime boundary by generating module loaders and launch entry points.
8. CUDA FFI and platform bindings
- cuda-bindings is the low-level bindgen-generated FFI layer over CUDA headers.
- libnvvm-sys and nvjitlink-sys provide dynamic bindings to NVIDIA compiler libraries used by the host-side artifact and LTOIR pipeline.
- These crates are thin foreign-function boundaries; higher layers avoid direct CUDA C API usage.
9. Auxiliary compiler and import tooling
- reserved-oxide-symbols and oxide-artifacts support symbol and artifact handling around the generated device code.
- cargo-oxide is the custom cargo subcommand that drives build, run, doctor, pipeline, and debug workflows.
- The repo also includes cuda-oxide-book, which documents the architecture, compiler internals, and API surface.
10. Testing and verification structure
- Compiler and dialect crates have unit and compile-fail tests to validate IR translation and safety rules.
- cuda-device includes compile-fail tests focused on thread indexing and safety invariants.
- rustc-codegen-cuda includes many runnable examples that double as integration tests for kernels, launch paths, and feature coverage.
11. Fuzzing and differential testing
- crates/fuzzer and the nested rustlantis workspace generate MIR test cases and compare behaviors across backends.
- This subsystem exercises the compiler pipeline rather than the runtime and is aimed at catching translation bugs and undefined-behavior mismatches.
12. Examples as architecture proof points
- crates/rustc-codegen-cuda/examples/ contains the practical top layer of the system: vector add, GEMM, atomics, clusters, async pipelines, FFI interop, and more.
- These examples show the full data flow from Rust source to compiled GPU artifact to host launch and result transfer.
13. Development and CI environment
- The repo includes a devcontainer, pinned Rust toolchain, lint/test workflows, and dependency/license checks.
- These do not change runtime architecture, but they materially support reproducible builds for a CUDA + LLVM + Clang toolchain-heavy project.
Main entry points: device_codegen.rs Device codegen · src/lib.rs rustc backend · src/lib.rs MIR importer · index.md cuda-oxide-book
Components 21
Compiler
Rustc backend plus MIR/IR translation and dialects.
- rustc backend · Entry point for Rust-to-CUDA compilation and example-driven integration surface.
crates/rustc-codegen-cuda/src/lib.rs - Collector · Finds device entry points and collects codegen-relevant items from Rust inputs.
crates/rustc-codegen-cuda/src/collector.rs - Device codegen · Drives device artifact emission from collected items into CUDA-compatible outputs.
crates/rustc-codegen-cuda/src/device_codegen.rs - MIR importer · Translates Rust MIR into the project-specific intermediate representation pipeline.
crates/mir-importer/src/lib.rs - MIR lower · Lowers the MIR dialect into LLVM-like forms and prepares backend-facing IR.
crates/mir-lower/src/lib.rs - MIR dialect · Models imported MIR and CUDA-oriented intrinsics before lowering.
crates/dialect-mir/src/lib.rs - LLVM dialect · Represents LLVM-like structures used during export and lowering.
crates/dialect-llvm/src/lib.rs - NVVM dialect · Captures NVVM and CUDA-specific intrinsics used in the device compilation path.
crates/dialect-nvvm/src/lib.rs
Runtime
Host and async GPU execution layers.
- cuda-core · Synchronous runtime for CUDA contexts, streams, events, memory, modules, and launches.
crates/cuda-core/src/lib.rs - cuda-host · Typed module loading, embedded artifact handling, and host-side launch helpers.
crates/cuda-host/src/lib.rs - cuda-async · Lazy composable GPU operations that execute on synchronization or await.
crates/cuda-async/src/lib.rs
Device
Device-side programming surface for kernels.
- cuda-device · Device-side API surface for threads, warps, grids, shared memory, atomics, and advanced GPU primitives.
crates/cuda-device/src/lib.rs
Tooling
Build, fuzzing, bindings, and artifact support.
- cuda-macros · Procedural macros for module and kernel annotations plus GPU printf support.
crates/cuda-macros/src/lib.rs - cuda-bindings · Low-level bindgen-generated CUDA header bindings.
crates/cuda-bindings/src/lib.rs - libnvvm-sys · Dynamic bindings to NVIDIA compiler libraries used in device artifact workflows.
crates/libnvvm-sys/src/lib.rs - nvjitlink-sys · Dynamic bindings to NVIDIA JIT linking libraries for LTOIR workflows.
crates/nvjitlink-sys/src/lib.rs - cargo-oxide · Custom cargo driver for build, run, doctor, pipeline, and debug workflows.
crates/cargo-oxide/src/main.rs - oxide-artifacts · Helpers for representing and handling generated device artifacts.
crates/oxide-artifacts/src/lib.rs - reserved symbols · Reserved-name and symbol handling around generated CUDA code.
crates/reserved-oxide-symbols/src/lib.rs - fuzzer · Generates MIR cases and compares backend behavior to find translation bugs.
crates/fuzzer/src/lib.rs - cuda-oxide-book · Architecture, compiler internals, API references, and workflow documentation.
cuda-oxide-book/index.md
Connections 23
- cuda-macros → rustc backend: annotates · Macros surface kernels/modules to the compiler backend.
- rustc backend → Collector: collects · Backend discovers device entry points and codegen items.
- rustc backend → Device codegen: emits · Backend drives device compilation and artifact generation.
- Collector → MIR importer: feeds · Collected MIR enters the import pipeline.
- MIR importer → MIR dialect: builds · Rust MIR is translated into the project MIR dialect.
- MIR dialect → MIR lower: lowers via · Imported MIR is lowered toward backend-ready representations.
- MIR lower → LLVM dialect: produces · Lowering yields LLVM-like IR structures for export.
- MIR importer → NVVM dialect: maps intrinsics · CUDA and NVVM intrinsics are modeled during import.
- NVVM dialect → Device codegen: specializes · NVVM intrinsics and target features shape final device emission.
- Device codegen → libnvvm-sys: calls · Device compilation may invoke NVIDIA compiler libraries.
- Device codegen → nvjitlink-sys: links · LTOIR and JIT linking paths use NVIDIA linker libraries.
- Device codegen → oxide-artifacts: packages · Compiled device outputs are packaged as embedded artifacts.
- cuda-host → oxide-artifacts: loads · Host runtime consumes embedded device artifacts.
- cuda-host → cuda-core: builds on · Typed host loading and launch helpers sit on the core runtime.
- cuda-async → cuda-core: wraps · Async operations are layered over the synchronous runtime.
- cuda-async → cuda-host: launches via · Async workflows use host module loading and launch helpers.
- cuda-device → cuda-bindings: binds to · Device-side APIs rely on low-level CUDA bindings where needed.
- cuda-macros → reserved symbols: guards names · Macro expansion avoids collisions with reserved generated symbols.
- cargo-oxide → rustc backend: drives · Custom cargo workflows build and run the backend examples and pipelines.
- fuzzer → MIR importer: exercises · Differential testing targets MIR translation and backend behavior.
- cuda-oxide-book → rustc backend: documents · Docs explain the compiler pipeline and public architecture.
- cuda-oxide-book → cuda-core: documents · Docs cover the host runtime and launch model.
- cuda-oxide-book → cuda-device: documents · Docs cover the device programming surface and safety model.