strip_debug_info_test.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright (c) 2016 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 <vector>
  15. #include "test/opt/pass_fixture.h"
  16. #include "test/opt/pass_utils.h"
  17. namespace spvtools {
  18. namespace opt {
  19. namespace {
  20. using StripLineDebugInfoTest = PassTest<::testing::Test>;
  21. TEST_F(StripLineDebugInfoTest, LineNoLine) {
  22. std::vector<const char*> text = {
  23. // clang-format off
  24. "OpCapability Shader",
  25. "%1 = OpExtInstImport \"GLSL.std.450\"",
  26. "OpMemoryModel Logical GLSL450",
  27. "OpEntryPoint Vertex %2 \"main\"",
  28. "%3 = OpString \"minimal.vert\"",
  29. "OpModuleProcessed \"42\"",
  30. "OpModuleProcessed \"43\"",
  31. "OpModuleProcessed \"44\"",
  32. "OpNoLine",
  33. "OpLine %3 10 10",
  34. "%void = OpTypeVoid",
  35. "OpLine %3 100 100",
  36. "%5 = OpTypeFunction %void",
  37. "%2 = OpFunction %void None %5",
  38. "OpLine %3 1 1",
  39. "OpNoLine",
  40. "OpLine %3 2 2",
  41. "OpLine %3 3 3",
  42. "%6 = OpLabel",
  43. "OpLine %3 4 4",
  44. "OpNoLine",
  45. "OpReturn",
  46. "OpLine %3 4 4",
  47. "OpNoLine",
  48. "OpFunctionEnd",
  49. "OpNoLine",
  50. "OpLine %3 4 5"
  51. // clang-format on
  52. };
  53. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(text),
  54. JoinNonDebugInsts(text),
  55. /* skip_nop = */ false);
  56. // Let's add more debug instruction before the "OpString" instruction.
  57. const std::vector<const char*> more_text = {
  58. "OpSourceContinued \"I'm a happy shader! Yay! ;)\"",
  59. "OpSourceContinued \"wahahaha\"",
  60. "OpSource ESSL 310",
  61. "OpSource ESSL 310",
  62. "OpSourceContinued \"wahahaha\"",
  63. "OpSourceContinued \"wahahaha\"",
  64. "OpSourceExtension \"save-the-world-extension\"",
  65. "OpName %2 \"main\"",
  66. };
  67. text.insert(text.begin() + 4, more_text.cbegin(), more_text.cend());
  68. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(text),
  69. JoinNonDebugInsts(text),
  70. /* skip_nop = */ false);
  71. }
  72. using StripDebugStringTest = PassTest<::testing::Test>;
  73. TEST_F(StripDebugStringTest, OpDecorateRemoved) {
  74. std::vector<const char*> input{
  75. // clang-format off
  76. "OpCapability Shader",
  77. "%1 = OpExtInstImport \"GLSL.std.450\"",
  78. "OpMemoryModel Logical GLSL450",
  79. "OpEntryPoint Vertex %2 \"main\"",
  80. "%3 = OpString \"minimal.vert\"",
  81. "OpDecorate %3 Location 1337",
  82. "%void = OpTypeVoid",
  83. "%5 = OpTypeFunction %void",
  84. "%2 = OpFunction %void None %5",
  85. "%6 = OpLabel",
  86. "OpReturn",
  87. "OpFunctionEnd",
  88. // clang-format on
  89. };
  90. std::vector<const char*> output{
  91. // clang-format off
  92. "OpCapability Shader",
  93. "%1 = OpExtInstImport \"GLSL.std.450\"",
  94. "OpMemoryModel Logical GLSL450",
  95. "OpEntryPoint Vertex %2 \"main\"",
  96. "%void = OpTypeVoid",
  97. "%5 = OpTypeFunction %void",
  98. "%2 = OpFunction %void None %5",
  99. "%6 = OpLabel",
  100. "OpReturn",
  101. "OpFunctionEnd",
  102. // clang-format on
  103. };
  104. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(input),
  105. JoinAllInsts(output),
  106. /* skip_nop = */ false,
  107. /* do_validation */ true);
  108. }
  109. TEST_F(StripDebugStringTest, OpNameRemoved) {
  110. std::vector<const char*> input{
  111. // clang-format off
  112. "OpCapability Shader",
  113. "%1 = OpExtInstImport \"GLSL.std.450\"",
  114. "OpMemoryModel Logical GLSL450",
  115. "OpEntryPoint Vertex %2 \"main\"",
  116. "%3 = OpString \"minimal.vert\"",
  117. "OpName %3 \"bob\"",
  118. "%void = OpTypeVoid",
  119. "%5 = OpTypeFunction %void",
  120. "%2 = OpFunction %void None %5",
  121. "%6 = OpLabel",
  122. "OpReturn",
  123. "OpFunctionEnd",
  124. // clang-format on
  125. };
  126. std::vector<const char*> output{
  127. // clang-format off
  128. "OpCapability Shader",
  129. "%1 = OpExtInstImport \"GLSL.std.450\"",
  130. "OpMemoryModel Logical GLSL450",
  131. "OpEntryPoint Vertex %2 \"main\"",
  132. "%void = OpTypeVoid",
  133. "%5 = OpTypeFunction %void",
  134. "%2 = OpFunction %void None %5",
  135. "%6 = OpLabel",
  136. "OpReturn",
  137. "OpFunctionEnd",
  138. // clang-format on
  139. };
  140. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(input),
  141. JoinAllInsts(output),
  142. /* skip_nop = */ false,
  143. /* do_validation */ true);
  144. }
  145. TEST_F(StripDebugStringTest, OpStringRemovedWithNonSemantic) {
  146. std::vector<const char*> input{
  147. // clang-format off
  148. "OpCapability Shader",
  149. "OpExtension \"SPV_KHR_non_semantic_info\"",
  150. "%1 = OpExtInstImport \"NonSemantic.Testing.Set\"",
  151. "OpMemoryModel Logical GLSL450",
  152. "OpEntryPoint Vertex %2 \"main\"",
  153. // this string is not referenced, should be removed fully
  154. "%3 = OpString \"minimal.vert\"",
  155. "OpName %3 \"bob\"",
  156. // this string is referenced and cannot be removed,
  157. // but the name should be
  158. "%4 = OpString \"secondary.inc\"",
  159. "OpName %4 \"sue\"",
  160. "%void = OpTypeVoid",
  161. "%6 = OpTypeFunction %void",
  162. "%2 = OpFunction %void None %6",
  163. "%7 = OpLabel",
  164. "%8 = OpExtInst %void %1 5 %4",
  165. "OpReturn",
  166. "OpFunctionEnd",
  167. // clang-format on
  168. };
  169. std::vector<const char*> output{
  170. // clang-format off
  171. "OpCapability Shader",
  172. "OpExtension \"SPV_KHR_non_semantic_info\"",
  173. "%1 = OpExtInstImport \"NonSemantic.Testing.Set\"",
  174. "OpMemoryModel Logical GLSL450",
  175. "OpEntryPoint Vertex %2 \"main\"",
  176. "%4 = OpString \"secondary.inc\"",
  177. "%void = OpTypeVoid",
  178. "%6 = OpTypeFunction %void",
  179. "%2 = OpFunction %void None %6",
  180. "%7 = OpLabel",
  181. "%8 = OpExtInst %void %1 5 %4",
  182. "OpReturn",
  183. "OpFunctionEnd",
  184. // clang-format on
  185. };
  186. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(input),
  187. JoinAllInsts(output),
  188. /* skip_nop = */ false,
  189. /* do_validation */ true);
  190. }
  191. using StripDebugInfoTest = PassTest<::testing::TestWithParam<const char*>>;
  192. TEST_P(StripDebugInfoTest, Kind) {
  193. std::vector<const char*> text = {
  194. "OpCapability Shader",
  195. "OpMemoryModel Logical GLSL450",
  196. GetParam(),
  197. };
  198. SinglePassRunAndCheck<StripDebugInfoPass>(JoinAllInsts(text),
  199. JoinNonDebugInsts(text),
  200. /* skip_nop = */ false);
  201. }
  202. // Test each possible non-line debug instruction.
  203. // clang-format off
  204. INSTANTIATE_TEST_SUITE_P(
  205. SingleKindDebugInst, StripDebugInfoTest,
  206. ::testing::ValuesIn(std::vector<const char*>({
  207. "OpSourceContinued \"I'm a happy shader! Yay! ;)\"",
  208. "OpSource ESSL 310",
  209. "OpSourceExtension \"save-the-world-extension\"",
  210. "OpName %main \"main\"",
  211. "OpMemberName %struct 0 \"field\"",
  212. "%1 = OpString \"name.vert\"",
  213. "OpModuleProcessed \"42\"",
  214. })));
  215. // clang-format on
  216. } // namespace
  217. } // namespace opt
  218. } // namespace spvtools