blob: fbafe6b081f4b2aa20dc79b413ee479f9ee3fef3 [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
38Features
Sean Silva08fd0882012-12-20 02:40:45 +000039--------
Nadav Rotemc4efbb82012-12-19 07:22:24 +000040
41The LLVM Loop Vectorizer has a number of features that allow it to vectorize
42complex loops.
43
44Loops with unknown trip count
Sean Silva08fd0882012-12-20 02:40:45 +000045^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +000046
47The Loop Vectorizer supports loops with an unknown trip count.
48In the loop below, the iteration ``start`` and ``finish`` points are unknown,
49and the Loop Vectorizer has a mechanism to vectorize loops that do not start
Sean Silva13ed79c2012-12-20 02:23:25 +000050at zero. In this example, 'n' may not be a multiple of the vector width, and
Nadav Rotemc4efbb82012-12-19 07:22:24 +000051the vectorizer has to execute the last few iterations as scalar code. Keeping
52a scalar copy of the loop increases the code size.
53
54.. code-block:: c++
55
56 void bar(float *A, float* B, float K, int start, int end) {
Sean Silva8c44a472012-12-20 22:47:41 +000057 for (int i = start; i < end; ++i)
58 A[i] *= B[i] + K;
Nadav Rotemc4efbb82012-12-19 07:22:24 +000059 }
60
61Runtime Checks of Pointers
Sean Silva08fd0882012-12-20 02:40:45 +000062^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +000063
64In the example below, if the pointers A and B point to consecutive addresses,
65then it is illegal to vectorize the code because some elements of A will be
66written before they are read from array B.
67
68Some programmers use the 'restrict' keyword to notify the compiler that the
69pointers are disjointed, but in our example, the Loop Vectorizer has no way of
70knowing that the pointers A and B are unique. The Loop Vectorizer handles this
71loop by placing code that checks, at runtime, if the arrays A and B point to
72disjointed memory locations. If arrays A and B overlap, then the scalar version
73of the loop is executed.
74
75.. code-block:: c++
76
77 void bar(float *A, float* B, float K, int n) {
Sean Silva8c44a472012-12-20 22:47:41 +000078 for (int i = 0; i < n; ++i)
79 A[i] *= B[i] + K;
Nadav Rotemc4efbb82012-12-19 07:22:24 +000080 }
81
82
83Reductions
Sean Silva08fd0882012-12-20 02:40:45 +000084^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +000085
86In this example the ``sum`` variable is used by consecutive iterations of
87the loop. Normally, this would prevent vectorization, but the vectorizer can
Sean Silva13ed79c2012-12-20 02:23:25 +000088detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector
Nadav Rotemc4efbb82012-12-19 07:22:24 +000089of integers, and at the end of the loop the elements of the array are added
90together to create the correct result. We support a number of different
91reduction operations, such as addition, multiplication, XOR, AND and OR.
92
93.. code-block:: c++
94
95 int foo(int *A, int *B, int n) {
96 unsigned sum = 0;
97 for (int i = 0; i < n; ++i)
98 sum += A[i] + 5;
99 return sum;
100 }
101
102Inductions
Sean Silva08fd0882012-12-20 02:40:45 +0000103^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000104
105In this example the value of the induction variable ``i`` is saved into an
106array. The Loop Vectorizer knows to vectorize induction variables.
107
108.. code-block:: c++
109
110 void bar(float *A, float* B, float K, int n) {
Sean Silva8c44a472012-12-20 22:47:41 +0000111 for (int i = 0; i < n; ++i)
112 A[i] = i;
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000113 }
114
115If Conversion
Sean Silva08fd0882012-12-20 02:40:45 +0000116^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000117
118The Loop Vectorizer is able to "flatten" the IF statement in the code and
119generate a single stream of instructions. The Loop Vectorizer supports any
120control flow in the innermost loop. The innermost loop may contain complex
121nesting of IFs, ELSEs and even GOTOs.
122
123.. code-block:: c++
124
125 int foo(int *A, int *B, int n) {
126 unsigned sum = 0;
127 for (int i = 0; i < n; ++i)
128 if (A[i] > B[i])
129 sum += A[i] + 5;
130 return sum;
131 }
132
133Pointer Induction Variables
Sean Silva08fd0882012-12-20 02:40:45 +0000134^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000135
136This example uses the "accumulate" function of the standard c++ library. This
137loop uses C++ iterators, which are pointers, and not integer indices.
138The Loop Vectorizer detects pointer induction variables and can vectorize
139this loop. This feature is important because many C++ programs use iterators.
140
141.. code-block:: c++
142
143 int baz(int *A, int n) {
144 return std::accumulate(A, A + n, 0);
145 }
146
147Reverse Iterators
Sean Silva08fd0882012-12-20 02:40:45 +0000148^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000149
150The Loop Vectorizer can vectorize loops that count backwards.
151
152.. code-block:: c++
153
154 int foo(int *A, int *B, int n) {
155 for (int i = n; i > 0; --i)
156 A[i] +=1;
157 }
158
159Scatter / Gather
Sean Silva08fd0882012-12-20 02:40:45 +0000160^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000161
Nadav Rotemaf14a3f2012-12-19 07:36:35 +0000162The Loop Vectorizer can vectorize code that becomes scatter/gather
163memory accesses.
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000164
165.. code-block:: c++
166
167 int foo(int *A, int *B, int n, int k) {
Sean Silva8c44a472012-12-20 22:47:41 +0000168 for (int i = 0; i < n; ++i)
169 A[i*7] += B[i*k];
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000170 }
171
Nadav Rotemaf14a3f2012-12-19 07:36:35 +0000172Vectorization of Mixed Types
Sean Silva08fd0882012-12-20 02:40:45 +0000173^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000174
175The Loop Vectorizer can vectorize programs with mixed types. The Vectorizer
176cost model can estimate the cost of the type conversion and decide if
177vectorization is profitable.
178
179.. code-block:: c++
180
181 int foo(int *A, char *B, int n, int k) {
Sean Silva8c44a472012-12-20 22:47:41 +0000182 for (int i = 0; i < n; ++i)
183 A[i] += 4 * B[i];
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000184 }
185
186Vectorization of function calls
Sean Silva08fd0882012-12-20 02:40:45 +0000187^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000188
189The Loop Vectorize can vectorize intrinsic math functions.
190See the table below for a list of these functions.
191
192+-----+-----+---------+
193| pow | exp | exp2 |
194+-----+-----+---------+
195| sin | cos | sqrt |
196+-----+-----+---------+
197| log |log2 | log10 |
198+-----+-----+---------+
199|fabs |floor| ceil |
200+-----+-----+---------+
201|fma |trunc|nearbyint|
202+-----+-----+---------+
203
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000204Performance
Sean Silva08fd0882012-12-20 02:40:45 +0000205-----------
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000206
207This section shows the the execution time of Clang on a simple benchmark:
Nadav Rotem90c8b4b2012-12-19 08:43:05 +0000208`gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`_.
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000209This benchmarks is a collection of loops from the GCC autovectorization
Nadav Rotem8f4a6cc2012-12-19 18:02:36 +0000210`page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>`_ by Dorit Nuzman.
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000211
Nadav Rotem12da3962012-12-20 00:03:36 +0000212The 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.
213The 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 +0000214
215.. image:: gcc-loops.png
Sean Silvafd706f72012-12-20 22:24:37 +0000216 :width: 100%
Nadav Rotem15bdbbe2012-12-19 08:28:24 +0000217
Sean Silva99e12f92012-12-20 22:42:20 +0000218.. _bb-vectorizer:
219
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000220The Basic Block Vectorizer
221==========================
222
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000223Usage
Sean Silva08fd0882012-12-20 02:40:45 +0000224------
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000225
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000226The Basic Block Vectorizer is not enabled by default, but it can be enabled
227through clang using the command line flag:
228
229.. code-block:: console
230
231 $ clang -fslp-vectorize file.c
232
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000233Details
Sean Silva08fd0882012-12-20 02:40:45 +0000234-------
Nadav Rotem0328f5e2012-12-19 18:04:44 +0000235
Nadav Rotemc4efbb82012-12-19 07:22:24 +0000236The goal of basic-block vectorization (a.k.a. superword-level parallelism) is
237to combine similar independent instructions within simple control-flow regions
238into vector instructions. Memory accesses, arithemetic operations, comparison
239operations and some math functions can all be vectorized using this technique
240(subject to the capabilities of the target architecture).
241
242For example, the following function performs very similar operations on its
243inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these
244into vector operations.
245
246.. code-block:: c++
247
248 int foo(int a1, int a2, int b1, int b2) {
249 int r1 = a1*(a1 + b1)/b1 + 50*b1/a1;
250 int r2 = a2*(a2 + b2)/b2 + 50*b2/a2;
251 return r1 + r2;
252 }
253
254