CUDA without nvcc

It is possible to compile CUDA programs for running on the GPU without using nvcc. This is what I'm doing in a project:

  1. Install the CUDA Toolkit
  2. Install a compiler capable of generating PTX assembly. I used clang 22.1, with the NVPTX target
  3. Compile your sources into LLVM bitcode, using target nvptx64-nvidia-cuda
  4. Use llvm-link and opt to link all bitcode files. Optionally, also merge libdevice from the CUDA SDK
  5. Generate ptx assembly code using llc
  6. Optionally, assemble the ptx into machine code using ptxas from the CUDA SDK

The resulting binary can be run on the GPU using the CUDA Driver API, and works fine with cuda-gdb, ncu, etc

An example program, including Makefile and host code, can be downloaded below.

Why do this instead of just using nvcc? One reason is that you can compile your sources as C++ for OpenCL, which the NVIDIA drivers don't support (actually the main reason I did all this). Another big reason is that there is no need to add the __device and/or __host function qualifiers to all functions in the program, so there is less clutter in the source code.

User: guest, password: guest (info)
cudatest.tar.xz [SIG] example code 3 KB 2026-Jun-09