2

I have a MacBook Pro 2017 with a 2.9 GHz Intel Core i7-7820HQ, running macOS Mojave 10.14.6. When I look for supported CPU instruction sets, I don't see AVX2

$ sysctl -a | grep cpu.feat
machdep.cpu.feature_bits: 9221960262849657855
machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C

However, if I run an ubuntu Docker container and request CPU information, I see AVX2 is supported

$ docker run -it --rm ubuntu
$ grep flags /proc/cpuinfo
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht pbe syscall nx pdpe1gb lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq dtes64 ds_cpl ssse3 sdbg fma cx16 xtpr pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch kaiser fsgsbase bmi1 hle avx2 bmi2 erms rtm xsaveopt arat
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht pbe syscall nx pdpe1gb lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq dtes64 ds_cpl ssse3 sdbg fma cx16 xtpr pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch kaiser fsgsbase bmi1 hle avx2 bmi2 erms rtm xsaveopt arat
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht pbe syscall nx pdpe1gb lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq dtes64 ds_cpl ssse3 sdbg fma cx16 xtpr pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch kaiser fsgsbase bmi1 hle avx2 bmi2 erms rtm xsaveopt arat
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht pbe syscall nx pdpe1gb lm constant_tsc rep_good nopl xtopology nonstop_tsc pni pclmulqdq dtes64 ds_cpl ssse3 sdbg fma cx16 xtpr pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch kaiser fsgsbase bmi1 hle avx2 bmi2 erms rtm xsaveopt arat

According to Intel, the Core i7-7820HQ supports AVX2.

Why does OS X not report my CPU supporting AVX2 when it apparently does?

Daniel
  • 8,179
  • 6
  • 31
  • 56
  • How old is MacOS Mojave? Does it ever identify AVX2 on any CPU? AVX2 doesn't introduce any new architectural state beyond AVX1, so the OS itself doesn't actually need to know about it for user-space processes to use it and FMA (as long as the OS supports AVX). So maybe MacOS got lazy and stopped updating `machdep.cpu.features` until AVX512 or something. Anyway yes, your CPU supports AVX2, like all i3/i5/i7 CPUs of Haswell or newer. (I don't have a Mac and have no idea why its tools don't bother to report that CPU feature.) – Peter Cordes Aug 28 '19 at 10:02
  • 1
    @PeterCordes This is the latest MacOS available. However, I don't recall any recent previous versions having reported AVX2 either. It would be annoying if they just don't bother to report it as I use this information to determine which instruction sets to enable in my application (in CMake). – Daniel Aug 28 '19 at 10:08
  • 1
    You know you can use gcc, icc, or `clang -O3 -march=native`, right? That enables all the `-mavx -maxv2 -mfma -mbmi2 -mbmi -mpopcnt` and so on options your CPU supports, *and* sets tuning options for your CPU instead of `tune=generic`. It defines the same CPP macros (`__AVX2__`) as using the manual options. BTW, your question would be more on-topic if you refocused it on how to detect CPU features in CMake on MacOS, e.g. if you have your AVX2 functions in a separate file, instead of just in `#ifdef` in files you compile anyway. – Peter Cordes Aug 28 '19 at 10:16
  • Probably worth another question, but how does the compiler determine which instructions the CPU supports if not using the methods I described? – Daniel Aug 28 '19 at 10:33
  • 2
    With the `cpuid` x86 instruction. Runtime CPU detection is well-supported on x86, including a CPUID bit that indicates the OS has promised to save/restore the high halves of YMM registers on context switches, making it possible to [detect both CPU support *and* OS support](https://stackoverflow.com/questions/34069054) without any system calls, purely in user-space. An alternative is running instructions and catching SIGILL. [AVX feature detection using SIGILL versus CPU probing](//stackoverflow.com/q/44144763); could be useful on non-x86 without a CPUID equivalent. – Peter Cordes Aug 28 '19 at 10:50

2 Answers2

2

You can also detect it using: sysctl -a | grep hw.optional.avx2_0

The main cpu features are under the hw.optional group.

Jim Perry
  • 21
  • 2
1

It turned out that AVX2 appears in a separate 'list' of CPU features (machdep.cpu.leaf7_features). See my answer here

Daniel
  • 8,179
  • 6
  • 31
  • 56