CUDA: NVCC gives controlling expression is constant warning on assert

前端 未结 5 1897
小鲜肉
小鲜肉 2021-01-18 14:09

I get the warning controlling expression is constant on assert statement like this:

assert(... && \"error message\");

相关标签:
5条回答
  • 2021-01-18 14:45

    I ran into this exact problem and finally found a way to disable warnings on the device. Here are the details...

    To disable a specific warning, you need to use the -Xcudafe flag combined with a token listed on this page (http://www.ssl.berkeley.edu/~jimm/grizzly_docs/SSL/opt/intel/cc/9.0/lib/locale/en_US/mcpcom.msg). For example, to disable the "controlling expression is constant" warning, pass the following to NVCC:

    -Xcudafe "--diag_suppress=boolean_controlling_expr_is_constant"

    This worked for me! For other warnings, see the above link.

    0 讨论(0)
  • 2021-01-18 14:46

    A portable alternative (possibly wrapped in a macro) would be something like:

     {
         const bool error_message = true;
         assert([...] && error_message);
     }
    

    To clear up what i meant:

    #define myAssert(msg, exp) { const bool msg(true); assert(msg && (exp)); }
    // usage:
    myAssert(ouch, a && b);
    

    ... gives e.g.:

    assertion "ouch && (a && b)" failed [...]

    0 讨论(0)
  • 2021-01-18 14:55

    Try #pragma warning.

    0 讨论(0)
  • 2021-01-18 15:02

    Add the following somewhere near the beginning of your code:

    #pragma diag_suppress boolean_controlling_expr_is_constant
    

    Note that this will suppress all compiler warnings for "controlling expression is constant", not just the warning associated with your assertions.

    I sometimes get this warning when checking constants or properties that are defined (or can be derived) at compile time, so in my experience the warning has been generally benign and OK to ignore.

    0 讨论(0)
  • 2021-01-18 15:02

    If it is LLVM based, then you can use something like this:

    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wmultichar"
    
    char b = 'df'; // no warning.
    
    #pragma clang diagnostic pop
    

    From Controlling Diagnostics via Pragmas.

    0 讨论(0)
提交回复
热议问题