Move vector and matrix to type/.

This CL moves vector and matrix to type/ and updates the namespaces as
needed.

Bug: tint:1718
Change-Id: I48423b37f15cd69c03ab288143b2d36564789fbf
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/113423
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair
2022-12-08 22:21:24 +00:00
committed by Dan Sinclair
parent 100d4bf339
commit 0e780da882
63 changed files with 1144 additions and 1129 deletions

74
src/tint/type/matrix.cc Normal file
View File

@@ -0,0 +1,74 @@
// Copyright 2022 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/tint/type/matrix.h"
#include "src/tint/program_builder.h"
#include "src/tint/type/vector.h"
#include "src/tint/utils/hash.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Matrix);
namespace tint::type {
Matrix::Matrix(const Vector* column_type, uint32_t columns)
: Base(type::TypeFlags{
Flag::kConstructable,
Flag::kCreationFixedFootprint,
Flag::kFixedFootprint,
}),
subtype_(column_type->type()),
column_type_(column_type),
rows_(column_type->Width()),
columns_(columns) {
TINT_ASSERT(AST, rows_ > 1);
TINT_ASSERT(AST, rows_ < 5);
TINT_ASSERT(AST, columns_ > 1);
TINT_ASSERT(AST, columns_ < 5);
}
Matrix::Matrix(Matrix&&) = default;
Matrix::~Matrix() = default;
size_t Matrix::Hash() const {
return utils::Hash(TypeInfo::Of<Vector>().full_hashcode, rows_, columns_, column_type_);
}
bool Matrix::Equals(const Type& other) const {
if (auto* v = other.As<Matrix>()) {
return v->rows_ == rows_ && v->columns_ == columns_ && v->column_type_ == column_type_;
}
return false;
}
std::string Matrix::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "mat" << columns_ << "x" << rows_ << "<" << subtype_->FriendlyName(symbols) << ">";
return out.str();
}
uint32_t Matrix::Size() const {
return column_type_->Align() * columns();
}
uint32_t Matrix::Align() const {
return column_type_->Align();
}
uint32_t Matrix::ColumnStride() const {
return column_type_->Align();
}
} // namespace tint::type

81
src/tint/type/matrix.h Normal file
View File

@@ -0,0 +1,81 @@
// Copyright 2022 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.
#ifndef SRC_TINT_TYPE_MATRIX_H_
#define SRC_TINT_TYPE_MATRIX_H_
#include <string>
#include "src/tint/type/type.h"
// Forward declarations
namespace tint::type {
class Vector;
} // namespace tint::type
namespace tint::type {
/// A matrix type
class Matrix final : public Castable<Matrix, type::Type> {
public:
/// Constructor
/// @param column_type the type of a column of the matrix
/// @param columns the number of columns in the matrix
Matrix(const Vector* column_type, uint32_t columns);
/// Move constructor
Matrix(Matrix&&);
~Matrix() override;
/// @returns a hash of the type.
size_t Hash() const override;
/// @param other the other type to compare against
/// @returns true if the this type is equal to the given type
bool Equals(const Type& other) const override;
/// @returns the type of the matrix
const type::Type* type() const { return subtype_; }
/// @returns the number of rows in the matrix
uint32_t rows() const { return rows_; }
/// @returns the number of columns in the matrix
uint32_t columns() const { return columns_; }
/// @returns the column-vector type of the matrix
const Vector* ColumnType() const { return column_type_; }
/// @param symbols the program's symbol table
/// @returns the name for this type that closely resembles how it would be
/// declared in WGSL.
std::string FriendlyName(const SymbolTable& symbols) const override;
/// @returns the size in bytes of the type. This may include tail padding.
uint32_t Size() const override;
/// @returns the alignment in bytes of the type. This may include tail
/// padding.
uint32_t Align() const override;
/// @returns the number of bytes between columns of the matrix
uint32_t ColumnStride() const;
private:
const type::Type* const subtype_;
const Vector* const column_type_;
const uint32_t rows_;
const uint32_t columns_;
};
} // namespace tint::type
#endif // SRC_TINT_TYPE_MATRIX_H_

View File

