Commit 6afa2110 authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

[wasm simd] Add unit tests for shuffle matching methods.

- Adds some unit tests for InstructionSelector::TryMatch* methods.
- Adds a TryMatchIdentity method. We should detect identity shuffles
  and emit no code in that case.

Bug: v8:6020
Change-Id: I5dea84738bf87db7112eb7d19f91b1e6b20811c7
Reviewed-on: https://chromium-review.googlesource.com/1116058
Commit-Queue: Bill Budge <bbudge@chromium.org>
Reviewed-by: 's avatarJaroslav Sevcik <jarin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54097}
parent 6c585ef0
......@@ -2892,6 +2892,14 @@ FrameStateDescriptor* InstructionSelector::GetFrameStateDescriptor(
state_info.shared_info(), outer_state);
}
// static
bool InstructionSelector::TryMatchIdentity(const uint8_t* shuffle) {
for (int i = 0; i < kSimd128Size; ++i) {
if (shuffle[i] != i) return false;
}
return true;
}
// static
bool InstructionSelector::TryMatch32x4Shuffle(const uint8_t* shuffle,
uint8_t* shuffle32x4) {
......
......@@ -448,6 +448,30 @@ class V8_EXPORT_PRIVATE InstructionSelector final {
return instr_origins_;
}
// Expose these SIMD helper functions for testing.
static bool TryMatchIdentityForTesting(const uint8_t* shuffle) {
return TryMatchIdentity(shuffle);
}
template <int LANES>
static bool TryMatchDupForTesting(const uint8_t* shuffle, int* index) {
return TryMatchDup<LANES>(shuffle, index);
}
static bool TryMatch32x4ShuffleForTesting(const uint8_t* shuffle,
uint8_t* shuffle32x4) {
return TryMatch32x4Shuffle(shuffle, shuffle32x4);
}
static bool TryMatch16x8ShuffleForTesting(const uint8_t* shuffle,
uint8_t* shuffle16x8) {
return TryMatch16x8Shuffle(shuffle, shuffle16x8);
}
static bool TryMatchConcatForTesting(const uint8_t* shuffle,
uint8_t* offset) {
return TryMatchConcat(shuffle, offset);
}
static bool TryMatchBlendForTesting(const uint8_t* shuffle) {
return TryMatchBlend(shuffle);
}
private:
friend class OperandGenerator;
......@@ -607,6 +631,11 @@ class V8_EXPORT_PRIVATE InstructionSelector final {
// ============= Vector instruction (SIMD) helper fns. =======================
// ===========================================================================
// Tries to match an 8x16 byte shuffle to the identity shuffle, which is
// [0 1 ... 15]. This should be called after canonicalizing the shuffle, so
// the second identity shuffle, [16 17 .. 31] is converted to the first one.
static bool TryMatchIdentity(const uint8_t* shuffle);
// Tries to match a byte shuffle to a scalar splat operation. Returns the
// index of the lane if successful.
template <int LANES>
......
......@@ -618,6 +618,164 @@ TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeoptRecursiveFrameState) {
EXPECT_EQ(index, s.size());
}
// Helper to make calls to private InstructionSelector::TryMatch* functions.
class InstructionSelectorShuffleTest : public ::testing::Test {
public:
using Shuffle = std::array<uint8_t, kSimd128Size>;
// Call private members
static bool TryMatchIdentity(const Shuffle& shuffle) {
return InstructionSelector::TryMatchIdentityForTesting(&shuffle[0]);
}
template <int LANES>
static bool TryMatchDup(const Shuffle& shuffle, int* index) {
return InstructionSelector::TryMatchDupForTesting<LANES>(&shuffle[0],
index);
}
static bool TryMatch32x4Shuffle(const Shuffle& shuffle,
uint8_t* shuffle32x4) {
return InstructionSelector::TryMatch32x4ShuffleForTesting(&shuffle[0],
shuffle32x4);
}
static bool TryMatch16x8Shuffle(const Shuffle& shuffle,
uint8_t* shuffle16x8) {
return InstructionSelector::TryMatch16x8ShuffleForTesting(&shuffle[0],
shuffle16x8);
}
static bool TryMatchConcat(const Shuffle& shuffle, uint8_t* offset) {
return InstructionSelector::TryMatchConcatForTesting(&shuffle[0], offset);
}
static bool TryMatchBlend(const Shuffle& shuffle) {
return InstructionSelector::TryMatchBlendForTesting(&shuffle[0]);
}
};
TEST_F(InstructionSelectorShuffleTest, TryMatchIdentity) {
// Match shuffle that returns first source operand.
EXPECT_TRUE(TryMatchIdentity(
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}));
// The non-canonicalized identity shuffle doesn't match.
EXPECT_FALSE(TryMatchIdentity(
{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}));
// Even one lane out of place is not an identity shuffle.
EXPECT_FALSE(TryMatchIdentity(
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 31}}));
}
TEST_F(InstructionSelectorShuffleTest, TryMatchDup) {
int index;
// All lanes from the same 32 bit source lane.
EXPECT_TRUE(TryMatchDup<4>({{4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}},
&index));
EXPECT_EQ(1, index);
// It shouldn't match for other vector shapes.
EXPECT_FALSE(TryMatchDup<8>(
{{4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}}, &index));
EXPECT_FALSE(TryMatchDup<16>(
{{4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}}, &index));
// All lanes from the same 16 bit source lane.
EXPECT_TRUE(TryMatchDup<8>(
{{16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17}},
&index));
EXPECT_EQ(8, index);
// It shouldn't match for other vector shapes.
EXPECT_FALSE(TryMatchDup<4>(
{{16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17}},
&index));
EXPECT_FALSE(TryMatchDup<16>(
{{16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17}},
&index));
// All lanes from the same 8 bit source lane.
EXPECT_TRUE(TryMatchDup<16>(
{{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}, &index));
EXPECT_EQ(7, index);
// It shouldn't match for other vector shapes.
EXPECT_FALSE(TryMatchDup<4>(
{{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}, &index));
EXPECT_FALSE(TryMatchDup<8>(
{{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}, &index));
}
TEST_F(InstructionSelectorShuffleTest, TryMatchConcat) {
uint8_t offset;
// Ascending indices, jump at end to same input (concatenating swizzle).
EXPECT_TRUE(TryMatchConcat(
{{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2}}, &offset));
EXPECT_EQ(3, offset);
// Ascending indices, jump at end to other input (concatenating shuffle).
EXPECT_TRUE(TryMatchConcat(
{{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}}, &offset));
EXPECT_EQ(4, offset);
// Shuffles that should not match:
// Ascending indices, but jump isn't at end/beginning.
EXPECT_FALSE(TryMatchConcat(
{{3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 6}}, &offset));
// Ascending indices, but multiple jumps.
EXPECT_FALSE(TryMatchConcat(
{{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3}}, &offset));
}
TEST_F(InstructionSelectorShuffleTest, TryMatch32x4Shuffle) {
uint8_t shuffle32x4[4];
// Match if each group of 4 bytes is from the same 32 bit lane.
EXPECT_TRUE(TryMatch32x4Shuffle(
{{12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 16, 17, 18, 19}},
shuffle32x4));
EXPECT_EQ(3, shuffle32x4[0]);
EXPECT_EQ(2, shuffle32x4[1]);
EXPECT_EQ(1, shuffle32x4[2]);
EXPECT_EQ(4, shuffle32x4[3]);
// Bytes must be in order in the 32 bit lane.
EXPECT_FALSE(TryMatch32x4Shuffle(
{{12, 13, 14, 14, 8, 9, 10, 11, 4, 5, 6, 7, 16, 17, 18, 19}},
shuffle32x4));
// Each group must start with the first byte in the 32 bit lane.
EXPECT_FALSE(TryMatch32x4Shuffle(
{{13, 14, 15, 12, 8, 9, 10, 11, 4, 5, 6, 7, 16, 17, 18, 19}},
shuffle32x4));
}
TEST_F(InstructionSelectorShuffleTest, TryMatch16x8Shuffle) {
uint8_t shuffle16x8[8];
// Match if each group of 2 bytes is from the same 16 bit lane.
EXPECT_TRUE(TryMatch16x8Shuffle(
{{12, 13, 30, 31, 8, 9, 26, 27, 4, 5, 22, 23, 16, 17, 2, 3}},
shuffle16x8));
EXPECT_EQ(6, shuffle16x8[0]);
EXPECT_EQ(15, shuffle16x8[1]);
EXPECT_EQ(4, shuffle16x8[2]);
EXPECT_EQ(13, shuffle16x8[3]);
EXPECT_EQ(2, shuffle16x8[4]);
EXPECT_EQ(11, shuffle16x8[5]);
EXPECT_EQ(8, shuffle16x8[6]);
EXPECT_EQ(1, shuffle16x8[7]);
// Bytes must be in order in the 16 bit lane.
EXPECT_FALSE(TryMatch16x8Shuffle(
{{12, 13, 30, 30, 8, 9, 26, 27, 4, 5, 22, 23, 16, 17, 2, 3}},
shuffle16x8));
// Each group must start with the first byte in the 16 bit lane.
EXPECT_FALSE(TryMatch16x8Shuffle(
{{12, 13, 31, 30, 8, 9, 26, 27, 4, 5, 22, 23, 16, 17, 2, 3}},
shuffle16x8));
}
TEST_F(InstructionSelectorShuffleTest, TryMatchBlend) {
// Match if each byte remains in place.
EXPECT_TRUE(TryMatchBlend(
{{0, 17, 2, 19, 4, 21, 6, 23, 8, 25, 10, 27, 12, 29, 14, 31}}));
// Identity is a blend.
EXPECT_TRUE(
TryMatchBlend({{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}}));
// Even one lane out of place is not a blend.
EXPECT_FALSE(TryMatchBlend(
{{1, 17, 2, 19, 4, 21, 6, 23, 8, 25, 10, 27, 12, 29, 14, 31}}));
}
} // namespace compiler
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment