operand_to_dominating_id_test.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright (c) 2018 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 "source/reduce/operand_to_dominating_id_reduction_opportunity_finder.h"
  15. #include "source/opt/build_module.h"
  16. #include "source/reduce/reduction_opportunity.h"
  17. #include "test/reduce/reduce_test_util.h"
  18. namespace spvtools {
  19. namespace reduce {
  20. namespace {
  21. TEST(OperandToDominatingIdReductionPassTest, BasicCheck) {
  22. std::string original = R"(
  23. OpCapability Shader
  24. %1 = OpExtInstImport "GLSL.std.450"
  25. OpMemoryModel Logical GLSL450
  26. OpEntryPoint Fragment %4 "main"
  27. OpExecutionMode %4 OriginUpperLeft
  28. OpSource ESSL 310
  29. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeInt 32 1
  32. %7 = OpTypePointer Function %6
  33. %9 = OpConstant %6 2
  34. %4 = OpFunction %2 None %3
  35. %5 = OpLabel
  36. %8 = OpVariable %7 Function
  37. %10 = OpVariable %7 Function
  38. %14 = OpVariable %7 Function
  39. OpStore %8 %9
  40. %11 = OpLoad %6 %8
  41. %12 = OpLoad %6 %8
  42. %13 = OpIAdd %6 %11 %12
  43. OpStore %10 %13
  44. %15 = OpLoad %6 %10
  45. OpStore %14 %15
  46. OpReturn
  47. OpFunctionEnd
  48. )";
  49. const auto env = SPV_ENV_UNIVERSAL_1_3;
  50. const auto consumer = nullptr;
  51. const auto context =
  52. BuildModule(env, consumer, original, kReduceAssembleOption);
  53. const auto ops = OperandToDominatingIdReductionOpportunityFinder()
  54. .GetAvailableOpportunities(context.get(), 0);
  55. ASSERT_EQ(10, ops.size());
  56. ASSERT_TRUE(ops[0]->PreconditionHolds());
  57. ops[0]->TryToApply();
  58. std::string after_op_0 = R"(
  59. OpCapability Shader
  60. %1 = OpExtInstImport "GLSL.std.450"
  61. OpMemoryModel Logical GLSL450
  62. OpEntryPoint Fragment %4 "main"
  63. OpExecutionMode %4 OriginUpperLeft
  64. OpSource ESSL 310
  65. %2 = OpTypeVoid
  66. %3 = OpTypeFunction %2
  67. %6 = OpTypeInt 32 1
  68. %7 = OpTypePointer Function %6
  69. %9 = OpConstant %6 2
  70. %4 = OpFunction %2 None %3
  71. %5 = OpLabel
  72. %8 = OpVariable %7 Function
  73. %10 = OpVariable %7 Function
  74. %14 = OpVariable %7 Function
  75. OpStore %8 %9
  76. %11 = OpLoad %6 %8
  77. %12 = OpLoad %6 %8
  78. %13 = OpIAdd %6 %11 %12
  79. OpStore %8 %13 ; %10 -> %8
  80. %15 = OpLoad %6 %10
  81. OpStore %14 %15
  82. OpReturn
  83. OpFunctionEnd
  84. )";
  85. CheckEqual(env, after_op_0, context.get());
  86. ASSERT_TRUE(ops[1]->PreconditionHolds());
  87. ops[1]->TryToApply();
  88. std::string after_op_1 = R"(
  89. OpCapability Shader
  90. %1 = OpExtInstImport "GLSL.std.450"
  91. OpMemoryModel Logical GLSL450
  92. OpEntryPoint Fragment %4 "main"
  93. OpExecutionMode %4 OriginUpperLeft
  94. OpSource ESSL 310
  95. %2 = OpTypeVoid
  96. %3 = OpTypeFunction %2
  97. %6 = OpTypeInt 32 1
  98. %7 = OpTypePointer Function %6
  99. %9 = OpConstant %6 2
  100. %4 = OpFunction %2 None %3
  101. %5 = OpLabel
  102. %8 = OpVariable %7 Function
  103. %10 = OpVariable %7 Function
  104. %14 = OpVariable %7 Function
  105. OpStore %8 %9
  106. %11 = OpLoad %6 %8
  107. %12 = OpLoad %6 %8
  108. %13 = OpIAdd %6 %11 %12
  109. OpStore %8 %13 ; %10 -> %8
  110. %15 = OpLoad %6 %8 ; %10 -> %8
  111. OpStore %14 %15
  112. OpReturn
  113. OpFunctionEnd
  114. )";
  115. CheckEqual(env, after_op_1, context.get());
  116. ASSERT_TRUE(ops[2]->PreconditionHolds());
  117. ops[2]->TryToApply();
  118. std::string after_op_2 = R"(
  119. OpCapability Shader
  120. %1 = OpExtInstImport "GLSL.std.450"
  121. OpMemoryModel Logical GLSL450
  122. OpEntryPoint Fragment %4 "main"
  123. OpExecutionMode %4 OriginUpperLeft
  124. OpSource ESSL 310
  125. %2 = OpTypeVoid
  126. %3 = OpTypeFunction %2
  127. %6 = OpTypeInt 32 1
  128. %7 = OpTypePointer Function %6
  129. %9 = OpConstant %6 2
  130. %4 = OpFunction %2 None %3
  131. %5 = OpLabel
  132. %8 = OpVariable %7 Function
  133. %10 = OpVariable %7 Function
  134. %14 = OpVariable %7 Function
  135. OpStore %8 %9
  136. %11 = OpLoad %6 %8
  137. %12 = OpLoad %6 %8
  138. %13 = OpIAdd %6 %11 %12
  139. OpStore %8 %13 ; %10 -> %8
  140. %15 = OpLoad %6 %8 ; %10 -> %8
  141. OpStore %8 %15 ; %14 -> %8
  142. OpReturn
  143. OpFunctionEnd
  144. )";
  145. CheckEqual(env, after_op_2, context.get());
  146. // The precondition has been disabled by an earlier opportunity's application.
  147. ASSERT_FALSE(ops[3]->PreconditionHolds());
  148. ASSERT_TRUE(ops[4]->PreconditionHolds());
  149. ops[4]->TryToApply();
  150. std::string after_op_4 = R"(
  151. OpCapability Shader
  152. %1 = OpExtInstImport "GLSL.std.450"
  153. OpMemoryModel Logical GLSL450
  154. OpEntryPoint Fragment %4 "main"
  155. OpExecutionMode %4 OriginUpperLeft
  156. OpSource ESSL 310
  157. %2 = OpTypeVoid
  158. %3 = OpTypeFunction %2
  159. %6 = OpTypeInt 32 1
  160. %7 = OpTypePointer Function %6
  161. %9 = OpConstant %6 2
  162. %4 = OpFunction %2 None %3
  163. %5 = OpLabel
  164. %8 = OpVariable %7 Function
  165. %10 = OpVariable %7 Function
  166. %14 = OpVariable %7 Function
  167. OpStore %8 %9
  168. %11 = OpLoad %6 %8
  169. %12 = OpLoad %6 %8
  170. %13 = OpIAdd %6 %11 %11 ; %12 -> %11
  171. OpStore %8 %13 ; %10 -> %8
  172. %15 = OpLoad %6 %8 ; %10 -> %8
  173. OpStore %8 %15 ; %14 -> %8
  174. OpReturn
  175. OpFunctionEnd
  176. )";
  177. CheckEqual(env, after_op_4, context.get());
  178. }
  179. } // namespace
  180. } // namespace reduce
  181. } // namespace spvtools