Title: [204881] trunk
Revision
204881
Author
[email protected]
Date
2016-08-23 19:36:40 -0700 (Tue, 23 Aug 2016)

Log Message

[JSC] Make ArithLog works with any type
https://bugs.webkit.org/show_bug.cgi?id=161110

Reviewed by Geoffrey Garen.

JSTests:

* stress/arith-log-on-various-types.js: Added.

Source/_javascript_Core:

Same old: if the type is not a number, assume the worst in every
phase and generate a fallback function call.

* dfg/DFGAbstractInterpreterInlines.h:
(JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
* dfg/DFGClobberize.h:
(JSC::DFG::clobberize):
* dfg/DFGFixupPhase.cpp:
(JSC::DFG::FixupPhase::fixupNode):
* dfg/DFGNodeType.h:
* dfg/DFGOperations.cpp:
* dfg/DFGOperations.h:
* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileArithLog):
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArithLog):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (204880 => 204881)


--- trunk/JSTests/ChangeLog	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/JSTests/ChangeLog	2016-08-24 02:36:40 UTC (rev 204881)
@@ -1,3 +1,12 @@
+2016-08-23  Benjamin Poulain  <[email protected]>
+
+        [JSC] Make ArithLog works with any type
+        https://bugs.webkit.org/show_bug.cgi?id=161110
+
+        Reviewed by Geoffrey Garen.
+
+        * stress/arith-log-on-various-types.js: Added.
+
 2016-08-23  Saam Barati  <[email protected]>
 
         JSC should have a "microbenchmarks" directory instead of "regress" directory

Added: trunk/JSTests/stress/arith-log-on-various-types.js (0 => 204881)


