Why WebKit Replaced LLVM with the B3 Backend in Safari

In 2016, Apple’s WebKit team made a major architectural change to JavaScriptCore, the engine that powers the Safari web browser, by replacing the LLVM compiler backend with their own custom-built Bare Bones Backend (B3). This article provides an overview of why this transition occurred, focusing on the core issues with LLVM in a browser environment—specifically slow compilation speeds, high memory overhead, and architectural mismatches—and how B3 solved these problems to make Safari faster and more efficient.

The Original Setup: FTL and LLVM

To run JavaScript quickly, Safari uses Just-In-Time (JIT) compilation, which translates JavaScript into machine code while the user is browsing. In 2014, WebKit introduced the "Fourth Tier LLVM" (FTL) JIT. The idea was to use LLVM—a massive, industry-standard compiler infrastructure used for languages like C, C++, and Swift—to generate highly optimized machine code for heavy JavaScript tasks.

While LLVM produced very fast machine code, the WebKit team quickly realized that a compiler designed for static languages was not ideal for the dynamic nature of the web.

1. Compilation Time (Latency) Was Too Slow

The primary reason for replacing LLVM was compilation speed. LLVM is designed primarily for Ahead-Of-Time (AOT) compilation. When a developer compiles a C++ application, they don't mind waiting several minutes for LLVM to analyze the code and produce the fastest possible binary.

In a web browser, however, compilation happens in real-time while the user waits for a webpage to load or interact. Every millisecond spent compiling JavaScript is a millisecond the browser is doing extra work. LLVM’s complex architecture made it too slow at generating code on the fly. B3 was designed to compile code roughly four times faster than LLVM, drastically reducing wait times.

2. High Memory Overhead

LLVM represents code internally using a rich, complex Intermediate Representation (IR). While this is great for deep code analysis, it consumes a significant amount of memory.

Because a browser might compile thousands of JavaScript functions across multiple open tabs, LLVM’s memory footprint became a major bottleneck, especially on memory-constrained devices like iPhones and iPads. B3 replaced this heavy system with a much simpler, lightweight C++ data structure that uses a fraction of the memory.

3. Lack of JIT-Specific Features

JavaScript is a dynamic language, meaning variables can change types on the fly. To optimize it, a JIT compiler makes educated guesses about the code. If a guess is wrong, the compiler must quickly bail out and return to a lower tier of execution—a process called an On-Stack Replacement (OSR) exit.

Implementing JIT-specific features like OSR exits and inline patching in LLVM was difficult and clumsy. Because LLVM was not built with dynamic languages in mind, the WebKit team had to use awkward workarounds that further slowed down compilation. B3 was built from the ground up with first-class support for these exact JIT techniques.

4. Redundant Optimization Passes

LLVM includes hundreds of optimization passes designed for C-like languages, such as complex loop vectorization, which rarely benefit JavaScript. Furthermore, JavaScriptCore already performed high-level optimizations before handing the code over to LLVM.

When LLVM received the code, it would waste time running analyses that JavaScriptCore had already done, or analyses that were completely irrelevant to JavaScript. With B3, the WebKit team included only the exact optimization passes necessary for their engine, cutting out the bloat.

The Result of Switching to B3

By replacing LLVM with the Bare Bones Backend, WebKit kept the "FTL" name (changing its meaning to "Faster Than Light") and achieved a massive performance boost. B3 generated machine code that ran just as fast as the code produced by LLVM, but it generated that code in a fraction of the time and with significantly less memory. This resulted in a much more responsive browsing experience and better battery life for Safari users.