C++ for OpenCL compile problem
My envirment: Ubuntu clang version 12.0.0-3ubuntu1~20.04.3 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin
My problem: when I do this: $ echo "kernel void k(){}" > test.cl $ clang -cl-std=CLC++ ./test.cl
error raises that:
/usr/bin/ld: /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crt1.o: in function _start': (.text+0x24): undefined reference to main'
I wonder how I can solve this problem? I can't even compile the simplest .cl file which supports C++ features. Thanks guys!
Hi @Glinttsd, I've transferred your issue to the OpenCL-Guide repository for now.
Have you had a look at the OpenCL guide section describing how to compile OpenCL kernels using offline tools?
https://github.com/KhronosGroup/OpenCL-Guide/blob/main/chapters/os_tooling.md
It provides several different command lines you can use to compile OpenCL kernels using clang, including OpenCL kernels using C++ features.
If it's helpful, you can also refer to this example, which we've used in several Khronos presentations:
https://godbolt.org/z/NGZw9U
Please let us know if these links answer your questions and if you have any suggestions to improve the OpenCL guide content. Thanks!
Thank you very much! It seems like I can't use clang to do all the work, it's just one step of the whole compiling process right? I am trying to go through the whole process. I am also wondering if I can use .bc file in clCreateProgramWithIL and use .spv file in clCreateProgramWithBinary , is that right? Thanks again for your patience!
It seems like I can't use clang to do all the work, it's just one step of the whole compiling process right?
Correct, for what you are trying to do Clang is one of three steps:
- Clang compiles from OpenCL C (or C++) to LLVM IR (.bc).
- The SPIR-V LLVM Translator translates from LLVM IR to SPIR-V (.spv).
- Your application host code loads SPIR-V from a .spv file and passes it to
clCreateProgramWithIL. From there, you would compile or build the program like usual, e.g. usingclBuildProgram.
If it's helpful I have an example demonstrating (3) here:
- https://github.com/bashbaug/SimpleOpenCLSamples
- https://github.com/bashbaug/SimpleOpenCLSamples/tree/master/samples/05_spirvkernelfromfile
- https://github.com/bashbaug/SimpleOpenCLSamples/blob/master/samples/05_spirvkernelfromfile/main.cpp#L84
I am also wondering if I can use
.bcfile inclCreateProgramWithILand use.spvfile inclCreateProgramWithBinary, is that right?
Not quite. The recommended flow today is to load SPIR-V from a .spv file and pass it to clCreateProgramWithIL. Some devices still support the older SPIR (note SPIR, not SPIR-V) extension cl_khr_spir. This allows passing a very specific version of LLVM bitcode from a .bc file to clCreateProgramWithBinary - see the extension spec here and the SPIR binary spec here - but this is an older flow that has largely been superseded by the SPIR-V process described above.
I know this isn't a forum per se, but I have been racking my brain about exactly the same issue and I am no step closer to finding a solution. My main goal is to translate a CUDA application that uses C++ kernels to OpenCL to be able to run it on the same Nvidia card but also on, say, integrated Intel graphics cards. I have been able to compile C++ kernels using clang but am unable to load them into my OpenCL application using clCreateProgramWithBinary. On Ubuntu, the OpenCL implementation bundled with the Nvidia driver does not support clCreateProgramWithIL, so I cannot go the preferred SPIR-V way. Is there any way for me to load the clang-generated binaries into my OpenCL application?
Thanks so much in advance for any help!
I know this isn't a forum per se, but I have been racking my brain about exactly the same issue and I am no step closer to finding a solution. My main goal is to translate a CUDA application that uses C++ kernels to OpenCL to be able to run it on the same Nvidia card but also on, say, integrated Intel graphics cards. I have been able to compile C++ kernels using clang but am unable to load them into my OpenCL application using
clCreateProgramWithBinary.
For that, Khronos has another standard named "SYCL" https://www.khronos.org/sycl/ with various implementations for all the common accelerators, some also using an OpenCL back-end. There is even a third-party CUDA-to-SYCL translator https://github.com/oneapi-src/SYCLomatic
I know this isn't a forum per se, but I have been racking my brain about exactly the same issue and I am no step closer to finding a solution. My main goal is to translate a CUDA application that uses C++ kernels to OpenCL to be able to run it on the same Nvidia card but also on, say, integrated Intel graphics cards. I have been able to compile C++ kernels using clang but am unable to load them into my OpenCL application using
clCreateProgramWithBinary.For that, Khronos has another standard named "SYCL" https://www.khronos.org/sycl/ with various implementations for all the common accelerators, some also using an OpenCL back-end. There is even a third-party CUDA-to-SYCL translator https://github.com/oneapi-src/SYCLomatic
Thank you so much for your answer, I will look into that!