如何避免由于编译时计算的值而导致"unreachable statement"?



如果我编写以下 CUDA 代码:

#include <stdio.h>
template <unsigned N>
__global__ void foo()
{
    printf("In kernel foo() with N = %un", N);
    if (N < 10) { return; }
    printf("Wow, N is really high!n");
    /* a whole lot of code here which I don't want to indent */
}
int main() {
    foo<5><<<1,1>>>();
    foo<20><<<1,1>>>();
    return 0;
}

我收到编译器警告:

a.cu(8): warning: statement is unreachable
          detected during instantiation of "void foo<N>() [with N=5U]" 
(12): here

"觉得"我不应该收到此警告,因为无法访问的代码仅对模板参数的某些值无法访问。如果我写"CPU等效",可以这么说:

#include <cstdio>
template <unsigned N>
void foo()
{
    std::printf("In kernel foo() with N = %un", N);
    if (N < 10) { return; }
    std::printf("Wow, N is really high!n");
    /* a whole lot of code here which I don't want to indent */
}
int main() {
    foo<5>();
    foo<20>();
    return 0;
}

并使用 gcc (5.4.0( 构建它 - 即使我使用 -Wall 编译,我也不会收到任何警告.

现在,我可以通过写作来规避这一点

if (not (N < 10)) { 
    printf("Wow, N is really high!n");
    /* a whole lot of code here which I don't want to indent */
}

但我宁愿避免不得不颠倒我的逻辑来跳过 nvcc 的"箍"。我也可以写

if (not (N < 10)) { 
    return;
}
else {
    printf("Wow, N is really high!n");
    /* a whole lot of code here which I don't want to indent */
}

但是 - 我不想缩进所有代码(同样的问题可能会再次发生,需要在 else 块内进行更多缩进。

我能做些什么吗?另外,这不是一个"错误",还是我应该报告为错误的错误功能?

怎么样:

template<unsigned N, bool>
struct FooImpl
{
  static void foo()
  {
    std::printf("In kernel foo() with N = %un", N);
  }
};
template<unsigned N>
struct FooImpl<N, false>
{
  static void foo()
  {
    std::printf("In kernel foo() with N = %un", N);
    std::printf("Wow, N is really high!n");
    /* a whole lot of code here which I don't want to indent */
  }
};
template <unsigned N>
__global__ void foo()
{
  FooImpl<N, N < 10>::foo();
}

最新更新