@@ -0,0 +1,75 @@
// Copyright 2022 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/tint/type/test_helper.h"
#include "src/tint/type/texture.h"
namespace tint::type {
namespace {
using MatrixTest = TestHelper;
TEST_F(MatrixTest, Creation) {
auto* a = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* b = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* c = create<Matrix>(create<Vector>(create<type::F32>(), 3u), 4u);
auto* d = create<Matrix>(create<Vector>(create<type::I32>(), 2u), 4u);
auto* e = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 2u);
EXPECT_EQ(a->type(), create<type::I32>());
EXPECT_EQ(a->rows(), 3u);
EXPECT_EQ(a->columns(), 4u);
EXPECT_EQ(a, b);
EXPECT_NE(a, c);
EXPECT_NE(a, d);
EXPECT_NE(a, e);
}
TEST_F(MatrixTest, Hash) {
auto* a = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* b = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* c = create<Matrix>(create<Vector>(create<type::F32>(), 3u), 4u);
auto* d = create<Matrix>(create<Vector>(create<type::I32>(), 2u), 4u);
auto* e = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 2u);
EXPECT_EQ(a->Hash(), b->Hash());
EXPECT_NE(a->Hash(), c->Hash());
EXPECT_NE(a->Hash(), d->Hash());
EXPECT_NE(a->Hash(), e->Hash());
}
TEST_F(MatrixTest, Equals) {
auto* a = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* b = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 4u);
auto* c = create<Matrix>(create<Vector>(create<type::F32>(), 3u), 4u);
auto* d = create<Matrix>(create<Vector>(create<type::I32>(), 2u), 4u);
auto* e = create<Matrix>(create<Vector>(create<type::I32>(), 3u), 2u);
EXPECT_TRUE(a->Equals(*b));
EXPECT_FALSE(a->Equals(*c));
EXPECT_FALSE(a->Equals(*d));
EXPECT_FALSE(a->Equals(*e));
EXPECT_FALSE(a->Equals(type::Void{}));
}
TEST_F(MatrixTest, FriendlyName) {
type::I32 i32;
Vector c{&i32, 3};
Matrix m{&c, 2};
EXPECT_EQ(m.FriendlyName(Symbols()), "mat2x3<i32>");
}
} // namespace
} // namespace tint::type

View File

