tint/transform: Simplify Renamer transform

Take advantage of the fact that all AST nodes now use an
`ast::Identifier` instead of directly using Symbols.

This will be important for ensuring that un-keyworded identifiers will
be preserved.

Bug: tint:1810
Change-Id: If5c3e9f00b4c1c14a6f11bd617bd8b895250fb7e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/119285
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
diff --git a/src/tint/transform/renamer.cc b/src/tint/transform/renamer.cc
index 7510520..3d4f450 100644
--- a/src/tint/transform/renamer.cc
+++ b/src/tint/transform/renamer.cc
@@ -15,7 +15,6 @@
 #include "src/tint/transform/renamer.h"
 
 #include <memory>
-#include <unordered_set>
 #include <utility>
 
 #include "src/tint/program_builder.h"
@@ -24,7 +23,6 @@
 #include "src/tint/sem/type_conversion.h"
 #include "src/tint/sem/type_initializer.h"
 #include "src/tint/text/unicode.h"
-#include "src/tint/type/builtin.h"
 
 TINT_INSTANTIATE_TYPEINFO(tint::transform::Renamer);
 TINT_INSTANTIATE_TYPEINFO(tint::transform::Renamer::Data);
@@ -1258,28 +1256,21 @@
 Transform::ApplyResult Renamer::Apply(const Program* src,
                                       const DataMap& inputs,
                                       DataMap& outputs) const {
-    ProgramBuilder b;
-    CloneContext ctx{&b, src, /* auto_clone_symbols */ false};
+    utils::Hashset<Symbol, 16> global_decls;
+    for (auto* decl : src->AST().TypeDecls()) {
+        global_decls.Add(decl->name->symbol);
+    }
 
     // Identifiers that need to keep their symbols preserved.
-    utils::Hashset<const ast::Identifier*, 8> preserved_identifiers;
-
-    auto is_type_short_name = [&](const Symbol& symbol) {
-        auto name = src->Symbols().NameFor(symbol);
-        if (type::ParseBuiltin(name) != type::Builtin::kUndefined) {
-            // Identifier *looks* like a builtin type, but check that the builtin type isn't being
-            // shadowed with a user declared type.
-            for (auto* decl : src->AST().TypeDecls()) {
-                if (decl->name->symbol == symbol) {
-                    return false;
-                }
-            }
-            return true;
-        }
-        return false;
-    };
+    utils::Hashset<const ast::Identifier*, 16> preserved_identifiers;
 
     for (auto* node : src->ASTNodes().Objects()) {
+        auto preserve_if_builtin_type = [&](const ast::Identifier* ident) {
+            if (!global_decls.Contains(ident->symbol)) {
+                preserved_identifiers.Add(ident);
+            }
+        };
+
         Switch(
             node,
             [&](const ast::MemberAccessorExpression* accessor) {
@@ -1294,38 +1285,24 @@
                     }
                 }
             },
-            [&](const ast::CallExpression* call) {
-                if (auto* ident = call->target.name) {
-                    Switch(
-                        src->Sem().Get(call)->UnwrapMaterialize()->As<sem::Call>()->Target(),
-                        [&](const sem::Builtin*) { preserved_identifiers.Add(ident); },
-                        [&](const sem::TypeConversion*) {
-                            if (is_type_short_name(ident->symbol)) {
-                                preserved_identifiers.Add(ident);
-                            }
-                        },
-                        [&](const sem::TypeInitializer*) {
-                            if (is_type_short_name(ident->symbol)) {
-                                preserved_identifiers.Add(ident);
-                            }
-                        });
-                }
-            },
             [&](const ast::DiagnosticAttribute* diagnostic) {
                 preserved_identifiers.Add(diagnostic->control.rule_name);
             },
             [&](const ast::DiagnosticDirective* diagnostic) {
                 preserved_identifiers.Add(diagnostic->control.rule_name);
             },
-            [&](const ast::TypeName* type_name) {
-                if (is_type_short_name(type_name->name->symbol)) {
-                    preserved_identifiers.Add(type_name->name);
+            [&](const ast::TypeName* ty) { preserve_if_builtin_type(ty->name); },
+            [&](const ast::CallExpression* call) {
+                if (auto* ident = call->target.name) {
+                    Switch(
+                        src->Sem().Get(call)->UnwrapMaterialize()->As<sem::Call>()->Target(),
+                        [&](const sem::Builtin*) { preserved_identifiers.Add(ident); },
+                        [&](const sem::TypeConversion*) { preserve_if_builtin_type(ident); },
+                        [&](const sem::TypeInitializer*) { preserve_if_builtin_type(ident); });
                 }
             });
     }
 
-    Data::Remappings remappings;
-
     Target target = Target::kAll;
     bool preserve_unicode = false;
 
@@ -1334,62 +1311,70 @@
         preserve_unicode = cfg->preserve_unicode;
     }
 
