tint/transform: Skip Unshadow if possible

Change-Id: I73b7213aafe3ff25a5f6526e7002d2ec3dda57c3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/116871
Kokoro: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2023-01-12 10:36:27 +00:00 committed by Dawn LUCI CQ
parent b607bfbddf
commit 7a41bf1e2f
2 changed files with 25 additions and 22 deletions

View File

@ -81,19 +81,29 @@ struct Unshadow::State {
}); });
}; };
ctx.ReplaceAll([&](const ast::Variable* v) -> const ast::Variable* { bool made_changes = false;
if (auto* local = sem.Get<sem::LocalVariable>(v)) {
for (auto* node : ctx.src->SemNodes().Objects()) {
Switch(
node, //
[&](const sem::LocalVariable* local) {
if (local->Shadows()) { if (local->Shadows()) {
return rename(local); ctx.Replace(local->Declaration(), [&, local] { return rename(local); });
made_changes = true;
} }
} },
if (auto* param = sem.Get<sem::Parameter>(v)) { [&](const sem::Parameter* param) {
if (param->Shadows()) { if (param->Shadows()) {
return rename(param); ctx.Replace(param->Declaration(), [&, param] { return rename(param); });
made_changes = true;
} }
}
return nullptr;
}); });
}
if (!made_changes) {
return SkipTransform;
}
ctx.ReplaceAll( ctx.ReplaceAll(
[&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* { [&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* {
if (auto* sem_ident = sem.Get(ident)) { if (auto* sem_ident = sem.Get(ident)) {

View File

@ -23,11 +23,8 @@ using UnshadowTest = TransformTest;
TEST_F(UnshadowTest, EmptyModule) { TEST_F(UnshadowTest, EmptyModule) {
auto* src = ""; auto* src = "";
auto* expect = "";
auto got = Run<Unshadow>(src); EXPECT_FALSE(ShouldRun<Unshadow>(src));
EXPECT_EQ(expect, str(got));
} }
TEST_F(UnshadowTest, Noop) { TEST_F(UnshadowTest, Noop) {
@ -48,11 +45,7 @@ fn F(c : i32) {
} }
)"; )";
auto* expect = src; EXPECT_FALSE(ShouldRun<Unshadow>(src));
auto got = Run<Unshadow>(src);
EXPECT_EQ(expect, str(got));
} }
TEST_F(UnshadowTest, LocalShadowsAlias) { TEST_F(UnshadowTest, LocalShadowsAlias) {