执行从Rust/Python源代码生成的LLVM IR代码
当我从C++生成LLVM IR代码时,我可以使用控制台命令clang++ -emit-llvm –S test.cpp
来获取我想要的LLVM IR的test.ll文件。执行从Rust/Python源代码生成的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代码,通过输入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特定部分(例如“PyO bject“,或者Rust的”恐慌“),用于生成.bc,.s和理论上的.native文件。
但是,为什么那些即使在IR的第一位呢? LLVM IR不应该统一,而且这些部分要进行转换,以便LLVM工具链可以与它们一起工作? 据我所知LLVMs模块化应该允许通过使用LLVM IR这些步骤。有没有另外一种方法可以做到这一点我不知道?
我能否以某种其他方式从那些语言生成IR,这种语言给出了clang能够理解的“纯粹”LLVM IR,或者我仍然可以从这些文件生成可执行文件,但以其他方式没有叮当声?
我能说的锈代码:
您需要关联锈病的性病库是这样的:
$(LLI) -load /Users/Stanislaw/.rustup/toolchains/stable-x86_64-apple-darwin/lib/libstd-f5a209a9.dylib ./target/debug/jitrust.bc
见Makefile文件的完整的例子我用here。
P.S.我会假设Python也是如此。你还必须提供包含这个“未引用”内容的库。
谢谢,我使用了'clang PATH/TO/RUSTLIB test.s -o test.native',并可以在之后生成并运行它。 Python也应该像这样工作。当我找到解决方案时,我会更新。 – Weber
我试图让它与Python以类似的方式工作,但我不能这样做。当我用numba生成IR时,仅连接python lib不足以用于铿锵声。尽管链接numba是不可能的,因为没有.so文件。我可以找到clang缺失的numba函数,但它们只能在本地使用。另外,作为内部python脚本的numba需要在python环境中调用。我没有看到用铿锵编译python IR的方法,通过使用numba生成IR。这可能是可能的,通过生成没有numba的IR,但我不认为它可能与numba。 – Weber
Possiby链接到:http://*.com/questions/15169015/call-python-code-from-llvm-jit?rq=1 – MKesper
如果你检查.ll源,你应该找到对外部定义函数的引用在Python和Rust的情况下。你需要把它们连接起来。对于从C++源代码生成的IR来说,情况也是如此,但是'clang ++'默认包含C++标准库。 – SPAT