9
votes

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?

2
What you want is a CPU dispatcher. Compile your code with -msse2 into one object file and -mfma into another then create a dispatcher which asks CPUID which hardware is available and set your function pointers to point to either the SSE2 or AVX/FMA versions stackoverflow.com/questions/23676426/…Z boson
@Zboson: Yep, that's what I did. Make it an answer and I will accept.Violet Giraffe
Recent gcc lets you use intrinsics without -mfma or -mavx. You can also specify the target per function.Marc Glisse

2 Answers

9
votes

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

2
votes

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.