- Revision
- 250198
- Author
- [email protected]
- Date
- 2019-09-22 00:19:43 -0700 (Sun, 22 Sep 2019)
Log Message
[JSC] Remove CheckAdd in JetStream2/async-fs's Math.random function
https://bugs.webkit.org/show_bug.cgi?id=201633
Reviewed by Mark Lam.
Int52Rep is used in DFG and FTL to calculate Int52 things faster. This is typically used when user code see uint32_t type.
In JS, we handles Int32 well, but if the value exceeds Int32 range (like, using 0xffffffff), we use Int52 instead not to fallback to Double.
The problem is that we do not have optimizations for Int52's overflow checks. This emits many ArithAdd(Int52Rep x 2, CheckOverflow). Each
of them emits OSR exit, which prevents dead-store-elimination in B3, and makes ValueToInt32(Int52) alive if it is referenced from some variable which
can be seen if OSR exit occurs.
In this patch, we perform strength-reduction for CheckAdd, converting to Add. We already have such a thing. But the existing one does not handle instructions
well emitted when Int52 is used.
When Int52 is used, we typically have the sequence like,
Int64 @78 = SExt32(@73, DFG:@67<Int52>) // Widen Int32 to Int64
Int64 @81 = Shl(@78, $12(@80), DFG:@162<Int52>) // Convert Int32 to Int52
While we have Shl handling for integer-range optimization in B3ReduceStrength, we lack handling of SExt32 while it is very easy.
This patch adds SExt8, SExt16, SExt32, and ZExt32 handling to B3ReduceStrength's integer range analysis.
This converts many CheckAdd in JetStream2/async-fs's hot function to simple Add, and removes a bunch of unnecessary instructions which exist because of this OSR exit.
We can see ~5% improvement in JetStream2/async-fs.
* b3/B3ReduceStrength.cpp:
* b3/testb3.h:
(int16Operands):
(int8Operands):
* b3/testb3_1.cpp:
(run):
* b3/testb3_5.cpp:
(testCheckAddRemoveCheckWithSExt8):
(testCheckAddRemoveCheckWithSExt16):
(testCheckAddRemoveCheckWithSExt32):
(testCheckAddRemoveCheckWithZExt32):
Modified Paths
Diff
Modified: trunk/Source/_javascript_Core/ChangeLog (250197 => 250198)
--- trunk/Source/_javascript_Core/ChangeLog 2019-09-22 06:12:57 UTC (rev 250197)
+++ trunk/Source/_javascript_Core/ChangeLog 2019-09-22 07:19:43 UTC (rev 250198)
@@ -1,3 +1,42 @@
+2019-09-21 Yusuke Suzuki <[email protected]>
+
+ [JSC] Remove CheckAdd in JetStream2/async-fs's Math.random function
+ https://bugs.webkit.org/show_bug.cgi?id=201633
+
+ Reviewed by Mark Lam.
+
+ Int52Rep is used in DFG and FTL to calculate Int52 things faster. This is typically used when user code see uint32_t type.
+ In JS, we handles Int32 well, but if the value exceeds Int32 range (like, using 0xffffffff), we use Int52 instead not to fallback to Double.
+
+ The problem is that we do not have optimizations for Int52's overflow checks. This emits many ArithAdd(Int52Rep x 2, CheckOverflow). Each
+ of them emits OSR exit, which prevents dead-store-elimination in B3, and makes ValueToInt32(Int52) alive if it is referenced from some variable which
+ can be seen if OSR exit occurs.
+
+ In this patch, we perform strength-reduction for CheckAdd, converting to Add. We already have such a thing. But the existing one does not handle instructions
+ well emitted when Int52 is used.
+
+ When Int52 is used, we typically have the sequence like,
+
+ Int64 @78 = SExt32(@73, DFG:@67<Int52>) // Widen Int32 to Int64
+ Int64 @81 = Shl(@78, $12(@80), DFG:@162<Int52>) // Convert Int32 to Int52
+
+ While we have Shl handling for integer-range optimization in B3ReduceStrength, we lack handling of SExt32 while it is very easy.
+ This patch adds SExt8, SExt16, SExt32, and ZExt32 handling to B3ReduceStrength's integer range analysis.
+ This converts many CheckAdd in JetStream2/async-fs's hot function to simple Add, and removes a bunch of unnecessary instructions which exist because of this OSR exit.
+ We can see ~5% improvement in JetStream2/async-fs.
+
+ * b3/B3ReduceStrength.cpp:
+ * b3/testb3.h:
+ (int16Operands):
+ (int8Operands):
+ * b3/testb3_1.cpp:
+ (run):
+ * b3/testb3_5.cpp:
+ (testCheckAddRemoveCheckWithSExt8):
+ (testCheckAddRemoveCheckWithSExt16):
+ (testCheckAddRemoveCheckWithSExt32):
+ (testCheckAddRemoveCheckWithZExt32):
+
2019-09-21 Mark Lam <[email protected]>
Move JSLexicalEnvironment, DirectArguments, and ScopedArguments cells out of the Gigacage.
Modified: trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp (250197 => 250198)
--- trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp 2019-09-22 06:12:57 UTC (rev 250197)
+++ trunk/Source/_javascript_Core/b3/B3ReduceStrength.cpp 2019-09-22 07:19:43 UTC (rev 250198)
@@ -391,6 +391,15 @@
}
}
+ IntRange zExt32()
+ {
+ ASSERT(m_min >= INT32_MIN);
+ ASSERT(m_max <= INT32_MAX);
+ int32_t min = m_min;
+ int32_t max = m_max;
+ return IntRange(static_cast<uint64_t>(static_cast<uint32_t>(min)), static_cast<uint64_t>(static_cast<uint32_t>(max)));
+ }
+
private:
int64_t m_min { 0 };
int64_t m_max { 0 };
@@ -2595,6 +2604,14 @@
return rangeFor(value->child(0), timeToLive - 1).mul(
rangeFor(value->child(1), timeToLive - 1), value->type());
+ case SExt8:
+ case SExt16:
+ case SExt32:
+ return rangeFor(value->child(0), timeToLive - 1);
+
+ case ZExt32:
+ return rangeFor(value->child(0), timeToLive - 1).zExt32();
+
default:
break;
}
Modified: trunk/Source/_javascript_Core/b3/testb3.h (250197 => 250198)
--- trunk/Source/_javascript_Core/b3/testb3.h 2019-09-22 06:12:57 UTC (rev 250197)
+++ trunk/Source/_javascript_Core/b3/testb3.h 2019-09-22 07:19:43 UTC (rev 250198)
@@ -270,6 +270,8 @@
typedef Operand<int64_t> Int64Operand;
typedef Operand<int32_t> Int32Operand;
+typedef Operand<int16_t> Int16Operand;
+typedef Operand<int8_t> Int8Operand;
#define MAKE_OPERAND(value) Operand<decltype(value)> { #value, value }
@@ -343,6 +345,38 @@
return operands;
}
+inline Vector<Int16Operand> int16Operands()
+{
+ Vector<Int16Operand> operands({
+ { "0", 0 },
+ { "1", 1 },
+ { "-1", -1 },
+ { "42", 42 },
+ { "-42", -42 },
+ { "int16-max", std::numeric_limits<int16_t>::max() },
+ { "int16-min", std::numeric_limits<int16_t>::min() },
+ { "uint16-max", static_cast<int16_t>(std::numeric_limits<uint16_t>::max()) },
+ { "uint16-min", static_cast<int16_t>(std::numeric_limits<uint16_t>::min()) }
+ });
+ return operands;
+}
+
+inline Vector<Int8Operand> int8Operands()
+{
+ Vector<Int8Operand> operands({
+ { "0", 0 },
+ { "1", 1 },
+ { "-1", -1 },
+ { "42", 42 },
+ { "-42", -42 },
+ { "int8-max", std::numeric_limits<int8_t>::max() },
+ { "int8-min", std::numeric_limits<int8_t>::min() },
+ { "uint8-max", static_cast<int8_t>(std::numeric_limits<uint8_t>::max()) },
+ { "uint8-min", static_cast<int8_t>(std::numeric_limits<uint8_t>::min()) }
+ });
+ return operands;
+}
+
inline void add32(CCallHelpers& jit, GPRReg src1, GPRReg src2, GPRReg dest)
{
if (src2 == dest)
@@ -761,6 +795,10 @@
void testCheckAddArgumentAliasing32();
void testCheckAddSelfOverflow64();
void testCheckAddSelfOverflow32();
+void testCheckAddRemoveCheckWithSExt8(int8_t);
+void testCheckAddRemoveCheckWithSExt16(int16_t);
+void testCheckAddRemoveCheckWithSExt32(int32_t);
+void testCheckAddRemoveCheckWithZExt32(int32_t);
void testCheckSubImm();
void testCheckSubBadImm();
void testCheckSub();
Modified: trunk/Source/_javascript_Core/b3/testb3_1.cpp (250197 => 250198)
--- trunk/Source/_javascript_Core/b3/testb3_1.cpp 2019-09-22 06:12:57 UTC (rev 250197)
+++ trunk/Source/_javascript_Core/b3/testb3_1.cpp 2019-09-22 07:19:43 UTC (rev 250198)
@@ -342,6 +342,11 @@
RUN(testIToDReducedToIToF64Arg());
RUN(testIToDReducedToIToF32Arg());
+ RUN_UNARY(testCheckAddRemoveCheckWithSExt8, int8Operands());
+ RUN_UNARY(testCheckAddRemoveCheckWithSExt16, int16Operands());
+ RUN_UNARY(testCheckAddRemoveCheckWithSExt32, int32Operands());
+ RUN_UNARY(testCheckAddRemoveCheckWithZExt32, int32Operands());
+
RUN(testStore32(44));
RUN(testStoreConstant(49));
RUN(testStoreConstantPtr(49));
Modified: trunk/Source/_javascript_Core/b3/testb3_5.cpp (250197 => 250198)
--- trunk/Source/_javascript_Core/b3/testb3_5.cpp 2019-09-22 06:12:57 UTC (rev 250197)
+++ trunk/Source/_javascript_Core/b3/testb3_5.cpp 2019-09-22 07:19:43 UTC (rev 250198)
@@ -932,6 +932,78 @@
CHECK(invoke<int32_t>(*code, std::numeric_limits<int32_t>::max()) == std::numeric_limits<int32_t>::max());
}
+void testCheckAddRemoveCheckWithSExt8(int8_t value)
+{
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* arg = root->appendNew<Value>(proc, SExt32, Origin(), root->appendNew<Value>(proc, SExt8, Origin(), root->appendNew<Value>(proc, Trunc, Origin(), root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0))));
+ CheckValue* checkAdd = root->appendNew<CheckValue>(proc, CheckAdd, Origin(), arg, arg);
+ checkAdd->setGenerator(
+ [&] (CCallHelpers& jit, const StackmapGenerationParams&) {
+ AllowMacroScratchRegisterUsage allowScratch(jit);
+ jit.abortWithReason(B3Oops);
+ });
+ root->appendNewControlValue(proc, Return, Origin(), checkAdd);
+
+ auto code = compileProc(proc);
+
+ CHECK(invoke<int64_t>(*code, value) == 2ll * static_cast<int32_t>(value));
+}
+
+void testCheckAddRemoveCheckWithSExt16(int16_t value)
+{
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* arg = root->appendNew<Value>(proc, SExt32, Origin(), root->appendNew<Value>(proc, SExt16, Origin(), root->appendNew<Value>(proc, Trunc, Origin(), root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0))));
+ CheckValue* checkAdd = root->appendNew<CheckValue>(proc, CheckAdd, Origin(), arg, arg);
+ checkAdd->setGenerator(
+ [&] (CCallHelpers& jit, const StackmapGenerationParams&) {
+ AllowMacroScratchRegisterUsage allowScratch(jit);
+ jit.abortWithReason(B3Oops);
+ });
+ root->appendNewControlValue(proc, Return, Origin(), checkAdd);
+
+ auto code = compileProc(proc);
+
+ CHECK(invoke<int64_t>(*code, value) == 2ll * value);
+}
+
+void testCheckAddRemoveCheckWithSExt32(int32_t value)
+{
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* arg = root->appendNew<Value>(proc, SExt32, Origin(), root->appendNew<Value>(proc, Trunc, Origin(), root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)));
+ CheckValue* checkAdd = root->appendNew<CheckValue>(proc, CheckAdd, Origin(), arg, arg);
+ checkAdd->setGenerator(
+ [&] (CCallHelpers& jit, const StackmapGenerationParams&) {
+ AllowMacroScratchRegisterUsage allowScratch(jit);
+ jit.abortWithReason(B3Oops);
+ });
+ root->appendNewControlValue(proc, Return, Origin(), checkAdd);
+
+ auto code = compileProc(proc);
+
+ CHECK(invoke<int64_t>(*code, value) == 2ll * value);
+}
+
+void testCheckAddRemoveCheckWithZExt32(int32_t value)
+{
+ Procedure proc;
+ BasicBlock* root = proc.addBlock();
+ Value* arg = root->appendNew<Value>(proc, ZExt32, Origin(), root->appendNew<Value>(proc, Trunc, Origin(), root->appendNew<ArgumentRegValue>(proc, Origin(), GPRInfo::argumentGPR0)));
+ CheckValue* checkAdd = root->appendNew<CheckValue>(proc, CheckAdd, Origin(), arg, arg);
+ checkAdd->setGenerator(
+ [&] (CCallHelpers& jit, const StackmapGenerationParams&) {
+ AllowMacroScratchRegisterUsage allowScratch(jit);
+ jit.abortWithReason(B3Oops);
+ });
+ root->appendNewControlValue(proc, Return, Origin(), checkAdd);
+
+ auto code = compileProc(proc);
+
+ CHECK(invoke<uint64_t>(*code, value) == static_cast<uint64_t>(2 * static_cast<uint64_t>(static_cast<uint32_t>(value))));
+}
+
void testCheckSubImm()
{
Procedure proc;