Repository navigation
Description
Description
#[unroll(N)] on a loop with a runtime trip count writes an N-wide main loop and a remainder loop, but the exported IR carries no loop metadata for them. The backend (libNVVM + nvJitLink) then unrolls the remainder loop a second time (N − 1 single copies plus another N-wide loop, and more in the assembler stage). Hand-unrolled loops get the same treatment. The result is larger code and more executed instructions than without the attribute, and under #[launch_bounds] it can push a kernel into local-memory spills. CUDA C controls this with #pragma unroll N; Rust code has no equivalent today.
Version: cuda-oxide at 26754ae, native route (Rust → NVVM IR → libNVVM → nvJitLink), CUDA 13.3, target sm_90.
Error / actual behavior
No error. The generated code is larger and slower than expected. Minimal kernel below (one dot product per node, #[launch_bounds(256, 8)]), H100, ncu over every launch, identical output bits in every variant:
| variant | static instructions | executed warp instructions | time |
|---|---|---|---|
no annotation (dot_auto) |
232 | 3.303e9 | 5.076 ms |
#[unroll(4)] (dot_attr) |
480 | 3.321e9 | 5.093 ms |
| 4 copies written by hand, counted loop | 464 | 3.549e9 | 5.596 ms |
#[unroll(4)] with llvm.loop.unroll.disable on both loops (IR edited by hand) |
216 | 3.015e9 | 4.909 ms |
In a larger production kernel (32 registers under launch_bounds(256, 8)) the same metadata takes the static code from 1,624 to 1,040 instructions and the time from 15.26 to 14.95 ms; the PTX then carries .pragma "nounroll" on those loops.
Expected behavior
After #[unroll(N)] the main loop and the remainder loop are final: the exporter marks their back-edge branches with !llvm.loop / llvm.loop.unroll.disable, so libNVVM (and llc) leave them as written. Loops without the attribute stay untouched.
Two related requests:
- An annotation that means "leave this loop as written" (for example
#[unroll(1)], today a compile error, or#[no_unroll]), for loops the author already unrolled by hand and for cold general-case loops. Without it the backend unrolls such loops 16-wide and long kernels reach thousands of static instructions. #[kernel(unchecked_indexing)]covers only the kernel's own body. Helper functions it calls keep their bounds checks when they are separate bodies (no MIR inlining in the native profile). A way to extend it to#[device]helpers, or a note in the docs, would help.
Minimal reproduction example
use cuda_device::{DisjointSlice, kernel, launch_bounds, thread};
use cuda_host::cuda_module;
const BELOW: u32 = 64;
#[cuda_module]
mod kernels {
use super::*;
#[inline(always)]
unsafe fn rows_of(values: &[f64], width: &[u32], offsets: &[u64], s: u32, tid: u32)
-> (*const f64, *const f64, u32, u32) {
unsafe {
let w = *width.as_ptr().add(s as usize);
let p = values.as_ptr().add(*offsets.as_ptr().add(s as usize) as usize);
let (bi, bj) = (w + (tid & (BELOW - 1)), w + ((tid >> 6) & (BELOW - 1)));
(p.add(bi as usize), p.add(bj as usize), w + BELOW - 1, w)
}
}
// Variant A: no annotation. Variant B: add `#[unroll(4)]` on the inner `while`.
#[kernel]
#[launch_bounds(256, 8)]
pub fn dot_attr(values: &[f64], width: &[u32], offsets: &[u64], mut out: DisjointSlice<f64>, nodes: u32) {
let tid = thread::index_1d();
let lane = tid.get() as u32;
let Some(slot) = out.get_mut(tid) else { return };
let mut sum = 0.;
let mut s = 0;
while s < nodes {
let (mut pi, mut pj, mut step, w) = unsafe { rows_of(values, width, offsets, s, lane) };
let mut dot = 0.;
let mut q = 0u32;
unsafe {
#[unroll(4)]
while q < w {
dot += *pi * *pj;
pi = pi.add(step as usize);
pj = pj.add(step as usize);
step -= 1;
q += 1;
}
}
sum += dot;
s += 1;
}
*slot = sum;
}
}Compare the PTX / cubin of the kernel with and without the attribute (instruction count), or run it under ncu (smsp__inst_executed). Each panel is a packed lower-triangular block of w columns with BELOW rows under it; widths are small (1 to about 40), so the remainder loop runs often.
Operating System
Ubuntu 24.04.4 LTS
GPU and GPU Architecture
NVIDIA H100 80GB HBM3 (sm_90)
CUDA driver version
580.178.04
Activity
feat(unroll): recognize range `for` loops (#1346)
feat(unroll): add #[nounroll] to keep loops rolled (NVIDIA#1552)