switch_case_fallthrough.cpp 4.5 KB

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