Skip to content

Commit 2eb82b3

Browse files
committed
[JSC] Truncate int32 multiplications by narrow integer typed array loads
https://bugs.webkit.org/show_bug.cgi?id=327584 rdar://190051724 Reviewed by Marcus Plutowski. When we have a code like `(i * data[i]) | 0` (the result is represented as int32), it can be computed with a wrapping 32-bit multiply only if it is exact as a double. This can be proven if one operand is within 2^22. We can leverage Int8, Uint8, Uint8Clamped, Int16 or Uint16 array result information to make it weaken. Graph::isWithinPowerOfTwo bounds an integer typed array load by |x| < 2^(8 * elementSize), so Int32 and Uint32 array loads also satisfy the 2^32 bound which backwards propagation uses for additions. This patch changes the pipeline, 1. Now fixup phase does not change ArithMode. We should weaken it in strength reduction phase. This weakening is relying on backward propagation, and it is fragile to do it at fixup phase. 2. Backwards propagation no longer folds "the product may be inexact" into NodeBytecodeUsesAsNumber of ArithMul. Backwards propagation runs once in the bytecode parser, before fixup decides array modes, and node flags are only ever merged, so a bit set there could never be withdrawn once a narrow typed array load proves the product exact. Instead, Graph::mulProductIsExact computes exactness from the operands wherever it is needed: Graph::mulShouldSpeculateInt32 uses it so that an overflowing, inexact multiply is not speculated as Int32 (which would keep OSR exiting), and strength reduction uses it, after fixup, to relax the multiply to Arith::Unchecked. Test: JSTests/stress/arith-mul-narrow-typed-array-load-truncation.js * JSTests/stress/arith-mul-narrow-typed-array-load-truncation.js: Added. (shouldBe): (hash): (referenceHash): (mulByLoad): (mulByLoadWithAnyIndex): * Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp: (JSC::DFG::BackwardsPropagationPhase::propagate): (JSC::DFG::BackwardsPropagationPhase::isWithinPowerOfTwoForConstant): Deleted. (JSC::DFG::BackwardsPropagationPhase::isWithinPowerOfTwo): Deleted. * Source/JavaScriptCore/dfg/DFGFixupPhase.cpp: (JSC::DFG::FixupPhase::fixupArithDivInt32): (JSC::DFG::FixupPhase::fixupArithDiv): (JSC::DFG::FixupPhase::fixupArithMul): (JSC::DFG::FixupPhase::fixupNode): (JSC::DFG::FixupPhase::fixupToNumberOrToNumericOrCallNumberConstructor): (JSC::DFG::FixupPhase::attemptToMakeIntegerAdd): * Source/JavaScriptCore/dfg/DFGGraph.h: * Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp: * Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp: (JSC::DFG::StrengthReductionPhase::hasBytecodeUseFlags const): (JSC::DFG::StrengthReductionPhase::relaxArithMode): (JSC::DFG::StrengthReductionPhase::relaxDivOrModArithMode): (JSC::DFG::StrengthReductionPhase::handleNode): Canonical link: https://commits.webkit.org/324148@main
1 parent 61f2d9a commit 2eb82b3

6 files changed

