# Automatically set the best argument for msvc /arch option

**URL:** https://discourse.cmake.org/t/automatically-set-the-best-argument-for-msvc-arch-option/9778
**Category:** Usage
**Created:** [January 5, 2024, 2:55pm UTC](https://discourse.cmake.org/t/automatically-set-the-best-argument-for-msvc-arch-option/9778 "2024-01-05T14:55:09Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [January 5, 2024, 2:55pm UTC](https://discourse.cmake.org/t/automatically-set-the-best-argument-for-msvc-arch-option/9778/1 "2024-01-05T14:55:09Z")

</div>

Hi,  
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:

```cmake
include(CheckCXXCompilerFlag)

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

```

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](https://learn.microsoft.com/en-us/cpp/parallel/auto-parallelization-and-auto-vectorization?view=msvc-170) seems to imply that their is some automatic mechanism by default but its interaction with `/arch` argument is unclear for me.

regards,  
A.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 11, 2024, 1:27pm UTC](https://discourse.cmake.org/t/automatically-set-the-best-argument-for-msvc-arch-option/9778/2 "2024-01-11T13:27:57Z")

</div>

> [@adaldev](#):
>
> 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.

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`).
