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* {
if (auto* local = sem.Get<sem::LocalVariable>(v)) {
if (local->Shadows()) {
return rename(local);
}
}
if (auto* param = sem.Get<sem::Parameter>(v)) {
if (param->Shadows()) {
return rename(param);
}
}
return nullptr;
});
bool made_changes = false;
for (auto* node : ctx.src->SemNodes().Objects()) {
Switch(
node, //
[&](const sem::LocalVariable* local) {
if (local->Shadows()) {
ctx.Replace(local->Declaration(), [&, local] { return rename(local); });
made_changes = true;
}
},
[&](const sem::Parameter* param) {
if (param->Shadows()) {
ctx.Replace(param->Declaration(), [&, param] { return rename(param); });
made_changes = true;
}
});
}
if (!made_changes) {
return SkipTransform;
}
ctx.ReplaceAll(
[&](const ast::IdentifierExpression* ident) -> const tint::ast::IdentifierExpression* {
if (auto* sem_ident = sem.Get(ident)) {

View File

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