--- trunk/JSTests/stress/arith-log-on-various-types.js	                        (rev 0)
+++ trunk/JSTests/stress/arith-log-on-various-types.js	2016-08-24 02:36:40 UTC (rev 204881)
@@ -0,0 +1,171 @@
+"use strict";
+
+let logOfFour = Math.log(4);
+
+let validInputTestCases = [
+    // input as string, expected result as string.
+    ["undefined", "NaN"],
+    ["null", "-Infinity"],
+    ["0", "-Infinity"],
+    ["-0.", "-Infinity"],
+    ["1.", "0"],
+    ["4", "" + logOfFour],
+    ["Math.E", "1"],
+    ["Infinity", "Infinity"],
+    ["-Infinity", "NaN"],
+    ["NaN", "NaN"],
+    ["\"WebKit\"", "NaN"],
+    ["\"4\"", "" + logOfFour],
+    ["{ valueOf: () => { return Math.E; } }", "1"],
+    ["{ valueOf: () => { return 1; } }", "0"],
+    ["{ valueOf: () => { return 4; } }", "" + logOfFour],
+];
+
+let validInputTypedTestCases = validInputTestCases.map((element) => { return [eval("(" + element[0] + ")"), eval(element[1])] });
+
+function isIdentical(result, expected)
+{
+    if (expected === expected) {
+        if (result !== expected)
+            return false;
+        if (!expected && 1 / expected === -Infinity && 1 / result !== -Infinity)
+            return false;
+
+        return true;
+    }
+    return result !== result;
+}
+
+
+// Test Math.log() with a very polymorphic input. All test cases are seen at each iteration.
+function opaqueAllTypesLog(argument) {
+    return Math.log(argument);
+}
+noInline(opaqueAllTypesLog);
+noOSRExitFuzzing(opaqueAllTypesLog);
+
+function testAllTypesCall() {
+    for (let i = 0; i < 1e3; ++i) {
+        for (let testCaseInput of validInputTypedTestCases) {
+            let output = opaqueAllTypesLog(testCaseInput[0]);
+            if (!isIdentical(output, testCaseInput[1]))
+                throw "Failed testAllTypesCall for input " + testCaseInput[0] + " expected " + testCaseInput[1] + " got " + output;
+        }
+    }
+    if (numberOfDFGCompiles(opaqueAllTypesLog) > 2)
+        throw "We should have detected log() was polymorphic and generated a generic version.";
+}
+testAllTypesCall();
+
+
+// Test Math.log() on a completely typed input. Every call see only one type.
+function testSingleTypeCall() {
+    for (let testCaseInput of validInputTestCases) {
+        eval(`
+            function opaqueLog(argument) {
+                return Math.log(argument);
+            }
+            noInline(opaqueLog);
+            noOSRExitFuzzing(opaqueLog);
+
+            for (let i = 0; i < 1e4; ++i) {
+                if (!isIdentical(opaqueLog(${testCaseInput[0]}), ${testCaseInput[1]})) {
+                    throw "Failed testSingleTypeCall()";
+                }
+            }
+            if (numberOfDFGCompiles(opaqueLog) > 1)
+                throw "We should have compiled a single log for the expected type.";
+        `);
+    }
+}
+testSingleTypeCall();
+
+
+// Verify we call valueOf() exactly once per call.
+function opaqueLogForSideEffects(argument) {
+    return Math.log(argument);
+}
+noInline(opaqueLogForSideEffects);
+noOSRExitFuzzing(opaqueLogForSideEffects);
+
+function testSideEffect() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let log16 = Math.log(16);
+    for (let i = 0; i < 1e4; ++i) {
+        if (opaqueLogForSideEffects(testObject) !== log16)
+            throw "Incorrect result in testSideEffect()";
+    }
+    if (testObject.counter !== 1e4)
+        throw "Failed testSideEffect()";
+    if (numberOfDFGCompiles(opaqueLogForSideEffects) > 1)
+        throw "opaqueLogForSideEffects() is predictable, it should only be compiled once.";
+}
+testSideEffect();
+
+
+// Verify SQRT is not subject to CSE if the argument has side effects.
+function opaqueLogForCSE(argument) {
+    return Math.log(argument) + Math.log(argument) + Math.log(argument);
+}
+noInline(opaqueLogForCSE);
+noOSRExitFuzzing(opaqueLogForCSE);
+
+function testCSE() {
+    let testObject = {
+        counter: 0,
+        valueOf: function() { ++this.counter; return 16; }
+    };
+    let log16 = Math.log(16);
+    let threeLog16 = log16 + log16 + log16;
+    for (let i = 0; i < 1e4; ++i) {
+        if (opaqueLogForCSE(testObject) !== threeLog16)
+            throw "Incorrect result in testCSE()";
+    }
+    if (testObject.counter !== 3e4)
+        throw "Failed testCSE()";
+    if (numberOfDFGCompiles(opaqueLogForCSE) > 1)
+        throw "opaqueLogForCSE() is predictable, it should only be compiled once.";
+}
+testCSE();
+
+
+// Test exceptions in the argument.
+function testException() {
+    let counter = 0;
+    function opaqueLogWithException(argument) {
+        let result = Math.log(argument);
+        ++counter;
+        return result;
+    }
+    noInline(opaqueLogWithException);
+
+    let testObject = { valueOf: () => {  return 64; } };
+    let log64 = Math.log(64);
+
+    // Warm up without exception.
+    for (let i = 0; i < 1e3; ++i) {
+        if (opaqueLogWithException(testObject) !== log64)
+            throw "Incorrect result in opaqueLogWithException()";
+    }
+
+    let testThrowObject = { valueOf: () => { throw testObject; return 64; } };
+
+    for (let i = 0; i < 1e2; ++i) {
+        try {
+            if (opaqueLogWithException(testThrowObject) !== 8)
+                throw "This code should not be reached!!";
+        } catch (e) {
+            if (e !== testObject) {
+                throw "Wrong object thrown from opaqueLogWithException."
+            }
+        }
+    }
+
+    if (counter !== 1e3) {
+        throw "Invalid count in testException()";
+    }
+}
+testException();

