blob: 0936a449c246ced6da383e69207e145d02617117 [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
Fangrui Songee4e2e72018-01-30 00:40:05 +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')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000041 parser.add_argument('tests', nargs='+')
42 args = parser.parse_args()
43
Fangrui Songee4e2e72018-01-30 00:40:05 +000044 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000045
Simon Pilgrimf509fe42019-03-05 10:44:37 +000046 test_paths = [test for pattern in args.tests for test in glob.glob(pattern)]
47 for test in test_paths:
Chandler Carruth06a5dd62015-01-12 04:43:18 +000048 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000049 print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000050 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000051 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000052
Tim Shen53ddc1d2016-12-22 20:59:39 +000053 triple_in_ir = None
54 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000055 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000056 if m:
57 triple_in_ir = m.groups()[0]
58 break
59
Bryant Wong291264b2016-12-29 19:32:34 +000060 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000061 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000062 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
63 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000064 if run_lines[-1].endswith("\\"):
65 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
66 else:
67 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000068
Chandler Carruth06a5dd62015-01-12 04:43:18 +000069 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000070 print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000071 for l in run_lines:
Serge Guelton4a274782019-01-03 14:11:33 +000072 print(' RUN: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000073
Tim Shen53ddc1d2016-12-22 20:59:39 +000074 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000075 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +000076 commands = [cmd.strip() for cmd in l.split('|', 1)]
77 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000078
79 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000080 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000081 if m:
82 triple_in_cmd = m.groups()[0]
83
Roman Lebedev98092f32019-05-18 13:00:03 +000084 march_in_cmd = None
85 m = common.MARCH_ARG_RE.search(llc_cmd)
86 if m:
87 march_in_cmd = m.groups()[0]
88
Zvi Rackover35a5acf2016-11-07 17:47:21 +000089 filecheck_cmd = ''
90 if len(commands) > 1:
91 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000092 if not llc_cmd.startswith('llc '):
Serge Guelton4a274782019-01-03 14:11:33 +000093 print('WARNING: Skipping non-llc RUN line: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000094 continue
95
96 if not filecheck_cmd.startswith('FileCheck '):
Serge Guelton4a274782019-01-03 14:11:33 +000097 print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000098 continue
99
100 llc_cmd_args = llc_cmd[len('llc'):].strip()
101 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
102
Fangrui Songee4e2e72018-01-30 00:40:05 +0000103 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000104 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000105 if not check_prefixes:
106 check_prefixes = ['CHECK']
107
108 # FIXME: We should use multiple check prefixes to common check lines. For
109 # now, we just ignore all but the last.
Roman Lebedev98092f32019-05-18 13:00:03 +0000110 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd, march_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000111
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000112 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000113 for p in run_list:
114 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000115 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000116 func_dict.update({prefix: dict()})
Roman Lebedev98092f32019-05-18 13:00:03 +0000117 for prefixes, llc_args, triple_in_cmd, march_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000118 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000119 print('Extracted LLC cmd: llc ' + llc_args, file=sys.stderr)
120 print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000121
Fangrui Songee4e2e72018-01-30 00:40:05 +0000122 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Roman Lebedev98092f32019-05-18 13:00:03 +0000123 triple = triple_in_cmd or triple_in_ir
124 if not triple:
125 triple = asm.get_triple_from_march(march_in_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000126
Fangrui Songee4e2e72018-01-30 00:40:05 +0000127 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
Roman Lebedev98092f32019-05-18 13:00:03 +0000128 triple, prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000129
130 is_in_function = False
131 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000132 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000133 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000134 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000135 print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000136 output_lines = []
137 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000138
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000139 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000140 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000141 if input_line == '':
142 continue
143 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000144 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000145 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000146 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000147 continue
148
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000149 # Print out the various check lines here.
Fangrui Song4f0f4262018-02-10 05:01:33 +0000150 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000151 is_in_function_start = False
152
153 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000154 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000155 # This input line of the function body will go as-is into the output.
156 output_lines.append(input_line)
157 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000158 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000159 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000160 is_in_function = False
161 continue
162
Fangrui Songee4e2e72018-01-30 00:40:05 +0000163 # Discard any previous script advertising.
164 if input_line.startswith(ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000165 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000166
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000167 # If it's outside a function, it just gets copied to the output.
168 output_lines.append(input_line)
169
Fangrui Songee4e2e72018-01-30 00:40:05 +0000170 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000171 if not m:
172 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000173 func_name = m.group(1)
174 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000175 # When filtering on a specific function, skip all others.
176 continue
177 is_in_function = is_in_function_start = True
178
179 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000180 print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000181
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000182 with open(test, 'wb') as f:
Simon Pilgrimd82bd4d2019-01-30 16:15:59 +0000183 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000184
185
186if __name__ == '__main__':
187 main()