OSDN Git Service

SpirvShader: Implement OpSwitch
authorBen Clayton <bclayton@google.com>
Thu, 21 Mar 2019 18:57:23 +0000 (18:57 +0000)
committerBen Clayton <bclayton@google.com>
Tue, 26 Mar 2019 12:46:26 +0000 (12:46 +0000)
Tests: dEQP-VK.spirv_assembly.instruction.compute.*
Tests: dEQP-VK.spirv_assembly.instruction.graphics.*
Bug: b/128527271
Change-Id: I7ba31ca504a582a4d36d25ef2747fb1c1607bade
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/27775
Presubmit-Ready: Ben Clayton <bclayton@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>

src/Pipeline/SpirvShader.cpp
src/Pipeline/SpirvShader.hpp
tests/VulkanUnitTests/unittests.cpp

index c226f13..cc81c9f 100644 (file)
@@ -1179,6 +1179,8 @@ namespace sw
                        case Block::Simple:
                        case Block::StructuredBranchConditional:
                        case Block::UnstructuredBranchConditional:
+                       case Block::StructuredSwitch:
+                       case Block::UnstructuredSwitch:
                                if (id != mainBlockId)
                                {
                                        // Emit all preceeding blocks and set the activeLaneMask.
@@ -1404,6 +1406,9 @@ namespace sw
                case spv::OpBranchConditional:
                        return EmitBranchConditional(insn, state);
 
+               case spv::OpSwitch:
+                       return EmitSwitch(insn, state);
+
                case spv::OpUnreachable:
                        return EmitUnreachable(insn, state);
 
@@ -2638,6 +2643,39 @@ namespace sw
                return EmitResult::Terminator;
        }
 
+       SpirvShader::EmitResult SpirvShader::EmitSwitch(InsnIterator insn, EmitState *state) const
+       {
+               auto block = getBlock(state->currentBlock);
+               ASSERT(block.branchInstruction == insn);
+
+               auto selId = Object::ID(block.branchInstruction.word(1));
+
+               auto sel = GenericValue(this, state->routine, selId);
+               ASSERT_MSG(getType(getObject(selId).type).sizeInComponents == 1, "Selector must be a scalar");
+
+               auto numCases = (block.branchInstruction.wordCount() - 3) / 2;
+
+               // TODO: Optimize for case where all lanes take same path.
+
+               SIMD::Int defaultLaneMask = state->activeLaneMask();
+
+               // Gather up the case label matches and calculate defaultLaneMask.
+               std::vector<RValue<SIMD::Int>> caseLabelMatches;
+               caseLabelMatches.reserve(numCases);
+               for (uint32_t i = 0; i < numCases; i++)
+               {
+                       auto label = block.branchInstruction.word(i * 2 + 3);
+                       auto caseBlockId = Block::ID(block.branchInstruction.word(i * 2 + 4));
+                       auto caseLabelMatch = CmpEQ(sel.Int(0), SIMD::Int(label));
+                       state->addOutputActiveLaneMaskEdge(caseBlockId, caseLabelMatch);
+                       defaultLaneMask &= ~caseLabelMatch;
+               }
+
+               auto defaultBlockId = Block::ID(block.branchInstruction.word(2));
+               state->addOutputActiveLaneMaskEdge(defaultBlockId, defaultLaneMask);
+
+               return EmitResult::Terminator;
+       }
 
        SpirvShader::EmitResult SpirvShader::EmitUnreachable(InsnIterator insn, EmitState *state) const
        {
index 8268c71..6d72745 100644 (file)
@@ -602,6 +602,7 @@ namespace sw
                EmitResult EmitAll(InsnIterator insn, EmitState *state) const;
                EmitResult EmitBranch(InsnIterator insn, EmitState *state) const;
                EmitResult EmitBranchConditional(InsnIterator insn, EmitState *state) const;
+               EmitResult EmitSwitch(InsnIterator insn, EmitState *state) const;
                EmitResult EmitUnreachable(InsnIterator insn, EmitState *state) const;
                EmitResult EmitReturn(InsnIterator insn, EmitState *state) const;
                EmitResult EmitPhi(InsnIterator insn, EmitState *state) const;
index 2dca716..d2bfcc5 100644 (file)
@@ -917,3 +917,455 @@ TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, BranchConditionalPhi)
     test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return (i % 2) == 0 ? 1 : 2; });\r
 }\r
 \r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchEmptyCases)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 2\n"               // int32(2)\r
