<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - autovectorization of repeated calls to vectorizable functions fails"
   href="https://bugs.llvm.org/show_bug.cgi?id=40265">40265</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>autovectorization of repeated calls to vectorizable functions fails
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>6.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>kfjahnke@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>After consulting the documentation at

<a href="https://llvm.org/docs/Vectorizers.html">https://llvm.org/docs/Vectorizers.html</a>

I tried to trigger autovectorization of loops with vectorizable functions. The
documentation gives a list of functions which are meant to be vectorized in the
section headed 'Vectorization of function calls', namely

pow     exp     exp2
sin     cos     sqrt
log     log2    log10
fabs    floor   ceil
fma     trunc   nearbyint
                fmuladd

I found that most of the listed functions are not autovectorized. Since some of
the functions (e.g. floor, ceil, trunc) are autovectorized, I was able to patch
the resulting assembler code, replacing the vector op-codes (vroundps to
vsqrtps, also adapting the argument pattern), and found that the resulting
binary was significantly faster and worked as intended (I exemplarily did this
for 'sqrt' on my AVX2 system and got about 400% speedup). So my guess is that
the autovectorization opportunity is simply missed - the code structure to
produce assembler code for the given loop pattern is obviously there and
functioning. The compiler does indeed state it is unable to vectorize. I was
using this test code:

#include <cmath>

extern float data [ 32768 ] ;

extern void vf1()
{
  #pragma vectorize enable 
  for ( int i = 0 ; i < 32768 ; i++ )
    data [ i ] = std::sqrt ( data [ i ] ) ;
}

and this compiler call:

clang++ -fvectorize  -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize
-std=c++11 -O3 -mavx2 -S -o sqrt.s sqrt_gcc.cc

resulting in these diagnosic messages:

/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/cmath:464:12:
remark: loop not vectorized: call instruction cannot be vectorized
[-Rpass-analysis=loop-vectorize]
  { return __builtin_sqrtf(__x); }
           ^
sqrt_gcc.cc:14:3: remark: loop not vectorized: read with atomic ordering or
volatile read [-Rpass-analysis=loop-vectorize]
  for ( int i = 0 ; i < 32768 ; i++ )

using e.g. 'trunc' instead of 'sqrt' vectorizes correctly.

I did find an old thread here complaining about this behaviour:

<a href="http://clang-developers.42468.n3.nabble.com/Bug-with-vectorization-of-transcendental-functions-tc4041229.html#a4041291">http://clang-developers.42468.n3.nabble.com/Bug-with-vectorization-of-transcendental-functions-tc4041229.html#a4041291</a>

but it seems that there was no conclusion, so I am submitting this bug report,
hoping to revive the topic.

With regards
Kay F. Jahnke</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>