Skip to content

Commit f274ef4

Browse files
committed
[JSC] Inlined Array#sort should keep the comparator call kind for bound functions
https://bugs.webkit.org/show_bug.cgi?id=327983 Reviewed by Yusuke Suzuki. When the comparator is a bound function, BoundFunctionCallIntrinsic inlined its target as BoundFunctionCall and dropped ArraySortComparatorCall. An OSR exit inside the target then returned past the sort call instead of restarting it. BoundFunctionCall and BoundFunctionTailCall behave like Call and TailCall, so remove them and inline the target with the kind of the bound function call itself. As a result, FTL now turns a recursive tail call through a bound function into a jump, as it does for a plain tail call. Tests: JSTests/stress/array-sort-inline-osr-exit-in-bound-comparator.js JSTests/stress/bound-function-recursive-tail-call.js * JSTests/stress/array-sort-inline-osr-exit-in-bound-comparator.js: Added. (shouldBe): (compare): (compareWithBoundArguments): (compareWithTailCall): (useResult): (ignoreResult): (tailCall): (callTailCall): (sortBoundTwice): (sortBoundWithArguments): (sortBoundWithTailCall): * Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp: (JSC::DFG::ByteCodeParser::handleIntrinsicCall): (JSC::DFG::ByteCodeParser::inlineCall): * JSTests/stress/bound-function-recursive-tail-call.js: Added. (shouldBe): (sum): (isEven): (isOdd): (count): (callCount): * Source/JavaScriptCore/bytecode/InlineCallFrame.cpp: (WTF::printInternal): * Source/JavaScriptCore/bytecode/InlineCallFrame.h: (JSC::InlineCallFrame::callModeFor): (JSC::InlineCallFrame::specializationKindFor): (JSC::InlineCallFrame::isTail): * Source/JavaScriptCore/dfg/DFGOSRExitCompilerCommon.cpp: (JSC::DFG::callerReturnPC): Canonical link: https://commits.webkit.org/324512@main
1 parent 071ce01 commit f274ef4

6 files changed