Lines changed: 282 additions & 171 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error('bad value: ' + actual + ' expected: ' + expected);
4+
}
5+
6+
function hash(array, scale) {
7+
var sum = 0;
8+
for (var i = 0; i < array.length; i++)
9+
sum ^= (i * scale * array[i]) | 0;
10+
return sum;
11+
}
12+
noInline(hash);
13+
14+
function referenceHash(array, scale) {
15+
var sum = 0;
16+
for (var i = 0; i < array.length; i++)
17+
sum ^= Number(BigInt.asIntN(32, BigInt(i * scale) * BigInt(array[i])));
18+
return sum;
19+
}
20+
21+
function mulByLoad(array, index, value) {
22+
return (value * array[index]) | 0;
23+
}
24+
noInline(mulByLoad);
25+
26+
var uint8 = new Uint8Array(64);
27+
var int16 = new Int16Array(64);
28+
for (var i = 0; i < 64; ++i) {
29+
uint8[i] = (i * 37) & 0xff;
30+
int16[i] = (i * 7919) & 0xffff;
31+
}
32+
33+
var expectedProducts = [];
34+
for (var i = 0; i < 64; ++i)
35+
expectedProducts.push(Number(BigInt.asIntN(32, 0x7fffffffn * BigInt(uint8[i]))));
36+
var indexStrings = [];
37+
for (var i = 0; i < 64; ++i)
38+
indexStrings.push(String(i));
39+
var expectedUint8Hash = referenceHash(uint8, 3);
40+
var expectedInt16Hash = referenceHash(int16, 30000001);
41+
for (var i = 0; i < testLoopCount; ++i) {
42+
shouldBe(hash(uint8, 3), expectedUint8Hash);
43+
shouldBe(hash(int16, 30000001), expectedInt16Hash);
44+
shouldBe(mulByLoad(uint8, i & 63, 0x7fffffff), expectedProducts[i & 63]);
45+
}
46+
47+
// A non-int32 index refines the load into a generic access even though only a Uint8Array was
48+
// profiled. The product must then stay exact for arbitrary int32 elements.
49+
function mulByLoadWithAnyIndex(array, index, value) {
50+
return (value * array[index]) | 0;
51+
}
52+
noInline(mulByLoadWithAnyIndex);
53+
54+
for (var i = 0; i < testLoopCount; ++i) {
55+
shouldBe(mulByLoadWithAnyIndex(uint8, i & 63, 3), (3 * uint8[i & 63]) | 0);
56+
shouldBe(mulByLoadWithAnyIndex(uint8, indexStrings[i & 63], 3), (3 * uint8[i & 63]) | 0);
57+
}
58+
59+
var wide = [0x7fffffff, -0x80000000, 0x12345678];
60+
for (var i = 0; i < testLoopCount; ++i) {
61+
var index = i % 3;
62+
shouldBe(mulByLoadWithAnyIndex(wide, index, 0x7fffffff), (0x7fffffff * wide[index]) | 0);
63+
}

‎Source/JavaScriptCore/dfg/DFGBackwardsPropagationPhase.cpp‎

Lines changed: 8 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -164,81 +164,6 @@ class BackwardsPropagationPhase : public Phase {
164164
return (value || 1.0 / value < 0.0);
165165
}
166166

