[ir] Add tests for compound operators.

This CL adds tests for the compound assignment builder_impl. The ops
which can not be compound assigned have been moved to report diagnostics
messages.

Bug: tint:1718
Change-Id: Ia3dbbf706f0f074fd988a2b1c01f3fb5cc602c04
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/131661
Auto-Submit: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
dan sinclair 2023-05-04 20:49:45 +00:00 committed by Dawn LUCI CQ
parent eae9902c81
commit c9734160bc
2 changed files with 246 additions and 18 deletions

View File

@ -377,24 +377,6 @@ void BuilderImpl::EmitCompoundAssignment(const ast::CompoundAssignmentStatement*
case ast::BinaryOp::kXor:
inst = builder.Xor(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kEqual:
inst = builder.Equal(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kNotEqual:
inst = builder.NotEqual(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kLessThan:
inst = builder.LessThan(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kGreaterThan:
inst = builder.GreaterThan(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kLessThanEqual:
inst = builder.LessThanEqual(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kGreaterThanEqual:
inst = builder.GreaterThanEqual(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kShiftLeft:
inst = builder.ShiftLeft(ty, lhs.Get(), rhs.Get());
break;
@ -416,6 +398,12 @@ void BuilderImpl::EmitCompoundAssignment(const ast::CompoundAssignmentStatement*
case ast::BinaryOp::kModulo:
inst = builder.Modulo(ty, lhs.Get(), rhs.Get());
break;
case ast::BinaryOp::kLessThanEqual:
case ast::BinaryOp::kGreaterThanEqual:
case ast::BinaryOp::kGreaterThan:
case ast::BinaryOp::kLessThan:
case ast::BinaryOp::kNotEqual:
case ast::BinaryOp::kEqual:
case ast::BinaryOp::kLogicalAnd:
case ast::BinaryOp::kLogicalOr:
TINT_ICE(IR, diagnostics_) << "invalid compound assignment";

View File

@ -44,6 +44,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Add) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundAdd) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kAdd);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = add %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Subtract) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Sub(Call("my_func"), 4_u);
@ -62,6 +86,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Subtract) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundSubtract) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kSubtract);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = sub %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Multiply) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Mul(Call("my_func"), 4_u);
@ -80,6 +128,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Multiply) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundMultiply) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kMultiply);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = mul %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Div) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Div(Call("my_func"), 4_u);
@ -98,6 +170,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Div) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundDiv) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kDivide);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = div %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Modulo) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Mod(Call("my_func"), 4_u);
@ -116,6 +212,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Modulo) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundModulo) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kModulo);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = mod %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_And) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = And(Call("my_func"), 4_u);
@ -134,6 +254,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_And) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundAnd) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.bool_());
auto* expr = CompoundAssign("v1", false, ast::BinaryOp::kAnd);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, bool, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, bool, read_write> = and %1:ref<private, bool, read_write>, false
store %1:ref<private, bool, read_write>, %2:ref<private, bool, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Or) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Or(Call("my_func"), 4_u);
@ -152,6 +296,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Or) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundOr) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.bool_());
auto* expr = CompoundAssign("v1", false, ast::BinaryOp::kOr);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, bool, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, bool, read_write> = or %1:ref<private, bool, read_write>, false
store %1:ref<private, bool, read_write>, %2:ref<private, bool, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Xor) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Xor(Call("my_func"), 4_u);
@ -170,6 +338,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Xor) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundXor) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kXor);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = xor %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_LogicalAnd) {
Func("my_func", utils::Empty, ty.bool_(), utils::Vector{Return(true)});
auto* expr = LogicalAnd(Call("my_func"), false);
@ -367,6 +559,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_ShiftLeft) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundShiftLeft) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kShiftLeft);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = shiftl %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_ShiftRight) {
Func("my_func", utils::Empty, ty.u32(), utils::Vector{Return(0_u)});
auto* expr = Shr(Call("my_func"), 4_u);
@ -385,6 +601,30 @@ TEST_F(IR_BuilderImplTest, EmitExpression_Binary_ShiftRight) {
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_CompoundShiftRight) {
GlobalVar("v1", builtin::AddressSpace::kPrivate, ty.u32());
auto* expr = CompoundAssign("v1", 1_u, ast::BinaryOp::kShiftRight);
WrapInFunction(expr);
auto r = Build();
ASSERT_TRUE(r) << Error();
auto m = r.Move();
EXPECT_EQ(Disassemble(m), R"(%fn0 = block
%1:ref<private, u32, read_write> = var private read_write
%fn1 = func test_function():void [@compute @workgroup_size(1, 1, 1)]
%fn2 = block
%2:ref<private, u32, read_write> = shiftr %1:ref<private, u32, read_write>, 1u
store %1:ref<private, u32, read_write>, %2:ref<private, u32, read_write>
ret
func_end
)");
}
TEST_F(IR_BuilderImplTest, EmitExpression_Binary_Compound) {
Func("my_func", utils::Empty, ty.f32(), utils::Vector{Return(0_f)});