Simon Jakobi pushed to branch wip/sjakobi/T25450-march-native at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • compiler/cbits/cpu_features_x86.c
    ... ... @@ -10,6 +10,10 @@
    10 10
     #include <cpuid.h>
    
    11 11
     #endif
    
    12 12
     
    
    13
    +#if defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__))
    
    14
    +#include <sys/sysctl.h>
    
    15
    +#endif
    
    16
    +
    
    13 17
     enum {
    
    14 18
       GHC_X86_FEAT_SSE2 = 0,
    
    15 19
       GHC_X86_FEAT_SSE3,
    
    ... ... @@ -70,6 +74,19 @@ static uint64_t ghc_xgetbv0(void)
    70 74
     #endif
    
    71 75
     }
    
    72 76
     
    
    77
    +#if defined(__APPLE__) && (defined(__i386__) || defined(__x86_64__))
    
    78
    +/* Query a macOS CPU-capability sysctl, e.g. "hw.optional.avx512f". */
    
    79
    +static int ghc_macos_sysctl_flag(const char *name)
    
    80
    +{
    
    81
    +  int result = 0;
    
    82
    +  size_t len = sizeof(result);
    
    83
    +  if (sysctlbyname(name, &result, &len, NULL, 0) != 0) {
    
    84
    +    return 0;
    
    85
    +  }
    
    86
    +  return result != 0;
    
    87
    +}
    
    88
    +#endif
    
    89
    +
    
    73 90
     HsWord64 ghc_detect_x86_cpu_features(void)
    
    74 91
     {
    
    75 92
       HsWord64 feats = 0;
    
    ... ... @@ -107,6 +124,20 @@ HsWord64 ghc_detect_x86_cpu_features(void)
    107 124
           avx512_usable = ((xcr0 & 0xE6u) == 0xE6u); /* XMM+YMM+opmask+ZMM */
    
    108 125
         }
    
    109 126
     
    
    127
    +#if defined(__APPLE__)
    
    128
    +    /* On x86_64 macOS the kernel enables AVX-512 XSAVE state lazily: XCR0
    
    129
    +       reads back with the opmask/ZMM bits clear until a process first faults
    
    130
    +       on an AVX-512 instruction, so the XCR0 check above is a false negative
    
    131
    +       on AVX-512-capable Macs. Use the OS feature query instead. Checking
    
    132
    +       AVX512F alone suffices here; the AVX-512 sub-features (BW/CD/DQ/VL) are
    
    133
    +       still decoded from CPUID leaf 7 below.
    
    134
    +
    
    135
    +       Refs:
    
    136
    +         https://zenn.dev/mod_poppo/articles/detect-processor-features-x86?locale=en#notes-on-detecting-avx-512-on-macos
    
    137
    +         https://github.com/minoki/haskell-cpu-features */
    
    138
    +    avx512_usable = ghc_macos_sysctl_flag("hw.optional.avx512f");
    
    139
    +#endif
    
    140
    +
    
    110 141
         if (has_sse2) {
    
    111 142
           SET_FEAT(feats, GHC_X86_FEAT_SSE2);
    
    112 143
         }