forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathbugpoint_gisel_reducer.py
executable file
·146 lines (118 loc) · 3.95 KB
/
bugpoint_gisel_reducer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
"""Reduces GlobalISel failures.
This script is a utility to reduce tests that GlobalISel
fails to compile.
It runs llc to get the error message using a regex and creates
a custom command to check that specific error. Then, it runs bugpoint
with the custom command.
"""
from __future__ importprint_function
importargparse
importre
importsubprocess
importsys
importtempfile
importos
deflog(msg):
print(msg)
defhr():
log('-'*50)
deflog_err(msg):
print('ERROR: {}'.format(msg), file=sys.stderr)
defcheck_path(path):
ifnotos.path.exists(path):
log_err('{} does not exist.'.format(path))
raise
returnpath
defcheck_bin(build_dir, bin_name):
file_name='{}/bin/{}'.format(build_dir, bin_name)
returncheck_path(file_name)
defrun_llc(llc, irfile):
pr=subprocess.Popen([llc,
'-o',
'-',
'-global-isel',
'-pass-remarks-missed=gisel',
irfile],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err=pr.communicate()
res=pr.wait()
ifres==0:
return0
re_err=re.compile(
r'LLVM ERROR: ([a-z\s]+):.*(G_INTRINSIC[_A-Z]* <intrinsic:@[a-zA-Z0-9\.]+>|G_[A-Z_]+)')
match=re_err.match(err)
ifnotmatch:
return0
else:
return [match.group(1), match.group(2)]
defrun_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp, ir_file):
compileCmd='-compile-command={} -c {} {}'.format(
os.path.realpath(__file__), llc_bin, tmp)
pr=subprocess.Popen([bugpoint_bin,
'-compile-custom',
compileCmd,
'-opt-command={}'.format(opt_bin),
ir_file])
res=pr.wait()
ifres!=0:
log_err("Unable to reduce the test.")
raise
defrun_bugpoint_check():
path_to_llc=sys.argv[2]
path_to_err=sys.argv[3]
path_to_ir=sys.argv[4]
withopen(path_to_err, 'r') asf:
err=f.read()
res=run_llc(path_to_llc, path_to_ir)
ifres==0:
return0
log('GlobalISed failed, {}: {}'.format(res[0], res[1]))
ifres!=err.split(';'):
return0
else:
return1
defmain():
# Check if this is called by bugpoint.
iflen(sys.argv) ==5andsys.argv[1] =='-c':
sys.exit(run_bugpoint_check())
# Parse arguments.
parser=argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('BuildDir', help="Path to LLVM build directory")
parser.add_argument('IRFile', help="Path to the input IR file")
args=parser.parse_args()
# Check if the binaries exist.
build_dir=check_path(args.BuildDir)
ir_file=check_path(args.IRFile)
llc_bin=check_bin(build_dir, 'llc')
opt_bin=check_bin(build_dir, 'opt')
bugpoint_bin=check_bin(build_dir, 'bugpoint')
# Run llc to see if GlobalISel fails.
log('Running llc...')
res=run_llc(llc_bin, ir_file)
ifres==0:
log_err("Expected failure")
raise
hr()
log('GlobalISel failed, {}: {}.'.format(res[0], res[1]))
tmp=tempfile.NamedTemporaryFile()
log('Writing error to {} for bugpoint.'.format(tmp.name))
tmp.write(';'.join(res))
tmp.flush()
hr()
# Run bugpoint.
log('Running bugpoint...')
run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp.name, ir_file)
hr()
log('Done!')
hr()
output_file='bugpoint-reduced-simplified.bc'
log('Run llvm-dis to disassemble the output:')
log('$ {}/bin/llvm-dis -o - {}'.format(build_dir, output_file))
log('Run llc to reproduce the problem:')
log('$ {}/bin/llc -o - -global-isel '
'-pass-remarks-missed=gisel {}'.format(build_dir, output_file))
if__name__=='__main__':
main()