The CubeCL Deep Technical Book¶
A kernel tuned to peak on an NVIDIA GPU does not run on an AMD card. Porting it to CPU is harder still, because it encodes "which lanes issue the same instruction" directly into index arithmetic — that is not an abstraction, it is a set of hardcoded assumptions. So every team either maintains N copies of a kernel or piles on runtime branches, and both invoices are expensive.
CubeCL's answer is to let a kernel read, at compile time, which hardware it is about to run on. This book follows that answer's causal chain all the way down: how four parallelism axes unify a fragmented programming model, how comptime turns device differences into compile-time constants, how autotuning searches the resulting configuration space, and where the whole thing stops holding.
19 chapters, 981 Rust source files, 210k lines of code, every claim bound to a file:line.
Source repository cubecl @ a6321cd4.
-
Where to start
Every chapter opens by answering "why does this mechanism need to exist" — use that to decide what to skip.
To understand the design: chapter 1 → 2 → 3, then jump as needed.
To read the source: chapter 2 (the four axes) → 7 (the IR) → 9 (the LLVM backend). Those three are the keys to everything else.
To write kernels: chapters 2, 3, and 4 are required reading, then jump straight to the worked case in chapter 18 — a serial GPU kernel optimized to cube-level parallelism, one axis changed per step.
If you only care about correctness: chapter 16. A missing barrier is not slower, it is wrong.
-
Source baseline
Citations are written
`path/file.rs:123`, relative to the repository root, against commita6321cd4. The repository moves, so each chapter ends with a version note.Every quoted code block was verified verbatim against the source by the build gates — none of them were retyped from memory.
-
Language
Switch between Chinese and English at the top right. Both versions share code blocks, Mermaid figures, and
file:linecitations verbatim; the prose in each was written in that language rather than translated.
Vision¶
Read a 210k-line Rust GPU compute language as the causal chain by which four orthogonal parallelism axes unify fragmented hardware.
Goals¶
- Name what Vector, Plane, CubeDim, and CubeCount each map to on real hardware, and explain why kernels must adapt on those axes rather than hardcode them
- Explain how comptime reads hardware properties at compile time, and why that removes runtime branching
- Navigate the CubeCL IR dialect structure and optimization pipeline, locating any pass's entry point
- Write a CubeCL kernel with comptime specialization and autotuning, and explain its parallel strategy
Prerequisites¶
- Comfortable reading Rust traits, generics, and lifetimes; no need to have written unsafe code
- Aware that CUDA, WebGPU, and Metal exist and roughly what they target; no shader authoring required
- Understand the difference between compile-time and runtime evaluation; no compiler literature required
How to read this book¶
Four paths, pick by purpose:
- To understand the system end to end: chapter 1 → 2 → 3 → 7 → 9. Those five form the spine; every other chapter is an expansion of one of them.
- To copy the architecture: chapter 2 (four axes), 5 (autotuning), 10 (devices and memory), 16 (synchronization). Together these cover most of CubeCL's abstraction boundaries.
- To write kernels: chapters 2, 3, 4, and 5 are required — the four-axis model, comptime specialization, vectorization, configuration search. Then go straight to the full case study in chapter 18.
- If you only care about correctness and failure modes: every chapter carries an "Edge cases" and a "Failure modes" section. Chapter 11 (how launch errors attach to output buffers) and chapter 16 (the memory-order promise of synchronization) are the two worth reading first.
The book's arguments¶
Four claims run through the whole book; every chapter states which ones it supports:
| id | Claim |
|---|---|
four-axes |
Four orthogonal parallelism axes unify every hardware platform's parallel model, letting a single kernel adapt to reach peak performance on any backend |
comptime-specialization |
Comptime is not a compile optimization but a programming paradigm: kernels read hardware properties at compile time and specialize themselves, eliminating runtime branching |
autotune-as-search |
Autotuning turns kernel selection from manual experience into systematic search over the comptime configuration space |
rust-as-kernel-language |
Using Rust as the kernel language is not a compromise but brings type checking, borrow checking, and composability to GPU programming without sacrificing performance |
Conventions¶
| Convention | Meaning |
|---|---|
`path/to/file.rs:123` |
Source citation, relative to the repository root, against the pinned commit |
> **Core sources** |
The main source files listed at the top of a chapter |
> **Book thesis** |
How the chapter supports the book's central arguments |
> **Inference**: ... |
The author's judgment, not a source claim. Unmarked strong claims all carry a line number |