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