Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate runnable LLVM IR from Julia script?

I am wondering how to convert Julia code into runnable LLVM IR(the *.ll file).

There is a command named code_llvm which can compile a Julia function into LLVM IR. But its result contains something like %jl_value_t* which seems to be an (hidden?) object type, and it doesn't look like pure LLVM IR.

Is there a way to generate runnable LLVM IR from Julia, so that I can run it with lli xx.ll (or do something else)?

like image 348
罗泽轩 Avatar asked Nov 09 '22 17:11

罗泽轩


1 Answers

The code_llvm function just shows the Function by default, but you can also have it print out a complete module:

open("file.ll", "w") do io
    code_llvm(io, +, (Int, Int); raw=true, dump_module=true, optimize=true)
end

This output (file.ll) is now valid to use with other llvm tools, such as llc and opt. However, since it's just the code for this one function, and assumes the existence of all the other code and data, it's not necessarily going to work with lli, so buyer beware.

If you want a complete system, you might be interested in the --output-bc flag to Julia, which will dump a complete object file in LLVM format. This is used extensively internally to build and bootstrap Julia. It's also wrapped into a utility tool at https://github.com/JuliaLang/PackageCompiler.jl to automate some of these steps.

like image 159
user1712368 Avatar answered Dec 12 '22 00:12

user1712368