test_compact_ids.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # Copyright (c) 2017 Google Inc.
  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. """Tests correctness of opt pass tools/opt --compact-ids."""
  15. from __future__ import print_function
  16. import os.path
  17. import sys
  18. import tempfile
  19. def test_spirv_file(path, temp_dir):
  20. optimized_spv_path = os.path.join(temp_dir, 'optimized.spv')
  21. optimized_dis_path = os.path.join(temp_dir, 'optimized.dis')
  22. converted_spv_path = os.path.join(temp_dir, 'converted.spv')
  23. converted_dis_path = os.path.join(temp_dir, 'converted.dis')
  24. os.system('tools/spirv-opt ' + path + ' -o ' + optimized_spv_path +
  25. ' --compact-ids')
  26. os.system('tools/spirv-dis ' + optimized_spv_path + ' -o ' +
  27. optimized_dis_path)
  28. os.system('tools/spirv-dis ' + path + ' -o ' + converted_dis_path)
  29. os.system('tools/spirv-as ' + converted_dis_path + ' -o ' +
  30. converted_spv_path)
  31. os.system('tools/spirv-dis ' + converted_spv_path + ' -o ' +
  32. converted_dis_path)
  33. with open(converted_dis_path, 'r') as f:
  34. converted_dis = f.readlines()[3:]
  35. with open(optimized_dis_path, 'r') as f:
  36. optimized_dis = f.readlines()[3:]
  37. return converted_dis == optimized_dis
  38. def print_usage():
  39. template= \
  40. """{script} tests correctness of opt pass tools/opt --compact-ids
  41. USAGE: python {script} [<spirv_files>]
  42. Requires tools/spirv-dis, tools/spirv-as and tools/spirv-opt to be in path
  43. (call the script from the SPIRV-Tools build output directory).
  44. TIP: In order to test all .spv files under current dir use
  45. find <path> -name "*.spv" -print0 | xargs -0 -s 2000000 python {script}
  46. """
  47. print(template.format(script=sys.argv[0]));
  48. def main():
  49. if not os.path.isfile('tools/spirv-dis'):
  50. print('error: tools/spirv-dis not found')
  51. print_usage()
  52. exit(1)
  53. if not os.path.isfile('tools/spirv-as'):
  54. print('error: tools/spirv-as not found')
  55. print_usage()
  56. exit(1)
  57. if not os.path.isfile('tools/spirv-opt'):
  58. print('error: tools/spirv-opt not found')
  59. print_usage()
  60. exit(1)
  61. paths = sys.argv[1:]
  62. if not paths:
  63. print_usage()
  64. num_failed = 0
  65. temp_dir = tempfile.mkdtemp()
  66. for path in paths:
  67. success = test_spirv_file(path, temp_dir)
  68. if not success:
  69. print('Test failed for ' + path)
  70. num_failed += 1
  71. print('Tested ' + str(len(paths)) + ' files')
  72. if num_failed:
  73. print(str(num_failed) + ' tests failed')
  74. exit(1)
  75. else:
  76. print('All tests successful')
  77. exit(0)
  78. if __name__ == '__main__':
  79. main()