167-
// Tests if the absolute value is strictly less than the power of two.
168-
template<int power>
169-
bool isWithinPowerOfTwoForConstant(Node* node)
170-
{
171-
JSValue immediateValue = node->asJSValue();
172-
if (!immediateValue.isNumber())
173-
return false;
174-
double immediate = immediateValue.asNumber();
175-
return isInteger(immediate) && immediate > -(static_cast<int64_t>(1) << power) && immediate < (static_cast<int64_t>(1) << power);
176-
}
177-
178-
template<int power>
179-
bool isWithinPowerOfTwo(Node* node)
180-
{
181-
switch (node->op()) {
182-
case DoubleConstant:
183-
case JSConstant:
184-
case Int52Constant: {
185-
return isWithinPowerOfTwoForConstant<power>(node);
186-
}
187-
188-
case ValueBitAnd:
189-
case ArithBitAnd: {
190-
if (power > 31)
191-
return true;
192-
193-
// (x & mask) is only bounded by |mask| when mask is non-negative; a negative
194-
// mask preserves the sign bit and the result can span the full int32 range.
195-
auto isNonNegativeMaskWithinPower = [](Node* operand) {
196-
if (!operand->isNumberConstant())
197-
return false;
198-
double immediate = operand->asNumber();
199-
return immediate >= 0 && immediate < (static_cast<int64_t>(1) << power);
200-
};
201-
return isNonNegativeMaskWithinPower(node->child1().node())
202-
|| isNonNegativeMaskWithinPower(node->child2().node());
203-
}
204-
205-
case ArithBitOr:
206-
case ArithBitXor:
207-
case ValueBitOr:
208-
case ValueBitXor:
209-
case ValueBitLShift:
210-
case ArithBitLShift: {
211-
return power > 31;
212-
}
213-
214-
case ArithBitRShift:
215-
case ValueBitRShift:
216-
case ArithBitURShift:
217-
case ValueBitURShift: {
218-
if (power > 31)
219-
return true;
220-
221-
Node* shiftAmount = node->child2().node();
222-
if (!shiftAmount->isNumberConstant())
223-
return false;
224-
JSValue immediateValue = shiftAmount->asJSValue();
225-
if (!immediateValue.isInt32())
226-
return false;
227-
// Shifting right by k = (count & 31) leaves |result| < 2^(32 - k), for both >> and >>>.
228-
return (immediateValue.asInt32() & 31) > 31 - power;
229-
}
230-
231-
default:
232-
return false;
233-
}
234-
}
235-
236-
template<int power>
237-
bool isWithinPowerOfTwo(Edge edge)
238-
{
239-
return isWithinPowerOfTwo<power>(edge.node());
240-
}
241-
242167
static bool LEGACY_NODELETE mergeFlags(NodeFlags& flagsRef, NodeFlags newFlags)
243168
{
244169
return checkAndSet(flagsRef, flagsRef | newFlags);
@@ -442,7 +367,7 @@ class BackwardsPropagationPhase : public Phase {
442367
flags &= ~NodeBytecodeNeedsNegZero;
443368
if (node->child1()->hasNumericResult() || node->child2()->hasNumericResult() || node->child1()->hasNumberResult() || node->child2()->hasNumberResult())
444369
flags &= ~NodeBytecodeUsesAsOther;
445-
if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
370+
if (!m_graph.isWithinPowerOfTwo<32>(node->child1()) && !m_graph.isWithinPowerOfTwo<32>(node->child2()))
446371
flags |= NodeBytecodeUsesAsNumber;
447372
if (!m_allowNestedOverflowingAdditions)
448373
flags |= NodeBytecodeUsesAsNumber;
@@ -457,7 +382,7 @@ class BackwardsPropagationPhase : public Phase {
457382
flags &= ~NodeBytecodeUsesAsOther;
458383
if (isNotNegZero(node->child1().node()) || isNotNegZero(node->child2().node()))
459384
flags &= ~NodeBytecodeNeedsNegZero;
460-
if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
385+
if (!m_graph.isWithinPowerOfTwo<32>(node->child1()) && !m_graph.isWithinPowerOfTwo<32>(node->child2()))
461386
flags |= NodeBytecodeUsesAsNumber;
462387
if (!m_allowNestedOverflowingAdditions)
463388
flags |= NodeBytecodeUsesAsNumber;
@@ -479,7 +404,7 @@ class BackwardsPropagationPhase : public Phase {
479404
flags &= ~NodeBytecodeUsesAsOther;
480405
if (isNotNegZero(node->child1().node()) || isNotPosZero(node->child2().node()))
481406
flags &= ~NodeBytecodeNeedsNegZero;
482-
if (!isWithinPowerOfTwo<32>(node->child1()) && !isWithinPowerOfTwo<32>(node->child2()))
407+
if (!m_graph.isWithinPowerOfTwo<32>(node->child1()) && !m_graph.isWithinPowerOfTwo<32>(node->child2()))
483408
flags |= NodeBytecodeUsesAsNumber;
484409
if (!m_allowNestedOverflowingAdditions)
485410
flags |= NodeBytecodeUsesAsNumber;
@@ -502,7 +427,7 @@ class BackwardsPropagationPhase : public Phase {
502427
case Dec: {
503428
flags &= ~NodeBytecodeNeedsNegZero;
504429
flags &= ~NodeBytecodeUsesAsOther;
505-
if (!isWithinPowerOfTwo<32>(node->child1()))
430+
if (!m_graph.isWithinPowerOfTwo<32>(node->child1()))
506431
flags |= NodeBytecodeUsesAsNumber;
507432
if (!m_allowNestedOverflowingAdditions)
508433
flags |= NodeBytecodeUsesAsNumber;
@@ -517,14 +442,9 @@ class BackwardsPropagationPhase : public Phase {
517442
// As soon as a multiply happens, we can easily end up in the part
518443
// of the double domain where the point at which you do truncation
519444
// can change the outcome. So, ArithMul always forces its inputs to
520-
// check for overflow. Additionally, it will have to check for overflow
521-
// itself unless we can prove that there is no way for the values
522-
// produced to cause double rounding.
523-
524-
if (!isWithinPowerOfTwo<22>(node->child1().node())
525-
&& !isWithinPowerOfTwo<22>(node->child2().node()))
526-
flags |= NodeBytecodeUsesAsNumber;
527-
445+
// check for overflow. Whether the multiply itself can be truncated
446+
// also depends on its product being exact, which Graph::mulProductIsExact
447+
// decides from the operands when the multiply is speculated.
528448
node->mergeFlags(flags);
529449

530450
flags |= NodeBytecodeUsesAsNumber | NodeBytecodeNeedsNegZero | NodeBytecodeNeedsNaNOrInfinity;
@@ -673,6 +593,7 @@ class BackwardsPropagationPhase : public Phase {
673593
}
674594

675595
case Identity:
596+
case ValueRep:
676597
ASSERT(m_graph.afterFixup());
677598
node->child1()->mergeFlags(flags);
678599
break;

0 commit comments

Comments
 (0)