0

Compilers: all of the three clang11 / gcc10 / msvc (vs2019).

What I want to write

my_math_func(raw_pointers_to_data)
{
    if (CpuSupportsAvx512()) {
        /* processing data via __m512  and _mm512_intrinsics */
    } else if(CpuSupportsAvx2()) {
        /* processing data via AVX2-specific */
    } else {
        /* processing data via SSE4 */
    }
}

The problem is that GCC (and AFAIK clang) refuse to compile when I set the -msse4.2.

If I set -mavx512f then the compiler can generate avx512 specific instructions anywhere in the binary, which is unacceptable for me.

The question is, is there a way to tell the compiler "please, let me use AVX+ intrinsics where I want, but don't generate avx+ instructions anywhere else" without using raw assembly or moving code to a separate dynamic library?

I'm ready to take responsibility (and future ERROR: Illegal Instruction) for checking that my avx512 blocks won't run on an incompatible CPU.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
FedyuninV
  • 141
  • 10
  • 6
    You'll need to write the AVX512 code in a separate function. And then you can use an attribute or pragma to tell gcc to compile this function for a different target, or more simply put the function in a different file that you compile with other options. – Marc Glisse Aug 08 '22 at 21:54
  • 1
    The simplest way is just let the compiler and linker handle this with the function multi-version feature using the `target` and `target_clones` attributes: [Building backward compatible binaries with newer CPU instructions support](https://stackoverflow.com/q/61005492/995714), [Generate code for multiple SIMD architectures](https://stackoverflow.com/q/44479069/995714) – phuclv Aug 09 '22 at 01:19

0 Answers0