NVCC: No warning when using uninitialized variable

I observed that nvcc does not warn when using uninitialzed variables in my kernel.

The Visual Studio 2008 compiler gave me this warning, when I changed my kernel to run with OpenMP (very easy to do, since CUDA is more restrictive than OpenMP) .

Compiler warnings are very useful and improve the software quality, so I hope that such warnings will be added to the nvcc compiler. Better error messages would be also very useful.


Something else: There are no bugs reported on the registered developers page … it would be nice to know of bugs in the CUDA toolkit to circumvent them until a bugfix is out.

Looks like it does with the 2.3 tookit.

__global__ void kernel(int *d_data)

{

  int i;

  d_data[threadIdx.x]=i;

}

int main()

{

  int *d_result;

  cudaMalloc((void**)&d_result, 256*sizeof(int));  

kernel<<<256, 256>>>(d_result); 

}
~/nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2009 NVIDIA Corporation

Built on Tue_Jul_14_12:31:13_PDT_2009

Cuda compilation tools, release 2.3, V0.2.1221

~/nvcc unused.cu

unused.cu

unused.cu(5): warning: variable "i" is used before its value is set

That is only because you aren’t asking it to. nvopencc has a huge range of warning options you can turn on if you want. For example (in linux at least):

nvcc -Xopencc "-Wall"

will generate all the warnings you could ever want, and some you don’t. There is information about passing additional arguments to nvopencc in the nvcc documentation and (again at least on linux) you can choose from:

avid@cuda:~/code/cuda_struct$ /opt/cuda-2.3/open64/bin/nvopencc --help 2>&1 | grep warn

	-LMSG::  option group to control error/warning messages in ld

	-W:  Enable extra warnings

	-Wall:  Enable most warning messages

	-Werror:  Make all warnings into errors

	-Wformat-nonliteral:  If -Wformat, warn if format string is not a string literal

	-Wformat-security:  If -Wformat, warn on potentially insecure format functions

	-Wmissing-format-attribute:  If -Wformat, warn on candidates for 'format' attributes

	-Wno-aggregate-return:  Do not warn about returning structures, unions or arrays

	-Wno-bad-function-cast:  Do not warn when a function call is cast to a non-matching type

	-Wno-cast-align:  Do not warn about pointer casts which increase alignment

	-Wno-cast-qual:  Do not warn about casts which discard qualifiers

	-Wno-char-subscripts:  Do not warn about subscripts whose type is 'char'

	-Wno-comment:  Do not warn if nested comments are detected

	-Wno-comments:  Do not warn if nested comments are detected

	-Wno-conversion:  Do not warn about possibly confusing type conversions

	-Wno-deprecated-declarations:  Do not warn about deprecated code

	-Wno-disabled-optimization:  Do not warn if a requested optimization pass is disabled

	-Wno-div-by-zero:  Suppress warnings about compile-time integer division by zero

	-Wno-endif-labels:  Do not warn if #if or #endif is followed by text

	-Wno-error:  Do not make all warnings into errors

	-Wno-error=:  makes -Ws warnings not be error

	-Wno-float-equal:  Do not warn if floating point values are compared for equality

	-Wno-format:  Do not warn about printf format anomalies

	-Wno-format-extra-args:  Do not warn about extra arguments to printf-like functions

	-Wno-format-nonliteral:  Do not warn if format string is not a string literal

	-Wno-format-security:  Do not warn on potentially insecure format functions

	-Wno-format-y2k:  Do not warn about 'strftime' formats that yield two-digit years

	-Wno-format-zero-length:  Do not warn about zero-length format string

	-Wno-implicit:  Do not warn about implicit declarations of functions or variables

	-Wno-implicit-function-declaration:  Do not warn when a function is used before being declared

	-Wno-implicit-int:  Do not warn when a declaration does not specify a type

	-Wno-import:  Do not warn about the use of the #import directive

	-Wno-inline:  Do not warn if a function declared as inline cannot be inlined

	-Wno-int-to-pointer-cast:  Don't warn for casts to pointers from integer types of a different size

	-Wno-larger-than-:  Do not warn if an object is larger than <number> bytes

	-Wno-long-long:  Do not warn if the long long type is used

	-Wno-main:  Do not warn about suspicious declarations of main

	-Wno-missing-braces:  Do not warn about possibly missing braces around initialisers

	-Wno-missing-declarations:  Do not warn about global funcs without previous declarations

	-Wno-missing-field-initializers:  Do not warn if a structure's initializer has some fields missing

	-Wno-missing-format-attribute:  Do not warn on candidates for 'format' attributes

	-Wno-missing-noreturn:  Do not warn about functions that are candidates for 'noreturn' attribute

	-Wno-missing-prototypes:  Do not warn about global funcs without prototypes

	-Wno-multichar:  Do not warn if a multicharacter constant is used

	-Wno-nested-externs:  Do not warn about externs not at file scope level

	-Wno-packed:  Do not warn when packed attribute of a struct has no effect

	-Wno-padded:  Do not warn when padding is included in a struct

	-Wno-parentheses:  Do not warn about possible missing parentheses

	-Wno-pointer-arith:  Do not warn about function pointer arithmetic

	-Wno-pointer-sign:  Don't warn for pointer asignments with different signedness

	-Wno-pointer-to-int-cast:  Don't warn for pointer casts to integer types of a different size

	-Wno-redundant-decls:  Do not warn about multiple declarations of the same object

	-Wno-return-type:  Do not warn when a function return type defaults to int

	-Wno-sequence-point:  Do not warn about code violating sequence point rules

	-Wno-shadow:  Do not warn when one local variable shadows another

	-Wno-sign-compare:  Do not warn about signed/unsigned comparisons

	-Wno-strict-aliasing:  Do not warn about code that breaks strict aliasing rules

	-Wno-strict-prototypes:  Do not warn about non-prototyped function decls

	-Wno-switch:  Do not warn when a switch statement is incorrectly indexed with an enum

	-Wno-system-headers:  Do not print warnings for constructs in system header files

	-Wno-traditional:  Do not warn about constructs whoes meaning change in ANSI C

	-Wno-trigraphs:  Do not warn when trigraphs are encountered

	-Wno-undef:  Do not warn if an undefined identifier appears in a #if directive

	-Wno-uninitialized:  Do not warn about unitialized automatic variables

	-Wno-unknown-pragmas:  Do not warn when an unknown #pragma directive is encountered

	-Wno-unreachable-code:  Do not warn about code that will never be executed

	-Wno-unused:  Do not warn when a variable is unused

	-Wno-unused-function:  Do not warn about unused static and inline functions

	-Wno-unused-label:  Do not warn about unused labels

	-Wno-unused-macros:  Do not warn about macros defined but not used

	-Wno-unused-parameter:  Do not warn about unused function parameters

	-Wno-unused-value:  Do not warn about statements whose results are not used

	-Wno-unused-variable:  Do not warnarn about local and static variables that are not used

	-Wno-vla:  prevent the -pedantic warning of the variable length array

	-Wsystem-headers:  Print warnings for constructs in system header files

	-fms-extensions:  Accept broken MFC extensions without warning

	-pedantic-errors:  Issue warnings needed by strict compliance to ANSI C

	-woffall:  turn off all warnings

	-woffoptions:  turn off warnings about options

Thanks for this information!

It is enabled by default on Visual Studio 2008, i tried your test code and get a warning. Try this code:

__global__ void kernel(int *d_data)

{

  int i;

if(d_data[0] == 1)

	  i = 2;

d_data[threadIdx.x]=i;

}

int main()

{

  int *d_result;

  cudaMalloc((void**)&d_result, 256*sizeof(int));  

kernel<<<256, 256>>>(d_result); 

}

– > 0 error(s), 0 warning(s)

I get no warnings for this, but with the MS compiler I do.

The variable i is only initialized in a special case … and you should be warned. I had this case in my code.