• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (c) 2017 Google Inc.
3
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Tests correctness of opt pass tools/opt --compact-ids."""
16
17import os.path
18import sys
19import tempfile
20
21def test_spirv_file(path, temp_dir):
22  optimized_spv_path = os.path.join(temp_dir, 'optimized.spv')
23  optimized_dis_path = os.path.join(temp_dir, 'optimized.dis')
24  converted_spv_path = os.path.join(temp_dir, 'converted.spv')
25  converted_dis_path = os.path.join(temp_dir, 'converted.dis')
26
27  os.system('tools/spirv-opt ' + path + ' -o ' + optimized_spv_path +
28            ' --compact-ids')
29  os.system('tools/spirv-dis ' + optimized_spv_path + ' -o ' +
30            optimized_dis_path)
31
32  os.system('tools/spirv-dis ' + path + ' -o ' + converted_dis_path)
33  os.system('tools/spirv-as ' + converted_dis_path + ' -o ' +
34            converted_spv_path)
35  os.system('tools/spirv-dis ' + converted_spv_path + ' -o ' +
36            converted_dis_path)
37
38  with open(converted_dis_path, 'r') as f:
39    converted_dis = f.readlines()[3:]
40
41  with open(optimized_dis_path, 'r') as f:
42    optimized_dis = f.readlines()[3:]
43
44  return converted_dis == optimized_dis
45
46def print_usage():
47  template= \
48"""{script} tests correctness of opt pass tools/opt --compact-ids
49
50USAGE: python {script} [<spirv_files>]
51
52Requires tools/spirv-dis, tools/spirv-as and tools/spirv-opt to be in path
53(call the script from the SPIRV-Tools build output directory).
54
55TIP: In order to test all .spv files under current dir use
56find <path> -name "*.spv" -print0 | xargs -0 -s 2000000 python {script}
57"""
58  print(template.format(script=sys.argv[0]));
59
60def main():
61  if not os.path.isfile('tools/spirv-dis'):
62      print('error: tools/spirv-dis not found')
63      print_usage()
64      exit(1)
65
66  if not os.path.isfile('tools/spirv-as'):
67      print('error: tools/spirv-as not found')
68      print_usage()
69      exit(1)
70
71  if not os.path.isfile('tools/spirv-opt'):
72      print('error: tools/spirv-opt not found')
73      print_usage()
74      exit(1)
75
76  paths = sys.argv[1:]
77  if not paths:
78      print_usage()
79
80  num_failed = 0
81
82  temp_dir = tempfile.mkdtemp()
83
84  for path in paths:
85    success = test_spirv_file(path, temp_dir)
86    if not success:
87      print('Test failed for ' + path)
88      num_failed += 1
89
90  print('Tested ' + str(len(paths)) + ' files')
91
92  if num_failed:
93    print(str(num_failed) + ' tests failed')
94    exit(1)
95  else:
96    print('All tests successful')
97    exit(0)
98
99if __name__ == '__main__':
100  main()
101