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



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

要获得可执行文件,这些是要遵循的步骤:

  • llvm-as test.ll->给我test.bc文件。

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

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

对于C ,这可以正常工作。

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

我使用我的Rust Code并通过键入rustc test.rs --emit llvm-ir获取LLVM IR。这给了我test.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)

生锈错误

/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"或用于生成.bc的Rust的" Panic")。从理论上讲。本地文件。

但是,为什么那些甚至首先是IR?LLVM IR不应该是统一的,而这些部分是否可以转换,以便LLVM工具链可以与它们一起使用?据我所知,LLVMS模块化应使用LLVM IR允许这些步骤。我可能不知道另一种方法吗?

我可以以其他方式从这些语言中生成IRS,从而给出"纯" LLVM IR,或者我仍然可以从这些文件中生成可执行文件,但是以某种方式没有clang?

我可以说锈蚀代码:

您需要将Rust的性病库链接到类似的内容:

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

请参阅我在这里使用的makefile的完整示例。

P.S。我认为Python也是如此。您还必须提供包含此"未参考"内容的库。

最新更新