Disable a warning?

Is there a way to disable a specific warning in nvcc? I need to turn off the ““some.cu”, line 30: warning: variable “x” was set but never used” warnings, if possible.

Thanks,
John

Not a very good idea to disable a warning… in this case the compiler has most likely optimised out everything to do with “x”. Assign its value to something that it can’t track like global memory allocated from the host.
Eric

That’s the idea - I have X and I know it is being optimized out and I’m glad of that, but I don’t need to be spammed by messages repeating it over and over again - it makes finding the worthwhile warnings a chore. In some cases, it’s ok to have code that just dies off if it’s not needed - this particular instance is some templated code that essentially disables some functionality based on template parameters. And the the warnings for templated code can be very very long depending on how deep the templating goes.

John, I had a vague feeling this can be done in gcc, so I had a look at gnu’s gcc documentation (gcc 3.4.6 pdf) which I grabbed from gnu.org.

In section 5.32 page 210 (pdf page 222) it says that ‘attribute followed by “unused” [page 214] can be applied to variables or structures’. I’m in bed, drinking my camomile tea, so you’ll have to try it yourself :)

HTH.

Garry

PS: Please tell us if it works :)

I notice Cyril’s reply to this question [url=“The Official NVIDIA Forums | NVIDIA”]http://forums.nvidia.com/index.php?showtopic=33616[/url] and the answer is no can do. I use my own makefiles with CUDA and don’t pass any warning flags to nvcc and these warnings still arrive.
Eric

I think Cyril was giving an answer to a different question in that topic. I believe Cyril’s answer to that question at http://forums.nvidia.com/index.php?showtopic=33616 is correct.

But, an answer to the question:

is yes, but the constraint may be too great.

Annotate the specific variable declarations that you are trying to suppress with “attribute ((unused))”. It works. (sorry, I was vague/misleading about the syntax in my previous post, but I did issue a sleepiness warning “in bed, drinking my camomile tea” :rolleyes: )

For example

const unsigned int timerId = blockIdx.x + blockIdx.y * gridDim.x;

becomes:

const unsigned int __attribute__((unused)) timerId = blockIdx.x + blockIdx.y * gridDim.x;

which works fine.

This may be too restrictive as you may want the warning in general, but suppress it in specific template expansions. I haven’t found a way to do that.

if I just insert the statement:

__attribute__((unused)) timerId;

somewhere (I tried the end of the function). I get an extra warning:

“cuda_kernel.cu”, line 60: omission of explicit type is nonstandard.

Trying to silence nvcc by providing a second declaration, which differs by including the attribute((unused), gets an error about timerId already declared.

HTH.

Garry

Health warning I wrote this reply on my Mac, but ran the code on a Linux box, so I may have made a transcription error, but the code works under Linux.

Depends on where the unused variable is. For the host code, you can pass any compiler flags using the -Xcompiler switch. See nvcc --help

Unfortunately, for the device code there is no such nvcc switch for passing arguments to the preprocessor (gcc -E) or the cudafe. Here the attribute((unused)) trick works. Would be nice, if NVIDIA could expose something like an nvcc -Xpreproc switch.

Peter

Hi guys, thanks for all the answers. To answer some of the questions asked so far:

The warnings are being produced when the variable is in device code, and the producer is the cudafe program. I am currently working in MSVC, so the attribute((unused)) trick is not recognized. Although even if I was using GCC, I don’t think it would be my solution since the problem is from cudafe. It seems that it’s currently not possible to alter the behavior of that program, so I think this makes things impossible at this moment for me.

The reason I’m looking for this is an interesting one. I have some templated code I am using on the device. In one case, I need to ensure that a template parameter has been derived from a certain base class. In this case, there is the interesting solution found in Exceptional C++, by Herb Sutter. Source: http://www.gotw.ca/publications/mxc+±item-4.htm

// Example 4-3(b): An IsDerivedFrom2 constraints base class

//

// Advantages: Compile-time evaluation

//             Simpler to use directly

// Drawbacks:  Not directly usable for compile-time value test

//

template<typename D, typename B>

class IsDerivedFrom2

{

  static void Constraints(D* p)

  {

    B* pb = p;

    pb = p; // suppress warnings about unused variables

  } 

protected:

  IsDerivedFrom2() { void(*p)(D*) = Constraints; }

}; 

// Force it to fail in the case where B is void

template<typename D>

class IsDerivedFrom2<D, void>

{

  IsDerivedFrom2() { char* p = (int*)0; /* error */ }

}; 

Now the check is much simpler: 

// Example 4-3(b), continued: Using IsDerivedFrom2

// constraints base to enforce derivation from Cloneable

//

template<typename T>

class X : IsDerivedFrom2<T,Cloneable>

{

  // ...

};

The wonderful thing about this class is that it will be fully optimized away, leaving you with a compile-time check of your template parameters with no runtime overhead. Heck, the resulting error messages are even nearly readable to tell you what you did wrong. All in all, it’s an elegant solution.

The bad side is if you cannot suppress the warnings. In this case, you end up with pages of warnings that look like this, every time you use this object:

1>"file.cu", line 30: warning: variable "pb" was set but never used

1>       B  * pb = p;

1>            ^

1>          detected during:

1>            instantiation of "void IsDerivedFrom2<D, B>::Constraints(D *) [with

1>                      D=derived, B=base]" at line 34

