transform/BoundArrayAccessors: Fix symbol renaming

We need to pre-clone the source program's symbols before creating the symbols `min` and `arrayLength`, otherwise the original program's symbols will be suffixed with a number to make them unique.

Fixes Dawn tests that triggered this issue.

Bug: tint:712
Change-Id: Ie1cf6cbcf2050a2ce1a94acf0ae131a06f635820
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/47761
Auto-Submit: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-04-14 14:29:28 +00:00 committed by Commit Bot service account
parent 3dd4fae775
commit f8313e5a6e
2 changed files with 50 additions and 2 deletions

View File

@ -29,6 +29,11 @@ BoundArrayAccessors::~BoundArrayAccessors() = default;
Transform::Output BoundArrayAccessors::Run(const Program* in, const DataMap&) { Transform::Output BoundArrayAccessors::Run(const Program* in, const DataMap&) {
ProgramBuilder out; ProgramBuilder out;
CloneContext ctx(&out, in); CloneContext ctx(&out, in);
// Start by cloning all the symbols. This ensures that the authored symbols
// won't get renamed if they collide with new symbols below.
ctx.CloneSymbols();
ctx.ReplaceAll([&](ast::ArrayAccessorExpression* expr) { ctx.ReplaceAll([&](ast::ArrayAccessorExpression* expr) {
return Transform(expr, &ctx); return Transform(expr, &ctx);
}); });

View File

@ -540,7 +540,7 @@ struct S {
a : f32; a : f32;
b : array<f32>; b : array<f32>;
}; };
var<in> s : S; var<storage> s : S;
fn f() { fn f() {
var d : f32 = s.b[25]; var d : f32 = s.b[25];
@ -554,7 +554,7 @@ struct S {
b : array<f32>; b : array<f32>;
}; };
var<in> s : S; var<storage> s : S;
fn f() { fn f() {
var d : f32 = s.b[min(u32(25), (arrayLength(s.b) - 1u))]; var d : f32 = s.b[min(u32(25), (arrayLength(s.b) - 1u))];
@ -592,6 +592,49 @@ TEST_F(BoundArrayAccessorsTest, DISABLED_Scoped_Variable) {
FAIL(); FAIL();
} }
// Check that existing use of min() and arrayLength() do not get renamed.
TEST_F(BoundArrayAccessorsTest, DontRenameSymbols) {
auto* src = R"(
[[block]]
struct S {
a : f32;
b : array<f32>;
};
var<storage> s : S;
let c : u32 = 1u;
fn f() {
let b : f32 = s.b[c];
let x : i32 = min(1, 2);
let y : u32 = arrayLength(s.b);
}
)";
auto* expect = R"(
[[block]]
struct S {
a : f32;
b : array<f32>;
};
var<storage> s : S;
let c : u32 = 1u;
fn f() {
let b : f32 = s.b[min(u32(c), (arrayLength(s.b) - 1u))];
let x : i32 = min(1, 2);
let y : u32 = arrayLength(s.b);
}
)";
auto got = Run<BoundArrayAccessors>(src);
EXPECT_EQ(expect, str(got));
}
} // namespace } // namespace
} // namespace transform } // namespace transform
} // namespace tint } // namespace tint