Skip to content

Commit fad9c39

Browse files
committed
[JSC] B3 LowerToAir's xor-and-rotate match should require a logical right shift and mask shift amounts
https://bugs.webkit.org/show_bug.cgi?id=328171 rdar://190502335 Reviewed by Tadeu Zagallo. We are returning int8_t without masking the constant shift amount with 63. As a result, large number exceeding int8_t (like 200) gets truncated into int8_t. For example, `VectorShl(x, 200)` will be seen as `VectorShl(x, -56)`, and it is the same to `VectorShr(x, 56)`. But the actual value was `VectorShl(x, 8)` as we take 200 & 63 (and that's the right behavior). Tests: Source/JavaScriptCore/b3/testb3_1.cpp Source/JavaScriptCore/b3/testb3_7.cpp * Source/JavaScriptCore/b3/B3LowerToAir.cpp: * Source/JavaScriptCore/b3/testb3.h: * Source/JavaScriptCore/b3/testb3_1.cpp: (run): * Source/JavaScriptCore/b3/testb3_7.cpp: (testVectorXorRotateRight64ShiftKindAndAmount): Canonical link: https://commits.webkit.org/324256@main
1 parent ae823d5 commit fad9c39

4 files changed

Lines changed: 83 additions & 17 deletions

File tree

