Title: [287236] trunk
Revision
287236
Author
[email protected]
Date
2021-12-19 00:33:06 -0800 (Sun, 19 Dec 2021)

Log Message

[JSC] OpPow should have a "small int exponent" fast path at lower tiers
https://bugs.webkit.org/show_bug.cgi?id=234408

Reviewed by Yusuke Suzuki.

JSTests:

* microbenchmarks/pow-double-int.js: Added.
* microbenchmarks/pow-int-int.js: Added.

Source/_javascript_Core:

DFG has an ArithPow fast path which just multiplies in a loop when the exponent is an int between 0 and 1000;
this can be done at lower tiers too.

Implementing this at LLInt gives the following speedup with JIT disabled:

                          Before                    After

pow-int-int         193.7180+-0.4897     ^    100.3569+-1.9804        ^ definitely 1.9303x faster
pow-double-int      194.0744+-0.7998     ^    100.0346+-0.8655        ^ definitely 1.9401x faster

<geometric>         193.8824+-0.4667     ^    100.0964+-0.9922        ^ definitely 1.9370x faster

Implementing this at Baseline gives similar results with DFG disabled:

                                  Before                    After

pow-int-int         195.6251+-0.9577     ^     99.9627+-0.3307        ^ definitely 1.9570x faster
pow-double-int      196.1975+-0.9307     ^    101.0056+-0.3124        ^ definitely 1.9424x faster

<geometric>         195.8786+-0.5883     ^    100.4767+-0.2333        ^ definitely 1.9495x faster

Results are neutral otherwise.

* jit/JIT.cpp:
(JSC::JIT::privateCompileMainPass):
(JSC::JIT::privateCompileSlowCases):
* jit/JIT.h:
* jit/JITArithmetic.cpp:
(JSC::JIT::emit_op_pow):
(JSC::JIT::emitSlow_op_pow):
* llint/LowLevelInterpreter.asm:
* llint/LowLevelInterpreter32_64.asm:
* llint/LowLevelInterpreter64.asm:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (287235 => 287236)


--- trunk/JSTests/ChangeLog	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/JSTests/ChangeLog	2021-12-19 08:33:06 UTC (rev 287236)
@@ -1,3 +1,13 @@
+2021-12-19  Ross Kirsling  <[email protected]>
+
+        [JSC] OpPow should have a "small int exponent" fast path at lower tiers
+        https://bugs.webkit.org/show_bug.cgi?id=234408
+
+        Reviewed by Yusuke Suzuki.
+
+        * microbenchmarks/pow-double-int.js: Added.
+        * microbenchmarks/pow-int-int.js: Added.
+
 2021-12-16  Devin Rousso  <[email protected]>
 
         Implement Array.prototype.groupBy and Array.prototype.groupByToMap

Added: trunk/JSTests/microbenchmarks/pow-double-int.js (0 => 287236)


--- trunk/JSTests/microbenchmarks/pow-double-int.js	                        (rev 0)
+++ trunk/JSTests/microbenchmarks/pow-double-int.js	2021-12-19 08:33:06 UTC (rev 287236)
@@ -0,0 +1,7 @@
+//@ skip if $model == "Apple Watch Series 3" # added by mark-jsc-stress-test.py
+function exponentiate(x, y) { return x ** y; }
+noInline(exponentiate);
+
+for (let i = -50; i <= 50; i += 0.1)
+    for (let j = 0; j <= 1000; j++) 
+        exponentiate(i, j);

Added: trunk/JSTests/microbenchmarks/pow-int-int.js (0 => 287236)


--- trunk/JSTests/microbenchmarks/pow-int-int.js	                        (rev 0)
+++ trunk/JSTests/microbenchmarks/pow-int-int.js	2021-12-19 08:33:06 UTC (rev 287236)
@@ -0,0 +1,7 @@
+//@ skip if $model == "Apple Watch Series 3" # added by mark-jsc-stress-test.py
+function exponentiate(x, y) { return x ** y; }
+noInline(exponentiate);
+
+for (let i = -500; i <= 500; i++)
+    for (let j = 0; j <= 1000; j++) 
+        exponentiate(i, j);

Modified: trunk/Source/_javascript_Core/ChangeLog (287235 => 287236)


