post.cpp 6.8 KB

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