Nihal Pasham
cuda-oxide · preview

Provably Safe and Fast GPU Kernels in Rust

Specify and verify safety properties on your Rust kernels, and emit fast PTX with zero runtime checks.

We have been working on something ambitious for the past couple of months, and we are finally ready to share it.

With cuda-oxide, we can now write provably safe and fast GPU kernels completely in Rust, backed by compile-time Lean 4 proofs. That means no unsafe blocks for shared memory or barriers, and no bounds-check tax inside hot GPU loops.

Early Preview Notice
This feature is landing in preview (cuda-oxide/oxide-proofs-preview/). We are opening it up early so the Rust, GPU, and formal-verification communities can jump in with us. Expect rough edges, missing instructions, bugs, and probably soundness corners we have not thought of yet. Please try to break it!

First: What Do We Mean by "Safe"?

On a CPU, Safe Rust means zero undefined behavior: no out-of-bounds memory accesses, no data races, and no dangling pointers. Notice what it does not mean. It does not prove functional correctness. If you write a[i] - b[i] when you meant a[i] + b[i], your math is wrong, and Safe Rust will happily compile it.

On the GPU, if we want that same contract (memory safety and barrier safety), we don't need to prove functional correctness.

At the host launch boundary, rustc already gives us a strong foundation. Device buffers are guaranteed live and aligned, and the borrow checker ensures &a, &b, and &mut c never alias each other.

What rustc does not provide is multi-thread reasoning inside the kernel body. rustc type-checks a kernel function as if a single thread called it, while the GPU runs thousands of threads through that function at the same time:

Visual 1 · Multi-Thread SIMT Safety

Three GPU Bugs Single-Thread Checking Does Not Catch

1. Out-of-Bounds Index
no_oob_idx(buf)
4 threads, 3-element slice t0 t1 t2 t3 OOB! [0] [1] [2]
Thread t3 walks past buf.len() and corrupts adjacent memory.
2. Shared-Memory Race
no_data_race(TILE)
same phase, no barrier between threads t1 t2 write read RACE!
t1 writes while t2 reads TILE[1] in the same step without a barrier.
3. Divergent Barrier
uniform_barriers
all CTA threads must arrive t0 t1 t2 t3 return; sync_threads() · 3/4 stall
t3 returns early inside an if, leaving the block deadlocked or desynced.
Toggle between Without #[verify] and With #[verify] to see how bounds, barrier phases, and uniform arrival behave across threads.

Until now, the moment your kernel staged tiles in shared memory, synchronized across warps, or needed bounds checks stripped out of a hot inner loop, you had to reach for unsafe or --unchecked-indexing.

How It Works

The core idea is specify and verify. You write a small specification on your Rust kernel stating what safety properties hold, and the compiler takes a quick detour during compilation to verify them before generating PTX:

Visual 2 · Specify & Verify

The Proof Detour: Collect, Verify, and Erase Checks

Step 1 · Specify
Safe Rust Kernel
Declare safety rules
#[launch_contract(
  requires = (
    a.len >= c.len
  )
)]
#[verify(
  no_oob_idx(a, b, c)
)]
Step 2 · Detour
Pause in MIR
Collect SSA + checks
^in_bounds:
mir.assert(idx < a.len) v1 = mir.load(a[idx])
mir.assert(idx < b.len) v2 = mir.load(b[idx])
mir.store(c[idx], v1+v2)
Step 3 · Verify
Check Spec in Lean 4
Prove bounds & barriers
def mirFunc : MirFunc
theorem verify_no_oob :
  MirNoOobGlobal ... :=
  by mir_vc mirFunc
✓ 0 sorryAx (QED)
Step 4 · Resume
Emitted PTX
Proven checks erased!
setp.ge %p1, idx, len; @%p1 bra trap; ld.global.f32 %f1, [a];
ld.global.f32 %f2, [b];
add.f32 %f3, %f1, %f2;
st.global.f32 [c], %f3;
// 0 trap branches
Step 1 (Specify): You declare what safety properties hold on the Rust kernel.
Click any stage card above (or watch the loop) to see the detour verify the specification and erase mir.assert checks before PTX emission.

Once Lean 4 verifies every safety theorem with zero sorryAx axioms, the compiler deletes the proven mir.assert bounds checks in memory and resumes normal lowering through LLVM to emitted PTX.

What It Looks Like in Practice

Here is a complete vector-add kernel using a plain mutable Rust slice c: &mut [f32], zero wrapper types, and zero unsafe:

