X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FVectorizers.rst;h=65c19aa2bc0cbfa4f7c0c9f88a91c2b2779a7368;hb=4d88c3ebad365105a7fd769b4f89e8a53470744a;hp=61ebca2bb529f41088385edb1ff310589a71d0a4;hpb=055028f6c805b089d10984ec691aa637cf21dc42;p=oota-llvm.git diff --git a/docs/Vectorizers.rst b/docs/Vectorizers.rst index 61ebca2bb52..65c19aa2bc0 100644 --- a/docs/Vectorizers.rst +++ b/docs/Vectorizers.rst @@ -51,6 +51,89 @@ Users can control the unroll factor using the command line flag "-force-vector-u $ clang -mllvm -force-vector-unroll=2 ... $ opt -loop-vectorize -force-vector-unroll=2 ... +Pragma loop hint directives +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The ``#pragma clang loop`` directive allows loop vectorization hints to be +specified for the subsequent for, while, do-while, or c++11 range-based for +loop. The directive allows vectorization and interleaving to be enabled or +disabled. Vector width as well as interleave count can also be manually +specified. The following example explicitly enables vectorization and +interleaving: + +.. code-block:: c++ + + #pragma clang loop vectorize(enable) interleave(enable) + while(...) { + ... + } + +The following example implicitly enables vectorization and interleaving by +specifying a vector width and interleaving count: + +.. code-block:: c++ + + #pragma clang loop vectorize_width(2) interleave_count(2) + for(...) { + ... + } + +See the Clang +`language extensions +`_ +for details. + +Diagnostics +----------- + +Many loops cannot be vectorized including loops with complicated control flow, +unvectorizable types, and unvectorizable calls. The loop vectorizer generates +optimization remarks which can be queried using command line options to identify +and diagnose loops that are skipped by the loop-vectorizer. + +Optimization remarks are enabled using: + +``-Rpass=loop-vectorize`` identifies loops that were successfully vectorized. + +``-Rpass-missed=loop-vectorize`` identifies loops that failed vectorization and +indicates if vectorization was specified. + +``-Rpass-analysis=loop-vectorize`` identifies the statements that caused +vectorization to fail. + +Consider the following loop: + +.. code-block:: c++ + + #pragma clang loop vectorize(enable) + for (int i = 0; i < Length; i++) { + switch(A[i]) { + case 0: A[i] = i*2; break; + case 1: A[i] = i; break; + default: A[i] = 0; + } + } + +The command line ``-Rpass-missed=loop-vectorized`` prints the remark: + +.. code-block:: console + + no_switch.cpp:4:5: remark: loop not vectorized: vectorization is explicitly enabled [-Rpass-missed=loop-vectorize] + +And the command line ``-Rpass-analysis=loop-vectorize`` indicates that the +switch statement cannot be vectorized. + +.. code-block:: console + + no_switch.cpp:4:5: remark: loop not vectorized: loop contains a switch statement [-Rpass-analysis=loop-vectorize] + switch(A[i]) { + ^ + +To ensure line and column numbers are produced include the command line options +``-gline-tables-only`` and ``-gcolumn-info``. See the Clang `user manual +`_ +for details + Features -------- @@ -182,11 +265,14 @@ that scatter/gathers memory. .. code-block:: c++ - int foo(int *A, int *B, int n, int k) { - for (int i = 0; i < n; ++i) - A[i*7] += B[i*k]; + int foo(int * A, int * B, int n) { + for (intptr_t i = 0; i < n; ++i) + A[i] += B[i * 4]; } +In many situations the cost model will inform LLVM that this is not beneficial +and LLVM will only vectorize such code if forced with "-mllvm -force-vector-width=#". + Vectorization of Mixed Types ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -280,7 +366,7 @@ The decision to unroll the loop depends on the register pressure and the generat Performance ----------- -This section shows the the execution time of Clang on a simple benchmark: +This section shows the execution time of Clang on a simple benchmark: `gcc-loops `_. This benchmarks is a collection of loops from the GCC autovectorization `page `_ by Dorit Nuzman.