blob: 6fbab0b6a898b4aafd09ede222742a14c8b72950 [file] [log] [blame]
Serge Guelton16228bc2019-01-03 15:44:24 +00001#!/usr/bin/env python
Chandler Carruth06a5dd62015-01-12 04:43:18 +00002
3"""A test case update script.
4
Fangrui Songee4e2e72018-01-30 00:40:05 +00005This script is a utility to update LLVM 'llc' based test cases with new
Chandler Carruth06a5dd62015-01-12 04:43:18 +00006FileCheck patterns. It can either update all of the tests in the file or
7a single test function.
8"""
9
Serge Guelton4a274782019-01-03 14:11:33 +000010from __future__ import print_function
11
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import argparse
Simon Pilgrimf509fe42019-03-05 10:44:37 +000013import glob
Sanjay Patel506fd0d2016-03-24 17:30:38 +000014import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000015import string
16import subprocess
17import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000018import re
19
Fangrui Songee4e2e72018-01-30 00:40:05 +000020from UpdateTestChecks import asm, common
Chandler Carruth06a5dd62015-01-12 04:43:18 +000021
Roman Tereshin84c368e2019-09-18 23:44:17 +000022ADVERT = ' NOTE: Assertions have been autogenerated by '
Sanjay Patelf3c5f462016-03-24 17:15:42 +000023
24
Chandler Carruth06a5dd62015-01-12 04:43:18 +000025def main():
26 parser = argparse.ArgumentParser(description=__doc__)
27 parser.add_argument('-v', '--verbose', action='store_true',
28 help='Show verbose output')
29 parser.add_argument('--llc-binary', default='llc',
30 help='The "llc" binary to use to generate the test case')
31 parser.add_argument(
32 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +000033 parser.add_argument(
Simon Pilgrimee769442018-06-01 13:37:01 +000034 '--extra_scrub', action='store_true',
35 help='Always use additional regex to further reduce diffs between various subtargets')
Reid Kleckner980c4df2018-07-23 21:14:35 +000036 parser.add_argument(
37 '--x86_scrub_rip', action='store_true', default=True,
38 help='Use more regex for x86 matching to reduce diffs between various subtargets')
39 parser.add_argument(
40 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
David Bolvansky7169ea32019-08-07 14:44:50 +000041 parser.add_argument('-u', '--update-only', action='store_true',
42 help='Only update test if it was already autogened')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000043 parser.add_argument('tests', nargs='+')
44 args = parser.parse_args()
45
David Bolvansky7169ea32019-08-07 14:44:50 +000046 script_name = os.path.basename(__file__)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000047
Simon Pilgrimf509fe42019-03-05 10:44:37 +000048 test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
49 for test in test_paths:
Chandler Carruth06a5dd62015-01-12 04:43:18 +000050 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000051 print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000052 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000053 input_lines = [l.rstrip() for l in f]
David Bolvansky7169ea32019-08-07 14:44:50 +000054
55 first_line = input_lines[0] if input_lines else ""
56 if 'autogenerated' in first_line and script_name not in first_line:
57 common.warn("Skipping test which wasn't autogenerated by " + script_name, test)
58 continue
59
60 if args.update_only:
61 if not first_line or 'autogenerated' not in first_line:
62 common.warn("Skipping test which isn't autogenerated: " + test)
63 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +000064
Tim Shen53ddc1d2016-12-22 20:59:39 +000065 triple_in_ir = None
66 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000067 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000068 if m:
69 triple_in_ir = m.groups()[0]
70 break
71
Bryant Wong291264b2016-12-29 19:32:34 +000072 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000073 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000074 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
75 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000076 if run_lines[-1].endswith("\\"):
77 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
78 else:
79 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000080
Chandler Carruth06a5dd62015-01-12 04:43:18 +000081 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000082 print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000083 for l in run_lines:
Serge Guelton4a274782019-01-03 14:11:33 +000084 print(' RUN: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000085
Tim Shen53ddc1d2016-12-22 20:59:39 +000086 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000087 for l in run_lines:
David Bolvansky7169ea32019-08-07 14:44:50 +000088 if '|' not in l:
89 common.warn('Skipping unparseable RUN line: ' + l)
90 continue
91
Zvi Rackover35a5acf2016-11-07 17:47:21 +000092 commands = [cmd.strip() for cmd in l.split('|', 1)]
93 llc_cmd = commands[0]
Roman Tereshin73778e92019-09-18 23:44:16 +000094 llc_tool = llc_cmd.split(' ')[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000095
96 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000097 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000098 if m:
99 triple_in_cmd = m.groups()[0]
100
Roman Lebedev98092f32019-05-18 13:00:03 +0000101 march_in_cmd = None
102 m = common.MARCH_ARG_RE.search(llc_cmd)
103 if m:
104 march_in_cmd = m.groups()[0]
105
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000106 filecheck_cmd = ''
107 if len(commands) > 1:
108 filecheck_cmd = commands[1]
David Bolvansky45be5e42019-07-29 17:41:00 +0000109 common.verify_filecheck_prefixes(filecheck_cmd)
Roman Tereshin73778e92019-09-18 23:44:16 +0000110 if llc_tool != 'llc':
David Bolvansky7169ea32019-08-07 14:44:50 +0000111 common.warn('Skipping non-llc RUN line: ' + l)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000112 continue
113
114 if not filecheck_cmd.startswith('FileCheck '):
David Bolvansky7169ea32019-08-07 14:44:50 +0000115 common.warn('Skipping non-FileChecked RUN line: ' + l)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000116 continue
117
Roman Tereshin73778e92019-09-18 23:44:16 +0000118 llc_cmd_args = llc_cmd[len(llc_tool):].strip()
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000119 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
Roman Tereshin84c368e2019-09-18 23:44:17 +0000120 if test.endswith('.mir'):
121 llc_cmd_args += ' -x mir'
122 comment_sym = '#'
123 check_indent = ' '
124 else:
125 comment_sym = ';'
126 check_indent = ''
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000127
Fangrui Songee4e2e72018-01-30 00:40:05 +0000128 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000129 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000130 if not check_prefixes:
131 check_prefixes = ['CHECK']
132
133 # FIXME: We should use multiple check prefixes to common check lines. For
134 # now, we just ignore all but the last.
Roman Lebedev98092f32019-05-18 13:00:03 +0000135 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd, march_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000136
Roman Tereshin84c368e2019-09-18 23:44:17 +0000137 autogenerated_note = (comment_sym + ADVERT + 'utils/' + script_name)
138
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000139 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000140 for p in run_list:
141 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000142 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000143 func_dict.update({prefix: dict()})
Roman Lebedev98092f32019-05-18 13:00:03 +0000144 for prefixes, llc_args, triple_in_cmd, march_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000145 if args.verbose:
Roman Tereshin73778e92019-09-18 23:44:16 +0000146 print('Extracted LLC cmd: ' + llc_tool + ' ' + llc_args, file=sys.stderr)
Serge Guelton4a274782019-01-03 14:11:33 +0000147 print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000148
Fangrui Songee4e2e72018-01-30 00:40:05 +0000149 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Roman Lebedev98092f32019-05-18 13:00:03 +0000150 triple = triple_in_cmd or triple_in_ir
151 if not triple:
152 triple = asm.get_triple_from_march(march_in_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000153
Fangrui Songee4e2e72018-01-30 00:40:05 +0000154 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
Roman Lebedev98092f32019-05-18 13:00:03 +0000155 triple, prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000156
157 is_in_function = False
158 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000159 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000160 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000161 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000162 print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000163 output_lines = []
164 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000165
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000166 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000167 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000168 if input_line == '':
169 continue
170 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000171 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000172 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000173 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000174 continue
175
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000176 # Print out the various check lines here.
Roman Tereshin84c368e2019-09-18 23:44:17 +0000177 asm.add_asm_checks(output_lines, check_indent + ';', run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000178 is_in_function_start = False
179
180 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000181 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000182 # This input line of the function body will go as-is into the output.
183 output_lines.append(input_line)
184 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000185 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000186 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000187 is_in_function = False
188 continue
189
Fangrui Songee4e2e72018-01-30 00:40:05 +0000190 # Discard any previous script advertising.
Roman Tereshin84c368e2019-09-18 23:44:17 +0000191 if input_line.startswith(comment_sym + ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000192 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000193
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000194 # If it's outside a function, it just gets copied to the output.
195 output_lines.append(input_line)
196
Fangrui Songee4e2e72018-01-30 00:40:05 +0000197 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000198 if not m:
199 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000200 func_name = m.group(1)
201 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000202 # When filtering on a specific function, skip all others.
203 continue
204 is_in_function = is_in_function_start = True
205
206 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000207 print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000208
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000209 with open(test, 'wb') as f:
Simon Pilgrimd82bd4d2019-01-30 16:15:59 +0000210 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000211
212
213if __name__ == '__main__':
214 main()