Support half4 return values from main() in the SPIR-V code generator.

This creates a helper function, _entrypoint, which invokes main() and
assigns its result into sk_FragColor. We also make sure to prevent
sk_FragColor from being dead-stripped from the code during IR
generation.

At present this is useful for allowing our SkSL test shaders to compile.

Change-Id: I2d7fab0e1959a77778ffdb18ca569e869bcaeece
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/358525
Commit-Queue: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
diff --git a/tests/sksl/shared/ForLoopControlFlow.asm.frag b/tests/sksl/shared/ForLoopControlFlow.asm.frag
index 31e45e3..bc59e24 100644
--- a/tests/sksl/shared/ForLoopControlFlow.asm.frag
+++ b/tests/sksl/shared/ForLoopControlFlow.asm.frag
@@ -1,43 +1,54 @@
 ### Compilation failed:
 
-error: SPIR-V validation error: OpEntryPoint Entry Point <id> '2[%main]'s function return type is not void.
-  OpEntryPoint Fragment %main "main" %sk_Clockwise
+error: SPIR-V validation error: Uniform OpVariable <id> '10[%colorWhite]' has illegal type.
+From Vulkan spec, section 14.5.2:
+Variables identified with the Uniform storage class are used to access transparent buffer backed resources. Such variables must be typed as OpTypeStruct, or an array of this type
+  %colorWhite = OpVariable %_ptr_Uniform_v4float Uniform
 
 OpCapability Shader
 %1 = OpExtInstImport "GLSL.std.450"
 OpMemoryModel Logical GLSL450
-OpEntryPoint Fragment %main "main" %sk_Clockwise
-OpExecutionMode %main OriginUpperLeft
+OpEntryPoint Fragment %_entrypoint "_entrypoint" %sk_FragColor %sk_Clockwise
+OpExecutionMode %_entrypoint OriginUpperLeft
+OpName %sk_FragColor "sk_FragColor"
 OpName %sk_Clockwise "sk_Clockwise"
 OpName %colorWhite "colorWhite"
+OpName %_entrypoint "_entrypoint"
 OpName %main "main"
 OpName %x "x"
 OpName %r "r"
 OpName %b "b"
+OpDecorate %sk_FragColor RelaxedPrecision
+OpDecorate %sk_FragColor Location 0
+OpDecorate %sk_FragColor Index 0
 OpDecorate %sk_Clockwise RelaxedPrecision
 OpDecorate %sk_Clockwise BuiltIn FrontFacing
 OpDecorate %colorWhite RelaxedPrecision
 OpDecorate %colorWhite DescriptorSet 0
-OpDecorate %14 RelaxedPrecision
-OpDecorate %23 RelaxedPrecision
-OpDecorate %27 RelaxedPrecision
-OpDecorate %31 RelaxedPrecision
-OpDecorate %37 RelaxedPrecision
-OpDecorate %39 RelaxedPrecision
+OpDecorate %21 RelaxedPrecision
+OpDecorate %30 RelaxedPrecision
+OpDecorate %34 RelaxedPrecision
+OpDecorate %38 RelaxedPrecision
+OpDecorate %44 RelaxedPrecision
 OpDecorate %46 RelaxedPrecision
-OpDecorate %48 RelaxedPrecision
-OpDecorate %51 RelaxedPrecision
+OpDecorate %53 RelaxedPrecision
+OpDecorate %55 RelaxedPrecision
 OpDecorate %58 RelaxedPrecision
-OpDecorate %59 RelaxedPrecision
-OpDecorate %60 RelaxedPrecision
+OpDecorate %65 RelaxedPrecision
+OpDecorate %66 RelaxedPrecision
+OpDecorate %67 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
-%float = OpTypeFloat 32
-%v4float = OpTypeVector %float 4
 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
 %colorWhite = OpVariable %_ptr_Uniform_v4float Uniform
-%10 = OpTypeFunction %v4float
+%void = OpTypeVoid
+%14 = OpTypeFunction %void
+%17 = OpTypeFunction %v4float
 %_ptr_Function_v4float = OpTypePointer Function %v4float
 %_ptr_Function_float = OpTypePointer Function %float
 %float_n5 = OpConstant %float -5
@@ -48,74 +59,80 @@
 %float_1 = OpConstant %float 1
 %int_2 = OpConstant %int 2
 %int_1 = OpConstant %int 1
-%main = OpFunction %v4float None %10
-%11 = OpLabel
+%_entrypoint = OpFunction %void None %14
+%15 = OpLabel
+%16 = OpFunctionCall %v4float %main
+OpStore %sk_FragColor %16
+OpReturn
+OpFunctionEnd
+%main = OpFunction %v4float None %17
+%18 = OpLabel
 %x = OpVariable %_ptr_Function_v4float Function
 %r = OpVariable %_ptr_Function_float Function
 %b = OpVariable %_ptr_Function_float Function
