From 1ca538274927ed969fa2c5105c71aa812e90dca5 Mon Sep 17 00:00:00 2001 From: David Neto Date: Mon, 22 Jun 2020 23:15:52 +0000 Subject: [PATCH] [spirv-reader] Simplify null composites Use the shortcut for zero-values: a type constructor without any parameters. Scalars still use plain literals like false, 1u, 1.0. Change-Id: Ie436f1af28cbab0b4c87a07b057deb04632eb534 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23680 Reviewed-by: dan sinclair --- src/reader/spirv/function_misc_test.cc | 22 ------- src/reader/spirv/function_var_test.cc | 11 ---- src/reader/spirv/parser_impl.cc | 46 ++----------- .../spirv/parser_impl_module_var_test.cc | 64 ------------------- 4 files changed, 4 insertions(+), 139 deletions(-) diff --git a/src/reader/spirv/function_misc_test.cc b/src/reader/spirv/function_misc_test.cc index 3cb8ddfcd0..56f4da0ace 100644 --- a/src/reader/spirv/function_misc_test.cc +++ b/src/reader/spirv/function_misc_test.cc @@ -135,8 +135,6 @@ TEST_F(SpvParserTestMiscInstruction, OpUndef_InFunction_Vector) { { TypeConstructor{ __vec_2__u32 - ScalarConstructor{0} - ScalarConstructor{0} } } } @@ -149,8 +147,6 @@ VariableDeclStatement{ { TypeConstructor{ __vec_2__i32 - ScalarConstructor{0} - ScalarConstructor{0} } } } @@ -163,8 +159,6 @@ VariableDeclStatement{ { TypeConstructor{ __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} } } } @@ -195,16 +189,6 @@ TEST_F(SpvParserTestMiscInstruction, OpUndef_InFunction_Matrix) { { TypeConstructor{ __mat_2_2__f32 - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } } } } @@ -236,8 +220,6 @@ TEST_F(SpvParserTestMiscInstruction, OpUndef_InFunction_Array) { { TypeConstructor{ __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} } } } @@ -268,10 +250,6 @@ TEST_F(SpvParserTestMiscInstruction, OpUndef_InFunction_Struct) { { TypeConstructor{ __alias_S__struct_S - ScalarConstructor{false} - ScalarConstructor{0} - ScalarConstructor{0} - ScalarConstructor{0.000000} } } } diff --git a/src/reader/spirv/function_var_test.cc b/src/reader/spirv/function_var_test.cc index dd9f283ae7..0a97ef543b 100644 --- a/src/reader/spirv/function_var_test.cc +++ b/src/reader/spirv/function_var_test.cc @@ -492,8 +492,6 @@ TEST_F(SpvParserTest, EmitFunctionVariables_ArrayInitializer_Null) { { TypeConstructor{ __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} } } } @@ -526,8 +524,6 @@ TEST_F(SpvParserTest, EmitFunctionVariables_ArrayInitializer_AliasType_Null) { { TypeConstructor{ __alias_Arr__array__u32_2_16 - ScalarConstructor{0} - ScalarConstructor{0} } } } @@ -599,13 +595,6 @@ TEST_F(SpvParserTest, EmitFunctionVariables_StructInitializer_Null) { { TypeConstructor{ __alias_S__struct_S - ScalarConstructor{0} - ScalarConstructor{0.000000} - TypeConstructor{ - __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} - } } } } diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index 4ad72eeb19..5a42b779c8 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -38,6 +38,7 @@ #include "src/ast/bool_literal.h" #include "src/ast/builtin_decoration.h" #include "src/ast/decorated_variable.h" +#include "src/ast/expression.h" #include "src/ast/float_literal.h" #include "src/ast/scalar_constructor_expression.h" #include "src/ast/sint_literal.h" @@ -969,10 +970,6 @@ TypedExpression ParserImpl::MakeConstantExpression(uint32_t id) { std::unique_ptr ParserImpl::MakeNullValue( ast::type::Type* type) { - // TODO(dneto): Use the no-operands constructor syntax when it becomes - // available in Tint. - // https://github.com/gpuweb/gpuweb/issues/685 - // https://bugs.chromium.org/p/tint/issues/detail?id=34 if (!type) { Fail() << "trying to create null value for a null type"; @@ -998,45 +995,10 @@ std::unique_ptr ParserImpl::MakeNullValue( return std::make_unique( std::make_unique(type, 0.0f)); } - if (type->IsVector()) { - const auto* vec_ty = type->AsVector(); - ast::ExpressionList ast_components; - for (size_t i = 0; i < vec_ty->size(); ++i) { - ast_components.emplace_back(MakeNullValue(vec_ty->type())); - } + if (type->IsVector() || type->IsMatrix() || type->IsArray() || + type->IsStruct()) { return std::make_unique( - type, std::move(ast_components)); - } - if (type->IsMatrix()) { - const auto* mat_ty = type->AsMatrix(); - // Matrix components are columns - auto* column_ty = - ctx_.type_mgr().Get(std::make_unique( - mat_ty->type(), mat_ty->rows())); - ast::ExpressionList ast_components; - for (size_t i = 0; i < mat_ty->columns(); ++i) { - ast_components.emplace_back(MakeNullValue(column_ty)); - } - return std::make_unique( - type, std::move(ast_components)); - } - if (type->IsArray()) { - auto* arr_ty = type->AsArray(); - ast::ExpressionList ast_components; - for (size_t i = 0; i < arr_ty->size(); ++i) { - ast_components.emplace_back(MakeNullValue(arr_ty->type())); - } - return std::make_unique( - original_type, std::move(ast_components)); - } - if (type->IsStruct()) { - auto* struct_ty = type->AsStruct(); - ast::ExpressionList ast_components; - for (auto& member : struct_ty->impl()->members()) { - ast_components.emplace_back(MakeNullValue(member->type())); - } - return std::make_unique( - original_type, std::move(ast_components)); + original_type, ast::ExpressionList{}); } Fail() << "can't make null value for type: " << type->type_name(); return nullptr; diff --git a/src/reader/spirv/parser_impl_module_var_test.cc b/src/reader/spirv/parser_impl_module_var_test.cc index b0c9d00348..d4c5b87435 100644 --- a/src/reader/spirv/parser_impl_module_var_test.cc +++ b/src/reader/spirv/parser_impl_module_var_test.cc @@ -382,8 +382,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolNullInitializer) { { TypeConstructor{ __vec_2__bool - ScalarConstructor{false} - ScalarConstructor{false} } } })")); @@ -405,8 +403,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorBoolUndefInitializer) { { TypeConstructor{ __vec_2__bool - ScalarConstructor{false} - ScalarConstructor{false} } } })")); @@ -428,8 +424,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintNullInitializer) { { TypeConstructor{ __vec_2__u32 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -451,8 +445,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorUintUndefInitializer) { { TypeConstructor{ __vec_2__u32 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -474,8 +466,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntNullInitializer) { { TypeConstructor{ __vec_2__i32 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -497,8 +487,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorIntUndefInitializer) { { TypeConstructor{ __vec_2__i32 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -520,8 +508,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatNullInitializer) { { TypeConstructor{ __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} } } })")); @@ -543,8 +529,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_VectorFloatUndefInitializer) { { TypeConstructor{ __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} } } })")); @@ -608,21 +592,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixNullInitializer) { { TypeConstructor{ __mat_2_3__f32 - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } } } })")); @@ -644,21 +613,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_MatrixUndefInitializer) { { TypeConstructor{ __mat_2_3__f32 - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } - TypeConstructor{ - __vec_2__f32 - ScalarConstructor{0.000000} - ScalarConstructor{0.000000} - } } } })")); @@ -704,8 +658,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayNullInitializer) { { TypeConstructor{ __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -727,8 +679,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_ArrayUndefInitializer) { { TypeConstructor{ __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} } } })")); @@ -781,13 +731,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_StructNullInitializer) { { TypeConstructor{ __alias_S__struct_S - ScalarConstructor{0} - ScalarConstructor{0.000000} - TypeConstructor{ - __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} - } } } })")) @@ -810,13 +753,6 @@ TEST_F(SpvParserTest, ModuleScopeVar_StructUndefInitializer) { { TypeConstructor{ __alias_S__struct_S - ScalarConstructor{0} - ScalarConstructor{0.000000} - TypeConstructor{ - __array__u32_2 - ScalarConstructor{0} - ScalarConstructor{0} - } } } })"))