| 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 |
|
| 15 |
import placeholder |
| 16 |
import expect |
| 17 |
import re |
| 18 |
|
| 19 |
from spirv_test_framework import inside_spirv_testsuite |
| 20 |
|
| 21 |
|
| 22 |
def empty_main_assembly(): |
| 23 |
return """ |
| 24 |
OpCapability Shader |
| 25 |
OpMemoryModel Logical GLSL450 |
| 26 |
OpEntryPoint Vertex %4 "main" |
| 27 |
OpName %4 "main" |
| 28 |
%2 = OpTypeVoid |
| 29 |
%3 = OpTypeFunction %2 |
| 30 |
%4 = OpFunction %2 None %3 |
| 31 |
%5 = OpLabel |
| 32 |
OpReturn |
| 33 |
OpFunctionEnd""" |
| 34 |
|
| 35 |
|
| 36 |
@inside_spirv_testsuite('SpirvOptBase') |
| 37 |
class TestAssemblyFileAsOnlyParameter(expect.ValidObjectFile1_5): |
| 38 |
"""Tests that spirv-opt accepts a SPIR-V object file.""" |
| 39 |
|
| 40 |
shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm') |
| 41 |
output = placeholder.TempFileName('output.spv') |
| 42 |
spirv_args = [shader, '-o', output] |
| 43 |
expected_object_filenames = (output) |
| 44 |
|
| 45 |
|
| 46 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 47 |
class TestHelpFlag(expect.ReturnCodeIsZero, expect.StdoutMatch): |
| 48 |
"""Test the --help flag.""" |
| 49 |
|
| 50 |
spirv_args = ['--help'] |
| 51 |
expected_stdout = re.compile(r'.*The SPIR-V binary is read from <input>') |
| 52 |
|
| 53 |
|
| 54 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 55 |
class TestValidPassFlags(expect.ValidObjectFile1_5, |
| 56 |
expect.ExecutedListOfPasses): |
| 57 |
"""Tests that spirv-opt accepts all valid optimization flags.""" |
| 58 |
|
| 59 |
flags = [ |
| 60 |
'--wrap-opkill', '--ccp', '--cfg-cleanup', '--combine-access-chains', '--compact-ids', |
| 61 |
'--convert-local-access-chains', '--copy-propagate-arrays', |
| 62 |
'--eliminate-dead-branches', |
| 63 |
'--eliminate-dead-code-aggressive', '--eliminate-dead-const', |
| 64 |
'--eliminate-dead-functions', '--eliminate-dead-inserts', |
| 65 |
'--eliminate-dead-variables', '--eliminate-insert-extract', |
| 66 |
'--eliminate-local-multi-store', '--eliminate-local-single-block', |
| 67 |
'--eliminate-local-single-store', '--flatten-decorations', |
| 68 |
'--fold-spec-const-op-composite', '--freeze-spec-const', |
| 69 |
'--if-conversion', '--inline-entry-points-exhaustive', '--loop-fission', |
| 70 |
'20', '--loop-fusion', '5', '--loop-unroll', '--loop-unroll-partial', '3', |
| 71 |
'--loop-peeling', '--merge-blocks', '--merge-return', '--loop-unswitch', |
| 72 |
'--private-to-local', '--reduce-load-size', '--redundancy-elimination', |
| 73 |
'--remove-duplicates', '--replace-invalid-opcode', '--ssa-rewrite', |
| 74 |
'--scalar-replacement', '--scalar-replacement=42', '--strength-reduction', |
| 75 |
'--strip-debug', '--strip-reflect', '--vector-dce', '--workaround-1209', |
| 76 |
'--unify-const', '--legalize-vector-shuffle', |
| 77 |
'--split-invalid-unreachable', '--generate-webgpu-initializers', |
| 78 |
'--decompose-initialized-variables', '--graphics-robust-access', |
| 79 |
'--wrap-opkill', '--amd-ext-to-khr' |
| 80 |
] |
| 81 |
expected_passes = [ |
| 82 |
'wrap-opkill', |
| 83 |
'ccp', |
| 84 |
'cfg-cleanup', |
| 85 |
'combine-access-chains', |
| 86 |
'compact-ids', |
| 87 |
'convert-local-access-chains', |
| 88 |
'copy-propagate-arrays', |
| 89 |
'eliminate-dead-branches', |
| 90 |
'eliminate-dead-code-aggressive', |
| 91 |
'eliminate-dead-const', |
| 92 |
'eliminate-dead-functions', |
| 93 |
'eliminate-dead-inserts', |
| 94 |
'eliminate-dead-variables', |
| 95 |
# --eliminate-insert-extract runs the simplify-instructions pass. |
| 96 |
'simplify-instructions', |
| 97 |
'ssa-rewrite', |
| 98 |
'eliminate-local-single-block', |
| 99 |
'eliminate-local-single-store', |
| 100 |
'flatten-decorations', |
| 101 |
'fold-spec-const-op-composite', |
| 102 |
'freeze-spec-const', |
| 103 |
'if-conversion', |
| 104 |
'inline-entry-points-exhaustive', |
| 105 |
'loop-fission', |
| 106 |
'loop-fusion', |
| 107 |
'loop-unroll', |
| 108 |
'loop-unroll', |
| 109 |
'loop-peeling', |
| 110 |
'merge-blocks', |
| 111 |
'merge-return', |
| 112 |
'loop-unswitch', |
| 113 |
'private-to-local', |
| 114 |
'reduce-load-size', |
| 115 |
'redundancy-elimination', |
| 116 |
'remove-duplicates', |
| 117 |
'replace-invalid-opcode', |
| 118 |
'ssa-rewrite', |
| 119 |
'scalar-replacement=100', |
| 120 |
'scalar-replacement=42', |
| 121 |
'strength-reduction', |
| 122 |
'strip-debug', |
| 123 |
'strip-reflect', |
| 124 |
'vector-dce', |
| 125 |
'workaround-1209', |
| 126 |
'unify-const', |
| 127 |
'legalize-vector-shuffle', |
| 128 |
'split-invalid-unreachable', |
| 129 |
'generate-webgpu-initializers', |
| 130 |
'decompose-initialized-variables', |
| 131 |
'graphics-robust-access', |
| 132 |
'wrap-opkill', |
| 133 |
'amd-ext-to-khr' |
| 134 |
] |
| 135 |
shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm') |
| 136 |
output = placeholder.TempFileName('output.spv') |
| 137 |
spirv_args = [shader, '-o', output, '--print-all'] + flags |
| 138 |
expected_object_filenames = (output) |
| 139 |
|
| 140 |
|
| 141 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 142 |
class TestPerformanceOptimizationPasses(expect.ValidObjectFile1_5, |
| 143 |
expect.ExecutedListOfPasses): |
| 144 |
"""Tests that spirv-opt schedules all the passes triggered by -O.""" |
| 145 |
|
| 146 |
flags = ['-O'] |
| 147 |
expected_passes = [ |
| 148 |
'wrap-opkill', |
| 149 |
'eliminate-dead-branches', |
| 150 |
'merge-return', |
| 151 |
'inline-entry-points-exhaustive', |
| 152 |
'eliminate-dead-functions', |
| 153 |
'eliminate-dead-code-aggressive', |
| 154 |
'private-to-local', |
| 155 |
'eliminate-local-single-block', |
| 156 |
'eliminate-local-single-store', |
| 157 |
'eliminate-dead-code-aggressive', |
| 158 |
'scalar-replacement=100', |
| 159 |
'convert-local-access-chains', |
| 160 |
'eliminate-local-single-block', |
| 161 |
'eliminate-local-single-store', |
| 162 |
'eliminate-dead-code-aggressive', |
| 163 |
'ssa-rewrite', |
| 164 |
'eliminate-dead-code-aggressive', |
| 165 |
'ccp', |
| 166 |
'eliminate-dead-code-aggressive', |
| 167 |
'loop-unroll', |
| 168 |
'eliminate-dead-branches', |
| 169 |
'redundancy-elimination', |
| 170 |
'combine-access-chains', |
| 171 |
'simplify-instructions', |
| 172 |
'scalar-replacement=100', |
| 173 |
'convert-local-access-chains', |
| 174 |
'eliminate-local-single-block', |
| 175 |
'eliminate-local-single-store', |
| 176 |
'eliminate-dead-code-aggressive', |
| 177 |
'ssa-rewrite', |
| 178 |
'eliminate-dead-code-aggressive', |
| 179 |
'vector-dce', |
| 180 |
'eliminate-dead-inserts', |
| 181 |
'eliminate-dead-branches', |
| 182 |
'simplify-instructions', |
| 183 |
'if-conversion', |
| 184 |
'copy-propagate-arrays', |
| 185 |
'reduce-load-size', |
| 186 |
'eliminate-dead-code-aggressive', |
| 187 |
'merge-blocks', |
| 188 |
'redundancy-elimination', |
| 189 |
'eliminate-dead-branches', |
| 190 |
'merge-blocks', |
| 191 |
'simplify-instructions', |
| 192 |
] |
| 193 |
shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm') |
| 194 |
output = placeholder.TempFileName('output.spv') |
| 195 |
spirv_args = [shader, '-o', output, '--print-all'] + flags |
| 196 |
expected_object_filenames = (output) |
| 197 |
|
| 198 |
|
| 199 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 200 |
class TestSizeOptimizationPasses(expect.ValidObjectFile1_5, |
| 201 |
expect.ExecutedListOfPasses): |
| 202 |
"""Tests that spirv-opt schedules all the passes triggered by -Os.""" |
| 203 |
|
| 204 |
flags = ['-Os'] |
| 205 |
expected_passes = [ |
| 206 |
'wrap-opkill', |
| 207 |
'eliminate-dead-branches', |
| 208 |
'merge-return', |
| 209 |
'inline-entry-points-exhaustive', |
| 210 |
'eliminate-dead-functions', |
| 211 |
'private-to-local', |
| 212 |
'scalar-replacement=0', |
| 213 |
'ssa-rewrite', |
| 214 |
'ccp', |
| 215 |
'loop-unroll', |
| 216 |
'eliminate-dead-branches', |
| 217 |
'simplify-instructions', |
| 218 |
'scalar-replacement=0', |
| 219 |
'eliminate-local-single-store', |
| 220 |
'if-conversion', |
| 221 |
'simplify-instructions', |
| 222 |
'eliminate-dead-code-aggressive', |
| 223 |
'eliminate-dead-branches', |
| 224 |
'merge-blocks', |
| 225 |
'convert-local-access-chains', |
| 226 |
'eliminate-local-single-block', |
| 227 |
'eliminate-dead-code-aggressive', |
| 228 |
'copy-propagate-arrays', |
| 229 |
'vector-dce', |
| 230 |
'eliminate-dead-inserts', |
| 231 |
'eliminate-dead-members', |
| 232 |
'eliminate-local-single-store', |
| 233 |
'merge-blocks', |
| 234 |
'ssa-rewrite', |
| 235 |
'redundancy-elimination', |
| 236 |
'simplify-instructions', |
| 237 |
'eliminate-dead-code-aggressive', |
| 238 |
'cfg-cleanup', |
| 239 |
] |
| 240 |
shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm') |
| 241 |
output = placeholder.TempFileName('output.spv') |
| 242 |
spirv_args = [shader, '-o', output, '--print-all'] + flags |
| 243 |
expected_object_filenames = (output) |
| 244 |
|
| 245 |
|
| 246 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 247 |
class TestLegalizationPasses(expect.ValidObjectFile1_5, |
| 248 |
expect.ExecutedListOfPasses): |
| 249 |
"""Tests that spirv-opt schedules all the passes triggered by --legalize-hlsl. |
| 250 |
""" |
| 251 |
|
| 252 |
flags = ['--legalize-hlsl'] |
| 253 |
expected_passes = [ |
| 254 |
'wrap-opkill', |
| 255 |
'eliminate-dead-branches', |
| 256 |
'merge-return', |
| 257 |
'inline-entry-points-exhaustive', |
| 258 |
'eliminate-dead-functions', |
| 259 |
'private-to-local', |
| 260 |
'fix-storage-class', |
| 261 |
'eliminate-local-single-block', |
| 262 |
'eliminate-local-single-store', |
| 263 |
'eliminate-dead-code-aggressive', |
| 264 |
'scalar-replacement=0', |
| 265 |
'eliminate-local-single-block', |
| 266 |
'eliminate-local-single-store', |
| 267 |
'eliminate-dead-code-aggressive', |
| 268 |
'ssa-rewrite', |
| 269 |
'eliminate-dead-code-aggressive', |
| 270 |
'ccp', |
| 271 |
'loop-unroll', |
| 272 |
'eliminate-dead-branches', |
| 273 |
'simplify-instructions', |
| 274 |
'eliminate-dead-code-aggressive', |
| 275 |
'copy-propagate-arrays', |
| 276 |
'vector-dce', |
| 277 |
'eliminate-dead-inserts', |
| 278 |
'reduce-load-size', |
| 279 |
'eliminate-dead-code-aggressive', |
| 280 |
] |
| 281 |
shader = placeholder.FileSPIRVShader(empty_main_assembly(), '.spvasm') |
| 282 |
output = placeholder.TempFileName('output.spv') |
| 283 |
spirv_args = [shader, '-o', output, '--print-all'] + flags |
| 284 |
expected_object_filenames = (output) |
| 285 |
|
| 286 |
|
| 287 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 288 |
class TestScalarReplacementArgsNegative(expect.ErrorMessageSubstr): |
| 289 |
"""Tests invalid arguments to --scalar-replacement.""" |
| 290 |
|
| 291 |
spirv_args = ['--scalar-replacement=-10'] |
| 292 |
expected_error_substr = 'must have no arguments or a non-negative integer argument' |
| 293 |
|
| 294 |
|
| 295 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 296 |
class TestScalarReplacementArgsInvalidNumber(expect.ErrorMessageSubstr): |
| 297 |
"""Tests invalid arguments to --scalar-replacement.""" |
| 298 |
|
| 299 |
spirv_args = ['--scalar-replacement=a10f'] |
| 300 |
expected_error_substr = 'must have no arguments or a non-negative integer argument' |
| 301 |
|
| 302 |
|
| 303 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 304 |
class TestLoopFissionArgsNegative(expect.ErrorMessageSubstr): |
| 305 |
"""Tests invalid arguments to --loop-fission.""" |
| 306 |
|
| 307 |
spirv_args = ['--loop-fission=-10'] |
| 308 |
expected_error_substr = 'must have a positive integer argument' |
| 309 |
|
| 310 |
|
| 311 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 312 |
class TestLoopFissionArgsInvalidNumber(expect.ErrorMessageSubstr): |
| 313 |
"""Tests invalid arguments to --loop-fission.""" |
| 314 |
|
| 315 |
spirv_args = ['--loop-fission=a10f'] |
| 316 |
expected_error_substr = 'must have a positive integer argument' |
| 317 |
|
| 318 |
|
| 319 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 320 |
class TestLoopFusionArgsNegative(expect.ErrorMessageSubstr): |
| 321 |
"""Tests invalid arguments to --loop-fusion.""" |
| 322 |
|
| 323 |
spirv_args = ['--loop-fusion=-10'] |
| 324 |
expected_error_substr = 'must have a positive integer argument' |
| 325 |
|
| 326 |
|
| 327 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 328 |
class TestLoopFusionArgsInvalidNumber(expect.ErrorMessageSubstr): |
| 329 |
"""Tests invalid arguments to --loop-fusion.""" |
| 330 |
|
| 331 |
spirv_args = ['--loop-fusion=a10f'] |
| 332 |
expected_error_substr = 'must have a positive integer argument' |
| 333 |
|
| 334 |
|
| 335 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 336 |
class TestLoopUnrollPartialArgsNegative(expect.ErrorMessageSubstr): |
| 337 |
"""Tests invalid arguments to --loop-unroll-partial.""" |
| 338 |
|
| 339 |
spirv_args = ['--loop-unroll-partial=-10'] |
| 340 |
expected_error_substr = 'must have a positive integer argument' |
| 341 |
|
| 342 |
|
| 343 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 344 |
class TestLoopUnrollPartialArgsInvalidNumber(expect.ErrorMessageSubstr): |
| 345 |
"""Tests invalid arguments to --loop-unroll-partial.""" |
| 346 |
|
| 347 |
spirv_args = ['--loop-unroll-partial=a10f'] |
| 348 |
expected_error_substr = 'must have a positive integer argument' |
| 349 |
|
| 350 |
|
| 351 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 352 |
class TestLoopPeelingThresholdArgsNegative(expect.ErrorMessageSubstr): |
| 353 |
"""Tests invalid arguments to --loop-peeling-threshold.""" |
| 354 |
|
| 355 |
spirv_args = ['--loop-peeling-threshold=-10'] |
| 356 |
expected_error_substr = 'must have a positive integer argument' |
| 357 |
|
| 358 |
|
| 359 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 360 |
class TestLoopPeelingThresholdArgsInvalidNumber(expect.ErrorMessageSubstr): |
| 361 |
"""Tests invalid arguments to --loop-peeling-threshold.""" |
| 362 |
|
| 363 |
spirv_args = ['--loop-peeling-threshold=a10f'] |
| 364 |
expected_error_substr = 'must have a positive integer argument' |
| 365 |
|
| 366 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 367 |
class TestWebGPUToVulkanThenVulkanToWebGPUIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 368 |
"""Tests Vulkan->WebGPU flag cannot be used after WebGPU->Vulkan flag.""" |
| 369 |
|
| 370 |
spirv_args = ['--webgpu-to-vulkan', '--vulkan-to-webgpu'] |
| 371 |
expected_error_substr = 'Cannot use both' |
| 372 |
|
| 373 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 374 |
class TestVulkanToWebGPUThenWebGPUToVulkanIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 375 |
"""Tests WebGPU->Vulkan flag cannot be used after Vulkan->WebGPU flag.""" |
| 376 |
|
| 377 |
spirv_args = ['--vulkan-to-webgpu', '--webgpu-to-vulkan'] |
| 378 |
expected_error_substr = 'Cannot use both' |
| 379 |
|
| 380 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 381 |
class TestTargetEnvThenVulkanToWebGPUIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 382 |
"""Tests Vulkan->WebGPU flag cannot be used after target env flag.""" |
| 383 |
|
| 384 |
spirv_args = ['--target-env=opengl4.0', '--vulkan-to-webgpu'] |
| 385 |
expected_error_substr = 'defines the target environment' |
| 386 |
|
| 387 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 388 |
class TestVulkanToWebGPUThenTargetEnvIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 389 |
"""Tests target env flag cannot be used after Vulkan->WebGPU flag.""" |
| 390 |
|
| 391 |
spirv_args = ['--vulkan-to-webgpu', '--target-env=opengl4.0'] |
| 392 |
expected_error_substr = 'defines the target environment' |
| 393 |
|
| 394 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 395 |
class TestTargetEnvThenWebGPUToVulkanIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 396 |
"""Tests WebGPU->Vulkan flag cannot be used after target env flag.""" |
| 397 |
|
| 398 |
spirv_args = ['--target-env=opengl4.0', '--webgpu-to-vulkan'] |
| 399 |
expected_error_substr = 'defines the target environment' |
| 400 |
|
| 401 |
@inside_spirv_testsuite('SpirvOptFlags') |
| 402 |
class TestWebGPUToVulkanThenTargetEnvIsInvalid(expect.ReturnCodeIsNonZero, expect.ErrorMessageSubstr): |
| 403 |
"""Tests target env flag cannot be used after WebGPU->Vulkan flag.""" |
| 404 |
|
| 405 |
spirv_args = ['--webgpu-to-vulkan', '--target-env=opengl4.0'] |
| 406 |
expected_error_substr = 'defines the target environment' |