-%14 = OpLoad %v4float %colorWhite
-OpStore %x %14
+%21 = OpLoad %v4float %colorWhite
+OpStore %x %21
 OpStore %r %float_n5
-OpBranch %18
-%18 = OpLabel
-OpLoopMerge %22 %21 None
-OpBranch %19
-%19 = OpLabel
-%23 = OpLoad %float %r
-%25 = OpFOrdLessThan %bool %23 %float_5
-OpBranchConditional %25 %20 %22
-%20 = OpLabel
-%27 = OpLoad %float %r
-%26 = OpExtInst %float %1 FAbs %27
-%28 = OpAccessChain %_ptr_Function_float %x %int_0
-OpStore %28 %26
-%31 = OpLoad %v4float %x
-%32 = OpCompositeExtract %float %31 0
-%34 = OpFOrdEqual %bool %32 %float_0
-OpSelectionMerge %36 None
-OpBranchConditional %34 %35 %36
-%35 = OpLabel
-OpBranch %22
-%36 = OpLabel
-OpBranch %21
-%21 = OpLabel
-%37 = OpLoad %float %r
-%39 = OpFAdd %float %37 %float_1
-OpStore %r %39
-OpBranch %18
-%22 = OpLabel
-OpStore %b %float_5
-OpBranch %41
-%41 = OpLabel
-OpLoopMerge %45 %44 None
-OpBranch %42
+OpBranch %25
+%25 = OpLabel
+OpLoopMerge %29 %28 None
+OpBranch %26
+%26 = OpLabel
+%30 = OpLoad %float %r
+%32 = OpFOrdLessThan %bool %30 %float_5
+OpBranchConditional %32 %27 %29
+%27 = OpLabel
+%34 = OpLoad %float %r
+%33 = OpExtInst %float %1 FAbs %34
+%35 = OpAccessChain %_ptr_Function_float %x %int_0
+OpStore %35 %33
+%38 = OpLoad %v4float %x
+%39 = OpCompositeExtract %float %38 0
+%41 = OpFOrdEqual %bool %39 %float_0
+OpSelectionMerge %43 None
+OpBranchConditional %41 %42 %43
 %42 = OpLabel
-%46 = OpLoad %float %b
-%47 = OpFOrdGreaterThanEqual %bool %46 %float_0
-OpBranchConditional %47 %43 %45
+OpBranch %29
 %43 = OpLabel
-%48 = OpLoad %float %b
-%49 = OpAccessChain %_ptr_Function_float %x %int_2
-OpStore %49 %48
-%51 = OpLoad %v4float %x
-%52 = OpCompositeExtract %float %51 3
-%53 = OpFOrdEqual %bool %52 %float_1
-OpSelectionMerge %55 None
-OpBranchConditional %53 %54 %55
-%54 = OpLabel
-OpBranch %44
-%55 = OpLabel
-%56 = OpAccessChain %_ptr_Function_float %x %int_1
-OpStore %56 %float_0
-OpBranch %44
-%44 = OpLabel
-%58 = OpLoad %float %b
-%59 = OpFSub %float %58 %float_1
-OpStore %b %59
-OpBranch %41
-%45 = OpLabel
-%60 = OpLoad %v4float %x
-OpReturnValue %60
+OpBranch %28
+%28 = OpLabel
+%44 = OpLoad %float %r
+%46 = OpFAdd %float %44 %float_1
+OpStore %r %46
+OpBranch %25
+%29 = OpLabel
+OpStore %b %float_5
+OpBranch %48
+%48 = OpLabel
+OpLoopMerge %52 %51 None
+OpBranch %49
+%49 = OpLabel
+%53 = OpLoad %float %b
+%54 = OpFOrdGreaterThanEqual %bool %53 %float_0
+OpBranchConditional %54 %50 %52
+%50 = OpLabel
+%55 = OpLoad %float %b
+%56 = OpAccessChain %_ptr_Function_float %x %int_2
+OpStore %56 %55
+%58 = OpLoad %v4float %x
+%59 = OpCompositeExtract %float %58 3
+%60 = OpFOrdEqual %bool %59 %float_1
+OpSelectionMerge %62 None
+OpBranchConditional %60 %61 %62
+%61 = OpLabel
+OpBranch %51
+%62 = OpLabel
+%63 = OpAccessChain %_ptr_Function_float %x %int_1
+OpStore %63 %float_0
+OpBranch %51
+%51 = OpLabel
+%65 = OpLoad %float %b
+%66 = OpFSub %float %65 %float_1
+OpStore %b %66
+OpBranch %48
+%52 = OpLabel
+%67 = OpLoad %v4float %x
+OpReturnValue %67
 OpFunctionEnd
 
 1 error