Add unit test for function dead-stripping.

This test verifies that dead-stripping works on both built-in and user
functions, if their function call is optimized away.

Change-Id: I3125a34640c69de43c383343cd00d97e5a32ac60
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/338836
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/gn/sksl_tests.gni b/gn/sksl_tests.gni
index fa0b908..b3cd2d2 100644
--- a/gn/sksl_tests.gni
+++ b/gn/sksl_tests.gni
@@ -200,6 +200,7 @@
   "$_tests/sksl/shared/DeadDoWhileLoop.sksl",
   "$_tests/sksl/shared/DeadIfStatement.sksl",
   "$_tests/sksl/shared/DeadLoopVariable.sksl",
+  "$_tests/sksl/shared/DeadStripFunctions.sksl",
   "$_tests/sksl/shared/DependentInitializers.sksl",
   "$_tests/sksl/shared/DerivativesUnused.sksl",
   "$_tests/sksl/shared/Discard.sksl",
diff --git a/tests/sksl/shared/DeadStripFunctions.sksl b/tests/sksl/shared/DeadStripFunctions.sksl
new file mode 100644
index 0000000..85bcaab
--- /dev/null
+++ b/tests/sksl/shared/DeadStripFunctions.sksl
@@ -0,0 +1,27 @@
+/*#pragma settings NoInline*/
+
+half4 dead_fn(half4 a, half4 b) {
+    return a * b;
+}
+
+half4 live_fn(half4 a, half4 b) {
+    return a + b;
+}
+
+void main() {
+    @switch (999) {
+        // Dead stripping a user function.
+        case 0: sk_FragColor = dead_fn(half4(0.5), half4(2)); break;
+
+        // A live user function.
+        case 999: sk_FragColor = live_fn(half4(3), half4(-3)); break;
+    }
+
+    @switch (777) {
+        // Dead stripping a built-in function.
+        case 0: sk_FragColor = blend_modulate(half4(1), half4(-1)); break;
+
+        // A live built-in function.
+        case 777: sk_FragColor = blend_dst(half4(7), half4(-7)); break;
+    }
+}
diff --git a/tests/sksl/shared/golden/DeadStripFunctions.asm.frag b/tests/sksl/shared/golden/DeadStripFunctions.asm.frag
new file mode 100644
index 0000000..a24e610
--- /dev/null
+++ b/tests/sksl/shared/golden/DeadStripFunctions.asm.frag
@@ -0,0 +1,70 @@
+OpCapability Shader
+%1 = OpExtInstImport "GLSL.std.450"
+OpMemoryModel Logical GLSL450
+OpEntryPoint Fragment %main "main" %sk_FragColor %sk_Clockwise
+OpExecutionMode %main OriginUpperLeft
+OpName %sk_FragColor "sk_FragColor"
+OpName %sk_Clockwise "sk_Clockwise"
+OpName %blend_dst "blend_dst"
+OpName %live_fn "live_fn"
+OpName %main "main"
+OpDecorate %sk_FragColor RelaxedPrecision
+OpDecorate %sk_FragColor Location 0
+OpDecorate %sk_FragColor Index 0
+OpDecorate %sk_Clockwise RelaxedPrecision
+OpDecorate %sk_Clockwise BuiltIn FrontFacing
+OpDecorate %17 RelaxedPrecision
+OpDecorate %21 RelaxedPrecision
+OpDecorate %22 RelaxedPrecision
+OpDecorate %23 RelaxedPrecision
+%float = OpTypeFloat 32
+%v4float = OpTypeVector %float 4
+%_ptr_Output_v4float = OpTypePointer Output %v4float
+%sk_FragColor = OpVariable %_ptr_Output_v4float Output
+%bool = OpTypeBool
+%_ptr_Input_bool = OpTypePointer Input %bool
+%sk_Clockwise = OpVariable %_ptr_Input_bool Input
+%_ptr_Function_v4float = OpTypePointer Function %v4float
+%12 = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
+%void = OpTypeVoid
+%25 = OpTypeFunction %void
+%float_3 = OpConstant %float 3
+%27 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
+%float_n3 = OpConstant %float -3
+%30 = OpConstantComposite %v4float %float_n3 %float_n3 %float_n3 %float_n3
+%float_7 = OpConstant %float 7
+%34 = OpConstantComposite %v4float %float_7 %float_7 %float_7 %float_7
+%float_n7 = OpConstant %float -7
+%37 = OpConstantComposite %v4float %float_n7 %float_n7 %float_n7 %float_n7
+%blend_dst = OpFunction %v4float None %12
+%14 = OpFunctionParameter %_ptr_Function_v4float
+%15 = OpFunctionParameter %_ptr_Function_v4float
+%16 = OpLabel
+%17 = OpLoad %v4float %15
+OpReturnValue %17
+OpFunctionEnd
+%live_fn = OpFunction %v4float None %12
+%18 = OpFunctionParameter %_ptr_Function_v4float
+%19 = OpFunctionParameter %_ptr_Function_v4float
+%20 = OpLabel
+%21 = OpLoad %v4float %18
+%22 = OpLoad %v4float %19
+%23 = OpFAdd %v4float %21 %22
+OpReturnValue %23
+OpFunctionEnd
+%main = OpFunction %void None %25
+%26 = OpLabel
+%29 = OpVariable %_ptr_Function_v4float Function
+%32 = OpVariable %_ptr_Function_v4float Function
+%36 = OpVariable %_ptr_Function_v4float Function
+%39 = OpVariable %_ptr_Function_v4float Function
+OpStore %29 %27
+OpStore %32 %30
+%33 = OpFunctionCall %v4float %live_fn %29 %32
+OpStore %sk_FragColor %33
+OpStore %36 %34
+OpStore %39 %37
+%40 = OpFunctionCall %v4float %blend_dst %36 %39
+OpStore %sk_FragColor %40
+OpReturn
+OpFunctionEnd
diff --git a/tests/sksl/shared/golden/DeadStripFunctions.glsl b/tests/sksl/shared/golden/DeadStripFunctions.glsl
new file mode 100644
index 0000000..517df40
--- /dev/null
+++ b/tests/sksl/shared/golden/DeadStripFunctions.glsl
@@ -0,0 +1,16 @@
+
+out vec4 sk_FragColor;
+vec4 blend_dst(vec4 src, vec4 dst) {
+    return dst;
+}
+vec4 live_fn(vec4 a, vec4 b) {
+    return a + b;
+}
+void main() {
+    {
+        sk_FragColor = live_fn(vec4(3.0), vec4(-3.0));
+    }
+    {
+        sk_FragColor = blend_dst(vec4(7.0), vec4(-7.0));
+    }
+}
diff --git a/tests/sksl/shared/golden/DeadStripFunctions.metal b/tests/sksl/shared/golden/DeadStripFunctions.metal
new file mode 100644
index 0000000..95556db
--- /dev/null
+++ b/tests/sksl/shared/golden/DeadStripFunctions.metal
@@ -0,0 +1,25 @@
+#include <metal_stdlib>
+#include <simd/simd.h>
+using namespace metal;
+struct Inputs {
+};
+struct Outputs {
+    float4 sk_FragColor [[color(0)]];
+};
+float4 blend_dst(float4 src, float4 dst) {
+    return dst;
+}
+float4 live_fn(float4 a, float4 b) {
+    return a + b;
+}
+fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
+    Outputs _outputStruct;
+    thread Outputs* _out = &_outputStruct;
+    {
+        _out->sk_FragColor = live_fn(float4(3.0), float4(-3.0));
+    }
+    {
+        _out->sk_FragColor = blend_dst(float4(7.0), float4(-7.0));
+    }
+    return *_out;
+}