Modified: trunk/Source/_javascript_Core/ChangeLog (204880 => 204881)


--- trunk/Source/_javascript_Core/ChangeLog	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-08-24 02:36:40 UTC (rev 204881)
@@ -1,3 +1,27 @@
+2016-08-23  Benjamin Poulain  <[email protected]>
+
+        [JSC] Make ArithLog works with any type
+        https://bugs.webkit.org/show_bug.cgi?id=161110
+
+        Reviewed by Geoffrey Garen.
+
+        Same old: if the type is not a number, assume the worst in every
+        phase and generate a fallback function call.
+
+        * dfg/DFGAbstractInterpreterInlines.h:
+        (JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
+        * dfg/DFGClobberize.h:
+        (JSC::DFG::clobberize):
+        * dfg/DFGFixupPhase.cpp:
+        (JSC::DFG::FixupPhase::fixupNode):
+        * dfg/DFGNodeType.h:
+        * dfg/DFGOperations.cpp:
+        * dfg/DFGOperations.h:
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileArithLog):
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileArithLog):
+
 2016-08-23  Ryan Haddad  <[email protected]>
 
         Rebaseline builtins-generator-tests after r204854.

Modified: trunk/Source/_javascript_Core/dfg/DFGAbstractInterpreterInlines.h (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGAbstractInterpreterInlines.h	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGAbstractInterpreterInlines.h	2016-08-24 02:36:40 UTC (rev 204881)
@@ -1010,7 +1010,10 @@
             setConstant(node, jsDoubleNumber(log(child.asNumber())));
             break;
         }
-        forNode(node).setType(typeOfDoubleUnaryOp(forNode(node->child1()).m_type));
+        SpeculatedType logType = SpecFullNumber;
+        if (node->child1().useKind() == DoubleRepUse)
+            logType = typeOfDoubleUnaryOp(forNode(node->child1()).m_type);
+        forNode(node).setType(logType);
         break;
     }
             

