Automatically set the best argument for msvc /arch option

It does not seem that msvc provides an automatic setting for /arch (which would be the equivalent of -march=native with gcc/clang).

Is there a way to achieve it with cmake?

I saw in msvc doc about a cpuid command but is it the way to go (I’d like to limit the number of tests I run at configuration time)?
Another thing that I tested is like this:


check_cxx_compiler_flag("/arch:AVX" COMPILER_SUPPORTS_AVX)
  message(STATUS "The compiler supports /arch:AVX")
check_cxx_compiler_flag("/arch:AVX2" COMPILER_SUPPORTS_AVX2)
  message(STATUS "The compiler supports /arch:AVX2")
check_cxx_compiler_flag("/arch:AVX512" COMPILER_SUPPORTS_AVX512)
  message(STATUS "The compiler supports /arch:AVX512")

Yet I don’t know if a success means that the instruction set actually exists or if it only mean that msvc frontend accepts the value of the argument. MSVC documentation does not say a word about this.

Auto-Parallelization and Auto-Vectorization | Microsoft Learn seems to imply that their is some automatic mechanism by default but its interaction with /arch argument is unclear for me.


I think it is only that the compiler understands the flag. There’s no reason one could not compile code (that doesn’t need to run on) a processor from 2014 while targeting a processor from this year. cpuid is probably the best way to go here (if you want to emulate -march=native).