# nvlabs/cuda-oxide architecture

> Architecture diagram of the GitHub repository nvlabs/cuda-oxide, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nvlabs/cuda-oxide
- Repository: https://github.com/nvlabs/cuda-oxide

## Overview

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: [`crates/rustc-codegen-cuda/src/device_codegen.rs`](https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/device_codegen.rs) (Device codegen), [`crates/rustc-codegen-cuda/src/lib.rs`](https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/lib.rs) (rustc backend), [`crates/mir-importer/src/lib.rs`](https://github.com/nvlabs/cuda-oxide/blob/main/crates/mir-importer/src/lib.rs) (MIR importer), [`cuda-oxide-book/index.md`](https://github.com/nvlabs/cuda-oxide/blob/main/cuda-oxide-book/index.md) (cuda-oxide-book)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workspace["Workspace"]
end

subgraph group_compiler["Compiler"]
  node_rustc_codegen_cuda["rustc backend<br/>codegen backend<br/>[lib.rs]"]
  node_collector["Collector<br/>kernel discovery<br/>[collector.rs]"]
  node_device_codegen["Device codegen<br/>ptx/ltoir export<br/>[device_codegen.rs]"]
  node_mir_importer["MIR importer<br/>mir translation<br/>[lib.rs]"]
  node_mir_lower["MIR lower<br/>ir lowering<br/>[lib.rs]"]
  node_dialect_mir["MIR dialect<br/>ir model<br/>[lib.rs]"]
  node_dialect_llvm["LLVM dialect<br/>ir model<br/>[lib.rs]"]
  node_dialect_nvvm["NVVM dialect<br/>ir model<br/>[lib.rs]"]
end

subgraph group_runtime["Runtime"]
  node_cuda_core["cuda-core<br/>sync runtime<br/>[lib.rs]"]
  node_cuda_host["cuda-host<br/>host launch<br/>[lib.rs]"]
  node_cuda_async["cuda-async<br/>async runtime<br/>[lib.rs]"]
end

subgraph group_device["Device"]
  node_cuda_device["cuda-device<br/>device stdlib<br/>[lib.rs]"]
end

subgraph group_tooling["Tooling"]
  node_cuda_macros["cuda-macros<br/>proc macros<br/>[lib.rs]"]
  node_cuda_bindings["cuda-bindings<br/>CUDA FFI<br/>[lib.rs]"]
  node_libnvvm_sys["libnvvm-sys<br/>NVVM FFI<br/>[lib.rs]"]
  node_nvjitlink_sys["nvjitlink-sys<br/>linker FFI<br/>[lib.rs]"]
  node_cargo_oxide["cargo-oxide<br/>cargo subcommand<br/>[main.rs]"]
  node_oxide_artifacts["oxide-artifacts<br/>artifact utils<br/>[lib.rs]"]
  node_reserved_symbols["reserved symbols<br/>symbol utils<br/>[lib.rs]"]
  node_fuzzer["fuzzer<br/>differential testing<br/>[lib.rs]"]
  node_book["cuda-oxide-book<br/>documentation<br/>[index.md]"]
end

node_cuda_macros -->|"annotates"| node_rustc_codegen_cuda
node_rustc_codegen_cuda -->|"collects"| node_collector
node_rustc_codegen_cuda -->|"emits"| node_device_codegen
node_collector -->|"feeds"| node_mir_importer
node_mir_importer -->|"builds"| node_dialect_mir
node_dialect_mir -->|"lowers via"| node_mir_lower
node_mir_lower -->|"produces"| node_dialect_llvm
node_mir_importer -->|"maps intrinsics"| node_dialect_nvvm
node_dialect_nvvm -->|"specializes"| node_device_codegen
node_device_codegen -->|"calls"| node_libnvvm_sys
node_device_codegen -->|"links"| node_nvjitlink_sys
node_device_codegen -->|"packages"| node_oxide_artifacts
node_cuda_host -->|"loads"| node_oxide_artifacts
node_cuda_host -->|"builds on"| node_cuda_core
node_cuda_async -->|"wraps"| node_cuda_core
node_cuda_async -->|"launches via"| node_cuda_host
node_cuda_device -->|"binds to"| node_cuda_bindings
node_cuda_macros -->|"guards names"| node_reserved_symbols
node_cargo_oxide -->|"drives"| node_rustc_codegen_cuda
node_fuzzer -->|"exercises"| node_mir_importer
node_book -->|"documents"| node_rustc_codegen_cuda
node_book -->|"documents"| node_cuda_core
node_book -->|"documents"| node_cuda_device

click node_rustc_codegen_cuda "https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/lib.rs"
click node_collector "https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/collector.rs"
click node_device_codegen "https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/device_codegen.rs"
click node_mir_importer "https://github.com/nvlabs/cuda-oxide/blob/main/crates/mir-importer/src/lib.rs"
click node_mir_lower "https://github.com/nvlabs/cuda-oxide/blob/main/crates/mir-lower/src/lib.rs"
click node_dialect_mir "https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-mir/src/lib.rs"
click node_dialect_llvm "https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-llvm/src/lib.rs"
click node_dialect_nvvm "https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-nvvm/src/lib.rs"
click node_cuda_core "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-core/src/lib.rs"
click node_cuda_host "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-host/src/lib.rs"
click node_cuda_async "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-async/src/lib.rs"
click node_cuda_device "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-device/src/lib.rs"
click node_cuda_macros "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-macros/src/lib.rs"
click node_cuda_bindings "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-bindings/src/lib.rs"
click node_libnvvm_sys "https://github.com/nvlabs/cuda-oxide/blob/main/crates/libnvvm-sys/src/lib.rs"
click node_nvjitlink_sys "https://github.com/nvlabs/cuda-oxide/blob/main/crates/nvjitlink-sys/src/lib.rs"
click node_cargo_oxide "https://github.com/nvlabs/cuda-oxide/blob/main/crates/cargo-oxide/src/main.rs"
click node_oxide_artifacts "https://github.com/nvlabs/cuda-oxide/blob/main/crates/oxide-artifacts/src/lib.rs"
click node_reserved_symbols "https://github.com/nvlabs/cuda-oxide/blob/main/crates/reserved-oxide-symbols/src/lib.rs"
click node_fuzzer "https://github.com/nvlabs/cuda-oxide/blob/main/crates/fuzzer/src/lib.rs"
click node_book "https://github.com/nvlabs/cuda-oxide/blob/main/cuda-oxide-book/index.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_rustc_codegen_cuda,node_collector,node_device_codegen,node_mir_importer,node_mir_lower,node_dialect_mir,node_dialect_llvm,node_dialect_nvvm toneAmber
class node_cuda_core,node_cuda_host,node_cuda_async toneMint
class node_cuda_device toneRose
class node_cuda_macros,node_cuda_bindings,node_libnvvm_sys,node_nvjitlink_sys,node_cargo_oxide,node_oxide_artifacts,node_reserved_symbols,node_fuzzer,node_book toneIndigo
```

## Components

### Compiler

Rustc backend plus MIR/IR translation and dialects.

- [rustc backend](https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/lib.rs): codegen backend · Entry point for Rust-to-CUDA compilation and example-driven integration surface. · `crates/rustc-codegen-cuda/src/lib.rs`
- [Collector](https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/collector.rs): kernel discovery · Finds device entry points and collects codegen-relevant items from Rust inputs. · `crates/rustc-codegen-cuda/src/collector.rs`
- [Device codegen](https://github.com/nvlabs/cuda-oxide/blob/main/crates/rustc-codegen-cuda/src/device_codegen.rs): ptx/ltoir export · Drives device artifact emission from collected items into CUDA-compatible outputs. · `crates/rustc-codegen-cuda/src/device_codegen.rs`
- [MIR importer](https://github.com/nvlabs/cuda-oxide/blob/main/crates/mir-importer/src/lib.rs): mir translation · Translates Rust MIR into the project-specific intermediate representation pipeline. · `crates/mir-importer/src/lib.rs`
- [MIR lower](https://github.com/nvlabs/cuda-oxide/blob/main/crates/mir-lower/src/lib.rs): ir lowering · Lowers the MIR dialect into LLVM-like forms and prepares backend-facing IR. · `crates/mir-lower/src/lib.rs`
- [MIR dialect](https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-mir/src/lib.rs): ir model · Models imported MIR and CUDA-oriented intrinsics before lowering. · `crates/dialect-mir/src/lib.rs`
- [LLVM dialect](https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-llvm/src/lib.rs): ir model · Represents LLVM-like structures used during export and lowering. · `crates/dialect-llvm/src/lib.rs`
- [NVVM dialect](https://github.com/nvlabs/cuda-oxide/blob/main/crates/dialect-nvvm/src/lib.rs): ir model · 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](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-core/src/lib.rs): sync runtime · Synchronous runtime for CUDA contexts, streams, events, memory, modules, and launches. · `crates/cuda-core/src/lib.rs`
- [cuda-host](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-host/src/lib.rs): host launch · Typed module loading, embedded artifact handling, and host-side launch helpers. · `crates/cuda-host/src/lib.rs`
- [cuda-async](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-async/src/lib.rs): async runtime · 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](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-device/src/lib.rs): device stdlib · 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](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-macros/src/lib.rs): proc macros · Procedural macros for module and kernel annotations plus GPU printf support. · `crates/cuda-macros/src/lib.rs`
- [cuda-bindings](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cuda-bindings/src/lib.rs): CUDA FFI · Low-level bindgen-generated CUDA header bindings. · `crates/cuda-bindings/src/lib.rs`
- [libnvvm-sys](https://github.com/nvlabs/cuda-oxide/blob/main/crates/libnvvm-sys/src/lib.rs): NVVM FFI · Dynamic bindings to NVIDIA compiler libraries used in device artifact workflows. · `crates/libnvvm-sys/src/lib.rs`
- [nvjitlink-sys](https://github.com/nvlabs/cuda-oxide/blob/main/crates/nvjitlink-sys/src/lib.rs): linker FFI · Dynamic bindings to NVIDIA JIT linking libraries for LTOIR workflows. · `crates/nvjitlink-sys/src/lib.rs`
- [cargo-oxide](https://github.com/nvlabs/cuda-oxide/blob/main/crates/cargo-oxide/src/main.rs): cargo subcommand · Custom cargo driver for build, run, doctor, pipeline, and debug workflows. · `crates/cargo-oxide/src/main.rs`
- [oxide-artifacts](https://github.com/nvlabs/cuda-oxide/blob/main/crates/oxide-artifacts/src/lib.rs): artifact utils · Helpers for representing and handling generated device artifacts. · `crates/oxide-artifacts/src/lib.rs`
- [reserved symbols](https://github.com/nvlabs/cuda-oxide/blob/main/crates/reserved-oxide-symbols/src/lib.rs): symbol utils · Reserved-name and symbol handling around generated CUDA code. · `crates/reserved-oxide-symbols/src/lib.rs`
- [fuzzer](https://github.com/nvlabs/cuda-oxide/blob/main/crates/fuzzer/src/lib.rs): differential testing · Generates MIR cases and compares backend behavior to find translation bugs. · `crates/fuzzer/src/lib.rs`
- [cuda-oxide-book](https://github.com/nvlabs/cuda-oxide/blob/main/cuda-oxide-book/index.md): documentation · Architecture, compiler internals, API references, and workflow documentation. · `cuda-oxide-book/index.md`

## Connections

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