Optimizing compilers in V8 (Maglev and TurboFan/Turboshaft) achieve high performance by speculating based on historical execution feedback collected by Ignition and Sparkplug. For example, if a + operator has only ever seen Smi (Small Integer) inputs, the compiler emits fast machine instructions for 32-bit integer addition with overflow checking rather than full ECMAScript type-coercion logic.
Whenever an assumption is violated at runtime -- for example, a function suddenly receives a String where a Smi was expected, an integer addition overflows, or a global prototype/field constancy assumption (DependentCode) is invalidated while a function is waiting on the call stack -- V8 cannot continue executing the optimized machine code. Instead, V8 performs Deoptimization:
DeoptimizationData and DeoptimizationFrameTranslation) to decode registers and stack slots.InterpreterEntryTrampoline (Builtin::kNotifyDeoptimized), resuming execution in Ignition (or OSR) as if the optimized code had never existed.kEager vs kLazy (src/common/globals.h)V8 defines two deoptimization kinds in enum class DeoptimizeKind : uint8_t (src/common/globals.h):
DeoptimizeKind::kEager (Synchronous Self-Check Failure)Triggered synchronously by a guard check inside the currently executing optimized frame itself.
CheckMaps), Smi check failure (CheckSmi), integer overflow (CheckedInt32Add), bounds check failure (CheckBounds), or hole check failure (CheckNotHole).InstructionStream, which pushes the deoptimization exit ID and jumps to Builtin::kDeoptimizationEntry_Eager.INTERPRETED_FRAME_WITHOUT_RETURN), so Ignition re-executes the operation using its generic slow path.DeoptimizeKind::kLazy (Asynchronous On-Stack Invalidation)Triggered asynchronously while an optimized activation is suspended on the call stack waiting for a callee (a JS call, runtime call, builtin call, or fast C API call) to return.
f() is paused waiting for g() to return, g() modifies a prototype, overwrites a PropertyConstness::kConst field, transitions a ContextCell, or deprecates a Map that f() depended on via DependentCode.f().MarkForDeoptimization & Return Address Patching):f() is not currently executing on the CPU, V8 cannot rewrite f()'s stack frame immediately.f()‘s Code object marked_for_deoptimization() and walks the stack (or uses the code’s deopt table / safepoint return PCs) to patch return flow so that when g() returns into f(), execution is redirected to Builtin::kDeoptimizationEntry_Lazy at the call site's lazy deopt point.kLazyAfterFastCall kind for V8 Fast API calls; in modern V8, fast C calls use standard kLazy deoptimization transitions when returning from the C++ callback if the caller frame was marked for deoptimization during the call.)g() has already finished executing and produced a return value (stored in the return register kReturnRegister0), the reconstructed unoptimized frame for f() must resume after the call bytecode and place g()'s return value into the target bytecode register or accumulator (INTERPRETED_FRAME_WITH_RETURN).src/objects/deoptimization-data.h)To protect the V8 Sandbox against attacker-controlled heap corruption, all deoptimization metadata lives in Trusted Space (ProtectedFixedArray and TrustedByteArray) attached to Code::deoptimization_data().
Code (TrustedSpace) | +-- deoptimization_data() --> DeoptimizationData (ProtectedFixedArray) |-- [0] FrameTranslation (Tagged<DeoptimizationFrameTranslation> : TrustedByteArray) |-- [1] InlinedFunctionCount (Smi) |-- [2] ProtectedLiteralArray (Tagged<ProtectedDeoptimizationLiteralArray> : ProtectedFixedArray) |-- [3] LiteralArray (Tagged<DeoptimizationLiteralArray> : TrustedWeakFixedArray) |-- [4] OsrBytecodeOffset (Smi) |-- [5] OsrPcOffset (Smi) |-- [6] OptimizationId (Smi) |-- [7] WrappedSharedFunctionInfo (SharedFunctionInfoWrapperOrSmi) |-- [8] InliningPositions (Tagged<TrustedPodArray<InliningPosition>>) |-- [9] DeoptExitStart (Smi) |-- [10] EagerDeoptCount (Smi) |-- [11] LazyDeoptCount (Smi) +-- [12..] Deopt Entries (Tuples of: BytecodeOffsetRaw, TranslationIndex, Pc [, NodeId])
LiteralArray vs ProtectedLiteralArray)During frame reconstruction, translation opcodes frequently reference compile-time constants (“deoptimization literals”) by index:
LiteralArray (DeoptimizationLiteralArray : TrustedWeakFixedArray):SharedFunctionInfo, Map, HeapNumber, String, etc.), held strongly or weakly according to InstructionStream::IsWeakObjectInOptimizedCode.ProtectedLiteralArray (ProtectedDeoptimizationLiteralArray : ProtectedFixedArray):BytecodeArray (and InterpreterData / Code) for the outer function and all inlined functions (TranslationOpcode::INTERPRETED_FRAME_* operand bytecode_array_literal).BytecodeArray pointers in ProtectedLiteralArray guarantees that a sandbox attacker cannot corrupt a deoptimization literal to make the deoptimizer construct an InterpretedFrame pointing to a forged BytecodeArray.DeoptimizationFrameTranslation & TranslationOpcode)Each deoptimization entry in DeoptimizationData contains a TranslationIndex pointing into the compressed byte stream of DeoptimizationFrameTranslation (src/deoptimizer/frame-translation-builder.cc, src/deoptimizer/translation-opcode.h).
A translation begins with a BEGIN opcode (BEGIN_WITHOUT_FEEDBACK or BEGIN_WITH_FEEDBACK, encoding distance_to_previous_translation, frame_count, and jsframe_count), followed by a sequence of Frame Opcodes (ordered outermost caller first, innermost inlined callee last) and Value Opcodes for each value in that frame.
[!TIP] Delta Compression (
MATCH_PREVIOUS_TRANSLATION): Consecutive deopt points in a function usually have nearly identical frame and register layouts.DeoptimizationFrameTranslationBuildercompresses the byte stream by emittingMATCH_PREVIOUS_TRANSLATION(repeat_count), instructingDeoptTranslationIteratorto replayrepeat_countopcodes from the precedingBEGINentry!
src/deoptimizer/translation-opcode.h)TranslationOpcode | Operands | Purpose |
|---|---|---|
INTERPRETED_FRAME_WITH_RETURN | 6: bytecode_offset, shared_info_id, bytecode_array_id, height, return_value_offset, return_value_count | Reconstructs an unoptimized InterpretedFrame for a lazy deopt (or inlined caller awaiting a return value), patching return_value_count return values at return_value_offset in the frame's register file. |
INTERPRETED_FRAME_WITHOUT_RETURN | 4: bytecode_offset, shared_info_id, bytecode_array_id, height | Reconstructs an InterpretedFrame where no callee return value is patched in (return_value_offset = 0, return_value_count = 0), such as an eager deopt. |
CONSTRUCT_CREATE_STUB_FRAME / CONSTRUCT_INVOKE_STUB_FRAME | 2 / 1 | Reconstructs a ConstructFrame around an inlined new Foo() constructor call (either before or after the constructor invocation). |
BUILTIN_CONTINUATION_FRAME / JAVASCRIPT_BUILTIN_CONTINUATION_FRAME / ..._WITH_CATCH | 3 | Reconstructs a continuation frame when an inlined C++ or Torque builtin deoptimizes mid-execution (for example, Array.prototype.map callback deoptimizing). |
INLINED_EXTRA_ARGUMENTS | 3 | Reconstructs under-applied or over-applied arguments for an inlined call so arguments / rest parameters inside the callee see the exact argument list. |
For each frame, the translation specifies how to materialize the closure, receiver, parameters, context, bytecode registers, and accumulator (src/deoptimizer/translated-state.h):
REGISTER, INT32_REGISTER, INT64_REGISTER, UINT32_REGISTER, BOOL_REGISTER, FLOAT_REGISTER, DOUBLE_REGISTER, HOLEY_DOUBLE_REGISTER, SIMD128_REGISTER, and their *_STACK_SLOT counterparts (TAGGED_STACK_SLOT, INT32_STACK_SLOT, etc.).LITERAL(literal_id): Reads a heap object from DeoptimizationLiteralArray.DOUBLE_LITERAL(low, high) vs HOLEY_DOUBLE_LITERAL(low, high): Encodes raw 64-bit Float64 vs HoleyFloat64 constants directly in the translation stream without laundering hole NaNs through LITERAL(the_hole_value). For tagged slots (GetRawValue() / GetValue()), DOUBLE_LITERAL canonicalizes all NaNs (including kHoleNanInt64) to a quiet NaN HeapNumber, whereas HOLEY_DOUBLE_LITERAL converts is_hole_nan() to undefined_value; for MaterializeFixedDoubleArray, GetDoubleValue() writes the raw Float64 bits via FixedDoubleArray::set_raw (see Holes and Sentinels).CAPTURED_OBJECT(length): Describes a scalar-replaced object that was never allocated on the heap in optimized code! Followed by length field translations (the first being its Map).DUPLICATED_OBJECT(object_id): References a previously declared CAPTURED_OBJECT within the same translation (handling cyclic or shared references among virtual objects).ARGUMENTS_ELEMENTS(type) / ARGUMENTS_LENGTH: Materializes an unallocated arguments backing store.REST_LENGTH: Materializes rest parameter length.OPTIMIZED_OUT: Places ReadOnlyRoots::optimized_out() into dead stack/register slots.UPDATE_FEEDBACK(vector_literal, slot): Instructs the deoptimizer to update a FeedbackVector slot (e.g. SpeculationMode::kDisallowSpeculation) so the compiler does not repeatedly make the same failed speculation on re-optimization!src/deoptimizer/deoptimizer.cc)sequenceDiagram participant Opt as Optimized Code (Maglev/TurboFan) participant Stub as Builtin::kDeoptimizationEntry participant Cpp as Deoptimizer (C++ no-GC) participant Notify as Builtin::kNotifyDeoptimized participant Ign as Ignition (InterpreterEntryTrampoline) Opt->>Stub: Jump to Deopt Exit (Eager) or Return to Patched PC (Lazy) Stub->>Stub: Spill all general & FP registers into FrameDescription (input_) Stub->>Cpp: Deoptimizer::New(raw_function, kind, deopt_exit_index, fp_to_sp) Cpp->>Cpp: 1. TranslatedState::Init() (decode translation & capture register/stack values) Cpp->>Cpp: 2. DoComputeOutputFrames() (build output_ FrameDescriptions) Cpp-->>Stub: Return to assembly stub Stub->>Stub: Pop input_ frame, copy output_ FrameDescriptions onto physical stack Stub->>Notify: Jump to Builtin::kNotifyDeoptimized Notify->>Cpp: Runtime_NotifyDeoptimized (GC allowed!) Cpp->>Cpp: 3. Deoptimizer::MaterializeHeapObjects() (box numbers, allocate CAPTURED_OBJECTs) Notify->>Ign: 4. Resume in InterpreterEntryTrampoline (or Builtin Continuation)
A crucial design constraint of V8's Deoptimizer is that Garbage Collection is strictly forbidden (DisallowGarbageCollection) while Deoptimizer::DoComputeOutputFrames() is rewriting the physical stack.
What if the deoptimized frame contains unboxed int32/float64 values that need HeapNumber boxes, or escape-analyzed CAPTURED_OBJECTs that must be allocated on the heap?
DoComputeOutputFrames, No-GC):TranslatedState::Init reads raw values out of input_ (FrameDescription) and records all required heap allocations inside TranslatedState (TranslatedFrame / TranslatedValue entries on the C++ heap).HeapNumber or a CAPTURED_OBJECT), DoComputeOutputFrames temporarily writes a safe sentinel (ReadOnlyRoots::arguments_marker()) into the output FrameDescription stack slot!Builtin::kNotifyDeoptimized -> Runtime_NotifyDeoptimized.Runtime_NotifyDeoptimized, GC Allowed):InterpretedFrames (with arguments_marker in unmaterialized slots), GC is safe!Deoptimizer::MaterializeHeapObjects() allocates all HeapNumbers, BigInts, arguments objects, and CAPTURED_OBJECT graphs on the V8 heap (carefully handling cycles and nested Representation::Double() fields) and patches their addresses directly into the reconstructed stack frame slots (output_slot_address_).src/deoptimizer/deoptimizer.h & src/deoptimizer/deoptimizer.cc: Core Deoptimizer orchestration and DoComputeOutputFrames.src/deoptimizer/translated-state.h & src/deoptimizer/translated-state.cc: TranslatedState, TranslatedFrame, TranslatedValue, and escape-analyzed object materialization.src/deoptimizer/translation-opcode.h: Complete list of TranslationOpcode frame and value opcodes.src/deoptimizer/frame-translation-builder.h & .cc: Compressing and emitting DeoptimizationFrameTranslation byte streams.src/objects/deoptimization-data.h & -inl.h: DeoptimizationData, DeoptimizationLiteralArray, and ProtectedDeoptimizationLiteralArray.