【发布时间】:2017-07-26 08:25:54
【问题描述】:
当我从 C++ 生成 LLVM IR 代码时,我可以使用控制台命令 clang++ -emit-llvm –S test.cpp 来获取我想要的 LLVM IR 的 test.ll 文件。
要获得可执行文件,请遵循以下步骤:
llvm-as test.ll-> 给了我 test.bc 文件。llc test.bc --o test.s-> 给了我 test.s 文件。clang++ test.s -o test.native-> 给了我一个可以执行的本机文件。
对于 C++,这工作得很好。
理论上,当我编写 Rust 或 Python 代码时应该应用相同的步骤吗?
我通过输入 rustc test.rs --emit llvm-ir 获取我的 Rust 代码并获取 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 不理解用于生成 .bc、. s 和理论上的 .native 文件。
但为什么一开始就在 IR 中呢? LLVM IR 不应该是统一的并且这些部分不应该被转换以便 LLVM 工具链可以使用它们吗? 据我所知,LLVM 的模块化应该允许使用 LLVM IR 执行这些步骤。有没有其他我不知道的方法?
我能否以其他方式从这些语言生成 IR,以提供 clang 理解的“纯”LLVM IR,或者我仍然可以从这些文件生成可执行文件,但以其他方式不使用 clang?
【问题讨论】:
-
如果您检查 .ll 源代码,您应该会在 Python 和 Rust 案例中找到对外部定义函数的引用。您需要将这些链接进去。从 C++ 源代码生成的 IR 可能也是如此,但
clang++默认包含 C++ 标准库。
标签: python rust clang llvm llvm-ir