BEAM There, Done That with Lukas Backström on Building the BEAM JIT

BEAM There, Done That just released an episode with Lukas Backström covering the BEAM JIT in technical depth. Worth discussing here.

The engineering story is interesting because of how long it took to get right. The BEAM’s threaded code interpreter was already unusually fast - direct threaded dispatch means no central loop overhead - so early JIT experiments were competing for performance in a system that had already captured a lot of the easy gains. Tracing JITs, which the team tried first in collaboration with SICS/RISE, worked on microbenchmarks and failed on real workloads because real Erlang code is too branchy for effective trace specialization.

The key decisions that produced the design that shipped:

Switching from LLVM to AsmJIT gave fast enough code generation to compile everything at load time without significant startup penalty, at the cost of LLVM’s optimization passes. The team judged that tradeoff correctly - LLVM’s startup cost would have been measured in minutes for large systems.

Switching from tracing to method-based compilation, covering entire modules rather than hot paths, avoided the worst cases of the branchy execution problem.

The template design - pre-compiled assembly per BEAM instruction, copied and specialized at load time - is what actually shipped in OTP 24.

Five years on, the type-guided optimization work flowing from the compiler into the JIT is where the active development is. The conversation covers how the Erlang compiler’s type analysis embeds metadata into compiled BEAM files, and how the JIT reads that metadata to eliminate guard checks when types are already proven.

Lucas also mentions keeping an eye on RISC-V but says the architecture isn’t dominant enough yet to warrant a backend.

4 Likes

I always wondered if it would be possible to design a Tier 2 JIT as an Erlang Application/Library. Like the Graal JIT in the GraalVM (a JVM) is written in Java.

I suppose it could fetch the BEAM Code through special BIFs, transformer it back into SSA form and do more global analysises and optimizations and then feed the result back into BeamAsm. No runtime profiling.

1 Like