‎Source/JavaScriptCore/b3/B3LowerToAir.cpp‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5547,33 +5547,27 @@ class LowerToAir {
55475547
return;
55485548
case B3::VectorOr: {
55495549
#if CPU(ARM64)
5550-
// Try to match XAR (XOR-and-rotate-right) pattern for i64x2.
5551-
// Pattern: VectorOr(VectorShl(x, constK), VectorShr(x, constK))
5552-
// or VectorOr(VectorAdd(x, x), VectorShr(x, constK)) after strength reduction.
5553-
// When x = VectorXor(a, b), emit XAR(a, b, rotateRight).
5550+
// Match XAR (XOR-and-rotate-right) for i64x2: VectorOr of a left shift and an unsigned
5551+
// right shift of the same x = VectorXor(a, b), with constant amounts summing to 64,
5552+
// becomes XAR(a, b, rotateRight). Strength reduction turns a left shift by 1 into
5553+
// VectorAdd(x, x), so that counts as a left shift too.
55545554
if (isARM64_SHA3()) {
55555555
Value* left = m_value->child(0);
55565556
Value* right = m_value->child(1);
55575557

55585558
auto tryMatchXAR = [&]() -> bool {
5559-
// Each child of VectorOr should be either:
5560-
// - VectorShl:i64x2(x, constK) — a left shift by constant
5561-
// - VectorShr:i64x2(x, constK) — a right shift by constant
5562-
// - VectorAdd:i64x2(x, x) — equivalent to shl-by-1 (from strength reduction)
5563-
// We need one left-shift and one right-shift on the same value x,
5564-
// with shift amounts summing to 64.
5565-
55665559
struct ShiftInfo {
55675560
Value* shiftedValue { nullptr };
55685561
int8_t amount { 0 }; // positive = left shift, negative = right shift
55695562
};
55705563

55715564
auto extractShift = [&](Value* v) -> std::optional<ShiftInfo> {
5565+
// Vector shifts use the shift amount modulo the lane width.
55725566
if (v->opcode() == B3::VectorShl && v->as<SIMDValue>()->simdLane() == SIMDLane::i64x2 && v->child(1)->hasInt32())
5573-
return ShiftInfo { v->child(0), static_cast<int8_t>(v->child(1)->asInt32()) };
5567+
return ShiftInfo { v->child(0), static_cast<int8_t>(v->child(1)->asInt32() & 63) };
55745568

5575-
if (v->opcode() == B3::VectorShr && v->as<SIMDValue>()->simdLane() == SIMDLane::i64x2 && v->child(1)->hasInt32())
5576-
return ShiftInfo { v->child(0), static_cast<int8_t>(-v->child(1)->asInt32()) };
5569+
if (v->opcode() == B3::VectorShr && v->as<SIMDValue>()->simdLane() == SIMDLane::i64x2 && v->as<SIMDValue>()->signMode() == SIMDSignMode::Unsigned && v->child(1)->hasInt32())
5570+
return ShiftInfo { v->child(0), static_cast<int8_t>(-(v->child(1)->asInt32() & 63)) };
55775571

55785572
// We lowered left-shift by 1 to VectorAdd(x, x)
55795573
if (v->opcode() == B3::VectorAdd && v->as<SIMDValue>()->simdLane() == SIMDLane::i64x2
@@ -5645,8 +5639,8 @@ class LowerToAir {
56455639
}
56465640
case B3::VectorXor: {
56475641
#if CPU(ARM64)
5648-
// VectorXor(VectorXor(a, b), c) → EOR3(a, b, c)
5649-
// VectorXor(a, VectorXor(b, c)) → EOR3(b, c, a)
5642+
// VectorXor(VectorXor(a, b), c) -> EOR3(a, b, c)
5643+
// VectorXor(a, VectorXor(b, c)) -> EOR3(b, c, a)
56505644
if (isARM64_SHA3()) {
56515645
for (unsigned childIdx = 0; childIdx < 2; ++childIdx) {
56525646
Value* inner = m_value->child(childIdx);

‎Source/JavaScriptCore/b3/testb3.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,7 @@ void testFCCmpNegatedAndDouble(double, double, double, double);
15681568

15691569
// SIMD XOR+rotate pattern matching
15701570
void testVectorXorRotateRight64();
1571+
void testVectorXorRotateRight64ShiftKindAndAmount();
15711572
void testVectorXor3();
15721573
void testVectorDotProductSplatOne();
15731574
void testVectorShrZipToExtend();

‎Source/JavaScriptCore/b3/testb3_1.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,7 @@ void run(const TestConfig* config)
15671567
RUN(testVectorFmulByElementFloat());
15681568
RUN(testVectorFmulByElementDouble());
15691569
RUN(testVectorXorRotateRight64());
1570+
RUN(testVectorXorRotateRight64ShiftKindAndAmount());
15701571
RUN(testVectorXor3());
15711572
RUN(testVectorShlImmediate());
15721573
RUN(testVectorShrImmediate());

‎Source/JavaScriptCore/b3/testb3_7.cpp‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3946,7 +3946,77 @@ void testVectorXorRotateRight64()
39463946
}
39473947
}
39483948

3949-
// Test dot(v, splat(1)) → extadd_pairwise strength reduction.
3949+
// VectorOr of two shifts of VectorXor(a, b) whose amounts sum to 64 only before or only after masking to
3950+
// the lane width, or that pair a left shift with a signed right shift. Only some of these are rotates.
3951+
void testVectorXorRotateRight64ShiftKindAndAmount()
3952+
{
3953+
struct Shift {
3954+
B3::Opcode opcode;
3955+
SIMDSignMode signMode;
3956+
int32_t amount;
3957+
};
3958+
3959+
auto apply = [](Shift shift, uint64_t value) -> uint64_t {
3960+
unsigned amount = shift.amount & 63;
3961+
if (shift.opcode == VectorShl)
3962+
return value << amount;
3963+
if (shift.signMode == SIMDSignMode::Signed)
3964+
return static_cast<uint64_t>(static_cast<int64_t>(value) >> amount);
3965+
return value >> amount;
3966+
};
3967+
3968+
struct TestCase {
3969+
Shift left;
3970+
Shift right;
3971+
};
3972+
const TestCase testCases[] = {
3973+
{ { VectorShl, SIMDSignMode::Unsigned, 8 }, { VectorShr, SIMDSignMode::Signed, 56 } },
3974+
{ { VectorShr, SIMDSignMode::Signed, 56 }, { VectorShl, SIMDSignMode::Unsigned, 8 } },
3975+
{ { VectorShl, SIMDSignMode::Unsigned, 200 }, { VectorShl, SIMDSignMode::Unsigned, 8 } },
3976+
{ { VectorShl, SIMDSignMode::Unsigned, 72 }, { VectorShr, SIMDSignMode::Unsigned, 56 } },
3977+
{ { VectorShl, SIMDSignMode::Unsigned, 8 }, { VectorShr, SIMDSignMode::Unsigned, 120 } },
3978+
};
3979+
3980+
for (const auto& testCase : testCases) {
3981+
Procedure proc;
3982+
BasicBlock* root = proc.addBlock();
3983+
auto arguments = cCallArgumentValues<void*>(proc, root);
3984+
Value* address = arguments[0];
3985+
3986+
Value* a = root->appendNew<MemoryValue>(proc, Load, V128, Origin(), address);
3987+
Value* b = root->appendNew<MemoryValue>(proc, Load, V128, Origin(), address, static_cast<int32_t>(sizeof(v128_t)));
3988+
Value* xorResult = root->appendNew<SIMDValue>(proc, Origin(), VectorXor, B3::V128, SIMDLane::v128, SIMDSignMode::None, a, b);
3989+
3990+
auto emitShift = [&](Shift shift) {
3991+
Value* amount = root->appendNew<Const32Value>(proc, Origin(), shift.amount);
3992+
return root->appendNew<SIMDValue>(proc, Origin(), shift.opcode, B3::V128, SIMDLane::i64x2, shift.signMode, xorResult, amount);
3993+
};
3994+
Value* left = emitShift(testCase.left);
3995+
Value* right = emitShift(testCase.right);
3996+
Value* orResult = root->appendNew<SIMDValue>(proc, Origin(), VectorOr, B3::V128, SIMDLane::v128, SIMDSignMode::None, left, right);
3997+
3998+
root->appendNew<MemoryValue>(proc, Store, Origin(), orResult, address, static_cast<int32_t>(2 * sizeof(v128_t)));
3999+
root->clearSuccessors();
4000+
root->appendNew<Value>(proc, Return, Origin());
4001+
4002+
auto code = compileProc(proc);
4003+
4004+
alignas(16) v128_t vectors[3];
4005+
vectors[0].u64x2[0] = 0x0123456789ABCDEFULL;
4006+
vectors[0].u64x2[1] = 0x7EDCBA9876543210ULL;
4007+
vectors[1].u64x2[0] = 0x8000000000000001ULL;
4008+
vectors[1].u64x2[1] = 0x8000000000000001ULL;
4009+
4010+
invoke<void>(*code, vectors);
4011+
4012+
for (unsigned lane = 0; lane < 2; ++lane) {
4013+
uint64_t x = vectors[0].u64x2[lane] ^ vectors[1].u64x2[lane];
4014+
CHECK_EQ(vectors[2].u64x2[lane], apply(testCase.left, x) | apply(testCase.right, x));
4015+
}
4016+
}
4017+
}
4018+
4019+
// Test dot(v, splat(1)) -> extadd_pairwise strength reduction.
39504020
// i32x4.dot_i16x8_s(v, {1,1,...}) = i32x4.extadd_pairwise_i16x8_s(v)
39514021
void testVectorDotProductSplatOne()
39524022
{

0 commit comments

Comments
 (0)