-    ctx.ReplaceAll([&](Symbol sym_in) {
-        auto name_in = src->Symbols().NameFor(sym_in);
-        if (preserve_unicode || text::utf8::IsASCII(name_in)) {
-            switch (target) {
-                case Target::kAll:
-                    // Always rename.
-                    break;
-                case Target::kGlslKeywords:
-                    if (!std::binary_search(kReservedKeywordsGLSL,
-                                            kReservedKeywordsGLSL +
-                                                sizeof(kReservedKeywordsGLSL) / sizeof(const char*),
-                                            name_in) &&
-                        name_in.compare(0, 3, "gl_")) {
-                        // No match, just reuse the original name.
-                        return ctx.dst->Symbols().New(name_in);
-                    }
-                    break;
-                case Target::kHlslKeywords:
-                    if (!std::binary_search(kReservedKeywordsHLSL,
-                                            kReservedKeywordsHLSL +
-                                                sizeof(kReservedKeywordsHLSL) / sizeof(const char*),
-                                            name_in)) {
-                        // No match, just reuse the original name.
-                        return ctx.dst->Symbols().New(name_in);
-                    }
-                    break;
-                case Target::kMslKeywords:
-                    if (!std::binary_search(kReservedKeywordsMSL,
-                                            kReservedKeywordsMSL +
-                                                sizeof(kReservedKeywordsMSL) / sizeof(const char*),
-                                            name_in)) {
-                        // No match, just reuse the original name.
-                        return ctx.dst->Symbols().New(name_in);
-                    }
-                    break;
-            }
+    // Returns true if the symbol should be renamed based on the input configuration settings.
+    auto should_rename = [&](Symbol symbol) {
+        if (target == Target::kAll) {
+            return true;
         }
+        auto name = src->Symbols().NameFor(symbol);
+        if (!text::utf8::IsASCII(name)) {
+            // name is non-ascii. All of the backend keywords are ascii, so rename if we're not
+            // preserving unicode symbols.
+            return !preserve_unicode;
+        }
+        switch (target) {
+            case Target::kGlslKeywords:
+                return std::binary_search(kReservedKeywordsGLSL,
+                                          kReservedKeywordsGLSL +
+                                              sizeof(kReservedKeywordsGLSL) / sizeof(const char*),
+                                          name) ||
+                       name.compare(0, 3, "gl_") == 0;
+            case Target::kHlslKeywords:
+                return std::binary_search(
+                    kReservedKeywordsHLSL,
+                    kReservedKeywordsHLSL + sizeof(kReservedKeywordsHLSL) / sizeof(const char*),
+                    name);
+            case Target::kMslKeywords:
+                return std::binary_search(
+                    kReservedKeywordsMSL,
+                    kReservedKeywordsMSL + sizeof(kReservedKeywordsMSL) / sizeof(const char*),
+                    name);
+            default:
+                break;
+        }
+        return true;
+    };
 
-        auto sym_out = ctx.dst->Sym();
-        remappings.emplace(name_in, ctx.dst->Symbols().NameFor(sym_out));
-        return sym_out;
-    });
+    utils::Hashmap<Symbol, Symbol, 32> remappings;
+
+    ProgramBuilder b;
+    CloneContext ctx{&b, src, /* auto_clone_symbols */ false};
 
     ctx.ReplaceAll([&](const ast::Identifier* ident) -> const ast::Identifier* {
-        if (preserved_identifiers.Contains(ident)) {
-            auto sym_in = ident->symbol;
-            auto str = src->Symbols().NameFor(sym_in);
-            auto sym_out = b.Symbols().Register(str);
-            return ctx.dst->create<ast::Identifier>(ctx.Clone(ident->source), sym_out);
+        const auto symbol = ident->symbol;
+        if (preserved_identifiers.Contains(ident) || !should_rename(symbol)) {
+            return nullptr;  // Preserve symbol
         }
-        return nullptr;  // Clone ident. Uses the symbol remapping above.
+
+        // Create a replacement for this symbol, if we haven't already.
+        auto replacement = remappings.GetOrCreate(symbol, [&] { return b.Symbols().New(); });
+
+        // Reconstruct the identifier
+        if (auto* tmpl_ident = ident->As<ast::TemplatedIdentifier>()) {
+            auto args = ctx.Clone(tmpl_ident->arguments);
+            return ctx.dst->create<ast::TemplatedIdentifier>(ctx.Clone(ident->source), replacement,
+                                                             std::move(args));
+        }
+        return ctx.dst->create<ast::Identifier>(ctx.Clone(ident->source), replacement);
     });
 
-    ctx.Clone();  // Must come before the std::move()
+    ctx.Clone();
 
-    outputs.Add<Data>(std::move(remappings));
+    Data::Remappings out;
+    for (auto it : remappings) {
+        out[ctx.src->Symbols().NameFor(it.key)] = ctx.dst->Symbols().NameFor(it.value);
+    }
+    outputs.Add<Data>(std::move(out));
 
     return Program(std::move(b));
 }
diff --git a/src/tint/transform/renamer_test.cc b/src/tint/transform/renamer_test.cc
index f76a7aa..982a9a2 100644
--- a/src/tint/transform/renamer_test.cc
+++ b/src/tint/transform/renamer_test.cc
@@ -15,10 +15,13 @@
 #include "src/tint/transform/renamer.h"
 
 #include <memory>
+#include <unordered_set>
+#include <vector>
 
 #include "gmock/gmock.h"
 #include "src/tint/transform/test_helper.h"
 #include "src/tint/type/builtin.h"
+#include "src/tint/type/texel_format.h"
 
 namespace tint::transform {
 namespace {
@@ -250,6 +253,30 @@
     EXPECT_EQ(expect, str(got));
 }
 
+TEST_F(RenamerTest, PreserveUnicodeRenameAll) {
+    auto src = R"(
+@fragment
+fn frag_main() {
+  var )" + std::string(kUnicodeIdentifier) +
+               R"( : i32;
+}
+)";
+
+    auto expect = R"(
+@fragment
+fn tint_symbol() {
+  var tint_symbol_1 : i32;
+}
+)";
+
+    DataMap inputs;
+    inputs.Add<Renamer::Config>(Renamer::Target::kAll,
+                                /* preserve_unicode */ true);
+    auto got = Run<Renamer>(src, inputs);
+
+    EXPECT_EQ(expect, str(got));
+}
+
 TEST_F(RenamerTest, AttemptSymbolCollision) {
     auto* src = R"(
 @vertex
@@ -287,6 +314,49 @@
     EXPECT_THAT(data->remappings, ContainerEq(expected_remappings));
 }
 
