Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 17 additions & 50 deletions Source/JavaScriptCore/dfg/DFGFixupPhase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5302,81 +5302,48 @@
unsigned index = 0;
bool shouldConvertToCallDOM = true;
m_graph.doToChildren(node, [&](Edge& edge) {
// Callee. Ignore this. DFGByteCodeParser already emit appropriate checks.
if (!index)
return;

if (index == 1) {
if (!index) {
// Callee. Ignore this. DFGByteCodeParser already emit appropriate checks.
} else if (index == 1) {
// DOM node case.
if (edge->shouldSpeculateNotCell())
shouldConvertToCallDOM = false;
} else {
switch (signature->arguments[index - 2]) {
case SpecString:
if (!edge->shouldSpeculateNotString())
if (edge->shouldSpeculateNotString())
shouldConvertToCallDOM = false;
break;
case SpecInt32Only:
if (!edge->shouldSpeculateNotInt32())
if (edge->shouldSpeculateNotInt32())
shouldConvertToCallDOM = false;
break;
case SpecBoolean:
if (!edge->shouldSpeculateNotBoolean())
shouldConvertToCallDOM = false;
break;
case SpecInt8Array: {
if (edge->shouldSpeculateInt8Array())
shouldConvertToCallDOM = false;
break;
}
case SpecInt16Array: {
if (edge->shouldSpeculateInt16Array())
shouldConvertToCallDOM = false;
break;
}
case SpecInt32Array: {
if (edge->shouldSpeculateInt32Array())
shouldConvertToCallDOM = false;
break;
}
case SpecUint8Array: {
if (edge->shouldSpeculateUint8Array())
if (edge->shouldSpeculateNotBoolean())
shouldConvertToCallDOM = false;
break;
}
case SpecUint8ClampedArray: {
if (edge->shouldSpeculateUint8ClampedArray())
shouldConvertToCallDOM = false;
break;
}
case SpecUint16Array: {
if (edge->shouldSpeculateUint16Array())
shouldConvertToCallDOM = false;
break;
}
case SpecUint32Array: {
if (edge->shouldSpeculateUint32Array())
shouldConvertToCallDOM = false;
break;
}
case SpecFloat32Array: {
if (edge->shouldSpeculateFloat32Array())
shouldConvertToCallDOM = false;
break;
}
case SpecInt8Array:
case SpecInt16Array:
case SpecInt32Array:
case SpecUint8Array:
case SpecUint8ClampedArray:
case SpecUint16Array:
case SpecUint32Array:
case SpecFloat32Array:
case SpecFloat64Array: {
if (edge->shouldSpeculateFloat64Array())
SpeculatedType expected = signature->arguments[index - 2];
if (edge->prediction() && !(edge->prediction() & expected))
shouldConvertToCallDOM = false;
break;
}
case SpecInt52Any:
case SpecInt32AsInt52:
case SpecNonInt32AsInt52:
case SpecAnyIntAsDouble: {
if (edge->shouldSpeculateInt52())
if (edge->prediction() && !(edge->prediction() & (SpecInt32Only | SpecInt52Any)))
shouldConvertToCallDOM = false;
break;
}

Check warning on line 5346 in Source/JavaScriptCore/dfg/DFGFixupPhase.cpp

View check run for this annotation

Claude / Claude Code Review

Int52 profitability mask omits SpecAnyIntAsDouble

The Int52 profitability mask at line 5343 tests `SpecInt32Only | SpecInt52Any`, but at Fixup time a large integer argument (e.g. `2**40`) is profiled as `SpecAnyIntAsDouble` (bit 41) — never `SpecInt52Any` (bits 39|40), which only appears on `NodeResultInt52` nodes *after* Fixup inserts `Int52Rep`. So a pure-large-int argument sets `shouldConvertToCallDOM = false` and skips the DOMJIT fast path even though `fixEdge<Int52RepUse>` handles `SpecAnyIntAsDouble` fine. Consider using `SpecIntAnyFormat
Comment on lines 5340 to 5346

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 The Int52 profitability mask at line 5343 tests SpecInt32Only | SpecInt52Any, but at Fixup time a large integer argument (e.g. 2**40) is profiled as SpecAnyIntAsDouble (bit 41) — never SpecInt52Any (bits 39|40), which only appears on NodeResultInt52 nodes after Fixup inserts Int52Rep. So a pure-large-int argument sets shouldConvertToCallDOM = false and skips the DOMJIT fast path even though fixEdge<Int52RepUse> handles SpecAnyIntAsDouble fine. Consider using SpecIntAnyFormat (SpeculatedType.h:107) as the mask — the case-label list here already includes SpecAnyIntAsDouble, which suggests the body should accept it too.

Extended reasoning...

What the issue is

Now that 1f1383b restructures the doToChildren lambda so ++index runs on every child, the profitability switch at DFGFixupPhase.cpp:5312 actually executes for the first time. The Int52 group (lines 5339-5346) tests:

case SpecInt52Any:
case SpecInt32AsInt52:
case SpecNonInt32AsInt52:
case SpecAnyIntAsDouble: {
    if (edge->prediction() && !(edge->prediction() & (SpecInt32Only | SpecInt52Any)))
        shouldConvertToCallDOM = false;
    break;
}

But at Fixup time the argument is still a raw Call-node varargs child (a GetLocal / JSConstant / etc.) whose prediction() comes from bytecode value profiling. The SpecInt52Any bits (39|40, SpeculatedType.h:92-94) only ever appear on nodes with NodeResultInt52 — i.e. after Fixup has inserted an Int52Rep conversion. Value profiles never produce them: speculationFromValue() (SpeculatedType.cpp:631-635) sees a large integer as a double-boxed JSValue and returns SpecAnyIntAsDouble (bit 41), and SpecHeapTop excludes SpecInt52Any entirely.

Step-by-step example

Take a bun:ffi reader whose argument was consistently observed as 2**40 (a pointer-sized value):

  1. 2**40 is boxed as a double JSValue; speculationFromValue() returns SpecAnyIntAsDouble (1ull << 41).
  2. At attemptToMakeCallDOM, the argument edge points to a GetLocal with prediction() == SpecAnyIntAsDouble.
  3. SpecInt32Only | SpecInt52Any = bits 34-40. SpecAnyIntAsDouble & (SpecInt32Only | SpecInt52Any) = 0.
  4. edge->prediction() is nonzero and the AND is zero → shouldConvertToCallDOM = false.
  5. The function returns false; the node stays a plain Call and never gets the DOMJIT fast path.

Note that fixEdge<Int52RepUse> in fixupCallDOM handles SpecAnyIntAsDouble just fine — it inserts Int52Rep(AnyIntUse), which converts an integral double to Int52 without a guaranteed BadType exit. So the profitability check is stricter than what the lowering actually requires. The case-label list itself already includes SpecAnyIntAsDouble (line 5342), which is a self-consistency signal that the mask should accept it.

Why nothing else prevents it

SpecIntAnyFormat (SpeculatedType.h:107) = SpecInt52Any | SpecInt32Only | SpecAnyIntAsDouble exists precisely for this "integer in any representation" test, and isIntAnyFormat() (line 417) is the corresponding predicate. The mask here just omits the third component. Mixed predictions that include any SpecInt32Only bit (e.g. an argument observed as both small and large integers) still pass because the test is any-overlap, so this only bites the pure-large-int case — which is exactly the shape a bun:ffi pointer/offset argument would have.

Impact

Not a correctness bug — the call falls back to a plain Call, so behavior is identical, just slower. Not a runtime regression vs. main either, since (a) this switch was dead code before 1f1383b, and (b) argument-bearing DOMJIT signatures are currently disabled in Bun. But the PR's stated purpose is to "unblock re-enabling argument-bearing DOMJIT signatures" and specifically calls out SpecInt52Any (bun:ffi readers); once those are re-enabled, this mask will keep the pure-large-int case on the slow path.

Suggested fix

Replace the mask with SpecIntAnyFormat:

if (edge->prediction() && !(edge->prediction() & SpecIntAnyFormat))
    shouldConvertToCallDOM = false;

or equivalently gate on !isIntAnyFormat(edge->prediction()) / !edge->shouldSpeculateInt52(), matching what Int52RepUse fixup can convert.

default:
RELEASE_ASSERT_NOT_REACHED();
break;
Expand Down
55 changes: 55 additions & 0 deletions Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11341,7 +11341,11 @@ void SpeculativeJIT::compileCallDOM(Node* node)

// FIXME: We should have a way to call functions with the vector of registers.
// https://bugs.webkit.org/show_bug.cgi?id=163099
#if USE(JSVALUE64)
using OperandVariant = Variant<SpeculateCellOperand, SpeculateInt32Operand, SpeculateBooleanOperand, SpeculateStrictInt52Operand>;
#else
using OperandVariant = Variant<SpeculateCellOperand, SpeculateInt32Operand, SpeculateBooleanOperand>;
#endif
Vector<OperandVariant, JSC_DOMJIT_SIGNATURE_MAX_ARGUMENTS_INCLUDING_THIS> operands;
Vector<GPRReg, JSC_DOMJIT_SIGNATURE_MAX_ARGUMENTS_INCLUDING_THIS> regs;

Expand Down Expand Up @@ -11371,6 +11375,22 @@ void SpeculativeJIT::compileCallDOM(Node* node)
operands.append(OperandVariant(WTF::InPlaceType<SpeculateBooleanOperand>, WTF::move(operand)));
};

auto appendTypedArray = [&](Edge& edge, JSType type) {
SpeculateCellOperand operand(this, edge);
GPRReg gpr = operand.gpr();
regs.append(gpr);
speculateCellTypeWithoutTypeFiltering(edge, gpr, type);
operands.append(OperandVariant(WTF::InPlaceType<SpeculateCellOperand>, WTF::move(operand)));
};

#if USE(JSVALUE64)
auto appendStrictInt52 = [&](Edge& edge) {
SpeculateStrictInt52Operand operand(this, edge);
regs.append(operand.gpr());
operands.append(OperandVariant(WTF::InPlaceType<SpeculateStrictInt52Operand>, WTF::move(operand)));
};
#endif

unsigned index = 0;
m_graph.doToChildren(node, [&](Edge edge) {
if (!index)
Expand All @@ -11386,6 +11406,41 @@ void SpeculativeJIT::compileCallDOM(Node* node)
case SpecBoolean:
appendBoolean(edge);
break;
case SpecInt8Array:
appendTypedArray(edge, JSType::Int8ArrayType);
break;
case SpecInt16Array:
appendTypedArray(edge, JSType::Int16ArrayType);
break;
case SpecInt32Array:
appendTypedArray(edge, JSType::Int32ArrayType);
break;
case SpecUint8Array:
appendTypedArray(edge, JSType::Uint8ArrayType);
break;
case SpecUint8ClampedArray:
appendTypedArray(edge, JSType::Uint8ClampedArrayType);
break;
case SpecUint16Array:
appendTypedArray(edge, JSType::Uint16ArrayType);
break;
case SpecUint32Array:
appendTypedArray(edge, JSType::Uint32ArrayType);
break;
case SpecFloat32Array:
appendTypedArray(edge, JSType::Float32ArrayType);
break;
case SpecFloat64Array:
appendTypedArray(edge, JSType::Float64ArrayType);
break;
#if USE(JSVALUE64)
case SpecInt52Any:
case SpecInt32AsInt52:
case SpecNonInt32AsInt52:
case SpecAnyIntAsDouble:
appendStrictInt52(edge);
break;
#endif
default:
RELEASE_ASSERT_NOT_REACHED();
break;
Expand Down
41 changes: 39 additions & 2 deletions Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -2640,8 +2640,21 @@ class SpeculateInt32Operand {
gpr();
}

explicit SpeculateInt32Operand(SpeculateInt32Operand&& other)
{
m_jit = other.m_jit;
m_edge = other.m_edge;
m_gprOrInvalid = other.m_gprOrInvalid;
m_format = other.m_format;

other.m_gprOrInvalid = InvalidGPRReg;
other.m_edge = Edge();
}

~SpeculateInt32Operand()
{
if (!m_edge)
return;
ASSERT(m_gprOrInvalid != InvalidGPRReg);
m_jit->unlock(m_gprOrInvalid);
}
Expand Down Expand Up @@ -2791,9 +2804,21 @@ class SpeculateStrictInt52Operand {
if (jit->isFilled(node()))
gpr();
}


explicit SpeculateStrictInt52Operand(SpeculateStrictInt52Operand&& other)
{
m_jit = other.m_jit;
m_edge = other.m_edge;
m_gprOrInvalid = other.m_gprOrInvalid;

other.m_gprOrInvalid = InvalidGPRReg;
other.m_edge = Edge();
}

~SpeculateStrictInt52Operand()
{
if (!m_edge)
return;
ASSERT(m_gprOrInvalid != InvalidGPRReg);
m_jit->unlock(m_gprOrInvalid);
}
Expand Down Expand Up @@ -3032,9 +3057,21 @@ class SpeculateBooleanOperand {
if (jit->isFilled(node()))
gpr();
}


explicit SpeculateBooleanOperand(SpeculateBooleanOperand&& other)
{
m_jit = other.m_jit;
m_edge = other.m_edge;
m_gprOrInvalid = other.m_gprOrInvalid;

other.m_gprOrInvalid = InvalidGPRReg;
other.m_edge = Edge();
}

~SpeculateBooleanOperand()
{
if (!m_edge)
return;
ASSERT(m_gprOrInvalid != InvalidGPRReg);
m_jit->unlock(m_gprOrInvalid);
}
Expand Down
Loading