writer/msl: Simplify type constructor generation

The special-case for zero-valued constructors is unnecessary, as an
empty initializer list already correctly zero-initializes for all
types. This was causing an additional {} to be emitted for empty
structures, which the MSL compiler rejects.

Fixed: tint:821
Change-Id: Ib48c73eadef15b517e14b248229ecfbbfeb13f81
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51822
Commit-Queue: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2021-05-20 16:45:47 +00:00 committed by Tint LUCI CQ
parent ba1fb84855
commit 6c582778cf
5 changed files with 29 additions and 25 deletions

View File

@ -892,23 +892,15 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
out_ << "("; out_ << "(";
} }
// If the type constructor is empty then we need to construct with the zero bool first = true;
// value for all components. for (auto* e : expr->values()) {
if (expr->values().empty()) { if (!first) {
if (!EmitZeroValue(type)) { out_ << ", ";
return false;
} }
} else { first = false;
bool first = true;
for (auto* e : expr->values()) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(e)) { if (!EmitExpression(e)) {
return false; return false;
}
} }
} }

View File

@ -112,7 +112,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) {
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("float3(0.0f)")); EXPECT_THAT(gen.result(), HasSubstr("float3()"));
} }
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) { TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat) {
@ -137,7 +137,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Mat_Empty) {
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("float4x4(0.0f)")); EXPECT_THAT(gen.result(), HasSubstr("float4x4()"));
} }
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) { TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) {
@ -155,10 +155,10 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Array) {
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) { TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) {
auto* str = Structure("S", { auto* str = Structure("S", {
Member("a", ty.i32()), Member("a", ty.i32()),
Member("b", ty.f32()), Member("b", ty.f32()),
Member("c", ty.vec3<i32>()), Member("c", ty.vec3<i32>()),
}); });
WrapInFunction(Construct(str, 1, 2.0f, vec3<i32>(3, 4, 5))); WrapInFunction(Construct(str, 1, 2.0f, vec3<i32>(3, 4, 5)));
@ -168,6 +168,18 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) {
EXPECT_THAT(gen.result(), HasSubstr("{1, 2.0f, int3(3, 4, 5)}")); EXPECT_THAT(gen.result(), HasSubstr("{1, 2.0f, int3(3, 4, 5)}"));
} }
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct_Empty) {
auto* str = Structure("S", {});
WrapInFunction(Construct(str));
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("{}"));
EXPECT_THAT(gen.result(), Not(HasSubstr("{{}}")));
}
} // namespace } // namespace
} // namespace msl } // namespace msl
} // namespace writer } // namespace writer

View File

@ -214,7 +214,7 @@ struct tint_symbol_3 {
}; };
vertex tint_symbol vert_main() { vertex tint_symbol vert_main() {
Interface const tint_symbol_1 = {0.5f, 0.25f, float4(0.0f)}; Interface const tint_symbol_1 = {0.5f, 0.25f, float4()};
return {tint_symbol_1.col1, tint_symbol_1.col2, tint_symbol_1.pos}; return {tint_symbol_1.col1, tint_symbol_1.col2, tint_symbol_1.pos};
} }

View File

@ -48,7 +48,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float const a = float(0.0f);\n"); EXPECT_EQ(gen.result(), " float const a = float();\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
@ -150,7 +150,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), R"(float3 a = float3(0.0f); EXPECT_EQ(gen.result(), R"(float3 a = float3();
)"); )");
} }

View File

@ -9,7 +9,7 @@ void bar() {
} }
fragment tint_symbol_1 tint_symbol() { fragment tint_symbol_1 tint_symbol() {
float2 a = float2(0.0f); float2 a = float2();
bar(); bar();
return {float4(0.400000006f, 0.400000006f, 0.800000012f, 1.0f)}; return {float4(0.400000006f, 0.400000006f, 0.800000012f, 1.0f)};
} }