[ir] Process uncalled functions in DemoteToHelper

Otherwise unreachable functions that contain discard instructions can
make it through to the printers and ICE.

Fixed: tint:2052
Change-Id: I52dbe4d41894c6e962e1f0ba7210035c1934592d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/155220
Reviewed-by: Ben Clayton <bclayton@google.com>
Auto-Submit: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
diff --git a/src/tint/lang/core/ir/transform/demote_to_helper.cc b/src/tint/lang/core/ir/transform/demote_to_helper.cc
index 8749807..b3c0431 100644
--- a/src/tint/lang/core/ir/transform/demote_to_helper.cc
+++ b/src/tint/lang/core/ir/transform/demote_to_helper.cc
@@ -50,15 +50,13 @@
 
     /// Process the module.
     void Process() {
-        // Check each fragment shader entry point for discard instruction, potentially inside
-        // functions called (transitively) by the entry point.
+        // Check each function for discard instructions, potentially inside other functions called
+        // (transitively) by the function.
         Vector<Function*, 4> to_process;
         for (auto* func : ir.functions) {
-            // If the function is a fragment shader that contains a discard, we need to process it.
-            if (func->Stage() == Function::PipelineStage::kFragment) {
-                if (HasDiscard(func)) {
-                    to_process.Push(func);
-                }
+            // If the function contains a discard (directly or indirectly), we need to process it.
+            if (HasDiscard(func)) {
+                to_process.Push(func);
             }
         }
         if (to_process.IsEmpty()) {
@@ -70,7 +68,7 @@
         continue_execution->SetInitializer(b.Constant(true));
         ir.root_block->Append(continue_execution);
 
-        // Process each entry point function that contains a discard.
+        // Process each function that directly or indirectly discards.
         for (auto* ep : to_process) {
             ProcessFunction(ep);
         }
diff --git a/src/tint/lang/core/ir/transform/demote_to_helper_test.cc b/src/tint/lang/core/ir/transform/demote_to_helper_test.cc
index 3efe9f4..9da0b70 100644
--- a/src/tint/lang/core/ir/transform/demote_to_helper_test.cc
+++ b/src/tint/lang/core/ir/transform/demote_to_helper_test.cc
@@ -860,5 +860,41 @@
     EXPECT_EQ(expect, str());
 }
 
+// Test that we transform unreachable functions that discard (see crbug.com/tint/2052).
+TEST_F(IR_DemoteToHelperTest, UnreachableHelperThatDiscards) {
+    auto* helper = b.Function("foo", ty.void_());
+    b.Append(helper->Block(), [&] {
+        b.Discard();
+        b.Return(helper);
+    });
+
+    auto* src = R"(
+%foo = func():void -> %b1 {
+  %b1 = block {
+    discard
+    ret
+  }
+}
+)";
+    EXPECT_EQ(src, str());
+
+    auto* expect = R"(
+%b1 = block {  # root
+  %continue_execution:ptr<private, bool, read_write> = var, true
+}
+
+%foo = func():void -> %b2 {
+  %b2 = block {
+    store %continue_execution, false
+    ret
+  }
+}
+)";
+
+    Run(DemoteToHelper);
+
+    EXPECT_EQ(expect, str());
+}
+
 }  // namespace
 }  // namespace tint::core::ir::transform
diff --git a/test/tint/bug/tint/2052.wgsl b/test/tint/bug/tint/2052.wgsl
new file mode 100644
index 0000000..cfcae8d
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl
@@ -0,0 +1,3 @@
+fn f() {
+  discard;
+}
diff --git a/test/tint/bug/tint/2052.wgsl.expected.dxc.hlsl b/test/tint/bug/tint/2052.wgsl.expected.dxc.hlsl
new file mode 100644
index 0000000..98f1f94
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.dxc.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  discard;
+}
diff --git a/test/tint/bug/tint/2052.wgsl.expected.fxc.hlsl b/test/tint/bug/tint/2052.wgsl.expected.fxc.hlsl
new file mode 100644
index 0000000..98f1f94
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.fxc.hlsl
@@ -0,0 +1,8 @@
+[numthreads(1, 1, 1)]
+void unused_entry_point() {
+  return;
+}
+
+void f() {
+  discard;
+}
diff --git a/test/tint/bug/tint/2052.wgsl.expected.glsl b/test/tint/bug/tint/2052.wgsl.expected.glsl
new file mode 100644
index 0000000..09f2599
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.glsl
@@ -0,0 +1,19 @@
+SKIP: FAILED
+
+#version 310 es
+
+layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
+void unused_entry_point() {
+  return;
+}
+void f() {
+  discard;
+}
+
+Error parsing GLSL shader:
+ERROR: 0:8: 'discard' : not supported in this stage: compute
+ERROR: 0:8: '' : compilation terminated 
+ERROR: 2 compilation errors.  No code generated.
+
+
+
diff --git a/test/tint/bug/tint/2052.wgsl.expected.msl b/test/tint/bug/tint/2052.wgsl.expected.msl
new file mode 100644
index 0000000..82e7a21
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.msl
@@ -0,0 +1,7 @@
+#include <metal_stdlib>
+
+using namespace metal;
+void f() {
+  discard_fragment();
+}
+
diff --git a/test/tint/bug/tint/2052.wgsl.expected.spvasm b/test/tint/bug/tint/2052.wgsl.expected.spvasm
new file mode 100644
index 0000000..e217d8e
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.spvasm
@@ -0,0 +1,21 @@
+; SPIR-V
+; Version: 1.3
+; Generator: Google Tint Compiler; 0
+; Bound: 7
+; 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
+%unused_entry_point = OpFunction %void None %1
+          %4 = OpLabel
+               OpReturn
+               OpFunctionEnd
+          %f = OpFunction %void None %1
+          %6 = OpLabel
+               OpKill
+               OpFunctionEnd
diff --git a/test/tint/bug/tint/2052.wgsl.expected.wgsl b/test/tint/bug/tint/2052.wgsl.expected.wgsl
new file mode 100644
index 0000000..cfcae8d
--- /dev/null
+++ b/test/tint/bug/tint/2052.wgsl.expected.wgsl
@@ -0,0 +1,3 @@
+fn f() {
+  discard;
+}