dead_variable_elim_test.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <string>
  15. #include "test/opt/pass_fixture.h"
  16. #include "test/opt/pass_utils.h"
  17. namespace spvtools {
  18. namespace opt {
  19. namespace {
  20. using DeadVariableElimTest = PassTest<::testing::Test>;
  21. // %dead is unused. Make sure we remove it along with its name.
  22. TEST_F(DeadVariableElimTest, RemoveUnreferenced) {
  23. const std::string before =
  24. R"(OpCapability Shader
  25. OpCapability Linkage
  26. %1 = OpExtInstImport "GLSL.std.450"
  27. OpMemoryModel Logical GLSL450
  28. OpEntryPoint Fragment %main "main"
  29. OpExecutionMode %main OriginUpperLeft
  30. OpSource GLSL 150
  31. OpName %main "main"
  32. OpName %dead "dead"
  33. %void = OpTypeVoid
  34. %5 = OpTypeFunction %void
  35. %float = OpTypeFloat 32
  36. %_ptr_Private_float = OpTypePointer Private %float
  37. %dead = OpVariable %_ptr_Private_float Private
  38. %main = OpFunction %void None %5
  39. %8 = OpLabel
  40. OpReturn
  41. OpFunctionEnd
  42. )";
  43. const std::string after =
  44. R"(OpCapability Shader
  45. OpCapability Linkage
  46. %1 = OpExtInstImport "GLSL.std.450"
  47. OpMemoryModel Logical GLSL450
  48. OpEntryPoint Fragment %main "main"
  49. OpExecutionMode %main OriginUpperLeft
  50. OpSource GLSL 150
  51. OpName %main "main"
  52. %void = OpTypeVoid
  53. %5 = OpTypeFunction %void
  54. %float = OpTypeFloat 32
  55. %_ptr_Private_float = OpTypePointer Private %float
  56. %main = OpFunction %void None %5
  57. %8 = OpLabel
  58. OpReturn
  59. OpFunctionEnd
  60. )";
  61. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  62. SinglePassRunAndCheck<DeadVariableElimination>(before, after, true, true);
  63. }
  64. // Since %dead is exported, make sure we keep it. It could be referenced
  65. // somewhere else.
  66. TEST_F(DeadVariableElimTest, KeepExported) {
  67. const std::string before =
  68. R"(OpCapability Shader
  69. OpCapability Linkage
  70. %1 = OpExtInstImport "GLSL.std.450"
  71. OpMemoryModel Logical GLSL450
  72. OpEntryPoint Fragment %main "main"
  73. OpExecutionMode %main OriginUpperLeft
  74. OpSource GLSL 150
  75. OpName %main "main"
  76. OpName %dead "dead"
  77. OpDecorate %dead LinkageAttributes "dead" Export
  78. %void = OpTypeVoid
  79. %5 = OpTypeFunction %void
  80. %float = OpTypeFloat 32
  81. %_ptr_Private_float = OpTypePointer Private %float
  82. %dead = OpVariable %_ptr_Private_float Private
  83. %main = OpFunction %void None %5
  84. %8 = OpLabel
  85. OpReturn
  86. OpFunctionEnd
  87. )";
  88. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  89. SinglePassRunAndCheck<DeadVariableElimination>(before, before, true, true);
  90. }
  91. // Delete %dead because it is unreferenced. Then %initializer becomes
  92. // unreferenced, so remove it as well.
  93. TEST_F(DeadVariableElimTest, RemoveUnreferencedWithInit1) {
  94. const std::string before =
  95. R"(OpCapability Shader
  96. OpCapability Linkage
  97. %1 = OpExtInstImport "GLSL.std.450"
  98. OpMemoryModel Logical GLSL450
  99. OpEntryPoint Fragment %main "main"
  100. OpExecutionMode %main OriginUpperLeft
  101. OpSource GLSL 150
  102. OpName %main "main"
  103. OpName %dead "dead"
  104. OpName %initializer "initializer"
  105. %void = OpTypeVoid
  106. %6 = OpTypeFunction %void
  107. %float = OpTypeFloat 32
  108. %_ptr_Private_float = OpTypePointer Private %float
  109. %initializer = OpVariable %_ptr_Private_float Private
  110. %dead = OpVariable %_ptr_Private_float Private %initializer
  111. %main = OpFunction %void None %6
  112. %9 = OpLabel
  113. OpReturn
  114. OpFunctionEnd
  115. )";
  116. const std::string after =
  117. R"(OpCapability Shader
  118. OpCapability Linkage
  119. %1 = OpExtInstImport "GLSL.std.450"
  120. OpMemoryModel Logical GLSL450
  121. OpEntryPoint Fragment %main "main"
  122. OpExecutionMode %main OriginUpperLeft
  123. OpSource GLSL 150
  124. OpName %main "main"
  125. %void = OpTypeVoid
  126. %6 = OpTypeFunction %void
  127. %float = OpTypeFloat 32
  128. %_ptr_Private_float = OpTypePointer Private %float
  129. %main = OpFunction %void None %6
  130. %9 = OpLabel
  131. OpReturn
  132. OpFunctionEnd
  133. )";
  134. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  135. SinglePassRunAndCheck<DeadVariableElimination>(before, after, true, true);
  136. }
  137. // Delete %dead because it is unreferenced. In this case, the initialized has
  138. // another reference, and should not be removed.
  139. TEST_F(DeadVariableElimTest, RemoveUnreferencedWithInit2) {
  140. const std::string before =
  141. R"(OpCapability Shader
  142. OpCapability Linkage
  143. %1 = OpExtInstImport "GLSL.std.450"
  144. OpMemoryModel Logical GLSL450
  145. OpEntryPoint Fragment %main "main"
  146. OpExecutionMode %main OriginUpperLeft
  147. OpSource GLSL 150
  148. OpName %main "main"
  149. OpName %dead "dead"
  150. OpName %initializer "initializer"
  151. %void = OpTypeVoid
  152. %6 = OpTypeFunction %void
  153. %float = OpTypeFloat 32
  154. %_ptr_Private_float = OpTypePointer Private %float
  155. %initializer = OpVariable %_ptr_Private_float Private
  156. %dead = OpVariable %_ptr_Private_float Private %initializer
  157. %main = OpFunction %void None %6
  158. %9 = OpLabel
  159. %10 = OpLoad %float %initializer
  160. OpReturn
  161. OpFunctionEnd
  162. )";
  163. const std::string after =
  164. R"(OpCapability Shader
  165. OpCapability Linkage
  166. %1 = OpExtInstImport "GLSL.std.450"
  167. OpMemoryModel Logical GLSL450
  168. OpEntryPoint Fragment %main "main"
  169. OpExecutionMode %main OriginUpperLeft
  170. OpSource GLSL 150
  171. OpName %main "main"
  172. OpName %initializer "initializer"
  173. %void = OpTypeVoid
  174. %6 = OpTypeFunction %void
  175. %float = OpTypeFloat 32
  176. %_ptr_Private_float = OpTypePointer Private %float
  177. %initializer = OpVariable %_ptr_Private_float Private
  178. %main = OpFunction %void None %6
  179. %9 = OpLabel
  180. %10 = OpLoad %float %initializer
  181. OpReturn
  182. OpFunctionEnd
  183. )";
  184. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  185. SinglePassRunAndCheck<DeadVariableElimination>(before, after, true, true);
  186. }
  187. // Keep %live because it is used, and its initializer.
  188. TEST_F(DeadVariableElimTest, KeepReferenced) {
  189. const std::string before =
  190. R"(OpCapability Shader
  191. OpCapability Linkage
  192. %1 = OpExtInstImport "GLSL.std.450"
  193. OpMemoryModel Logical GLSL450
  194. OpEntryPoint Fragment %main "main"
  195. OpExecutionMode %main OriginUpperLeft
  196. OpSource GLSL 150
  197. OpName %main "main"
  198. OpName %live "live"
  199. OpName %initializer "initializer"
  200. %void = OpTypeVoid
  201. %6 = OpTypeFunction %void
  202. %float = OpTypeFloat 32
  203. %_ptr_Private_float = OpTypePointer Private %float
  204. %initializer = OpConstant %float 0
  205. %live = OpVariable %_ptr_Private_float Private %initializer
  206. %main = OpFunction %void None %6
  207. %9 = OpLabel
  208. %10 = OpLoad %float %live
  209. OpReturn
  210. OpFunctionEnd
  211. )";
  212. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  213. SinglePassRunAndCheck<DeadVariableElimination>(before, before, true, true);
  214. }
  215. // This test that the decoration associated with a variable are removed when the
  216. // variable is removed.
  217. TEST_F(DeadVariableElimTest, RemoveVariableAndDecorations) {
  218. const std::string before =
  219. R"(OpCapability Shader
  220. %1 = OpExtInstImport "GLSL.std.450"
  221. OpMemoryModel Logical GLSL450
  222. OpEntryPoint Vertex %main "main"
  223. OpSource GLSL 450
  224. OpName %main "main"
  225. OpName %B "B"
  226. OpMemberName %B 0 "a"
  227. OpName %Bdat "Bdat"
  228. OpMemberDecorate %B 0 Offset 0
  229. OpDecorate %B BufferBlock
  230. OpDecorate %Bdat DescriptorSet 0
  231. OpDecorate %Bdat Binding 0
  232. %void = OpTypeVoid
  233. %6 = OpTypeFunction %void
  234. %uint = OpTypeInt 32 0
  235. %B = OpTypeStruct %uint
  236. %_ptr_Uniform_B = OpTypePointer Uniform %B
  237. %Bdat = OpVariable %_ptr_Uniform_B Uniform
  238. %int = OpTypeInt 32 1
  239. %int_0 = OpConstant %int 0
  240. %uint_1 = OpConstant %uint 1
  241. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  242. %main = OpFunction %void None %6
  243. %13 = OpLabel
  244. OpReturn
  245. OpFunctionEnd
  246. )";
  247. const std::string after =
  248. R"(OpCapability Shader
  249. %1 = OpExtInstImport "GLSL.std.450"
  250. OpMemoryModel Logical GLSL450
  251. OpEntryPoint Vertex %main "main"
  252. OpSource GLSL 450
  253. OpName %main "main"
  254. OpName %B "B"
  255. OpMemberName %B 0 "a"
  256. OpMemberDecorate %B 0 Offset 0
  257. OpDecorate %B BufferBlock
  258. %void = OpTypeVoid
  259. %6 = OpTypeFunction %void
  260. %uint = OpTypeInt 32 0
  261. %B = OpTypeStruct %uint
  262. %_ptr_Uniform_B = OpTypePointer Uniform %B
  263. %int = OpTypeInt 32 1
  264. %int_0 = OpConstant %int 0
  265. %uint_1 = OpConstant %uint 1
  266. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  267. %main = OpFunction %void None %6
  268. %13 = OpLabel
  269. OpReturn
  270. OpFunctionEnd
  271. )";
  272. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  273. SinglePassRunAndCheck<DeadVariableElimination>(before, after, true, true);
  274. }
  275. } // namespace
  276. } // namespace opt
  277. } // namespace spvtools