# Why do libraries need hand-vectorized code instead of compiler auto vectorization

C++ eigen library does vectorization for different architecture, like SSE, NEON etc. In their documentation they mentioned that, Eigen vectorization is not compiler dependent. But most modern compilers like gcc does the vectorization automatically if the vectorization flag is enabled using -O3 flag.

So my question is, why Eigen or any other libraries does this hard coded vectorization when compilers does this automatically for us?

• You should ask the Eigen developers. Also, the compiler is almost never perfect when it vectorizes, maybe the Eigen developers have done a better job for each architecture. – Bill Barth Jul 6 '16 at 22:54
• Vectorization in clang appears to be only 4 years old (?): blog.llvm.org/2012/12/new-loop-vectorizer.html – Kirill Jul 7 '16 at 1:00
• I think that there is a reasonable question at the core that is definitely not off-topic (namely, why do libraries need hand-vectorized code for different architectures if there are -- or claim to be -- auto-vectorizing compilers?) I therefore vote to leave open, hoping that someone more knowledgeable will edit and answer the (general) question. – Christian Clason Jul 7 '16 at 15:17

It is true that compilers are getting better and better at auto-vectorization, and for basic coefficient-wise operations like 2*A-4*B a library like Eigen cannot do much better than recent compilers. However, for slightly more complicated expressions like matrix products, reductions, transposition, powers, etc. the compiler cannot do much. On the other hand, Eigen can take advantage of the higher level knowledge of the expression semantic to explicitly vectorize them. Moreover, complex scalar types are not vectorized by compilers. You can check by yourself by disabling Eigen's explicit vectorization (-DEIGEN_DONT_VECTORIZE).