libtooling/libclang cannot find "__clang_cuda_runtime_wrapper.h"


I am attempting to write a static analyzer for CUDA using the Clang infrastructure.

After writing a simple tool, and giving it the example CUDA file in , I get the following error:

fatal error: '__clang_cuda_runtime_wrapper.h' file not found

so far this happens with both libtooling and libclang. I have tried using clang to compile the same CUDA file and it works.
I am currently linking against clang sources from the trunk, but this also happens with Clang version 6.
I currently have CUDA 10 installed on this computer.

Any help would be appreciated.


It would help a lot if you could figure out what compiler options we end up using when the file is handled by libtooling or libclang. The fact that you’ve got the error means that the file is treated as a CUDA file (good news) but we either didn’t add include path to clang headers, or the headers are not where they should be.

I don’t know if there’s an easy way to do it libtooling or libclang. In the past I dug these options out with debugger. You can try passing ‘-v’ and see if you get back the usual printout of header search path.


I did some more testing and the problem does not occur if I put the libTooling-based executable in the $LLVM_INSTALL_DIR/bin directory. I originally did an out of source build which would have put the executable not in tree with the rest of the LLVM library.
The libclang-based tool still exhibit the same issue no matter where I put the final executable.

I’ve attached the terminal output using the -v flag with tools I’ve made using libtooling, libclang, and also the output if I compile a test CUDA file using Clang. For some reason they all check different include directories…

I’ve also attached what would happen if you force the include of the CUDA wrappers using the -I flag, in short: A lot of weird errors.

– Franklin

libtooling_log.txt (4.27 KB)

libclang_log.txt (1.77 KB)

clang_log.txt (8.79 KB)

libclang_include_clang_directory_log.txt (4.81 KB)

It looks like -resource-dir is missing in case of libclang tool.
Try passing -Xclang -resource-dir=/path/to/clang/installation/root

You can get resouce dir with ‘clang -print-resource-dir’


Thanks for the help Artem.

After a bit of fumbling around, adding the resource directory results in the same error as directly including the clang directory via -I

I was hoping to use libclang as libTooling is restricted to either host or device code in a single pass from my understanding. In the meantime, I’ll proceed with libTooling.

– Franklin

Error message attached below:
$ ./cuda_libclang_sa /tmp/ -v --cuda-gpu-arch=sm_35 -resource-dir=/home/n0mad/code_libs/llvm_trunk/lib/clang/8.0.0

clang version 8.0.0 ( 3129a82102a7de23d44f02c0df89c1d6a535f7a7) ( 00233291cf25baa54e5d058610e8a985deaac9e4)

Target: x86_64-unknown-linux-gnu

Thread model: posix

InstalledDir: /home/n0mad/CLionProjects/cudadadadadadadadadada/cmake-build-debug/libclang

Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/8

Selected GCC installation: /usr/lib/gcc/x86_64-redhat-linux/8

Candidate multilib: .;@m64

Candidate multilib: 32;@m32

Selected multilib: .;@m64

Found CUDA installation: /usr/local/cuda-10.0, version 10.0

ignoring nonexistent directory “/include”

ignoring nonexistent directory “/include”

ignoring duplicate directory “/usr/lib/gcc/x86_64-redhat-linux/8/…/…/…/…/include/c++/8”

ignoring duplicate directory “/usr/lib/gcc/x86_64-redhat-linux/8/…/…/…/…/include/c++/8/x86_64-redhat-linux”

ignoring duplicate directory “/usr/lib/gcc/x86_64-redhat-linux/8/…/…/…/…/include/c++/8/backward”

ignoring duplicate directory “/usr/local/include”

ignoring duplicate directory “/home/n0mad/code_libs/llvm_trunk/lib/clang/8.0.0/include”

ignoring duplicate directory “/usr/include”

#include “…” search starts here:

#include <…> search starts here:









End of search list.

/home/n0mad/code_libs/llvm_trunk/lib/clang/8.0.0/include/__clang_cuda_builtin_vars.h:67:27: error: use of undeclared identifier ‘__nvvm_read_ptx_sreg_tid_x’

/home/n0mad/code_libs/llvm_trunk/lib/clang/8.0.0/include/__clang_cuda_builtin_vars.h:68:27: error: use of undeclared identifier ‘__nvvm_read_ptx_sreg_tid_y’

/home/n0mad/code_libs/llvm_trunk/lib/clang/8.0.0/include/__clang_cuda_builtin_vars.h:69:27: error: use of undeclared identifier ‘__nvvm_read_ptx_sreg_tid_z’

Now the problem is that you somehow ended up targeting x86 during GPU-side compilation.
One thing you need to keep in mind when using libclang or libtooling with CUDA is that compiling a CUDA file is actually N different compilations - one for the host and one per specified GPU architecture.

I’m not quite sure what libclang or libtooling does in this case. You may want to avoid the ambiguity and use --cuda-host-only or --cuda-device-only with only one GPU variant specified. I’m not sure if that’s going to fix this particular problem, but it’s something you will probably need to do in any case.