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");
else
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.