LLVM IR: How to call a function in another .ll file

我是研究僧i 提交于 2020-01-17 06:09:26

问题


I am writing LLVM IR code, can I call a function in another .ll file?

For example: In a.ll file, there is a function foo(); Can I use this function in b.ll, like just call foo? If so, how can I include a.ll

Thanks


回答1:


You need to add declaration of function foo in the ll file in which you are calling it, then as usual convert link ll files to generate executable

 llvm-link a.ll b.ll -o a.out

example a.ll

declare i32 @foo(i32)

define i32 @main() {
    start:
    %0 = call i32 @foo(i32 0)
    ret i32 %0
}

b.ll

define i32 @foo(i32) {
    start:
    ret i32 %0
}



回答2:


I tried the above procedure but the a.out file produced is not an executable. It initially gives a Permission denied error and after adding the appropriate permissions says:

-bash: ./a.out: cannot execute binary file

Taking the same two llvm files i.e a.ll and b.ll what works for me is:

llvm-link-8 -S a.ll b.ll > hello.ll
llc-8 -filetype=obj hello.ll
clang hello.o

The following 3 commands creates an executable which executes fine. The first command creates an LLVM bitcode file called hello.ll which links a.ll and b.ll. After that it is simply a process of creating an executable binary from an llvm bitcode file. which the next 2 steps do. (Note that I am using LLVM 8)



来源:https://stackoverflow.com/questions/38246502/llvm-ir-how-to-call-a-function-in-another-ll-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!