1>            instantiation of "IsDerivedFrom2<D, B>::IsDerivedFrom2() [with

1>                      D=derived, B=base]" at line 202

1>            instantiation of "some_class<T>::some_class()

1>                      [with T=derived]"

(And that’s when the check passes!)

John, I apologise for asking this, but what happens if you do change:

...

 static void Constraints(D* p)

 {

   B* pb = p;

   pb = p; // ...

 } 

...

into

static void Constraints(D* p)

 {

   B* __attribute__((unused)) pb = p;

   pb = p; // ...

 }

I’m curious, so I’m only asking to satisfy my thirst :rolleyes:

Garry

PS - I haven’t got Visual Studio/MSVC, or, I promise, I wouldn’t ask :)

PPS - I understand you’re likely very busy, and I don’t want to waste your time.

Thanks for the all the responses so far (especially Garry.) I just tried the attribute(unused) trick in MSVC and it appears to be a no-go:

1>"file.cu", line 30: error: identifier "unused" is undefined

1>      B* __attribute__((unused)) pb = p;

1>                        ^

1>          detected during:

1>            instantiation of "void IsDerivedFrom<D, B>::Constraints(D *) [with

1>                      (blah blah blah)

1>"file.cu", line 30: error: expected a ";"

1>      B* __attribute__((unused)) pb = p;

1>                                 ^

1>          detected during:

1>            instantiation of "void IsDerivedFrom<D, B>::Constraints(D *) [with

1>                     (blah blah blah)

Strangely, this is from the cudafe step, so it really has nothing at all to do with MSVC yet, which I don’t entirely understand.

Also, you’ll note that the attribute() portion passes the compilation, whereas the “unused” is what messes it up.

Dear NVIDIA,

I’ve run into this problem too. It would be great if it were possible to disable certain warnings for device code in NVCC.

Jim

nvcc allows passing options to cudafe using -Xcudafe. cudafe seems to based on the C++ Front End from the Edison Design Group, and therefore can take the same command line options: http://www.edg.com/docs/edg_cpp.pdf

To suppress a warning, you can use --diag_suppress. The actual warning codes or tags are however not so easy to come by. I have found a list here:

http://www.ssl.berkeley.edu/~jimm/grizzly_docs/SSL/opt/intel/cc/9.0/lib/locale/en_US/mcpcom.msg

This is how I got rid of annoying warnings while keeping the ones I want:

-Xcudafe “–diag_suppress=base_class_has_different_dll_interface --diag_suppress=useless_type_qualifier_on_return_type --diag_suppress=nested_comment --diag_suppress=bad_combination_of_dll_attributes --diag_suppress=field_without_dll_interface”

This information was incredibly hard to come by, I do not understand why this is so badly documented.

I would like to bump this thread.

Disabling these warnings is pretty common on Windows: /wd4251 /wd4275

I had a hard time to find out how to suppress these warnings during cudafe compilation.

First I had to find out the error numbers with:
-Xcudafe=“–display_error_number”
Then I could disable them:
-Xcudafe=“–diag_suppress=1388 --diag_suppress=1394”

Please document the Xcudafe option!!!

1 Like

you can request changes to the documentation by filing a bug at developer.nvidia.com

[url]https://devtalk.nvidia.com/default/topic/1044668/cuda-programming-and-performance/-how-to-report-a-bug/[/url]