[renamer] Preserve swizzles through materialize

Fixed: tint:2175
Change-Id: Ie93250ecfb9ff91ea49b7c4d6393d442b7afdef8
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/176080
Commit-Queue: James Price <jrprice@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/lang/wgsl/ast/transform/renamer.cc b/src/tint/lang/wgsl/ast/transform/renamer.cc
index 487697d..423df11 100644
--- a/src/tint/lang/wgsl/ast/transform/renamer.cc
+++ b/src/tint/lang/wgsl/ast/transform/renamer.cc
@@ -1294,7 +1294,7 @@
         Switch(
             node,
             [&](const MemberAccessorExpression* accessor) {
-                auto* sem = src.Sem().Get(accessor)->UnwrapLoad();
+                auto* sem = src.Sem().Get(accessor)->Unwrap();
                 if (sem->Is<sem::Swizzle>()) {
                     preserved_identifiers.Add(accessor->member);
                 } else if (auto* str_expr = src.Sem().GetVal(accessor->object)) {
diff --git a/src/tint/lang/wgsl/ast/transform/renamer_test.cc b/src/tint/lang/wgsl/ast/transform/renamer_test.cc
index b24a440..b3aa2ac 100644
--- a/src/tint/lang/wgsl/ast/transform/renamer_test.cc
+++ b/src/tint/lang/wgsl/ast/transform/renamer_test.cc
@@ -208,6 +208,32 @@
     EXPECT_THAT(data->remappings, ContainerEq(expected_remappings));
 }
 
+TEST_F(RenamerTest, PreserveSwizzles_ThroughMaterialize) {
+    auto* src = R"(
+const v = vec4();
+const x = v.x * 2u;
+)";
+
+    auto* expect = R"(
+const tint_symbol = vec4();
+
+const tint_symbol_1 = (tint_symbol.x * 2u);
+)";
+
+    auto got = Run<Renamer>(src);
+
+    EXPECT_EQ(expect, str(got));
+
+    auto* data = got.data.Get<Renamer::Data>();
+
+    ASSERT_NE(data, nullptr);
+    Renamer::Remappings expected_remappings = {
+        {"v", "tint_symbol"},
+        {"x", "tint_symbol_1"},
+    };
+    EXPECT_THAT(data->remappings, ContainerEq(expected_remappings));
+}
+
 TEST_F(RenamerTest, PreserveBuiltins) {
     auto* src = R"(
 @vertex
diff --git a/test/tint/bug/tint/2175.wgsl b/test/tint/bug/tint/2175.wgsl
new file mode 100644
index 0000000..0cc5e85
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl
@@ -0,0 +1,12 @@
+// flags: --rename-all
+
+const v = vec4();
+const x = v.x * 2u;
+
+@group(0) @binding(0)
+var<storage, read_write> output : u32;
+
+@compute @workgroup_size(1)
+fn main() {
+  output = x;
+}
diff --git a/test/tint/bug/tint/2175.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/2175.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..e33c7e0
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.dxc.hlsl
@@ -0,0 +1,7 @@
+RWByteAddressBuffer tint_symbol_2 : register(u0);
+
+[numthreads(1, 1, 1)]
+void tint_symbol_3() {
+  tint_symbol_2.Store(0u, asuint(0u));
+  return;
+}
diff --git a/test/tint/bug/tint/2175.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/2175.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..e33c7e0
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.fxc.hlsl
@@ -0,0 +1,7 @@
+RWByteAddressBuffer tint_symbol_2 : register(u0);
+
+[numthreads(1, 1, 1)]
+void tint_symbol_3() {
+  tint_symbol_2.Store(0u, asuint(0u));
+  return;
+}
diff --git a/test/tint/bug/tint/2175.wgsl.expected.glsl b/test/tint/bug/tint/2175.wgsl.expected.glsl
new file mode 100644
index 0000000..b68746a
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.glsl
@@ -0,0 +1,15 @@
+#version 310 es
+
+layout(binding = 0, std430) buffer tint_symbol_2_block_ssbo {
+  uint inner;
+} tint_symbol_2;
+
+void tint_symbol_3() {
+  tint_symbol_2.inner = 0u;
+}
+
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void main() {
+  tint_symbol_3();
+  return;
+}
diff --git a/test/tint/bug/tint/2175.wgsl.expected.msl b/test/tint/bug/tint/2175.wgsl.expected.msl
new file mode 100644
index 0000000..d34d506
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.msl
@@ -0,0 +1,8 @@
+#include <metal_stdlib>
+
+using namespace metal;
+kernel void tint_symbol_3(device uint* tint_symbol_4 [[buffer(0)]]) {
+  *(tint_symbol_4) = 0u;
+  return;
+}
+
diff --git a/test/tint/bug/tint/2175.wgsl.expected.spvasm b/test/tint/bug/tint/2175.wgsl.expected.spvasm
new file mode 100644
index 0000000..b439598
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.spvasm
@@ -0,0 +1,32 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 13
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %tint_symbol_3 "tint_symbol_3"
+               OpExecutionMode %tint_symbol_3 LocalSize 1 1 1
+               OpName %tint_symbol_2_block "tint_symbol_2_block"
+               OpMemberName %tint_symbol_2_block 0 "inner"
+               OpName %tint_symbol_2 "tint_symbol_2"
+               OpName %tint_symbol_3 "tint_symbol_3"
+               OpDecorate %tint_symbol_2_block Block
+               OpMemberDecorate %tint_symbol_2_block 0 Offset 0
+               OpDecorate %tint_symbol_2 DescriptorSet 0
+               OpDecorate %tint_symbol_2 Binding 0
+       %uint = OpTypeInt 32 0
+%tint_symbol_2_block = OpTypeStruct %uint
+%_ptr_StorageBuffer_tint_symbol_2_block = OpTypePointer StorageBuffer %tint_symbol_2_block
+%tint_symbol_2 = OpVariable %_ptr_StorageBuffer_tint_symbol_2_block StorageBuffer
+       %void = OpTypeVoid
+          %5 = OpTypeFunction %void
+     %uint_0 = OpConstant %uint 0
+%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
+         %12 = OpConstantNull %uint
+%tint_symbol_3 = OpFunction %void None %5
+          %8 = OpLabel
+         %11 = OpAccessChain %_ptr_StorageBuffer_uint %tint_symbol_2 %uint_0
+               OpStore %11 %12
+               OpReturn
+               OpFunctionEnd
diff --git a/test/tint/bug/tint/2175.wgsl.expected.wgsl b/test/tint/bug/tint/2175.wgsl.expected.wgsl
new file mode 100644
index 0000000..d043338
--- /dev/null
+++ b/test/tint/bug/tint/2175.wgsl.expected.wgsl
@@ -0,0 +1,10 @@
+const tint_symbol = vec4();
+
+const tint_symbol_1 = (tint_symbol.x * 2u);
+
+@group(0) @binding(0) var<storage, read_write> tint_symbol_2 : u32;
+
+@compute @workgroup_size(1)
+fn tint_symbol_3() {
+  tint_symbol_2 = tint_symbol_1;
+}