--- trunk/Source/_javascript_Core/ChangeLog	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-12-19 08:33:06 UTC (rev 287236)
@@ -1,3 +1,44 @@
+2021-12-19  Ross Kirsling  <[email protected]>
+
+        [JSC] OpPow should have a "small int exponent" fast path at lower tiers
+        https://bugs.webkit.org/show_bug.cgi?id=234408
+
+        Reviewed by Yusuke Suzuki.
+
+        DFG has an ArithPow fast path which just multiplies in a loop when the exponent is an int between 0 and 1000;
+        this can be done at lower tiers too.
+
+        Implementing this at LLInt gives the following speedup with JIT disabled:
+
+                                  Before                    After
+
+        pow-int-int         193.7180+-0.4897     ^    100.3569+-1.9804        ^ definitely 1.9303x faster
+        pow-double-int      194.0744+-0.7998     ^    100.0346+-0.8655        ^ definitely 1.9401x faster
+
+        <geometric>         193.8824+-0.4667     ^    100.0964+-0.9922        ^ definitely 1.9370x faster
+
+        Implementing this at Baseline gives similar results with DFG disabled:
+
+                                          Before                    After
+
+        pow-int-int         195.6251+-0.9577     ^     99.9627+-0.3307        ^ definitely 1.9570x faster
+        pow-double-int      196.1975+-0.9307     ^    101.0056+-0.3124        ^ definitely 1.9424x faster
+
+        <geometric>         195.8786+-0.5883     ^    100.4767+-0.2333        ^ definitely 1.9495x faster
+
+        Results are neutral otherwise.
+
+        * jit/JIT.cpp:
+        (JSC::JIT::privateCompileMainPass):
+        (JSC::JIT::privateCompileSlowCases):
+        * jit/JIT.h:
+        * jit/JITArithmetic.cpp:
+        (JSC::JIT::emit_op_pow):
+        (JSC::JIT::emitSlow_op_pow):
+        * llint/LowLevelInterpreter.asm:
+        * llint/LowLevelInterpreter32_64.asm:
+        * llint/LowLevelInterpreter64.asm:
+
 2021-12-18  Mikhail R. Gadelha  <[email protected]>
 
         [JSC][32bit] Fix undefined behavior causing miscompilation with clang 13 on ARM

Modified: trunk/Source/_javascript_Core/jit/JIT.cpp (287235 => 287236)


--- trunk/Source/_javascript_Core/jit/JIT.cpp	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/jit/JIT.cpp	2021-12-19 08:33:06 UTC (rev 287236)
@@ -343,7 +343,6 @@
         DEFINE_SLOW_OP(create_generator)
         DEFINE_SLOW_OP(create_async_generator)
         DEFINE_SLOW_OP(new_generator)
-        DEFINE_SLOW_OP(pow)
 
         DEFINE_OP(op_add)
         DEFINE_OP(op_bitnot)
@@ -435,6 +434,7 @@
         DEFINE_OP(op_super_sampler_end)
         DEFINE_OP(op_lshift)
         DEFINE_OP(op_mod)
+        DEFINE_OP(op_pow)
         DEFINE_OP(op_mov)
         DEFINE_OP(op_mul)
         DEFINE_OP(op_negate)
@@ -616,6 +616,7 @@
         DEFINE_SLOWCASE_OP(op_loop_hint)
         DEFINE_SLOWCASE_OP(op_check_traps)
         DEFINE_SLOWCASE_OP(op_mod)
+        DEFINE_SLOWCASE_OP(op_pow)
         DEFINE_SLOWCASE_OP(op_mul)
         DEFINE_SLOWCASE_OP(op_negate)
         DEFINE_SLOWCASE_OP(op_neq)

Modified: trunk/Source/_javascript_Core/jit/JIT.h (287235 => 287236)


--- trunk/Source/_javascript_Core/jit/JIT.h	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/jit/JIT.h	2021-12-19 08:33:06 UTC (rev 287236)
@@ -485,6 +485,7 @@
         void emit_op_super_sampler_end(const Instruction*);
         void emit_op_lshift(const Instruction*);
         void emit_op_mod(const Instruction*);
+        void emit_op_pow(const Instruction*);
         void emit_op_mov(const Instruction*);
         void emit_op_mul(const Instruction*);
         void emit_op_negate(const Instruction*);
