AppendVector() - support pointer args

Add tests.

Fixed: tint:639
Change-Id: I6718a1fcf5a46aca36209bec037633a862d8e568
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/45600
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-03-22 21:05:47 +00:00 committed by Commit Bot service account
parent 247b922545
commit 5622b6bbd1
4 changed files with 177 additions and 8 deletions

View File

@ -517,6 +517,7 @@ if(${TINT_BUILD_TESTS})
validator/validator_type_test.cc validator/validator_type_test.cc
validator/validator_test_helper.cc validator/validator_test_helper.cc
validator/validator_test_helper.h validator/validator_test_helper.h
writer/append_vector_test.cc
writer/float_to_string_test.cc writer/float_to_string_test.cc
) )

View File

@ -40,21 +40,17 @@ ast::TypeConstructorExpression* AppendVector(ProgramBuilder* b,
uint32_t packed_size; uint32_t packed_size;
type::Type* packed_el_ty; // Currently must be f32. type::Type* packed_el_ty; // Currently must be f32.
auto* vector_sem = b->Sem().Get(vector); auto* vector_sem = b->Sem().Get(vector);
if (auto* vec = vector_sem->Type()->As<type::Vector>()) { auto* vector_ty = vector_sem->Type()->UnwrapPtrIfNeeded();
if (auto* vec = vector_ty->As<type::Vector>()) {
packed_size = vec->size() + 1; packed_size = vec->size() + 1;
packed_el_ty = vec->type(); packed_el_ty = vec->type();
} else { } else {
packed_size = 2; packed_size = 2;
packed_el_ty = vector_sem->Type(); packed_el_ty = vector_ty;
} }
auto* statement = vector_sem->Stmt(); auto* statement = vector_sem->Stmt();
// Cast scalar to the vector element type
auto* scalar_cast = b->Construct(packed_el_ty, scalar);
b->Sem().Add(scalar_cast, b->create<semantic::Expression>(
scalar_cast, packed_el_ty, statement));
auto* packed_ty = b->create<type::Vector>(packed_el_ty, packed_size); auto* packed_ty = b->create<type::Vector>(packed_el_ty, packed_size);
// If the coordinates are already passed in a vector constructor, extract // If the coordinates are already passed in a vector constructor, extract
@ -65,7 +61,11 @@ ast::TypeConstructorExpression* AppendVector(ProgramBuilder* b,
} else { } else {
packed.emplace_back(vector); packed.emplace_back(vector);
} }
if (packed_el_ty != b->Sem().Get(scalar)->Type()) { if (packed_el_ty != b->Sem().Get(scalar)->Type()->UnwrapPtrIfNeeded()) {
// Cast scalar to the vector element type
auto* scalar_cast = b->Construct(packed_el_ty, scalar);
b->Sem().Add(scalar_cast, b->create<semantic::Expression>(
scalar_cast, packed_el_ty, statement));
packed.emplace_back(scalar_cast); packed.emplace_back(scalar_cast);
} else { } else {
packed.emplace_back(scalar); packed.emplace_back(scalar);

View File

@ -0,0 +1,167 @@
// Copyright 2021 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/writer/append_vector.h"
#include "src/program_builder.h"
#include "src/resolver/resolver.h"
#include "gtest/gtest.h"
namespace tint {
namespace writer {
namespace {
class AppendVectorTest : public ::testing::Test, public ProgramBuilder {};
TEST_F(AppendVectorTest, Vec2i32_i32) {
auto* scalar_1 = Expr(1);
auto* scalar_2 = Expr(2);
auto* scalar_3 = Expr(3);
auto* vec_12 = vec2<i32>(scalar_1, scalar_2);
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 3u);
EXPECT_EQ(vec_123->values()[0], scalar_1);
EXPECT_EQ(vec_123->values()[1], scalar_2);
EXPECT_EQ(vec_123->values()[2], scalar_3);
}
TEST_F(AppendVectorTest, Vec2i32_f32) {
auto* scalar_1 = Expr(1);
auto* scalar_2 = Expr(2);
auto* scalar_3 = Expr(3.0f);
auto* vec_12 = vec2<i32>(scalar_1, scalar_2);
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 3u);
EXPECT_EQ(vec_123->values()[0], scalar_1);
EXPECT_EQ(vec_123->values()[1], scalar_2);
auto* f32_to_i32 = vec_123->values()[2]->As<ast::TypeConstructorExpression>();
ASSERT_NE(f32_to_i32, nullptr);
ASSERT_EQ(f32_to_i32->values().size(), 1u);
EXPECT_EQ(f32_to_i32->values()[0], scalar_3);
}
TEST_F(AppendVectorTest, Vec3i32_i32) {
auto* scalar_1 = Expr(1);
auto* scalar_2 = Expr(2);
auto* scalar_3 = Expr(3);
auto* scalar_4 = Expr(3);
auto* vec_123 = vec3<i32>(scalar_1, scalar_2, scalar_3);
WrapInFunction(vec_123, scalar_4);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_1234 = AppendVector(this, vec_123, scalar_4)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_1234, nullptr);
ASSERT_EQ(vec_1234->values().size(), 4u);
EXPECT_EQ(vec_1234->values()[0], scalar_1);
EXPECT_EQ(vec_1234->values()[1], scalar_2);
EXPECT_EQ(vec_1234->values()[2], scalar_3);
EXPECT_EQ(vec_1234->values()[3], scalar_4);
}
TEST_F(AppendVectorTest, Vec2i32Var_i32) {
Global("vec_12", ty.vec2<i32>(), ast::StorageClass::kInput);
auto* vec_12 = Expr("vec_12");
auto* scalar_3 = Expr(3);
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 2u);
EXPECT_EQ(vec_123->values()[0], vec_12);
EXPECT_EQ(vec_123->values()[1], scalar_3);
}
TEST_F(AppendVectorTest, Vec2i32_i32Var) {
Global("scalar_3", ty.i32(), ast::StorageClass::kInput);
auto* scalar_1 = Expr(1);
auto* scalar_2 = Expr(2);
auto* scalar_3 = Expr("scalar_3");
auto* vec_12 = vec2<i32>(scalar_1, scalar_2);
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 3u);
EXPECT_EQ(vec_123->values()[0], scalar_1);
EXPECT_EQ(vec_123->values()[1], scalar_2);
EXPECT_EQ(vec_123->values()[2], scalar_3);
}
TEST_F(AppendVectorTest, Vec2i32Var_i32Var) {
Global("vec_12", ty.vec2<i32>(), ast::StorageClass::kInput);
Global("scalar_3", ty.i32(), ast::StorageClass::kInput);
auto* vec_12 = Expr("vec_12");
auto* scalar_3 = Expr("scalar_3");
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 2u);
EXPECT_EQ(vec_123->values()[0], vec_12);
EXPECT_EQ(vec_123->values()[1], scalar_3);
}
TEST_F(AppendVectorTest, Vec2i32Var_f32Var) {
Global("vec_12", ty.vec2<i32>(), ast::StorageClass::kInput);
Global("scalar_3", ty.f32(), ast::StorageClass::kInput);
auto* vec_12 = Expr("vec_12");
auto* scalar_3 = Expr("scalar_3");
WrapInFunction(vec_12, scalar_3);
resolver::Resolver resolver(this);
ASSERT_TRUE(resolver.Resolve()) << resolver.error();
auto* vec_123 = AppendVector(this, vec_12, scalar_3)
->As<ast::TypeConstructorExpression>();
ASSERT_NE(vec_123, nullptr);
ASSERT_EQ(vec_123->values().size(), 2u);
EXPECT_EQ(vec_123->values()[0], vec_12);
auto* f32_to_i32 = vec_123->values()[1]->As<ast::TypeConstructorExpression>();
ASSERT_NE(f32_to_i32, nullptr);
ASSERT_EQ(f32_to_i32->values().size(), 1u);
EXPECT_EQ(f32_to_i32->values()[0], scalar_3);
}
} // namespace
} // namespace writer
} // namespace tint

View File

@ -224,6 +224,7 @@ source_set("tint_unittests_core_src") {
"../src/validator/validator_test.cc", "../src/validator/validator_test.cc",
"../src/validator/validator_test_helper.cc", "../src/validator/validator_test_helper.cc",
"../src/validator/validator_test_helper.h", "../src/validator/validator_test_helper.h",
"../src/writer/append_vector_test.cc",
"../src/writer/float_to_string_test.cc", "../src/writer/float_to_string_test.cc",
] ]