nested_ifs_post.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 330 core
  31. layout(location = 0) out vec4 v;
  32. void main(){
  33. if (true) {
  34. if (true) {
  35. v = vec4(1,1,1,1);
  36. } else {
  37. v = vec4(2,2,2,2);
  38. }
  39. } else {
  40. if (true) {
  41. v = vec4(3,3,3,3);
  42. } else {
  43. v = vec4(4,4,4,4);
  44. }
  45. }
  46. }
  47. */
  48. TEST_F(PassClassTest, UnreachableNestedIfs) {
  49. const std::string text = R"(
  50. OpCapability Shader
  51. %1 = OpExtInstImport "GLSL.std.450"
  52. OpMemoryModel Logical GLSL450
  53. OpEntryPoint Fragment %4 "main" %15
  54. OpExecutionMode %4 OriginUpperLeft
  55. OpSource GLSL 330
  56. OpName %4 "main"
  57. OpName %15 "v"
  58. OpDecorate %15 Location 0
  59. %2 = OpTypeVoid
  60. %3 = OpTypeFunction %2
  61. %6 = OpTypeBool
  62. %7 = OpConstantTrue %6
  63. %12 = OpTypeFloat 32
  64. %13 = OpTypeVector %12 4
  65. %14 = OpTypePointer Output %13
  66. %15 = OpVariable %14 Output
  67. %16 = OpConstant %12 1
  68. %17 = OpConstantComposite %13 %16 %16 %16 %16
  69. %19 = OpConstant %12 2
  70. %20 = OpConstantComposite %13 %19 %19 %19 %19
  71. %24 = OpConstant %12 3
  72. %25 = OpConstantComposite %13 %24 %24 %24 %24
  73. %27 = OpConstant %12 4
  74. %28 = OpConstantComposite %13 %27 %27 %27 %27
  75. %4 = OpFunction %2 None %3
  76. %5 = OpLabel
  77. OpSelectionMerge %9 None
  78. OpBranchConditional %7 %8 %21
  79. %8 = OpLabel
  80. OpSelectionMerge %11 None
  81. OpBranchConditional %7 %10 %18
  82. %10 = OpLabel
  83. OpStore %15 %17
  84. OpBranch %11
  85. %18 = OpLabel
  86. OpStore %15 %20
  87. OpBranch %11
  88. %11 = OpLabel
  89. OpBranch %9
  90. %21 = OpLabel
  91. OpSelectionMerge %23 None
  92. OpBranchConditional %7 %22 %26
  93. %22 = OpLabel
  94. OpStore %15 %25
  95. OpBranch %23
  96. %26 = OpLabel
  97. OpStore %15 %28
  98. OpBranch %23
  99. %23 = OpLabel
  100. OpBranch %9
  101. %9 = OpLabel
  102. OpReturn
  103. OpFunctionEnd
  104. )";
  105. // clang-format on
  106. std::unique_ptr<IRContext> context =
  107. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  108. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  109. Module* module = context->module();
  110. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  111. << text << std::endl;
  112. const Function* f = spvtest::GetFunction(module, 4);
  113. PostDominatorAnalysis* analysis = context->GetPostDominatorAnalysis(f);
  114. EXPECT_TRUE(analysis->Dominates(5, 5));
  115. EXPECT_TRUE(analysis->Dominates(8, 8));
  116. EXPECT_TRUE(analysis->Dominates(9, 9));
  117. EXPECT_TRUE(analysis->Dominates(10, 10));
  118. EXPECT_TRUE(analysis->Dominates(11, 11));
  119. EXPECT_TRUE(analysis->Dominates(18, 18));
  120. EXPECT_TRUE(analysis->Dominates(21, 21));
  121. EXPECT_TRUE(analysis->Dominates(22, 22));
  122. EXPECT_TRUE(analysis->Dominates(23, 23));
  123. EXPECT_TRUE(analysis->Dominates(26, 26));
  124. EXPECT_TRUE(analysis->Dominates(9, 5));
  125. EXPECT_TRUE(analysis->Dominates(9, 11));
  126. EXPECT_TRUE(analysis->Dominates(9, 23));
  127. EXPECT_TRUE(analysis->Dominates(11, 10));
  128. EXPECT_TRUE(analysis->Dominates(11, 18));
  129. EXPECT_TRUE(analysis->Dominates(11, 8));
  130. EXPECT_TRUE(analysis->Dominates(23, 22));
  131. EXPECT_TRUE(analysis->Dominates(23, 26));
  132. EXPECT_TRUE(analysis->Dominates(23, 21));
  133. EXPECT_TRUE(analysis->StrictlyDominates(9, 5));
  134. EXPECT_TRUE(analysis->StrictlyDominates(9, 11));
  135. EXPECT_TRUE(analysis->StrictlyDominates(9, 23));
  136. EXPECT_TRUE(analysis->StrictlyDominates(11, 10));
  137. EXPECT_TRUE(analysis->StrictlyDominates(11, 18));
  138. EXPECT_TRUE(analysis->StrictlyDominates(11, 8));
  139. EXPECT_TRUE(analysis->StrictlyDominates(23, 22));
  140. EXPECT_TRUE(analysis->StrictlyDominates(23, 26));
  141. EXPECT_TRUE(analysis->StrictlyDominates(23, 21));
  142. }
  143. } // namespace
  144. } // namespace opt
  145. } // namespace spvtools