switch_case_fallthrough.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <string>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "source/opt/dominator_analysis.h"
  19. #include "source/opt/pass.h"
  20. #include "test/opt/assembly_builder.h"
  21. #include "test/opt/function_utils.h"
  22. #include "test/opt/pass_fixture.h"
  23. #include "test/opt/pass_utils.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace {
  27. using ::testing::UnorderedElementsAre;
  28. using PassClassTest = PassTest<::testing::Test>;
  29. /*
  30. Generated from the following GLSL
  31. #version 440 core
  32. layout(location = 0) out vec4 v;
  33. layout(location = 1) in vec4 in_val;
  34. void main() {
  35. int i;
  36. switch (int(in_val.x)) {
  37. case 0:
  38. i = 0;
  39. case 1:
  40. i = 1;
  41. break;
  42. case 2:
  43. i = 2;
  44. case 3:
  45. i = 3;
  46. case 4:
  47. i = 4;
  48. break;
  49. default:
  50. i = 0;
  51. }
  52. v = vec4(i, i, i, i);
  53. }
  54. */
  55. TEST_F(PassClassTest, UnreachableNestedIfs) {
  56. const std::string text = R"(
  57. OpCapability Shader
  58. %1 = OpExtInstImport "GLSL.std.450"
  59. OpMemoryModel Logical GLSL450
  60. OpEntryPoint Fragment %4 "main" %9 %35
  61. OpExecutionMode %4 OriginUpperLeft
  62. OpSource GLSL 440
  63. OpName %4 "main"
  64. OpName %9 "in_val"
  65. OpName %25 "i"
  66. OpName %35 "v"
  67. OpDecorate %9 Location 1
  68. OpDecorate %35 Location 0
  69. %2 = OpTypeVoid
  70. %3 = OpTypeFunction %2
  71. %6 = OpTypeFloat 32
  72. %7 = OpTypeVector %6 4
  73. %8 = OpTypePointer Input %7
  74. %9 = OpVariable %8 Input
  75. %10 = OpTypeInt 32 0
  76. %11 = OpConstant %10 0
  77. %12 = OpTypePointer Input %6
  78. %15 = OpTypeInt 32 1
  79. %24 = OpTypePointer Function %15
  80. %26 = OpConstant %15 0
  81. %27 = OpConstant %15 1
  82. %29 = OpConstant %15 2
  83. %30 = OpConstant %15 3
  84. %31 = OpConstant %15 4
  85. %34 = OpTypePointer Output %7
  86. %35 = OpVariable %34 Output
  87. %4 = OpFunction %2 None %3
  88. %5 = OpLabel
  89. %25 = OpVariable %24 Function
  90. %13 = OpAccessChain %12 %9 %11
  91. %14 = OpLoad %6 %13
  92. %16 = OpConvertFToS %15 %14
  93. OpSelectionMerge %23 None
  94. OpSwitch %16 %22 0 %17 1 %18 2 %19 3 %20 4 %21
  95. %22 = OpLabel
  96. OpStore %25 %26
  97. OpBranch %23
  98. %17 = OpLabel
  99. OpStore %25 %26
  100. OpBranch %18
  101. %18 = OpLabel
  102. OpStore %25 %27
  103. OpBranch %23
  104. %19 = OpLabel
  105. OpStore %25 %29
  106. OpBranch %20
  107. %20 = OpLabel
  108. OpStore %25 %30
  109. OpBranch %21
  110. %21 = OpLabel
  111. OpStore %25 %31
  112. OpBranch %23
  113. %23 = OpLabel
  114. %36 = OpLoad %15 %25
  115. %37 = OpConvertSToF %6 %36
  116. %38 = OpLoad %15 %25
  117. %39 = OpConvertSToF %6 %38
  118. %40 = OpLoad %15 %25
  119. %41 = OpConvertSToF %6 %40
  120. %42 = OpLoad %15 %25
  121. %43 = OpConvertSToF %6 %42
  122. %44 = OpCompositeConstruct %7 %37 %39 %41 %43
  123. OpStore %35 %44
  124. OpReturn
  125. OpFunctionEnd
  126. )";
  127. // clang-format on
  128. std::unique_ptr<IRContext> context =
  129. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  130. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  131. Module* module = context->module();
  132. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  133. << text << std::endl;
  134. const Function* f = spvtest::GetFunction(module, 4);
  135. DominatorAnalysis* analysis = context->GetDominatorAnalysis(f);
  136. EXPECT_TRUE(analysis->Dominates(5, 5));
  137. EXPECT_TRUE(analysis->Dominates(5, 17));
  138. EXPECT_TRUE(analysis->Dominates(5, 18));
  139. EXPECT_TRUE(analysis->Dominates(5, 19));
  140. EXPECT_TRUE(analysis->Dominates(5, 20));
  141. EXPECT_TRUE(analysis->Dominates(5, 21));
  142. EXPECT_TRUE(analysis->Dominates(5, 22));
  143. EXPECT_TRUE(analysis->Dominates(5, 23));
  144. EXPECT_TRUE(analysis->StrictlyDominates(5, 17));
  145. EXPECT_TRUE(analysis->StrictlyDominates(5, 18));
  146. EXPECT_TRUE(analysis->StrictlyDominates(5, 19));
  147. EXPECT_TRUE(analysis->StrictlyDominates(5, 20));
  148. EXPECT_TRUE(analysis->StrictlyDominates(5, 21));
  149. EXPECT_TRUE(analysis->StrictlyDominates(5, 22));
  150. EXPECT_TRUE(analysis->StrictlyDominates(5, 23));
  151. }
  152. } // namespace
  153. } // namespace opt
  154. } // namespace spvtools