为什么march=native在苹果M1上不起作用?

nue99wik  于 2023-01-12  发布在  其他
关注(0)|答案(5)|浏览(282)

每当我尝试在带有M1芯片的MacBook上使用march=native编译任何C++程序时,使用clang时会出现以下错误:

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

不过,它曾经在配备英特尔CPU的较老MacBook上工作,Clang(还)不支持这种架构吗?
clang --version给出:

Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: arm64-apple-darwin20.2.0
mrzz3bfm

mrzz3bfm1#

在Apple Clang 13.0.0版本中,-mcpu=apple-m1现在可用。

ia2d9nvy

ia2d9nvy2#

$ 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可能是M1的最佳选择。

cngwdvgl

cngwdvgl3#

据我所知,这不是Apple M1特有的,它也会在各种其他架构(主要是各种其他arm处理器)中发生。例如,请参阅此处的错误报告https://github.com/DMOJ/judge-server/issues/303
基本上,每一个新架构的clang构建都必须选择为编译器构建的目标提供默认值“march=native”;如果没有,那么你会看到这个错误消息。即使对于那些确实有优化目标的ARM处理器,你也经常不得不特别使用“-MCPU=xxx”而不是“-March”。
例如,对于iPhone,您将使用-mcpu=apple-a11-mcpu=apple-a12等。
但是,尚未针对Apple M1实现此类目标

qv7cva1a

qv7cva1a4#

在Clang 15中,-march=native现在为苹果M1而存在(我个人使用它)。

z6psavjg

z6psavjg5#

另一种解决方案是用BREW更新Clang
lang:错误:clang编译器不支持“-march=native”
1° -安装llvm conbrew

brew install llvm

2° -检查clang版本

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

结果:

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

3° -使用新版本修改路径,例如添加到.zshrc

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

相关问题