unreachable_for_post.cpp 3.5 KB

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