docs/compiler/turboshaft/compiler-turboshaft.md
This document covers V8's next-generation optimizing compiler pipeline, Turboshaft, and its frontend for Maglev graphs, Turbolev.
Turboshaft is designed to replace the Sea-of-Nodes IR used in TurboFan. It introduces a more traditional Control Flow Graph (CFG) representation with explicit blocks and instructions.
Turboshaft includes many reducers for different optimization passes. Some examples include:
Turboshaft was created to address several pain points with TurboFan's Sea-of-Nodes IR:
Turboshaft's return to a CFG IR solves these issues, resulting in simpler code and significantly faster compilation times.
For a more detailed discussion on why V8 adopted CFG and the benefits it brings, see Why V8 Embraced CFG.
Turbolev (implemented in src/compiler/turboshaft/turbolev-graph-builder.cc) is a frontend for Turboshaft that constructs a Turboshaft graph directly from a Maglev graph.
Maglev is fast to compile and collects good type feedback, building a graph that is already somewhat optimized and has environment/frame state information attached. By starting from the Maglev graph instead of starting over from bytecode, Turboshaft can:
Translating from Maglev to Turboshaft is not always a 1-to-1 mapping because they have different IR constraints.
A prime example of non-trivial translation is how Turbolev handles Generators (yield/resume).
GeneratorAnalyzer in turbolev-graph-builder.cc detects these loop header bypasses in the Maglev graph. Turbolev then transforms the graph by re-routing these resume edges to the loop headers and inserting secondary switches inside the loop to dispatch to the correct resume point.The general pipeline from IR to machine code in Turboshaft is as follows:
InstructionSelector visits the operations and emits corresponding machine instructions into an InstructionSequence.InstructionSequence.Instruction Selection is the phase that bridges the high-level Turboshaft IR to the low-level machine instructions.
src/compiler/turboshaft/instruction-selection-phase.cc, Turboshaft initializes an InstructionSelector specifically for Turboshaft graphs (InstructionSelector::ForTurboshaft). This selector traverses the Turboshaft IR and generates an InstructionSequence, mapping Turboshaft operations to architecture-specific machine instructions (defined in shared files like instruction-selector-x64.cc).src/compiler/turboshaft/: Core Turboshaft implementation.src/compiler/turboshaft/turbolev-graph-builder.cc: The main implementation of the Maglev-to-Turboshaft translator.src/compiler/turboshaft/turbolev-frontend-pipeline.cc: The pipeline stages for the Turbolev frontend.