llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.37k stars 12.14k forks source link

std::exp not vectorized on static sized array #62283

Open vient opened 1 year ago

vient commented 1 year ago

Hi, Clang documentation on Loop Vectorizer mentions vectorizing of mathematical functions. I created a small test case, using latest Clang and GCC with -std=c++20 -march=skylake -Ofast: https://godbolt.org/z/dq1EGqszs

#include <array>
#include <cmath>
#include <numeric>

extern void use(auto&);
extern void use(auto&&);

int main() {
    double d[4], e[4];
    for (auto& x : d) {
        use(x);
    }
    for (size_t i = 0; i < std::size(d); ++i) {
        e[i] = std::exp(d[i]);  // should be vectorized
    }
    use(std::accumulate(std::begin(e), std::end(e), 0.));
}

GCC successfully inserted call to _ZGVdN4v_exp while Clang just calls exp 4 times. Array size or using float do not affect this. Documentation does not mention anything explicitly needed for vectorization besides -fno-math-errno — I use -Ofast.

In this case Clang should be able to vectorize exp calls, right?

vient commented 1 year ago

Trying other functions, Clang replaces std::floor with vroundpd while std::sin is not vectorized.

RKSimon commented 1 year ago

You need to specify what vector library you are using, e.g. -fveclib=libmvec for glibc vectormath

vient commented 1 year ago

Ok, it works with -fveclib=libmvec. Guess it would be nice to mention it in auto vectorization docs.

Thank you!

RKSimon commented 1 year ago

reopening - as we should mention this in the clang docs