118

目前我对 ARM 感兴趣,特别是 iphone/android 目标。但我只是想了解更多关于 clang 的信息,因为感觉它在未来几年会发挥重要作用。

我试过了

clang -cc1 --help|grep -i list
clang -cc1 --help|grep arch|grep -v search
clang -cc1 --help|grep target

 -triple <value>         Specify target triple (e.g. i686-apple-darwin9)

我知道 clang 有 -triplet 参数,但是如何列出它的所有可能值?我发现 clang 在交叉编译方面与 gcc 非常不同,在 GCC 世界中,您应该对所有内容都有单独的二进制文件,例如 PLATFORM_make 或 PLATFORM_ld (i*86-pc-cygwin i*86-*-linux-gnu 等。http ://git.savannah.gnu.org/cgit/libtool.git/tree/doc/PLATFORMS )

在 clang 世界中,它只是一个二进制文件(正如我在一些论坛上看到的那样)。但是我如何获得支持的目标列表呢?如果我的目标在我的发行版(linux/windows/macos/whatever)上不受支持,我怎样才能获得支持更多平台的目标?

如果我 SVN 最新的叮当声是这样的:

svn co http://llvm.org/svn/llvm-project/cfe/trunk clang

我会得到大部分平台吗?看起来 Clang 并没有立即考虑交叉编译,但是由于它是基于 llvm 的,所以理论上应该是非常交叉友好的?谢谢你!

4

11 回答 11

68

据我所知,没有命令行选项可以列出给定clang二进制文件支持的体系结构,甚至strings在其上运行也无济于事。Clang 本质上只是一个 C 到 LLVM 的翻译器,而 LLVM 本身负责处理生成实际机器代码的细节,所以 Clang 没有过多关注底层架构也就不足为奇了。

正如其他人已经指出的那样,您可以询问llc它支持哪些架构。这并不是很有帮助,不仅因为这些 LLVM 组件可能没有安装,而且由于搜索路径和打包系统的变幻莫测,您的llc二进制clang文件可能与同一版本的 LLVM 不对应。

但是,为了争论,假设您自己编译了 LLVM 和 Clang,或者您很乐意接受您的 LLVM 二进制文件足够好:

  • llc --version将列出它支持的所有架构。默认情况下,它被编译为支持所有架构。您可能认为像 ARM 这样的单一架构可能有多个 LLVM 架构,例如常规 ARM、Thumb 和 AArch64。这主要是为了实现方便,因为不同的执行模式具有非常不同的指令编码和语义。
  • 对于列出的每个架构,llc -march=ARCH -mattr=help将列出“可用的 CPU”和“可用的功能”。CPU 通常只是设置默认功能集合的便捷方式。

但现在是坏消息。在 Clang 或 LLVM 中没有可以转储的方便的三元组表,因为特定于体系结构的后端可以选择将三元组字符串解析为llvm::Triple对象(在include/llvm/ADT/Triple.h中定义)。换句话说,转储所有可用的三元组需要解决停机问题。例如,查看llvm::ARM_MC::ParseARMTriple(...)解析字符串的特殊情况"generic"

不过,归根结底,“三元组”主要是一种向后兼容功能,使 Clang 成为 GCC 的直接替代品,因此您通常不需要过多关注它,除非您将 Clang 或 LLVM 移植到新平台或建筑。相反,您可能会发现llc -march=arm -mattr=help大量不同 ARM 功能的输出和令人难以置信的结果在您的调查中更有用。

祝你的研究好运!

于 2016-01-28T12:46:05.417 回答
38

我正在使用 Clang 3.3,我认为获得答案的最佳方法是阅读源代码。在 llvm/ADT/Triple.h ( http://llvm.org/doxygen/Triple_8h_source.html ):

  enum ArchType {
    UnknownArch,

    arm,     // ARM: arm, armv.*, xscale
    aarch64, // AArch64: aarch64
    hexagon, // Hexagon: hexagon
    mips,    // MIPS: mips, mipsallegrex
    mipsel,  // MIPSEL: mipsel, mipsallegrexel
    mips64,  // MIPS64: mips64
    mips64el,// MIPS64EL: mips64el
    msp430,  // MSP430: msp430
    ppc,     // PPC: powerpc
    ppc64,   // PPC64: powerpc64, ppu
    r600,    // R600: AMD GPUs HD2XXX - HD6XXX
    sparc,   // Sparc: sparc
    sparcv9, // Sparcv9: Sparcv9
    systemz, // SystemZ: s390x
    tce,     // TCE (http://tce.cs.tut.fi/): tce
    thumb,   // Thumb: thumb, thumbv.*
    x86,     // X86: i[3-9]86
    x86_64,  // X86-64: amd64, x86_64
    xcore,   // XCore: xcore
    mblaze,  // MBlaze: mblaze
    nvptx,   // NVPTX: 32-bit
    nvptx64, // NVPTX: 64-bit
    le32,    // le32: generic little-endian 32-bit CPU (PNaCl / Emscripten)
    amdil,   // amdil: amd IL
    spir,    // SPIR: standard portable IR for OpenCL 32-bit version
    spir64   // SPIR: standard portable IR for OpenCL 64-bit version
  };

在 clang/lib/Driver/ToolChains.cpp 中,有关于 arm 的东西。

static const char *GetArmArchForMArch(StringRef Value) {
  return llvm::StringSwitch<const char*>(Value)
    .Case("armv6k", "armv6")
    .Case("armv6m", "armv6m")
    .Case("armv5tej", "armv5")
    .Case("xscale", "xscale")
    .Case("armv4t", "armv4t")
    .Case("armv7", "armv7")
    .Cases("armv7a", "armv7-a", "armv7")
    .Cases("armv7r", "armv7-r", "armv7")
    .Cases("armv7em", "armv7e-m", "armv7em")
    .Cases("armv7f", "armv7-f", "armv7f")
    .Cases("armv7k", "armv7-k", "armv7k")
    .Cases("armv7m", "armv7-m", "armv7m")
    .Cases("armv7s", "armv7-s", "armv7s")
    .Default(0);
}

static const char *GetArmArchForMCpu(StringRef Value) {
  return llvm::StringSwitch<const char *>(Value)
    .Cases("arm9e", "arm946e-s", "arm966e-s", "arm968e-s", "arm926ej-s","armv5")
    .Cases("arm10e", "arm10tdmi", "armv5")
    .Cases("arm1020t", "arm1020e", "arm1022e", "arm1026ej-s", "armv5")
    .Case("xscale", "xscale")
    .Cases("arm1136j-s", "arm1136jf-s", "arm1176jz-s", "arm1176jzf-s", "armv6")
    .Case("cortex-m0", "armv6m")
    .Cases("cortex-a8", "cortex-r4", "cortex-a9", "cortex-a15", "armv7")
    .Case("cortex-a9-mp", "armv7f")
    .Case("cortex-m3", "armv7m")
    .Case("cortex-m4", "armv7em")
    .Case("swift", "armv7s")
    .Default(0);
}
于 2013-09-02T15:12:25.453 回答
23

你可以做的一个提示:如果你想找到一个特定的目标三元组,是在那个系统上安装 llvm然后做一个

$ llc --version | grep Default
  Default target: x86_64-apple-darwin16.1.0

或者:

$ llvm-config --host-target
x86_64-apple-darwin16.0.0
or
$ clang -v 2>&1 | grep Target
Target: x86_64-apple-darwin16.1.0

然后你知道在交叉编译时如何定位它。

显然那里有“很多”目标,这里有一个列表,请随意添加,社区 wiki 风格:

arm-none-eabi
armv7a-none-eabi
arm-linux-gnueabihf 
arm-none-linux-gnueabi
i386-pc-linux-gnu 
x86_64-apple-darwin10
i686-w64-windows-gnu # same as i686-w64-mingw32
x86_64-pc-linux-gnu # from ubuntu 64 bit
x86_64-unknown-windows-cygnus # cygwin 64-bit
x86_64-w64-windows-gnu # same as x86_64-w64-mingw32
i686-pc-windows-gnu # MSVC
x86_64-pc-windows-gnu # MSVC 64-BIT

无论如何,这是文档列表的内容(显然它是四倍[或五倍?],而不是这些天的三倍):

The triple has the general format <arch><sub>-<vendor>-<sys>-<abi>, where:
arch = x86, arm, thumb, mips, etc.
sub = for ex. on ARM: v5, v6m, v7a, v7m, etc.
vendor = pc, apple, nvidia, ibm, etc.
sys = none, linux, win32, darwin, cuda, etc.
abi = eabi, gnu, android, macho, elf, etc.

您甚至可以微调指定目标 cpu,尽管它使用基于三元组的目标 cpu 的合理默认值。

有时目标“解决”到同一件事,因此要查看目标实际被视为什么:

 $ clang -target x86_64-w64-mingw32 -v 2>&1 | grep Target
 Target: x86_64-w64-windows-gnu
于 2016-11-30T14:00:42.970 回答
16

从 Clang 11(主干)开始,可以使用新添加的-print-targets标志轻松打印支持的目标架构列表:

$ clang-11 -print-targets
  Registered Targets:
    aarch64    - AArch64 (little endian)
    aarch64_32 - AArch64 (little endian ILP32)
    aarch64_be - AArch64 (big endian)
    amdgcn     - AMD GCN GPUs
    arm        - ARM
    arm64      - ARM64 (little endian)
    arm64_32   - ARM64 (little endian ILP32)
    armeb      - ARM (big endian)
    avr        - Atmel AVR Microcontroller
    bpf        - BPF (host endian)
    bpfeb      - BPF (big endian)
    bpfel      - BPF (little endian)
    hexagon    - Hexagon
    lanai      - Lanai
    mips       - MIPS (32-bit big endian)
    mips64     - MIPS (64-bit big endian)
    mips64el   - MIPS (64-bit little endian)
    mipsel     - MIPS (32-bit little endian)
    msp430     - MSP430 [experimental]
    nvptx      - NVIDIA PTX 32-bit
    nvptx64    - NVIDIA PTX 64-bit
    ppc32      - PowerPC 32
    ppc64      - PowerPC 64
    ppc64le    - PowerPC 64 LE
    r600       - AMD GPUs HD2XXX-HD6XXX
    riscv32    - 32-bit RISC-V
    riscv64    - 64-bit RISC-V
    sparc      - Sparc
    sparcel    - Sparc LE
    sparcv9    - Sparc V9
    systemz    - SystemZ
    thumb      - Thumb
    thumbeb    - Thumb (big endian)
    wasm32     - WebAssembly 32-bit
    wasm64     - WebAssembly 64-bit
    x86        - 32-bit X86: Pentium-Pro and above
    x86-64     - 64-bit X86: EM64T and AMD64
    xcore      - XCore

参考:LLVM PRLLVM commitClang 11 文档

于 2020-06-30T05:04:38.050 回答
14
于 2016-08-24T05:00:41.203 回答
7

也试试

> llc -mattr=help

Available CPUs for this target:

  amdfam10      - Select the amdfam10 processor.
  athlon        - Select the athlon processor.
  athlon-4      - Select the athlon-4 processor.
  athlon-fx     - Select the athlon-fx processor.
  athlon-mp     - Select the athlon-mp processor.
  athlon-tbird  - Select the athlon-tbird processor.
  athlon-xp     - Select the athlon-xp processor.
  athlon64      - Select the athlon64 processor.
  athlon64-sse3 - Select the athlon64-sse3 processor.
  atom          - Select the atom processor.
  ...
Available features for this target:

  16bit-mode           - 16-bit mode (i8086).
  32bit-mode           - 32-bit mode (80386).
  3dnow                - Enable 3DNow! instructions.
  3dnowa               - Enable 3DNow! Athlon instructions.
  64bit                - Support 64-bit instructions.
  64bit-mode           - 64-bit mode (x86_64).
  adx                  - Support ADX instructions.
  ...
于 2014-01-28T15:43:03.563 回答
3

它不会列出所有的三元组,但是

llvm-as < /dev/null | llc -mcpu=help

至少会列出所有的 CPU。

于 2013-06-20T04:27:51.503 回答
3

clang -march=dont-know empty.c

错误:未知目标 CPU '不知道'

注意:有效的目标 CPU 值为:nocona、core2、penryn、bonnell、atom、silvermont、slm、goldmont、goldmont-plus、tremont、nehalem、corei7、westmere、sandybridge、corei7-avx、ivybridge、core-avx-i、 haswell、core-avx2、broadwell、skylake、skylake-avx512、skx、cascadelake、cooperlake、cannonlake、icelake-client、icelake-server、tigerlake、knl、knm、k8、athlon64、athlon-fx、opteron、k8-sse3、 athlon64-sse3、opteron-sse3、amdfam10、巴塞罗那、btver1、btver2、bdver1、bdver2、bdver3、bdver4、znver1、znver2、x86-64

于 2020-11-18T05:58:07.963 回答
2

如果您对从源代码构建 LLVM 或 Clang 支持哪些目标感兴趣(的值),请在源代码分发的文件夹中-DLLVM_TARGETS_TO_BUILD查找子目录列表。llvm/lib/Target从 9.0.1 开始,有:

AArch64
AMDGPU
ARC
ARM
AVR
BPF
Hexagon
Lanai
MSP430
Mips
NVPTX
PowerPC
RISCV
Sparc
SystemZ
WebAssembly
X86
于 2020-02-29T18:07:56.230 回答
0

只有第一个(CPU架构)需要准确,其他参数都经过智能复杂的处理,可以使用“clang++ ... --verbose ...”查看处理结果,例如:

Command Line Input      After triple processing
x86_64                  x86_64
x86_64-foo              x86_64-foo
x86_64-windows          x86_64-unknown-windows-msvc19.28.29335
x86_64-windows-bar      x86_64-unknown-windows-msvc19.28.29335
x86_64-foo-windows-bar  x86_64-foo-windows-msvc19.28.29335
x86_64-foo-bar-foobar   x86_64-foo-bar-foobar

通常除了第一个参数之外的参数只有在正确的情况下才会生效(经过三次处理可能会巧妙地使错误的正确正确),例如“windows”会影响代码:

/// Tests whether the OS is Windows.
bool isOSWindows() const {
    return getOS() == Triple::Win32;
}

此方法被 Clang/LLVM 中的其他代码用于影响编译结果,它仅在参数为“windows”时返回 true,如果为其他任何东西,例如“foo”,则返回 false。

于 2021-04-03T08:37:04.673 回答
0

对于那些最终来到这里查看他们特定的 x86 CPU 系列架构是否有 llvm/clang 优化目标的人(例如:zen3、zen1、skylake、penryn 等)

您可以查看下面的列表或运行以下命令:

$ llc -march=x86 -mattr=help
Available CPUs for this target:

  alderlake      - Select the alderlake processor.
  amdfam10       - Select the amdfam10 processor.
  athlon         - Select the athlon processor.
  athlon-4       - Select the athlon-4 processor.
  athlon-fx      - Select the athlon-fx processor.
  athlon-mp      - Select the athlon-mp processor.
  athlon-tbird   - Select the athlon-tbird processor.
  athlon-xp      - Select the athlon-xp processor.
  athlon64       - Select the athlon64 processor.
  athlon64-sse3  - Select the athlon64-sse3 processor.
  atom           - Select the atom processor.
  barcelona      - Select the barcelona processor.
  bdver1         - Select the bdver1 processor.
  bdver2         - Select the bdver2 processor.
  bdver3         - Select the bdver3 processor.
  bdver4         - Select the bdver4 processor.
  bonnell        - Select the bonnell processor.
  broadwell      - Select the broadwell processor.
  btver1         - Select the btver1 processor.
  btver2         - Select the btver2 processor.
  c3             - Select the c3 processor.
  c3-2           - Select the c3-2 processor.
  cannonlake     - Select the cannonlake processor.
  cascadelake    - Select the cascadelake processor.
  cooperlake     - Select the cooperlake processor.
  core-avx-i     - Select the core-avx-i processor.
  core-avx2      - Select the core-avx2 processor.
  core2          - Select the core2 processor.
  corei7         - Select the corei7 processor.
  corei7-avx     - Select the corei7-avx processor.
  generic        - Select the generic processor.
  geode          - Select the geode processor.
  goldmont       - Select the goldmont processor.
  goldmont-plus  - Select the goldmont-plus processor.
  haswell        - Select the haswell processor.
  i386           - Select the i386 processor.
  i486           - Select the i486 processor.
  i586           - Select the i586 processor.
  i686           - Select the i686 processor.
  icelake-client - Select the icelake-client processor.
  icelake-server - Select the icelake-server processor.
  ivybridge      - Select the ivybridge processor.
  k6             - Select the k6 processor.
  k6-2           - Select the k6-2 processor.
  k6-3           - Select the k6-3 processor.
  k8             - Select the k8 processor.
  k8-sse3        - Select the k8-sse3 processor.
  knl            - Select the knl processor.
  knm            - Select the knm processor.
  lakemont       - Select the lakemont processor.
  nehalem        - Select the nehalem processor.
  nocona         - Select the nocona processor.
  opteron        - Select the opteron processor.
  opteron-sse3   - Select the opteron-sse3 processor.
  penryn         - Select the penryn processor.
  pentium        - Select the pentium processor.
  pentium-m      - Select the pentium-m processor.
  pentium-mmx    - Select the pentium-mmx processor.
  pentium2       - Select the pentium2 processor.
  pentium3       - Select the pentium3 processor.
  pentium3m      - Select the pentium3m processor.
  pentium4       - Select the pentium4 processor.
  pentium4m      - Select the pentium4m processor.
  pentiumpro     - Select the pentiumpro processor.
  prescott       - Select the prescott processor.
  rocketlake     - Select the rocketlake processor.
  sandybridge    - Select the sandybridge processor.
  sapphirerapids - Select the sapphirerapids processor.
  silvermont     - Select the silvermont processor.
  skx            - Select the skx processor.
  skylake        - Select the skylake processor.
  skylake-avx512 - Select the skylake-avx512 processor.
  slm            - Select the slm processor.
  tigerlake      - Select the tigerlake processor.
  tremont        - Select the tremont processor.
  westmere       - Select the westmere processor.
  winchip-c6     - Select the winchip-c6 processor.
  winchip2       - Select the winchip2 processor.
  x86-64         - Select the x86-64 processor.
  x86-64-v2      - Select the x86-64-v2 processor.
  x86-64-v3      - Select the x86-64-v3 processor.
  x86-64-v4      - Select the x86-64-v4 processor.
  yonah          - Select the yonah processor.
  znver1         - Select the znver1 processor.
  znver2         - Select the znver2 processor.
  znver3         - Select the znver3 processor.

上面的列表是截至 llvm-13 的最新列表

要运行上述程序,您至少需要安装 llvm 并获得与上述相同的结果,您至少需要 llvm-13。

于 2021-10-23T05:17:39.193 回答