[cfe-dev] Bug with vectorization of transcendental functions
Erik Schnetter
schnetter at cct.lsu.edu
Tue Aug 19 12:28:16 PDT 2014
On Sun, Aug 17, 2014 at 9:43 AM, Ian Bell <ian.h.bell at gmail.com> wrote:
> Hello all,
>
> I am trying to optimize an inner loop in C++ in which nearly 100% of my
> runtime occurs. In this loop I have to do some exponential calls, and
> according to the docs at
> http://llvm.org/docs/Vectorizers.html#vectorization-of-function-calls ,
> it seems that the exp function should be able to be vectorized using SIMD
> instructions. I'm using the tippy SVN clang.
>
To my knowledge, Clang does not vectorize arbitrary math functions; it only
vectorizes those functions for which a SIMD machine instruction exists,
such as floor.
There are libraries that provide vectorized exp() functions; you could try
using one of these. However, these libraries require that you rewrite your
code to use vectors instead of scalars. I maintain such a library
"vecmathlib" <https://bitbucket.org/eschnett/vecmathlib/wiki/Home> that is
used in pocl <http://pocl.sourceforge.net/>, an OpenCL implementation based
on LLVM.
-erik
The example here shows that the floor function is getting vectorized, but
> the exp function isn't. Is this is a bug or known behavior? I see the
> same problem if I use floats for what its worth.
>
> By the way, there are some problems with the auto-vectorization docs,
> vectorization is only enabled at -O3, it took me an hour to try to figure
> out why my -Rpass=loop-vectorize wasn't giving ANY output.
>
> My code (loop_test2.cpp):
>
> #include <vector>
> #include <cmath>
>
> typedef double numtype;
>
> void foo(numtype *f) {
> for (int i = 0; i != 1024; ++i)
> f[i] = floor(f[i]);
> }
>
> void foo2(numtype *f) {
> for (int i = 0; i != 1024; ++i)
> f[i] = exp(f[i]);
> }
>
> int main()
> {
> std::vector<numtype> f(1024, 1.3);
> foo(&f[0]);
> return 0;
> }
>
> Compilation:
> ../../../build/Debug+Asserts/bin/clang++ -c -Rpass=loop-vectorize
> -Rpass-analysis=loop-vectorize -O3 -gline-tables-only -gcolumn-info
> loop_test2.cpp
>
> which yields
>
> loop_test2.cpp:7:8: remark: unrolled with interleaving factor 2
> (vectorization
> not beneficial) [-Rpass=loop-vectorize]
> for (int i = 0; i != 1024; ++i)
> ^
> loop_test2.cpp:13:12: remark: loop not vectorized: call instruction cannot
> be
> vectorized [-Rpass-analysis=loop-vectorize]
> f[i] = exp(f[i]);
> ^
> loop_test2.cpp:18:3: remark: vectorized loop (vectorization factor: 2,
> unrolling
> interleave factor: 2) [-Rpass=loop-vectorize]
> std::vector<numtype> f(1024, 1.3);
> ^
> loop_test2.cpp:8:12: remark: unrolled with interleaving factor 2
> (vectorization
> not beneficial) [-Rpass=loop-vectorize]
> f[i] = floor(f[i]);
> ^
>
> Thanks,
> Ian
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
--
Erik Schnetter <schnetter at cct.lsu.edu>
http://www.perimeterinstitute.ca/personal/eschnetter/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140819/6d3e150a/attachment.html>
More information about the cfe-dev
mailing list