blob: 07486347e374ece62f1c2e612ba51f8d50bf4b3c [file] [log] [blame]
Nadav Rotem59f2af92012-12-19 07:22:24 +00001==========================
2Auto-Vectorization in LLVM
3==========================
4
Sean Silva12ae5152012-12-20 22:42:20 +00005.. contents::
6 :local:
7
8LLVM has two vectorizers: The :ref:`Loop Vectorizer <loop-vectorizer>`,
9which operates on Loops, and the :ref:`Basic Block Vectorizer
10<bb-vectorizer>`, which optimizes straight-line code. These vectorizers
11focus on different optimization opportunities and use different techniques.
12The BB vectorizer merges multiple scalars that are found in the code into
13vectors while the Loop Vectorizer widens instructions in the original loop
14to operate on multiple consecutive loop iterations.
15
16.. _loop-vectorizer:
Nadav Rotem59f2af92012-12-19 07:22:24 +000017
18The Loop Vectorizer
19===================
20
Nadav Rotem649a33e2012-12-19 18:04:44 +000021Usage
Sean Silva62417032012-12-20 02:40:45 +000022-----
Nadav Rotem649a33e2012-12-19 18:04:44 +000023
Sean Silva68d5b272012-12-20 02:23:25 +000024LLVM's Loop Vectorizer is now available and will be useful for many people.
Nadav Rotem59f2af92012-12-19 07:22:24 +000025It is not enabled by default, but can be enabled through clang using the
26command line flag:
27
28.. code-block:: console
29
Nadav Rotem3e6da7e2012-12-19 18:02:36 +000030 $ clang -fvectorize -O3 file.c
31
32If the ``-fvectorize`` flag is used then the loop vectorizer will be enabled
33when running with ``-O3``, ``-O2``. When ``-Os`` is used, the loop vectorizer
34will only vectorize loops that do not require a major increase in code size.
Nadav Rotem59f2af92012-12-19 07:22:24 +000035
36We plan to enable the Loop Vectorizer by default as part of the LLVM 3.3 release.
37
Nadav Rotem4aa55bb2013-01-04 17:49:45 +000038Command line flags
39^^^^^^^^^^^^^^^^^^
40
41The loop vectorizer uses a cost model to decide on the optimal vectorization factor
42and unroll factor. However, users of the vectorizer can force the vectorizer to use
43specific values. Both 'clang' and 'opt' support the flags below.
44
45Users can control the vectorization SIMD width using the command line flag "-force-vector-width".
46
47.. code-block:: console
48
49 $ clang -mllvm -force-vector-width=8 ...
50 $ opt -loop-vectorize -force-vector-width=8 ...
51
52Users can control the unroll factor using the command line flag "-force-vector-unroll"
53
54.. code-block:: console
55
56 $ clang -mllvm -force-vector-unroll=2 ...
57 $ opt -loop-vectorize -force-vector-unroll=2 ...
58
Nadav Rotem59f2af92012-12-19 07:22:24 +000059Features
Sean Silva62417032012-12-20 02:40:45 +000060--------
Nadav Rotem59f2af92012-12-19 07:22:24 +000061
62The LLVM Loop Vectorizer has a number of features that allow it to vectorize
63complex loops.
64
65Loops with unknown trip count
Sean Silva62417032012-12-20 02:40:45 +000066^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +000067
68The Loop Vectorizer supports loops with an unknown trip count.
69In the loop below, the iteration ``start`` and ``finish`` points are unknown,
70and the Loop Vectorizer has a mechanism to vectorize loops that do not start
Sean Silva68d5b272012-12-20 02:23:25 +000071at zero. In this example, 'n' may not be a multiple of the vector width, and
Nadav Rotem59f2af92012-12-19 07:22:24 +000072the vectorizer has to execute the last few iterations as scalar code. Keeping
73a scalar copy of the loop increases the code size.
74
75.. code-block:: c++
76
77 void bar(float *A, float* B, float K, int start, int end) {
Sean Silva9baa6e42012-12-20 22:47:41 +000078 for (int i = start; i < end; ++i)
79 A[i] *= B[i] + K;
Nadav Rotem59f2af92012-12-19 07:22:24 +000080 }
81
82Runtime Checks of Pointers
Sean Silva62417032012-12-20 02:40:45 +000083^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +000084
85In the example below, if the pointers A and B point to consecutive addresses,
86then it is illegal to vectorize the code because some elements of A will be
87written before they are read from array B.
88
89Some programmers use the 'restrict' keyword to notify the compiler that the
90pointers are disjointed, but in our example, the Loop Vectorizer has no way of
91knowing that the pointers A and B are unique. The Loop Vectorizer handles this
92loop by placing code that checks, at runtime, if the arrays A and B point to
93disjointed memory locations. If arrays A and B overlap, then the scalar version
Sean Silva689858b2012-12-20 22:59:36 +000094of the loop is executed.
Nadav Rotem59f2af92012-12-19 07:22:24 +000095
96.. code-block:: c++
97
98 void bar(float *A, float* B, float K, int n) {
Sean Silva9baa6e42012-12-20 22:47:41 +000099 for (int i = 0; i < n; ++i)
100 A[i] *= B[i] + K;
Nadav Rotem59f2af92012-12-19 07:22:24 +0000101 }
102
103
104Reductions
Sean Silva62417032012-12-20 02:40:45 +0000105^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000106
Sean Silva689858b2012-12-20 22:59:36 +0000107In this example the ``sum`` variable is used by consecutive iterations of
Nadav Rotem59f2af92012-12-19 07:22:24 +0000108the loop. Normally, this would prevent vectorization, but the vectorizer can
Sean Silva68d5b272012-12-20 02:23:25 +0000109detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector
Nadav Rotem59f2af92012-12-19 07:22:24 +0000110of integers, and at the end of the loop the elements of the array are added
Sean Silva689858b2012-12-20 22:59:36 +0000111together to create the correct result. We support a number of different
Nadav Rotem59f2af92012-12-19 07:22:24 +0000112reduction operations, such as addition, multiplication, XOR, AND and OR.
113
114.. code-block:: c++
115
116 int foo(int *A, int *B, int n) {
117 unsigned sum = 0;
118 for (int i = 0; i < n; ++i)
Sean Silva689858b2012-12-20 22:59:36 +0000119 sum += A[i] + 5;
Nadav Rotem59f2af92012-12-19 07:22:24 +0000120 return sum;
121 }
122
Nadav Rotem2a92c102013-01-08 17:46:30 +0000123We support floating point reduction operations when `-ffast-math` is used.
124
Nadav Rotem59f2af92012-12-19 07:22:24 +0000125Inductions
Sean Silva62417032012-12-20 02:40:45 +0000126^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000127
128In this example the value of the induction variable ``i`` is saved into an
129array. The Loop Vectorizer knows to vectorize induction variables.
130
131.. code-block:: c++
132
133 void bar(float *A, float* B, float K, int n) {
Sean Silva9baa6e42012-12-20 22:47:41 +0000134 for (int i = 0; i < n; ++i)
135 A[i] = i;
Nadav Rotem59f2af92012-12-19 07:22:24 +0000136 }
137
138If Conversion
Sean Silva62417032012-12-20 02:40:45 +0000139^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000140
141The Loop Vectorizer is able to "flatten" the IF statement in the code and
142generate a single stream of instructions. The Loop Vectorizer supports any
143control flow in the innermost loop. The innermost loop may contain complex
144nesting of IFs, ELSEs and even GOTOs.
145
146.. code-block:: c++
147
148 int foo(int *A, int *B, int n) {
149 unsigned sum = 0;
150 for (int i = 0; i < n; ++i)
151 if (A[i] > B[i])
152 sum += A[i] + 5;
153 return sum;
154 }
155
156Pointer Induction Variables
Sean Silva62417032012-12-20 02:40:45 +0000157^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000158
159This example uses the "accumulate" function of the standard c++ library. This
160loop uses C++ iterators, which are pointers, and not integer indices.
161The Loop Vectorizer detects pointer induction variables and can vectorize
162this loop. This feature is important because many C++ programs use iterators.
163
164.. code-block:: c++
165
166 int baz(int *A, int n) {
167 return std::accumulate(A, A + n, 0);
168 }
169
170Reverse Iterators
Sean Silva62417032012-12-20 02:40:45 +0000171^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000172
173The Loop Vectorizer can vectorize loops that count backwards.
174
175.. code-block:: c++
176
177 int foo(int *A, int *B, int n) {
178 for (int i = n; i > 0; --i)
179 A[i] +=1;
180 }
181
182Scatter / Gather
Sean Silva62417032012-12-20 02:40:45 +0000183^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000184
Nadav Rotemf574b882013-01-03 01:47:02 +0000185The Loop Vectorizer can vectorize code that becomes a sequence of scalar instructions
186that scatter/gathers memory.
Nadav Rotem59f2af92012-12-19 07:22:24 +0000187
188.. code-block:: c++
189
190 int foo(int *A, int *B, int n, int k) {
Sean Silva9baa6e42012-12-20 22:47:41 +0000191 for (int i = 0; i < n; ++i)
Sean Silva5e816332012-12-20 22:49:13 +0000192 A[i*7] += B[i*k];
Nadav Rotem59f2af92012-12-19 07:22:24 +0000193 }
194
Nadav Rotemaf086272012-12-19 07:36:35 +0000195Vectorization of Mixed Types
Sean Silva62417032012-12-20 02:40:45 +0000196^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000197
198The Loop Vectorizer can vectorize programs with mixed types. The Vectorizer
199cost model can estimate the cost of the type conversion and decide if
200vectorization is profitable.
201
202.. code-block:: c++
203
204 int foo(int *A, char *B, int n, int k) {
Sean Silva9baa6e42012-12-20 22:47:41 +0000205 for (int i = 0; i < n; ++i)
Sean Silva5e816332012-12-20 22:49:13 +0000206 A[i] += 4 * B[i];
Nadav Rotem59f2af92012-12-19 07:22:24 +0000207 }
208
209Vectorization of function calls
Sean Silva62417032012-12-20 02:40:45 +0000210^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotem59f2af92012-12-19 07:22:24 +0000211
212The Loop Vectorize can vectorize intrinsic math functions.
213See the table below for a list of these functions.
214
215+-----+-----+---------+
216| pow | exp | exp2 |
217+-----+-----+---------+
218| sin | cos | sqrt |
219+-----+-----+---------+
220| log |log2 | log10 |
221+-----+-----+---------+
222|fabs |floor| ceil |
223+-----+-----+---------+
224|fma |trunc|nearbyint|
225+-----+-----+---------+
Nadav Rotemf7769e32012-12-26 06:03:35 +0000226| | | fmuladd |
227+-----+-----+---------+
Nadav Rotem59f2af92012-12-19 07:22:24 +0000228
Nadav Rotemf574b882013-01-03 01:47:02 +0000229
230Partial unrolling during vectorization
231^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
232
233Modern processors feature multiple execution units, and only programs that contain a
Nadav Rotem43f39282013-01-03 01:56:33 +0000234high degree of parallelism can fully utilize the entire width of the machine.
Nadav Rotemf574b882013-01-03 01:47:02 +0000235The Loop Vectorizer increases the instruction level parallelism (ILP) by
236performing partial-unrolling of loops.
237
238In the example below the entire array is accumulated into the variable 'sum'.
Nadav Rotem43f39282013-01-03 01:56:33 +0000239This is inefficient because only a single execution port can be used by the processor.
Nadav Rotemf574b882013-01-03 01:47:02 +0000240By unrolling the code the Loop Vectorizer allows two or more execution ports
Nadav Rotem43f39282013-01-03 01:56:33 +0000241to be used simultaneously.
Nadav Rotemf574b882013-01-03 01:47:02 +0000242
243.. code-block:: c++
244
245 int foo(int *A, int *B, int n) {
246 unsigned sum = 0;
247 for (int i = 0; i < n; ++i)
248 sum += A[i];
249 return sum;
250 }
251
Nadav Rotem4aa55bb2013-01-04 17:49:45 +0000252The Loop Vectorizer uses a cost model to decide when it is profitable to unroll loops.
253The decision to unroll the loop depends on the register pressure and the generated code size.
Nadav Rotemf574b882013-01-03 01:47:02 +0000254
Nadav Rotem67a6ec82012-12-19 08:28:24 +0000255Performance
Sean Silva62417032012-12-20 02:40:45 +0000256-----------
Nadav Rotem67a6ec82012-12-19 08:28:24 +0000257
Sean Silva689858b2012-12-20 22:59:36 +0000258This section shows the the execution time of Clang on a simple benchmark:
Nadav Rotem05754292012-12-19 08:43:05 +0000259`gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`_.
Sean Silva689858b2012-12-20 22:59:36 +0000260This benchmarks is a collection of loops from the GCC autovectorization
Nadav Rotem3e6da7e2012-12-19 18:02:36 +0000261`page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>`_ by Dorit Nuzman.
Nadav Rotem67a6ec82012-12-19 08:28:24 +0000262
Nadav Rotem6d1fc532012-12-20 00:03:36 +0000263The 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.
Sean Silva689858b2012-12-20 22:59:36 +0000264The Y-axis shows the time in msec. Lower is better. The last column shows the geomean of all the kernels.
Nadav Rotem67a6ec82012-12-19 08:28:24 +0000265
266.. image:: gcc-loops.png
267
Nadav Rotem13410a12013-01-04 19:00:42 +0000268And Linpack-pc with the same configuration. Result is Mflops, higher is better.
269
270.. image:: linpack-pc.png
271
Sean Silva12ae5152012-12-20 22:42:20 +0000272.. _bb-vectorizer:
273
Nadav Rotem59f2af92012-12-19 07:22:24 +0000274The Basic Block Vectorizer
275==========================
276
Nadav Rotem649a33e2012-12-19 18:04:44 +0000277Usage
Sean Silva62417032012-12-20 02:40:45 +0000278------
Nadav Rotem649a33e2012-12-19 18:04:44 +0000279
Nadav Rotem59f2af92012-12-19 07:22:24 +0000280The Basic Block Vectorizer is not enabled by default, but it can be enabled
281through clang using the command line flag:
282
283.. code-block:: console
284
Sean Silva689858b2012-12-20 22:59:36 +0000285 $ clang -fslp-vectorize file.c
Nadav Rotem59f2af92012-12-19 07:22:24 +0000286
Nadav Rotem649a33e2012-12-19 18:04:44 +0000287Details
Sean Silva62417032012-12-20 02:40:45 +0000288-------
Nadav Rotem649a33e2012-12-19 18:04:44 +0000289
Nadav Rotem59f2af92012-12-19 07:22:24 +0000290The goal of basic-block vectorization (a.k.a. superword-level parallelism) is
291to combine similar independent instructions within simple control-flow regions
292into vector instructions. Memory accesses, arithemetic operations, comparison
293operations and some math functions can all be vectorized using this technique
Sean Silva689858b2012-12-20 22:59:36 +0000294(subject to the capabilities of the target architecture).
Nadav Rotem59f2af92012-12-19 07:22:24 +0000295
296For example, the following function performs very similar operations on its
297inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these
298into vector operations.
299
300.. code-block:: c++
301
302 int foo(int a1, int a2, int b1, int b2) {
303 int r1 = a1*(a1 + b1)/b1 + 50*b1/a1;
304 int r2 = a2*(a2 + b2)/b2 + 50*b2/a2;
305 return r1 + r2;
306 }
307
308