[cfe-dev] Clang and CUDA with C++11 features

Peter Colberg peter at colberg.org
Wed Jun 13 18:57:01 PDT 2012


Hi,

I am experimenting with CUDA language support in Clang, and so far
the kernel-call test in Clang trunk (r158426) compiles and runs, on
a Tesla C2050.

Now I would like to enable C++11 support in Clang to use compile-time
C++11 features in GPU code, primarily variadic templates and lambda
functions.

The following code compiles fine:

    // kernel-call.cu

    #include <cuda_runtime.h>

    __attribute__((global)) void g1(int x) {}

    int main(void) {
      // CHECK: call{{.*}}cudaConfigureCall
      // CHECK: icmp
      // CHECK: br
      // CHECK: call{{.*}}g1
      g1<<<1, 1>>>(42);
    }

    clang++ -std=cuda -I/usr/local/cuda-4.2/cuda/include -L/usr/local/cuda-4.2/cuda/lib64 -lcudart -o kernel-call kernel-call.cu

After adding a lambda function (in the host code), compilation fails:

    #include <cuda_runtime.h>

    __attribute__((global)) void g1(int x) {}

    int main(void) {
      // CHECK: call{{.*}}cudaConfigureCall
      // CHECK: icmp
      // CHECK: br
      // CHECK: call{{.*}}g1
      g1<<<1, 1>>>(42);

      auto lambda = [](){};
    }

    clang++ -std=cuda -I/usr/local/cuda-4.2/cuda/include -L/usr/local/cuda-4.2/cuda/lib64 -lcudart -o kernel-call kernel-call.cu 
    kernel-call.cu:14:3: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
      auto lambda = [](){};
      ^
    kernel-call.cu:14:17: error: expected expression
      auto lambda = [](){};
                    ^
    1 warning and 1 error generated.

As expected, -std=c++11 does not work, since it disables CUDA support:

    clang++ -std=c++11 -I/usr/local/cuda-4.2/cuda/include -L/usr/local/cuda-4.2/cuda/lib64 -lcudart -o kernel-call kernel-call.cu 
    kernel-call.cu:5:16: warning: global attribute ignored
    __attribute__((global)) void g1(int x) {}
                   ^
    kernel-call.cu:12:7: error: expected expression
      g1<<<1, 1>>>(42);
          ^
    kernel-call.cu:12:14: error: expected expression
      g1<<<1, 1>>>(42);
                 ^
    1 warning and 2 errors generated.


Is there a way to tell Clang to enable C++11 extensions?

Thanks,
Peter



More information about the cfe-dev mailing list