1 ==========================
2 Auto-Vectorization in LLVM
3 ==========================
8 LLVM has two vectorizers: The :ref:`Loop Vectorizer <loop-vectorizer>`,
9 which operates on Loops, and the :ref:`Basic Block Vectorizer
10 <bb-vectorizer>`, which optimizes straight-line code. These vectorizers
11 focus on different optimization opportunities and use different techniques.
12 The BB vectorizer merges multiple scalars that are found in the code into
13 vectors while the Loop Vectorizer widens instructions in the original loop
14 to operate on multiple consecutive loop iterations.
24 LLVM's Loop Vectorizer is now available and will be useful for many people.
25 It is not enabled by default, but can be enabled through clang using the
28 .. code-block:: console
30 $ clang -fvectorize -O3 file.c
32 If the ``-fvectorize`` flag is used then the loop vectorizer will be enabled
33 when running with ``-O3``, ``-O2``. When ``-Os`` is used, the loop vectorizer
34 will only vectorize loops that do not require a major increase in code size.
36 We plan to enable the Loop Vectorizer by default as part of the LLVM 3.3 release.
41 The loop vectorizer uses a cost model to decide on the optimal vectorization factor
42 and unroll factor. However, users of the vectorizer can force the vectorizer to use
43 specific values. Both 'clang' and 'opt' support the flags below.
45 Users can control the vectorization SIMD width using the command line flag "-force-vector-width".
47 .. code-block:: console
49 $ clang -mllvm -force-vector-width=8 ...
50 $ opt -loop-vectorize -force-vector-width=8 ...
52 Users can control the unroll factor using the command line flag "-force-vector-unroll"
54 .. code-block:: console
56 $ clang -mllvm -force-vector-unroll=2 ...
57 $ opt -loop-vectorize -force-vector-unroll=2 ...
62 The LLVM Loop Vectorizer has a number of features that allow it to vectorize
65 Loops with unknown trip count
66 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68 The Loop Vectorizer supports loops with an unknown trip count.
69 In the loop below, the iteration ``start`` and ``finish`` points are unknown,
70 and the Loop Vectorizer has a mechanism to vectorize loops that do not start
71 at zero. In this example, 'n' may not be a multiple of the vector width, and
72 the vectorizer has to execute the last few iterations as scalar code. Keeping
73 a scalar copy of the loop increases the code size.
77 void bar(float *A, float* B, float K, int start, int end) {
78 for (int i = start; i < end; ++i)
82 Runtime Checks of Pointers
83 ^^^^^^^^^^^^^^^^^^^^^^^^^^
85 In the example below, if the pointers A and B point to consecutive addresses,
86 then it is illegal to vectorize the code because some elements of A will be
87 written before they are read from array B.
89 Some programmers use the 'restrict' keyword to notify the compiler that the
90 pointers are disjointed, but in our example, the Loop Vectorizer has no way of
91 knowing that the pointers A and B are unique. The Loop Vectorizer handles this
92 loop by placing code that checks, at runtime, if the arrays A and B point to
93 disjointed memory locations. If arrays A and B overlap, then the scalar version
94 of the loop is executed.
98 void bar(float *A, float* B, float K, int n) {
99 for (int i = 0; i < n; ++i)
107 In this example the ``sum`` variable is used by consecutive iterations of
108 the loop. Normally, this would prevent vectorization, but the vectorizer can
109 detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector
110 of integers, and at the end of the loop the elements of the array are added
111 together to create the correct result. We support a number of different
112 reduction operations, such as addition, multiplication, XOR, AND and OR.
116 int foo(int *A, int *B, int n) {
118 for (int i = 0; i < n; ++i)
126 In this example the value of the induction variable ``i`` is saved into an
127 array. The Loop Vectorizer knows to vectorize induction variables.
131 void bar(float *A, float* B, float K, int n) {
132 for (int i = 0; i < n; ++i)
139 The Loop Vectorizer is able to "flatten" the IF statement in the code and
140 generate a single stream of instructions. The Loop Vectorizer supports any
141 control flow in the innermost loop. The innermost loop may contain complex
142 nesting of IFs, ELSEs and even GOTOs.
146 int foo(int *A, int *B, int n) {
148 for (int i = 0; i < n; ++i)
154 Pointer Induction Variables
155 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
157 This example uses the "accumulate" function of the standard c++ library. This
158 loop uses C++ iterators, which are pointers, and not integer indices.
159 The Loop Vectorizer detects pointer induction variables and can vectorize
160 this loop. This feature is important because many C++ programs use iterators.
164 int baz(int *A, int n) {
165 return std::accumulate(A, A + n, 0);
171 The Loop Vectorizer can vectorize loops that count backwards.
175 int foo(int *A, int *B, int n) {
176 for (int i = n; i > 0; --i)
183 The Loop Vectorizer can vectorize code that becomes a sequence of scalar instructions
184 that scatter/gathers memory.
188 int foo(int *A, int *B, int n, int k) {
189 for (int i = 0; i < n; ++i)
193 Vectorization of Mixed Types
194 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196 The Loop Vectorizer can vectorize programs with mixed types. The Vectorizer
197 cost model can estimate the cost of the type conversion and decide if
198 vectorization is profitable.
202 int foo(int *A, char *B, int n, int k) {
203 for (int i = 0; i < n; ++i)
207 Vectorization of function calls
208 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
210 The Loop Vectorize can vectorize intrinsic math functions.
211 See the table below for a list of these functions.
213 +-----+-----+---------+
215 +-----+-----+---------+
217 +-----+-----+---------+
218 | log |log2 | log10 |
219 +-----+-----+---------+
221 +-----+-----+---------+
222 |fma |trunc|nearbyint|
223 +-----+-----+---------+
225 +-----+-----+---------+
228 Partial unrolling during vectorization
229 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
231 Modern processors feature multiple execution units, and only programs that contain a
232 high degree of parallelism can fully utilize the entire width of the machine.
233 The Loop Vectorizer increases the instruction level parallelism (ILP) by
234 performing partial-unrolling of loops.
236 In the example below the entire array is accumulated into the variable 'sum'.
237 This is inefficient because only a single execution port can be used by the processor.
238 By unrolling the code the Loop Vectorizer allows two or more execution ports
239 to be used simultaneously.
243 int foo(int *A, int *B, int n) {
245 for (int i = 0; i < n; ++i)
250 The Loop Vectorizer uses a cost model to decide when it is profitable to unroll loops.
251 The decision to unroll the loop depends on the register pressure and the generated code size.
256 This section shows the the execution time of Clang on a simple benchmark:
257 `gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`_.
258 This benchmarks is a collection of loops from the GCC autovectorization
259 `page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>`_ by Dorit Nuzman.
261 The chart below compares GCC-4.7, ICC-13, and Clang-SVN with and without loop vectorization at -O3, tuned for "corei7-avx", running on a Sandybridge iMac.
262 The Y-axis shows the time in msec. Lower is better. The last column shows the geomean of all the kernels.
264 .. image:: gcc-loops.png
266 And Linpack-pc with the same configuration. Result is Mflops, higher is better.
268 .. image:: linpack-pc.png
272 The Basic Block Vectorizer
273 ==========================
278 The Basic Block Vectorizer is not enabled by default, but it can be enabled
279 through clang using the command line flag:
281 .. code-block:: console
283 $ clang -fslp-vectorize file.c
288 The goal of basic-block vectorization (a.k.a. superword-level parallelism) is
289 to combine similar independent instructions within simple control-flow regions
290 into vector instructions. Memory accesses, arithemetic operations, comparison
291 operations and some math functions can all be vectorized using this technique
292 (subject to the capabilities of the target architecture).
294 For example, the following function performs very similar operations on its
295 inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these
296 into vector operations.
300 int foo(int a1, int a2, int b1, int b2) {
301 int r1 = a1*(a1 + b1)/b1 + 50*b1/a1;
302 int r2 = a2*(a2 + b2)/b2 + 50*b2/a2;