# New Loop Vectorizer

DevFeed: [New Loop Vectorizer](<https://devfeed.tech/articles/new-loop-vectorizer-42872.md>)

Original publisher: [Read original article](<https://blog.llvm.org/2012/12/new-loop-vectorizer.html>)

Author: Nadav Rotem

Published: 2012-12-07T10:12:00Z

Content type: article

Language: en

Sources: [LLVM Project](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [LLVM](<https://devfeed.tech/topics/llvm.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [clang](<https://devfeed.tech/topics/clang.md>), [Code](<https://devfeed.tech/topics/code.md>), [C](<https://devfeed.tech/topics/c.md>)

Tags: [benchmark](<https://devfeed.tech/tags/benchmark.md>), [c](<https://devfeed.tech/tags/c.md>), [clang](<https://devfeed.tech/tags/clang.md>), [code](<https://devfeed.tech/tags/code.md>), [codegen](<https://devfeed.tech/tags/codegen.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [if-statement](<https://devfeed.tech/tags/if-statement.md>), [llvm](<https://devfeed.tech/tags/llvm.md>), [loops](<https://devfeed.tech/tags/loops.md>), [new-in-llvm-3-3](<https://devfeed.tech/tags/new-in-llvm-3-3.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [performance](<https://devfeed.tech/tags/performance.md>), [reduction](<https://devfeed.tech/tags/reduction.md>), [vectorization](<https://devfeed.tech/tags/vectorization.md>)

## AI overview

An update on LLVM's Loop Vectorizer, which complements the Basic Block Vectorizer by widening loop instructions across consecutive iterations. The article describes support for reductions, control-flow flattening, and loops with unknown trip counts, along with benchmarked performance improvements.

## Source excerpt

I would like to give a brief update regarding the development of the Loop Vectorizer. LLVM now has two vectorizers: The Loop Vectorizer, which operates on Loops, and the Basic Block Vectorizer, which optimizes straight-line code. These vectorizers focus on different optimization opportunities and use different techniques. The BB vectorizer merges multiple scalars that are found in the code into vectors while the Loop Vectorizer widens instructions in the original loop to operate on multiple consecutive loop iterations. LLVM's Loop Vectorizer is now available and will be useful for many people. It is not enabled by default, but can be enabled through clang using the command line flag "-mllvm -vectorize-loops". We plan to enable the Loop Vectorizer by default as part of the LLVM 3.3 release. The Loop Vectorizer can boost the performance of many loops, including some loops that are not vectorizable by GCC. In one benchmark, Linpack-pc, the Loop Vectorizer boosts the performance of gaussian elimination of single precision matrices from 984 MFlops to 2539 MFlops - a 2.6X boost in performance. The vectorizer also boosts the "GCC vectorization examples" benchmark by a geomean of 2.15X. The LLVM Loop Vectorizer has a number of features that allow it to vectorize complex loops. Most of the features described in this post are available as part of the LLVM 3.2 release, but some features were added after the cutoff date. Here is one small example of a loop that the LLVM Loop Vectorizer can vectorize. int foo(int *A, int *B, int n) { unsigned sum = 0; for (int i = 0; i < n; ++i) if (A[i] > B[i]) sum += A[i] + 5; return sum; } In this example, the Loop Vectorizer uses a number of non-trivial features to vectorize the loop. The 'sum' variable is used by consecutive iterations of the loop. Normally, this would prevent vectorization, but the vectorizer can detect that 'sum' is a reduction variable. The variable 'sum' becomes a vector of integers, and at the end of the loop the eleme