I don’t actually plan on doing this, just curious and trying to learn how various intermediate representation languages work. Similar to how the JVM and .NET compile higher level languages to an assembly-like intermediate language, which is then executed on a JIT compiler, could we do that with LLVM IR to make any LLVM-based language like Rust, Objective C or Swift “interpreted” on a level similar to the JVM? Maybe even interop them since now they’re running on the same interpreter like how Java, Kotlin and Scala can all work together; or C#, F# and Visual Basic. And since LLVM IR is cross platform, I can imagine this can be an easy way to have platform agnostic executables for languages where you’d normally have to compile separate binaries, and since LLVM doesn’t have a garbage collector, of you use a parent language that also doesn’t have a garbage collector, you could probably get more reliable latency than JVM or .NET, right?

Does such a runtime already exist? And does LLVM IR have a pure binary version that would actually make it similar to the bytecodes of other languages?

  • HMH
    link
    42 years ago

    You will want to checkout Julia, it pretty much does exactly what you described. For example you can do this:

    julia> f(a,b) = a + b  # defines a function that adds two things
    f (generic function with 1 method)
    
    julia> @code_llvm f(20,22)  # asks julia what llvm ir code is generated by evaluating f for the integers 20 and 22
    ;  @ REPL[1]:1 within `f`
    define i64 @julia_f_426(i64 signext %0, i64 signext %1) #0 {
    top:
    ; ┌ @ int.jl:87 within `+`
       %2 = add i64 %1, %0
    ; └
      ret i64 %2
    }
    
    julia> f(20,22)
    42
    

    Julia uses a GC though.

  • @LLVMcompile
    link
    22 years ago

    Keep in mind that the IR has optimization instructions as well (see “phi” directives)

  • @kevincox
    link
    22 years ago

    This exists as part of the LLVM project. LLVM was originally a virtual machine, the title used to stand for Low-Level Virtual Machine. It was only later that it gained the focus on ahead-of-time compiling.

    LLVM IR has a binary format, it is semantically equivalent to the text format.

    I’m not aware of any general purpose “execute this IR file” programs, but I am not aware of any reason you couldn’t do it. I suspect that the IR format isn’t optimal for this use case but it is probably decent enough.