[llvm] r264002 - [CUDA] Add documentation explaining how to detect clang vs nvcc.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 21 16:05:15 PDT 2016

Author: jlebar
Date: Mon Mar 21 18:05:15 2016
New Revision: 264002

URL: http://llvm.org/viewvc/llvm-project?rev=264002&view=rev
[CUDA] Add documentation explaining how to detect clang vs nvcc.


Modified: llvm/trunk/docs/CompileCudaWithLLVM.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CompileCudaWithLLVM.rst?rev=264002&r1=264001&r2=264002&view=diff
--- llvm/trunk/docs/CompileCudaWithLLVM.rst (original)
+++ llvm/trunk/docs/CompileCudaWithLLVM.rst Mon Mar 21 18:05:15 2016
@@ -119,6 +119,34 @@ your GPU <https://developer.nvidia.com/c
 to run your program on a GPU with compute capability of 3.5, you should specify
+Detecting clang vs NVCC
+Although clang's CUDA implementation is largely compatible with NVCC's, you may
+still want to detect when you're compiling CUDA code specifically with clang.
+This is tricky, because clang defines the ``__NVCC__`` macro, and because NVCC
+may invoke clang as part of its own compilation process!  For example, NVCC
+uses the host compiler's preprocessor when compiling for device code, and that
+host compiler may in fact be clang.
+When clang is actually compiling CUDA code -- rather than being used as a
+subtool of NVCC's -- it defines the ``__CUDA__`` macro.  ``__CUDA_ARCH__`` is
+defined only in device mode (but will be defined if NVCC is using clang as a
+preprocessor).  So you can use the following incantations to detect clang CUDA
+compilation, in host and device modes:
+.. code-block:: c++
+  #if defined(__clang__) && defined(__CUDA__) && !defined(__CUDA_ARCH__)
+    // clang compiling CUDA code, host mode.
+  #endif
+  #if defined(__clang__) && defined(__CUDA__) && defined(__CUDA_ARCH__)
+    // clang compiling CUDA code, device mode.
+  #endif

More information about the llvm-commits mailing list