Nvcc misses the variable ? Problem report

With CUDA 2.0 Beta2 the following problem arised: a number of “warning: variable ### was set but never used” warnings appeared with the code that never generated them.

It is very easy to reproduce the problem, just compile:

int main(int argc, char **argv)

{

    cudaError err;

    if (true)

        err = cudaSuccess;

}

and see the warning for the “err” variable.

However, it is possible that this warning is actually a good thing rather than bad as I’ve noticed a number of times that this code:

while (pBranchLengths[nBranch])

{

    ...

    nBranch++;

    ...

}

works fine but this code:

int nBranchLength = pBranchLengths[nBranch];

while (nBranchLength)

{

    ...

    nBranch++;

    nBranchLength = pBranchLengths[nBranch];

    ...

}

does not (in both CUDA 1.1 and 2.0). And with CUDA 2.0 Beta2 compiler started to warning me that nBranchLength variable was set but never used (under 1.1 no warnings were produced). This is kind of strange and annoying problem, it is not clear whether the warning is unneeded or the code is compiled with mistakes and this warning discloses it.

Regards,

Romant.

Hi,

The warning indicates that you did set a variable, but did not act on its contents. For example, in the following program:

int main() {

     int x = 3;

     return 0;

}

The setting of the variable x doesn’t change the outcome of the program, so this is an extraneous statement. This is to prevent resource waste. This is almost a perfect mapping of what happened in your first code: while err was set, your programs behavior did not change based on err’s value.

In your second example, it is possible that the optimizer got you. Your code does this:

int nBranchLength = pBranchLengths[nBranch];

while (nBranchLength)

{

   ...

   nBranch++;

   nBranchLength = pBranchLengths[nBranch];

   ...

}

However, if these are the only uses of your variable nBranch, the following may have been substituted in its place:

int nBranchLength = pBranchLengths[nBranch];

while (pBranchLengths[nBranch])

{

   ...

   nBranch++;

   ...

}

The optimizer eliminated an instruction, but not the declaration of the variable. Since you never used the variable again, it does not affect the correctness of the program. Still, the extraneous instruction is there.

Many thanks for the clarification, now I see.