From 7a41bf1e2f9b24325430d924f2e8c539c1987f79 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 12 Jan 2023 10:36:27 +0000 Subject: [PATCH] tint/transform: Skip Unshadow if possible Change-Id: I73b7213aafe3ff25a5f6526e7002d2ec3dda57c3 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/116871 Kokoro: Ben Clayton Reviewed-by: Dan Sinclair Commit-Queue: Ben Clayton --- src/tint/transform/unshadow.cc | 36 ++++++++++++++++++----------- src/tint/transform/unshadow_test.cc | 11 ++------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/tint/transform/unshadow.cc b/src/tint/transform/unshadow.cc index 3ec5f3053e..7c9f75cced 100644 --- a/src/tint/transform/unshadow.cc +++ b/src/tint/transform/unshadow.cc @@ -81,19 +81,29 @@ struct Unshadow::State { }); }; - ctx.ReplaceAll([&](const ast::Variable* v) -> const ast::Variable* { - if (auto* local = sem.Get(v)) { - if (local->Shadows()) { - return rename(local); - } - } - if (auto* param = sem.Get(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)) { diff --git a/src/tint/transform/unshadow_test.cc b/src/tint/transform/unshadow_test.cc index f9e976b361..d2ee8a5c4e 100644 --- a/src/tint/transform/unshadow_test.cc +++ b/src/tint/transform/unshadow_test.cc @@ -23,11 +23,8 @@ using UnshadowTest = TransformTest; TEST_F(UnshadowTest, EmptyModule) { auto* src = ""; - auto* expect = ""; - auto got = Run(src); - - EXPECT_EQ(expect, str(got)); + EXPECT_FALSE(ShouldRun(src)); } TEST_F(UnshadowTest, Noop) { @@ -48,11 +45,7 @@ fn F(c : i32) { } )"; - auto* expect = src; - - auto got = Run(src); - - EXPECT_EQ(expect, str(got)); + EXPECT_FALSE(ShouldRun(src)); } TEST_F(UnshadowTest, LocalShadowsAlias) {