validation: matrix element type must be 'f32'

Bug: tint:784
Change-Id: Iafb1d3e16beb489d588b7af6aac18f7cee26154b
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54900
Auto-Submit: Sarah Mashayekhi <sarahmashay@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Sarah 2021-06-21 17:08:05 +00:00 committed by Sarah Mashayekhi
parent b96ed7bda4
commit 4d69751572
10 changed files with 65 additions and 81 deletions

View File

@ -77,14 +77,8 @@ static constexpr Params all_param_types[] = {
ParamsFor<vec3<i32>>(), //
ParamsFor<vec3<u32>>(), //
ParamsFor<vec3<f32>>(), //
ParamsFor<mat3x3<i32>>(), //
ParamsFor<mat3x3<u32>>(), //
ParamsFor<mat3x3<f32>>(), //
ParamsFor<mat2x3<i32>>(), //
ParamsFor<mat2x3<u32>>(), //
ParamsFor<mat2x3<f32>>(), //
ParamsFor<mat3x2<i32>>(), //
ParamsFor<mat3x2<u32>>(), //
ParamsFor<mat3x2<f32>>() //
};

View File

@ -564,17 +564,8 @@ static constexpr Params cases[] = {
ParamsFor<vec3<f32>>(true), //
ParamsFor<vec4<f32>>(true), //
ParamsFor<mat2x2<f32>>(false), //
ParamsFor<mat2x2<i32>>(false), //
ParamsFor<mat2x2<u32>>(false), //
ParamsFor<mat2x2<bool>>(false), //
ParamsFor<mat3x3<f32>>(false), //
ParamsFor<mat3x3<i32>>(false), //
ParamsFor<mat3x3<u32>>(false), //
ParamsFor<mat3x3<bool>>(false), //
ParamsFor<mat4x4<f32>>(false), //
ParamsFor<mat4x4<i32>>(false), //
ParamsFor<mat4x4<u32>>(false), //
ParamsFor<mat4x4<bool>>(false), //
ParamsFor<alias<f32>>(true), //
ParamsFor<alias<i32>>(true), //
ParamsFor<alias<u32>>(true), //

View File

@ -65,8 +65,6 @@ Params all_cases[] = {
ParamsFor<vec3<i32>>(), //
ParamsFor<vec3<u32>>(), //
ParamsFor<vec3<f32>>(), //
ParamsFor<mat3x3<i32>>(), //
ParamsFor<mat3x3<u32>>(), //
ParamsFor<mat3x3<f32>>(), //
ParamsFor<alias<bool>>(), //
ParamsFor<alias<u32>>(), //
@ -76,8 +74,6 @@ Params all_cases[] = {
ParamsFor<alias<vec3<i32>>>(), //
ParamsFor<alias<vec3<u32>>>(), //
ParamsFor<alias<vec3<f32>>>(), //
ParamsFor<alias<mat3x3<i32>>>(), //
ParamsFor<alias<mat3x3<u32>>>(), //
ParamsFor<alias<mat3x3<f32>>>(), //
};

View File

@ -293,9 +293,15 @@ sem::Type* Resolver::Type(const ast::Type* ty) {
}
if (auto* t = ty->As<ast::Matrix>()) {
if (auto* el = Type(t->type())) {
auto* column_type = builder_->create<sem::Vector>(
const_cast<sem::Type*>(el), t->rows());
return builder_->create<sem::Matrix>(column_type, t->columns());
if (auto* column_type = builder_->create<sem::Vector>(
const_cast<sem::Type*>(el), t->rows())) {
if (auto* matrix_type =
builder_->create<sem::Matrix>(column_type, t->columns())) {
if (ValidateMatrix(matrix_type, t->source())) {
return matrix_type;
}
}
}
}
return nullptr;
}
@ -2139,6 +2145,15 @@ bool Resolver::ValidateVectorConstructor(
return true;
}
bool Resolver::ValidateMatrix(const sem::Matrix* matrix_type,
const Source& source) {
if (!matrix_type->is_float_matrix()) {
diagnostics_.add_error("matrix element type must be 'f32'", source);
return false;
}
return true;
} // namespace resolver
bool Resolver::ValidateMatrixConstructor(
const ast::TypeConstructorExpression* ctor,
const sem::Matrix* matrix_type) {
@ -2148,6 +2163,10 @@ bool Resolver::ValidateMatrixConstructor(
return true;
}
if (!ValidateMatrix(matrix_type, ctor->source())) {
return false;
}
auto* elem_type = matrix_type->type();
if (matrix_type->columns() != values.size()) {
const Source& values_start = values[0]->source();

View File

@ -272,6 +272,7 @@ class Resolver {
bool ValidateEntryPoint(const ast::Function* func, const FunctionInfo* info);
bool ValidateFunction(const ast::Function* func, const FunctionInfo* info);
bool ValidateGlobalVariable(const VariableInfo* var);
bool ValidateMatrix(const sem::Matrix* matirx_type, const Source& source);
bool ValidateMatrixConstructor(const ast::TypeConstructorExpression* ctor,
const sem::Matrix* matrix_type);
bool ValidateParameter(const VariableInfo* info);

View File

@ -496,7 +496,7 @@ TEST_F(ResolverTest, ArrayAccessor_Matrix_Dynamic_Ref) {
}
TEST_F(ResolverTest, ArrayAccessor_Matrix_BothDimensions_Dynamic_Ref) {
Global("my_var", ty.mat4x4<bool>(), ast::StorageClass::kOutput);
Global("my_var", ty.mat4x4<f32>(), ast::StorageClass::kOutput);
auto* idx = Var("idx", ty.u32(), Expr(3u));
auto* idy = Var("idy", ty.u32(), Expr(2u));
auto* acc = IndexAccessor(IndexAccessor("my_var", idx), idy);
@ -517,7 +517,7 @@ TEST_F(ResolverTest, ArrayAccessor_Matrix_Dynamic) {
}
TEST_F(ResolverTest, ArrayAccessor_Matrix_XDimension_Dynamic) {
GlobalConst("my_var", ty.mat4x4<bool>(), Construct(ty.mat4x4<bool>()));
GlobalConst("my_var", ty.mat4x4<f32>(), Construct(ty.mat4x4<f32>()));
auto* idx = Var("idx", ty.u32(), Expr(3u));
auto* acc = IndexAccessor("my_var", Expr(Source{{12, 34}}, idx));
WrapInFunction(Decl(idx), acc);
@ -528,7 +528,7 @@ TEST_F(ResolverTest, ArrayAccessor_Matrix_XDimension_Dynamic) {
}
TEST_F(ResolverTest, ArrayAccessor_Matrix_BothDimension_Dynamic) {
GlobalConst("my_var", ty.mat4x4<bool>(), Construct(ty.mat4x4<bool>()));
GlobalConst("my_var", ty.mat4x4<f32>(), Construct(ty.mat4x4<f32>()));
auto* idx = Var("idy", ty.u32(), Expr(2u));
auto* acc =
IndexAccessor(IndexAccessor("my_var", Expr(Source{{12, 34}}, idx)), 1);
@ -1486,14 +1486,8 @@ static constexpr builder::ast_type_func_ptr all_create_type_funcs[] = {
DataType<vec3<i32>>::AST, //
DataType<vec3<u32>>::AST, //
DataType<vec3<f32>>::AST, //
DataType<mat3x3<i32>>::AST, //
DataType<mat3x3<u32>>::AST, //
DataType<mat3x3<f32>>::AST, //
DataType<mat2x3<i32>>::AST, //
DataType<mat2x3<u32>>::AST, //
DataType<mat2x3<f32>>::AST, //
DataType<mat3x2<i32>>::AST, //
DataType<mat3x2<u32>>::AST, //
DataType<mat3x2<f32>>::AST //
};

View File

@ -242,15 +242,15 @@ TEST_F(ResolverStructLayoutTest, Vector) {
TEST_F(ResolverStructLayoutTest, Matrix) {
auto* s = Structure("S", {
Member("a", ty.mat2x2<i32>()),
Member("b", ty.mat2x3<i32>()),
Member("c", ty.mat2x4<i32>()),
Member("d", ty.mat3x2<i32>()),
Member("e", ty.mat3x3<i32>()),
Member("f", ty.mat3x4<i32>()),
Member("g", ty.mat4x2<i32>()),
Member("h", ty.mat4x3<i32>()),
Member("i", ty.mat4x4<i32>()),
Member("a", ty.mat2x2<f32>()),
Member("b", ty.mat2x3<f32>()),
Member("c", ty.mat2x4<f32>()),
Member("d", ty.mat3x2<f32>()),
Member("e", ty.mat3x3<f32>()),
Member("f", ty.mat3x4<f32>()),
Member("g", ty.mat4x2<f32>()),
Member("h", ty.mat4x3<f32>()),
Member("i", ty.mat4x4<f32>()),
});
ASSERT_TRUE(r()->Resolve()) << r()->error();
@ -292,7 +292,7 @@ TEST_F(ResolverStructLayoutTest, Matrix) {
TEST_F(ResolverStructLayoutTest, NestedStruct) {
auto* inner = Structure("Inner", {
Member("a", ty.mat3x3<i32>()),
Member("a", ty.mat3x3<f32>()),
});
auto* s = Structure("S", {
Member("a", ty.i32()),

View File

@ -110,8 +110,6 @@ static constexpr Params from_constructor_expression_cases[] = {
ParamsFor<vec3<i32>>(),
ParamsFor<vec3<u32>>(),
ParamsFor<vec3<f32>>(),
ParamsFor<mat3x3<i32>>(),
ParamsFor<mat3x3<u32>>(),
ParamsFor<mat3x3<f32>>(),
ParamsFor<alias<bool>>(),
ParamsFor<alias<i32>>(),
@ -120,8 +118,6 @@ static constexpr Params from_constructor_expression_cases[] = {
ParamsFor<alias<vec3<i32>>>(),
ParamsFor<alias<vec3<u32>>>(),
ParamsFor<alias<vec3<f32>>>(),
ParamsFor<alias<mat3x3<i32>>>(),
ParamsFor<alias<mat3x3<u32>>>(),
ParamsFor<alias<mat3x3<f32>>>(),
};
INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest,
@ -208,8 +204,6 @@ static constexpr Params from_call_expression_cases[] = {
ParamsFor<vec3<i32>>(),
ParamsFor<vec3<u32>>(),
ParamsFor<vec3<f32>>(),
ParamsFor<mat3x3<i32>>(),
ParamsFor<mat3x3<u32>>(),
ParamsFor<mat3x3<f32>>(),
ParamsFor<alias<bool>>(),
ParamsFor<alias<i32>>(),
@ -218,8 +212,6 @@ static constexpr Params from_call_expression_cases[] = {
ParamsFor<alias<vec3<i32>>>(),
ParamsFor<alias<vec3<u32>>>(),
ParamsFor<alias<vec3<f32>>>(),
ParamsFor<alias<mat3x3<i32>>>(),
ParamsFor<alias<mat3x3<u32>>>(),
ParamsFor<alias<mat3x3<f32>>>(),
};
INSTANTIATE_TEST_SUITE_P(ResolverTypeConstructorValidationTest,

View File

@ -876,20 +876,6 @@ TEST_F(ResolverValidationTest,
"expected 'array<i32, 2>', found 'array<u32, 2>'");
}
TEST_F(ResolverValidationTest,
Expr_Constructor_ArrayOfMatrix_SubElemTypeMismatch) {
// array<mat2x2<f32>, 2>(mat2x2<f32>(), mat2x2<i32>());
auto* e0 = mat2x2<f32>();
SetSource(Source::Location({12, 34}));
auto* e1 = mat2x2<i32>();
auto* t = Construct(ty.array(ty.mat2x2<f32>(), 2), e0, e1);
WrapInFunction(t);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(),
"12:34 error: type in array constructor does not match array type: "
"expected 'mat2x2<f32>', found 'mat2x2<i32>'");
}
TEST_F(ResolverValidationTest, Expr_Constructor_Array_TooFewElements) {
// array<i32, 4>(1, 2, 3);
SetSource(Source::Location({12, 34}));
@ -1987,6 +1973,17 @@ std::string VecStr(uint32_t dimensions, std::string subtype = "f32") {
using MatrixConstructorTest = ResolverTestWithParam<MatrixDimensions>;
TEST_F(MatrixConstructorTest, Expr_Constructor_Matrix_NotF32) {
// m2x2<i32>()
SetSource(Source::Location({12, 34}));
auto* tc = mat2x2<i32>(
create<ast::TypeConstructorExpression>(ty.mat2x2<i32>(), ExprList()));
WrapInFunction(tc);
EXPECT_FALSE(r()->Resolve());
EXPECT_EQ(r()->error(), "12:34 error: matrix element type must be 'f32'");
}
TEST_P(MatrixConstructorTest, Expr_Constructor_Error_TooFewArguments) {
// matNxM<f32>(vecM<f32>(), ...); with N - 1 arguments

View File

@ -202,32 +202,32 @@ INSTANTIATE_TEST_SUITE_P(
TypeCase{ty_vec4<f32>, "asfloat(data.Load4(16u))"},
TypeCase{ty_vec4<i32>, "asint(data.Load4(16u))"},
TypeCase{
ty_mat2x2<u32>,
R"(return uint2x2(buffer.Load2((offset + 0u)), buffer.Load2((offset + 8u)));)"},
ty_mat2x2<f32>,
R"(return float2x2(asfloat(buffer.Load2((offset + 0u))), asfloat(buffer.Load2((offset + 8u))));)"},
TypeCase{
ty_mat2x3<f32>,
R"(return float2x3(asfloat(buffer.Load3((offset + 0u))), asfloat(buffer.Load3((offset + 16u))));)"},
TypeCase{
ty_mat2x4<i32>,
R"(return int2x4(asint(buffer.Load4((offset + 0u))), asint(buffer.Load4((offset + 16u))));)"},
ty_mat2x4<f32>,
R"(return float2x4(asfloat(buffer.Load4((offset + 0u))), asfloat(buffer.Load4((offset + 16u))));)"},
TypeCase{
ty_mat3x2<u32>,
R"(return uint3x2(buffer.Load2((offset + 0u)), buffer.Load2((offset + 8u)), buffer.Load2((offset + 16u)));)"},
ty_mat3x2<f32>,
R"(return float3x2(asfloat(buffer.Load2((offset + 0u))), asfloat(buffer.Load2((offset + 8u))), asfloat(buffer.Load2((offset + 16u))));)"},
TypeCase{
ty_mat3x3<f32>,
R"(return float3x3(asfloat(buffer.Load3((offset + 0u))), asfloat(buffer.Load3((offset + 16u))), asfloat(buffer.Load3((offset + 32u))));)"},
TypeCase{
ty_mat3x4<i32>,
R"(return int3x4(asint(buffer.Load4((offset + 0u))), asint(buffer.Load4((offset + 16u))), asint(buffer.Load4((offset + 32u))));)"},
ty_mat3x4<f32>,
R"(return float3x4(asfloat(buffer.Load4((offset + 0u))), asfloat(buffer.Load4((offset + 16u))), asfloat(buffer.Load4((offset + 32u))));)"},
TypeCase{
ty_mat4x2<u32>,
R"(return uint4x2(buffer.Load2((offset + 0u)), buffer.Load2((offset + 8u)), buffer.Load2((offset + 16u)), buffer.Load2((offset + 24u)));)"},
ty_mat4x2<f32>,
R"(return float4x2(asfloat(buffer.Load2((offset + 0u))), asfloat(buffer.Load2((offset + 8u))), asfloat(buffer.Load2((offset + 16u))), asfloat(buffer.Load2((offset + 24u))));)"},
TypeCase{
ty_mat4x3<f32>,
R"(return float4x3(asfloat(buffer.Load3((offset + 0u))), asfloat(buffer.Load3((offset + 16u))), asfloat(buffer.Load3((offset + 32u))), asfloat(buffer.Load3((offset + 48u))));)"},
TypeCase{
ty_mat4x4<i32>,
R"(return int4x4(asint(buffer.Load4((offset + 0u))), asint(buffer.Load4((offset + 16u))), asint(buffer.Load4((offset + 32u))), asint(buffer.Load4((offset + 48u))));)"}));
ty_mat4x4<f32>,
R"(return float4x4(asfloat(buffer.Load4((offset + 0u))), asfloat(buffer.Load4((offset + 16u))), asfloat(buffer.Load4((offset + 32u))), asfloat(buffer.Load4((offset + 48u))));)"}));
using HlslGeneratorImplTest_MemberAccessor_StorageBufferStore =
HlslGeneratorImplTest_MemberAccessorWithParam<TypeCase>;
@ -273,7 +273,7 @@ INSTANTIATE_TEST_SUITE_P(
TypeCase{ty_vec4<u32>, "data.Store4(16u, asuint(value))"},
TypeCase{ty_vec4<f32>, "data.Store4(16u, asuint(value))"},
TypeCase{ty_vec4<i32>, "data.Store4(16u, asuint(value))"},
TypeCase{ty_mat2x2<u32>, R"({
TypeCase{ty_mat2x2<f32>, R"({
buffer.Store2((offset + 0u), asuint(value[0u]));
buffer.Store2((offset + 8u), asuint(value[1u]));
})"},
@ -281,11 +281,11 @@ INSTANTIATE_TEST_SUITE_P(
buffer.Store3((offset + 0u), asuint(value[0u]));
buffer.Store3((offset + 16u), asuint(value[1u]));
})"},
TypeCase{ty_mat2x4<i32>, R"({
TypeCase{ty_mat2x4<f32>, R"({
buffer.Store4((offset + 0u), asuint(value[0u]));
buffer.Store4((offset + 16u), asuint(value[1u]));
})"},
TypeCase{ty_mat3x2<u32>, R"({
TypeCase{ty_mat3x2<f32>, R"({
buffer.Store2((offset + 0u), asuint(value[0u]));
buffer.Store2((offset + 8u), asuint(value[1u]));
buffer.Store2((offset + 16u), asuint(value[2u]));
@ -295,12 +295,12 @@ INSTANTIATE_TEST_SUITE_P(
buffer.Store3((offset + 16u), asuint(value[1u]));
buffer.Store3((offset + 32u), asuint(value[2u]));
})"},
TypeCase{ty_mat3x4<i32>, R"({
TypeCase{ty_mat3x4<f32>, R"({
buffer.Store4((offset + 0u), asuint(value[0u]));
buffer.Store4((offset + 16u), asuint(value[1u]));
buffer.Store4((offset + 32u), asuint(value[2u]));
})"},
TypeCase{ty_mat4x2<u32>, R"({
TypeCase{ty_mat4x2<f32>, R"({
buffer.Store2((offset + 0u), asuint(value[0u]));
buffer.Store2((offset + 8u), asuint(value[1u]));
buffer.Store2((offset + 16u), asuint(value[2u]));
@ -312,7 +312,7 @@ INSTANTIATE_TEST_SUITE_P(
buffer.Store3((offset + 32u), asuint(value[2u]));
buffer.Store3((offset + 48u), asuint(value[3u]));
})"},
TypeCase{ty_mat4x4<i32>, R"({
TypeCase{ty_mat4x4<f32>, R"({
buffer.Store4((offset + 0u), asuint(value[0u]));
buffer.Store4((offset + 16u), asuint(value[1u]));
buffer.Store4((offset + 32u), asuint(value[2u]));