simple.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 c;
  32. layout(location = 1)in vec4 in_val;
  33. void main(){
  34. if ( in_val.x < 10) {
  35. int z = 0;
  36. int i = 0;
  37. for (i = 0; i < in_val.y; ++i) {
  38. z += i;
  39. }
  40. c = vec4(i,i,i,i);
  41. } else {
  42. c = vec4(1,1,1,1);
  43. }
  44. }
  45. */
  46. TEST_F(PassClassTest, BasicVisitFromEntryPoint) {
  47. const std::string text = R"(
  48. OpCapability Shader
  49. %1 = OpExtInstImport "GLSL.std.450"
  50. OpMemoryModel Logical GLSL450
  51. OpEntryPoint Fragment %4 "main" %9 %43
  52. OpExecutionMode %4 OriginUpperLeft
  53. OpSource GLSL 440
  54. OpName %4 "main"
  55. OpName %9 "in_val"
  56. OpName %22 "z"
  57. OpName %24 "i"
  58. OpName %43 "c"
  59. OpDecorate %9 Location 1
  60. OpDecorate %43 Location 0
  61. %2 = OpTypeVoid
  62. %3 = OpTypeFunction %2
  63. %6 = OpTypeFloat 32
  64. %7 = OpTypeVector %6 4
  65. %8 = OpTypePointer Input %7
  66. %9 = OpVariable %8 Input
  67. %10 = OpTypeInt 32 0
  68. %11 = OpConstant %10 0
  69. %12 = OpTypePointer Input %6
  70. %15 = OpConstant %6 10
  71. %16 = OpTypeBool
  72. %20 = OpTypeInt 32 1
  73. %21 = OpTypePointer Function %20
  74. %23 = OpConstant %20 0
  75. %32 = OpConstant %10 1
  76. %40 = OpConstant %20 1
  77. %42 = OpTypePointer Output %7
  78. %43 = OpVariable %42 Output
  79. %54 = OpConstant %6 1
  80. %55 = OpConstantComposite %7 %54 %54 %54 %54
  81. %4 = OpFunction %2 None %3
  82. %5 = OpLabel
  83. %22 = OpVariable %21 Function
  84. %24 = OpVariable %21 Function
  85. %13 = OpAccessChain %12 %9 %11
  86. %14 = OpLoad %6 %13
  87. %17 = OpFOrdLessThan %16 %14 %15
  88. OpSelectionMerge %19 None
  89. OpBranchConditional %17 %18 %53
  90. %18 = OpLabel
  91. OpStore %22 %23
  92. OpStore %24 %23
  93. OpStore %24 %23
  94. OpBranch %25
  95. %25 = OpLabel
  96. OpLoopMerge %27 %28 None
  97. OpBranch %29
  98. %29 = OpLabel
  99. %30 = OpLoad %20 %24
  100. %31 = OpConvertSToF %6 %30
  101. %33 = OpAccessChain %12 %9 %32
  102. %34 = OpLoad %6 %33
  103. %35 = OpFOrdLessThan %16 %31 %34
  104. OpBranchConditional %35 %26 %27
  105. %26 = OpLabel
  106. %36 = OpLoad %20 %24
  107. %37 = OpLoad %20 %22
  108. %38 = OpIAdd %20 %37 %36
  109. OpStore %22 %38
  110. OpBranch %28
  111. %28 = OpLabel
  112. %39 = OpLoad %20 %24
  113. %41 = OpIAdd %20 %39 %40
  114. OpStore %24 %41
  115. OpBranch %25
  116. %27 = OpLabel
  117. %44 = OpLoad %20 %24
  118. %45 = OpConvertSToF %6 %44
  119. %46 = OpLoad %20 %24
  120. %47 = OpConvertSToF %6 %46
  121. %48 = OpLoad %20 %24
  122. %49 = OpConvertSToF %6 %48
  123. %50 = OpLoad %20 %24
  124. %51 = OpConvertSToF %6 %50
  125. %52 = OpCompositeConstruct %7 %45 %47 %49 %51
  126. OpStore %43 %52
  127. OpBranch %19
  128. %53 = OpLabel
  129. OpStore %43 %55
  130. OpBranch %19
  131. %19 = OpLabel
  132. OpReturn
  133. OpFunctionEnd
  134. )";
  135. // clang-format on
  136. std::unique_ptr<IRContext> context =
  137. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  138. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  139. Module* module = context->module();
  140. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  141. << text << std::endl;
  142. const Function* f = spvtest::GetFunction(module, 4);
  143. DominatorAnalysis* analysis = context->GetDominatorAnalysis(f);
  144. const CFG& cfg = *context->cfg();
  145. DominatorTree& tree = analysis->GetDomTree();
  146. EXPECT_EQ(tree.GetRoot()->bb_, cfg.pseudo_entry_block());
  147. EXPECT_TRUE(analysis->Dominates(5, 18));
  148. EXPECT_TRUE(analysis->Dominates(5, 53));
  149. EXPECT_TRUE(analysis->Dominates(5, 19));
  150. EXPECT_TRUE(analysis->Dominates(5, 25));
  151. EXPECT_TRUE(analysis->Dominates(5, 29));
  152. EXPECT_TRUE(analysis->Dominates(5, 27));
  153. EXPECT_TRUE(analysis->Dominates(5, 26));
  154. EXPECT_TRUE(analysis->Dominates(5, 28));
  155. EXPECT_TRUE(analysis->StrictlyDominates(5, 18));
  156. EXPECT_TRUE(analysis->StrictlyDominates(5, 53));
  157. EXPECT_TRUE(analysis->StrictlyDominates(5, 19));
  158. EXPECT_TRUE(analysis->StrictlyDominates(5, 25));
  159. EXPECT_TRUE(analysis->StrictlyDominates(5, 29));
  160. EXPECT_TRUE(analysis->StrictlyDominates(5, 27));
  161. EXPECT_TRUE(analysis->StrictlyDominates(5, 26));
  162. EXPECT_TRUE(analysis->StrictlyDominates(5, 28));
  163. }
  164. } // namespace
  165. } // namespace opt
  166. } // namespace spvtools