writer/msl: Emit field designators for structures

This fixes constructors for structures that contain padding members
due to explicit layout attributes.

Also fix one test that was wrongly using an identity type constructor
for a structure.

Fixed: tint:853
Change-Id: I0a3e84fcd7c6a7f2ad92a4970ed11378e6ce2465
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53240
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
James Price 2021-06-03 18:12:15 +00:00 committed by Tint LUCI CQ
parent 71786c99b3
commit 851b18b2f2
23 changed files with 102 additions and 71 deletions

View File

@ -918,16 +918,24 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
out_ << "(";
}
bool first = true;
int i = 0;
for (auto* e : expr->values()) {
if (!first) {
if (i > 0) {
out_ << ", ";
}
first = false;
if (auto* struct_ty = type->As<sem::Struct>()) {
// Emit field designators for structures to account for padding members.
auto* member = struct_ty->Members()[i]->Declaration();
auto name = program_->Symbols().NameFor(member->symbol());
out_ << "." << name << "=";
}
if (!EmitExpression(e)) {
return false;
}
i++;
}
if (type->IsAnyOf<sem::Array, sem::Struct>()) {

View File

@ -165,7 +165,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr("{1, 2.0f, int3(3, 4, 5)}"));
EXPECT_THAT(gen.result(), HasSubstr("{.a=1, .b=2.0f, .c=int3(3, 4, 5)}"));
}
TEST_F(MslGeneratorImplTest, EmitConstructor_Type_Struct_Empty) {

View File

@ -113,7 +113,7 @@ struct tint_symbol_2 {
fragment tint_symbol_2 frag_main(tint_symbol_1 tint_symbol [[stage_in]]) {
float const foo = tint_symbol.foo;
tint_symbol_2 const tint_symbol_3 = {foo};
tint_symbol_2 const tint_symbol_3 = {.value=foo};
return tint_symbol_3;
}
@ -148,7 +148,7 @@ struct tint_symbol_2 {
fragment tint_symbol_2 frag_main(tint_symbol_1 tint_symbol [[stage_in]]) {
float4 const coord = tint_symbol.coord;
tint_symbol_2 const tint_symbol_3 = {coord.x};
tint_symbol_2 const tint_symbol_3 = {.value=coord.x};
return tint_symbol_3;
}
@ -216,13 +216,13 @@ struct tint_symbol_3 {
};
vertex tint_symbol vert_main() {
Interface const tint_symbol_1 = {0.5f, 0.25f, float4()};
tint_symbol const tint_symbol_4 = {tint_symbol_1.col1, tint_symbol_1.col2, tint_symbol_1.pos};
Interface const tint_symbol_1 = {.col1=0.5f, .col2=0.25f, .pos=float4()};
tint_symbol const tint_symbol_4 = {.col1=tint_symbol_1.col1, .col2=tint_symbol_1.col2, .pos=tint_symbol_1.pos};
return tint_symbol_4;
}
fragment void frag_main(tint_symbol_3 tint_symbol_2 [[stage_in]]) {
Interface const colors = {tint_symbol_2.col1, tint_symbol_2.col2, tint_symbol_2.pos};
Interface const colors = {.col1=tint_symbol_2.col1, .col2=tint_symbol_2.col2, .pos=tint_symbol_2.pos};
float const r = colors.col1;
float const g = colors.col2;
return;
@ -257,13 +257,12 @@ TEST_F(MslGeneratorImplTest,
{});
Func("vert_main1", {}, vertex_output_struct,
{Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.5f)))))},
{Return(Expr(Call("foo", Expr(0.5f))))},
{Stage(ast::PipelineStage::kVertex)});
Func(
"vert_main2", {}, vertex_output_struct,
{Return(Construct(vertex_output_struct, Expr(Call("foo", Expr(0.25f)))))},
{Stage(ast::PipelineStage::kVertex)});
Func("vert_main2", {}, vertex_output_struct,
{Return(Expr(Call("foo", Expr(0.25f))))},
{Stage(ast::PipelineStage::kVertex)});
GeneratorImpl& gen = SanitizeAndBuild();
@ -282,19 +281,19 @@ struct tint_symbol_2 {
};
VertexOutput foo(float x) {
VertexOutput const tint_symbol_4 = {float4(x, x, x, 1.0f)};
VertexOutput const tint_symbol_4 = {.pos=float4(x, x, x, 1.0f)};
return tint_symbol_4;
}
vertex tint_symbol vert_main1() {
VertexOutput const tint_symbol_1 = {foo(0.5f)};
tint_symbol const tint_symbol_5 = {tint_symbol_1.pos};
VertexOutput const tint_symbol_1 = foo(0.5f);
tint_symbol const tint_symbol_5 = {.pos=tint_symbol_1.pos};
return tint_symbol_5;
}
vertex tint_symbol_2 vert_main2() {
VertexOutput const tint_symbol_3 = {foo(0.25f)};
tint_symbol_2 const tint_symbol_6 = {tint_symbol_3.pos};
VertexOutput const tint_symbol_3 = foo(0.25f);
tint_symbol_2 const tint_symbol_6 = {.pos=tint_symbol_3.pos};
return tint_symbol_6;
}

