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

2024-05-23 18:10:01 发布

您现在位置:Python中文网/ 问答频道 /正文

当我从C++生成LLVM IR代码时,我可以使用控制台命令^ {< CD1>}来获得测试.ll文件是我想要的LLVM-IR。在

要获得可执行文件,请执行以下步骤:

  • llvm-as test.ll->;给了我测试.bc文件。

  • llc test.bc --o test.s->;给我test.s文件。

  • clang++ test.s -o test.native->;给我一个可以执行的本机文件。

对于C++来说,这很好。在

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

我用我的Rust代码,通过输入rustc test.rs --emit llvm-ir得到LLVM-IR。这给了我测试.ll再次归档。在

对于Python,我使用“Numba”并通过键入numba --dump-llvm test.py> test.ll获得LLVM IR,这也给了我测试.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)

生锈错误

^{pr2}$

我从中得到的是,clang不理解llvmir文件中用于生成.bc、.s和理论上的.native文件的Rust/Python特定部分(例如Python中的“PyObject”或Rust中的“panic”)。在

但为什么这些东西会出现在红外波段呢?llvmir不应该是统一的,并且应该对这些部分进行转换,以便LLVM工具链能够与它们一起工作吗? 据我所知,LLVMs的模块化应该允许使用llvmir实现这些步骤。有没有别的方法我不知道?在

我可以用其他方式从这些语言生成IRs,以提供clang能够理解的“纯”llvmir,还是可以从这些文件生成可执行文件,但以其他方式不使用clang?在


Tags: 文件totexttest可执行文件ir步骤rust
1条回答
网友
1楼 · 发布于 2024-05-23 18:10:01

我可以说生锈代码:

你需要像这样链接Rust的std库:

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

请参阅我使用的Makefile的完整示例here。在

另外,我假设Python也是这样。您还必须提供包含这些“未引用”内容的库。在

相关问题 更多 >