[ir][spirv-writer] Implement binary subtract

Bug: tint:1906
Change-Id: Ie57fecbafd4823de78c0b8d3322865253eb4c39b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132743
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2023-05-17 00:17:31 +00:00 committed by Dawn LUCI CQ
parent 6eed684a8b
commit a6d8e81371
2 changed files with 147 additions and 3 deletions

View File

@ -85,11 +85,151 @@ OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Add_Chain) {
TEST_F(SpvGeneratorImplTest, Binary_Sub_I32) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
auto* a = b.Add(mod.types.Get<type::I32>(), b.Constant(1_i), b.Constant(2_i));
func->start_target->instructions.Push(
b.Subtract(mod.types.Get<type::I32>(), b.Constant(1_i), b.Constant(2_i)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%6 = OpTypeInt 32 1
%7 = OpConstant %6 1
%8 = OpConstant %6 2
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpISub %6 %7 %8
OpReturn
OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Sub_U32) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
func->start_target->instructions.Push(
b.Subtract(mod.types.Get<type::U32>(), b.Constant(1_u), b.Constant(2_u)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%6 = OpTypeInt 32 0
%7 = OpConstant %6 1
%8 = OpConstant %6 2
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpISub %6 %7 %8
OpReturn
OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Sub_F32) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
func->start_target->instructions.Push(
b.Subtract(mod.types.Get<type::F32>(), b.Constant(1_f), b.Constant(2_f)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%6 = OpTypeFloat 32
%7 = OpConstant %6 1
%8 = OpConstant %6 2
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpFSub %6 %7 %8
OpReturn
OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Sub_Vec2i) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
auto* lhs = mod.constants.Create<constant::Composite>(
mod.types.Get<type::Vector>(mod.types.Get<type::I32>(), 2u),
utils::Vector{b.Constant(42_i)->value, b.Constant(-1_i)->value}, false, false);
auto* rhs = mod.constants.Create<constant::Composite>(
mod.types.Get<type::Vector>(mod.types.Get<type::I32>(), 2u),
utils::Vector{b.Constant(0_i)->value, b.Constant(-43_i)->value}, false, false);
func->start_target->instructions.Push(
b.Subtract(mod.types.Get<type::Vector>(mod.types.Get<type::I32>(), 2u), b.Constant(lhs),
b.Constant(rhs)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%7 = OpTypeInt 32 1
%6 = OpTypeVector %7 2
%9 = OpConstant %7 42
%10 = OpConstant %7 -1
%8 = OpConstantComposite %6 %9 %10
%12 = OpConstant %7 0
%13 = OpConstant %7 -43
%11 = OpConstantComposite %6 %12 %13
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpISub %6 %8 %11
OpReturn
OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Sub_Vec4f) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
auto* lhs = mod.constants.Create<constant::Composite>(
mod.types.Get<type::Vector>(mod.types.Get<type::F32>(), 4u),
utils::Vector{b.Constant(42_f)->value, b.Constant(-1_f)->value, b.Constant(0_f)->value,
b.Constant(1.25_f)->value},
false, false);
auto* rhs = mod.constants.Create<constant::Composite>(
mod.types.Get<type::Vector>(mod.types.Get<type::F32>(), 4u),
utils::Vector{b.Constant(0_f)->value, b.Constant(1.25_f)->value, b.Constant(-42_f)->value,
b.Constant(1_f)->value},
false, false);
func->start_target->instructions.Push(
b.Subtract(mod.types.Get<type::Vector>(mod.types.Get<type::F32>(), 4u), b.Constant(lhs),
b.Constant(rhs)));
generator_.EmitFunction(func);
EXPECT_EQ(DumpModule(generator_.Module()), R"(OpName %1 "foo"
%2 = OpTypeVoid
%3 = OpTypeFunction %2
%7 = OpTypeFloat 32
%6 = OpTypeVector %7 4
%9 = OpConstant %7 42
%10 = OpConstant %7 -1
%11 = OpConstant %7 0
%12 = OpConstant %7 1.25
%8 = OpConstantComposite %6 %9 %10 %11 %12
%14 = OpConstant %7 -42
%15 = OpConstant %7 1
%13 = OpConstantComposite %6 %11 %12 %14 %15
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpFSub %6 %8 %13
OpReturn
OpFunctionEnd
)");
}
TEST_F(SpvGeneratorImplTest, Binary_Chain) {
auto* func = b.CreateFunction(mod.symbols.Register("foo"), mod.types.Get<type::Void>());
b.Branch(func->start_target, func->end_target);
auto* a = b.Subtract(mod.types.Get<type::I32>(), b.Constant(1_i), b.Constant(2_i));
func->start_target->instructions.Push(a);
func->start_target->instructions.Push(b.Add(mod.types.Get<type::I32>(), a, a));
@ -102,7 +242,7 @@ TEST_F(SpvGeneratorImplTest, Binary_Add_Chain) {
%8 = OpConstant %6 2
%1 = OpFunction %2 None %3
%4 = OpLabel
%5 = OpIAdd %6 %7 %8
%5 = OpISub %6 %7 %8
%9 = OpIAdd %6 %5 %5
OpReturn
OpFunctionEnd

View File

@ -271,6 +271,10 @@ uint32_t GeneratorImplIr::EmitBinary(const ir::Binary* binary) {
op = binary->Type()->is_integer_scalar_or_vector() ? spv::Op::OpIAdd : spv::Op::OpFAdd;
break;
}
case ir::Binary::Kind::kSubtract: {
op = binary->Type()->is_integer_scalar_or_vector() ? spv::Op::OpISub : spv::Op::OpFSub;
break;
}
default: {
TINT_ICE(Writer, diagnostics_)
<< "unimplemented binary instruction: " << static_cast<uint32_t>(binary->kind);