Fix symbol collision in RenameReservedKeywords()

Fixed: tint:711
Bug: tint:712
Change-Id: I8d0b5c926772c4265d273f6420f101f48ac29b9c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47628
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-04-13 17:53:09 +00:00 committed by Commit Bot service account
parent ebc5bd4e28
commit 095cd1c255
4 changed files with 37 additions and 34 deletions

View File

@ -67,6 +67,9 @@ void Transform::RenameReservedKeywords(CloneContext* ctx,
const char* names[], const char* names[],
size_t count) { size_t count) {
ctx->ReplaceAll([=](Symbol in) { ctx->ReplaceAll([=](Symbol in) {
if (!ctx->src->Symbols().HasName(in)) {
return ctx->dst->Symbols().New();
}
auto name_in = ctx->src->Symbols().NameFor(in); auto name_in = ctx->src->Symbols().NameFor(in);
if (!std::binary_search(names, names + count, name_in)) { if (!std::binary_search(names, names + count, name_in)) {
return ctx->dst->Symbols().Register(name_in); return ctx->dst->Symbols().Register(name_in);

View File

@ -119,15 +119,15 @@ TEST_F(HlslGeneratorImplTest_Function,
GeneratorImpl& gen = SanitizeAndBuild(); GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct tint_symbol_1 { EXPECT_EQ(result(), R"(struct tint_symbol_5 {
float foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
struct tint_symbol_2 { struct tint_symbol_2 {
float value : SV_Target1; float value : SV_Target1;
}; };
tint_symbol_2 frag_main(tint_symbol_1 tint_symbol_6) { tint_symbol_2 frag_main(tint_symbol_5 tint_symbol_7) {
const float foo = tint_symbol_6.foo; const float foo = tint_symbol_7.foo;
const tint_symbol_2 tint_symbol_1 = {foo}; const tint_symbol_2 tint_symbol_1 = {foo};
return tint_symbol_1; return tint_symbol_1;
} }
@ -153,15 +153,15 @@ TEST_F(HlslGeneratorImplTest_Function,
GeneratorImpl& gen = SanitizeAndBuild(); GeneratorImpl& gen = SanitizeAndBuild();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct tint_symbol_1 { EXPECT_EQ(result(), R"(struct tint_symbol_6 {
float4 coord : SV_Position; float4 coord : SV_Position;
}; };
struct tint_symbol_2 { struct tint_symbol_2 {
float value : SV_Depth; float value : SV_Depth;
}; };
tint_symbol_2 frag_main(tint_symbol_1 tint_symbol_6) { tint_symbol_2 frag_main(tint_symbol_6 tint_symbol_8) {
const float4 coord = tint_symbol_6.coord; const float4 coord = tint_symbol_8.coord;
const tint_symbol_2 tint_symbol_1 = {coord.x}; const tint_symbol_2 tint_symbol_1 = {coord.x};
return tint_symbol_1; return tint_symbol_1;
} }
@ -214,7 +214,7 @@ struct tint_symbol_2 {
float col1 : TEXCOORD1; float col1 : TEXCOORD1;
float col2 : TEXCOORD2; float col2 : TEXCOORD2;
}; };
struct tint_symbol_7 { struct tint_symbol_8 {
float col1 : TEXCOORD1; float col1 : TEXCOORD1;
float col2 : TEXCOORD2; float col2 : TEXCOORD2;
}; };
@ -225,8 +225,8 @@ tint_symbol_2 vert_main() {
return tint_symbol_1; return tint_symbol_1;
} }
void frag_main(tint_symbol_7 tint_symbol_9) { void frag_main(tint_symbol_8 tint_symbol_10) {
const Interface colors = {tint_symbol_9.col1, tint_symbol_9.col2}; const Interface colors = {tint_symbol_10.col1, tint_symbol_10.col2};
const float r = colors.col1; const float r = colors.col1;
const float g = colors.col2; const float g = colors.col2;
return; return;

View File

@ -62,10 +62,10 @@ TEST_F(HlslSanitizerTest, ArrayLength) {
ByteAddressBuffer sb : register(t0, space1); ByteAddressBuffer sb : register(t0, space1);
void main() { void main() {
uint tint_symbol_7 = 0u; uint tint_symbol_9 = 0u;
sb.GetDimensions(tint_symbol_7); sb.GetDimensions(tint_symbol_9);
const uint tint_symbol_9 = ((tint_symbol_7 - 16u) / 16u); const uint tint_symbol_10 = ((tint_symbol_9 - 16u) / 16u);
uint len = tint_symbol_9; uint len = tint_symbol_10;
return; return;
} }

View File

@ -108,15 +108,15 @@ TEST_F(MslGeneratorImplTest, Emit_Decoration_EntryPoint_WithInOutVars) {
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal; using namespace metal;
struct tint_symbol_3 { struct tint_symbol_1 {
float foo [[user(locn0)]]; float foo [[user(locn0)]];
}; };
struct tint_symbol_5 { struct tint_symbol_3 {
float value [[color(1)]]; float value [[color(1)]];
}; };
fragment tint_symbol_5 frag_main(tint_symbol_3 tint_symbol_1 [[stage_in]]) { fragment tint_symbol_3 frag_main(tint_symbol_1 tint_symbol_6 [[stage_in]]) {
const float foo = tint_symbol_1.foo; const float foo = tint_symbol_6.foo;
return {foo}; return {foo};
} }
@ -141,15 +141,15 @@ TEST_F(MslGeneratorImplTest, Emit_Decoration_EntryPoint_WithInOut_Builtins) {
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
using namespace metal; using namespace metal;
struct tint_symbol_3 { struct tint_symbol_1 {
float4 coord [[position]]; float4 coord [[position]];
}; };
struct tint_symbol_5 { struct tint_symbol_3 {
float value [[depth(any)]]; float value [[depth(any)]];
}; };
fragment tint_symbol_5 frag_main(tint_symbol_3 tint_symbol_1 [[stage_in]]) { fragment tint_symbol_3 frag_main(tint_symbol_1 tint_symbol_6 [[stage_in]]) {
const float4 coord = tint_symbol_1.coord; const float4 coord = tint_symbol_6.coord;
return {coord.x}; return {coord.x};
} }
@ -202,18 +202,18 @@ struct tint_symbol_4 {
float col1 [[user(locn1)]]; float col1 [[user(locn1)]];
float col2 [[user(locn2)]]; float col2 [[user(locn2)]];
}; };
struct tint_symbol_9 { struct tint_symbol_7 {
float col1 [[user(locn1)]]; float col1 [[user(locn1)]];
float col2 [[user(locn2)]]; float col2 [[user(locn2)]];
}; };
vertex tint_symbol_4 vert_main() { vertex tint_symbol_4 vert_main() {
const Interface tint_symbol_5 = {0.5f, 0.25f}; const Interface tint_symbol_6 = {0.5f, 0.25f};
return {tint_symbol_5.col1, tint_symbol_5.col2}; return {tint_symbol_6.col1, tint_symbol_6.col2};
} }
fragment void frag_main(tint_symbol_9 tint_symbol_7 [[stage_in]]) { fragment void frag_main(tint_symbol_7 tint_symbol_9 [[stage_in]]) {
const Interface colors = {tint_symbol_7.col1, tint_symbol_7.col2}; const Interface colors = {tint_symbol_9.col1, tint_symbol_9.col2};
const float r = colors.col1; const float r = colors.col1;
const float g = colors.col2; const float g = colors.col2;
return; return;
@ -266,10 +266,10 @@ using namespace metal;
struct VertexOutput { struct VertexOutput {
float4 pos; float4 pos;
}; };
struct tint_symbol_3 { struct tint_symbol_5 {
float4 pos [[position]]; float4 pos [[position]];
}; };
struct tint_symbol_7 { struct tint_symbol_8 {
float4 pos [[position]]; float4 pos [[position]];
}; };
@ -277,14 +277,14 @@ VertexOutput foo(float x) {
return {float4(x, x, x, 1.0f)}; return {float4(x, x, x, 1.0f)};
} }
vertex tint_symbol_3 vert_main1() { vertex tint_symbol_5 vert_main1() {
const VertexOutput tint_symbol_5 = {foo(0.5f)}; const VertexOutput tint_symbol_7 = {foo(0.5f)};
return {tint_symbol_5.pos}; return {tint_symbol_7.pos};
} }
vertex tint_symbol_7 vert_main2() { vertex tint_symbol_8 vert_main2() {
const VertexOutput tint_symbol_8 = {foo(0.25f)}; const VertexOutput tint_symbol_10 = {foo(0.25f)};
return {tint_symbol_8.pos}; return {tint_symbol_10.pos};
} }
)"); )");