Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright 2021 The Tint Authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Extract SPIR-V assembly dumps from the output of |
| 18 | # tint_unittests --dump-spirv |
| 19 | # Writes each module to a distinct filename, which is a sanitized |
| 20 | # form of the test name, and with a ".spvasm" suffix. |
| 21 | # |
| 22 | # Usage: |
| 23 | # tint_unittests --dump-spirv | python3 extract-spvasm.py |
| 24 | |
| 25 | import sys |
| 26 | import re |
| 27 | |
| 28 | |
| 29 | def extract(): |
| 30 | test_name = '' |
| 31 | in_spirv = False |
| 32 | parts = [] |
| 33 | for line in sys.stdin: |
| 34 | run_match = re.match('\[ RUN\s+\]\s+(\S+)', line) |
| 35 | if run_match: |
| 36 | test_name = run_match.group(1) |
| 37 | test_name = re.sub('[^0-9a-zA-Z]', '_', test_name) + '.spvasm' |
| 38 | elif re.match('BEGIN ConvertedOk', line): |
| 39 | parts = [] |
| 40 | in_spirv = True |
| 41 | elif re.match('END ConvertedOk', line): |
| 42 | with open(test_name, 'w') as f: |
| 43 | f.write('; Test: ' + test_name + '\n') |
| 44 | for l in parts: |
| 45 | f.write(l) |
| 46 | f.close() |
| 47 | elif in_spirv: |
| 48 | parts.append(line) |
| 49 | |
| 50 | |
| 51 | def main(argv): |
| 52 | if '--help' in argv or '-h' in argv: |
| 53 | print( |
| 54 | 'Extract SPIR-V from the output of tint_unittests --dump-spirv\n') |
| 55 | print( |
| 56 | 'Usage:\n tint_unittests --dump-spirv | python3 extract-spvasm.py\n' |
| 57 | ) |
| 58 | print( |
| 59 | 'Writes each module to a distinct filename, which is a sanitized') |
| 60 | print('form of the test name, and with a ".spvasm" suffix.') |
| 61 | return 1 |
| 62 | else: |
| 63 | extract() |
| 64 | return 0 |
| 65 | |
| 66 | |
| 67 | if __name__ == '__main__': |
| 68 | exit(main(sys.argv[1:])) |