flags.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # Copyright (c) 2018 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. import placeholder
  15. import expect
  16. import re
  17. from spirv_test_framework import inside_spirv_testsuite
  18. def empty_main_assembly():
  19. return """
  20. OpCapability Shader
  21. OpMemoryModel Logical GLSL450
  22. OpEntryPoint Vertex %4 "main"
  23. OpName %4 "main"
  24. %2 = OpTypeVoid
  25. %3 = OpTypeFunction %2
  26. %4 = OpFunction %2 None %3
  27. %5 = OpLabel
  28. OpReturn
  29. OpFunctionEnd"""
  30. @inside_spirv_testsuite('SpirvOptBase')
  31. class TestAssemblyFileAsOnlyParameter(expect.ValidObjectFile1_6):
  32. """Tests that spirv-opt accepts a SPIR-V object file."""
  33. shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm')
  34. output = placeholder.TempFileName('output.spv')
  35. spirv_args = [shader, '-o', output]
  36. expected_object_filenames = (output)
  37. @inside_spirv_testsuite('SpirvOptFlags')
  38. class TestHelpFlag(expect.ReturnCodeIsZero, expect.StdoutMatch):
  39. """Test the --help flag."""
  40. spirv_args = ['--help']
  41. expected_stdout = re.compile(r'.*The SPIR-V binary is read from <input>')
  42. @inside_spirv_testsuite('SpirvOptFlags')
  43. class TestValidPassFlags(expect.ValidObjectFile1_6,
  44. expect.ExecutedListOfPasses):
  45. """Tests that spirv-opt accepts all valid optimization flags."""
  46. flags = [
  47. '--wrap-opkill', '--ccp', '--cfg-cleanup', '--combine-access-chains', '--compact-ids',
  48. '--convert-local-access-chains', '--copy-propagate-arrays',
  49. '--eliminate-dead-branches',
  50. '--eliminate-dead-code-aggressive', '--eliminate-dead-const',
  51. '--eliminate-dead-functions', '--eliminate-dead-inserts',
  52. '--eliminate-dead-variables', '--eliminate-insert-extract',
  53. '--eliminate-local-multi-store', '--eliminate-local-single-block',
  54. '--eliminate-local-single-store', '--flatten-decorations',
  55. '--fold-spec-const-op-composite', '--freeze-spec-const',
  56. '--if-conversion', '--inline-entry-points-exhaustive', '--loop-fission',
  57. '20', '--loop-fusion', '5', '--loop-unroll', '--loop-unroll-partial', '3',
  58. '--loop-peeling', '--merge-blocks', '--merge-return', '--loop-unswitch',
  59. '--private-to-local', '--reduce-load-size', '--redundancy-elimination',
  60. '--remove-duplicates', '--replace-invalid-opcode', '--ssa-rewrite',
  61. '--scalar-replacement', '--scalar-replacement=42', '--strength-reduction',
  62. '--strip-debug', '--strip-nonsemantic', '--vector-dce', '--workaround-1209',
  63. '--unify-const', '--graphics-robust-access', '--wrap-opkill', '--amd-ext-to-khr'
  64. ]
  65. expected_passes = [
  66. 'wrap-opkill',
  67. 'ccp',
  68. 'cfg-cleanup',
  69. 'combine-access-chains',
  70. 'compact-ids',
  71. 'convert-local-access-chains',
  72. 'copy-propagate-arrays',
  73. 'eliminate-dead-branches',
  74. 'eliminate-dead-code-aggressive',
  75. 'eliminate-dead-const',
  76. 'eliminate-dead-functions',
  77. 'eliminate-dead-inserts',
  78. 'eliminate-dead-variables',
  79. # --eliminate-insert-extract runs the simplify-instructions pass.
  80. 'simplify-instructions',
  81. 'ssa-rewrite',
  82. 'eliminate-local-single-block',
  83. 'eliminate-local-single-store',
  84. 'flatten-decorations',
  85. 'fold-spec-const-op-composite',
  86. 'freeze-spec-const',
  87. 'if-conversion',
  88. 'inline-entry-points-exhaustive',
  89. 'loop-fission',
  90. 'loop-fusion',
  91. 'loop-unroll',
  92. 'loop-unroll',
  93. 'loop-peeling',
  94. 'merge-blocks',
  95. 'merge-return',
  96. 'loop-unswitch',
  97. 'private-to-local',
  98. 'reduce-load-size',
  99. 'redundancy-elimination',
  100. 'remove-duplicates',
  101. 'replace-invalid-opcode',
  102. 'ssa-rewrite',
  103. 'scalar-replacement=0',
  104. 'scalar-replacement=42',
  105. 'strength-reduction',
  106. 'strip-debug',
  107. 'strip-nonsemantic',
  108. 'vector-dce',
  109. 'workaround-1209',
  110. 'unify-const',
  111. 'graphics-robust-access',
  112. 'wrap-opkill',
  113. 'amd-ext-to-khr'
  114. ]
  115. shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm')
  116. output = placeholder.TempFileName('output.spv')
  117. spirv_args = [shader, '-o', output, '--print-all'] + flags
  118. expected_object_filenames = (output)
  119. @inside_spirv_testsuite('SpirvOptFlags')
  120. class TestPerformanceOptimizationPasses(expect.ValidObjectFile1_6,
  121. expect.ExecutedListOfPasses):
  122. """Tests that spirv-opt schedules all the passes triggered by -O."""
  123. flags = ['-O']
  124. expected_passes = [
  125. 'wrap-opkill',
  126. 'eliminate-dead-branches',
  127. 'merge-return',
  128. 'inline-entry-points-exhaustive',
  129. 'eliminate-dead-functions',
  130. 'eliminate-dead-code-aggressive',
  131. 'private-to-local',
  132. 'eliminate-local-single-block',
  133. 'eliminate-local-single-store',
  134. 'eliminate-dead-code-aggressive',
  135. 'scalar-replacement=0',
  136. 'convert-local-access-chains',
  137. 'eliminate-local-single-block',
  138. 'eliminate-local-single-store',
  139. 'eliminate-dead-code-aggressive',
  140. 'ssa-rewrite',
  141. 'eliminate-dead-code-aggressive',
  142. 'ccp',
  143. 'eliminate-dead-code-aggressive',
  144. 'loop-unroll',
  145. 'eliminate-dead-branches',
  146. 'redundancy-elimination',
  147. 'combine-access-chains',
  148. 'simplify-instructions',
  149. 'scalar-replacement=0',
  150. 'convert-local-access-chains',
  151. 'eliminate-local-single-block',
  152. 'eliminate-local-single-store',
  153. 'eliminate-dead-code-aggressive',
  154. 'ssa-rewrite',
  155. 'eliminate-dead-code-aggressive',
  156. 'vector-dce',
  157. 'eliminate-dead-inserts',
  158. 'eliminate-dead-branches',
  159. 'simplify-instructions',
  160. 'if-conversion',
  161. 'copy-propagate-arrays',
  162. 'reduce-load-size',
  163. 'eliminate-dead-code-aggressive',
  164. 'merge-blocks',
  165. 'redundancy-elimination',
  166. 'eliminate-dead-branches',
  167. 'merge-blocks',
  168. 'simplify-instructions',
  169. ]
  170. shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm')
  171. output = placeholder.TempFileName('output.spv')
  172. spirv_args = [shader, '-o', output, '--print-all'] + flags
  173. expected_object_filenames = (output)
  174. @inside_spirv_testsuite('SpirvOptFlags')
  175. class TestSizeOptimizationPasses(expect.ValidObjectFile1_6,
  176. expect.ExecutedListOfPasses):
  177. """Tests that spirv-opt schedules all the passes triggered by -Os."""
  178. flags = ['-Os']
  179. expected_passes = [
  180. 'wrap-opkill',
  181. 'eliminate-dead-branches',
  182. 'merge-return',
  183. 'inline-entry-points-exhaustive',
  184. 'eliminate-dead-functions',
  185. 'private-to-local',
  186. 'scalar-replacement=0',
  187. 'ssa-rewrite',
  188. 'ccp',
  189. 'loop-unroll',
  190. 'eliminate-dead-branches',
  191. 'simplify-instructions',
  192. 'scalar-replacement=0',
  193. 'eliminate-local-single-store',
  194. 'if-conversion',
  195. 'simplify-instructions',
  196. 'eliminate-dead-code-aggressive',
  197. 'eliminate-dead-branches',
  198. 'merge-blocks',
  199. 'convert-local-access-chains',
  200. 'eliminate-local-single-block',
  201. 'eliminate-dead-code-aggressive',
  202. 'copy-propagate-arrays',
  203. 'vector-dce',
  204. 'eliminate-dead-inserts',
  205. 'eliminate-dead-members',
  206. 'eliminate-local-single-store',
  207. 'merge-blocks',
  208. 'ssa-rewrite',
  209. 'redundancy-elimination',
  210. 'simplify-instructions',
  211. 'eliminate-dead-code-aggressive',
  212. 'cfg-cleanup',
  213. ]
  214. shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm')
  215. output = placeholder.TempFileName('output.spv')
  216. spirv_args = [shader, '-o', output, '--print-all'] + flags
  217. expected_object_filenames = (output)
  218. @inside_spirv_testsuite('SpirvOptFlags')
  219. class TestLegalizationPasses(expect.ValidObjectFile1_6,
  220. expect.ExecutedListOfPasses):
  221. """Tests that spirv-opt schedules all the passes triggered by --legalize-hlsl.
  222. """
  223. flags = ['--legalize-hlsl']
  224. expected_passes = [
  225. 'wrap-opkill',
  226. 'eliminate-dead-branches',
  227. 'merge-return',
  228. 'inline-entry-points-exhaustive',
  229. 'eliminate-dead-functions',
  230. 'private-to-local',
  231. 'fix-storage-class',
  232. 'eliminate-local-single-block',
  233. 'eliminate-local-single-store',
  234. 'eliminate-dead-code-aggressive',
  235. 'scalar-replacement=0',
  236. 'eliminate-local-single-block',
  237. 'eliminate-local-single-store',
  238. 'eliminate-dead-code-aggressive',
  239. 'ssa-rewrite',
  240. 'eliminate-dead-code-aggressive',
  241. 'ccp',
  242. 'loop-unroll',
  243. 'eliminate-dead-branches',
  244. 'simplify-instructions',
  245. 'eliminate-dead-code-aggressive',
  246. 'copy-propagate-arrays',
  247. 'vector-dce',
  248. 'eliminate-dead-inserts',
  249. 'reduce-load-size',
  250. 'eliminate-dead-code-aggressive',
  251. ]
  252. shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm')
  253. output = placeholder.TempFileName('output.spv')
  254. spirv_args = [shader, '-o', output, '--print-all'] + flags
  255. expected_object_filenames = (output)
  256. @inside_spirv_testsuite('SpirvOptFlags')
  257. class TestScalarReplacementArgsNegative(expect.ErrorMessageSubstr):
  258. """Tests invalid arguments to --scalar-replacement."""
  259. spirv_args = ['--scalar-replacement=-10']
  260. expected_error_substr = 'must have no arguments or a non-negative integer argument'
  261. @inside_spirv_testsuite('SpirvOptFlags')
  262. class TestScalarReplacementArgsInvalidNumber(expect.ErrorMessageSubstr):
  263. """Tests invalid arguments to --scalar-replacement."""
  264. spirv_args = ['--scalar-replacement=a10f']
  265. expected_error_substr = 'must have no arguments or a non-negative integer argument'
  266. @inside_spirv_testsuite('SpirvOptFlags')
  267. class TestLoopFissionArgsNegative(expect.ErrorMessageSubstr):
  268. """Tests invalid arguments to --loop-fission."""
  269. spirv_args = ['--loop-fission=-10']
  270. expected_error_substr = 'must have a positive integer argument'
  271. @inside_spirv_testsuite('SpirvOptFlags')
  272. class TestLoopFissionArgsInvalidNumber(expect.ErrorMessageSubstr):
  273. """Tests invalid arguments to --loop-fission."""
  274. spirv_args = ['--loop-fission=a10f']
  275. expected_error_substr = 'must have a positive integer argument'
  276. @inside_spirv_testsuite('SpirvOptFlags')
  277. class TestLoopFusionArgsNegative(expect.ErrorMessageSubstr):
  278. """Tests invalid arguments to --loop-fusion."""
  279. spirv_args = ['--loop-fusion=-10']
  280. expected_error_substr = 'must have a positive integer argument'
  281. @inside_spirv_testsuite('SpirvOptFlags')
  282. class TestLoopFusionArgsInvalidNumber(expect.ErrorMessageSubstr):
  283. """Tests invalid arguments to --loop-fusion."""
  284. spirv_args = ['--loop-fusion=a10f']
  285. expected_error_substr = 'must have a positive integer argument'
  286. @inside_spirv_testsuite('SpirvOptFlags')
  287. class TestLoopUnrollPartialArgsNegative(expect.ErrorMessageSubstr):
  288. """Tests invalid arguments to --loop-unroll-partial."""
  289. spirv_args = ['--loop-unroll-partial=-10']
  290. expected_error_substr = 'must have a positive integer argument'
  291. @inside_spirv_testsuite('SpirvOptFlags')
  292. class TestLoopUnrollPartialArgsInvalidNumber(expect.ErrorMessageSubstr):
  293. """Tests invalid arguments to --loop-unroll-partial."""
  294. spirv_args = ['--loop-unroll-partial=a10f']
  295. expected_error_substr = 'must have a positive integer argument'
  296. @inside_spirv_testsuite('SpirvOptFlags')
  297. class TestLoopPeelingThresholdArgsNegative(expect.ErrorMessageSubstr):
  298. """Tests invalid arguments to --loop-peeling-threshold."""
  299. spirv_args = ['--loop-peeling-threshold=-10']
  300. expected_error_substr = 'must have a positive integer argument'
  301. @inside_spirv_testsuite('SpirvOptFlags')
  302. class TestLoopPeelingThresholdArgsInvalidNumber(expect.ErrorMessageSubstr):
  303. """Tests invalid arguments to --loop-peeling-threshold."""
  304. spirv_args = ['--loop-peeling-threshold=a10f']
  305. expected_error_substr = 'must have a positive integer argument'