[ir] Fix cloning of variable initializers

Fix the test that was checking the source initializer instead of the
result initializer.

Fixed: tint:2121, chromium:1512621
Change-Id: Ia38494036b208efdf640e63c5fba93cfa33a1002
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/166000
Reviewed-by: Ben Clayton <bclayton@google.com>
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
(cherry picked from commit 9c54476ce64f21d6878ede05af9397dd55973a14)
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/168022
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/core/ir/transform/direct_variable_access_wgsl_test.cc b/src/tint/lang/core/ir/transform/direct_variable_access_wgsl_test.cc
index 7b8a407..a22171f 100644
--- a/src/tint/lang/core/ir/transform/direct_variable_access_wgsl_test.cc
+++ b/src/tint/lang/core/ir/transform/direct_variable_access_wgsl_test.cc
@@ -618,7 +618,7 @@
 fn c_U() {
   let v_2 = first();
   let v_3 = second();
-  for(var i : i32; (i < 3i); a_U_X_X(10i, array<u32, 2u>(u32(v_2), u32(v_3)), 20i)) {
+  for(var i : i32 = 0i; (i < 3i); a_U_X_X(10i, array<u32, 2u>(u32(v_2), u32(v_3)), 20i)) {
     i = (i + 1i);
   }
 }
diff --git a/src/tint/lang/core/ir/var.cc b/src/tint/lang/core/ir/var.cc
index b9a8f91..d95885b 100644
--- a/src/tint/lang/core/ir/var.cc
+++ b/src/tint/lang/core/ir/var.cc
@@ -56,6 +56,10 @@
     new_var->binding_point_ = binding_point_;
     new_var->attributes_ = attributes_;
 
+    if (auto* init = Initializer()) {
+        new_var->SetInitializer(ctx.Clone(init));
+    }
+
     auto name = ctx.ir.NameOf(this);
     if (name.IsValid()) {
         ctx.ir.SetName(new_var, name.Name());
diff --git a/src/tint/lang/core/ir/var_test.cc b/src/tint/lang/core/ir/var_test.cc
index f49c79b..3353672 100644
--- a/src/tint/lang/core/ir/var_test.cc
+++ b/src/tint/lang/core/ir/var_test.cc
@@ -87,7 +87,8 @@
     EXPECT_EQ(new_v->Result(0)->Type(),
               mod.Types().ptr(core::AddressSpace::kFunction, mod.Types().f32()));
 
-    auto new_val = v->Initializer()->As<Constant>()->Value();
+    ASSERT_NE(nullptr, new_v->Initializer());
+    auto new_val = new_v->Initializer()->As<Constant>()->Value();
     ASSERT_TRUE(new_val->Is<core::constant::Scalar<f32>>());
     EXPECT_FLOAT_EQ(4_f, new_val->As<core::constant::Scalar<f32>>()->ValueAs<f32>());
 
diff --git a/test/tint/bug/tint/2121.wgsl b/test/tint/bug/tint/2121.wgsl
new file mode 100644
index 0000000..5b9f056
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl
@@ -0,0 +1,14 @@
+struct VSOut {
+  @builtin(position) pos: vec4<f32>,
+};
+
+fn foo(out: ptr<function, VSOut>) {
+  var pos = vec4f(1, 2, 3, 4);
+  (*out).pos = pos;
+}
+
+@vertex fn main() -> VSOut {
+  var out: VSOut;
+  foo(&out);
+  return out;
+}
diff --git a/test/tint/bug/tint/2121.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/2121.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..db50590
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.dxc.hlsl
@@ -0,0 +1,25 @@
+struct VSOut {
+  float4 pos;
+};
+
+void foo(inout VSOut tint_symbol) {
+  float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
+  tint_symbol.pos = pos;
+}
+
+struct tint_symbol_1 {
+  float4 pos : SV_Position;
+};
+
+VSOut main_inner() {
+  VSOut tint_symbol = (VSOut)0;
+  foo(tint_symbol);
+  return tint_symbol;
+}
+
+tint_symbol_1 main() {
+  const VSOut inner_result = main_inner();
+  tint_symbol_1 wrapper_result = (tint_symbol_1)0;
+  wrapper_result.pos = inner_result.pos;
+  return wrapper_result;
+}
diff --git a/test/tint/bug/tint/2121.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/2121.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..db50590
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.fxc.hlsl
@@ -0,0 +1,25 @@
+struct VSOut {
+  float4 pos;
+};
+
+void foo(inout VSOut tint_symbol) {
+  float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
+  tint_symbol.pos = pos;
+}
+
+struct tint_symbol_1 {
+  float4 pos : SV_Position;
+};
+
+VSOut main_inner() {
+  VSOut tint_symbol = (VSOut)0;
+  foo(tint_symbol);
+  return tint_symbol;
+}
+
+tint_symbol_1 main() {
+  const VSOut inner_result = main_inner();
+  tint_symbol_1 wrapper_result = (tint_symbol_1)0;
+  wrapper_result.pos = inner_result.pos;
+  return wrapper_result;
+}
diff --git a/test/tint/bug/tint/2121.wgsl.expected.glsl b/test/tint/bug/tint/2121.wgsl.expected.glsl
new file mode 100644
index 0000000..6736f17
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.glsl
@@ -0,0 +1,25 @@
+#version 310 es
+
+struct VSOut {
+  vec4 pos;
+};
+
+void foo(inout VSOut tint_symbol) {
+  vec4 pos = vec4(1.0f, 2.0f, 3.0f, 4.0f);
+  tint_symbol.pos = pos;
+}
+
+VSOut tint_symbol_1() {
+  VSOut tint_symbol = VSOut(vec4(0.0f, 0.0f, 0.0f, 0.0f));
+  foo(tint_symbol);
+  return tint_symbol;
+}
+
+void main() {
+  gl_PointSize = 1.0;
+  VSOut inner_result = tint_symbol_1();
+  gl_Position = inner_result.pos;
+  gl_Position.y = -(gl_Position.y);
+  gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
+  return;
+}
diff --git a/test/tint/bug/tint/2121.wgsl.expected.msl b/test/tint/bug/tint/2121.wgsl.expected.msl
new file mode 100644
index 0000000..594ee9d
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.msl
@@ -0,0 +1,29 @@
+#include <metal_stdlib>
+
+using namespace metal;
+struct VSOut {
+  float4 pos;
+};
+
+void foo(thread VSOut* const out) {
+  float4 pos = float4(1.0f, 2.0f, 3.0f, 4.0f);
+  (*(out)).pos = pos;
+}
+
+struct tint_symbol_1 {
+  float4 pos [[position]];
+};
+
+VSOut tint_symbol_inner() {
+  VSOut out = {};
+  foo(&(out));
+  return out;
+}
+
+vertex tint_symbol_1 tint_symbol() {
+  VSOut const inner_result = tint_symbol_inner();
+  tint_symbol_1 wrapper_result = {};
+  wrapper_result.pos = inner_result.pos;
+  return wrapper_result;
+}
+
diff --git a/test/tint/bug/tint/2121.wgsl.expected.spvasm b/test/tint/bug/tint/2121.wgsl.expected.spvasm
new file mode 100644
index 0000000..f0651b9
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.spvasm
@@ -0,0 +1,69 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 41
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint Vertex %main "main" %pos_1 %vertex_point_size
+               OpName %pos_1 "pos_1"
+               OpName %vertex_point_size "vertex_point_size"
+               OpName %VSOut "VSOut"
+               OpMemberName %VSOut 0 "pos"
+               OpName %foo "foo"
+               OpName %out "out"
+               OpName %pos "pos"
+               OpName %main_inner "main_inner"
+               OpName %out_0 "out"
+               OpName %main "main"
+               OpDecorate %pos_1 BuiltIn Position
+               OpDecorate %vertex_point_size BuiltIn PointSize
+               OpMemberDecorate %VSOut 0 Offset 0
+      %float = OpTypeFloat 32
+    %v4float = OpTypeVector %float 4
+%_ptr_Output_v4float = OpTypePointer Output %v4float
+          %5 = OpConstantNull %v4float
+      %pos_1 = OpVariable %_ptr_Output_v4float Output %5
+%_ptr_Output_float = OpTypePointer Output %float
+          %8 = OpConstantNull %float
+%vertex_point_size = OpVariable %_ptr_Output_float Output %8
+       %void = OpTypeVoid
+      %VSOut = OpTypeStruct %v4float
+%_ptr_Function_VSOut = OpTypePointer Function %VSOut
+          %9 = OpTypeFunction %void %_ptr_Function_VSOut
+    %float_1 = OpConstant %float 1
+    %float_2 = OpConstant %float 2
+    %float_3 = OpConstant %float 3
+    %float_4 = OpConstant %float 4
+         %20 = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
+%_ptr_Function_v4float = OpTypePointer Function %v4float
+       %uint = OpTypeInt 32 0
+     %uint_0 = OpConstant %uint 0
+         %28 = OpTypeFunction %VSOut
+         %32 = OpConstantNull %VSOut
+         %36 = OpTypeFunction %void
+        %foo = OpFunction %void None %9
+        %out = OpFunctionParameter %_ptr_Function_VSOut
+         %15 = OpLabel
+        %pos = OpVariable %_ptr_Function_v4float Function %5
+               OpStore %pos %20
+         %26 = OpAccessChain %_ptr_Function_v4float %out %uint_0
+         %27 = OpLoad %v4float %pos
+               OpStore %26 %27
+               OpReturn
+               OpFunctionEnd
+ %main_inner = OpFunction %VSOut None %28
+         %30 = OpLabel
+      %out_0 = OpVariable %_ptr_Function_VSOut Function %32
+         %33 = OpFunctionCall %void %foo %out_0
+         %35 = OpLoad %VSOut %out_0
+               OpReturnValue %35
+               OpFunctionEnd
+       %main = OpFunction %void None %36
+         %38 = OpLabel
+         %39 = OpFunctionCall %VSOut %main_inner
+         %40 = OpCompositeExtract %v4float %39 0
+               OpStore %pos_1 %40
+               OpStore %vertex_point_size %float_1
+               OpReturn
+               OpFunctionEnd
diff --git a/test/tint/bug/tint/2121.wgsl.expected.wgsl b/test/tint/bug/tint/2121.wgsl.expected.wgsl
new file mode 100644
index 0000000..beed357
--- /dev/null
+++ b/test/tint/bug/tint/2121.wgsl.expected.wgsl
@@ -0,0 +1,16 @@
+struct VSOut {
+  @builtin(position)
+  pos : vec4<f32>,
+}
+
+fn foo(out : ptr<function, VSOut>) {
+  var pos = vec4f(1, 2, 3, 4);
+  (*(out)).pos = pos;
+}
+
+@vertex
+fn main() -> VSOut {
+  var out : VSOut;
+  foo(&(out));
+  return out;
+}