From 94867592cd43bc1dc907b927e34dafe72175619e Mon Sep 17 00:00:00 2001 From: James Price Date: Wed, 19 May 2021 11:15:58 +0000 Subject: [PATCH] writer/msl: Fix zero-valued matrix constructors Fixed: tint:813 Change-Id: Ia22f4264de89da76ab0a10cf52f267fcb1e2dbd4 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51440 Auto-Submit: James Price Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: Ben Clayton --- src/writer/msl/generator_impl.cc | 9 ++++++++- src/writer/msl/generator_impl_constructor_test.cc | 9 +++++++++ .../msl/generator_impl_variable_decl_statement_test.cc | 2 +- test/access/var/matrix.wgsl.expected.msl | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index ee976d0cae..f2928a9c01 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -935,7 +935,14 @@ bool GeneratorImpl::EmitZeroValue(typ::Type type) { } else if (auto* vec = type->As()) { return EmitZeroValue(vec->type()); } else if (auto* mat = type->As()) { - return EmitZeroValue(mat->type()); + if (!EmitType(mat, "")) { + return false; + } + out_ << "("; + if (!EmitZeroValue(mat->type())) { + return false; + } + out_ << ")"; } else if (auto* arr = type->As()) { out_ << "{"; if (!EmitZeroValue(arr->ElemType())) { diff --git a/src/writer/msl/generator_impl_constructor_test.cc b/src/writer/msl/generator_impl_constructor_test.cc index 18a49885e7..aa76ded225 100644 --- a/src/writer/msl/generator_impl_constructor_test.cc +++ b/src/writer/msl/generator_impl_constructor_test.cc @@ -131,6 +131,15 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) { "float2x3(float3(1.0f, 2.0f, 3.0f), float3(3.0f, 4.0f, 5.0f))")); } +TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat_Empty) { + WrapInFunction(mat4x4()); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.Generate()) << gen.error(); + EXPECT_THAT(gen.result(), HasSubstr("float4x4(0.0f)")); +} + TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) { WrapInFunction( Construct(ty.array(ty.vec3(), 3), vec3(1.0f, 2.0f, 3.0f), diff --git a/src/writer/msl/generator_impl_variable_decl_statement_test.cc b/src/writer/msl/generator_impl_variable_decl_statement_test.cc index 548a705958..3ff3388f8b 100644 --- a/src/writer/msl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/msl/generator_impl_variable_decl_statement_test.cc @@ -107,7 +107,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) { gen.increment_indent(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); - EXPECT_EQ(gen.result(), " float3x2 a = 0.0f;\n"); + EXPECT_EQ(gen.result(), " float3x2 a = float3x2(0.0f);\n"); } // TODO(crbug.com/tint/726): module-scope private and workgroup variables not diff --git a/test/access/var/matrix.wgsl.expected.msl b/test/access/var/matrix.wgsl.expected.msl index c577ac11f5..95fd2d6e1b 100644 --- a/test/access/var/matrix.wgsl.expected.msl +++ b/test/access/var/matrix.wgsl.expected.msl @@ -2,7 +2,7 @@ using namespace metal; kernel void tint_symbol() { - float3x3 m = 0.0f; + float3x3 m = float3x3(0.0f); float3 const v = m[1]; float const f = v[1]; return;