[tint][IRToProgram] Create phony assignment if a value is not used

Bug: chromium:335592006
Change-Id: I7a88cd14e8180e41968702c1624b4f32a49526b0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/185584
Reviewed-by: James Price <jrprice@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/wgsl/ir_roundtrip_test.cc b/src/tint/lang/wgsl/ir_roundtrip_test.cc
index 07fcbb3..6573c32 100644
--- a/src/tint/lang/wgsl/ir_roundtrip_test.cc
+++ b/src/tint/lang/wgsl/ir_roundtrip_test.cc
@@ -1929,6 +1929,21 @@
 )");
 }
 
+TEST_F(IRToProgramRoundtripTest, PhonyAssign_Conversion) {
+    RUN_TEST(R"(
+fn f() {
+  let i = 42i;
+  _ = u32(i);
+}
+)",
+             R"(
+fn f() {
+  let i = 42i;
+  _ = u32(i);
+}
+)");
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // let
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.cc b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.cc
index 72a26ef..8840d93 100644
--- a/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.cc
+++ b/src/tint/lang/wgsl/writer/ir_to_program/ir_to_program.cc
@@ -1058,12 +1058,17 @@
         });
     }
 
-    /// Associates the IR value @p value with the AST expression @p expr.
+    /// Associates the IR value @p value with the AST expression @p expr if it is used, otherwise
+    /// creates a phony assignment with @p expr.
     void Bind(const core::ir::Value* value, const ast::Expression* expr) {
         TINT_ASSERT(value);
-        // Value will be inlined at its place of usage.
-        if (TINT_UNLIKELY(!bindings_.Add(value, InlinedValue{expr}))) {
-            TINT_ICE() << "Bind(" << value->TypeInfo().name << ") called twice for same value";
+        if (value->IsUsed()) {
+            // Value will be inlined at its place of usage.
+            if (TINT_UNLIKELY(!bindings_.Add(value, InlinedValue{expr}))) {
+                TINT_ICE() << "Bind(" << value->TypeInfo().name << ") called twice for same value";
+            }
+        } else {
+            Append(b.Assign(b.Phony(), expr));
         }
     }
 
diff --git a/test/tint/bug/chromium/335592006.wgsl b/test/tint/bug/chromium/335592006.wgsl
new file mode 100644
index 0000000..191cccf
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl
@@ -0,0 +1 @@
+fn f(){let a=1;_=i32(a);}
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.dxc.hlsl b/test/tint/bug/chromium/335592006.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..705b3d6
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.dxc.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  int a = 1;
+}
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.fxc.hlsl b/test/tint/bug/chromium/335592006.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..705b3d6
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.fxc.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  int a = 1;
+}
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.glsl b/test/tint/bug/chromium/335592006.wgsl.expected.glsl
new file mode 100644
index 0000000..3390b56
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.glsl
@@ -0,0 +1,10 @@
+#version 310 es
+
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void unused_entry_point() {
+  return;
+}
+void f() {
+  int a = 1;
+}
+
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.msl b/test/tint/bug/chromium/335592006.wgsl.expected.msl
new file mode 100644
index 0000000..eca94fd
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.msl
@@ -0,0 +1,7 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f() {
+  int const a = 1;
+}
+
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.spvasm b/test/tint/bug/chromium/335592006.wgsl.expected.spvasm
new file mode 100644
index 0000000..a8ec188
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.spvasm
@@ -0,0 +1,23 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 9
+; Schema: 0
+               OpCapability Shader
+               OpMemoryModel Logical GLSL450
+               OpEntryPoint GLCompute %unused_entry_point "unused_entry_point"
+               OpExecutionMode %unused_entry_point LocalSize 1 1 1
+               OpName %unused_entry_point "unused_entry_point"
+               OpName %f "f"
+       %void = OpTypeVoid
+          %1 = OpTypeFunction %void
+        %int = OpTypeInt 32 1
+      %int_1 = OpConstant %int 1
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+               OpReturn
+               OpFunctionEnd
diff --git a/test/tint/bug/chromium/335592006.wgsl.expected.wgsl b/test/tint/bug/chromium/335592006.wgsl.expected.wgsl
new file mode 100644
index 0000000..1eb9472
--- /dev/null
+++ b/test/tint/bug/chromium/335592006.wgsl.expected.wgsl
@@ -0,0 +1,4 @@
+fn f() {
+  let a = 1;
+  _ = i32(a);
+}