blob: 0894b1eb3f10f3f3ac4bd26c9896937110945764 [file] [log] [blame]
Nadav Rotemc4efbb82012-12-19 07:22:24 +00001==========================
2Auto-Vectorization in LLVM
3==========================
4
Sean Silva99e12f92012-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 Rotemc4efbb82012-12-19 07:22:24 +000017
18The Loop Vectorizer
19===================
20
Nadav Rotem0328f5e2012-12-19 18:04:44 +000021Usage
Sean Silva08fd0882012-12-20 02:40:45 +000022-----
Nadav Rotem0328f5e2012-12-19 18:04:44 +000023
Sean Silva13ed79c2012-12-20 02:23:25 +000024LLVM's Loop Vectorizer is now available and will be useful for many people.
Nadav Rotemc4efbb82012-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 Rotem8f4a6cc2012-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 Rotemc4efbb82012-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 Rotem7daadf22013-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 Rotemc4efbb82012-12-19 07:22:24 +000059Features
Sean Silva08fd0882012-12-20 02:40:45 +000060--------
Nadav Rotemc4efbb82012-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 Silva08fd0882012-12-20 02:40:45 +000066^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva13ed79c2012-12-20 02:23:25 +000071at zero. In this example, 'n' may not be a multiple of the vector width, and
Nadav Rotemc4efbb82012-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 Silva8c44a472012-12-20 22:47:41 +000078 for (int i = start; i < end; ++i)
79 A[i] *= B[i] + K;
Nadav Rotemc4efbb82012-12-19 07:22:24 +000080 }
81
82Runtime Checks of Pointers
Sean Silva08fd0882012-12-20 02:40:45 +000083^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva287e7d22012-12-20 22:59:36 +000094of the loop is executed.
Nadav Rotemc4efbb82012-12-19 07:22:24 +000095
96.. code-block:: c++
97
98 void bar(float *A, float* B, float K, int n) {
Sean Silva8c44a472012-12-20 22:47:41 +000099 for (int i = 0; i < n; ++i)
100 A[i] *= B[i] + K;
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000101 }
102
103
104Reductions
Sean Silva08fd0882012-12-20 02:40:45 +0000105^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000106
Sean Silva287e7d22012-12-20 22:59:36 +0000107In this example the ``sum`` variable is used by consecutive iterations of
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000108the loop. Normally, this would prevent vectorization, but the vectorizer can
Sean Silva13ed79c2012-12-20 02:23:25 +0000109detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000110of integers, and at the end of the loop the elements of the array are added
Sean Silva287e7d22012-12-20 22:59:36 +0000111together to create the correct result. We support a number of different
Nadav Rotemc4efbb82012-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 Silva287e7d22012-12-20 22:59:36 +0000119 sum += A[i] + 5;
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000120 return sum;
121 }
122
Nadav Rotem9f207812013-01-08 17:46:30 +0000123We support floating point reduction operations when `-ffast-math` is used.
124
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000125Inductions
Sean Silva08fd0882012-12-20 02:40:45 +0000126^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva8c44a472012-12-20 22:47:41 +0000134 for (int i = 0; i < n; ++i)
135 A[i] = i;
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000136 }
137
138If Conversion
Sean Silva08fd0882012-12-20 02:40:45 +0000139^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva08fd0882012-12-20 02:40:45 +0000157^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva08fd0882012-12-20 02:40:45 +0000171^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva08fd0882012-12-20 02:40:45 +0000183^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000184
Nadav Rotema616d682013-01-03 01:47:02 +0000185The Loop Vectorizer can vectorize code that becomes a sequence of scalar instructions
186that scatter/gathers memory.
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000187
188.. code-block:: c++
189
190 int foo(int *A, int *B, int n, int k) {
Sean Silva8c44a472012-12-20 22:47:41 +0000191 for (int i = 0; i < n; ++i)
Sean Silvae140b2e2012-12-20 22:49:13 +0000192 A[i*7] += B[i*k];
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000193 }
194
Nadav Rotemaf14a3f2012-12-19 07:36:35 +0000195Vectorization of Mixed Types
Sean Silva08fd0882012-12-20 02:40:45 +0000196^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-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 Silva8c44a472012-12-20 22:47:41 +0000205 for (int i = 0; i < n; ++i)
Sean Silvae140b2e2012-12-20 22:49:13 +0000206 A[i] += 4 * B[i];
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000207 }
208
Renato Golinf2ea19e2013-02-23 13:25:41 +0000209Global Structures Alias Analysis
210^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211
212Access to global structures can also be vectorized, with alias analysis being
213used to make sure accesses don't alias. Run-time checks can also be added on
214pointer access to structure members.
215
216Many variations are supported, but some that rely on undefined behaviour being
217ignored (as other compilers do) are still being left un-vectorized.
218
219.. code-block:: c++
220
221 struct { int A[100], K, B[100]; } Foo;
222
223 int foo() {
224 for (int i = 0; i < 100; ++i)
225 Foo.A[i] = Foo.B[i] + 100;
226 }
227
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000228Vectorization of function calls
Sean Silva08fd0882012-12-20 02:40:45 +0000229^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000230
231The Loop Vectorize can vectorize intrinsic math functions.
232See the table below for a list of these functions.
233
234+-----+-----+---------+
235| pow | exp | exp2 |
236+-----+-----+---------+
237| sin | cos | sqrt |
238+-----+-----+---------+
239| log |log2 | log10 |
240+-----+-----+---------+
241|fabs |floor| ceil |
242+-----+-----+---------+
243|fma |trunc|nearbyint|
244+-----+-----+---------+
Nadav Rotem7375d352012-12-26 06:03:35 +0000245| | | fmuladd |
246+-----+-----+---------+
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000247
Nadav Rotema616d682013-01-03 01:47:02 +0000248
249Partial unrolling during vectorization
250^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
251
252Modern processors feature multiple execution units, and only programs that contain a
Nadav Rotem7ea18a72013-01-03 01:56:33 +0000253high degree of parallelism can fully utilize the entire width of the machine.
Nadav Rotema616d682013-01-03 01:47:02 +0000254The Loop Vectorizer increases the instruction level parallelism (ILP) by
255performing partial-unrolling of loops.
256
257In the example below the entire array is accumulated into the variable 'sum'.
Nadav Rotem7ea18a72013-01-03 01:56:33 +0000258This is inefficient because only a single execution port can be used by the processor.
Nadav Rotema616d682013-01-03 01:47:02 +0000259By unrolling the code the Loop Vectorizer allows two or more execution ports
Nadav Rotem7ea18a72013-01-03 01:56:33 +0000260to be used simultaneously.
Nadav Rotema616d682013-01-03 01:47:02 +0000261
262.. code-block:: c++
263
264 int foo(int *A, int *B, int n) {
265 unsigned sum = 0;
266 for (int i = 0; i < n; ++i)
267 sum += A[i];
268 return sum;
269 }
270
Nadav Rotem7daadf22013-01-04 17:49:45 +0000271The Loop Vectorizer uses a cost model to decide when it is profitable to unroll loops.
272The decision to unroll the loop depends on the register pressure and the generated code size.
Nadav Rotema616d682013-01-03 01:47:02 +0000273
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000274Performance
Sean Silva08fd0882012-12-20 02:40:45 +0000275-----------
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000276
Sean Silva287e7d22012-12-20 22:59:36 +0000277This section shows the the execution time of Clang on a simple benchmark:
Nadav Rotem90c8b4b2012-12-19 08:43:05 +0000278`gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`_.
Sean Silva287e7d22012-12-20 22:59:36 +0000279This benchmarks is a collection of loops from the GCC autovectorization
Nadav Rotem8f4a6cc2012-12-19 18:02:36 +0000280`page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>`_ by Dorit Nuzman.
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000281
Nadav Rotem12da3962012-12-20 00:03:36 +0000282The 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 Silva287e7d22012-12-20 22:59:36 +0000283The Y-axis shows the time in msec. Lower is better. The last column shows the geomean of all the kernels.
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000284
285.. image:: gcc-loops.png
286
Nadav Rotem014e19c2013-01-04 19:00:42 +0000287And Linpack-pc with the same configuration. Result is Mflops, higher is better.
288
289.. image:: linpack-pc.png
290
Sean Silva99e12f92012-12-20 22:42:20 +0000291.. _bb-vectorizer:
292
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000293The Basic Block Vectorizer
294==========================
295
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000296Usage
Sean Silva08fd0882012-12-20 02:40:45 +0000297------
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000298
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000299The Basic Block Vectorizer is not enabled by default, but it can be enabled
300through clang using the command line flag:
301
302.. code-block:: console
303
Sean Silva287e7d22012-12-20 22:59:36 +0000304 $ clang -fslp-vectorize file.c
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000305
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000306Details
Sean Silva08fd0882012-12-20 02:40:45 +0000307-------
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000308
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000309The goal of basic-block vectorization (a.k.a. superword-level parallelism) is
310to combine similar independent instructions within simple control-flow regions
311into vector instructions. Memory accesses, arithemetic operations, comparison
312operations and some math functions can all be vectorized using this technique
Sean Silva287e7d22012-12-20 22:59:36 +0000313(subject to the capabilities of the target architecture).
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000314
315For example, the following function performs very similar operations on its
316inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these
317into vector operations.
318
319.. code-block:: c++
320
321 int foo(int a1, int a2, int b1, int b2) {
322 int r1 = a1*(a1 + b1)/b1 + 50*b1/a1;
323 int r2 = a2*(a2 + b2)/b2 + 50*b2/a2;
324 return r1 + r2;
325 }
326
327