Miscompilation of simple CPU code with nvc/21.7

The following small example (reduced from a real-world one with C-Reduce) does not compile correctly with the nvc compiler from the 21.7 HPC SDK if -O2 is used:

int printf(const char*, ...);
static double a = 1.0;
int main() {
  if (a > 0.0)
    a = printf("right");
    a = printf("wrong ");
  return 0;

Compiling with nvc -O2 -pedantic -Werror does not emit any warnings/errors, but the resulting binary outputs “wrong right” — i.e. it executes both branches of the if statement.

With -O1 then nvc/21.7 produces the correct result (i.e. “right“), and nvc/21.2 produces the correct result with both -O1 and -O2.

I am not aware of this example triggering any undefined behaviour, it compiles cleanly with gcc/11.2 (-Wall -pedantic) and clang/12.0.1 (-Wall -Weverything -pedantic) and executes cleanly under valgrind.

I did not try the versions between 21.2 and 21.7 to see exactly when this issue was introduced, but that should be easy to do with the example above. Almost all of the investigations above were done on a RHEL7.6 system with the HPC-SDK installed via Spack, but I verified the reproducer above using the nvcr.io/nvidia/nvhpc:21.7-devel-cuda_multi-ubuntu20.04 Docker image.

1 Like

Thanks olupton, I was able to reproduce the issue in 21.7 (21.5 is ok).

The good news is that it appears that we may have fixed this already since the code produces the correct results in our pre-release 21.9 compiler. I didn’t see a related problem report, so it’s likely something we found and fixed internally.

Though just because it works in the pre-release does not guarantee it be fixed in the final release. Hence, I went ahead and added a new problem report, TPR #30533, to track.


Hi Mat,

Thanks for the update! Good news that it already seems to be fixed internally.

Best, Olli

Hi Oli,

I missed updating this to confirm that the fix did indeed make it into 21.9.