Why is llvm considered unsuitable for implementing a JIT?

LlvmJit

Llvm Problem Overview


Many dynamic languages implement (or want to implement) a JIT Compiler in order to speed up their execution times. Inevitably, someone from the peanut gallery asks why they don't use LLVM. The answer is often, "LLVM is unsuitable for building a JIT." (For Example, Armin Rigo's comment here.)

Why is LLVM Unsuitable for building a JIT?

Note: I know LLVM has its own JIT. If LLVM used to be unsuitable, but now is suitable, please say what changed. I'm not talking about running LLVM Bytecode on the LLVM JIT, I'm talking about using the LLVM libraries to implement a JIT for a dynamic language.

Llvm Solutions


Solution 1 - Llvm

> Why is LLVM Unsuitable for building a JIT?

I wrote HLVM, a high-level virtual machine with a rich static type system including value types, tail call elimination, generic printing, C FFI and POSIX threads with support for both static and JIT compilation. In particular, HLVM offers incredible performance for a high-level VM. I even implemented an ML-like interactive front-end with variant types and pattern matching using the JIT compiler, as seen in this computer algebra demonstration. All of my HLVM-related work combined totals just a few weeks work (and I am not a computer scientist, just a dabbler).

I think the results speak for themselves and demonstrate unequivocally that LLVM is perfectly suitable for JIT compilation.

Solution 2 - Llvm

There are some notes about LLVM in the Unladen Swallow post-mortem blog post: http://qinsb.blogspot.com/2011/03/unladen-swallow-retrospective.html .

> Unfortunately, LLVM in its current state is really designed as a static compiler optimizer and back end. LLVM code generation and optimization is good but expensive. The optimizations are all designed to work on IR generated by static C-like languages. Most of the important optimizations for optimizing Python require high-level knowledge of how the program executed on previous iterations, and LLVM didn't help us do that.

Solution 3 - Llvm

There is a presentation on using LLVM as a JIT backened where the address many of the concerns raised as to why its bad, most of its seems to boil down to people building a static compiler as a JIT instead of building an actual JIT.

Solution 4 - Llvm

It takes a long time to start up is the biggest complaint - however, this is not so much of an issue if you did what Java does and start up in interpreter mode, and use LLVM to compile the most used parts of the program.

Also while there are arguments like this scattered all over the internet, Mono has been using LLVM as a JIT compiler successfully for a while now (though it's worth noting that it defaults to their own faster but less efficient backend, and they also modified parts of LLVM).

For dynamic languages, LLVM might not be the right tool, just because it was designed for optimizing system programming languages like C and C++ which are strongly/statically typed and support very low level features. In general the optimizations performed on C don't really make dynamic languages fast, because you're just creating an efficient way of running a slow system. Modern dynamic language JITs do things like inlining functions that are only known at runtime, or optimizing based on what type a variable has most of the time, which LLVM is not designed for.

Solution 5 - Llvm

Update: as of 7/2014, LLVM has added a feature called "Patch Points", which are used to support Polymorphic Inline Caches in Safari's FTL JavaScript JIT. This covers exactly the use case complained about int Armin Rigo's comment in the original question.

Solution 6 - Llvm

For a more detailed rant about the LLVM IR see here: LLVM IR is a compiler IR.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSean McMillanView Question on Stackoverflow
Solution 1 - LlvmJ DView Answer on Stackoverflow
Solution 2 - LlvmMikhail KorobovView Answer on Stackoverflow
Solution 3 - LlvmNecrolisView Answer on Stackoverflow
Solution 4 - LlvmparkovskiView Answer on Stackoverflow
Solution 5 - LlvmSean McMillanView Answer on Stackoverflow
Solution 6 - LlvmSean McMillanView Answer on Stackoverflow