执行从Rust/Python源代码生成的LLVM IR代码 [英] Execute LLVM IR code generated from Rust/Python source code

查看:634
本文介绍了执行从Rust/Python源代码生成的LLVM IR代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I generate LLVM IR Code from C++, I can use the console command clang++ -emit-llvm –S test.cpp to get a test.ll file which is the LLVM IR I want.

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

To get an executable these are the steps to follow:

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

  • llvm-as test.ll -> gives me the test.bc file.

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

llc test.bc --o test.s -> gives me the test.s file.

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

clang++ test.s -o test.native -> gives me a native file that i can execute.

对于C ++,这很好用.

For C++ this works just fine.

从理论上讲,当我编写Rust或Python代码时,应该采用相同的步骤吗?

In theory, should the same steps apply when I write Rust or Python Code?

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

I take my Rust code and get the LLVM IR by typing rustc test.rs --emit llvm-ir. This gives me the test.ll file again.

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

For Python, I use "Numba" and get the LLVM IR by typing numba --dump-llvm test.py> test.llwhich also gives me the test.ll file.

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

The steps to generate an executable from those .ll files should be the same.

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

They work up until the last step that creates the native executable:

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)

我从中得到的是,lang无法理解LLVM IR文件的Rust/Python特定部分(例如,Python中的"PyObject"或Rust的"panic"),这些部分用于生成.bc,. ,理论上是.native文件.

What I get from this is that clang doesn't understand the Rust/Python specific parts of the LLVM IR file (e.g. "PyObject" in Python or "panic" from Rust) that were used to generate the .bc, .s and in theory the .native files.

但是,为什么那些人甚至首先在IR中? LLVM IR是否应该统一并且对那些零件进行转换,以便LLVM工具链可以与之配合使用? 据我所知,LLVM的模块化应该通过使用LLVM IR允许这些步骤.也许还有另一种我不知道的方法吗?

But why are those even in the IR in the first place? Shouldn't the LLVM IR be uniform and those parts be transformed so the LLVM toolchain can work with them? As far as I know LLVMs modularity should allow those steps by using LLVM IR. Is there maybe another way to do this i don't know about?

我可以通过其他方式从那些语言生成IR,从而获得clang可以理解的纯" LLVM IR,还是可以从那些文件中生成可执行文件,但是以其他方式生成不带有clang的可执行文件?

Can I generate the IRs from those languages in some other way that gives "pure" LLVM IR that clang understands, or could I still generate executables from those files, but in some other way without clang?

推荐答案

我可以说说Rust代码:

I can speak of Rust code:

您需要链接Rust的std库,如下所示:

You need to link Rust's std library something like this:

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

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

See the full example of Makefile I use here.

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

P.S. I would assume that the same goes about Python. You have to also supply libraries which contain this "unreferenced" stuff.

这篇关于执行从Rust/Python源代码生成的LLVM IR代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