Modified: trunk/Source/_javascript_Core/dfg/DFGClobberize.h (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGClobberize.h	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGClobberize.h	2016-08-24 02:36:40 UTC (rev 204881)
@@ -155,7 +155,6 @@
     case ArithMax:
     case ArithPow:
     case ArithFRound:
-    case ArithLog:
     case GetScope:
     case SkipScope:
     case GetGlobalObject:
@@ -188,6 +187,7 @@
         return;
 
     case ArithCos:
+    case ArithLog:
     case ArithSin:
     case ArithSqrt:
         if (node->child1().useKind() == DoubleRepUse)

Modified: trunk/Source/_javascript_Core/dfg/DFGFixupPhase.cpp (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGFixupPhase.cpp	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGFixupPhase.cpp	2016-08-24 02:36:40 UTC (rev 204881)
@@ -387,6 +387,7 @@
         }
 
         case ArithCos:
+        case ArithLog:
         case ArithSin:
         case ArithSqrt: {
             Edge& child1 = node->child1();
@@ -396,8 +397,7 @@
                 fixEdge<UntypedUse>(child1);
             break;
         }
-        case ArithFRound:
-        case ArithLog: {
+        case ArithFRound: {
             fixDoubleOrBooleanEdge(node->child1());
             node->setResult(NodeResultDouble);
             break;

Modified: trunk/Source/_javascript_Core/dfg/DFGNodeType.h (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGNodeType.h	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGNodeType.h	2016-08-24 02:36:40 UTC (rev 204881)
@@ -162,7 +162,7 @@
     macro(ArithSqrt, NodeResultDouble) \
     macro(ArithSin, NodeResultDouble) \
     macro(ArithCos, NodeResultDouble) \
-    macro(ArithLog, NodeResultNumber) \
+    macro(ArithLog, NodeResultDouble) \
     \
     /* Add of values may either be arithmetic, or result in string concatenation. */\
     macro(ValueAdd, NodeResultJS | NodeMustGenerate) \

Modified: trunk/Source/_javascript_Core/dfg/DFGOperations.cpp (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGOperations.cpp	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGOperations.cpp	2016-08-24 02:36:40 UTC (rev 204881)
@@ -334,6 +334,18 @@
     return cos(a);
 }
 
+double JIT_OPERATION operationArithLog(ExecState* exec, EncodedJSValue encodedOp1)
+{
+    VM* vm = &exec->vm();
+    NativeCallFrameTracer tracer(vm, exec);
+
+    JSValue op1 = JSValue::decode(encodedOp1);
+    double a = op1.toNumber(exec);
+    if (UNLIKELY(vm->exception()))
+        return JSValue::encode(JSValue());
+    return log(a);
+}
+
 double JIT_OPERATION operationArithSin(ExecState* exec, EncodedJSValue encodedOp1)
 {
     VM* vm = &exec->vm();

Modified: trunk/Source/_javascript_Core/dfg/DFGOperations.h (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGOperations.h	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGOperations.h	2016-08-24 02:36:40 UTC (rev 204881)
@@ -54,6 +54,7 @@
 EncodedJSValue JIT_OPERATION operationValueAddNotNumber(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
 EncodedJSValue JIT_OPERATION operationValueDiv(ExecState*, EncodedJSValue encodedOp1, EncodedJSValue encodedOp2) WTF_INTERNAL;
 double JIT_OPERATION operationArithCos(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
+double JIT_OPERATION operationArithLog(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 double JIT_OPERATION operationArithSin(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 double JIT_OPERATION operationArithSqrt(ExecState*, EncodedJSValue encodedOp1) WTF_INTERNAL;
 EncodedJSValue JIT_OPERATION operationGetByVal(ExecState*, EncodedJSValue encodedBase, EncodedJSValue encodedProperty) WTF_INTERNAL;

Modified: trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp (204880 => 204881)


--- trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2016-08-24 02:36:40 UTC (rev 204881)
@@ -5125,11 +5125,22 @@
 
 void SpeculativeJIT::compileArithLog(Node* node)
 {
-    SpeculateDoubleOperand op1(this, node->child1());
-    FPRReg op1FPR = op1.fpr();
+    if (node->child1().useKind() == DoubleRepUse) {
+        SpeculateDoubleOperand op1(this, node->child1());
+        FPRReg op1FPR = op1.fpr();
+        flushRegisters();
+        FPRResult result(this);
+        callOperation(log, result.fpr(), op1FPR);
+        doubleResult(result.fpr(), node);
+        return;
+    }
+
+    JSValueOperand op1(this, node->child1());
+    JSValueRegs op1Regs = op1.jsValueRegs();
     flushRegisters();
     FPRResult result(this);
-    callOperation(log, result.fpr(), op1FPR);
+    callOperation(operationArithLog, result.fpr(), op1Regs);
+    m_jit.exceptionCheck();
     doubleResult(result.fpr(), node);
 }
 

Modified: trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp (204880 => 204881)


--- trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2016-08-24 02:23:19 UTC (rev 204880)
+++ trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2016-08-24 02:36:40 UTC (rev 204881)
@@ -2302,7 +2302,16 @@
         setDouble(result);
     }
 
-    void compileArithLog() { setDouble(m_out.doubleLog(lowDouble(m_node->child1()))); }
+    void compileArithLog()
+    {
+        if (m_node->child1().useKind() == DoubleRepUse) {
+            setDouble(m_out.doubleLog(lowDouble(m_node->child1())));
+            return;
+        }
+        LValue argument = lowJSValue(m_node->child1());
+        LValue result = vmCall(Double, m_out.operation(operationArithLog), m_callFrame, argument);
+        setDouble(result);
+    }
     
     void compileArithFRound()
     {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to