Summary
With -gpu=cuda13.3, nvc 26.9 (and nvc++) emits the body of the OpenACC vector loop below 32 times. Two things are wrong with that:
- It cannot be switched off the documented ways. Neither
-gpu=nounrollnor an unroll pragma on the loop changes it (#pragma unroll 1,unroll 0,unroll(1),nounrollandloop nounrollwere all accepted silently and ignored). Only-Wnvvm,-Xopt,-unroll-max-upperbound=0does. - 31 of the 32 copies are dead code. nvc turns the vector loop into
for (l = threadIdx.x; l < 32; l += blockDim.x), and the loop is unrolled to its maximum trip count, as if a block could be one thread wide. But the kernel runs withvector_lengththreads per block, 32, so every thread executes one iteration and leaves; the other 31 copies never run. They still cost registers, spills and compile time.
With -gpu=cuda12.9 the same nvc emits the body once.
Reproducer
vector-loop-unroll.c (attached; main() checks the result against the host’s). The kernel:
#define LANES 32
#define SL 8
#pragma acc parallel loop gang num_gangs(nGangs) vector_length(vl) \
copyin(x[0:nCells], n[0:nCells]) copy(y[0:nCells])
for (size_t g = 0; g < nGangs; ++g) {
#pragma acc loop vector
for (size_t l = 0; l < LANES; ++l) {
size_t c = g * LANES + l;
size_t slots[SL];
double v = x[c];
for (size_t s = 0; s < nSl; ++s) {
slots[s] = n[c] + s;
if (slots[s / 2] % 3 != 0)
y[c] += v * slots[s / 2];
}
}
}
vl is read at run time (default 32). The body stores to y 15 times in the PTX, so the number of st.global instructions counts the copies:
$ nvc -acc=gpu -gpu=cc100,cuda13.3,keep -O2 vector-loop-unroll.c
$ grep -c st.global vector-loop-unroll.n001.ptx
nvc 26.9 st.global registers
-gpu=cc100,cuda12.9 15 32
-gpu=cc100,cuda13.3 480 78
-gpu=cc100,cuda13.3 -gpu=nounroll 480 78
-gpu=cc100,cuda13.3, #pragma unroll 1 on the vector loop * 480 78
-gpu=cc100,cuda13.3 -Wnvvm,-Xopt,-unroll-max-upperbound=0 15 32
* likewise #pragma unroll 0, unroll(1), nounroll, loop nounroll. The results are correct in every case.
nvcc 13.3 does the same to the equivalent CUDA loop (block-stride-unroll.cu, attached: 480 against 15 st.global); there #pragma unroll 1 does suppress it.
Impact
In our production kernel (a callable-bond pricer, one cell per vector lane, -gpu=maxregcount:64) the copies take the stack frame from 872 to 2,048 bytes and spill loads from 3.5 KB to 95 KB, compilation from seconds to minutes, and the kernel runs about 4 % slower than with the unrolling suppressed. They also change the floating-point results in the last bits: with -unroll-max-upperbound=0, CUDA 13.3’s results are bit-identical to 12.9’s.
Expected
-gpu=nounrolland#pragma unroll 1suppress this unrolling, or an ignored pragma draws a warning.- A loop whose step is the block size is not unrolled to its worst-case trip count, which assumes one thread per block: such a loop normally runs once per thread, and the extra copies only add dead code.
Seen with CUDA 13.3 (nvcc V13.3.73, HPC SDK 26.9), from nvc 26.9-0 and from nvc 25.11-0 pointed at it with NVHPC_CUDA_HOME; nvc++ behaves the same. CUDA 12.9 (V12.9.86) and 13.0 (V13.0.88) do not do it, with either release.
vector-loop-unroll.c.txt (2.7 KB)