View File

@ -107,7 +107,7 @@ void matrix_matrix_f32() {
}
fragment tint_symbol_1 tint_symbol() {
tint_symbol_1 const tint_symbol_2 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
tint_symbol_1 const tint_symbol_2 = {.value=float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_2;
}

View File

@ -28,17 +28,17 @@ struct tint_symbol_5 {
};
vertex tint_symbol_2 vtx_main(tint_symbol_1 tint_symbol [[stage_in]], constant Uniforms& uniforms [[buffer(0)]]) {
VertexInput const input = {tint_symbol.cur_position, tint_symbol.color};
VertexInput const input = {.cur_position=tint_symbol.cur_position, .color=tint_symbol.color};
VertexOutput output = {};
output.Position = (uniforms.modelViewProjectionMatrix * input.cur_position);
output.vtxFragColor = input.color;
tint_symbol_2 const tint_symbol_6 = {output.vtxFragColor, output.Position};
tint_symbol_2 const tint_symbol_6 = {.vtxFragColor=output.vtxFragColor, .Position=output.Position};
return tint_symbol_6;
}
fragment tint_symbol_5 frag_main(tint_symbol_4 tint_symbol_3 [[stage_in]]) {
float4 const fragColor = tint_symbol_3.fragColor;
tint_symbol_5 const tint_symbol_7 = {fragColor};
tint_symbol_5 const tint_symbol_7 = {.value=fragColor};
return tint_symbol_7;
}

View File

@ -11,7 +11,7 @@ void bar() {
fragment tint_symbol_1 tint_symbol() {
float2 a = float2();
bar();
tint_symbol_1 const tint_symbol_2 = {float4(0.400000006f, 0.400000006f, 0.800000012f, 1.0f)};
tint_symbol_1 const tint_symbol_2 = {.value=float4(0.400000006f, 0.400000006f, 0.800000012f, 1.0f)};
return tint_symbol_2;
}

View File

@ -15,7 +15,7 @@ struct tint_symbol_2 {
};
fragment void tint_symbol(tint_symbol_2 tint_symbol_1 [[stage_in]]) {
FragmentInputs const inputs = {tint_symbol_1.position, tint_symbol_1.front_facing, tint_symbol_1.sample_index, tint_symbol_1.sample_mask};
FragmentInputs const inputs = {.position=tint_symbol_1.position, .front_facing=tint_symbol_1.front_facing, .sample_index=tint_symbol_1.sample_index, .sample_mask=tint_symbol_1.sample_mask};
if (inputs.front_facing) {
float4 const foo = inputs.position;
uint const bar = (inputs.sample_index + inputs.sample_mask);

View File

@ -15,7 +15,7 @@ struct tint_symbol_2 {
};
fragment void tint_symbol(tint_symbol_2 tint_symbol_1 [[stage_in]]) {
FragmentInputs const inputs = {tint_symbol_1.loc0, tint_symbol_1.loc1, tint_symbol_1.loc2, tint_symbol_1.loc3};
FragmentInputs const inputs = {.loc0=tint_symbol_1.loc0, .loc1=tint_symbol_1.loc1, .loc2=tint_symbol_1.loc2, .loc3=tint_symbol_1.loc3};
int const i = inputs.loc0;
uint const u = inputs.loc1;
float const f = inputs.loc2;

View File

@ -21,11 +21,11 @@ struct tint_symbol_2 {
};
fragment void tint_symbol(tint_symbol_2 tint_symbol_1 [[stage_in]]) {
FragmentInputs0 const inputs0 = {tint_symbol_1.position, tint_symbol_1.loc0};
FragmentInputs0 const inputs0 = {.position=tint_symbol_1.position, .loc0=tint_symbol_1.loc0};
bool const front_facing = tint_symbol_1.front_facing;
uint const loc1 = tint_symbol_1.loc1;
uint const sample_index = tint_symbol_1.sample_index;
FragmentInputs1 const inputs1 = {tint_symbol_1.loc3, tint_symbol_1.sample_mask};
FragmentInputs1 const inputs1 = {.loc3=tint_symbol_1.loc3, .sample_mask=tint_symbol_1.sample_mask};
float const loc2 = tint_symbol_1.loc2;
if (front_facing) {
float4 const foo = inputs0.position;

View File

@ -9,12 +9,12 @@ struct tint_symbol_1 {
};
fragment tint_symbol main1() {
tint_symbol const tint_symbol_2 = {1.0f};
tint_symbol const tint_symbol_2 = {.value=1.0f};
return tint_symbol_2;
}
fragment tint_symbol_1 main2() {
tint_symbol_1 const tint_symbol_3 = {1u};
tint_symbol_1 const tint_symbol_3 = {.value=1u};
return tint_symbol_3;
}

View File

@ -11,8 +11,8 @@ struct tint_symbol_1 {
};
fragment tint_symbol_1 tint_symbol() {
FragmentOutputs const tint_symbol_2 = {1.0f, 1u};
tint_symbol_1 const tint_symbol_3 = {tint_symbol_2.frag_depth, tint_symbol_2.sample_mask};
FragmentOutputs const tint_symbol_2 = {.frag_depth=1.0f, .sample_mask=1u};
tint_symbol_1 const tint_symbol_3 = {.frag_depth=tint_symbol_2.frag_depth, .sample_mask=tint_symbol_2.sample_mask};
return tint_symbol_3;
}

View File

@ -15,22 +15,22 @@ struct tint_symbol_3 {
};
fragment tint_symbol main0() {
tint_symbol const tint_symbol_4 = {1};
tint_symbol const tint_symbol_4 = {.value=1};
return tint_symbol_4;
}
fragment tint_symbol_1 main1() {
tint_symbol_1 const tint_symbol_5 = {1u};
tint_symbol_1 const tint_symbol_5 = {.value=1u};
return tint_symbol_5;
}
fragment tint_symbol_2 main2() {
tint_symbol_2 const tint_symbol_6 = {1.0f};
tint_symbol_2 const tint_symbol_6 = {.value=1.0f};
return tint_symbol_6;
}
fragment tint_symbol_3 main3() {
tint_symbol_3 const tint_symbol_7 = {float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_3 const tint_symbol_7 = {.value=float4(1.0f, 2.0f, 3.0f, 4.0f)};
return tint_symbol_7;
}

View File

@ -15,8 +15,8 @@ struct tint_symbol_1 {
};
fragment tint_symbol_1 tint_symbol() {
FragmentOutputs const tint_symbol_2 = {1, 1u, 1.0f, float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {tint_symbol_2.loc0, tint_symbol_2.loc1, tint_symbol_2.loc2, tint_symbol_2.loc3};
FragmentOutputs const tint_symbol_2 = {.loc0=1, .loc1=1u, .loc2=1.0f, .loc3=float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {.loc0=tint_symbol_2.loc0, .loc1=tint_symbol_2.loc1, .loc2=tint_symbol_2.loc2, .loc3=tint_symbol_2.loc3};
return tint_symbol_3;
}

View File

@ -19,8 +19,8 @@ struct tint_symbol_1 {
};
fragment tint_symbol_1 tint_symbol() {
FragmentOutputs const tint_symbol_2 = {1, 2.0f, 1u, 1.0f, 2u, float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {tint_symbol_2.loc0, tint_symbol_2.loc1, tint_symbol_2.loc2, tint_symbol_2.loc3, tint_symbol_2.frag_depth, tint_symbol_2.sample_mask};
FragmentOutputs const tint_symbol_2 = {.loc0=1, .frag_depth=2.0f, .loc1=1u, .loc2=1.0f, .sample_mask=2u, .loc3=float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {.loc0=tint_symbol_2.loc0, .loc1=tint_symbol_2.loc1, .loc2=tint_symbol_2.loc2, .loc3=tint_symbol_2.loc3, .frag_depth=tint_symbol_2.frag_depth, .sample_mask=tint_symbol_2.sample_mask};
return tint_symbol_3;
}

View File

@ -18,13 +18,13 @@ struct tint_symbol_3 {
};
vertex tint_symbol vert_main() {
Interface const tint_symbol_1 = {0.400000006f, 0.600000024f, float4()};
tint_symbol const tint_symbol_4 = {tint_symbol_1.col1, tint_symbol_1.col2, tint_symbol_1.pos};
Interface const tint_symbol_1 = {.col1=0.400000006f, .col2=0.600000024f, .pos=float4()};
tint_symbol const tint_symbol_4 = {.col1=tint_symbol_1.col1, .col2=tint_symbol_1.col2, .pos=tint_symbol_1.pos};
return tint_symbol_4;
}
fragment void frag_main(tint_symbol_3 tint_symbol_2 [[stage_in]]) {
Interface const colors = {tint_symbol_2.col1, tint_symbol_2.col2, tint_symbol_2.pos};
Interface const colors = {.col1=tint_symbol_2.col1, .col2=tint_symbol_2.col2, .pos=tint_symbol_2.pos};
float const r = colors.col1;
float const g = colors.col2;
return;

View File

@ -7,7 +7,7 @@ struct VertexOutput {
};
VertexOutput foo(float x) {
VertexOutput const tint_symbol = {float4(x, x, x, 1.0f), 42};
VertexOutput const tint_symbol = {.pos=float4(x, x, x, 1.0f), .loc0=42};
return tint_symbol;
}

View File

@ -1 +1,25 @@
SKIP: crbug.com/tint/853 type constructors broken with explicit layout structs
#include <metal_stdlib>
using namespace metal;
struct S {
/* 0x0000 */ float f;
/* 0x0004 */ uint u;
/* 0x0008 */ int8_t tint_pad_0[120];
/* 0x0080 */ packed_float4 v;
/* 0x0090 */ int8_t tint_pad_1[112];
};
struct tint_symbol_1 {
float f [[user(locn0)]];
uint u [[user(locn1)]];
float4 v [[position]];
};
fragment void frag_main(tint_symbol_1 tint_symbol [[stage_in]], device S& output [[buffer(0)]]) {
S const input = {.f=tint_symbol.f, .u=tint_symbol.u, .v=tint_symbol.v};
float const f = input.f;
uint const u = input.u;
float4 const v = input.v;
output = input;
return;
}

View File

@ -20,7 +20,7 @@ vertex tint_symbol_3 tint_symbol(tint_symbol_2 tint_symbol_1 [[stage_in]]) {
uint const u = loc1;
float const f = loc2;
float4 const v = loc3;
tint_symbol_3 const tint_symbol_4 = {float4()};
tint_symbol_3 const tint_symbol_4 = {.value=float4()};
return tint_symbol_4;
}

View File

@ -18,12 +18,12 @@ struct tint_symbol_3 {
};
vertex tint_symbol_3 tint_symbol(tint_symbol_2 tint_symbol_1 [[stage_in]]) {
VertexInputs const inputs = {tint_symbol_1.loc0, tint_symbol_1.loc1, tint_symbol_1.loc2, tint_symbol_1.loc3};
VertexInputs const inputs = {.loc0=tint_symbol_1.loc0, .loc1=tint_symbol_1.loc1, .loc2=tint_symbol_1.loc2, .loc3=tint_symbol_1.loc3};
int const i = inputs.loc0;
uint const u = inputs.loc1;
float const f = inputs.loc2;
float4 const v = inputs.loc3;
tint_symbol_3 const tint_symbol_4 = {float4()};
tint_symbol_3 const tint_symbol_4 = {.value=float4()};
return tint_symbol_4;
}

View File

@ -6,7 +6,7 @@ struct tint_symbol_1 {
};
vertex tint_symbol_1 tint_symbol() {
tint_symbol_1 const tint_symbol_2 = {float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_2 = {.value=float4(1.0f, 2.0f, 3.0f, 4.0f)};
return tint_symbol_2;
}

View File

@ -9,8 +9,8 @@ struct tint_symbol_1 {
};
vertex tint_symbol_1 tint_symbol() {
VertexOutputs const tint_symbol_2 = {float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {tint_symbol_2.position};
VertexOutputs const tint_symbol_2 = {.position=float4(1.0f, 2.0f, 3.0f, 4.0f)};
tint_symbol_1 const tint_symbol_3 = {.position=tint_symbol_2.position};
return tint_symbol_3;
}

View File

@ -17,8 +17,8 @@ struct tint_symbol_1 {
};
vertex tint_symbol_1 tint_symbol() {
VertexOutputs const tint_symbol_2 = {1, 1u, 1.0f, float4(1.0f, 2.0f, 3.0f, 4.0f), float4()};
tint_symbol_1 const tint_symbol_3 = {tint_symbol_2.loc0, tint_symbol_2.loc1, tint_symbol_2.loc2, tint_symbol_2.loc3, tint_symbol_2.position};
VertexOutputs const tint_symbol_2 = {.loc0=1, .loc1=1u, .loc2=1.0f, .loc3=float4(1.0f, 2.0f, 3.0f, 4.0f), .position=float4()};
tint_symbol_1 const tint_symbol_3 = {.loc0=tint_symbol_2.loc0, .loc1=tint_symbol_2.loc1, .loc2=tint_symbol_2.loc2, .loc3=tint_symbol_2.loc3, .position=tint_symbol_2.position};
return tint_symbol_3;
}

View File

@ -29,38 +29,38 @@ struct tint_array_wrapper_1 {
kernel void tint_symbol() {
int const x = 42;
S1 const empty = {};
S1 const nonempty = {1, 2, 3, 4};
S1 const nonempty_with_expr = {1, x, (x + 1), nonempty.d};
S1 const nonempty = {.a=1, .b=2, .c=3, .d=4};
S1 const nonempty_with_expr = {.a=1, .b=x, .c=(x + 1), .d=nonempty.d};
S3 const nested_empty = {};
S1 const tint_symbol_1 = {2, 3, 4, 5};
S1 const tint_symbol_2 = {7, 8, 9, 10};
S2 const tint_symbol_3 = {6, tint_symbol_2};
S3 const nested_nonempty = {1, tint_symbol_1, tint_symbol_3};
S1 const tint_symbol_4 = {2, x, (x + 1), nested_nonempty.i.f.d};
S2 const tint_symbol_5 = {6, nonempty};
S3 const nested_nonempty_with_expr = {1, tint_symbol_4, tint_symbol_5};
S1 const tint_symbol_1 = {.a=2, .b=3, .c=4, .d=5};
S1 const tint_symbol_2 = {.a=7, .b=8, .c=9, .d=10};
S2 const tint_symbol_3 = {.e=6, .f=tint_symbol_2};
S3 const nested_nonempty = {.g=1, .h=tint_symbol_1, .i=tint_symbol_3};
S1 const tint_symbol_4 = {.a=2, .b=x, .c=(x + 1), .d=nested_nonempty.i.f.d};
S2 const tint_symbol_5 = {.e=6, .f=nonempty};
S3 const nested_nonempty_with_expr = {.g=1, .h=tint_symbol_4, .i=tint_symbol_5};
S1 const tint_symbol_6 = {};
int const subexpr_empty = tint_symbol_6.a;
S1 const tint_symbol_7 = {1, 2, 3, 4};
S1 const tint_symbol_7 = {.a=1, .b=2, .c=3, .d=4};
int const subexpr_nonempty = tint_symbol_7.b;
S1 const tint_symbol_8 = {1, x, (x + 1), nonempty.d};
S1 const tint_symbol_8 = {.a=1, .b=x, .c=(x + 1), .d=nonempty.d};
int const subexpr_nonempty_with_expr = tint_symbol_8.c;
S2 const tint_symbol_9 = {};
S1 const subexpr_nested_empty = tint_symbol_9.f;
S1 const tint_symbol_10 = {2, 3, 4, 5};
S2 const tint_symbol_11 = {1, tint_symbol_10};
S1 const tint_symbol_10 = {.a=2, .b=3, .c=4, .d=5};
S2 const tint_symbol_11 = {.e=1, .f=tint_symbol_10};
S1 const subexpr_nested_nonempty = tint_symbol_11.f;
S1 const tint_symbol_12 = {2, x, (x + 1), nested_nonempty.i.f.d};
S2 const tint_symbol_13 = {1, tint_symbol_12};
S1 const tint_symbol_12 = {.a=2, .b=x, .c=(x + 1), .d=nested_nonempty.i.f.d};
S2 const tint_symbol_13 = {.e=1, .f=tint_symbol_12};
S1 const subexpr_nested_nonempty_with_expr = tint_symbol_13.f;
tint_array_wrapper_1 const aosoa_empty = {};
tint_array_wrapper_0 const tint_symbol_14 = {1, 2};
T const tint_symbol_15 = {tint_symbol_14};
T const tint_symbol_15 = {.a=tint_symbol_14};
tint_array_wrapper_0 const tint_symbol_16 = {3, 4};
T const tint_symbol_17 = {tint_symbol_16};
T const tint_symbol_17 = {.a=tint_symbol_16};
tint_array_wrapper_1 const aosoa_nonempty = {tint_symbol_15, tint_symbol_17};
tint_array_wrapper_0 const tint_symbol_18 = {1, (aosoa_nonempty.array[0].a.array[0] + 1)};
T const tint_symbol_19 = {tint_symbol_18};
T const tint_symbol_19 = {.a=tint_symbol_18};
tint_array_wrapper_1 const aosoa_nonempty_with_expr = {tint_symbol_19, aosoa_nonempty.array[1]};
return;
}