- Revision
- 250430
- Author
- [email protected]
- Date
- 2019-09-27 09:56:58 -0700 (Fri, 27 Sep 2019)
Log Message
[JSC] DFG recursive-tail-call optimization should not emit jump to call-frame with varargs
https://bugs.webkit.org/show_bug.cgi?id=202299
<rdar://problem/52669116>
Reviewed by Saam Barati.
JSTests:
* stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs-simple.js: Added.
(foo):
(test):
* stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs.js: Added.
(foo):
(C1.prototype.baz):
(C1):
(bar):
(noInline.bar.goo):
(C2.prototype.baz):
(C2):
(test):
Source/_javascript_Core:
When converting recursive-tail-call to jump to the upper call frame, we picked call-frame which is spread by LoadVarargs.
This is wrong since this call-frame does not know the exact number of arguments. We are using InlineCallFrame::argumentCountIncludingThis,
but this is maximal argumentCountIncludingThis when InlineCallFrame is Varargs call-frame. Let's see the simple example.
'use strict';
var count = 0;
function foo() {
count--;
if (count === 0)
return 30;
return foo(42, 42); // HERE
}
function test() {
count = 100;
return foo(...[42, 42]); // THERE
}
noInline(test);
In the above case, currently, we convert HERE's foo call to the jump to the prologue of the foo function inlined by "test". But since foo is called
in a varargs form, "test" emits LoadVarargs, and it also emits `SetArgumentMaybe` for 1st and 2nd arguments. Since HERE's foo call is actually passing
two arguments, we emit a Phi node which Upsilon is from SetArgumentMaybe and 42 Constant. This is wrong since SetArgumentMaybe should not be used. Later,
SSA conversion phase emits Upsilon with SetArgumentMaybe, and since SetArgumentMaybe is simply removed in SSA conversion phase, it ends up emitting
Upsilon without a child.
We are currently only performing recursive-tail-call optimization when argument count matches. Given this condition, we should not pick varargs CallFrame
as a jump target.
* dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleRecursiveTailCall):
* dfg/DFGSSAConversionPhase.cpp:
(JSC::DFG::SSAConversionPhase::run):
Modified Paths
Added Paths
Diff
Modified: trunk/JSTests/ChangeLog (250429 => 250430)
--- trunk/JSTests/ChangeLog 2019-09-27 16:49:54 UTC (rev 250429)
+++ trunk/JSTests/ChangeLog 2019-09-27 16:56:58 UTC (rev 250430)
@@ -1,3 +1,24 @@
+2019-09-26 Yusuke Suzuki <[email protected]>
+
+ [JSC] DFG recursive-tail-call optimization should not emit jump to call-frame with varargs
+ https://bugs.webkit.org/show_bug.cgi?id=202299
+ <rdar://problem/52669116>
+
+ Reviewed by Saam Barati.
+
+ * stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs-simple.js: Added.
+ (foo):
+ (test):
+ * stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs.js: Added.
+ (foo):
+ (C1.prototype.baz):
+ (C1):
+ (bar):
+ (noInline.bar.goo):
+ (C2.prototype.baz):
+ (C2):
+ (test):
+
2019-09-26 Alexey Shvayka <[email protected]>
toExponential, toFixed, and toPrecision should allow arguments up to 100
Added: trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs-simple.js (0 => 250430)
--- trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs-simple.js (rev 0)
+++ trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs-simple.js 2019-09-27 16:56:58 UTC (rev 250430)
@@ -0,0 +1,18 @@
+'use strict';
+
+var count = 0;
+function foo() {
+ count--;
+ if (count === 0)
+ return 30;
+ return foo(42, 42);
+}
+
+function test() {
+ count = 100;
+ return foo(...[42, 42]);
+}
+noInline(test);
+
+for (var i = 0; i < 1e6; ++i)
+ test();
Added: trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs.js (0 => 250430)
--- trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs.js (rev 0)
+++ trunk/JSTests/stress/recursive-tail-call-optimization-should-not-jump-into-call-frame-with-varargs.js 2019-09-27 16:56:58 UTC (rev 250430)
@@ -0,0 +1,37 @@
+'use strict';
+var o;
+function foo() {
+ return o.baz();
+}
+
+class C1 {
+ baz() {
+ }
+};
+var x = new C1();
+
+function bar() {
+ o = x;
+};
+noInline(bar)
+
+function goo() {
+ return foo([[]], bar());
+}
+
+class C2 {
+ baz() {
+ return goo();
+ }
+};
+var y = new C2();
+
+function test() {
+ o = y;
+ return foo(...[43646, 43754]);
+}
+noInline(test)
+
+var result = 0;
+for (var i = 0; i < 1000000; ++i)
+ result += test();
Modified: trunk/Source/_javascript_Core/ChangeLog (250429 => 250430)
--- trunk/Source/_javascript_Core/ChangeLog 2019-09-27 16:49:54 UTC (rev 250429)
+++ trunk/Source/_javascript_Core/ChangeLog 2019-09-27 16:56:58 UTC (rev 250430)
@@ -1,3 +1,44 @@
+2019-09-26 Yusuke Suzuki <[email protected]>
+
+ [JSC] DFG recursive-tail-call optimization should not emit jump to call-frame with varargs
+ https://bugs.webkit.org/show_bug.cgi?id=202299
+ <rdar://problem/52669116>
+
+ Reviewed by Saam Barati.
+
+ When converting recursive-tail-call to jump to the upper call frame, we picked call-frame which is spread by LoadVarargs.
+ This is wrong since this call-frame does not know the exact number of arguments. We are using InlineCallFrame::argumentCountIncludingThis,
+ but this is maximal argumentCountIncludingThis when InlineCallFrame is Varargs call-frame. Let's see the simple example.
+
+ 'use strict';
+ var count = 0;
+ function foo() {
+ count--;
+ if (count === 0)
+ return 30;
+ return foo(42, 42); // HERE
+ }
+
+ function test() {
+ count = 100;
+ return foo(...[42, 42]); // THERE
+ }
+ noInline(test);
+
+ In the above case, currently, we convert HERE's foo call to the jump to the prologue of the foo function inlined by "test". But since foo is called
+ in a varargs form, "test" emits LoadVarargs, and it also emits `SetArgumentMaybe` for 1st and 2nd arguments. Since HERE's foo call is actually passing
+ two arguments, we emit a Phi node which Upsilon is from SetArgumentMaybe and 42 Constant. This is wrong since SetArgumentMaybe should not be used. Later,
+ SSA conversion phase emits Upsilon with SetArgumentMaybe, and since SetArgumentMaybe is simply removed in SSA conversion phase, it ends up emitting
+ Upsilon without a child.
+
+ We are currently only performing recursive-tail-call optimization when argument count matches. Given this condition, we should not pick varargs CallFrame
+ as a jump target.
+
+ * dfg/DFGByteCodeParser.cpp:
+ (JSC::DFG::ByteCodeParser::handleRecursiveTailCall):
+ * dfg/DFGSSAConversionPhase.cpp:
+ (JSC::DFG::SSAConversionPhase::run):
+
2019-09-26 Alexey Shvayka <[email protected]>
toExponential, toFixed, and toPrecision should allow arguments up to 100
Modified: trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp (250429 => 250430)
--- trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp 2019-09-27 16:49:54 UTC (rev 250429)
+++ trunk/Source/_javascript_Core/dfg/DFGByteCodeParser.cpp 2019-09-27 16:56:58 UTC (rev 250430)
@@ -1397,10 +1397,19 @@
VERBOSE_LOG(" We found a recursive tail call, trying to optimize it into a jump.\n");
if (auto* callFrame = stackEntry->m_inlineCallFrame) {
+ // FIXME: We only accept jump to CallFrame which has exact same argumentCountIncludingThis. But we can remove this by fixing up arguments.
+ // And we can also allow jumping into CallFrame with Varargs if the passing number of arguments is greater than or equal to mandatoryMinimum of CallFrame.
+ // https://bugs.webkit.org/show_bug.cgi?id=202317
+
// Some code may statically use the argument count from the InlineCallFrame, so it would be invalid to loop back if it does not match.
// We "continue" instead of returning false in case another stack entry further on the stack has the right number of arguments.
if (argumentCountIncludingThis != static_cast<int>(callFrame->argumentCountIncludingThis))
continue;
+ // If the target InlineCallFrame is Varargs, we do not know how many arguments are actually filled by LoadVarargs. Varargs InlineCallFrame's
+ // argumentCountIncludingThis is maximum number of potentially filled arguments by LoadVarargs. We "continue" to the upper frame which may be
+ // a good target to jump into.
+ if (callFrame->isVarargs())
+ continue;
} else {
// We are in the machine code entry (i.e. the original caller).
// If we have more arguments than the number of parameters to the function, it is not clear where we could put them on the stack.
Modified: trunk/Source/_javascript_Core/dfg/DFGSSAConversionPhase.cpp (250429 => 250430)
--- trunk/Source/_javascript_Core/dfg/DFGSSAConversionPhase.cpp 2019-09-27 16:49:54 UTC (rev 250429)
+++ trunk/Source/_javascript_Core/dfg/DFGSSAConversionPhase.cpp 2019-09-27 16:56:58 UTC (rev 250430)
@@ -424,6 +424,8 @@
// We have to use an unchecked use because at least sometimes, the end of the block
// is not exitOK.
UseKind useKind = uncheckedUseKindFor(format);
+
+ dataLogLnIf(verbose, "Inserting Upsilon for ", variable->local(), " propagating ", valueForOperand.operand(variable->local()), " to ", phiNode);
m_insertionSet.insertNode(
upsilonInsertionPoint, SpecNone, Upsilon, upsilonOrigin,