简体   繁体   中英

C++ auto-vectorization requirements for gcc, clang and msvc

Are the following statements correct?

  1. With GCC and clang, my code will be auto-vectorized if I compile with :

    • -O2 -ftree-vectorize -march=XYZ (XYZ being the target instruction set: native, SSE, AVX2, etc.)
    • -O3 -march=XYZ
  2. With MSVC, my code will be auto-vectorized if I compile with:

    • /O2

This video seems to suggest that I do not need to specify the architecture with MSVC. Is that correct? The compiler will use the native architecture by default, and fall back on scalar operations at runtime if vector instructions can't be found.

I do not need to specify the architecture with MSVC. Is that correct?

Yes that is indeed correct. With MSVC, By default, the Auto-Vectorizer is enabled and picks up appropriate instructurion set for fastest vectorization. Moreover, even if you do specify arch , The Auto-Vectorizer may generate different instructions than specified by the /arch switch - as stated by documentation . For example, when you compile /arch:SSE2 , SSE4.2 instructions may be emitted.

On another note, The VS vectorizer lacks quite a bit of features when compared to gcc or clang.

With GCC and clang, my code will be auto-vectorized if I compile with -O2 -ftree-vectorize -march=XYZ ? -O3 -march=XYZ ?

Not necessarily, To enable vectorization of floating point reductions you need to use -ffast-math or -fassociative-math as well. However, in general, Yes it'll be enabled. You may find same written in documentation , Vectorization is enabled by the flag -ftree-vectorize and by default at -O3

PS: You may use https://godbolt.org to see all this in action!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM