Maglev (mid-tier)
Maglev is V8's mid-tier optimizing compiler, added to fill a painful gap: between Sparkplug (fast to compile, no optimization) and TurboFan (slow to compile, maximal optimization) there was nothing. Functions that were hot but not white-hot either paid TurboFan's full compile cost too early or stayed un-optimized too long. Maglev is the compromise: fast compilation, good-but-not-maximal code.
::: info Ubiquitous language SSA (static single assignment): an IR form where every value is assigned exactly once, which makes data-flow analysis simple. CFG (control-flow graph): basic blocks connected by edges. Maglev uses an SSA CFG; TurboFan uses a looser "Sea of Nodes". :::
A simpler IR than TurboFan
Maglev builds a straightforward control-flow graph of basic blocks, each holding SSA-form nodes — much closer to the bytecode's structure than TurboFan's free-floating graph:
class Graph final : public ZoneObject {
public:
BasicBlock* operator[](int i) { return blocks_[i]; }
int num_blocks() const { return static_cast<int>(blocks_.size()); }
ZoneVector<BasicBlock*>& blocks() { return blocks_; }
// separate constant pools per representation:
ZoneVector<Value*> smi_constants_;
ZoneVector<Value*> int32_constants_;
// …
};
— src/maglev/maglev-graph.h#L46-L150
The whole graph is Zone-allocated and freed in one shot when compilation ends. Compilation can run off the main thread:
class MaglevCompiler : public AllStatic {
public:
static bool Compile(LocalIsolate*, MaglevCompilationInfo*);
static std::pair<MaybeHandle<Code>, BailoutReason> GenerateCode(
Isolate*, MaglevCompilationInfo*);
};
— src/maglev/maglev-compiler.h#L24-L40
What it does — and deliberately doesn't
Maglev uses the FeedbackVector to specialize, like TurboFan, but keeps its pass list short. It does things like:
Type specialization from feedback — emit Smi/double fast paths where the ICs say the types are stable.
Light inlining and constant folding.
Untagging of values that are known integers/doubles within a region.
But it intentionally skips TurboFan's heaviest analyses (the full Sea-of-Nodes global value numbering, escape analysis, deep loop optimization, aggressive scheduling). The point is to get most of the benefit at a fraction of the compile cost. A function that keeps getting hotter and stays type-stable graduates to TurboFan.
::: tip The mid-tier as a strategy, not just a compiler Maglev embodies a general performance idea worth stealing: when two stages are far apart on a cost/quality curve, a middle stage can capture a large share of the benefit cheaply. The win is not just Maglev's code quality — it is that V8 can afford to optimize far more functions than if TurboFan were the only optimizer. :::
Speculation and deopt
Maglev speculates (it bets on observed types) and therefore participates in deoptimization: its guards bail out to the interpreter when an assumption fails. Its speculation is more conservative than TurboFan's, which — together with its cheaper compiles — makes it a good "default optimized tier" for code that is hot but whose extreme hotness has not yet been proven.
Where it sits
Sparkplug ─(hot)─► Maglev ─(hotter & type-stable)─► TurboFan
│ (assumption violated)
▼ Deoptimization → interpreter
Maglev also has its own OSR path (invocation_count_for_maglev_osr, default
so a hot loop can enter Maglev code mid-flight — see OSR.
See also
TurboFan — the top tier Maglev hands off to.
Feedback & tiering decisions — how Maglev is selected.
Deoptimization — what happens when Maglev's bets fail.