test_compact_ids.py 2.9 KB

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