Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 992
Violet Giraffe Avatar asked Sep 18 '13 09:09

Violet Giraffe


2 Answers

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

like image 51
Z boson Avatar answered Oct 17 '22 14:10

Z boson


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.

like image 32
Mats Petersson Avatar answered Oct 17 '22 16:10

Mats Petersson