Lines changed: 124 additions & 22 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
function compare(a, b) {
7+
OSRExit();
8+
return a - b;
9+
}
10+
11+
function compareWithBoundArguments(sign, a, b) {
12+
OSRExit();
13+
return this.factor * sign * (a - b);
14+
}
15+
16+
function compareWithTailCall(a, b) {
17+
"use strict";
18+
return compare(a, b);
19+
}
20+
21+
const bound = compare.bind(null);
22+
const boundTwice = bound.bind(null);
23+
const boundWithArguments = compareWithBoundArguments.bind({ factor: 2 }, -1);
24+
const boundWithTailCall = compareWithTailCall.bind(null);
25+
26+
function useResult(a, b, c) {
27+
let array = [a, b, c];
28+
let result = array.sort(bound);
29+
shouldBe(result, array);
30+
return result;
31+
}
32+
33+
function ignoreResult(a, b, c) {
34+
let array = [a, b, c];
35+
array.sort(bound);
36+
return array;
37+
}
38+
39+
function tailCall(a, b, c) {
40+
"use strict";
41+
return [a, b, c].sort(bound);
42+
}
43+
44+
function callTailCall(a, b, c) {
45+
return [tailCall(a, b, c)];
46+
}
47+
48+
function sortBoundTwice(a, b, c) {
49+
return [a, b, c].sort(boundTwice);
50+
}
51+
52+
function sortBoundWithArguments(a, b, c) {
53+
return [a, b, c].sort(boundWithArguments);
54+
}
55+
56+
function sortBoundWithTailCall(a, b, c) {
57+
return [a, b, c].sort(boundWithTailCall);
58+
}
59+
60+
for (let i = 0; i < testLoopCount; ++i) {
61+
shouldBe(useResult(3, 1, 2).join(), "1,2,3");
62+
shouldBe(ignoreResult(3, 1, 2).join(), "1,2,3");
63+
shouldBe(tailCall(3, 1, 2).join(), "1,2,3");
64+
shouldBe(callTailCall(3, 1, 2)[0].join(), "1,2,3");
65+
shouldBe(sortBoundTwice(3, 1, 2).join(), "1,2,3");
66+
shouldBe(sortBoundWithArguments(3, 1, 2).join(), "3,2,1");
67+
shouldBe(sortBoundWithTailCall(3, 1, 2).join(), "1,2,3");
68+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
function sum(step, n, acc) {
7+
"use strict";
8+
if (!n)
9+
return acc + this.base;
10+
return boundSum(n - 1, acc + step);
11+
}
12+
noInline(sum);
13+
14+
function isEven(n) {
15+
"use strict";
16+
if (!n)
17+
return true;
18+
return boundIsOdd(n - 1);
19+
}
20+
noInline(isEven);
21+
22+
function isOdd(n) {
23+
"use strict";
24+
if (!n)
25+
return false;
26+
return isEven(n - 1);
27+
}
28+
29+
function count(n, acc) {
30+
"use strict";
31+
if (!n)
32+
return acc;
33+
return boundCount(n - 1, acc + 1);
34+
}
35+
36+
function callCount(n) {
37+
return count(n, 0) + 1;
38+
}
39+
noInline(callCount);
40+
41+
const boundSum = sum.bind({ base: 1000 }, 2);
42+
const boundIsOdd = isOdd.bind(null);
43+
const boundCount = count.bind(null);
44+
const receiver = { base: 1 };
45+
46+
for (let i = 0; i < testLoopCount; ++i) {
47+
shouldBe(sum.call(receiver, 5, 0, 0), 1);
48+
shouldBe(sum.call(receiver, 5, 10, 0), 1023);
49+
shouldBe(isEven(10), true);
50+
shouldBe(isEven(11), false);
51+
shouldBe(callCount(10), 11);
52+
}

‎Source/JavaScriptCore/bytecode/InlineCallFrame.cpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ void printInternal(PrintStream& out, JSC::InlineCallFrame::Kind kind)
127127
case JSC::InlineCallFrame::ProxyObjectInCall:
128128
out.print("ProxyObjectInCall");
129129
return;
130-
case JSC::InlineCallFrame::BoundFunctionCall:
131-
out.print("BoundFunctionCall");
132-
return;
133-
case JSC::InlineCallFrame::BoundFunctionTailCall:
134-
out.print("BoundFunctionTailCall");
135-
return;
136130
case JSC::InlineCallFrame::ArraySortComparatorCall:
137131
out.print("ArraySortComparatorCall");
138132
return;

‎Source/JavaScriptCore/bytecode/InlineCallFrame.h‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ struct InlineCallFrame {
6161
ProxyObjectLoadCall,
6262
ProxyObjectStoreCall,
6363
ProxyObjectInCall,
64-
BoundFunctionCall,
65-
BoundFunctionTailCall,
6664
ArraySortComparatorCall,
6765
};
6866
static constexpr unsigned bitWidthOfKind = 4;
@@ -77,12 +75,10 @@ struct InlineCallFrame {
7775
case ProxyObjectLoadCall:
7876
case ProxyObjectStoreCall:
7977
case ProxyObjectInCall:
80-
case BoundFunctionCall:
8178
case ArraySortComparatorCall:
8279
return CallMode::Regular;
8380
case TailCall:
8481
case TailCallVarargs:
85-
case BoundFunctionTailCall:
8682
return CallMode::Tail;
8783
case Construct:
8884
case ConstructVarargs:
@@ -129,8 +125,6 @@ struct InlineCallFrame {
129125
case ProxyObjectLoadCall:
130126
case ProxyObjectStoreCall:
131127
case ProxyObjectInCall:
132-
case BoundFunctionCall:
133-
case BoundFunctionTailCall:
134128
case ArraySortComparatorCall:
135129
return CodeSpecializationKind::CodeForCall;
136130
case Construct:
@@ -157,7 +151,6 @@ struct InlineCallFrame {
157151
switch (kind) {
158152
case TailCall:
159153
case TailCallVarargs:
160-
case BoundFunctionTailCall:
161154
return true;
162155
default:
163156
return false;

‎Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,9 +2197,7 @@ void ByteCodeParser::inlineCall(Node* callTargetNode, Operand result, CallVarian
21972197
case InlineCallFrame::SetterCall:
21982198
case InlineCallFrame::ProxyObjectLoadCall:
21992199
case InlineCallFrame::ProxyObjectStoreCall:
2200-
case InlineCallFrame::ProxyObjectInCall:
2201-
case InlineCallFrame::BoundFunctionCall:
2202-
case InlineCallFrame::BoundFunctionTailCall: {
2200+
case InlineCallFrame::ProxyObjectInCall: {
22032201
// When inlining getter and setter calls, we setup a stack frame which does not appear in the bytecode.
22042202
// Because Inlining can switch on executable, we could have a graph like this.
22052203
//
@@ -2951,7 +2949,6 @@ auto ByteCodeParser::handleIntrinsicCall(Node* callee, Operand resultOperand, Ca
29512949
{
29522950
VERBOSE_LOG(" The intrinsic is ", intrinsic, "\n");
29532951
UNUSED_PARAM(callOp);
2954-
UNUSED_PARAM(kind);
29552952
UNUSED_PARAM(specializationKind);
29562953

29572954
if (!isOpcodeShape<OpCallShape>(m_currentInstruction)) {
@@ -5759,7 +5756,7 @@ auto ByteCodeParser::handleIntrinsicCall(Node* callee, Operand resultOperand, Ca
57595756

57605757
// Bound function itself is completely wiped. And we should behave as if the current caller is directly calling this function.
57615758
// If the current caller is calling the callee in a tail-call form, then it should be a tail-call.
5762-
Terminality terminality = handleCall(resultOperand, callOp, callOp == Call ? InlineCallFrame::BoundFunctionCall : InlineCallFrame::BoundFunctionTailCall, osrExitIndex, jsConstant(boundFunction->targetFunction()), numberOfParameters - 1, newRegisterOffset, CallLinkStatus(CallVariant(boundFunction->targetFunction())), prediction, nullptr);
5759+
Terminality terminality = handleCall(resultOperand, callOp, kind, osrExitIndex, jsConstant(boundFunction->targetFunction()), numberOfParameters - 1, newRegisterOffset, CallLinkStatus(CallVariant(boundFunction->targetFunction())), prediction, nullptr);
57635760
didSetResult = true;
57645761
return terminality == NonTerminal ? CallOptimizationResult::Inlined : CallOptimizationResult::InlinedTerminal;
57655762
}

‎Source/JavaScriptCore/dfg/DFGOSRExitCompilerCommon.cpp‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ static CodePtr<JSEntryPtrTag> callerReturnPC(CodeBlock* baselineCodeBlockForCall
164164
#define LLINT_RETURN_LOCATION(name) LLInt::returnLocationThunk(name##_return_location, callInstruction.width()).code()
165165

166166
switch (trueCallerCallKind) {
167-
case InlineCallFrame::Call:
168-
case InlineCallFrame::BoundFunctionCall: {
167+
case InlineCallFrame::Call: {
169168
if (callInstruction.opcodeID() == op_call)
170169
jumpTarget = LLINT_RETURN_LOCATION(op_call);
171170
else if (callInstruction.opcodeID() == op_call_ignore_result)
@@ -249,8 +248,7 @@ static CodePtr<JSEntryPtrTag> callerReturnPC(CodeBlock* baselineCodeBlockForCall
249248
case InlineCallFrame::Call:
250249
case InlineCallFrame::Construct:
251250
case InlineCallFrame::CallVarargs:
252-
case InlineCallFrame::ConstructVarargs:
253-
case InlineCallFrame::BoundFunctionCall: {
251+
case InlineCallFrame::ConstructVarargs: {
254252
jumpTarget = static_cast<const BaselineJITCode*>(baselineCodeBlockForCaller->jitCode().get())->getCallLinkDoneLocationForBytecodeIndex(callBytecodeIndex).retagged<JSEntryPtrTag>();
255253
break;
256254
}

0 commit comments

Comments
 (0)