Skip to content

Commit 858b529

Browse files
committed
[JSC] B3 ReduceStrength should check the zip lane before turning a vector shift of a zip into an extend
https://bugs.webkit.org/show_bug.cgi?id=328172 rdar://190503732 Reviewed by Keith Miller. We have a comment. // Supported combinations: // shr i16x8 by 8 + zip i8x16 -> i8->i16 sign extension // shr i32x4 by 16 + zip i16x8 -> i16->i32 sign extension // shr i64x2 by 32 + zip i32x4 -> i32->i64 sign extension But zip lane size is not checked. This patch fixes it. Tests: Source/JavaScriptCore/b3/testb3_1.cpp Source/JavaScriptCore/b3/testb3_7.cpp * Source/JavaScriptCore/b3/B3ReduceStrength.cpp: * Source/JavaScriptCore/b3/testb3.h: * Source/JavaScriptCore/b3/testb3_1.cpp: (run): * Source/JavaScriptCore/b3/testb3_7.cpp: (testVectorShrZipToExtend): (testVectorShrZipWithMismatchedLane): Canonical link: https://commits.webkit.org/324248@main
1 parent df5a49f commit 858b529

4 files changed

Lines changed: 98 additions & 31 deletions

File tree

‎Source/JavaScriptCore/b3/B3ReduceStrength.cpp‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,18 +3726,15 @@ class ReduceStrength {
37263726
// shr i32x4 by 16 + zip i16x8 -> i16->i32 sign extension
37273727
// shr i64x2 by 32 + zip i32x4 -> i32->i64 sign extension
37283728
SIMDValue* shr = m_value->as<SIMDValue>();
3729-
if (shr->signMode() == SIMDSignMode::Signed) {
3730-
SIMDLane lane = shr->simdLane();
3731-
bool matches = (lane == SIMDLane::i16x8 && m_value->child(1)->isInt32(8))
3732-
|| (lane == SIMDLane::i32x4 && m_value->child(1)->isInt32(16))
3733-
|| (lane == SIMDLane::i64x2 && m_value->child(1)->isInt32(32));
3734-
if (matches) {
3735-
Value* child0 = m_value->child(0);
3736-
if ((child0->opcode() == VectorZipLower || child0->opcode() == VectorZipHigher) && child0->child(0) == child0->child(1)) {
3737-
Opcode extendOp = child0->opcode() == VectorZipLower ? VectorExtendLow : VectorExtendHigh;
3738-
replaceWithNew<SIMDValue>(m_value->origin(), extendOp, B3::V128, lane, SIMDSignMode::Signed, child0->child(0));
3739-
break;
3740-
}
3729+
Value* zip = m_value->child(0);
3730+
if (shr->signMode() == SIMDSignMode::Signed
3731+
&& (zip->opcode() == VectorZipLower || zip->opcode() == VectorZipHigher)
3732+
&& zip->child(0) == zip->child(1)) {
3733+
SIMDLane zipLane = zip->as<SIMDValue>()->simdLane();
3734+
if (wideningLaneFor(zipLane) == shr->simdLane() && m_value->child(1)->isInt32(elementByteSize(zipLane) * 8)) {
3735+
Opcode extendOp = zip->opcode() == VectorZipLower ? VectorExtendLow : VectorExtendHigh;
3736+
replaceWithNew<SIMDValue>(m_value->origin(), extendOp, B3::V128, shr->simdLane(), SIMDSignMode::Signed, zip->child(0));
3737+
break;
37413738
}
37423739
}
37433740
break;

‎Source/JavaScriptCore/b3/testb3.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,7 @@ void testVectorDotProductSplatOne();
15731573
void testVectorShrZipToExtend();
15741574
void testVectorShrZipToExtendI32();
15751575
void testVectorShrZipToExtendI64();
1576+
void testVectorShrZipWithMismatchedLane();
15761577

15771578
// SIMD VectorUnzip/Zip/Transpose/Reverse B3 opcodes
15781579
void testVectorUnzipEven();

‎Source/JavaScriptCore/b3/testb3_1.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,7 @@ void run(const TestConfig* config)
15571557
RUN(testVectorShrZipToExtend());
15581558
RUN(testVectorShrZipToExtendI32());
15591559
RUN(testVectorShrZipToExtendI64());
1560+
RUN(testVectorShrZipWithMismatchedLane());
15601561
RUN_UNARY(testVectorXorOrAllOnesConstantToVectorAndXor, v128Operands());
15611562
RUN_UNARY(testVectorXorAndAllOnesConstantToVectorOrXor, v128Operands());
15621563
RUN_BINARY(testVectorOrConstants, v128Operands(), v128Operands());

‎Source/JavaScriptCore/b3/testb3_7.cpp‎

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6099,10 +6099,10 @@ void testVectorSwizzleBinaryOnlyOneSideSide1Scattered()
60996099
runBinarySwizzleAndCheck(pattern, a, b);
61006100
}
61016101

6102-
// Test VectorShr(VectorZip{Lower,Higher}(x, x), 8) → VectorExtend{Low,High}(x, i16x8, Signed)
6102+
// Test VectorShr(VectorZip{Lower,Higher}(x, x), 8) -> VectorExtend{Low,High}(x, i16x8, Signed)
61036103
void testVectorShrZipToExtend()
61046104
{
6105-
// Test ExtendLow: VectorShr(VectorZipLower(x, x), 8) → VectorExtendLow(x, i16x8, Signed)
6105+
// Test ExtendLow: VectorShr(VectorZipLower(x, x), 8) -> VectorExtendLow(x, i16x8, Signed)
61066106
{
61076107
alignas(16) v128_t vectors[2];
61086108
Procedure proc;
@@ -6125,14 +6125,14 @@ void testVectorShrZipToExtend()
61256125
auto code = compileProc(proc);
61266126

61276127
// Input i8x16: {-5, 127, -128, 0, 1, -1, 42, -42, 99, 100, 101, 102, 103, 104, 105, 106}
6128-
vectors[0].u8x16[0] = static_cast<uint8_t>(-5); // 0xFB → sign-extend to 0xFFFB = -5
6129-
vectors[0].u8x16[1] = 127; // 0x7F → 0x007F = 127
6130-
vectors[0].u8x16[2] = static_cast<uint8_t>(-128); // 0x80 → 0xFF80 = -128
6131-
vectors[0].u8x16[3] = 0; // 0x00 → 0x0000 = 0
6132-
vectors[0].u8x16[4] = 1; // 0x01 → 0x0001 = 1
6133-
vectors[0].u8x16[5] = static_cast<uint8_t>(-1); // 0xFF → 0xFFFF = -1
6134-
vectors[0].u8x16[6] = 42; // 0x2A → 0x002A = 42
6135-
vectors[0].u8x16[7] = static_cast<uint8_t>(-42); // 0xD6 → 0xFFD6 = -42
6128+
vectors[0].u8x16[0] = static_cast<uint8_t>(-5); // 0xFB -> sign-extend to 0xFFFB = -5
6129+
vectors[0].u8x16[1] = 127; // 0x7F -> 0x007F = 127
6130+
vectors[0].u8x16[2] = static_cast<uint8_t>(-128); // 0x80 -> 0xFF80 = -128
6131+
vectors[0].u8x16[3] = 0; // 0x00 -> 0x0000 = 0
6132+
vectors[0].u8x16[4] = 1; // 0x01 -> 0x0001 = 1
6133+
vectors[0].u8x16[5] = static_cast<uint8_t>(-1); // 0xFF -> 0xFFFF = -1
6134+
vectors[0].u8x16[6] = 42; // 0x2A -> 0x002A = 42
6135+
vectors[0].u8x16[7] = static_cast<uint8_t>(-42); // 0xD6 -> 0xFFD6 = -42
61366136
// Upper 8 bytes (not used by ExtendLow, but fill them)
61376137
for (unsigned i = 8; i < 16; ++i)
61386138
vectors[0].u8x16[i] = 99 + (i - 8);
@@ -6150,7 +6150,7 @@ void testVectorShrZipToExtend()
61506150
CHECK(vectors[1].u16x8[7] == static_cast<uint16_t>(-42));
61516151
}
61526152

6153-
// Test ExtendHigh: VectorShr(VectorZipHigher(x, x), 8) → VectorExtendHigh(x, i16x8, Signed)
6153+
// Test ExtendHigh: VectorShr(VectorZipHigher(x, x), 8) -> VectorExtendHigh(x, i16x8, Signed)
61546154
{
61556155
alignas(16) v128_t vectors[2];
61566156
Procedure proc;
@@ -6175,14 +6175,14 @@ void testVectorShrZipToExtend()
61756175
// Input: lower 8 bytes don't matter for ExtendHigh, upper 8 bytes are tested
61766176
for (unsigned i = 0; i < 8; ++i)
61776177
vectors[0].u8x16[i] = 0;
6178-
vectors[0].u8x16[8] = static_cast<uint8_t>(-10); // 0xF6 → -10
6179-
vectors[0].u8x16[9] = 50; // 0x32 → 50
6180-
vectors[0].u8x16[10] = static_cast<uint8_t>(-128); // 0x80 → -128
6181-
vectors[0].u8x16[11] = 127; // 0x7F → 127
6182-
vectors[0].u8x16[12] = 0; // 0x00 → 0
6183-
vectors[0].u8x16[13] = static_cast<uint8_t>(-1); // 0xFF → -1
6184-
vectors[0].u8x16[14] = 1; // 0x01 → 1
6185-
vectors[0].u8x16[15] = static_cast<uint8_t>(-100); // 0x9C → -100
6178+
vectors[0].u8x16[8] = static_cast<uint8_t>(-10); // 0xF6 -> -10
6179+
vectors[0].u8x16[9] = 50; // 0x32 -> 50
6180+
vectors[0].u8x16[10] = static_cast<uint8_t>(-128); // 0x80 -> -128
6181+
vectors[0].u8x16[11] = 127; // 0x7F -> 127
6182+
vectors[0].u8x16[12] = 0; // 0x00 -> 0
6183+
vectors[0].u8x16[13] = static_cast<uint8_t>(-1); // 0xFF -> -1
6184+
vectors[0].u8x16[14] = 1; // 0x01 -> 1
6185+
vectors[0].u8x16[15] = static_cast<uint8_t>(-100); // 0x9C -> -100
61866186

61876187
invoke<void>(*code, vectors);
61886188

@@ -6358,6 +6358,74 @@ void testVectorShrZipToExtendI64()
63586358
}
63596359
}
63606360

6361+
// VectorShr(VectorZip{Lower,Higher}(x, x), shift) is a sign extension only when the zip lane is
6362+
// exactly half the width of the shift lane.
6363+
void testVectorShrZipWithMismatchedLane()
6364+
{
6365+
auto runOne = [](B3::Opcode zipOp, SIMDLane zipLane, SIMDLane shrLane, int32_t shiftAmount) {
6366+
alignas(16) v128_t vectors[2];
6367+
Procedure proc;
6368+
BasicBlock* root = proc.addBlock();
6369+
auto arguments = cCallArgumentValues<void*>(proc, root);
6370+
Value* address = arguments[0];
6371+
Value* input = root->appendNew<MemoryValue>(proc, Load, V128, Origin(), address);
6372+
Value* zipped = root->appendNew<SIMDValue>(proc, Origin(), zipOp, B3::V128, zipLane, SIMDSignMode::None, input, input);
6373+
Value* shifted = root->appendNew<SIMDValue>(proc, Origin(), VectorShr, B3::V128, shrLane, SIMDSignMode::Signed, zipped, root->appendNew<Const32Value>(proc, Origin(), shiftAmount));
6374+
root->appendNew<MemoryValue>(proc, Store, Origin(), shifted, address, static_cast<int32_t>(sizeof(v128_t)));
6375+
root->appendNew<Value>(proc, Return, Origin());
6376+
6377+
auto code = compileProc(proc);
6378+
6379+
for (unsigned i = 0; i < 16; ++i)
6380+
vectors[0].u8x16[i] = static_cast<uint8_t>(i * 0x37 + 0x91);
6381+
v128_t source = vectors[0];
6382+
6383+
unsigned zipBytes = elementByteSize(zipLane);
6384+
unsigned halfCount = 16 / zipBytes / 2;
6385+
unsigned base = zipOp == VectorZipLower ? 0 : halfCount;
6386+
v128_t zippedExpected;
6387+
for (unsigned i = 0; i < halfCount; ++i) {
6388+
for (unsigned j = 0; j < 2; ++j)
6389+
memcpySpan(std::span { zippedExpected.u8x16 }.subspan((2 * i + j) * zipBytes, zipBytes), std::span { source.u8x16 }.subspan((base + i) * zipBytes, zipBytes));
6390+
}
6391+
6392+
invoke<void>(*code, vectors);
6393+
6394+
unsigned shrBytes = elementByteSize(shrLane);
6395+
for (unsigned lane = 0; lane < 16 / shrBytes; ++lane) {
6396+
int64_t element = 0;
6397+
for (unsigned byte = shrBytes; byte--;)
6398+
element = (element << 8) | zippedExpected.u8x16[lane * shrBytes + byte];
6399+
unsigned unusedBits = 64 - shrBytes * 8;
6400+
int64_t expected = (static_cast<int64_t>(static_cast<uint64_t>(element) << unusedBits) >> unusedBits) >> shiftAmount;
6401+
uint64_t actual = 0;
6402+
for (unsigned byte = shrBytes; byte--;)
6403+
actual = (actual << 8) | vectors[1].u8x16[lane * shrBytes + byte];
6404+
uint64_t mask = shrBytes == 8 ? std::numeric_limits<uint64_t>::max() : (1ULL << (shrBytes * 8)) - 1;
6405+
CHECK_EQ(actual, static_cast<uint64_t>(expected) & mask);
6406+
}
6407+
};
6408+
6409+
struct ShrCase {
6410+
SIMDLane shrLane;
6411+
int32_t shiftAmount;
6412+
SIMDLane matchingZipLane;
6413+
};
6414+
constexpr ShrCase shrCases[] = {
6415+
{ SIMDLane::i16x8, 8, SIMDLane::i8x16 },
6416+
{ SIMDLane::i32x4, 16, SIMDLane::i16x8 },
6417+
{ SIMDLane::i64x2, 32, SIMDLane::i32x4 },
6418+
};
6419+
for (auto shrCase : shrCases) {
6420+
for (SIMDLane zipLane : { SIMDLane::i8x16, SIMDLane::i16x8, SIMDLane::i32x4 }) {
6421+
if (zipLane == shrCase.matchingZipLane)
6422+
continue;
6423+
for (B3::Opcode zipOp : { VectorZipLower, VectorZipHigher })
6424+
runOne(zipOp, zipLane, shrCase.shrLane, shrCase.shiftAmount);
6425+
}
6426+
}
6427+
}
6428+
63616429
#endif // ENABLE(B3_JIT)
63626430

63636431
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END

0 commit comments

Comments
 (0)