[wasm-simd] Move shuffle tests into unittests
These are no longer tied to instruction-selector, so move them out into their own unittests. We can then remove the *ForTesting methods. Bug: v8:10696 Change-Id: I387cf38290d9602b011ee1d13ee5285ac660f208 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2326951 Reviewed-by: Bill Budge <bbudge@chromium.org> Commit-Queue: Zhi An Ng <zhin@chromium.org> Cr-Commit-Position: refs/heads/master@{#69149}
This commit is contained in:
parent
a03d0483bb
commit
2d395f6578
@ -3163,7 +3163,8 @@ void InstructionSelector::CanonicalizeShuffle(Node* node, uint8_t* shuffle,
|
||||
bool needs_swap;
|
||||
bool inputs_equal = GetVirtualRegister(node->InputAt(0)) ==
|
||||
GetVirtualRegister(node->InputAt(1));
|
||||
wasm::CanonicalizeShuffle(inputs_equal, shuffle, &needs_swap, is_swizzle);
|
||||
wasm::SimdShuffle::CanonicalizeShuffle(inputs_equal, shuffle, &needs_swap,
|
||||
is_swizzle);
|
||||
if (needs_swap) {
|
||||
SwapShuffleInputs(node);
|
||||
}
|
||||
@ -3182,38 +3183,27 @@ void InstructionSelector::SwapShuffleInputs(Node* node) {
|
||||
node->ReplaceInput(1, input0);
|
||||
}
|
||||
|
||||
void InstructionSelector::CanonicalizeShuffleForTesting(bool inputs_equal,
|
||||
uint8_t* shuffle,
|
||||
bool* needs_swap,
|
||||
bool* is_swizzle) {
|
||||
wasm::CanonicalizeShuffle(inputs_equal, shuffle, needs_swap, is_swizzle);
|
||||
}
|
||||
|
||||
bool InstructionSelector::TryMatchIdentityForTesting(const uint8_t* shuffle) {
|
||||
return TryMatchIdentity(shuffle);
|
||||
}
|
||||
|
||||
// static
|
||||
bool InstructionSelector::TryMatch32x4Shuffle(const uint8_t* shuffle,
|
||||
uint8_t* shuffle32x4) {
|
||||
return wasm::TryMatch32x4Shuffle(shuffle, shuffle32x4);
|
||||
return wasm::SimdShuffle::TryMatch32x4Shuffle(shuffle, shuffle32x4);
|
||||
}
|
||||
|
||||
// static
|
||||
bool InstructionSelector::TryMatch16x8Shuffle(const uint8_t* shuffle,
|
||||
uint8_t* shuffle16x8) {
|
||||
return wasm::TryMatch16x8Shuffle(shuffle, shuffle16x8);
|
||||
return wasm::SimdShuffle::TryMatch16x8Shuffle(shuffle, shuffle16x8);
|
||||
}
|
||||
|
||||
// static
|
||||
bool InstructionSelector::TryMatchConcat(const uint8_t* shuffle,
|
||||
uint8_t* offset) {
|
||||
return wasm::TryMatchConcat(shuffle, offset);
|
||||
return wasm::SimdShuffle::TryMatchConcat(shuffle, offset);
|
||||
}
|
||||
|
||||
// static
|
||||
bool InstructionSelector::TryMatchBlend(const uint8_t* shuffle) {
|
||||
return wasm::TryMatchBlend(shuffle);
|
||||
return wasm::SimdShuffle::TryMatchBlend(shuffle);
|
||||
}
|
||||
|
||||
// static
|
||||
|
@ -456,31 +456,6 @@ class V8_EXPORT_PRIVATE InstructionSelector final {
|
||||
return instr_origins_;
|
||||
}
|
||||
|
||||
// Expose these SIMD helper functions for testing.
|
||||
static void CanonicalizeShuffleForTesting(bool inputs_equal, uint8_t* shuffle,
|
||||
bool* needs_swap, bool* is_swizzle);
|
||||
|
||||
static bool TryMatchIdentityForTesting(const uint8_t* 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;
|
||||
|
||||
@ -665,14 +640,14 @@ class V8_EXPORT_PRIVATE InstructionSelector final {
|
||||
// [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) {
|
||||
return wasm::TryMatchIdentity(shuffle);
|
||||
return wasm::SimdShuffle::TryMatchIdentity(shuffle);
|
||||
}
|
||||
|
||||
// Tries to match a byte shuffle to a scalar splat operation. Returns the
|
||||
// index of the lane if successful.
|
||||
template <int LANES>
|
||||
static bool TryMatchDup(const uint8_t* shuffle, int* index) {
|
||||
return wasm::TryMatchSplat<LANES>(shuffle, index);
|
||||
return wasm::SimdShuffle::TryMatchSplat<LANES>(shuffle, index);
|
||||
}
|
||||
|
||||
// Tries to match an 8x16 byte shuffle to an equivalent 32x4 shuffle. If
|
||||
|
@ -10,8 +10,8 @@ namespace v8 {
|
||||
namespace internal {
|
||||
namespace wasm {
|
||||
|
||||
void CanonicalizeShuffle(bool inputs_equal, uint8_t* shuffle, bool* needs_swap,
|
||||
bool* is_swizzle) {
|
||||
void SimdShuffle::CanonicalizeShuffle(bool inputs_equal, uint8_t* shuffle,
|
||||
bool* needs_swap, bool* is_swizzle) {
|
||||
*needs_swap = false;
|
||||
// Inputs equal, then it's a swizzle.
|
||||
if (inputs_equal) {
|
||||
@ -51,14 +51,15 @@ void CanonicalizeShuffle(bool inputs_equal, uint8_t* shuffle, bool* needs_swap,
|
||||
}
|
||||
}
|
||||
|
||||
bool TryMatchIdentity(const uint8_t* shuffle) {
|
||||
bool SimdShuffle::TryMatchIdentity(const uint8_t* shuffle) {
|
||||
for (int i = 0; i < kSimd128Size; ++i) {
|
||||
if (shuffle[i] != i) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryMatch32x4Shuffle(const uint8_t* shuffle, uint8_t* shuffle32x4) {
|
||||
bool SimdShuffle::TryMatch32x4Shuffle(const uint8_t* shuffle,
|
||||
uint8_t* shuffle32x4) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
if (shuffle[i * 4] % 4 != 0) return false;
|
||||
for (int j = 1; j < 4; ++j) {
|
||||
@ -69,7 +70,8 @@ bool TryMatch32x4Shuffle(const uint8_t* shuffle, uint8_t* shuffle32x4) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryMatch16x8Shuffle(const uint8_t* shuffle, uint8_t* shuffle16x8) {
|
||||
bool SimdShuffle::TryMatch16x8Shuffle(const uint8_t* shuffle,
|
||||
uint8_t* shuffle16x8) {
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
if (shuffle[i * 2] % 2 != 0) return false;
|
||||
for (int j = 1; j < 2; ++j) {
|
||||
@ -80,7 +82,7 @@ bool TryMatch16x8Shuffle(const uint8_t* shuffle, uint8_t* shuffle16x8) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryMatchConcat(const uint8_t* shuffle, uint8_t* offset) {
|
||||
bool SimdShuffle::TryMatchConcat(const uint8_t* shuffle, uint8_t* offset) {
|
||||
// Don't match the identity shuffle (e.g. [0 1 2 ... 15]).
|
||||
uint8_t start = shuffle[0];
|
||||
if (start == 0) return false;
|
||||
@ -97,7 +99,7 @@ bool TryMatchConcat(const uint8_t* shuffle, uint8_t* offset) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryMatchBlend(const uint8_t* shuffle) {
|
||||
bool SimdShuffle::TryMatchBlend(const uint8_t* shuffle) {
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
if ((shuffle[i] & 0xF) != i) return false;
|
||||
}
|
||||
|
@ -5,54 +5,57 @@
|
||||
#ifndef V8_WASM_SIMD_SHUFFLE_H_
|
||||
#define V8_WASM_SIMD_SHUFFLE_H_
|
||||
|
||||
#include "src/base/macros.h"
|
||||
#include "src/common/globals.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
namespace wasm {
|
||||
|
||||
// Converts a shuffle into canonical form, meaning that the first lane index
|
||||
// is in the range [0 .. 15]. Set |inputs_equal| true if this is an explicit
|
||||
// swizzle. Returns canonicalized |shuffle|, |needs_swap|, and |is_swizzle|.
|
||||
// If |needs_swap| is true, inputs must be swapped. If |is_swizzle| is true,
|
||||
// the second input can be ignored.
|
||||
void CanonicalizeShuffle(bool inputs_equal, uint8_t* shuffle, bool* needs_swap,
|
||||
bool* is_swizzle);
|
||||
class V8_EXPORT_PRIVATE SimdShuffle {
|
||||
public:
|
||||
// Converts a shuffle into canonical form, meaning that the first lane index
|
||||
// is in the range [0 .. 15]. Set |inputs_equal| true if this is an explicit
|
||||
// swizzle. Returns canonicalized |shuffle|, |needs_swap|, and |is_swizzle|.
|
||||
// If |needs_swap| is true, inputs must be swapped. If |is_swizzle| is true,
|
||||
// the second input can be ignored.
|
||||
static void CanonicalizeShuffle(bool inputs_equal, uint8_t* shuffle,
|
||||
bool* needs_swap, bool* is_swizzle);
|
||||
|
||||
bool TryMatchIdentity(const uint8_t* shuffle);
|
||||
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>
|
||||
bool TryMatchSplat(const uint8_t* shuffle, int* index) {
|
||||
const int kBytesPerLane = kSimd128Size / LANES;
|
||||
// Get the first lane's worth of bytes and check that indices start at a
|
||||
// lane boundary and are consecutive.
|
||||
uint8_t lane0[kBytesPerLane];
|
||||
lane0[0] = shuffle[0];
|
||||
if (lane0[0] % kBytesPerLane != 0) return false;
|
||||
for (int i = 1; i < kBytesPerLane; ++i) {
|
||||
lane0[i] = shuffle[i];
|
||||
if (lane0[i] != lane0[0] + i) return false;
|
||||
}
|
||||
// Now check that the other lanes are identical to lane0.
|
||||
for (int i = 1; i < LANES; ++i) {
|
||||
for (int j = 0; j < kBytesPerLane; ++j) {
|
||||
if (lane0[j] != shuffle[i * kBytesPerLane + j]) return false;
|
||||
// Tries to match a byte shuffle to a scalar splat operation. Returns the
|
||||
// index of the lane if successful.
|
||||
template <int LANES>
|
||||
static bool TryMatchSplat(const uint8_t* shuffle, int* index) {
|
||||
const int kBytesPerLane = kSimd128Size / LANES;
|
||||
// Get the first lane's worth of bytes and check that indices start at a
|
||||
// lane boundary and are consecutive.
|
||||
uint8_t lane0[kBytesPerLane];
|
||||
lane0[0] = shuffle[0];
|
||||
if (lane0[0] % kBytesPerLane != 0) return false;
|
||||
for (int i = 1; i < kBytesPerLane; ++i) {
|
||||
lane0[i] = shuffle[i];
|
||||
if (lane0[i] != lane0[0] + i) return false;
|
||||
}
|
||||
// Now check that the other lanes are identical to lane0.
|
||||
for (int i = 1; i < LANES; ++i) {
|
||||
for (int j = 0; j < kBytesPerLane; ++j) {
|
||||
if (lane0[j] != shuffle[i * kBytesPerLane + j]) return false;
|
||||
}
|
||||
}
|
||||
*index = lane0[0] / kBytesPerLane;
|
||||
return true;
|
||||
}
|
||||
*index = lane0[0] / kBytesPerLane;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryMatch32x4Shuffle(const uint8_t* shuffle, uint8_t* shuffle32x4);
|
||||
static bool TryMatch32x4Shuffle(const uint8_t* shuffle, uint8_t* shuffle32x4);
|
||||
|
||||
bool TryMatch16x8Shuffle(const uint8_t* shuffle, uint8_t* shuffle16x8);
|
||||
static bool TryMatch16x8Shuffle(const uint8_t* shuffle, uint8_t* shuffle16x8);
|
||||
|
||||
bool TryMatchConcat(const uint8_t* shuffle, uint8_t* offset);
|
||||
|
||||
bool TryMatchBlend(const uint8_t* shuffle);
|
||||
static bool TryMatchConcat(const uint8_t* shuffle, uint8_t* offset);
|
||||
|
||||
static bool TryMatchBlend(const uint8_t* shuffle);
|
||||
};
|
||||
} // namespace wasm
|
||||
} // namespace internal
|
||||
} // namespace v8
|
||||
|
@ -318,6 +318,7 @@ v8_source_set("unittests_sources") {
|
||||
"wasm/leb-helper-unittest.cc",
|
||||
"wasm/loop-assignment-analysis-unittest.cc",
|
||||
"wasm/module-decoder-unittest.cc",
|
||||
"wasm/simd-shuffle-unittest.cc",
|
||||
"wasm/streaming-decoder-unittest.cc",
|
||||
"wasm/subtyping-unittest.cc",
|
||||
"wasm/wasm-code-manager-unittest.cc",
|
||||
|
@ -595,252 +595,6 @@ TARGET_TEST_F(InstructionSelectorTest, CallStubWithDeoptRecursiveFrameState) {
|
||||
EXPECT_EQ(index, s.size());
|
||||
}
|
||||
|
||||
// Helper to make calls to private InstructionSelector shuffle functions.
|
||||
class InstructionSelectorShuffleTest : public ::testing::Test {
|
||||
public:
|
||||
using Shuffle = std::array<uint8_t, kSimd128Size>;
|
||||
|
||||
struct TestShuffle {
|
||||
Shuffle non_canonical;
|
||||
Shuffle canonical;
|
||||
bool needs_swap;
|
||||
bool is_swizzle;
|
||||
};
|
||||
|
||||
// Call testing members in InstructionSelector.
|
||||
static void CanonicalizeShuffle(bool inputs_equal, Shuffle* shuffle,
|
||||
bool* needs_swap, bool* is_swizzle) {
|
||||
InstructionSelector::CanonicalizeShuffleForTesting(
|
||||
inputs_equal, &(*shuffle)[0], needs_swap, is_swizzle);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
};
|
||||
|
||||
bool operator==(const InstructionSelectorShuffleTest::Shuffle& a,
|
||||
const InstructionSelectorShuffleTest::Shuffle& b) {
|
||||
for (int i = 0; i < kSimd128Size; ++i) {
|
||||
if (a[i] != b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_F(InstructionSelectorShuffleTest, CanonicalizeShuffle) {
|
||||
const bool kInputsEqual = true;
|
||||
const bool kNeedsSwap = true;
|
||||
const bool kIsSwizzle = true;
|
||||
|
||||
bool needs_swap;
|
||||
bool is_swizzle;
|
||||
|
||||
// Test canonicalization driven by input shuffle.
|
||||
TestShuffle test_shuffles[] = {
|
||||
// Identity is canonical.
|
||||
{{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Non-canonical identity requires a swap.
|
||||
{{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// General shuffle, canonical is unchanged.
|
||||
{{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
!kNeedsSwap,
|
||||
!kIsSwizzle},
|
||||
// Non-canonical shuffle requires a swap.
|
||||
{{{16, 0, 17, 1, 18, 2, 19, 3, 20, 4, 21, 5, 22, 6, 23, 7}},
|
||||
{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
kNeedsSwap,
|
||||
!kIsSwizzle},
|
||||
};
|
||||
for (size_t i = 0; i < arraysize(test_shuffles); ++i) {
|
||||
Shuffle shuffle = test_shuffles[i].non_canonical;
|
||||
CanonicalizeShuffle(!kInputsEqual, &shuffle, &needs_swap, &is_swizzle);
|
||||
EXPECT_EQ(shuffle, test_shuffles[i].canonical);
|
||||
EXPECT_EQ(needs_swap, test_shuffles[i].needs_swap);
|
||||
EXPECT_EQ(is_swizzle, test_shuffles[i].is_swizzle);
|
||||
}
|
||||
|
||||
// Test canonicalization when inputs are equal (explicit swizzle).
|
||||
TestShuffle test_swizzles[] = {
|
||||
// Identity is canonical.
|
||||
{{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Non-canonical identity requires a swap.
|
||||
{{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Canonicalized to swizzle.
|
||||
{{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
{{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Canonicalized to swizzle.
|
||||
{{{16, 0, 17, 1, 18, 2, 19, 3, 20, 4, 21, 5, 22, 6, 23, 7}},
|
||||
{{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
};
|
||||
for (size_t i = 0; i < arraysize(test_swizzles); ++i) {
|
||||
Shuffle shuffle = test_swizzles[i].non_canonical;
|
||||
CanonicalizeShuffle(kInputsEqual, &shuffle, &needs_swap, &is_swizzle);
|
||||
EXPECT_EQ(shuffle, test_swizzles[i].canonical);
|
||||
EXPECT_EQ(needs_swap, test_swizzles[i].needs_swap);
|
||||
EXPECT_EQ(is_swizzle, test_swizzles[i].is_swizzle);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
257
test/unittests/wasm/simd-shuffle-unittest.cc
Normal file
257
test/unittests/wasm/simd-shuffle-unittest.cc
Normal file
@ -0,0 +1,257 @@
|
||||
// Copyright 2020 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "src/wasm/simd-shuffle.h"
|
||||
|
||||
#include "test/unittests/test-utils.h"
|
||||
|
||||
namespace v8 {
|
||||
namespace internal {
|
||||
namespace wasm {
|
||||
// Helper to make calls to private wasm shuffle functions.
|
||||
class SimdShuffleTest : public ::testing::Test {
|
||||
public:
|
||||
using Shuffle = std::array<uint8_t, kSimd128Size>;
|
||||
|
||||
struct TestShuffle {
|
||||
Shuffle non_canonical;
|
||||
Shuffle canonical;
|
||||
bool needs_swap;
|
||||
bool is_swizzle;
|
||||
};
|
||||
|
||||
// Call testing members in wasm.
|
||||
static void CanonicalizeShuffle(bool inputs_equal, Shuffle* shuffle,
|
||||
bool* needs_swap, bool* is_swizzle) {
|
||||
SimdShuffle::CanonicalizeShuffle(inputs_equal, &(*shuffle)[0], needs_swap,
|
||||
is_swizzle);
|
||||
}
|
||||
|
||||
static bool TryMatchIdentity(const Shuffle& shuffle) {
|
||||
return SimdShuffle::TryMatchIdentity(&shuffle[0]);
|
||||
}
|
||||
template <int LANES>
|
||||
static bool TryMatchSplat(const Shuffle& shuffle, int* index) {
|
||||
return SimdShuffle::TryMatchSplat<LANES>(&shuffle[0], index);
|
||||
}
|
||||
static bool TryMatch32x4Shuffle(const Shuffle& shuffle,
|
||||
uint8_t* shuffle32x4) {
|
||||
return SimdShuffle::TryMatch32x4Shuffle(&shuffle[0], shuffle32x4);
|
||||
}
|
||||
static bool TryMatch16x8Shuffle(const Shuffle& shuffle,
|
||||
uint8_t* shuffle16x8) {
|
||||
return SimdShuffle::TryMatch16x8Shuffle(&shuffle[0], shuffle16x8);
|
||||
}
|
||||
static bool TryMatchConcat(const Shuffle& shuffle, uint8_t* offset) {
|
||||
return SimdShuffle::TryMatchConcat(&shuffle[0], offset);
|
||||
}
|
||||
static bool TryMatchBlend(const Shuffle& shuffle) {
|
||||
return SimdShuffle::TryMatchBlend(&shuffle[0]);
|
||||
}
|
||||
};
|
||||
|
||||
bool operator==(const SimdShuffleTest::Shuffle& a,
|
||||
const SimdShuffleTest::Shuffle& b) {
|
||||
for (int i = 0; i < kSimd128Size; ++i) {
|
||||
if (a[i] != b[i]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
TEST_F(SimdShuffleTest, CanonicalizeShuffle) {
|
||||
const bool kInputsEqual = true;
|
||||
const bool kNeedsSwap = true;
|
||||
const bool kIsSwizzle = true;
|
||||
|
||||
bool needs_swap;
|
||||
bool is_swizzle;
|
||||
|
||||
// Test canonicalization driven by input shuffle.
|
||||
TestShuffle test_shuffles[] = {
|
||||
// Identity is canonical.
|
||||
{{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Non-canonical identity requires a swap.
|
||||
{{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// General shuffle, canonical is unchanged.
|
||||
{{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
!kNeedsSwap,
|
||||
!kIsSwizzle},
|
||||
// Non-canonical shuffle requires a swap.
|
||||
{{{16, 0, 17, 1, 18, 2, 19, 3, 20, 4, 21, 5, 22, 6, 23, 7}},
|
||||
{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
kNeedsSwap,
|
||||
!kIsSwizzle},
|
||||
};
|
||||
for (size_t i = 0; i < arraysize(test_shuffles); ++i) {
|
||||
Shuffle shuffle = test_shuffles[i].non_canonical;
|
||||
CanonicalizeShuffle(!kInputsEqual, &shuffle, &needs_swap, &is_swizzle);
|
||||
EXPECT_EQ(shuffle, test_shuffles[i].canonical);
|
||||
EXPECT_EQ(needs_swap, test_shuffles[i].needs_swap);
|
||||
EXPECT_EQ(is_swizzle, test_shuffles[i].is_swizzle);
|
||||
}
|
||||
|
||||
// Test canonicalization when inputs are equal (explicit swizzle).
|
||||
TestShuffle test_swizzles[] = {
|
||||
// Identity is canonical.
|
||||
{{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Non-canonical identity requires a swap.
|
||||
{{{16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}},
|
||||
{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Canonicalized to swizzle.
|
||||
{{{0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23}},
|
||||
{{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
// Canonicalized to swizzle.
|
||||
{{{16, 0, 17, 1, 18, 2, 19, 3, 20, 4, 21, 5, 22, 6, 23, 7}},
|
||||
{{0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}},
|
||||
!kNeedsSwap,
|
||||
kIsSwizzle},
|
||||
};
|
||||
for (size_t i = 0; i < arraysize(test_swizzles); ++i) {
|
||||
Shuffle shuffle = test_swizzles[i].non_canonical;
|
||||
CanonicalizeShuffle(kInputsEqual, &shuffle, &needs_swap, &is_swizzle);
|
||||
EXPECT_EQ(shuffle, test_swizzles[i].canonical);
|
||||
EXPECT_EQ(needs_swap, test_swizzles[i].needs_swap);
|
||||
EXPECT_EQ(is_swizzle, test_swizzles[i].is_swizzle);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(SimdShuffleTest, 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(SimdShuffleTest, TryMatchSplat) {
|
||||
int index;
|
||||
// All lanes from the same 32 bit source lane.
|
||||
EXPECT_TRUE(TryMatchSplat<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(TryMatchSplat<8>(
|
||||
{{4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7}}, &index));
|
||||
EXPECT_FALSE(TryMatchSplat<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(TryMatchSplat<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(TryMatchSplat<4>(
|
||||
{{16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17, 16, 17}},
|
||||
&index));
|
||||
EXPECT_FALSE(TryMatchSplat<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(TryMatchSplat<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(TryMatchSplat<4>(
|
||||
{{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}, &index));
|
||||
EXPECT_FALSE(TryMatchSplat<8>(
|
||||
{{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7}}, &index));
|
||||
}
|
||||
|
||||
TEST_F(SimdShuffleTest, 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(SimdShuffleTest, 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(SimdShuffleTest, 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(SimdShuffleTest, 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 wasm
|
||||
} // namespace internal
|
||||
} // namespace v8
|
Loading…
Reference in New Issue
Block a user