nested_ifs.cpp 4.7 KB

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