remove_function_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Copyright (c) 2019 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 "reduce_test_util.h"
  15. #include "source/opt/build_module.h"
  16. #include "source/reduce/reduction_opportunity.h"
  17. #include "source/reduce/remove_function_reduction_opportunity_finder.h"
  18. namespace spvtools {
  19. namespace reduce {
  20. namespace {
  21. // Helper to count the number of functions in the module.
  22. // Remove if there turns out to be a more direct way to do this.
  23. uint32_t count_functions(opt::IRContext* context) {
  24. uint32_t result = 0;
  25. for (auto& function : *context->module()) {
  26. (void)(function);
  27. ++result;
  28. }
  29. return result;
  30. }
  31. TEST(RemoveFunctionTest, BasicCheck) {
  32. std::string shader = R"(
  33. OpCapability Shader
  34. %1 = OpExtInstImport "GLSL.std.450"
  35. OpMemoryModel Logical GLSL450
  36. OpEntryPoint Fragment %4 "main"
  37. OpExecutionMode %4 OriginUpperLeft
  38. OpSource ESSL 310
  39. %2 = OpTypeVoid
  40. %3 = OpTypeFunction %2
  41. %4 = OpFunction %2 None %3
  42. %5 = OpLabel
  43. OpReturn
  44. OpFunctionEnd
  45. %6 = OpFunction %2 None %3
  46. %7 = OpLabel
  47. OpReturn
  48. OpFunctionEnd
  49. %8 = OpFunction %2 None %3
  50. %9 = OpLabel
  51. %10 = OpFunctionCall %2 %6
  52. OpReturn
  53. OpFunctionEnd
  54. )";
  55. const auto env = SPV_ENV_UNIVERSAL_1_3;
  56. const auto consumer = nullptr;
  57. const auto context =
  58. BuildModule(env, consumer, shader, kReduceAssembleOption);
  59. ASSERT_EQ(3, count_functions(context.get()));
  60. auto ops =
  61. RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  62. context.get());
  63. ASSERT_EQ(1, ops.size());
  64. ASSERT_TRUE(ops[0]->PreconditionHolds());
  65. ops[0]->TryToApply();
  66. ASSERT_EQ(2, count_functions(context.get()));
  67. std::string after_first = R"(
  68. OpCapability Shader
  69. %1 = OpExtInstImport "GLSL.std.450"
  70. OpMemoryModel Logical GLSL450
  71. OpEntryPoint Fragment %4 "main"
  72. OpExecutionMode %4 OriginUpperLeft
  73. OpSource ESSL 310
  74. %2 = OpTypeVoid
  75. %3 = OpTypeFunction %2
  76. %4 = OpFunction %2 None %3
  77. %5 = OpLabel
  78. OpReturn
  79. OpFunctionEnd
  80. %6 = OpFunction %2 None %3
  81. %7 = OpLabel
  82. OpReturn
  83. OpFunctionEnd
  84. )";
  85. CheckEqual(env, after_first, context.get());
  86. ops = RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  87. context.get());
  88. ASSERT_EQ(1, ops.size());
  89. ASSERT_TRUE(ops[0]->PreconditionHolds());
  90. ops[0]->TryToApply();
  91. ASSERT_EQ(1, count_functions(context.get()));
  92. std::string after_second = R"(
  93. OpCapability Shader
  94. %1 = OpExtInstImport "GLSL.std.450"
  95. OpMemoryModel Logical GLSL450
  96. OpEntryPoint Fragment %4 "main"
  97. OpExecutionMode %4 OriginUpperLeft
  98. OpSource ESSL 310
  99. %2 = OpTypeVoid
  100. %3 = OpTypeFunction %2
  101. %4 = OpFunction %2 None %3
  102. %5 = OpLabel
  103. OpReturn
  104. OpFunctionEnd
  105. )";
  106. CheckEqual(env, after_second, context.get());
  107. }
  108. TEST(RemoveFunctionTest, NothingToRemove) {
  109. std::string shader = R"(
  110. OpCapability Shader
  111. %1 = OpExtInstImport "GLSL.std.450"
  112. OpMemoryModel Logical GLSL450
  113. OpEntryPoint Fragment %4 "main"
  114. OpExecutionMode %4 OriginUpperLeft
  115. OpSource ESSL 310
  116. %2 = OpTypeVoid
  117. %3 = OpTypeFunction %2
  118. %4 = OpFunction %2 None %3
  119. %5 = OpLabel
  120. %11 = OpFunctionCall %2 %8
  121. OpReturn
  122. OpFunctionEnd
  123. %6 = OpFunction %2 None %3
  124. %7 = OpLabel
  125. OpReturn
  126. OpFunctionEnd
  127. %8 = OpFunction %2 None %3
  128. %9 = OpLabel
  129. %10 = OpFunctionCall %2 %6
  130. OpReturn
  131. OpFunctionEnd
  132. )";
  133. const auto env = SPV_ENV_UNIVERSAL_1_3;
  134. const auto consumer = nullptr;
  135. const auto context =
  136. BuildModule(env, consumer, shader, kReduceAssembleOption);
  137. auto ops =
  138. RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  139. context.get());
  140. ASSERT_EQ(0, ops.size());
  141. }
  142. TEST(RemoveFunctionTest, TwoRemovableFunctions) {
  143. std::string shader = R"(
  144. OpCapability Shader
  145. %1 = OpExtInstImport "GLSL.std.450"
  146. OpMemoryModel Logical GLSL450
  147. OpEntryPoint Fragment %4 "main"
  148. OpExecutionMode %4 OriginUpperLeft
  149. OpSource ESSL 310
  150. %2 = OpTypeVoid
  151. %3 = OpTypeFunction %2
  152. %4 = OpFunction %2 None %3
  153. %5 = OpLabel
  154. OpReturn
  155. OpFunctionEnd
  156. %6 = OpFunction %2 None %3
  157. %7 = OpLabel
  158. OpReturn
  159. OpFunctionEnd
  160. %8 = OpFunction %2 None %3
  161. %9 = OpLabel
  162. OpReturn
  163. OpFunctionEnd
  164. )";
  165. const auto env = SPV_ENV_UNIVERSAL_1_3;
  166. const auto consumer = nullptr;
  167. const auto context =
  168. BuildModule(env, consumer, shader, kReduceAssembleOption);
  169. ASSERT_EQ(3, count_functions(context.get()));
  170. auto ops =
  171. RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  172. context.get());
  173. ASSERT_EQ(2, ops.size());
  174. ASSERT_TRUE(ops[0]->PreconditionHolds());
  175. ops[0]->TryToApply();
  176. ASSERT_EQ(2, count_functions(context.get()));
  177. ASSERT_TRUE(ops[1]->PreconditionHolds());
  178. ops[1]->TryToApply();
  179. ASSERT_EQ(1, count_functions(context.get()));
  180. std::string after = R"(
  181. OpCapability Shader
  182. %1 = OpExtInstImport "GLSL.std.450"
  183. OpMemoryModel Logical GLSL450
  184. OpEntryPoint Fragment %4 "main"
  185. OpExecutionMode %4 OriginUpperLeft
  186. OpSource ESSL 310
  187. %2 = OpTypeVoid
  188. %3 = OpTypeFunction %2
  189. %4 = OpFunction %2 None %3
  190. %5 = OpLabel
  191. OpReturn
  192. OpFunctionEnd
  193. )";
  194. CheckEqual(env, after, context.get());
  195. }
  196. TEST(RemoveFunctionTest, NoRemovalsDueToOpName) {
  197. std::string shader = R"(
  198. OpCapability Shader
  199. %1 = OpExtInstImport "GLSL.std.450"
  200. OpMemoryModel Logical GLSL450
  201. OpEntryPoint Fragment %4 "main"
  202. OpExecutionMode %4 OriginUpperLeft
  203. OpSource ESSL 310
  204. OpName %4 "main"
  205. OpName %6 "foo("
  206. OpName %8 "bar("
  207. %2 = OpTypeVoid
  208. %3 = OpTypeFunction %2
  209. %4 = OpFunction %2 None %3
  210. %5 = OpLabel
  211. OpReturn
  212. OpFunctionEnd
  213. %6 = OpFunction %2 None %3
  214. %7 = OpLabel
  215. OpReturn
  216. OpFunctionEnd
  217. %8 = OpFunction %2 None %3
  218. %9 = OpLabel
  219. OpReturn
  220. OpFunctionEnd
  221. )";
  222. const auto env = SPV_ENV_UNIVERSAL_1_3;
  223. const auto consumer = nullptr;
  224. const auto context =
  225. BuildModule(env, consumer, shader, kReduceAssembleOption);
  226. auto ops =
  227. RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  228. context.get());
  229. ASSERT_EQ(0, ops.size());
  230. }
  231. TEST(RemoveFunctionTest, NoRemovalDueToLinkageDecoration) {
  232. // The non-entry point function is not removable because it is referenced by a
  233. // linkage decoration. Thus no function can be removed.
  234. std::string shader = R"(
  235. OpCapability Shader
  236. OpCapability Linkage
  237. OpMemoryModel Logical GLSL450
  238. OpEntryPoint Fragment %1 "main"
  239. OpName %1 "main"
  240. OpDecorate %2 LinkageAttributes "ExportedFunc" Export
  241. %4 = OpTypeVoid
  242. %5 = OpTypeFunction %4
  243. %1 = OpFunction %4 None %5
  244. %6 = OpLabel
  245. OpReturn
  246. OpFunctionEnd
  247. %2 = OpFunction %4 None %5
  248. %7 = OpLabel
  249. OpReturn
  250. OpFunctionEnd
  251. )";
  252. const auto env = SPV_ENV_UNIVERSAL_1_3;
  253. const auto consumer = nullptr;
  254. const auto context =
  255. BuildModule(env, consumer, shader, kReduceAssembleOption);
  256. auto ops =
  257. RemoveFunctionReductionOpportunityFinder().GetAvailableOpportunities(
  258. context.get());
  259. ASSERT_EQ(0, ops.size());
  260. }
  261. } // namespace
  262. } // namespace reduce
  263. } // namespace spvtools