+TEST_F(RenamerTest, PreserveTexelFormatAndAccess) {
+    auto src = R"(
+@group(0) @binding(0) var texture : texture_storage_2d<rgba8unorm, write>;
+
+fn f() {
+  var dims = textureDimensions(texture);
+}
+)";
+
+    auto expect = R"(
+@group(0) @binding(0) var tint_symbol : texture_storage_2d<rgba8unorm, write>;
+
+fn tint_symbol_1() {
+  var tint_symbol_2 = textureDimensions(tint_symbol);
+}
+)";
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+TEST_F(RenamerTest, PreserveAddressSpace) {
+    auto src = R"(
+var<private> p : i32;
+
+fn f() {
+  var v = p;
+}
+)";
+
+    auto expect = R"(
+var<private> tint_symbol : i32;
+
+fn tint_symbol_1() {
+  var tint_symbol_2 = tint_symbol;
+}
+)";
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
 using RenamerTestGlsl = TransformTestWithParam<std::string>;
 using RenamerTestHlsl = TransformTestWithParam<std::string>;
 using RenamerTestMsl = TransformTestWithParam<std::string>;
@@ -1500,7 +1570,7 @@
         // "while"  // WGSL reserved keyword
         kUnicodeIdentifier));
 
-const char* ExpandBuiltinType(std::string_view name) {
+std::string ExpandBuiltinType(std::string_view name) {
     if (name == "mat2x2f") {
         return "mat2x2<f32>";
     }
@@ -1591,13 +1661,47 @@
     if (name == "vec4u") {
         return "vec4<u32>";
     }
-    ADD_FAILURE() << "unhandled type: " << name;
-    return "<invalid>";
+    return std::string(name);
 }
 
-using RenamerTypeBuiltinTypesTest = TransformTestWithParam<const char*>;
+/// @return all the identifiers parsed as keywords
+std::unordered_set<std::string> Keywords() {
+    return {
+        "bool",
+        "f16",
+        "f32",
+        "i32",
+        "sampler_comparison",
+        "sampler",
+        "texture_depth_2d_array",
+        "texture_depth_2d",
+        "texture_depth_cube_array",
+        "texture_depth_cube",
+        "texture_depth_multisampled_2d",
+        "texture_external",
+        "texture_storage_1d",
+        "texture_storage_2d",
+        "texture_storage_2d_array",
+        "texture_storage_3d",
+        "u32",
+    };
+}
 
-TEST_P(RenamerTypeBuiltinTypesTest, PreserveTypeUsage) {
+/// @return WGSL builtin types that aren't keywords
+std::vector<const char*> NonKeywordBuiltinTypes() {
+    auto keywords = Keywords();
+    std::vector<const char*> out;
+    for (auto* ident : type::kBuiltinStrings) {
+        if (!keywords.count(ident)) {
+            out.push_back(ident);
+        }
+    }
+    return out;
+}
+
+using RenamerBuiltinTypeTest = TransformTestWithParam<const char*>;
+
+TEST_P(RenamerBuiltinTypeTest, PreserveTypeUsage) {
     auto expand = [&](const char* source) {
         auto out = utils::ReplaceAll(source, "$name", GetParam());
         out = utils::ReplaceAll(out, "$type", ExpandBuiltinType(GetParam()));
@@ -1638,7 +1742,7 @@
 
     EXPECT_EQ(expect, str(got));
 }
-TEST_P(RenamerTypeBuiltinTypesTest, PreserveTypeInitializer) {
+TEST_P(RenamerBuiltinTypeTest, PreserveTypeInitializer) {
     auto expand = [&](const char* source) {
         auto out = utils::ReplaceAll(source, "$name", GetParam());
         out = utils::ReplaceAll(out, "$type", ExpandBuiltinType(GetParam()));
@@ -1668,7 +1772,7 @@
     EXPECT_EQ(expect, str(got));
 }
 
-TEST_P(RenamerTypeBuiltinTypesTest, PreserveTypeConversion) {
+TEST_P(RenamerBuiltinTypeTest, PreserveTypeConversion) {
     auto expand = [&](const char* source) {
         auto out = utils::ReplaceAll(source, "$name", GetParam());
         out = utils::ReplaceAll(out, "$type", ExpandBuiltinType(GetParam()));
@@ -1698,7 +1802,7 @@
     EXPECT_EQ(expect, str(got));
 }
 
-TEST_P(RenamerTypeBuiltinTypesTest, RenameShadowedByAlias) {
+TEST_P(RenamerBuiltinTypeTest, RenameShadowedByAlias) {
     auto expand = [&](const char* source) {
         auto out = utils::ReplaceAll(source, "$name", GetParam());
         out = utils::ReplaceAll(out, "$type", ExpandBuiltinType(GetParam()));
@@ -1728,7 +1832,7 @@
     EXPECT_EQ(expect, str(got));
 }
 
-TEST_P(RenamerTypeBuiltinTypesTest, RenameShadowedByStruct) {
+TEST_P(RenamerBuiltinTypeTest, RenameShadowedByStruct) {
     auto expand = [&](const char* source) {
         auto out = utils::ReplaceAll(source, "$name", GetParam());
         out = utils::ReplaceAll(out, "$type", ExpandBuiltinType(GetParam()));
@@ -1764,9 +1868,148 @@
     EXPECT_EQ(expect, str(got));
 }
 
-INSTANTIATE_TEST_SUITE_P(RenamerTypeBuiltinTypesTest,
-                         RenamerTypeBuiltinTypesTest,
-                         testing::ValuesIn(type::kBuiltinStrings));
+INSTANTIATE_TEST_SUITE_P(RenamerBuiltinTypeTest,
+                         RenamerBuiltinTypeTest,
+                         testing::ValuesIn(NonKeywordBuiltinTypes()));
+
+/// @return WGSL builtin identifiers keywords
+std::vector<const char*> NonKeywordIdentifiers() {
+    auto keywords = Keywords();
+    std::vector<const char*> out;
+    for (auto* ident : type::kBuiltinStrings) {
+        if (!keywords.count(ident)) {
+            out.push_back(ident);
+        }
+    }
+    for (auto* ident : type::kAddressSpaceStrings) {
+        if (!keywords.count(ident)) {
+            out.push_back(ident);
+        }
+    }
+    for (auto* ident : type::kTexelFormatStrings) {
+        if (!keywords.count(ident)) {
+            out.push_back(ident);
+        }
+    }
+    for (auto* ident : type::kAccessStrings) {
+        if (!keywords.count(ident)) {
+            out.push_back(ident);
+        }
+    }
+    return out;
+}
+
+using RenamerBuiltinIdentifierTest = TransformTestWithParam<const char*>;
+
+TEST_P(RenamerBuiltinIdentifierTest, GlobalVarName) {
+    auto expand = [&](const char* source) {
+        return utils::ReplaceAll(source, "$name", GetParam());
+    };
+
+    auto src = expand(R"(
+var<private> $name = 42;
+
+fn f() {
+  var v = $name;
+}
+)");
+
+    auto expect = expand(R"(
+var<private> tint_symbol = 42;
+
+fn tint_symbol_1() {
+  var tint_symbol_2 = tint_symbol;
+}
+)");
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+TEST_P(RenamerBuiltinIdentifierTest, LocalVarName) {
+    auto expand = [&](const char* source) {
+        return utils::ReplaceAll(source, "$name", GetParam());
+    };
+
+    auto src = expand(R"(
+fn f() {
+  var $name = 42;
+}
+)");
+
+    auto expect = expand(R"(
+fn tint_symbol() {
+  var tint_symbol_1 = 42;
+}
+)");
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+TEST_P(RenamerBuiltinIdentifierTest, FunctionName) {
+    auto expand = [&](const char* source) {
+        return utils::ReplaceAll(source, "$name", GetParam());
+    };
+
+    auto src = expand(R"(
+fn $name() {
+}
+
+fn f() {
+  $name();
+}
+)");
+
+    auto expect = expand(R"(
+fn tint_symbol() {
+}
+
+fn tint_symbol_1() {
+  tint_symbol();
+}
+)");
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+TEST_P(RenamerBuiltinIdentifierTest, StructName) {
+    auto expand = [&](const char* source) {
+        return utils::ReplaceAll(source, "$name", GetParam());
+    };
+
+    auto src = expand(R"(
+struct $name {
+  i : i32,
+}
+
+fn f() {
+  var x = $name();
+}
+)");
+
+    auto expect = expand(R"(
+struct tint_symbol {
+  tint_symbol_1 : i32,
+}
+
+fn tint_symbol_2() {
+  var tint_symbol_3 = tint_symbol();
+}
+)");
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+}
+
+INSTANTIATE_TEST_SUITE_P(RenamerBuiltinIdentifierTest,
+                         RenamerBuiltinIdentifierTest,
+                         testing::ValuesIn(NonKeywordIdentifiers()));
 
 }  // namespace
 }  // namespace tint::transform
diff --git a/test/tint/bug/chromium/1273230.wgsl.expected.dxc.hlsl b/test/tint/bug/chromium/1273230.wgsl.expected.dxc.hlsl
index 8b61a3e..c29e7d6 100644
--- a/test/tint/bug/chromium/1273230.wgsl.expected.dxc.hlsl
+++ b/test/tint/bug/chromium/1273230.wgsl.expected.dxc.hlsl
@@ -36,10 +36,10 @@
 }
 
 uint3 toIndex4D(uint gridSize, uint index) {
-  uint z_1 = tint_div(gridSize, (index * index));
-  uint y_1 = tint_div((gridSize - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(z_1, y_1, y_1);
+  uint z = tint_div(gridSize, (index * index));
+  uint y = tint_div((gridSize - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(z, y, y);
 }
 
 float3 loadPosition(uint vertexIndex) {
diff --git a/test/tint/bug/chromium/1273230.wgsl.expected.fxc.hlsl b/test/tint/bug/chromium/1273230.wgsl.expected.fxc.hlsl
index 8b61a3e..c29e7d6 100644
--- a/test/tint/bug/chromium/1273230.wgsl.expected.fxc.hlsl
+++ b/test/tint/bug/chromium/1273230.wgsl.expected.fxc.hlsl
@@ -36,10 +36,10 @@
 }
 
 uint3 toIndex4D(uint gridSize, uint index) {
-  uint z_1 = tint_div(gridSize, (index * index));
-  uint y_1 = tint_div((gridSize - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(z_1, y_1, y_1);
+  uint z = tint_div(gridSize, (index * index));
+  uint y = tint_div((gridSize - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(z, y, y);
 }
 
 float3 loadPosition(uint vertexIndex) {
diff --git a/test/tint/bug/chromium/1273230.wgsl.expected.msl b/test/tint/bug/chromium/1273230.wgsl.expected.msl
index aa34606..2312e14 100644
--- a/test/tint/bug/chromium/1273230.wgsl.expected.msl
+++ b/test/tint/bug/chromium/1273230.wgsl.expected.msl
@@ -89,10 +89,10 @@
 }
 
 uint3 toIndex4D(uint gridSize, uint index) {
-  uint z_1 = tint_div(gridSize, (index * index));
-  uint y_1 = tint_div((gridSize - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(z_1, y_1, y_1);
+  uint z = tint_div(gridSize, (index * index));
+  uint y = tint_div((gridSize - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(z, y, y);
 }
 
 float3 loadPosition(uint vertexIndex, device F32s* const tint_symbol_1) {
diff --git a/test/tint/bug/tint/1113.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/1113.wgsl.expected.dxc.hlsl
index 68248ca..25be8e4 100644
--- a/test/tint/bug/tint/1113.wgsl.expected.dxc.hlsl
+++ b/test/tint/bug/tint/1113.wgsl.expected.dxc.hlsl
@@ -33,10 +33,10 @@
 }
 
 uint3 toIndex3D(uint gridSize, uint index) {
-  uint z_1 = tint_div(index, (gridSize * gridSize));
-  uint y_1 = tint_div((index - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(x_1, y_1, z_1);
+  uint z = tint_div(index, (gridSize * gridSize));
+  uint y = tint_div((index - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(x, y, z);
 }
 
 float3 loadPosition(uint vertexIndex) {
diff --git a/test/tint/bug/tint/1113.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/1113.wgsl.expected.fxc.hlsl
index 68248ca..25be8e4 100644
--- a/test/tint/bug/tint/1113.wgsl.expected.fxc.hlsl
+++ b/test/tint/bug/tint/1113.wgsl.expected.fxc.hlsl
@@ -33,10 +33,10 @@
 }
 
 uint3 toIndex3D(uint gridSize, uint index) {
-  uint z_1 = tint_div(index, (gridSize * gridSize));
-  uint y_1 = tint_div((index - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(x_1, y_1, z_1);
+  uint z = tint_div(index, (gridSize * gridSize));
+  uint y = tint_div((index - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(x, y, z);
 }
 
 float3 loadPosition(uint vertexIndex) {
diff --git a/test/tint/bug/tint/1113.wgsl.expected.msl b/test/tint/bug/tint/1113.wgsl.expected.msl
index 2660500..cd110c1 100644
--- a/test/tint/bug/tint/1113.wgsl.expected.msl
+++ b/test/tint/bug/tint/1113.wgsl.expected.msl
@@ -86,10 +86,10 @@
 }
 
 uint3 toIndex3D(uint gridSize, uint index) {
-  uint z_1 = tint_div(index, (gridSize * gridSize));
-  uint y_1 = tint_div((index - ((gridSize * gridSize) * z_1)), gridSize);
-  uint x_1 = tint_mod(index, gridSize);
-  return uint3(x_1, y_1, z_1);
+  uint z = tint_div(index, (gridSize * gridSize));
+  uint y = tint_div((index - ((gridSize * gridSize) * z)), gridSize);
+  uint x = tint_mod(index, gridSize);
+  return uint3(x, y, z);
 }
 
 float3 loadPosition(uint vertexIndex, device F32s* const tint_symbol_2) {
diff --git a/test/tint/bug/tint/1121.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/1121.wgsl.expected.dxc.hlsl
index f384631..5dee593 100644
--- a/test/tint/bug/tint/1121.wgsl.expected.dxc.hlsl
+++ b/test/tint/bug/tint/1121.wgsl.expected.dxc.hlsl
@@ -54,10 +54,10 @@
   const int TILE_COUNT_X = 2;
   const int TILE_COUNT_Y = 2;
   {
-    for(int y_1 = 0; (y_1 < TILE_COUNT_Y); y_1 = (y_1 + 1)) {
+    for(int y = 0; (y < TILE_COUNT_Y); y = (y + 1)) {
       {
-        for(int x_1 = 0; (x_1 < TILE_COUNT_X); x_1 = (x_1 + 1)) {
-          int2 tilePixel0Idx = int2((x_1 * TILE_SIZE), (y_1 * TILE_SIZE));
+        for(int x = 0; (x < TILE_COUNT_X); x = (x + 1)) {
+          int2 tilePixel0Idx = int2((x * TILE_SIZE), (y * TILE_SIZE));
           float2 floorCoord = (((2.0f * float2(tilePixel0Idx)) / asfloat(uniforms[10]).xy) - (1.0f).xx);
           float2 ceilCoord = (((2.0f * float2((tilePixel0Idx + int2((TILE_SIZE).xx)))) / asfloat(uniforms[10]).xy) - (1.0f).xx);
           float2 viewFloorCoord = float2((((-(viewNear) * floorCoord.x) - (M[2][0] * viewNear)) / M[0][0]), (((-(viewNear) * floorCoord.y) - (M[2][1] * viewNear)) / M[1][1]));
@@ -90,7 +90,7 @@
             }
           }
           if ((dp >= 0.0f)) {
-            uint tileId = uint((x_1 + (y_1 * TILE_COUNT_X)));
+            uint tileId = uint((x + (y * TILE_COUNT_X)));
             bool tint_tmp = (tileId < 0u);
             if (!tint_tmp) {
               tint_tmp = (tileId >= config[0].y);
diff --git a/test/tint/bug/tint/1121.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/1121.wgsl.expected.fxc.hlsl
index f384631..5dee593 100644
--- a/test/tint/bug/tint/1121.wgsl.expected.fxc.hlsl
+++ b/test/tint/bug/tint/1121.wgsl.expected.fxc.hlsl
@@ -54,10 +54,10 @@
   const int TILE_COUNT_X = 2;
   const int TILE_COUNT_Y = 2;
   {
-    for(int y_1 = 0; (y_1 < TILE_COUNT_Y); y_1 = (y_1 + 1)) {
+    for(int y = 0; (y < TILE_COUNT_Y); y = (y + 1)) {
       {
-        for(int x_1 = 0; (x_1 < TILE_COUNT_X); x_1 = (x_1 + 1)) {
-          int2 tilePixel0Idx = int2((x_1 * TILE_SIZE), (y_1 * TILE_SIZE));
+        for(int x = 0; (x < TILE_COUNT_X); x = (x + 1)) {
+          int2 tilePixel0Idx = int2((x * TILE_SIZE), (y * TILE_SIZE));
           float2 floorCoord = (((2.0f * float2(tilePixel0Idx)) / asfloat(uniforms[10]).xy) - (1.0f).xx);
           float2 ceilCoord = (((2.0f * float2((tilePixel0Idx + int2((TILE_SIZE).xx)))) / asfloat(uniforms[10]).xy) - (1.0f).xx);
           float2 viewFloorCoord = float2((((-(viewNear) * floorCoord.x) - (M[2][0] * viewNear)) / M[0][0]), (((-(viewNear) * floorCoord.y) - (M[2][1] * viewNear)) / M[1][1]));
@@ -90,7 +90,7 @@
             }
           }
           if ((dp >= 0.0f)) {
-            uint tileId = uint((x_1 + (y_1 * TILE_COUNT_X)));
+            uint tileId = uint((x + (y * TILE_COUNT_X)));
             bool tint_tmp = (tileId < 0u);
             if (!tint_tmp) {
               tint_tmp = (tileId >= config[0].y);
diff --git a/test/tint/bug/tint/1121.wgsl.expected.glsl b/test/tint/bug/tint/1121.wgsl.expected.glsl
index 864fe54..5e6dfaf 100644
--- a/test/tint/bug/tint/1121.wgsl.expected.glsl
+++ b/test/tint/bug/tint/1121.wgsl.expected.glsl
@@ -75,10 +75,10 @@
   int TILE_COUNT_X = 2;
   int TILE_COUNT_Y = 2;
   {
-    for(int y_1 = 0; (y_1 < TILE_COUNT_Y); y_1 = (y_1 + 1)) {
+    for(int y = 0; (y < TILE_COUNT_Y); y = (y + 1)) {
       {
-        for(int x_1 = 0; (x_1 < TILE_COUNT_X); x_1 = (x_1 + 1)) {
-          ivec2 tilePixel0Idx = ivec2((x_1 * TILE_SIZE), (y_1 * TILE_SIZE));
+        for(int x = 0; (x < TILE_COUNT_X); x = (x + 1)) {
+          ivec2 tilePixel0Idx = ivec2((x * TILE_SIZE), (y * TILE_SIZE));
           vec2 floorCoord = (((2.0f * vec2(tilePixel0Idx)) / uniforms.inner.fullScreenSize.xy) - vec2(1.0f));
           vec2 ceilCoord = (((2.0f * vec2((tilePixel0Idx + ivec2(TILE_SIZE)))) / uniforms.inner.fullScreenSize.xy) - vec2(1.0f));
           vec2 viewFloorCoord = vec2((((-(viewNear) * floorCoord.x) - (M[2][0] * viewNear)) / M[0][0]), (((-(viewNear) * floorCoord.y) - (M[2][1] * viewNear)) / M[1][1]));
@@ -111,7 +111,7 @@
             }
           }
           if ((dp >= 0.0f)) {
-            uint tileId = uint((x_1 + (y_1 * TILE_COUNT_X)));
+            uint tileId = uint((x + (y * TILE_COUNT_X)));
             bool tint_tmp = (tileId < 0u);
             if (!tint_tmp) {
               tint_tmp = (tileId >= config.inner.numTiles);
diff --git a/test/tint/bug/tint/1121.wgsl.expected.msl b/test/tint/bug/tint/1121.wgsl.expected.msl
index 22cc6dd..756cc43 100644
--- a/test/tint/bug/tint/1121.wgsl.expected.msl
+++ b/test/tint/bug/tint/1121.wgsl.expected.msl
@@ -74,9 +74,9 @@
   int const TILE_SIZE = 16;
   int const TILE_COUNT_X = 2;
   int const TILE_COUNT_Y = 2;
-  for(int y_1 = 0; (y_1 < TILE_COUNT_Y); y_1 = as_type<int>((as_type<uint>(y_1) + as_type<uint>(1)))) {
-    for(int x_1 = 0; (x_1 < TILE_COUNT_X); x_1 = as_type<int>((as_type<uint>(x_1) + as_type<uint>(1)))) {
-      int2 tilePixel0Idx = int2(as_type<int>((as_type<uint>(x_1) * as_type<uint>(TILE_SIZE))), as_type<int>((as_type<uint>(y_1) * as_type<uint>(TILE_SIZE))));
+  for(int y = 0; (y < TILE_COUNT_Y); y = as_type<int>((as_type<uint>(y) + as_type<uint>(1)))) {
+    for(int x = 0; (x < TILE_COUNT_X); x = as_type<int>((as_type<uint>(x) + as_type<uint>(1)))) {
+      int2 tilePixel0Idx = int2(as_type<int>((as_type<uint>(x) * as_type<uint>(TILE_SIZE))), as_type<int>((as_type<uint>(y) * as_type<uint>(TILE_SIZE))));
       float2 floorCoord = (((2.0f * float2(tilePixel0Idx)) / float4((*(tint_symbol_3)).fullScreenSize).xy) - float2(1.0f));
       float2 ceilCoord = (((2.0f * float2(as_type<int2>((as_type<uint2>(tilePixel0Idx) + as_type<uint2>(int2(TILE_SIZE)))))) / float4((*(tint_symbol_3)).fullScreenSize).xy) - float2(1.0f));
       float2 viewFloorCoord = float2((((-(viewNear) * floorCoord[0]) - (M[2][0] * viewNear)) / M[0][0]), (((-(viewNear) * floorCoord[1]) - (M[2][1] * viewNear)) / M[1][1]));
@@ -107,7 +107,7 @@
         dp = (dp + fmin(0.0f, dot(p, frustumPlanes[i])));
       }
       if ((dp >= 0.0f)) {
-        uint tileId = uint(as_type<int>((as_type<uint>(x_1) + as_type<uint>(as_type<int>((as_type<uint>(y_1) * as_type<uint>(TILE_COUNT_X)))))));
+        uint tileId = uint(as_type<int>((as_type<uint>(x) + as_type<uint>(as_type<int>((as_type<uint>(y) * as_type<uint>(TILE_COUNT_X)))))));
         if (((tileId < 0u) || (tileId >= (*(tint_symbol_1)).numTiles))) {
           continue;
         }
diff --git a/test/tint/shadowing/short_names/let.wgsl.expected.dxc.hlsl b/test/tint/shadowing/short_names/let.wgsl.expected.dxc.hlsl
index e4d88a3..0e6094b 100644
--- a/test/tint/shadowing/short_names/let.wgsl.expected.dxc.hlsl
+++ b/test/tint/shadowing/short_names/let.wgsl.expected.dxc.hlsl
@@ -5,8 +5,8 @@
 
 void f() {
   {
-    const int vec3f_1 = 1;
-    const int b = vec3f_1;
+    const int vec3f = 1;
+    const int b = vec3f;
   }
   const float3 c = (0.0f).xxx;
   const float3 d = (0.0f).xxx;
diff --git a/test/tint/shadowing/short_names/let.wgsl.expected.fxc.hlsl b/test/tint/shadowing/short_names/let.wgsl.expected.fxc.hlsl
index e4d88a3..0e6094b 100644
--- a/test/tint/shadowing/short_names/let.wgsl.expected.fxc.hlsl
+++ b/test/tint/shadowing/short_names/let.wgsl.expected.fxc.hlsl
@@ -5,8 +5,8 @@
 
 void f() {
   {
-    const int vec3f_1 = 1;
-    const int b = vec3f_1;
+    const int vec3f = 1;
+    const int b = vec3f;
   }
   const float3 c = (0.0f).xxx;
   const float3 d = (0.0f).xxx;
diff --git a/test/tint/shadowing/short_names/let.wgsl.expected.glsl b/test/tint/shadowing/short_names/let.wgsl.expected.glsl
index 36e810e..fc24838 100644
--- a/test/tint/shadowing/short_names/let.wgsl.expected.glsl
+++ b/test/tint/shadowing/short_names/let.wgsl.expected.glsl
@@ -6,8 +6,8 @@
 }
 void f() {
   {
-    int vec3f_1 = 1;
-    int b = vec3f_1;
+    int vec3f = 1;
+    int b = vec3f;
   }
   vec3 c = vec3(0.0f);
   vec3 d = vec3(0.0f);
diff --git a/test/tint/shadowing/short_names/let.wgsl.expected.msl b/test/tint/shadowing/short_names/let.wgsl.expected.msl
index cf3c9ce..34d9028 100644
--- a/test/tint/shadowing/short_names/let.wgsl.expected.msl
+++ b/test/tint/shadowing/short_names/let.wgsl.expected.msl
@@ -3,8 +3,8 @@
 using namespace metal;
 void f() {
   {
-    int const vec3f_1 = 1;
-    int const b = vec3f_1;
+    int const vec3f = 1;
+    int const b = vec3f;
   }
   float3 const c = float3(0.0f);
   float3 const d = float3(0.0f);
diff --git a/test/tint/shadowing/short_names/var.wgsl.expected.dxc.hlsl b/test/tint/shadowing/short_names/var.wgsl.expected.dxc.hlsl
index c7f6db2..8a019ba 100644
--- a/test/tint/shadowing/short_names/var.wgsl.expected.dxc.hlsl
+++ b/test/tint/shadowing/short_names/var.wgsl.expected.dxc.hlsl
@@ -5,8 +5,8 @@
 
 void f() {
   {
-    int vec3f_1 = 1;
-    int b = vec3f_1;
+    int vec3f = 1;
+    int b = vec3f;
   }
   float3 c = (0.0f).xxx;
   float3 d = (0.0f).xxx;
diff --git a/test/tint/shadowing/short_names/var.wgsl.expected.fxc.hlsl b/test/tint/shadowing/short_names/var.wgsl.expected.fxc.hlsl
index c7f6db2..8a019ba 100644
--- a/test/tint/shadowing/short_names/var.wgsl.expected.fxc.hlsl
+++ b/test/tint/shadowing/short_names/var.wgsl.expected.fxc.hlsl
@@ -5,8 +5,8 @@
 
 void f() {
   {
-    int vec3f_1 = 1;
-    int b = vec3f_1;
+    int vec3f = 1;
+    int b = vec3f;
   }
   float3 c = (0.0f).xxx;
   float3 d = (0.0f).xxx;
diff --git a/test/tint/shadowing/short_names/var.wgsl.expected.glsl b/test/tint/shadowing/short_names/var.wgsl.expected.glsl
index 36e810e..fc24838 100644
--- a/test/tint/shadowing/short_names/var.wgsl.expected.glsl
+++ b/test/tint/shadowing/short_names/var.wgsl.expected.glsl
@@ -6,8 +6,8 @@
 }
 void f() {
   {
-    int vec3f_1 = 1;
-    int b = vec3f_1;
+    int vec3f = 1;
+    int b = vec3f;
   }
   vec3 c = vec3(0.0f);
   vec3 d = vec3(0.0f);
diff --git a/test/tint/shadowing/short_names/var.wgsl.expected.msl b/test/tint/shadowing/short_names/var.wgsl.expected.msl
index 16f68a8..1446f18 100644
--- a/test/tint/shadowing/short_names/var.wgsl.expected.msl
+++ b/test/tint/shadowing/short_names/var.wgsl.expected.msl
@@ -3,8 +3,8 @@
 using namespace metal;
 void f() {
   {
-    int vec3f_1 = 1;
-    int b = vec3f_1;
+    int vec3f = 1;
+    int b = vec3f;
   }
   float3 c = float3(0.0f);
   float3 d = float3(0.0f);