Nvc (CUDA 13.3): unroll switches ignored, loop may be unrolled into useless copies

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:

  1. It cannot be switched off the documented ways. Neither -gpu=nounroll nor an unroll pragma on the loop changes it (#pragma unroll 1, unroll 0, unroll(1), nounroll and loop nounroll were all accepted silently and ignored). Only -Wnvvm,-Xopt,-unroll-max-upperbound=0 does.
  2. 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 with vector_length threads 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=nounroll and #pragma unroll 1 suppress 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)

Hi UnitedMarsupials,

Thanks for the report. For the compiler not disabling unrolling by NVVM, I added TPR #39165. From what I can tell, the compiler is including “llvm.loop.unroll.disable” in the generated LLVM code, but looks like NVVM is only applying it to the outer gang loop. Engineering will need to decide how to fix it, but we may need to issue the instruction again for the inner vector loop.

Another work around is to compile at “-O3”. There we apply an addition induction variable optimization. This doesn’t explicitly disable unrolling by NVVM, but changes the code so doesn’t unroll the loop.

As to why NVVM 13.x is unrolling this loop when targeting cc100 and cc120, I’m not sure. But I noted this in the report but if we need to reach out to that team, we will.

-Mat

(Side note, I moved your post from the CUDA UF to the NVHPC UF)