Can C++17 be used together with CUDA using clang?
Yes, as you already guessed the CUDA clang frontend is indeed ahead in C++ feature support, even in device code. It was already in the past, introducing C++14 features before NVCC which was mostly unnoticed by the community.
Take this C++17, unnecessarily modified if constexpr
, snippet: Fibo
#include <cuda_runtime.h>
#include <cstdio>
constexpr unsigned
fibonacci(const unsigned x) {
if constexpr (false)
{
return 0u;
}
if( x <= 1 )
return 1;
return fibonacci(x - 1) + fibonacci(x - 2);
}
__global__
void k()
{
constexpr unsigned arg = fibonacci(5);
printf("%u", arg);
}
int main()
{
k<<<1,1>>>();
return 0;
}
It already runs with clang++ -std=c++17 -x cuda
: https://cuda.godbolt.org/z/GcIqeW
Nevertheless, for this specific example, C++17 extended lambdas and C++14 relaxed constexpr are that important in modern C++, that even in C++11 and C++14 mode of NVCC 8.0+ flags were added to enable those features already: https://devblogs.nvidia.com/new-compiler-features-cuda-8/
That means the above example compiles for example with NVCC 9.2 even without __device__
qualifiers when removing the demonstrating C++17 if constexpr
construct and adding -std=c++14 --expt-relaxed-constexpr
flags.
Here is a list about C++ standard support on the device side for nvcc
and clang -x cuda
: https://gist.github.com/ax3l/9489132#device-side-c-standard-support (NVCC 11.0 supports device-side C++17 now.)
Currently up to C++14 is supported in device code (Introduced in CUDA 9)
--std {c++03|c++11|c++14}
Options for Specifying Behavior of Compiler/Linker
However, if your host is only using C++17, it should be possible to use separate compilation and link them with library. Separate Compilation and Linking of CUDA C++ Device Code
Update: formatting and more info