structured_construct_to_block_test.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright (c) 2021 Alastair F. Donaldson
  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/structured_construct_to_block_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(StructuredConstructToBlockReductionPassTest, SimpleTest) {
  22. std::string shader = 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 320
  29. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeInt 32 1
  32. %7 = OpTypePointer Function %6
  33. %9 = OpConstant %6 0
  34. %10 = OpTypeBool
  35. %11 = OpConstantTrue %10
  36. %19 = OpConstant %6 3
  37. %29 = OpConstant %6 1
  38. %31 = OpConstant %6 2
  39. %4 = OpFunction %2 None %3
  40. %5 = OpLabel
  41. %8 = OpVariable %7 Function
  42. OpStore %8 %9
  43. OpSelectionMerge %13 None
  44. OpBranchConditional %11 %12 %13
  45. %12 = OpLabel
  46. OpBranch %13
  47. %13 = OpLabel
  48. OpBranch %14
  49. %14 = OpLabel
  50. OpLoopMerge %16 %17 None
  51. OpBranch %15
  52. %15 = OpLabel
  53. %18 = OpLoad %6 %8
  54. %20 = OpSGreaterThan %10 %18 %19
  55. OpSelectionMerge %22 None
  56. OpBranchConditional %20 %21 %22
  57. %21 = OpLabel
  58. OpBranch %16
  59. %22 = OpLabel
  60. OpBranch %17
  61. %17 = OpLabel
  62. OpBranch %14
  63. %16 = OpLabel
  64. %24 = OpLoad %6 %8
  65. OpSelectionMerge %28 None
  66. OpSwitch %24 %27 1 %25 2 %26
  67. %27 = OpLabel
  68. OpStore %8 %19
  69. OpBranch %28
  70. %25 = OpLabel
  71. OpStore %8 %29
  72. OpBranch %28
  73. %26 = OpLabel
  74. OpStore %8 %31
  75. OpBranch %28
  76. %28 = OpLabel
  77. OpReturn
  78. OpFunctionEnd
  79. )";
  80. const auto env = SPV_ENV_UNIVERSAL_1_3;
  81. const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
  82. const auto ops = StructuredConstructToBlockReductionOpportunityFinder()
  83. .GetAvailableOpportunities(context.get(), 0);
  84. ASSERT_EQ(3, ops.size());
  85. ASSERT_TRUE(ops[0]->PreconditionHolds());
  86. ops[0]->TryToApply();
  87. CheckValid(env, context.get());
  88. ASSERT_TRUE(ops[1]->PreconditionHolds());
  89. ops[1]->TryToApply();
  90. CheckValid(env, context.get());
  91. ASSERT_TRUE(ops[2]->PreconditionHolds());
  92. ops[2]->TryToApply();
  93. CheckValid(env, context.get());
  94. std::string expected = R"(
  95. OpCapability Shader
  96. %1 = OpExtInstImport "GLSL.std.450"
  97. OpMemoryModel Logical GLSL450
  98. OpEntryPoint Fragment %4 "main"
  99. OpExecutionMode %4 OriginUpperLeft
  100. OpSource ESSL 320
  101. %2 = OpTypeVoid
  102. %3 = OpTypeFunction %2
  103. %6 = OpTypeInt 32 1
  104. %7 = OpTypePointer Function %6
  105. %9 = OpConstant %6 0
  106. %10 = OpTypeBool
  107. %11 = OpConstantTrue %10
  108. %19 = OpConstant %6 3
  109. %29 = OpConstant %6 1
  110. %31 = OpConstant %6 2
  111. %4 = OpFunction %2 None %3
  112. %5 = OpLabel
  113. %8 = OpVariable %7 Function
  114. OpStore %8 %9
  115. OpBranch %13
  116. %13 = OpLabel
  117. OpBranch %14
  118. %14 = OpLabel
  119. OpBranch %16
  120. %16 = OpLabel
  121. %24 = OpLoad %6 %8
  122. OpBranch %28
  123. %28 = OpLabel
  124. OpReturn
  125. OpFunctionEnd
  126. )";
  127. CheckEqual(env, expected, context.get());
  128. }
  129. TEST(StructuredConstructToBlockReductionPassTest, CannotBeRemovedDueToUses) {
  130. std::string shader = R"(
  131. OpCapability Shader
  132. %1 = OpExtInstImport "GLSL.std.450"
  133. OpMemoryModel Logical GLSL450
  134. OpEntryPoint Fragment %4 "main"
  135. OpExecutionMode %4 OriginUpperLeft
  136. OpSource ESSL 320
  137. OpName %100 "temp"
  138. %2 = OpTypeVoid
  139. %3 = OpTypeFunction %2
  140. %6 = OpTypeInt 32 1
  141. %7 = OpTypePointer Function %6
  142. %9 = OpConstant %6 0
  143. %10 = OpTypeBool
  144. %11 = OpConstantTrue %10
  145. %19 = OpConstant %6 3
  146. %29 = OpConstant %6 1
  147. %31 = OpConstant %6 2
  148. %4 = OpFunction %2 None %3
  149. %5 = OpLabel
  150. %8 = OpVariable %7 Function
  151. OpStore %8 %9
  152. OpSelectionMerge %13 None
  153. OpBranchConditional %11 %12 %13
  154. %12 = OpLabel
  155. %100 = OpCopyObject %10 %11
  156. OpBranch %13
  157. %13 = OpLabel
  158. OpBranch %14
  159. %14 = OpLabel
  160. OpLoopMerge %16 %17 None
  161. OpBranch %15
  162. %15 = OpLabel
  163. %18 = OpLoad %6 %8
  164. %20 = OpSGreaterThan %10 %18 %19
  165. OpSelectionMerge %22 None
  166. OpBranchConditional %20 %21 %22
  167. %21 = OpLabel
  168. OpBranch %16
  169. %22 = OpLabel
  170. OpBranch %17
  171. %17 = OpLabel
  172. OpBranch %14
  173. %16 = OpLabel
  174. %101 = OpCopyObject %6 %18
  175. %24 = OpLoad %6 %8
  176. OpSelectionMerge %28 None
  177. OpSwitch %24 %27 1 %25 2 %26
  178. %27 = OpLabel
  179. OpStore %8 %19
  180. %102 = OpCopyObject %10 %11
  181. OpBranch %28
  182. %25 = OpLabel
  183. OpStore %8 %29
  184. OpBranch %28
  185. %26 = OpLabel
  186. OpStore %8 %31
  187. OpBranch %28
  188. %28 = OpLabel
  189. %103 = OpPhi %10 %102 %27 %11 %25 %11 %26
  190. OpReturn
  191. OpFunctionEnd
  192. )";
  193. const auto env = SPV_ENV_UNIVERSAL_1_3;
  194. const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
  195. const auto ops = StructuredConstructToBlockReductionOpportunityFinder()
  196. .GetAvailableOpportunities(context.get(), 0);
  197. ASSERT_TRUE(ops.empty());
  198. }
  199. TEST(StructuredConstructToBlockReductionPassTest,
  200. CannotBeRemovedDueToOpPhiAtMerge) {
  201. std::string shader = R"(
  202. OpCapability Shader
  203. %1 = OpExtInstImport "GLSL.std.450"
  204. OpMemoryModel Logical GLSL450
  205. OpEntryPoint Fragment %4 "main"
  206. OpExecutionMode %4 OriginUpperLeft
  207. OpSource ESSL 320
  208. %2 = OpTypeVoid
  209. %3 = OpTypeFunction %2
  210. %10 = OpTypeBool
  211. %11 = OpConstantTrue %10
  212. %4 = OpFunction %2 None %3
  213. %5 = OpLabel
  214. OpSelectionMerge %13 None
  215. OpBranchConditional %11 %12 %13
  216. %12 = OpLabel
  217. OpBranch %13
  218. %13 = OpLabel
  219. %101 = OpPhi %10 %11 %5 %11 %12
  220. OpReturn
  221. OpFunctionEnd
  222. )";
  223. const auto env = SPV_ENV_UNIVERSAL_1_3;
  224. const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
  225. const auto ops = StructuredConstructToBlockReductionOpportunityFinder()
  226. .GetAvailableOpportunities(context.get(), 0);
  227. ASSERT_TRUE(ops.empty());
  228. }
  229. } // namespace
  230. } // namespace reduce
  231. } // namespace spvtools