Commit 894bf6df authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

[wasm-simd][scalar-lowering] Fix lowering of narrowing

Narrowing operations need to sign extend the result.

E.g. for narrowing uint16 to uint8, we compare uint16 to uint8 max,
0xff. The final result should be 0xffffffff (sign extended) since we
try to keep nodes in their sign extended form, to work well with
the rest of the lowering operations.

With this, we pass the last spec test (that is not ignored),
simd_conversions.

Bug: v8:10507
Change-Id: I8914fd69db9378b8244cba5dcacff98d36893649
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2436613Reviewed-by: 's avatarBill Budge <bbudge@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#70272}
parent c5f960a8
......@@ -1003,25 +1003,28 @@ void SimdScalarLowering::LowerPack(Node* node, SimdType input_rep_type,
const Operator* less_op = machine()->Int32LessThan();
Node* min = nullptr;
Node* max = nullptr;
const Operator* sign_extend;
MachineRepresentation phi_rep;
if (output_rep_type == SimdType::kInt16x8) {
sign_extend = machine()->SignExtendWord16ToInt32();
DCHECK(input_rep_type == SimdType::kInt32x4);
if (is_signed) {
min = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::min());
max = mcgraph_->Int32Constant(std::numeric_limits<int16_t>::max());
} else {
min = mcgraph_->Int32Constant(std::numeric_limits<uint16_t>::min());
min = mcgraph_->Uint32Constant(std::numeric_limits<uint16_t>::min());
max = mcgraph_->Uint32Constant(std::numeric_limits<uint16_t>::max());
}
phi_rep = MachineRepresentation::kWord16;
} else {
sign_extend = machine()->SignExtendWord8ToInt32();
DCHECK(output_rep_type == SimdType::kInt8x16 &&
input_rep_type == SimdType::kInt16x8);
if (is_signed) {
min = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::min());
max = mcgraph_->Int32Constant(std::numeric_limits<int8_t>::max());
} else {
min = mcgraph_->Int32Constant(std::numeric_limits<uint8_t>::min());
min = mcgraph_->Uint32Constant(std::numeric_limits<uint8_t>::min());
max = mcgraph_->Uint32Constant(std::numeric_limits<uint8_t>::max());
}
phi_rep = MachineRepresentation::kWord8;
......@@ -1037,7 +1040,10 @@ void SimdScalarLowering::LowerPack(Node* node, SimdType input_rep_type,
Diamond d_min(graph(), common(), graph()->NewNode(less_op, input, min));
input = d_min.Phi(phi_rep, min, input);
Diamond d_max(graph(), common(), graph()->NewNode(less_op, max, input));
rep_node[i] = d_max.Phi(phi_rep, max, input);
// We keep nodes in sign-extended form. E.g. for uint8_t, we need to
// compare with 0x000000ff (saturated narrowing), but the result of
// conversion should be 0xffffffff to work well with the rest of lowering.
rep_node[i] = graph()->NewNode(sign_extend, d_max.Phi(phi_rep, max, input));
}
ReplaceNode(node, rep_node, num_lanes);
}
......@@ -2255,7 +2261,7 @@ void SimdScalarLowering::Int32ToSmallerInt(Node** replacements, Node** result) {
for (int j = 0; j < num_ints; j++) {
result[num_ints * i + j] = graph()->NewNode(
sign_extend,
graph()->NewNode(machine()->Word32Sar(), replacements[i],
graph()->NewNode(machine()->Word32Shr(), replacements[i],
mcgraph_->Int32Constant(j * bit_size)));
}
} else {
......
......@@ -273,6 +273,21 @@ WASM_SIMD_TEST(V128_I64_PARAMS) {
CHECK_EQ(0, r.Call(0));
}
WASM_SIMD_TEST(I8x16WidenS_I16x8NarrowU) {
// Test any_true lowring with splats of different shapes.
{
WasmRunner<int32_t, int16_t> r(execution_tier, lower_simd);
BUILD(r, WASM_SIMD_I16x8_SPLAT(WASM_GET_LOCAL(0)),
WASM_SIMD_I16x8_SPLAT(WASM_GET_LOCAL(0)),
WASM_SIMD_OP(kExprI8x16UConvertI16x8),
WASM_SIMD_OP(kExprI16x8SConvertI8x16Low),
WASM_SIMD_OP(kExprI32x4ExtractLane), TO_BYTE(0));
CHECK_EQ(bit_cast<int32_t>(0xffffffff), r.Call(0x7fff));
}
}
} // namespace test_run_wasm_simd
} // namespace wasm
} // namespace internal
......
......@@ -36,11 +36,6 @@
# This test requires the reftypes flag to be disabled.
'proposals/bulk-memory-operations/imports': [FAIL],
# SIMD test cases
# Scalar lowering is incomplete, we skip these and selectively enable as
# we finish the implementation, see v8:10507.
'proposals/simd/simd_conversions' : [PASS, FAIL],
}], # ALWAYS
['arch == arm and not simulator_run', {
......
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