编写由 LLVM 后端编译的代码时,体系结构重要吗?

When writing code compiled by LLVM backend, does architecture matter?

我的问题其实比标题更笼统:

在编写最终将编译为 LLVM 中间代码,然后从那里编译为机器语言的代码时,体系结构在什么时候很重要?

  1. 假设我正在编写 Rust(它使用 LLVM 作为后端)。我是否能够自动将我的 Rust 代码编译到 LLVM 可以定位的每个架构(假设该机器上有一个 OS 可以 运行 它)?

  2. 或者是 Rust 标准库还没有制作"ARM compatible",所以即使 LLVM 以它为目标我也无法编译到 ARM?

  3. 如果我不使用任何标准库,我的整个程序就只是一个 returns 的程序怎么办?难道即使没有任何库,即使 LLVM 以 Rust(或你拥有的)为目标,Rust(或你拥有的)也无法编译成 ARM(或你拥有的)?

  4. 如果以上所有示例都可以正常编译,我需要做什么才能让我的代码在一种架构上中断而不是编译到特定架构?

同类型加分题:

  1. 假设标准库使用 OS 系统调用(确实如此)。进行系统调用时是否必须关心体系结构?或者 OS(例如 Linux)是否也抽象出架构?

谢谢。

TL;DR

根据我的理解,您可以编译 到 LLVM 支持的任何目标(此处可能仍有一些注意事项,前端使用 inline assembler or module level inline assembly),但是,您不能保证它实际上 会正确执行。前端负责跨作者支持的平台移植的工作。

另请注意,作为前端开发人员,您有责任提供 data layout and target triple

另见:

您的问题:

Let's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?

这取决于 Rust 前端的作者。

Or could it be that the Rust standard library hasn't been made "ARM compatible" yet, so I couldn't compile to ARM even if the LLVM targets it?

我很确定 LLVM 能够发出指令,但它在寻址方面可能不正确。

我自己没有使用过上面提到的内联汇编工具,但我假设如果它允许特定于平台的汇编,那么这也会破坏与平台无关的编译。

What if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?

这又取决于 Rust 前端发出的内容。甚至在为您的逻辑发出指令之前,它可能会发出一些样板设置逻辑。

我正在用 LLVM 编写我自己的语言,它在名为“main”的特殊函数的情况下执行此操作。我的目标是 C ABI,因此它将使用适当的 C 样式 main 包装此 main 并使用一组更严格的参数调用它。

If all the above examples compile just fine, what do I have to do to get my code to break on one architecture not compile to a certain architecture?

考虑 C/C++ 和 llvm FAQ 中提到的 Clang。 Clang 是 LLVM 的前端,可能是最流行的前端,编写 C/C++ 的用户负责#include-ing 适当的平台特定功能。

某些语言的设计可能更独立于平台,然后前端可以为您处理这些工作。

Let's say the standard library makes use of OS system calls (which is surely does). Do you have to care about architecture when making system calls? Or does the OS (Linux, for example) abstract away architecture as well?

我假设你在谈论前端以 C 标准库为目标的情况,在这种情况下 LLVM 有 standard C library intrinsics which could be used by the frontend. This is not the only way, however, as you can use the call instruction to invoke C functions directly if targeting the C ABI as in the Kaleidoscope example

最终,标准库可能是一个可移植性问题,必须由前端开发人员解决。