@@ -15,20 +15,20 @@
#include "src/tint/type/type.h"
#include "src/tint/sem/array.h"
#include "src/tint/sem/matrix.h"
#include "src/tint/sem/struct.h"
#include "src/tint/sem/vector.h"
#include "src/tint/type/abstract_float.h"
#include "src/tint/type/abstract_int.h"
#include "src/tint/type/bool.h"
#include "src/tint/type/f16.h"
#include "src/tint/type/f32.h"
#include "src/tint/type/i32.h"
#include "src/tint/type/matrix.h"
#include "src/tint/type/pointer.h"
#include "src/tint/type/reference.h"
#include "src/tint/type/sampler.h"
#include "src/tint/type/texture.h"
#include "src/tint/type/u32.h"
#include "src/tint/type/vector.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Type);
@@ -81,17 +81,17 @@ bool Type::is_float_scalar() const {
}
bool Type::is_float_matrix() const {
return Is([](const sem::Matrix* m) { return m->type()->is_float_scalar(); });
return Is([](const type::Matrix* m) { return m->type()->is_float_scalar(); });
}
bool Type::is_square_float_matrix() const {
return Is([](const sem::Matrix* m) {
return Is([](const type::Matrix* m) {
return m->type()->is_float_scalar() && m->rows() == m->columns();
});
}
bool Type::is_float_vector() const {
return Is([](const sem::Vector* v) { return v->type()->is_float_scalar(); });
return Is([](const type::Vector* v) { return v->type()->is_float_scalar(); });
}
bool Type::is_float_scalar_or_vector() const {
@@ -116,11 +116,11 @@ bool Type::is_unsigned_integer_scalar() const {
bool Type::is_signed_integer_vector() const {
return Is(
[](const sem::Vector* v) { return v->type()->IsAnyOf<type::I32, type::AbstractInt>(); });
[](const type::Vector* v) { return v->type()->IsAnyOf<type::I32, type::AbstractInt>(); });
}
bool Type::is_unsigned_integer_vector() const {
return Is([](const sem::Vector* v) { return v->type()->Is<type::U32>(); });
return Is([](const type::Vector* v) { return v->type()->Is<type::U32>(); });
}
bool Type::is_unsigned_integer_scalar_or_vector() const {
@@ -136,11 +136,11 @@ bool Type::is_integer_scalar_or_vector() const {
}
bool Type::is_abstract_integer_vector() const {
return Is([](const sem::Vector* v) { return v->type()->Is<type::AbstractInt>(); });
return Is([](const type::Vector* v) { return v->type()->Is<type::AbstractInt>(); });
}
bool Type::is_abstract_float_vector() const {
return Is([](const sem::Vector* v) { return v->type()->Is<type::AbstractFloat>(); });
return Is([](const type::Vector* v) { return v->type()->Is<type::AbstractFloat>(); });
}
bool Type::is_abstract_integer_scalar_or_vector() const {
@@ -152,7 +152,7 @@ bool Type::is_abstract_float_scalar_or_vector() const {
}
bool Type::is_bool_vector() const {
return Is([](const sem::Vector* v) { return v->type()->Is<type::Bool>(); });
return Is([](const type::Vector* v) { return v->type()->Is<type::Bool>(); });
}
bool Type::is_bool_scalar_or_vector() const {
@@ -160,11 +160,11 @@ bool Type::is_bool_scalar_or_vector() const {
}
bool Type::is_numeric_vector() const {
return Is([](const sem::Vector* v) { return v->type()->is_numeric_scalar(); });
return Is([](const type::Vector* v) { return v->type()->is_numeric_scalar(); });
}
bool Type::is_scalar_vector() const {
return Is([](const sem::Vector* v) { return v->type()->is_scalar(); });
return Is([](const type::Vector* v) { return v->type()->is_scalar(); });
}
bool Type::is_numeric_scalar_or_vector() const {
@@ -179,8 +179,8 @@ bool Type::HoldsAbstract() const {
return Switch(
this, //
[&](const type::AbstractNumeric*) { return true; },
[&](const sem::Vector* v) { return v->type()->HoldsAbstract(); },
[&](const sem::Matrix* m) { return m->type()->HoldsAbstract(); },
[&](const type::Vector* v) { return v->type()->HoldsAbstract(); },
[&](const type::Matrix* m) { return m->type()->HoldsAbstract(); },
[&](const sem::Array* a) { return a->ElemType()->HoldsAbstract(); },
[&](const sem::Struct* s) {
for (auto* m : s->Members()) {
@@ -215,16 +215,16 @@ uint32_t Type::ConversionRank(const Type* from, const Type* to) {
[&](const type::F16*) { return 7; }, //
[&](Default) { return kNoConversion; });
},
[&](const sem::Vector* from_vec) {
if (auto* to_vec = to->As<sem::Vector>()) {
[&](const type::Vector* from_vec) {
if (auto* to_vec = to->As<type::Vector>()) {
if (from_vec->Width() == to_vec->Width()) {
return ConversionRank(from_vec->type(), to_vec->type());
}
}
return kNoConversion;
},
[&](const sem::Matrix* from_mat) {
if (auto* to_mat = to->As<sem::Matrix>()) {
[&](const type::Matrix* from_mat) {
if (auto* to_mat = to->As<type::Matrix>()) {
if (from_mat->columns() == to_mat->columns() &&
from_mat->rows() == to_mat->rows()) {
return ConversionRank(from_mat->type(), to_mat->type());
@@ -261,13 +261,13 @@ const Type* Type::ElementOf(const Type* ty, uint32_t* count /* = nullptr */) {
}
return Switch(
ty, //
[&](const sem::Vector* v) {
[&](const type::Vector* v) {
if (count) {
*count = v->Width();
}
return v->type();
},
[&](const sem::Matrix* m) {
[&](const type::Matrix* m) {
if (count) {
*count = m->columns();
}

View File

@@ -29,20 +29,20 @@ struct TypeTest : public TestHelper {
const type::F16* f16 = create<type::F16>();
const type::I32* i32 = create<type::I32>();
const type::U32* u32 = create<type::U32>();
const sem::Vector* vec2_f32 = create<sem::Vector>(f32, 2u);
const sem::Vector* vec3_f32 = create<sem::Vector>(f32, 3u);
const sem::Vector* vec3_f16 = create<sem::Vector>(f16, 3u);
const sem::Vector* vec4_f32 = create<sem::Vector>(f32, 4u);
const sem::Vector* vec3_u32 = create<sem::Vector>(u32, 3u);
const sem::Vector* vec3_i32 = create<sem::Vector>(i32, 3u);
const sem::Vector* vec3_af = create<sem::Vector>(af, 3u);
const sem::Vector* vec3_ai = create<sem::Vector>(ai, 3u);
const sem::Matrix* mat2x4_f32 = create<sem::Matrix>(vec4_f32, 2u);
const sem::Matrix* mat3x4_f32 = create<sem::Matrix>(vec4_f32, 3u);
const sem::Matrix* mat4x2_f32 = create<sem::Matrix>(vec2_f32, 4u);
const sem::Matrix* mat4x3_f32 = create<sem::Matrix>(vec3_f32, 4u);
const sem::Matrix* mat4x3_f16 = create<sem::Matrix>(vec3_f16, 4u);
const sem::Matrix* mat4x3_af = create<sem::Matrix>(vec3_af, 4u);
const type::Vector* vec2_f32 = create<type::Vector>(f32, 2u);
const type::Vector* vec3_f32 = create<type::Vector>(f32, 3u);
const type::Vector* vec3_f16 = create<type::Vector>(f16, 3u);
const type::Vector* vec4_f32 = create<type::Vector>(f32, 4u);
const type::Vector* vec3_u32 = create<type::Vector>(u32, 3u);
const type::Vector* vec3_i32 = create<type::Vector>(i32, 3u);
const type::Vector* vec3_af = create<type::Vector>(af, 3u);
const type::Vector* vec3_ai = create<type::Vector>(ai, 3u);
const type::Matrix* mat2x4_f32 = create<type::Matrix>(vec4_f32, 2u);
const type::Matrix* mat3x4_f32 = create<type::Matrix>(vec4_f32, 3u);
const type::Matrix* mat4x2_f32 = create<type::Matrix>(vec2_f32, 4u);
const type::Matrix* mat4x3_f32 = create<type::Matrix>(vec3_f32, 4u);
const type::Matrix* mat4x3_f16 = create<type::Matrix>(vec3_f16, 4u);
const type::Matrix* mat4x3_af = create<type::Matrix>(vec3_af, 4u);
const type::Reference* ref_u32 =
create<type::Reference>(u32, ast::AddressSpace::kPrivate, ast::Access::kReadWrite);
const sem::Struct* str_f32 = create<sem::Struct>(nullptr,

73
src/tint/type/vector.cc Normal file
View File

@@ -0,0 +1,73 @@
// Copyright 2022 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/tint/type/vector.h"
#include "src/tint/program_builder.h"
#include "src/tint/utils/hash.h"
TINT_INSTANTIATE_TYPEINFO(tint::type::Vector);
namespace tint::type {
Vector::Vector(Type const* subtype, uint32_t width)
: Base(type::TypeFlags{
Flag::kConstructable,
Flag::kCreationFixedFootprint,
Flag::kFixedFootprint,
}),
subtype_(subtype),
width_(width) {
TINT_ASSERT(Type, width_ > 1);
TINT_ASSERT(Type, width_ < 5);
}
Vector::Vector(Vector&&) = default;
Vector::~Vector() = default;
size_t Vector::Hash() const {
return utils::Hash(TypeInfo::Of<Vector>().full_hashcode, width_, subtype_);
}
bool Vector::Equals(const Type& other) const {
if (auto* v = other.As<Vector>()) {
return v->width_ == width_ && v->subtype_ == subtype_;
}
return false;
}
std::string Vector::FriendlyName(const SymbolTable& symbols) const {
std::ostringstream out;
out << "vec" << width_ << "<" << subtype_->FriendlyName(symbols) << ">";
return out.str();
}
uint32_t Vector::Size() const {
return subtype_->Size() * width_;
}
uint32_t Vector::Align() const {
switch (width_) {
case 2:
return subtype_->Size() * 2;
case 3:
return subtype_->Size() * 4;
case 4:
return subtype_->Size() * 4;
}
return 0; // Unreachable
}
} // namespace tint::type

75
src/tint/type/vector.h Normal file
View File

@@ -0,0 +1,75 @@
// Copyright 2022 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.
#ifndef SRC_TINT_TYPE_VECTOR_H_
#define SRC_TINT_TYPE_VECTOR_H_
#include <string>
#include "src/tint/type/type.h"
namespace tint::type {
/// A vector type.
class Vector final : public Castable<Vector, type::Type> {
public:
/// Constructor
/// @param subtype the vector element type
/// @param size the number of elements in the vector
Vector(Type const* subtype, uint32_t size);
/// Move constructor
Vector(Vector&&);
~Vector() override;
/// @returns a hash of the type.
size_t Hash() const override;
/// @param other the other type to compare against
/// @returns true if the this type is equal to the given type
bool Equals(const Type& other) const override;
/// @returns the type of the vector elements
const type::Type* type() const { return subtype_; }
/// @param symbols the program's symbol table
/// @returns the name for this type that closely resembles how it would be
/// declared in WGSL.
std::string FriendlyName(const SymbolTable& symbols) const override;
/// @returns the number of elements in the vector
uint32_t Width() const { return width_; }
/// @returns the size in bytes of the type. This may include tail padding.
uint32_t Size() const override;
/// @returns the alignment in bytes of the type. This may include tail
/// padding.
uint32_t Align() const override;
/// @param width the width of the vector
/// @returns the size in bytes of a vector of the given width.
static uint32_t SizeOf(uint32_t width);
/// @param width the width of the vector
/// @returns the alignment in bytes of a vector of the given width.
static uint32_t AlignOf(uint32_t width);
private:
Type const* const subtype_;
const uint32_t width_;
};
} // namespace tint::type
#endif // SRC_TINT_TYPE_VECTOR_H_

View File

@@ -0,0 +1,67 @@
// Copyright 2022 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/tint/type/test_helper.h"
#include "src/tint/type/texture.h"
namespace tint::type {
namespace {
using VectorTest = TestHelper;
TEST_F(VectorTest, Creation) {
auto* a = create<Vector>(create<type::I32>(), 2u);
auto* b = create<Vector>(create<type::I32>(), 2u);
auto* c = create<Vector>(create<type::F32>(), 2u);
auto* d = create<Vector>(create<type::F32>(), 3u);
EXPECT_EQ(a->type(), create<type::I32>());
EXPECT_EQ(a->Width(), 2u);
EXPECT_EQ(a, b);
EXPECT_NE(a, c);
EXPECT_NE(a, d);
}
TEST_F(VectorTest, Hash) {
auto* a = create<Vector>(create<type::I32>(), 2u);
auto* b = create<Vector>(create<type::I32>(), 2u);
auto* c = create<Vector>(create<type::F32>(), 2u);
auto* d = create<Vector>(create<type::F32>(), 3u);
EXPECT_EQ(a->Hash(), b->Hash());
EXPECT_NE(a->Hash(), c->Hash());
EXPECT_NE(a->Hash(), d->Hash());
}
TEST_F(VectorTest, Equals) {
auto* a = create<Vector>(create<type::I32>(), 2u);
auto* b = create<Vector>(create<type::I32>(), 2u);
auto* c = create<Vector>(create<type::F32>(), 2u);
auto* d = create<Vector>(create<type::F32>(), 3u);
EXPECT_TRUE(a->Equals(*b));
EXPECT_FALSE(a->Equals(*c));
EXPECT_FALSE(a->Equals(*d));
EXPECT_FALSE(a->Equals(type::Void{}));
}
TEST_F(VectorTest, FriendlyName) {
auto* f32 = create<type::F32>();
auto* v = create<Vector>(f32, 3u);
EXPECT_EQ(v->FriendlyName(Symbols()), "vec3<f32>");
}
} // namespace
} // namespace tint::type