@@ -596,6 +597,7 @@
         void emitSlow_op_loop_hint(const Instruction*, Vector<SlowCaseEntry>::iterator&);
         void emitSlow_op_check_traps(const Instruction*, Vector<SlowCaseEntry>::iterator&);
         void emitSlow_op_mod(const Instruction*, Vector<SlowCaseEntry>::iterator&);
+        void emitSlow_op_pow(const Instruction*, Vector<SlowCaseEntry>::iterator&);
         void emitSlow_op_mul(const Instruction*, Vector<SlowCaseEntry>::iterator&);
         void emitSlow_op_negate(const Instruction*, Vector<SlowCaseEntry>::iterator&);
         void emitSlow_op_neq(const Instruction*, Vector<SlowCaseEntry>::iterator&);

Modified: trunk/Source/_javascript_Core/jit/JITArithmetic.cpp (287235 => 287236)


--- trunk/Source/_javascript_Core/jit/JITArithmetic.cpp	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/jit/JITArithmetic.cpp	2021-12-19 08:33:06 UTC (rev 287236)
@@ -476,6 +476,60 @@
 
 /* ------------------------------ END: OP_MOD ------------------------------ */
 
+void JIT::emit_op_pow(const Instruction* currentInstruction)
+{
+    auto bytecode = currentInstruction->as<OpPow>();
+    VirtualRegister result = bytecode.m_dst;
+    VirtualRegister op1 = bytecode.m_lhs;
+    VirtualRegister op2 = bytecode.m_rhs;
+
+    constexpr JSValueRegs leftRegs = jsRegT10;
+    constexpr JSValueRegs rightRegs = jsRegT32;
+    constexpr JSValueRegs resultRegs = leftRegs;
+    constexpr GPRReg scratchGPR = regT4;
+
+    emitGetVirtualRegister(op1, leftRegs);
+    emitGetVirtualRegister(op2, rightRegs);
+    emitJumpSlowCaseIfNotInt(rightRegs);
+
+    addSlowCase(branch32(LessThan, rightRegs.payloadGPR(), TrustedImm32(0)));
+    addSlowCase(branch32(GreaterThan, rightRegs.payloadGPR(), TrustedImm32(maxExponentForIntegerMathPow)));
+
+    Jump lhsNotInt = branchIfNotInt32(leftRegs);
+    convertInt32ToDouble(leftRegs.payloadGPR(), fpRegT0);
+    Jump lhsReady = jump();
+    lhsNotInt.link(this);
+    addSlowCase(branchIfNotNumber(leftRegs, scratchGPR));
+#if USE(JSVALUE64)
+    unboxDouble(leftRegs.payloadGPR(), scratchGPR, fpRegT0);
+#else
+    unboxDouble(leftRegs, fpRegT0);
+#endif
+    lhsReady.link(this);
+
+    move(TrustedImm32(1), scratchGPR);
+    convertInt32ToDouble(scratchGPR, fpRegT1);
+
+    Label loop = label();
+    Jump exponentIsEven = branchTest32(Zero, rightRegs.payloadGPR(), TrustedImm32(1));
+    mulDouble(fpRegT0, fpRegT1);
+    exponentIsEven.link(this);
+    mulDouble(fpRegT0, fpRegT0);
+    rshift32(TrustedImm32(1), rightRegs.payloadGPR());
+    branchTest32(NonZero, rightRegs.payloadGPR()).linkTo(loop, this);
+
+    boxDouble(fpRegT1, resultRegs);
+    emitPutVirtualRegister(result, resultRegs);
+}
+
+void JIT::emitSlow_op_pow(const Instruction* currentInstruction, Vector<SlowCaseEntry>::iterator& iter)
+{
+    linkAllSlowCases(iter);
+
+    JITSlowPathCall slowPathCall(this, currentInstruction, slow_path_pow);
+    slowPathCall.call();
+}
+
 void JIT::emit_op_negate(const Instruction* currentInstruction)
 {
     UnaryArithProfile* arithProfile = &m_unlinkedCodeBlock->unaryArithProfile(currentInstruction->as<OpNegate>().m_profileIndex);
@@ -961,8 +1015,6 @@
     emitMathICSlow<OpSub>(subIC, currentInstruction, operationValueSubProfiledOptimize, operationValueSubProfiled, operationValueSubOptimize);
 }
 
