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

Commits:

17 changed files:

Changes:

  • changelog.d/march-native
    1
    +section: compiler
    
    2
    +synopsis: Add :ghc-flag:`-march=native`, which on x86 and x86_64 enables the
    
    3
    +  CPU features of the machine running GHC.
    
    4
    +issues: #25450
    
    5
    +mrs: !16126

  • compiler/GHC/Driver/DynFlags.hs
    ... ... @@ -446,7 +446,7 @@ data DynFlags = DynFlags {
    446 446
       interactivePrint      :: Maybe String,
    
    447 447
     
    
    448 448
       -- Machine dependent flags (-m\<blah> stuff)
    
    449
    -  cpuFeatures           :: EnumSet CpuFeature, -- ^ The @-m\<feature\>@ flags that were passed.
    
    449
    +  cpuFeatures           :: EnumSet CpuFeature, -- ^ The CPU features requested by @-m...@ flags.
    
    450 450
                                                    -- See Note [CPU features] in GHC.Platform.CpuFeatures.
    
    451 451
       la664                 :: Bool, -- ^ Enable LA664 instructions
    
    452 452
     
    

  • compiler/GHC/Driver/Session.hs
    1 1
     {-# LANGUAGE CPP #-}
    
    2
    +{-# LANGUAGE MultiWayIf #-}
    
    2 3
     
    
    3 4
     -------------------------------------------------------------------------------
    
    4 5
     --
    
    ... ... @@ -229,6 +230,8 @@ import GHC.Platform.CpuFeatures
    229 230
     import GHC.Platform.Ways
    
    230 231
     import GHC.Platform.Profile
    
    231 232
     import GHC.Platform.ArchOS
    
    233
    +import GHC.Platform.Host (hostPlatformArch)
    
    234
    +import GHC.Platform.HostCpuFeatures (hostX86CpuFeatures)
    
    232 235
     
    
    233 236
     import GHC.Unit.Types
    
    234 237
     import GHC.Unit.Parser
    
    ... ... @@ -1709,6 +1712,7 @@ dynamic_flags_deps = [
    1709 1712
             (setDumpFlag Opt_D_dump_faststrings)
    
    1710 1713
     
    
    1711 1714
             ------ Machine dependent (-m<blah>) stuff ---------------------------
    
    1715
    +  , make_ord_flag defGhcFlag "march=native" (NoArg enableHostCpuFeatures)
    
    1712 1716
     
    
    1713 1717
       , make_ord_flag defGhcFlag "mla664"       (noArg (\d -> d { la664 = True }))
    
    1714 1718
     
    
    ... ... @@ -2934,6 +2938,17 @@ setDumpFlag dump_flag = NoArg (setDumpFlag' dump_flag)
    2934 2938
     setCpuFeature :: CpuFeature -> DynFlags -> DynFlags
    
    2935 2939
     setCpuFeature feat dflags = dflags { cpuFeatures = EnumSet.insert feat (cpuFeatures dflags) }
    
    2936 2940
     
    
    2941
    +-- | @-march=native@: enable the CPU features of the host GHC is running on.
    
    2942
    +enableHostCpuFeatures :: DynP ()
    
    2943
    +enableHostCpuFeatures = do
    
    2944
    +  arch <- platformArch . targetPlatform <$> liftEwM getCmdLineState
    
    2945
    +  if | arch /= ArchX86 && arch /= ArchX86_64
    
    2946
    +     -> addErr "-march=native is only supported on x86 and x86_64 targets"
    
    2947
    +     | arch /= hostPlatformArch
    
    2948
    +     -> addErr "-march=native is not supported when the target architecture differs from the host's"
    
    2949
    +     | otherwise
    
    2950
    +     -> upd $ \d -> d { cpuFeatures = cpuFeatures d `mappend` hostX86CpuFeatures }
    
    2951
    +
    
    2937 2952
     -- | The @-m\<feature\>@ flag for a 'CpuFeature'.
    
    2938 2953
     cpuFeatureFlagDep :: CpuFeature -> (Deprecation, Flag (CmdLineP DynFlags))
    
    2939 2954
     cpuFeatureFlagDep feat =
    

  • compiler/GHC/Platform/CpuFeatures.hs
    ... ... @@ -64,8 +64,9 @@ others:
    64 64
     
    
    65 65
     We keep two representations:
    
    66 66
     
    
    67
    -  A. 'DynFlags' records the set of flags that were passed, as an
    
    68
    -     'EnumSet' of 'CpuFeature'. Nothing is implied at this stage.
    
    67
    +  A. 'DynFlags' records the set of requested features, as an 'EnumSet' of
    
    68
    +     'CpuFeature': one per -m<feature> flag passed, plus the features of the
    
    69
    +     host under -march=native. Nothing is implied at this stage.
    
    69 70
     
    
    70 71
       B. 'initCpuFeatures' turns this set into a 'CpuFeatures' value, applying the
    
    71 72
          implications and the baseline of the target architecture. This
    

  • compiler/GHC/Platform/HostCpuFeatures.hs
    1
    +{-# LANGUAGE CPP #-}
    
    2
    +
    
    3
    +module GHC.Platform.HostCpuFeatures
    
    4
    +  ( hostX86CpuFeatures
    
    5
    +  ) where
    
    6
    +
    
    7
    +import GHC.Prelude
    
    8
    +
    
    9
    +import GHC.Data.EnumSet (EnumSet)
    
    10
    +import qualified GHC.Data.EnumSet as EnumSet
    
    11
    +import GHC.Platform.CpuFeatures (CpuFeature(..))
    
    12
    +
    
    13
    +import Data.Word (Word64)
    
    14
    +import System.IO.Unsafe (unsafePerformIO)
    
    15
    +
    
    16
    +#include "HostCpuFeaturesX86.h"
    
    17
    +
    
    18
    +#define GHC_X86_FEATURE_CONS(feat, cond) feat :
    
    19
    +
    
    20
    +-- | The features of the table in @HostCpuFeaturesX86.h@, in row order.
    
    21
    +probedX86CpuFeatures :: [CpuFeature]
    
    22
    +probedX86CpuFeatures = GHC_X86_PROBED_FEATURES(GHC_X86_FEATURE_CONS) []
    
    23
    +
    
    24
    +-- | The host's x86 CPU features.
    
    25
    +hostX86CpuFeatures :: EnumSet CpuFeature
    
    26
    +hostX86CpuFeatures = unsafePerformIO $ do
    
    27
    +  mask <- hostX86CpuFeatureMask
    
    28
    +  pure $ EnumSet.fromList
    
    29
    +    [ feat
    
    30
    +    | (bit, feat) <- zip [0 ..] probedX86CpuFeatures
    
    31
    +    , testBit mask bit
    
    32
    +    ]
    
    33
    +{-# NOINLINE hostX86CpuFeatures #-}
    
    34
    +
    
    35
    +#if defined(javascript_HOST_ARCH)
    
    36
    +hostX86CpuFeatureMask :: IO Word64
    
    37
    +hostX86CpuFeatureMask = pure 0
    
    38
    +#else
    
    39
    +foreign import ccall unsafe "ghc_host_x86_cpu_feature_mask"
    
    40
    +  hostX86CpuFeatureMask :: IO Word64
    
    41
    +#endif

  • compiler/HostCpuFeaturesX86.h
    1
    +/* The x86 features that -march=native probes. Each row pairs a CpuFeature
    
    2
    +   constructor with the C condition (see cbits/host_cpu_features_x86.c) that
    
    3
    +   detects the feature on the host. SSE and SSE2 are not probed: they are
    
    4
    +   GHC's x86 baseline.
    
    5
    +
    
    6
    +   Row i is bit i of the mask that ghc_host_x86_cpu_feature_mask returns and
    
    7
    +   GHC.Platform.HostCpuFeatures decodes, so the table can hold at most 64
    
    8
    +   rows. */
    
    9
    +#define GHC_X86_PROBED_FEATURES(F) \
    
    10
    +  F(Feat_SSE3,     SUPPORTS("sse3"))     \
    
    11
    +  F(Feat_SSSE3,    SUPPORTS("ssse3"))    \
    
    12
    +  F(Feat_SSE4,     SUPPORTS("sse4.1"))   \
    
    13
    +  F(Feat_SSE4_2,   SUPPORTS("sse4.2"))   \
    
    14
    +  F(Feat_AVX,      SUPPORTS("avx"))      \
    
    15
    +  F(Feat_AVX2,     SUPPORTS("avx2"))     \
    
    16
    +  F(Feat_AVX512F,  SUPPORTS("avx512f"))  \
    
    17
    +  F(Feat_AVX512BW, SUPPORTS("avx512bw")) \
    
    18
    +  F(Feat_AVX512CD, SUPPORTS("avx512cd")) \
    
    19
    +  F(Feat_AVX512DQ, SUPPORTS("avx512dq")) \
    
    20
    +  F(Feat_AVX512VL, SUPPORTS("avx512vl")) \
    
    21
    +  F(Feat_BMI,      SUPPORTS("bmi"))      \
    
    22
    +  F(Feat_BMI2,     SUPPORTS("bmi2"))     \
    
    23
    +  F(Feat_FMA,      SUPPORTS_FMA)         \
    
    24
    +  F(Feat_GFNI,     SUPPORTS_GFNI)

  • compiler/cbits/host_cpu_features_x86.c
    1
    +#include <HsFFI.h>
    
    2
    +
    
    3
    +/* Runtime x86 CPU feature detection backing GHC.Platform.HostCpuFeatures
    
    4
    +   (-march=native).
    
    5
    +
    
    6
    +   The __builtin_cpu_* calls below compile to references to libgcc/compiler-rt
    
    7
    +   symbols (__cpu_model & co.), which GHCi's runtime linker can only resolve
    
    8
    +   because the RTS exports them: see RTS_X86_CPU_MODEL_SYMBOLS in
    
    9
    +   rts/RtsSymbols.c. */
    
    10
    +
    
    11
    +#include "HostCpuFeaturesX86.h"
    
    12
    +
    
    13
    +#if defined(__i386__) || defined(__x86_64__)
    
    14
    +
    
    15
    +#define SUPPORTS(name) __builtin_cpu_supports(name)
    
    16
    +
    
    17
    +/* FMA is usable only when the OS saves the AVX register state.  However
    
    18
    +   compiler-rt's cpu_model/x86.c reports "fma" from the raw CPUID bit. */
    
    19
    +#define SUPPORTS_FMA (SUPPORTS("fma") && SUPPORTS("avx"))
    
    20
    +
    
    21
    +/* GFNI joined the __builtin_cpu_supports feature lists later than the
    
    22
    +   other features here (GCC 11; compiler-rt's cpu_model in LLVM 12).
    
    23
    +   Under an older bootstrap compiler we conservatively report no GFNI. */
    
    24
    +#if (defined(__clang_major__) && __clang_major__ >= 12) \
    
    25
    +    || (!defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 11)
    
    26
    +#define SUPPORTS_GFNI SUPPORTS("gfni")
    
    27
    +#else
    
    28
    +#define SUPPORTS_GFNI 0
    
    29
    +#endif
    
    30
    +
    
    31
    +HsWord64 ghc_host_x86_cpu_feature_mask(void)
    
    32
    +{
    
    33
    +  __builtin_cpu_init();
    
    34
    +
    
    35
    +  HsWord64 mask = 0;
    
    36
    +  int bit = 0;
    
    37
    +
    
    38
    +#define GHC_X86_PROBE(feat, cond) \
    
    39
    +  if (cond) { mask |= (HsWord64)1 << bit; } \
    
    40
    +  bit++;
    
    41
    +
    
    42
    +  GHC_X86_PROBED_FEATURES(GHC_X86_PROBE)
    
    43
    +
    
    44
    +#undef GHC_X86_PROBE
    
    45
    +
    
    46
    +  return mask;
    
    47
    +}
    
    48
    +
    
    49
    +#else
    
    50
    +
    
    51
    +HsWord64 ghc_host_x86_cpu_feature_mask(void)
    
    52
    +{
    
    53
    +  return 0;
    
    54
    +}
    
    55
    +
    
    56
    +#endif

  • compiler/ghc.cabal.in
    ... ... @@ -32,6 +32,7 @@ extra-source-files:
    32 32
         GHC/Builtin/primops.txt.pp
    
    33 33
         Unique.h
    
    34 34
         CodeGen.Platform.h
    
    35
    +    HostCpuFeaturesX86.h
    
    35 36
         -- Shared with rts via hard-link at configure time. This is safer
    
    36 37
         -- for Windows, where symlinks don't work out of the box, so we
    
    37 38
         -- can't just commit some in git.
    
    ... ... @@ -186,6 +187,7 @@ Library
    186 187
           c-sources:
    
    187 188
             cbits/cutils.c
    
    188 189
             cbits/genSym.c
    
    190
    +        cbits/host_cpu_features_x86.c
    
    189 191
             cbits/keepCAFsForGHCi.c
    
    190 192
     
    
    191 193
         hs-source-dirs:
    
    ... ... @@ -690,6 +692,7 @@ Library
    690 692
             GHC.Platform.AArch64
    
    691 693
             GHC.Platform.Constants
    
    692 694
             GHC.Platform.CpuFeatures
    
    695
    +        GHC.Platform.HostCpuFeatures
    
    693 696
             GHC.Platform.LA64
    
    694 697
             GHC.Platform.NoRegs
    
    695 698
             GHC.Platform.PPC
    

  • docs/users_guide/expected-undocumented-flags.txt
    ... ... @@ -74,6 +74,7 @@
    74 74
     -instantiated-with
    
    75 75
     -keep-hi-file
    
    76 76
     -keep-o-file
    
    77
    +-march=native
    
    77 78
     -n
    
    78 79
     -no-keep-hi-file
    
    79 80
     -no-keep-o-file
    

  • docs/users_guide/using.rst
    ... ... @@ -1887,6 +1887,30 @@ Some flags only make sense for particular target platforms.
    1887 1887
         so this flag has no effect when used with the :ref:`native code generator <native-code-gen>`
    
    1888 1888
         or the :ref:`LLVM backend <llvm-code-gen>`.
    
    1889 1889
     
    
    1890
    +.. ghc-flag:: -march=native
    
    1891
    +    :shortdesc: (x86 only) Enable the CPU features of the host
    
    1892
    +    :type: dynamic
    
    1893
    +    :category: platform-options
    
    1894
    +
    
    1895
    +    (x86 only) Probe the CPU of the machine running GHC and enable the
    
    1896
    +    corresponding ``-m...`` CPU-feature options (for example ``-msse4.2``,
    
    1897
    +    ``-mavx2``, ``-mbmi2``, ``-mfma``). The detected features apply to both the
    
    1898
    +    :ref:`native code generator <native-code-gen>` and the
    
    1899
    +    :ref:`LLVM backend <llvm-code-gen>`.
    
    1900
    +
    
    1901
    +    The detected features are enabled in addition to any ``-m...`` options
    
    1902
    +    given explicitly, regardless of their order on the command line.
    
    1903
    +
    
    1904
    +    .. warning::
    
    1905
    +
    
    1906
    +        Code compiled with ``-march=native`` may use instructions that are not
    
    1907
    +        available on other CPUs, and is therefore not portable to a different
    
    1908
    +        machine.
    
    1909
    +
    
    1910
    +    The flag is rejected on other targets. A cross-compiler also rejects it
    
    1911
    +    when its target architecture differs from the one GHC itself runs on, since
    
    1912
    +    the probed CPU then says nothing about the target.
    
    1913
    +
    
    1890 1914
     Haddock
    
    1891 1915
     -------
    
    1892 1916
     
    

  • rts/RtsSymbols.c
    ... ... @@ -1045,6 +1045,35 @@ extern char **environ;
    1045 1045
     #define RTS_ARM_OUTLINE_ATOMIC_SYMBOLS
    
    1046 1046
     #endif
    
    1047 1047
     
    
    1048
    +// libgcc/compiler-rt symbols behind __builtin_cpu_supports and
    
    1049
    +// __builtin_cpu_init, which compiler/cbits/host_cpu_features_x86.c uses
    
    1050
    +// (-march=native). Without them the runtime linker cannot load the ghc
    
    1051
    +// library's object code, e.g. for plugins in a statically linked GHCi.
    
    1052
    +#if defined(i386_HOST_ARCH) || defined(x86_64_HOST_ARCH)
    
    1053
    +
    
    1054
    +// Weak: missing before GCC 11 and LLVM 12.
    
    1055
    +extern StgWord __cpu_features2[] __attribute__((weak));
    
    1056
    +
    
    1057
    +// Alpine's and Gentoo's musl GCCs patch __builtin_cpu_init to call this
    
    1058
    +// alias of __cpu_indicator_init. Weak: other musl toolchains lack it.
    
    1059
    +#if defined(linux_HOST_OS) && !defined(__GLIBC__)
    
    1060
    +extern void __cpu_indicator_init_local(void) __attribute__((weak));
    
    1061
    +#define RTS_X86_CPU_INIT_LOCAL_SYMBOLS \
    
    1062
    +      SymI_NeedsProto(__cpu_indicator_init_local)
    
    1063
    +#else
    
    1064
    +#define RTS_X86_CPU_INIT_LOCAL_SYMBOLS
    
    1065
    +#endif
    
    1066
    +
    
    1067
    +#define RTS_X86_CPU_MODEL_SYMBOLS \
    
    1068
    +      SymI_NeedsProto(__cpu_indicator_init) \
    
    1069
    +      SymI_NeedsDataProto(__cpu_model) \
    
    1070
    +      SymI_NeedsDataProto(__cpu_features2) \
    
    1071
    +      RTS_X86_CPU_INIT_LOCAL_SYMBOLS
    
    1072
    +
    
    1073
    +#else
    
    1074
    +#define RTS_X86_CPU_MODEL_SYMBOLS
    
    1075
    +#endif
    
    1076
    +
    
    1048 1077
     // Symbols defined by libc
    
    1049 1078
     #define RTS_LIBC_SYMBOLS                               \
    
    1050 1079
           SymI_HasProto_redirect(atexit, atexit, STRENGTH_STRONG, SYM_TYPE_CODE) /* See Note [Strong symbols] */ \
    
    ... ... @@ -1205,6 +1234,7 @@ RTS_OPENBSD_ONLY_SYMBOLS
    1205 1234
     RTS_LIBC_SYMBOLS
    
    1206 1235
     RTS_LIBGCC_SYMBOLS
    
    1207 1236
     RTS_ARCH_LIBGCC_SYMBOLS
    
    1237
    +RTS_X86_CPU_MODEL_SYMBOLS
    
    1208 1238
     RTS_FINI_ARRAY_SYMBOLS
    
    1209 1239
     RTS_LIBFFI_SYMBOLS
    
    1210 1240
     RTS_ARM_OUTLINE_ATOMIC_SYMBOLS
    
    ... ... @@ -1260,6 +1290,7 @@ void initLinkerRtsSyms (StrHashTable *symhash) {
    1260 1290
           RTS_OPENBSD_ONLY_SYMBOLS
    
    1261 1291
           RTS_LIBGCC_SYMBOLS
    
    1262 1292
           RTS_ARCH_LIBGCC_SYMBOLS
    
    1293
    +      RTS_X86_CPU_MODEL_SYMBOLS
    
    1263 1294
           RTS_FINI_ARRAY_SYMBOLS
    
    1264 1295
           RTS_LIBFFI_SYMBOLS
    
    1265 1296
           RTS_ARM_OUTLINE_ATOMIC_SYMBOLS
    

  • testsuite/tests/codeGen/should_gen_asm/all.T
    ... ... @@ -17,6 +17,9 @@ test('msse-option-order', [unless(arch('x86_64') or arch('i386'), skip),
    17 17
                                when(unregisterised(), skip)], compile_grep_asm, ['hs', False, '-msse4.2 -msse2'])
    
    18 18
     test('mavx-should-enable-popcnt', [unless(arch('x86_64') or arch('i386'), skip),
    
    19 19
                                        when(unregisterised(), skip)], compile_grep_asm, ['hs', False, '-mavx'])
    
    20
    +test('march-native-enables-popcnt',
    
    21
    +     [unless((arch('x86_64') or arch('i386')) and have_cpu_feature('sse4_2'), skip),
    
    22
    +      when(unregisterised(), skip)], compile_grep_asm, ['hs', False, '-march=native'])
    
    20 23
     test('avx512-int64-mul', [unless(arch('x86_64'), skip),
    
    21 24
                               when(unregisterised(), skip)], compile_grep_asm, ['hs', True, '-mavx512dq -mavx512vl'])
    
    22 25
     test('avx512-int64-minmax', [unless(arch('x86_64'), skip),
    

  • testsuite/tests/codeGen/should_gen_asm/march-native-enables-popcnt.asm
    1
    +popcnt(?![0-9])
    \ No newline at end of file

  • testsuite/tests/codeGen/should_gen_asm/march-native-enables-popcnt.hs
    1
    +-- On a host with SSE4.2, `-march=native` should enable `popcnt`.
    
    2
    +import Data.Bits
    
    3
    +
    
    4
    +{-# NOINLINE foo #-}
    
    5
    +foo :: Int -> Int
    
    6
    +foo x = 1 + popCount x
    
    7
    +
    
    8
    +main :: IO ()
    
    9
    +main = print (foo 42)

  • testsuite/tests/driver/all.T
    ... ... @@ -223,6 +223,11 @@ test('T9938B', [], makefile_test, [])
    223 223
     test('T9963', exit_code(1), run_command,
    
    224 224
          ['{compiler} --interactive -ignore-dot-ghci --print-libdir'])
    
    225 225
     
    
    226
    +test('march_native_unsupported_arch',
    
    227
    +     [when(arch('x86_64') or arch('i386'), skip)],
    
    228
    +     compile_fail,
    
    229
    +     ['-march=native'])
    
    230
    +
    
    226 231
     test('T10219', normal, run_command,
    
    227 232
          # `-x hspp` in make mode should work.
    
    228 233
          # Note: need to specify `-x hspp` before the filename.
    

  • testsuite/tests/driver/march_native_unsupported_arch.hs
    1
    +module M where

  • testsuite/tests/driver/march_native_unsupported_arch.stderr
    1
    +ghc: on the commandline: -march=native is only supported on x86 and x86_64 targets
    
    2
    +Usage: For basic information, try the `--help' option.