【问题标题】:Execute LLVM IR code generated from Rust/Python source code执行从 Rust/Python 源代码生成的 LLVM IR 代码
【发布时间】: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


【解决方案1】:

我可以说 Rust 代码:

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

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

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

附:我会假设 Python 也是如此。您还必须提供包含此“未引用”内容的库。

【讨论】:

  • 谢谢,我使用了clang PATH/TO/RUSTLIB test.s -o test.native,之后可以生成并运行它。 Python 也应该像这样工作。当我找到解决方案时,我会更新。
  • 我试图让它以类似的方式与 Python 一起工作,但我做不到。当我使用 numba 生成 IR 时,仅链接 python 库对于 clang 来说是不够的。链接 numba 是不可能的,因为没有 .so 文件。我可以找到 clang 缺少的 numba 函数,但它们仅在本地可用。还需要在 python 环境中调用 numba 作为内部 python 脚本。通过使用 numba 生成 IR,我没有看到用 clang 编译 python IR 的方法。通过在没有 numba 的情况下生成 IR 可能是可能的,但我认为使用 numba 是不可能的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多