31

Whenever I try to compile any C++ program with march=native on a Macbook with a M1 chip, I get the following error when using clang:

clang: error: the clang compiler does not support '-march=native'

However, it used to work on an older Macbook with an Intel CPU. Does clang not support this architecture (yet)?

clang --version gives:

Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: arm64-apple-darwin20.2.0
Momo
  • 444
  • 1
  • 4
  • 11

5 Answers5

24

In Apple clang version 13.0.0, -mcpu=apple-m1 is now available.

champagniac
  • 444
  • 3
  • 4
  • where did you get this information? It doesn't work for me with Apple clang version 13.0.0, error: "clang: error: the clang compiler does not support '-march=apple-m1'" (base) build % clang --version Apple clang version 13.0.0 (clang-1300.0.29.3) Target: arm64-apple-darwin21.1.0 – SevenDays Dec 06 '21 at 09:21
  • 5
    @SevenDays my answer says "-mcpu" and not "-march". It provides the wanted functionality. The source of this information is me using it myself. You can also verify by running "clang --print-supported-cpus". – champagniac Dec 07 '21 at 10:05
  • thank you! It seems that new option is more optimized for macbook m1 and my mac works quieter after I recompiled with mcpu option – SevenDays Dec 07 '21 at 21:03
17
$ clang --print-supported-cpus
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin20.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Available CPUs for this target:

    a64fx
    apple-a10
    apple-a11
    apple-a12
    apple-a13
    apple-a14
    apple-a7
    apple-a8
    apple-a9
    apple-latest
    apple-s4
    apple-s5
    carmel
    cortex-a34
    cortex-a35
    cortex-a53
    cortex-a55
    cortex-a57
    cortex-a65
    cortex-a65ae
    cortex-a72
    cortex-a73
    cortex-a75
    cortex-a76
    cortex-a76ae
    cortex-a77
    cortex-a78
    cortex-x1
    cyclone
    exynos-m3
    exynos-m4
    exynos-m5
    falkor
    generic
    kryo
    lightning
    neoverse-e1
    neoverse-n1
    saphira
    thunderx
    thunderx2t99
    thunderx3t110
    thunderxt81
    thunderxt83
    thunderxt88
    tsv110
    vortex

-mcpu=apple-a14 might be the best one for M1.

Guanyang Xue
  • 171
  • 1
  • 2
  • According to the latest llvm definitions, the definition of apple-m1 and apple-a14 are identical. If your system doesn't have a cpu name defined for apple-m1 yet, but does for apple-a14, this definition update from April confirms Guanyang's answer is the best one at the time I'm writing this comment. See: https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Support/AArch64TargetParser.def – Khan Sep 06 '21 at 18:30
9

In Clang 15, -march=native now exists for Apple M1 (I personally use it).

Nick Botticelli
  • 175
  • 2
  • 6
8

As far as I know this is not Apple M1 specific, it occurs also with clang for various other architectures (mostly various other arm processors). e.g. See this bug report here https://github.com/DMOJ/judge-server/issues/303

Basically every build of clang for a new architecture must choose to provide defaults for "march=native" for the target for which the compiler is built or not; and if not then you will see this error message. Even for those arm processors who do have optimisation targets you often have to use specifically "-mcpu=xxx" instead of "-march".

e.g. For the iphone you would use -mcpu=apple-a11 -mcpu=apple-a12 etc.

However no such target has been implemented yet for Apple M1

Malcolm MacLeod
  • 624
  • 7
  • 15
  • 1
    Can I force a non `native` option? – Henry Mazza Mar 22 '21 at 18:19
  • @HenryMazza Sure, you can pass e.g. `-mcpu=apple-a11` even though you are compiling on an M1, this will produce code thats not optimised (or potentially may not even run on) your "native" target but for an a11 instead. In terms of this the best flags to currently optimise for M1 (given that clang has no M1 specific target yet) might be one of the targets for the closest ipad or iphone products. – Malcolm MacLeod Mar 22 '21 at 21:06
  • 5
    The closest option to use at the time of writing is `-mcpu=lightning`, which optimizes for an A13, which gives the compiler access to ARMv8.4-A instructions. – Charphacy May 12 '21 at 07:46
0

another solutions is update clang with brew

lang: error: the clang compiler does not support '-march=native'

1° - Install llvm con brew

brew install llvm

2° - Check clang version

cd /opt/homebrew/opt/llvm
.clang --version

Result:

Homebrew clang version 15.0.6
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

3° - modify path with the new version, for example add to .zshrc

export PATH="/opt/homebrew/opt/llvm/bin:$PATH"