执行从 Rust/Python 源代码生成的 LLVM IR 代码

Execute LLVM IR code generated from Rust/Python source code

当我从 C++ 生成 LLVM IR 代码时,我可以使用控制台命令 clang++ -emit-llvm –S test.cpp 获取一个 test.ll 文件,这是我想要的 LLVM IR。

要获取可执行文件,请遵循以下步骤:

对于 C++,这工作得很好。

理论上,我编写 Rust 或 Python 代码时是否应该应用相同的步骤?

我使用我的 Rust 代码并通过键入 rustc test.rs --emit llvm-ir 获取 LLVM IR。这再次给我 test.ll 文件。

对于 Python,我使用 "Numba" 并通过键入 numba --dump-llvm test.py> test.ll 获取 LLVM IR,这也为我提供了 test.ll 文件。

从这些 .ll 文件生成可执行文件的步骤应该是相同的。

它们一直工作到创建本机可执行文件的最后一步:

Python错误

/tmp/test-9aa440.o: In function 'main':
test.bc:(.text+0x67): undefined reference to 'numba_gil_ensure'
test.bc:(.text+0x79): undefined reference to 'numba_unpickle'
test.bc:(.text+0x84): undefined reference to 'PyObject_Str'
test.bc:(.text+0x8f): undefined reference to 'PyString_AsString'
test.bc:(.text+0xa1): undefined reference to 'PySys_WriteStdout'
test.bc:(.text+0xa9): undefined reference to 'Py_DecRef'
test.bc:(.text+0xb1): undefined reference to 'Py_DecRef'
test.bc:(.text+0xbd): undefined reference to 'PySys_WriteStdout'
test.bc:(.text+0xc5): undefined reference to 'numba_gil_release'
test.bc:(.text+0xff): undefined reference to 'numba_gil_ensure'
test.bc:(.text+0x10b): undefined reference to 'PySys_WriteStdout'
test.bc:(.text+0x113): undefined reference to 'numba_gil_release'
clang: error: linker command failed with exit code 1 (use -v to see     invocation)

生锈错误

/tmp/main-5e59bd.o: In function ‘main::sum::h514304ffa40dd7c3’:
main.bc:(.text+0xf): undefined reference to ‘core::panicking::panic::h2596388ccef1871c’
/tmp/main-5e59bd.o: In function ‘main’: main.bc:(.text+0x53): undefined reference to ‘std::rt::lang_start::h65647f6e36cffdae’
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我从这里得到的是 clang 不理解 LLVM IR 文件的 Rust/Python 特定部分(例如 Python 中的 "PyObject" 或 "panic" 来自Rust),用于生成 .bc、.s 和理论上的 .native 文件。

但是为什么那些甚至在 IR 中? LLVM IR 不应该是统一的并且那些部分应该被转换以便 LLVM 工具链可以与它们一起工作吗? 据我所知,LLVM 模块化应该通过使用 LLVM IR 来允许这些步骤。有没有其他我不知道的方法?

我能否以其他方式从这些语言生成 IR,从而提供 clang 理解的 "pure" LLVM IR,或者我是否仍可以从这些文件生成可执行文件,但以其他方式没有 clang?

我会说 Rust 代码:

你需要 link Rust 的标准库是这样的:

$(LLI) -load /Users/Stanislaw/.rustup/toolchains/stable-x86_64-apple-darwin/lib/libstd-f5a209a9.dylib ./target/debug/jitrust.bc

查看我使用的 Makefile 的完整示例 here

P.S。我假设 Python 也是如此。您还必须提供包含此 "unreferenced" 内容的库。