From c73d67397624cab512a1bd680370a146a2640a3a Mon Sep 17 00:00:00 2001 From: Antonio Maiorano Date: Fri, 23 Dec 2022 21:09:10 +0000 Subject: [PATCH] tint: add const eval vector accessor unit tests Bug: tint:1581 Change-Id: I135c0cdc5d45c673aa156cd96ebad5c613545bd2 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/115504 Kokoro: Kokoro Reviewed-by: David Neto Commit-Queue: Antonio Maiorano --- src/tint/resolver/const_eval_indexing_test.cc | 6 +- .../resolver/const_eval_member_access_test.cc | 73 +++++++++++++++++++ src/tint/resolver/resolver_test_helper.h | 1 + 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/tint/resolver/const_eval_indexing_test.cc b/src/tint/resolver/const_eval_indexing_test.cc index 1b0edc44f2..9229f983e7 100644 --- a/src/tint/resolver/const_eval_indexing_test.cc +++ b/src/tint/resolver/const_eval_indexing_test.cc @@ -106,9 +106,9 @@ std::vector SwizzleCases() { C(Vec(T(0), T(1), T(2)), "xx", Vec(T(0), T(0))), C(Vec(T(0), T(1), T(2)), "yy", Vec(T(1), T(1))), C(Vec(T(0), T(1), T(2)), "zz", Vec(T(2), T(2))), - C(Vec(T(0), T(1), T(2)), "x", Vec(T(0))), - C(Vec(T(0), T(1), T(2)), "y", Vec(T(1))), - C(Vec(T(0), T(1), T(2)), "z", Vec(T(2))), + C(Vec(T(0), T(1), T(2)), "x", Val(T(0))), + C(Vec(T(0), T(1), T(2)), "y", Val(T(1))), + C(Vec(T(0), T(1), T(2)), "z", Val(T(2))), }; } INSTANTIATE_TEST_SUITE_P(Swizzle, diff --git a/src/tint/resolver/const_eval_member_access_test.cc b/src/tint/resolver/const_eval_member_access_test.cc index e6a66abe2b..f9f0127ed7 100644 --- a/src/tint/resolver/const_eval_member_access_test.cc +++ b/src/tint/resolver/const_eval_member_access_test.cc @@ -172,5 +172,78 @@ INSTANTIATE_TEST_SUITE_P( // ArrayAccessCases()))); } // namespace ArrayAccess +namespace VectorAccess { +struct Case { + Value input; +}; +static Case C(Value input) { + return Case{std::move(input)}; +} +static std::ostream& operator<<(std::ostream& o, const Case& c) { + return o << "input: " << c.input; +} + +using ResolverConstEvalVectorAccessTest = ResolverTestWithParam; +TEST_P(ResolverConstEvalVectorAccessTest, Test) { + Enable(ast::Extension::kF16); + + auto& param = GetParam(); + auto* expr = param.input.Expr(*this); + auto* a = Const("a", expr); + + utils::Vector index_accessors; + for (size_t i = 0; i < param.input.args.Length(); ++i) { + auto* index = IndexAccessor("a", Expr(i32(i))); + index_accessors.Push(index); + } + + utils::Vector stmts; + stmts.Push(WrapInStatement(a)); + for (auto* ia : index_accessors) { + stmts.Push(WrapInStatement(ia)); + } + WrapInFunction(std::move(stmts)); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto* sem = Sem().Get(expr); + ASSERT_NE(sem, nullptr); + auto* vec = sem->Type()->As(); + ASSERT_NE(vec, nullptr); + + EXPECT_TYPE(sem->ConstantValue()->Type(), sem->Type()); + for (size_t i = 0; i < index_accessors.Length(); ++i) { + auto* ia_sem = Sem().Get(index_accessors[i]); + ASSERT_NE(ia_sem, nullptr); + ASSERT_NE(ia_sem->ConstantValue(), nullptr); + EXPECT_EQ(ia_sem->ConstantValue()->ValueAs(), i); + } +} +template +std::vector VectorAccessCases() { + if constexpr (std::is_same_v) { + return { + C(Vec(false, true)), + }; + } else { + return { + C(Vec(T(0), T(1))), // + C(Vec(T(0), T(1), T(2))), // + C(Vec(T(0), T(1), T(2), T(3))), // + }; + } +} +INSTANTIATE_TEST_SUITE_P( // + VectorAccess, + ResolverConstEvalVectorAccessTest, + testing::ValuesIn(Concat(VectorAccessCases(), // + VectorAccessCases(), // + VectorAccessCases(), // + VectorAccessCases(), // + VectorAccessCases(), // + VectorAccessCases(), // + VectorAccessCases()))); +} // namespace VectorAccess + } // namespace } // namespace tint::resolver diff --git a/src/tint/resolver/resolver_test_helper.h b/src/tint/resolver/resolver_test_helper.h index 24aea11033..53c187f985 100644 --- a/src/tint/resolver/resolver_test_helper.h +++ b/src/tint/resolver/resolver_test_helper.h @@ -815,6 +815,7 @@ Value Val(T v) { template Value Vec(Ts... args) { using FirstT = std::tuple_element_t<0, std::tuple>; + static_assert(sizeof...(args) >= 2 && sizeof...(args) <= 4, "Invalid vector size"); static_assert(std::conjunction_v...>, "Vector args must all be the same type"); constexpr size_t N = sizeof...(args);