1 ==========================
2 Auto-Vectorization in LLVM
3 ==========================
5 LLVM has two vectorizers: The *Loop Vectorizer*, which operates on Loops,
6 and the *Basic Block Vectorizer*, which optimizes straight-line code. These
7 vectorizers focus on different optimization opportunities and use different
8 techniques. The BB vectorizer merges multiple scalars that are found in the
9 code into vectors while the Loop Vectorizer widens instructions in the
10 original loop to operate on multiple consecutive loop iterations.
15 LLVM’s Loop Vectorizer is now available and will be useful for many people.
16 It is not enabled by default, but can be enabled through clang using the
19 .. code-block:: console
21 $ clang -fvectorize file.c
23 We plan to enable the Loop Vectorizer by default as part of the LLVM 3.3 release.
28 The LLVM Loop Vectorizer has a number of features that allow it to vectorize
31 Loops with unknown trip count
32 ------------------------------
34 The Loop Vectorizer supports loops with an unknown trip count.
35 In the loop below, the iteration ``start`` and ``finish`` points are unknown,
36 and the Loop Vectorizer has a mechanism to vectorize loops that do not start
37 at zero. In this example, ‘n’ may not be a multiple of the vector width, and
38 the vectorizer has to execute the last few iterations as scalar code. Keeping
39 a scalar copy of the loop increases the code size.
43 void bar(float *A, float* B, float K, int start, int end) {
44 for (int i = start; i < end; ++i)
48 Runtime Checks of Pointers
49 --------------------------
51 In the example below, if the pointers A and B point to consecutive addresses,
52 then it is illegal to vectorize the code because some elements of A will be
53 written before they are read from array B.
55 Some programmers use the 'restrict' keyword to notify the compiler that the
56 pointers are disjointed, but in our example, the Loop Vectorizer has no way of
57 knowing that the pointers A and B are unique. The Loop Vectorizer handles this
58 loop by placing code that checks, at runtime, if the arrays A and B point to
59 disjointed memory locations. If arrays A and B overlap, then the scalar version
60 of the loop is executed.
64 void bar(float *A, float* B, float K, int n) {
65 for (int i = 0; i < n; ++i)
71 --------------------------
73 In this example the ``sum`` variable is used by consecutive iterations of
74 the loop. Normally, this would prevent vectorization, but the vectorizer can
75 detect that ‘sum’ is a reduction variable. The variable ‘sum’ becomes a vector
76 of integers, and at the end of the loop the elements of the array are added
77 together to create the correct result. We support a number of different
78 reduction operations, such as addition, multiplication, XOR, AND and OR.
82 int foo(int *A, int *B, int n) {
84 for (int i = 0; i < n; ++i)
90 --------------------------
92 In this example the value of the induction variable ``i`` is saved into an
93 array. The Loop Vectorizer knows to vectorize induction variables.
97 void bar(float *A, float* B, float K, int n) {
98 for (int i = 0; i < n; ++i)
103 --------------------------
105 The Loop Vectorizer is able to "flatten" the IF statement in the code and
106 generate a single stream of instructions. The Loop Vectorizer supports any
107 control flow in the innermost loop. The innermost loop may contain complex
108 nesting of IFs, ELSEs and even GOTOs.
112 int foo(int *A, int *B, int n) {
114 for (int i = 0; i < n; ++i)
120 Pointer Induction Variables
121 ---------------------------
123 This example uses the "accumulate" function of the standard c++ library. This
124 loop uses C++ iterators, which are pointers, and not integer indices.
125 The Loop Vectorizer detects pointer induction variables and can vectorize
126 this loop. This feature is important because many C++ programs use iterators.
130 int baz(int *A, int n) {
131 return std::accumulate(A, A + n, 0);
135 --------------------------
137 The Loop Vectorizer can vectorize loops that count backwards.
141 int foo(int *A, int *B, int n) {
142 for (int i = n; i > 0; --i)
149 The Loop Vectorizer can vectorize code that becomes scatter/gather
154 int foo(int *A, int *B, int n, int k) {
155 for (int i = 0; i < n; ++i)
159 Vectorization of Mixed Types
160 --------------------------
162 The Loop Vectorizer can vectorize programs with mixed types. The Vectorizer
163 cost model can estimate the cost of the type conversion and decide if
164 vectorization is profitable.
168 int foo(int *A, char *B, int n, int k) {
169 for (int i = 0; i < n; ++i)
173 Vectorization of function calls
174 --------------------------
176 The Loop Vectorize can vectorize intrinsic math functions.
177 See the table below for a list of these functions.
179 +-----+-----+---------+
181 +-----+-----+---------+
183 +-----+-----+---------+
184 | log |log2 | log10 |
185 +-----+-----+---------+
187 +-----+-----+---------+
188 |fma |trunc|nearbyint|
189 +-----+-----+---------+
194 This section shows the the execution time of Clang on a simple benchmark:
195 `gcc-loops <http://llvm.org/viewvc/llvm-project/test-suite/trunk/SingleSource/UnitTests/Vectorizer/>`._
196 This benchmarks is a collection of loops from the GCC autovectorization
197 `page <http://gcc.gnu.org/projects/tree-ssa/vectorization.html>` by Dorit Nuzman._
199 The chart below compares GCC-4.7, ICC-13, and Clang-SVN at -O3, running on a Sandybridge.
200 The Y-axis shows time in msec. Lower is better.
202 .. image:: gcc-loops.png
204 The Basic Block Vectorizer
205 ==========================
207 The Basic Block Vectorizer is not enabled by default, but it can be enabled
208 through clang using the command line flag:
210 .. code-block:: console
212 $ clang -fslp-vectorize file.c
214 The goal of basic-block vectorization (a.k.a. superword-level parallelism) is
215 to combine similar independent instructions within simple control-flow regions
216 into vector instructions. Memory accesses, arithemetic operations, comparison
217 operations and some math functions can all be vectorized using this technique
218 (subject to the capabilities of the target architecture).
220 For example, the following function performs very similar operations on its
221 inputs (a1, b1) and (a2, b2). The basic-block vectorizer may combine these
222 into vector operations.
226 int foo(int a1, int a2, int b1, int b2) {
227 int r1 = a1*(a1 + b1)/b1 + 50*b1/a1;
228 int r2 = a2*(a2 + b2)/b2 + 50*b2/a2;