1 ===================================
2 Compiling CUDA C/C++ with LLVM
3 ===================================
11 This document contains the user guides and the internals of compiling CUDA
12 C/C++ with LLVM. It is aimed at both users who want to compile CUDA with LLVM
13 and developers who want to improve LLVM for GPUs. This document assumes a basic
14 familiarity with CUDA. Information about CUDA programming can be found in the
15 `CUDA programming guide
16 <http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html>`_.
18 How to Build LLVM with CUDA Support
19 ===================================
21 Below is a quick summary of downloading and building LLVM. Consult the `Getting
22 Started <http://llvm.org/docs/GettingStarted.html>`_ page for more details on
27 .. code-block:: console
29 $ cd where-you-want-llvm-to-live
30 $ svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
34 .. code-block:: console
36 $ cd where-you-want-llvm-to-live
38 $ svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
40 #. Configure and build LLVM and Clang
42 .. code-block:: console
44 $ cd where-you-want-llvm-to-live
50 How to Compile CUDA C/C++ with LLVM
51 ===================================
53 We assume you have installed the CUDA driver and runtime. Consult the `NVIDIA
54 CUDA installation Guide
55 <https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html>`_ if
58 Suppose you want to compile and run the following CUDA program (``axpy.cu``)
59 which multiplies a ``float`` array by a ``float`` scalar (AXPY).
63 #include <helper_cuda.h> // for checkCudaErrors
67 __global__ void axpy(float a, float* x, float* y) {
68 y[threadIdx.x] = a * x[threadIdx.x];
71 int main(int argc, char* argv[]) {
72 const int kDataLen = 4;
75 float host_x[kDataLen] = {1.0f, 2.0f, 3.0f, 4.0f};
76 float host_y[kDataLen];
78 // Copy input data to device.
81 checkCudaErrors(cudaMalloc(&device_x, kDataLen * sizeof(float)));
82 checkCudaErrors(cudaMalloc(&device_y, kDataLen * sizeof(float)));
83 checkCudaErrors(cudaMemcpy(device_x, host_x, kDataLen * sizeof(float),
84 cudaMemcpyHostToDevice));
87 axpy<<<1, kDataLen>>>(a, device_x, device_y);
89 // Copy output data to host.
90 checkCudaErrors(cudaDeviceSynchronize());
91 checkCudaErrors(cudaMemcpy(host_y, device_y, kDataLen * sizeof(float),
92 cudaMemcpyDeviceToHost));
95 for (int i = 0; i < kDataLen; ++i) {
96 std::cout << "y[" << i << "] = " << host_y[i] << "\n";
99 checkCudaErrors(cudaDeviceReset());
103 The command line for compilation is similar to what you would use for C++.
105 .. code-block:: console
107 $ clang++ -o axpy -I<CUDA install path>/samples/common/inc -L<CUDA install path>/<lib64 or lib> axpy.cu -lcudart_static -lcuda -ldl -lrt -pthread
114 Note that ``helper_cuda.h`` comes from the CUDA samples, so you need the
115 samples installed for this example. ``<CUDA install path>`` is the root
116 directory where you installed CUDA SDK, typically ``/usr/local/cuda``.
121 CPU and GPU have different design philosophies and architectures. For example, a
122 typical CPU has branch prediction, out-of-order execution, and is superscalar,
123 whereas a typical GPU has none of these. Due to such differences, an
124 optimization pipeline well-tuned for CPUs may be not suitable for GPUs.
126 LLVM performs several general and CUDA-specific optimizations for GPUs. The
127 list below shows some of the more important optimizations for GPUs. Most of
128 them have been upstreamed to ``lib/Transforms/Scalar`` and
129 ``lib/Target/NVPTX``. A few of them have not been upstreamed due to lack of a
130 customizable target-independent optimization pipeline.
132 * **Straight-line scalar optimizations**. These optimizations reduce redundancy
133 in straight-line code. Details can be found in the `design document for
134 straight-line scalar optimizations <https://goo.gl/4Rb9As>`_.
136 * **Inferring memory spaces**. `This optimization
137 <http://www.llvm.org/docs/doxygen/html/NVPTXFavorNonGenericAddrSpaces_8cpp_source.html>`_
138 infers the memory space of an address so that the backend can emit faster
139 special loads and stores from it. Details can be found in the `design
140 document for memory space inference <https://goo.gl/5wH2Ct>`_.
142 * **Aggressive loop unrooling and function inlining**. Loop unrolling and
143 function inlining need to be more aggressive for GPUs than for CPUs because
144 control flow transfer in GPU is more expensive. They also promote other
145 optimizations such as constant propagation and SROA which sometimes speed up
146 code by over 10x. An empirical inline threshold for GPUs is 1100. This
147 configuration has yet to be upstreamed with a target-specific optimization
148 pipeline. LLVM also provides `loop unrolling pragmas
149 <http://clang.llvm.org/docs/AttributeReference.html#pragma-unroll-pragma-nounroll>`_
150 and ``__attribute__((always_inline))`` for programmers to force unrolling and
153 * **Aggressive speculative execution**. `This transformation
154 <http://llvm.org/docs/doxygen/html/SpeculativeExecution_8cpp_source.html>`_ is
155 mainly for promoting straight-line scalar optimizations which are most
156 effective on code along dominator paths.
158 * **Memory-space alias analysis**. `This alias analysis
159 <http://reviews.llvm.org/D12414>`_ infers that two pointers in different
160 special memory spaces do not alias. It has yet to be integrated to the new
161 alias analysis infrastructure; the new infrastructure does not run
162 target-specific alias analysis.
164 * **Bypassing 64-bit divides**. `An existing optimization
165 <http://llvm.org/docs/doxygen/html/BypassSlowDivision_8cpp_source.html>`_
166 enabled in the NVPTX backend. 64-bit integer divides are much slower than
167 32-bit ones on NVIDIA GPUs due to lack of a divide unit. Many of the 64-bit
168 divides in our benchmarks have a divisor and dividend which fit in 32-bits at
169 runtime. This optimization provides a fast path for this common case.