+        "%15 = OpConstant %10 0\n"              // uint32(0)\r
+        "%16 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%17 = OpTypePointer Input %16\n"       // vec4<int32>*\r
+         "%2 = OpVariable %17 Input\n"          // gl_GlobalInvocationId\r
+        "%18 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%19 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%20 = OpLabel\n"\r
+        "%21 = OpAccessChain %18 %2 %15\n"      // &gl_GlobalInvocationId.x\r
+        "%22 = OpLoad %10 %21\n"                // gl_GlobalInvocationId.x\r
+        "%23 = OpAccessChain %19 %6 %13 %22\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%24 = OpLoad %9 %23\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpAccessChain %19 %5 %13 %22\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %24 = in value\r
+        "%26 = OpSMod %9 %24 %14\n"             // in % 2\r
+              "OpSelectionMerge %27 None\n"\r
+              "OpSwitch %26 %27 0 %28 1 %29\n"\r
+        "%28 = OpLabel\n"                       // (in % 2) == 0\r
+              "OpBranch %27\n"\r
+        "%29 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpBranch %27\n"\r
+        "%27 = OpLabel\n"\r
+    // %26 = out value\r
+    // End of branch logic\r
+              "OpStore %25 %26\n"               // use SSA value from previous block\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return i%2; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchStore)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %28 0 %29 1 %30\n"\r
+        "%29 = OpLabel\n"                       // (in % 2) == 0\r
+              "OpStore %26 %15\n"               // write 2\r
+              "OpBranch %28\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpStore %26 %14\n"               // write 1\r
+              "OpBranch %28\n"\r
+        "%28 = OpLabel\n"\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return (i % 2) == 0 ? 2 : 1; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchCaseReturn)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %28 0 %29 1 %30\n"\r
+        "%29 = OpLabel\n"                       // (in % 2) == 0\r
+              "OpBranch %28\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpReturn\n"\r
+        "%28 = OpLabel\n"\r
+              "OpStore %26 %14\n"               // write 1\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return (i % 2) == 1 ? 0 : 1; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchDefaultReturn)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %29 1 %30\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpBranch %28\n"\r
+        "%29 = OpLabel\n"                       // (in % 2) != 1\r
+              "OpReturn\n"\r
+        "%28 = OpLabel\n"                       // merge\r
+              "OpStore %26 %14\n"               // write 1\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return (i % 2) == 1 ? 1 : 0; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchCaseFallthrough)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %29 0 %30 1 %31\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 0\r
+        "%32 = OpIAdd %9 %27 %14\n"             // generate an intermediate\r
+              "OpStore %26 %32\n"               // write a value (overwritten later)\r
+              "OpBranch %31\n"                  // fallthrough\r
+        "%31 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpStore %26 %15\n"               // write 2\r
+              "OpBranch %28\n"\r
+        "%29 = OpLabel\n"                       // unreachable\r
+              "OpUnreachable\n"\r
+        "%28 = OpLabel\n"                       // merge\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return 2; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchDefaultFallthrough)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %29 0 %30 1 %31\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 0\r
+        "%32 = OpIAdd %9 %27 %14\n"             // generate an intermediate\r
+              "OpStore %26 %32\n"               // write a value (overwritten later)\r
+              "OpBranch %29\n"                  // fallthrough\r
+        "%29 = OpLabel\n"                       // default\r
+        "%33 = OpIAdd %9 %27 %14\n"             // generate an intermediate\r
+              "OpStore %26 %33\n"               // write a value (overwritten later)\r
+              "OpBranch %31\n"                  // fallthrough\r
+        "%31 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpStore %26 %15\n"               // write 2\r
+              "OpBranch %28\n"\r
+        "%28 = OpLabel\n"                       // merge\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return 2; });\r
+}\r
+\r
+TEST_P(SwiftShaderVulkanBufferToBufferComputeTest, SwitchPhi)\r
+{\r
+    std::stringstream src;\r
+    src <<\r
+              "OpCapability Shader\n"\r
+              "OpMemoryModel Logical GLSL450\n"\r
+              "OpEntryPoint GLCompute %1 \"main\" %2\n"\r
+              "OpExecutionMode %1 LocalSize " <<\r
+                GetParam().localSizeX << " " <<\r
+                GetParam().localSizeY << " " <<\r
+                GetParam().localSizeZ << "\n" <<\r
+              "OpDecorate %3 ArrayStride 4\n"\r
+              "OpMemberDecorate %4 0 Offset 0\n"\r
+              "OpDecorate %4 BufferBlock\n"\r
+              "OpDecorate %5 DescriptorSet 0\n"\r
+              "OpDecorate %5 Binding 1\n"\r
+              "OpDecorate %2 BuiltIn GlobalInvocationId\n"\r
+              "OpDecorate %6 DescriptorSet 0\n"\r
+              "OpDecorate %6 Binding 0\n"\r
+         "%7 = OpTypeVoid\n"\r
+         "%8 = OpTypeFunction %7\n"             // void()\r
+         "%9 = OpTypeInt 32 1\n"                // int32\r
+        "%10 = OpTypeInt 32 0\n"                // uint32\r
+        "%11 = OpTypeBool\n"\r
+         "%3 = OpTypeRuntimeArray %9\n"         // int32[]\r
+         "%4 = OpTypeStruct %3\n"               // struct{ int32[] }\r
+        "%12 = OpTypePointer Uniform %4\n"      // struct{ int32[] }*\r
+         "%5 = OpVariable %12 Uniform\n"        // struct{ int32[] }* in\r
+        "%13 = OpConstant %9 0\n"               // int32(0)\r
+        "%14 = OpConstant %9 1\n"               // int32(1)\r
+        "%15 = OpConstant %9 2\n"               // int32(2)\r
+        "%16 = OpConstant %10 0\n"              // uint32(0)\r
+        "%17 = OpTypeVector %10 3\n"            // vec4<int32>\r
+        "%18 = OpTypePointer Input %17\n"       // vec4<int32>*\r
+         "%2 = OpVariable %18 Input\n"          // gl_GlobalInvocationId\r
+        "%19 = OpTypePointer Input %10\n"       // uint32*\r
+         "%6 = OpVariable %12 Uniform\n"        // struct{ int32[] }* out\r
+        "%20 = OpTypePointer Uniform %9\n"      // int32*\r
+         "%1 = OpFunction %7 None %8\n"         // -- Function begin --\r
+        "%21 = OpLabel\n"\r
+        "%22 = OpAccessChain %19 %2 %16\n"      // &gl_GlobalInvocationId.x\r
+        "%23 = OpLoad %10 %22\n"                // gl_GlobalInvocationId.x\r
+        "%24 = OpAccessChain %20 %6 %13 %23\n"  // &in.arr[gl_GlobalInvocationId.x]\r
+        "%25 = OpLoad %9 %24\n"                 // in.arr[gl_GlobalInvocationId.x]\r
+        "%26 = OpAccessChain %20 %5 %13 %23\n"  // &out.arr[gl_GlobalInvocationId.x]\r
+    // Start of branch logic\r
+    // %25 = in value\r
+        "%27 = OpSMod %9 %25 %15\n"             // in % 2\r
+              "OpSelectionMerge %28 None\n"\r
+              "OpSwitch %27 %29 1 %30\n"\r
+        "%30 = OpLabel\n"                       // (in % 2) == 1\r
+              "OpBranch %28\n"\r
+        "%29 = OpLabel\n"                       // (in % 2) != 1\r
+              "OpBranch %28\n"\r
+        "%28 = OpLabel\n"                       // merge\r
+        "%31 = OpPhi %9 %14 %30 %15 %29\n"      // (in % 2) == 1 ? 1 : 2\r
+              "OpStore %26 %31\n"\r
+    // End of branch logic\r
+              "OpReturn\n"\r
+              "OpFunctionEnd\n";\r
+\r
+    test(src.str(), [](uint32_t i) { return i; }, [](uint32_t i) { return (i % 2) == 1 ? 1 : 2; });\r
+}\r