-/* ------------------------------ END: OP_ADD, OP_SUB, OP_MUL, OP_POW ------------------------------ */
-
 } // namespace JSC
 
 #endif // ENABLE(JIT)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm (287235 => 287236)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter.asm	2021-12-19 08:33:06 UTC (rev 287236)
@@ -2134,7 +2134,6 @@
 slowPathOp(is_constructor)
 slowPathOp(new_array_buffer)
 slowPathOp(new_array_with_spread)
-slowPathOp(pow)
 slowPathOp(push_with_scope)
 slowPathOp(put_by_id_with_this)
 slowPathOp(put_by_val_with_this)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm (287235 => 287236)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter32_64.asm	2021-12-19 08:33:06 UTC (rev 287236)
@@ -1208,7 +1208,43 @@
     end,
     macro (lhs, rhs) divd rhs, lhs end)
 
+llintOpWithReturn(op_pow, OpPow, macro (size, get, dispatch, return)
+    get(m_rhs, t2)
+    get(m_lhs, t0)
+    loadConstantOrVariable(size, t2, t3, t1)
+    loadConstantOrVariable2Reg(size, t0, t2, t0)
+    bineq t3, Int32Tag, .slow
 
+    bilt t1, 0, .slow
+    bigt t1, (constexpr maxExponentForIntegerMathPow), .slow
+
+    bineq t2, Int32Tag, .lhsNotInt
+    ci2ds t0, ft0
+    jmp .lhsReady
+.lhsNotInt:
+    bia t2, LowestTag, .slow
+    fii2d t0, t2, ft0
+.lhsReady:
+    get(m_dst, t2)
+    move 1, t0
+    ci2ds t0, ft1
+
+.loop:
+    btiz t1, 0x1, .exponentIsEven
+    muld ft0, ft1
+.exponentIsEven:
+    muld ft0, ft0
+    rshifti 1, t1
+    btinz t1, .loop
+
+    stored ft1, [cfr, t2, 8]
+    dispatch()
+
+.slow:
+    callSlowPath(_slow_path_pow)
+    dispatch()
+end)
+
 llintOpWithReturn(op_unsigned, OpUnsigned, macro (size, get, dispatch, return)
     get(m_operand, t1)
     loadConstantOrVariablePayload(size, t1, Int32Tag, t2, .opUnsignedSlow)

Modified: trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm (287235 => 287236)


--- trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2021-12-19 06:30:39 UTC (rev 287235)
+++ trunk/Source/_javascript_Core/llint/LowLevelInterpreter64.asm	2021-12-19 08:33:06 UTC (rev 287236)
@@ -1319,6 +1319,45 @@
     slowPathOp(mod)
 end
 
+llintOpWithReturn(op_pow, OpPow, macro (size, get, dispatch, return)
+    get(m_rhs, t0)
+    get(m_lhs, t2)
+    loadConstantOrVariableInt32(size, t0, t1, .slow)
+    loadConstantOrVariable(size, t2, t0)
+
+    bilt t1, 0, .slow
+    bigt t1, (constexpr maxExponentForIntegerMathPow), .slow
+
+    bqb t0, numberTag, .lhsNotInt
+    ci2ds t0, ft0
+    jmp .lhsReady
+.lhsNotInt:
+    btqz t0, numberTag, .slow
+    addq numberTag, t0
+    fq2d t0, ft0
+.lhsReady:
+    get(m_dst, t2)
+    move 1, t0
+    ci2ds t0, ft1
+
+.loop:
+    btiz t1, 0x1, .exponentIsEven
+    muld ft0, ft1
+.exponentIsEven:
+    muld ft0, ft0
+    rshifti 1, t1
+    btinz t1, .loop
+
+    fd2q ft1, t0
+    subq numberTag, t0
+    storeq t0, [cfr, t2, 8]
+    dispatch()
+
+.slow:
+    callSlowPath(_slow_path_pow)
+    dispatch()
+end)
+
 llintOpWithReturn(op_unsigned, OpUnsigned, macro (size, get, dispatch, return)
     get(m_operand, t1)
     loadConstantOrVariable(size, t1, t2)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to