简体   繁体   English

gcc、clang 和 msvc 的 C++ 自动矢量化要求

[英]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 :使用 GCC 和 clang,如果我使用以下命令进行编译,我的代码将自动矢量化:

    • -O2 -ftree-vectorize -march=XYZ (XYZ being the target instruction set: native, SSE, AVX2, etc.) -O2 -ftree-vectorize -march=XYZ (XYZ 是目标指令集:native、SSE、AVX2 等)
    • -O3 -march=XYZ
  2. With MSVC, my code will be auto-vectorized if I compile with:使用 MSVC,如果我使用以下命令进行编译,我的代码将自动矢量化:

    • /O2

This video seems to suggest that I do not need to specify the architecture with MSVC. 这个视频似乎暗示我不需要用 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.我不需要用 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.使用 MSVC,默认情况下,自动矢量化器处于启用状态并选取合适的指令集以实现最快的矢量化。 Moreover, even if you do specify arch , The Auto-Vectorizer may generate different instructions than specified by the /arch switch - as stated by documentation .此外,即使您确实指定了arch ,自动矢量化器可能会生成与/arch开关指定的指令不同的指令 - 如文档所述 For example, when you compile /arch:SSE2 , SSE4.2 instructions may be emitted.例如,当您编译/arch:SSE2 ,可能会发出SSE4.2指令。

On another note, The VS vectorizer lacks quite a bit of features when compared to gcc or clang.另一方面,与 gcc 或 clang 相比,VS 矢量化器缺少相当多的功能。

With GCC and clang, my code will be auto-vectorized if I compile with -O2 -ftree-vectorize -march=XYZ ?使用 GCC 和 clang,如果我使用-O2 -ftree-vectorize -march=XYZ编译,我的代码将被自动矢量-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.不一定,要启用浮点归约的矢量化,您还需要使用-ffast-math-fassociative-math 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您可能会在文档中找到相同的内容矢量化由标志 -ftree-vectorize 启用,默认情况下在 -O3

PS: You may use https://godbolt.org to see all this in action! PS:您可以使用https://godbolt.org来查看这一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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