unreachable_for.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. void main() {
  32. for (int i = 0; i < 1; i++) {
  33. break;
  34. }
  35. }
  36. */
  37. TEST_F(PassClassTest, UnreachableNestedIfs) {
  38. const std::string text = R"(
  39. OpCapability Shader
  40. %1 = OpExtInstImport "GLSL.std.450"
  41. OpMemoryModel Logical GLSL450
  42. OpEntryPoint Fragment %4 "main"
  43. OpExecutionMode %4 OriginUpperLeft
  44. OpSource GLSL 440
  45. OpName %4 "main"
  46. OpName %8 "i"
  47. %2 = OpTypeVoid
  48. %3 = OpTypeFunction %2
  49. %6 = OpTypeInt 32 1
  50. %7 = OpTypePointer Function %6
  51. %9 = OpConstant %6 0
  52. %16 = OpConstant %6 1
  53. %17 = OpTypeBool
  54. %4 = OpFunction %2 None %3
  55. %5 = OpLabel
  56. %8 = OpVariable %7 Function
  57. OpStore %8 %9
  58. OpBranch %10
  59. %10 = OpLabel
  60. OpLoopMerge %12 %13 None
  61. OpBranch %14
  62. %14 = OpLabel
  63. %15 = OpLoad %6 %8
  64. %18 = OpSLessThan %17 %15 %16
  65. OpBranchConditional %18 %11 %12
  66. %11 = OpLabel
  67. OpBranch %12
  68. %13 = OpLabel
  69. %20 = OpLoad %6 %8
  70. %21 = OpIAdd %6 %20 %16
  71. OpStore %8 %21
  72. OpBranch %10
  73. %12 = OpLabel
  74. OpReturn
  75. OpFunctionEnd
  76. )";
  77. // clang-format on
  78. std::unique_ptr<IRContext> context =
  79. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  80. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  81. Module* module = context->module();
  82. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  83. << text << std::endl;
  84. const Function* f = spvtest::GetFunction(module, 4);
  85. DominatorAnalysis* analysis = context->GetDominatorAnalysis(f);
  86. EXPECT_TRUE(analysis->Dominates(5, 5));
  87. EXPECT_TRUE(analysis->Dominates(5, 10));
  88. EXPECT_TRUE(analysis->Dominates(5, 14));
  89. EXPECT_TRUE(analysis->Dominates(5, 11));
  90. EXPECT_TRUE(analysis->Dominates(5, 12));
  91. EXPECT_TRUE(analysis->Dominates(10, 10));
  92. EXPECT_TRUE(analysis->Dominates(10, 14));
  93. EXPECT_TRUE(analysis->Dominates(10, 11));
  94. EXPECT_TRUE(analysis->Dominates(10, 12));
  95. EXPECT_TRUE(analysis->Dominates(14, 14));
  96. EXPECT_TRUE(analysis->Dominates(14, 11));
  97. EXPECT_TRUE(analysis->Dominates(14, 12));
  98. EXPECT_TRUE(analysis->Dominates(11, 11));
  99. EXPECT_TRUE(analysis->Dominates(12, 12));
  100. EXPECT_TRUE(analysis->StrictlyDominates(5, 10));
  101. EXPECT_TRUE(analysis->StrictlyDominates(5, 14));
  102. EXPECT_TRUE(analysis->StrictlyDominates(5, 11));
  103. EXPECT_TRUE(analysis->StrictlyDominates(5, 12));
  104. EXPECT_TRUE(analysis->StrictlyDominates(10, 14));
  105. EXPECT_TRUE(analysis->StrictlyDominates(10, 11));
  106. EXPECT_TRUE(analysis->StrictlyDominates(10, 12));
  107. EXPECT_TRUE(analysis->StrictlyDominates(14, 11));
  108. EXPECT_TRUE(analysis->StrictlyDominates(14, 12));
  109. }
  110. } // namespace
  111. } // namespace opt
  112. } // namespace spvtools