examples/safe_vecadd/src/main.rs 100% Safe Rust · 3 bounds checks erased
#[launch_contract(
    domain = 1,
    block = (256, 1, 1),
    requires = (a.len() >= c.len(), b.len() >= c.len()),
)]
#[verify(no_oob_idx(a, b, c), disjoint_writes(c), writes_all_elems(c))]
#[kernel]
pub fn vecadd(a: &[f32], b: &[f32], c: &mut [f32]) {
    let idx = (thread::blockIdx_x() as usize) * (thread::blockDim_x() as usize)
        + (thread::threadIdx_x() as usize);
    if idx < c.len() {
        c[idx] = a[idx] + b[idx];
    }
}

Notice c: &mut [f32] in the signature. Normally cuda-oxide rejects a plain mutable slice because two GPU threads could overwrite the same slot, so #[verify] unlocks it once exclusivity is proved. Here is what happens at each stage:

And if you sneak in an off-by-one bug like a[idx + 1]? Compilation halts before PTX is ever emitted, and Lean's integer solver hands back a concrete counterexample. Try toggling the bug live below:

Visual 3 · Line-by-Line Translation

Inside MirFunc (Click Any Line, or Break It!)

1. Rust Source#[kernel]
let idx = bx * 256 + tx;
if idx < c.len() {
  let x = a[idx];
  let sum = x + b[idx];
  c[idx] = sum;
2. PostMem2Reg MIRPliron SSA
v6 = nvvm.index_1d()
v8 = mir.lt(v6, c.len)
mir.cond_br v8 ^bb1, ^exit
mir.assert(v6 < a.len)
v11 = mir.load(&a[v6])
v15 = mir.fadd(v11, v14)
mir.store(&c[v6], v15)
3. Lean 4 MirFunc1-to-1 AST
(.assign 6 .index1D)
(.assign 8 (.cmpOp .lt 6 7))
(.condBr 8 1 [] 2 [])
(.assertCond 0 9)
(.assign 11 (.load 10))
(.assign 15 .opaqueVal)
(.store 17 15)
$ lake env lean Vecadd.lean -> PASS (0 sorryAx)
• Proved idx < c.len ∧ c.len ≤ a.len → idx < a.len via Lean's integer solver
• Proved bx₁*256 + tx₁ = bx₂*256 + tx₂ → t₁ = t₂ (MirDisjointWrites)
• Action: Erased all 3 mir.assert ops in memory; proceeding to LLVM/PTX.
Click any row to trace a single instruction across Rust, Pliron SSA, and Lean 4, or click Inject Bug: a[idx + 1] to see Lean reject it.

Scaling Up: 2.118 PFLOP/s on NVIDIA B200

Proving vector addition is a nice sanity check, so we pushed the exact same line-by-line engine all the way up to a full speed-of-light 2-CTA persistent BF16 GEMM on Blackwell (safe_gemm_sol_final, 338 MIR basic blocks).

Because MirFunc and ControlFlowCfg verify individual instructions rather than hardcoded indexing shapes, the same proof library verifies a warp-specialized 2-block cluster pipeline:

Visual 4 · NVIDIA B200 (sm_100a)

Verified 2-CTA Blackwell GEMM: Zero Safety Overhead at 2.12 PFLOP/s

Warp 4 · Producer + CLC
7-Stage TMA Ring (224 KiB)
S0
S1
S2
S3
S4
S5
S6
✓ Proved: no_oob_idx + no_data_race
→
Warp 5 · 2-CTA Tensor Core
512-Col TMEM Ping-Pong
Stage 0
[0..256]
Stage 1
[256..512]
✓ Proved: tmem_lifecycle (cta_group::2)
→
Warps 0..3 · Epilogue
Parallel TMEM Unload
W0
0..32
W1
32..64
W2
64..96
W3
96..128
✓ 27 checks erased · 0 B spills
NVIDIA B200 BF16 GEMM Throughput (TFLOP/s)
Unsafe (--unchecked-indexing) Lean-Verified Safe (#[verify])
4096³
1,855.5
1,858.4
100.15%
8192³
2,081.4
2,082.3
100.04%
16384³
2,118.1
2,118.4
100.01%
Live simulation of the 7-stage TMA ring and 2-stage TMEM ping-pong verified by Lean 4, alongside back-to-back B200 measurements.

Trust Boundary and Summary

We want to be clear about what is proved and what sits in our Trusted Computing Base (TCB) today:

What #[verify] Gives You

What to Watch Out For

You can inspect the live MIR and generated Lean 4 theorems on any verified kernel with one command:

Terminal Inspect MIR + Lean 4 theorems
cargo oxide lean-export safe_vecadd

Help Us Stress-Test the Preview

This is our first public preview of compile-time Lean 4 kernel verification in cuda-oxide (PR #1851). Unsupported MIR patterns fail closed at compile time, syntax will evolve, and there are almost certainly bugs and soundness edge cases waiting to be found. If you enjoy Rust, GPU kernels, compilers, or Lean 4, take it for a spin, try to break the verifier, and open an issue with whatever you hit!