blob: b8248270b1ed49c43e6ee2c756004e8789106556 [file] [log] [blame]
Chandler Carruth06a5dd62015-01-12 04:43:18 +00001#!/usr/bin/env python2.7
2
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
10import argparse
Sanjay Patel506fd0d2016-03-24 17:30:38 +000011import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import string
13import subprocess
14import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000015import re
16
Fangrui Songee4e2e72018-01-30 00:40:05 +000017from UpdateTestChecks import asm, common
Chandler Carruth06a5dd62015-01-12 04:43:18 +000018
Fangrui Songee4e2e72018-01-30 00:40:05 +000019ADVERT = '; NOTE: Assertions have been autogenerated by '
Sanjay Patelf3c5f462016-03-24 17:15:42 +000020
21
Tim Shen53ddc1d2016-12-22 20:59:39 +000022def add_checks(output_lines, run_list, func_dict, func_name):
Sanjay Patelf3c5f462016-03-24 17:15:42 +000023 printed_prefixes = []
Tim Shen53ddc1d2016-12-22 20:59:39 +000024 for p in run_list:
25 checkprefixes = p[0]
Sanjay Patelf3c5f462016-03-24 17:15:42 +000026 for checkprefix in checkprefixes:
27 if checkprefix in printed_prefixes:
28 break
29 if not func_dict[checkprefix][func_name]:
30 continue
31 # Add some space between different check prefixes.
32 if len(printed_prefixes) != 0:
33 output_lines.append(';')
34 printed_prefixes.append(checkprefix)
35 output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name))
36 func_body = func_dict[checkprefix][func_name].splitlines()
37 output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
38 for func_line in func_body[1:]:
39 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
40 # Add space between different check prefixes and the first line of code.
41 # output_lines.append(';')
42 break
43 return output_lines
44
45
Chandler Carruth06a5dd62015-01-12 04:43:18 +000046def main():
47 parser = argparse.ArgumentParser(description=__doc__)
48 parser.add_argument('-v', '--verbose', action='store_true',
49 help='Show verbose output')
50 parser.add_argument('--llc-binary', default='llc',
51 help='The "llc" binary to use to generate the test case')
52 parser.add_argument(
53 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +000054 parser.add_argument(
55 '--x86_extra_scrub', action='store_true',
56 help='Use more regex for x86 matching to reduce diffs between various subtargets')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000057 parser.add_argument('tests', nargs='+')
58 args = parser.parse_args()
59
Fangrui Songee4e2e72018-01-30 00:40:05 +000060 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000061
62 for test in args.tests:
63 if args.verbose:
64 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
65 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000066 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000067
Tim Shen53ddc1d2016-12-22 20:59:39 +000068 triple_in_ir = None
69 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000070 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000071 if m:
72 triple_in_ir = m.groups()[0]
73 break
74
Bryant Wong291264b2016-12-29 19:32:34 +000075 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000076 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000077 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
78 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000079 if run_lines[-1].endswith("\\"):
80 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
81 else:
82 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000083
Chandler Carruth06a5dd62015-01-12 04:43:18 +000084 if args.verbose:
85 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
86 for l in run_lines:
87 print >>sys.stderr, ' RUN: ' + l
88
Tim Shen53ddc1d2016-12-22 20:59:39 +000089 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000090 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +000091 commands = [cmd.strip() for cmd in l.split('|', 1)]
92 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000093
94 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000095 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000096 if m:
97 triple_in_cmd = m.groups()[0]
98
Zvi Rackover35a5acf2016-11-07 17:47:21 +000099 filecheck_cmd = ''
100 if len(commands) > 1:
101 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000102 if not llc_cmd.startswith('llc '):
103 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
104 continue
105
106 if not filecheck_cmd.startswith('FileCheck '):
107 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
108 continue
109
110 llc_cmd_args = llc_cmd[len('llc'):].strip()
111 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
112
Fangrui Songee4e2e72018-01-30 00:40:05 +0000113 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000114 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000115 if not check_prefixes:
116 check_prefixes = ['CHECK']
117
118 # FIXME: We should use multiple check prefixes to common check lines. For
119 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000120 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000121
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000122 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000123 for p in run_list:
124 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000125 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000126 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000127 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000128 if args.verbose:
129 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
130 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000131
Fangrui Songee4e2e72018-01-30 00:40:05 +0000132 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000133 if not (triple_in_cmd or triple_in_ir):
134 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
135
Fangrui Songee4e2e72018-01-30 00:40:05 +0000136 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
137 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000138
139 is_in_function = False
140 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000141 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000142 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000143 if args.verbose:
144 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000145 output_lines = []
146 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000147
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000148 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000149 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000150 if input_line == '':
151 continue
152 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000153 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000154 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000155 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000156 continue
157
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000158 # Print out the various check lines here.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000159 output_lines = add_checks(output_lines, run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000160 is_in_function_start = False
161
162 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000163 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000164 # This input line of the function body will go as-is into the output.
165 output_lines.append(input_line)
166 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000167 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000168 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000169 is_in_function = False
170 continue
171
Fangrui Songee4e2e72018-01-30 00:40:05 +0000172 # Discard any previous script advertising.
173 if input_line.startswith(ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000174 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000175
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000176 # If it's outside a function, it just gets copied to the output.
177 output_lines.append(input_line)
178
Fangrui Songee4e2e72018-01-30 00:40:05 +0000179 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000180 if not m:
181 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000182 func_name = m.group(1)
183 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000184 # When filtering on a specific function, skip all others.
185 continue
186 is_in_function = is_in_function_start = True
187
188 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000189 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
190
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000191 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000192 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000193
194
195if __name__ == '__main__':
196 main()