bugpoint_gisel_reducer.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/env python
  2. """Reduces GlobalISel failures.
  3. This script is a utility to reduce tests that GlobalISel
  4. fails to compile.
  5. It runs llc to get the error message using a regex and creates
  6. a custom command to check that specific error. Then, it runs bugpoint
  7. with the custom command.
  8. """
  9. from __future__ import print_function
  10. import argparse
  11. import re
  12. import subprocess
  13. import sys
  14. import tempfile
  15. import os
  16. def log(msg):
  17. print(msg)
  18. def hr():
  19. log('-' * 50)
  20. def log_err(msg):
  21. print('ERROR: {}'.format(msg), file=sys.stderr)
  22. def check_path(path):
  23. if not os.path.exists(path):
  24. log_err('{} does not exist.'.format(path))
  25. raise
  26. return path
  27. def check_bin(build_dir, bin_name):
  28. file_name = '{}/bin/{}'.format(build_dir, bin_name)
  29. return check_path(file_name)
  30. def run_llc(llc, irfile):
  31. pr = subprocess.Popen([llc,
  32. '-o',
  33. '-',
  34. '-global-isel',
  35. '-pass-remarks-missed=gisel',
  36. irfile],
  37. stdout=subprocess.PIPE,
  38. stderr=subprocess.PIPE)
  39. out, err = pr.communicate()
  40. res = pr.wait()
  41. if res == 0:
  42. return 0
  43. re_err = re.compile(
  44. r'LLVM ERROR: ([a-z\s]+):.*(G_INTRINSIC[_A-Z]* <intrinsic:@[a-zA-Z0-9\.]+>|G_[A-Z_]+)')
  45. match = re_err.match(err)
  46. if not match:
  47. return 0
  48. else:
  49. return [match.group(1), match.group(2)]
  50. def run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp, ir_file):
  51. compileCmd = '-compile-command={} -c {} {}'.format(
  52. os.path.realpath(__file__), llc_bin, tmp)
  53. pr = subprocess.Popen([bugpoint_bin,
  54. '-compile-custom',
  55. compileCmd,
  56. '-opt-command={}'.format(opt_bin),
  57. ir_file])
  58. res = pr.wait()
  59. if res != 0:
  60. log_err("Unable to reduce the test.")
  61. raise
  62. def run_bugpoint_check():
  63. path_to_llc = sys.argv[2]
  64. path_to_err = sys.argv[3]
  65. path_to_ir = sys.argv[4]
  66. with open(path_to_err, 'r') as f:
  67. err = f.read()
  68. res = run_llc(path_to_llc, path_to_ir)
  69. if res == 0:
  70. return 0
  71. log('GlobalISed failed, {}: {}'.format(res[0], res[1]))
  72. if res != err.split(';'):
  73. return 0
  74. else:
  75. return 1
  76. def main():
  77. # Check if this is called by bugpoint.
  78. if len(sys.argv) == 5 and sys.argv[1] == '-c':
  79. sys.exit(run_bugpoint_check())
  80. # Parse arguments.
  81. parser = argparse.ArgumentParser(
  82. description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
  83. parser.add_argument('BuildDir', help="Path to LLVM build directory")
  84. parser.add_argument('IRFile', help="Path to the input IR file")
  85. args = parser.parse_args()
  86. # Check if the binaries exist.
  87. build_dir = check_path(args.BuildDir)
  88. ir_file = check_path(args.IRFile)
  89. llc_bin = check_bin(build_dir, 'llc')
  90. opt_bin = check_bin(build_dir, 'opt')
  91. bugpoint_bin = check_bin(build_dir, 'bugpoint')
  92. # Run llc to see if GlobalISel fails.
  93. log('Running llc...')
  94. res = run_llc(llc_bin, ir_file)
  95. if res == 0:
  96. log_err("Expected failure")
  97. raise
  98. hr()
  99. log('GlobalISel failed, {}: {}.'.format(res[0], res[1]))
  100. tmp = tempfile.NamedTemporaryFile()
  101. log('Writing error to {} for bugpoint.'.format(tmp.name))
  102. tmp.write(';'.join(res))
  103. tmp.flush()
  104. hr()
  105. # Run bugpoint.
  106. log('Running bugpoint...')
  107. run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp.name, ir_file)
  108. hr()
  109. log('Done!')
  110. hr()
  111. output_file = 'bugpoint-reduced-simplified.bc'
  112. log('Run llvm-dis to disassemble the output:')
  113. log('$ {}/bin/llvm-dis -o - {}'.format(build_dir, output_file))
  114. log('Run llc to reproduce the problem:')
  115. log('$ {}/bin/llc -o - -global-isel '
  116. '-pass-remarks-missed=gisel {}'.format(build_dir, output_file))
  117. if __name__ == '__main__':
  118. main()