writer/msl: Use UniqueIdentifier() for padding field names

UniqueIdentifier() will generate a program-global unique symbol.

MslGeneratorImplTest.AttemptTintPadSymbolCollision tests for collisions with the field names.
TextGeneratorTest.UniqueIdentifier_ConflictWithExisting tests for collisions between general symbols.

Fixed: tint:654
Change-Id: If2ba75d04ff0e2a9975e878596ac114d51adcd46
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56580
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-06-30 16:01:40 +00:00 committed by Tint LUCI CQ
parent fb4e751258
commit 885488da41
8 changed files with 25 additions and 26 deletions

View File

@ -1885,11 +1885,10 @@ bool GeneratorImpl::EmitStructType(const sem::Struct* str) {
out.flags(saved_flag_state); out.flags(saved_flag_state);
}; };
uint32_t pad_count = 0;
auto add_padding = [&](uint32_t size, uint32_t msl_offset) { auto add_padding = [&](uint32_t size, uint32_t msl_offset) {
std::string name; std::string name;
do { do {
name = "tint_pad_" + std::to_string(pad_count++); name = UniqueIdentifier("tint_pad");
} while (str->FindMember(program_->Symbols().Get(name))); } while (str->FindMember(program_->Symbols().Get(name)));
auto out = line(); auto out = line();

View File

@ -133,7 +133,7 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayWithStride) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_THAT(gen.result(), HasSubstr(R"(struct tint_padded_array_element { EXPECT_THAT(gen.result(), HasSubstr(R"(struct tint_padded_array_element {
/* 0x0000 */ float el; /* 0x0000 */ float el;
/* 0x0004 */ int8_t tint_pad_0[60]; /* 0x0004 */ int8_t tint_pad[60];
};)")); };)"));
EXPECT_THAT(gen.result(), HasSubstr(R"(struct tint_array_wrapper { EXPECT_THAT(gen.result(), HasSubstr(R"(struct tint_array_wrapper {
/* 0x0000 */ tint_padded_array_element arr[4]; /* 0x0000 */ tint_padded_array_element arr[4];
@ -275,7 +275,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_NonComposites) {
// for each field of the structure s. // for each field of the structure s.
#define ALL_FIELDS() \ #define ALL_FIELDS() \
FIELD(0x0000, int, a, /*NO SUFFIX*/) \ FIELD(0x0000, int, a, /*NO SUFFIX*/) \
FIELD(0x0004, int8_t, tint_pad_0, [124]) \ FIELD(0x0004, int8_t, tint_pad, [124]) \
FIELD(0x0080, float, b, /*NO SUFFIX*/) \ FIELD(0x0080, float, b, /*NO SUFFIX*/) \
FIELD(0x0084, int8_t, tint_pad_1, [124]) \ FIELD(0x0084, int8_t, tint_pad_1, [124]) \
FIELD(0x0100, packed_float2, c, /*NO SUFFIX*/) \ FIELD(0x0100, packed_float2, c, /*NO SUFFIX*/) \
@ -384,7 +384,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_Structures) {
// for each field of the structure s. // for each field of the structure s.
#define ALL_FIELDS() \ #define ALL_FIELDS() \
FIELD(0x0000, int, a, /*NO SUFFIX*/) \ FIELD(0x0000, int, a, /*NO SUFFIX*/) \
FIELD(0x0004, int8_t, tint_pad_0, [508]) \ FIELD(0x0004, int8_t, tint_pad, [508]) \
FIELD(0x0200, inner_x, b, /*NO SUFFIX*/) \ FIELD(0x0200, inner_x, b, /*NO SUFFIX*/) \
FIELD(0x0600, float, c, /*NO SUFFIX*/) \ FIELD(0x0600, float, c, /*NO SUFFIX*/) \
FIELD(0x0604, inner_y, d, /*NO SUFFIX*/) \ FIELD(0x0604, inner_y, d, /*NO SUFFIX*/) \
@ -476,14 +476,14 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_ArrayDefaultStride) {
// ALL_FIELDS() calls the macro FIELD(ADDR, TYPE, NAME, SUFFIX) // ALL_FIELDS() calls the macro FIELD(ADDR, TYPE, NAME, SUFFIX)
// for each field of the structure s. // for each field of the structure s.
#define ALL_FIELDS() \ #define ALL_FIELDS() \
FIELD(0x0000, int, a, /*NO SUFFIX*/) \ FIELD(0x0000, int, a, /*NO SUFFIX*/) \
FIELD(0x0004, float, b, [7]) \ FIELD(0x0004, float, b, [7]) \
FIELD(0x0020, float, c, /*NO SUFFIX*/) \ FIELD(0x0020, float, c, /*NO SUFFIX*/) \
FIELD(0x0024, int8_t, tint_pad_0, [476]) \ FIELD(0x0024, int8_t, tint_pad, [476]) \
FIELD(0x0200, inner, d, [4]) \ FIELD(0x0200, inner, d, [4]) \
FIELD(0x1200, float, e, /*NO SUFFIX*/) \ FIELD(0x1200, float, e, /*NO SUFFIX*/) \
FIELD(0x1204, float, f, [1]) \ FIELD(0x1204, float, f, [1]) \
FIELD(0x1208, int8_t, tint_pad_1, [504]) FIELD(0x1208, int8_t, tint_pad_1, [504])
// Check that the generated string is as expected. // Check that the generated string is as expected.
@ -561,11 +561,11 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_Layout_ArrayVec3DefaultStride) {
// ALL_FIELDS() calls the macro FIELD(ADDR, TYPE, NAME, SUFFIX) // ALL_FIELDS() calls the macro FIELD(ADDR, TYPE, NAME, SUFFIX)
// for each field of the structure s. // for each field of the structure s.
#define ALL_FIELDS() \ #define ALL_FIELDS() \
FIELD(0x0000, int, a, /*NO SUFFIX*/) \ FIELD(0x0000, int, a, /*NO SUFFIX*/) \
FIELD(0x0004, int8_t, tint_pad_0, [12]) \ FIELD(0x0004, int8_t, tint_pad, [12]) \
FIELD(0x0010, float3, b, [4]) \ FIELD(0x0010, float3, b, [4]) \
FIELD(0x0050, int, c, /*NO SUFFIX*/) \ FIELD(0x0050, int, c, /*NO SUFFIX*/) \
FIELD(0x0054, int8_t, tint_pad_1, [12]) FIELD(0x0054, int8_t, tint_pad_1, [12])
// Check that the generated string is as expected. // Check that the generated string is as expected.
@ -592,7 +592,7 @@ TEST_F(MslGeneratorImplTest, AttemptTintPadSymbolCollision) {
Member("tint_pad_27", ty.mat2x2<f32>()), Member("tint_pad_27", ty.mat2x2<f32>()),
Member("tint_pad_24", ty.u32()), Member("tint_pad_24", ty.u32()),
Member("tint_pad_23", ty.mat2x3<f32>()), Member("tint_pad_23", ty.mat2x3<f32>()),
Member("tint_pad_0", ty.u32()), Member("tint_pad", ty.u32()),
Member("tint_pad_8", ty.mat2x4<f32>()), Member("tint_pad_8", ty.mat2x4<f32>()),
Member("tint_pad_26", ty.u32()), Member("tint_pad_26", ty.u32()),
Member("tint_pad_29", ty.mat3x2<f32>()), Member("tint_pad_29", ty.mat3x2<f32>()),
@ -637,7 +637,7 @@ TEST_F(MslGeneratorImplTest, AttemptTintPadSymbolCollision) {
/* 0x0148 */ uint tint_pad_24; /* 0x0148 */ uint tint_pad_24;
/* 0x014c */ int8_t tint_pad_14[4]; /* 0x014c */ int8_t tint_pad_14[4];
/* 0x0150 */ float2x3 tint_pad_23; /* 0x0150 */ float2x3 tint_pad_23;
/* 0x0170 */ uint tint_pad_0; /* 0x0170 */ uint tint_pad;
/* 0x0174 */ int8_t tint_pad_15[12]; /* 0x0174 */ int8_t tint_pad_15[12];
/* 0x0180 */ float2x4 tint_pad_8; /* 0x0180 */ float2x4 tint_pad_8;
/* 0x01a0 */ uint tint_pad_26; /* 0x01a0 */ uint tint_pad_26;

View File

@ -3,7 +3,7 @@
using namespace metal; using namespace metal;
struct tint_padded_array_element { struct tint_padded_array_element {
/* 0x0000 */ int el; /* 0x0000 */ int el;
/* 0x0004 */ int8_t tint_pad_0[12]; /* 0x0004 */ int8_t tint_pad[12];
}; };
struct tint_array_wrapper { struct tint_array_wrapper {
/* 0x0000 */ tint_padded_array_element arr[4]; /* 0x0000 */ tint_padded_array_element arr[4];

View File

@ -3,7 +3,7 @@
using namespace metal; using namespace metal;
struct tint_padded_array_element { struct tint_padded_array_element {
/* 0x0000 */ int el; /* 0x0000 */ int el;
/* 0x0004 */ int8_t tint_pad_0[12]; /* 0x0004 */ int8_t tint_pad[12];
}; };
struct tint_array_wrapper { struct tint_array_wrapper {
/* 0x0000 */ tint_padded_array_element arr[4]; /* 0x0000 */ tint_padded_array_element arr[4];

View File

@ -3,7 +3,7 @@
using namespace metal; using namespace metal;
struct tint_padded_array_element { struct tint_padded_array_element {
/* 0x0000 */ int el; /* 0x0000 */ int el;
/* 0x0004 */ int8_t tint_pad_0[12]; /* 0x0004 */ int8_t tint_pad[12];
}; };
struct tint_array_wrapper { struct tint_array_wrapper {
/* 0x0000 */ tint_padded_array_element arr[4]; /* 0x0000 */ tint_padded_array_element arr[4];

View File

@ -3,7 +3,7 @@
using namespace metal; using namespace metal;
struct tint_padded_array_element { struct tint_padded_array_element {
/* 0x0000 */ int el; /* 0x0000 */ int el;
/* 0x0004 */ int8_t tint_pad_0[12]; /* 0x0004 */ int8_t tint_pad[12];
}; };
struct tint_array_wrapper { struct tint_array_wrapper {
/* 0x0000 */ tint_padded_array_element arr[4]; /* 0x0000 */ tint_padded_array_element arr[4];

View File

@ -3,7 +3,7 @@
using namespace metal; using namespace metal;
struct Light { struct Light {
/* 0x0000 */ packed_float3 position; /* 0x0000 */ packed_float3 position;
/* 0x000c */ int8_t tint_pad_0[4]; /* 0x000c */ int8_t tint_pad[4];
/* 0x0010 */ packed_float3 colour; /* 0x0010 */ packed_float3 colour;
/* 0x001c */ int8_t tint_pad_1[4]; /* 0x001c */ int8_t tint_pad_1[4];
}; };

View File

@ -4,7 +4,7 @@ using namespace metal;
struct S { struct S {
/* 0x0000 */ float f; /* 0x0000 */ float f;
/* 0x0004 */ uint u; /* 0x0004 */ uint u;
/* 0x0008 */ int8_t tint_pad_0[120]; /* 0x0008 */ int8_t tint_pad[120];
/* 0x0080 */ packed_float4 v; /* 0x0080 */ packed_float4 v;
/* 0x0090 */ int8_t tint_pad_1[112]; /* 0x0090 */ int8_t tint_pad_1[112];
}; };