Preventing GCC from automatically using AVX and FMA instructions when compiled with -mavx and -mfma

不羁的心 提交于 2019-11-26 16:38:30

问题


How can I disable auto-vectorization with AVX and FMA instructions? I would still prefer the compiler to employ SSE and SSE2 automatically, but not FMA and AVX.

My code that uses AVX checks for its availability, but GCC doesn't do it when auto-vectorizing. So if I compile with -mfma and run the code on any CPU prior to Haswell I get SIGILL. How to solve this issue?


回答1:


What you want to do is compile different object files for each instruction set you are targeting. Then create a cpu dispatcher which asks CPUID for the available instruction set and then jumps to the appropriate version of the function.

I already described this in several different questions and answers

  • disable-avx2-functions-on-non-haswell-processors

  • do-i-need-to-make-multiple-executables-for-targetting-different-instruction-set

  • how-to-check-with-intel-intrinsics-if-avx-extensions-is-supported-by-the-cpu

  • cpu-dispatcher-for-visual-studio-for-avx-and-sse

  • create-separate-object-files-from-the-same-source-code-and-link-to-an-executable




回答2:


You will need to separate the code that uses AVX into a separate compile unit (in other words, a separate .cpp file), and compile only that with -mfma or whatever options you want. Normally, gcc will use -march=native, so it will compile for "your processor", and if you want generic code, you will need to use -march=x86_64 or -march=core2, or something like that.



来源:https://stackoverflow.com/questions/18868235/preventing-gcc-from-automatically-using-avx-and-fma-instructions-when-compiled-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!