remove_unused_struct_member_test.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright (c) 2020 Google LLC
  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/remove_unused_struct_member_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(RemoveUnusedStructMemberTest, RemoveOneMember) {
  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 310
  29. %2 = OpTypeVoid
  30. %3 = OpTypeFunction %2
  31. %6 = OpTypeInt 32 1
  32. %7 = OpTypeStruct %6 %6
  33. %8 = OpTypePointer Function %7
  34. %50 = OpConstant %6 0
  35. %10 = OpConstant %6 1
  36. %11 = OpConstant %6 2
  37. %12 = OpConstantComposite %7 %10 %11
  38. %13 = OpConstant %6 4
  39. %14 = OpTypePointer Function %6
  40. %4 = OpFunction %2 None %3
  41. %5 = OpLabel
  42. %9 = OpVariable %8 Function
  43. OpStore %9 %12
  44. %15 = OpAccessChain %14 %9 %10
  45. %22 = OpInBoundsAccessChain %14 %9 %10
  46. %20 = OpLoad %7 %9
  47. %21 = OpCompositeExtract %6 %20 1
  48. %23 = OpCompositeInsert %7 %10 %20 1
  49. OpStore %15 %13
  50. OpReturn
  51. OpFunctionEnd
  52. )";
  53. const auto env = SPV_ENV_UNIVERSAL_1_3;
  54. const auto consumer = nullptr;
  55. const auto context =
  56. BuildModule(env, consumer, shader, kReduceAssembleOption);
  57. auto ops = RemoveUnusedStructMemberReductionOpportunityFinder()
  58. .GetAvailableOpportunities(context.get(), 0);
  59. ASSERT_EQ(1, ops.size());
  60. ASSERT_TRUE(ops[0]->PreconditionHolds());
  61. ops[0]->TryToApply();
  62. CheckValid(env, context.get());
  63. std::string expected = R"(
  64. OpCapability Shader
  65. %1 = OpExtInstImport "GLSL.std.450"
  66. OpMemoryModel Logical GLSL450
  67. OpEntryPoint Fragment %4 "main"
  68. OpExecutionMode %4 OriginUpperLeft
  69. OpSource ESSL 310
  70. %2 = OpTypeVoid
  71. %3 = OpTypeFunction %2
  72. %6 = OpTypeInt 32 1
  73. %7 = OpTypeStruct %6
  74. %8 = OpTypePointer Function %7
  75. %50 = OpConstant %6 0
  76. %10 = OpConstant %6 1
  77. %11 = OpConstant %6 2
  78. %12 = OpConstantComposite %7 %11
  79. %13 = OpConstant %6 4
  80. %14 = OpTypePointer Function %6
  81. %4 = OpFunction %2 None %3
  82. %5 = OpLabel
  83. %9 = OpVariable %8 Function
  84. OpStore %9 %12
  85. %15 = OpAccessChain %14 %9 %50
  86. %22 = OpInBoundsAccessChain %14 %9 %50
  87. %20 = OpLoad %7 %9
  88. %21 = OpCompositeExtract %6 %20 0
  89. %23 = OpCompositeInsert %7 %10 %20 0
  90. OpStore %15 %13
  91. OpReturn
  92. OpFunctionEnd
  93. )";
  94. CheckEqual(env, expected, context.get());
  95. }
  96. TEST(RemoveUnusedStructMemberTest, RemoveUniformBufferMember) {
  97. std::string shader = R"(
  98. OpCapability Shader
  99. %1 = OpExtInstImport "GLSL.std.450"
  100. OpMemoryModel Logical GLSL450
  101. OpEntryPoint Fragment %4 "main"
  102. OpExecutionMode %4 OriginUpperLeft
  103. OpSource ESSL 310
  104. OpMemberDecorate %10 0 Offset 0
  105. OpMemberDecorate %10 1 Offset 4
  106. OpDecorate %10 Block
  107. OpDecorate %12 DescriptorSet 0
  108. OpDecorate %12 Binding 0
  109. %2 = OpTypeVoid
  110. %3 = OpTypeFunction %2
  111. %6 = OpTypeFloat 32
  112. %7 = OpTypePointer Function %6
  113. %9 = OpTypeInt 32 1
  114. %10 = OpTypeStruct %9 %6
  115. %11 = OpTypePointer Uniform %10
  116. %12 = OpVariable %11 Uniform
  117. %13 = OpConstant %9 1
  118. %20 = OpConstant %9 0
  119. %14 = OpTypePointer Uniform %6
  120. %4 = OpFunction %2 None %3
  121. %5 = OpLabel
  122. %8 = OpVariable %7 Function
  123. %15 = OpAccessChain %14 %12 %13
  124. %16 = OpLoad %6 %15
  125. OpStore %8 %16
  126. OpReturn
  127. OpFunctionEnd
  128. )";
  129. const auto env = SPV_ENV_UNIVERSAL_1_3;
  130. const auto consumer = nullptr;
  131. const auto context =
  132. BuildModule(env, consumer, shader, kReduceAssembleOption);
  133. auto ops = RemoveUnusedStructMemberReductionOpportunityFinder()
  134. .GetAvailableOpportunities(context.get(), 0);
  135. ASSERT_EQ(1, ops.size());
  136. ASSERT_TRUE(ops[0]->PreconditionHolds());
  137. ops[0]->TryToApply();
  138. CheckValid(env, context.get());
  139. std::string expected = R"(
  140. OpCapability Shader
  141. %1 = OpExtInstImport "GLSL.std.450"
  142. OpMemoryModel Logical GLSL450
  143. OpEntryPoint Fragment %4 "main"
  144. OpExecutionMode %4 OriginUpperLeft
  145. OpSource ESSL 310
  146. OpMemberDecorate %10 0 Offset 4
  147. OpDecorate %10 Block
  148. OpDecorate %12 DescriptorSet 0
  149. OpDecorate %12 Binding 0
  150. %2 = OpTypeVoid
  151. %3 = OpTypeFunction %2
  152. %6 = OpTypeFloat 32
  153. %7 = OpTypePointer Function %6
  154. %9 = OpTypeInt 32 1
  155. %10 = OpTypeStruct %6
  156. %11 = OpTypePointer Uniform %10
  157. %12 = OpVariable %11 Uniform
  158. %13 = OpConstant %9 1
  159. %20 = OpConstant %9 0
  160. %14 = OpTypePointer Uniform %6
  161. %4 = OpFunction %2 None %3
  162. %5 = OpLabel
  163. %8 = OpVariable %7 Function
  164. %15 = OpAccessChain %14 %12 %20
  165. %16 = OpLoad %6 %15
  166. OpStore %8 %16
  167. OpReturn
  168. OpFunctionEnd
  169. )";
  170. CheckEqual(env, expected, context.get());
  171. }
  172. TEST(RemoveUnusedStructMemberTest, DoNotRemoveNamedMemberRemoveOneMember) {
  173. // This illustrates that naming a member is enough to prevent its removal.
  174. // Removal of names is done by a different pass.
  175. std::string shader = R"(
  176. OpCapability Shader
  177. %1 = OpExtInstImport "GLSL.std.450"
  178. OpMemoryModel Logical GLSL450
  179. OpEntryPoint Fragment %4 "main"
  180. OpExecutionMode %4 OriginUpperLeft
  181. OpSource ESSL 310
  182. OpMemberName %7 0 "someName"
  183. OpMemberName %7 1 "someOtherName"
  184. %2 = OpTypeVoid
  185. %3 = OpTypeFunction %2
  186. %6 = OpTypeInt 32 1
  187. %7 = OpTypeStruct %6 %6
  188. %8 = OpTypePointer Function %7
  189. %50 = OpConstant %6 0
  190. %10 = OpConstant %6 1
  191. %11 = OpConstant %6 2
  192. %12 = OpConstantComposite %7 %10 %11
  193. %13 = OpConstant %6 4
  194. %14 = OpTypePointer Function %6
  195. %4 = OpFunction %2 None %3
  196. %5 = OpLabel
  197. %9 = OpVariable %8 Function
  198. OpStore %9 %12
  199. %15 = OpAccessChain %14 %9 %10
  200. %22 = OpInBoundsAccessChain %14 %9 %10
  201. %20 = OpLoad %7 %9
  202. %21 = OpCompositeExtract %6 %20 1
  203. %23 = OpCompositeInsert %7 %10 %20 1
  204. OpStore %15 %13
  205. OpReturn
  206. OpFunctionEnd
  207. )";
  208. const auto env = SPV_ENV_UNIVERSAL_1_3;
  209. const auto consumer = nullptr;
  210. const auto context =
  211. BuildModule(env, consumer, shader, kReduceAssembleOption);
  212. auto ops = RemoveUnusedStructMemberReductionOpportunityFinder()
  213. .GetAvailableOpportunities(context.get(), 0);
  214. ASSERT_EQ(0, ops.size());
  215. }
  216. } // namespace
  217. } // namespace reduce
  218. } // namespace spvtools