blob: d85fed56c18c41b4ce695a65aa2495041d2a3257 [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
Serge Guelton4a274782019-01-03 14:11:33 +000010from __future__ import print_function
11
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import argparse
Sanjay Patel506fd0d2016-03-24 17:30:38 +000013import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000014import string
15import subprocess
16import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000017import re
18
Fangrui Songee4e2e72018-01-30 00:40:05 +000019from UpdateTestChecks import asm, common
Chandler Carruth06a5dd62015-01-12 04:43:18 +000020
Fangrui Songee4e2e72018-01-30 00:40:05 +000021ADVERT = '; NOTE: Assertions have been autogenerated by '
Sanjay Patelf3c5f462016-03-24 17:15:42 +000022
23
Chandler Carruth06a5dd62015-01-12 04:43:18 +000024def main():
25 parser = argparse.ArgumentParser(description=__doc__)
26 parser.add_argument('-v', '--verbose', action='store_true',
27 help='Show verbose output')
28 parser.add_argument('--llc-binary', default='llc',
29 help='The "llc" binary to use to generate the test case')
30 parser.add_argument(
31 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +000032 parser.add_argument(
Simon Pilgrimee769442018-06-01 13:37:01 +000033 '--extra_scrub', action='store_true',
34 help='Always use additional regex to further reduce diffs between various subtargets')
Reid Kleckner980c4df2018-07-23 21:14:35 +000035 parser.add_argument(
36 '--x86_scrub_rip', action='store_true', default=True,
37 help='Use more regex for x86 matching to reduce diffs between various subtargets')
38 parser.add_argument(
39 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000040 parser.add_argument('tests', nargs='+')
41 args = parser.parse_args()
42
Fangrui Songee4e2e72018-01-30 00:40:05 +000043 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000044
45 for test in args.tests:
46 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000047 print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000048 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000049 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000050
Tim Shen53ddc1d2016-12-22 20:59:39 +000051 triple_in_ir = None
52 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000053 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000054 if m:
55 triple_in_ir = m.groups()[0]
56 break
57
Bryant Wong291264b2016-12-29 19:32:34 +000058 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000059 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000060 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
61 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000062 if run_lines[-1].endswith("\\"):
63 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
64 else:
65 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000066
Chandler Carruth06a5dd62015-01-12 04:43:18 +000067 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +000068 print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000069 for l in run_lines:
Serge Guelton4a274782019-01-03 14:11:33 +000070 print(' RUN: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000071
Tim Shen53ddc1d2016-12-22 20:59:39 +000072 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000073 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +000074 commands = [cmd.strip() for cmd in l.split('|', 1)]
75 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000076
77 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000078 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000079 if m:
80 triple_in_cmd = m.groups()[0]
81
Zvi Rackover35a5acf2016-11-07 17:47:21 +000082 filecheck_cmd = ''
83 if len(commands) > 1:
84 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000085 if not llc_cmd.startswith('llc '):
Serge Guelton4a274782019-01-03 14:11:33 +000086 print('WARNING: Skipping non-llc RUN line: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000087 continue
88
89 if not filecheck_cmd.startswith('FileCheck '):
Serge Guelton4a274782019-01-03 14:11:33 +000090 print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000091 continue
92
93 llc_cmd_args = llc_cmd[len('llc'):].strip()
94 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
95
Fangrui Songee4e2e72018-01-30 00:40:05 +000096 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +000097 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000098 if not check_prefixes:
99 check_prefixes = ['CHECK']
100
101 # FIXME: We should use multiple check prefixes to common check lines. For
102 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000103 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000104
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000105 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000106 for p in run_list:
107 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000108 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000109 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000110 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000111 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000112 print('Extracted LLC cmd: llc ' + llc_args, file=sys.stderr)
113 print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000114
Fangrui Songee4e2e72018-01-30 00:40:05 +0000115 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000116 if not (triple_in_cmd or triple_in_ir):
Serge Guelton4a274782019-01-03 14:11:33 +0000117 print("Cannot find a triple. Assume 'x86'", file=sys.stderr)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000118
Fangrui Songee4e2e72018-01-30 00:40:05 +0000119 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
120 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000121
122 is_in_function = False
123 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000124 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000125 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000126 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000127 print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000128 output_lines = []
129 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000130
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000131 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000132 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000133 if input_line == '':
134 continue
135 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000136 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000137 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000138 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000139 continue
140
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000141 # Print out the various check lines here.
Fangrui Song4f0f4262018-02-10 05:01:33 +0000142 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000143 is_in_function_start = False
144
145 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000146 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000147 # This input line of the function body will go as-is into the output.
148 output_lines.append(input_line)
149 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000150 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000151 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000152 is_in_function = False
153 continue
154
Fangrui Songee4e2e72018-01-30 00:40:05 +0000155 # Discard any previous script advertising.
156 if input_line.startswith(ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000157 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000158
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000159 # If it's outside a function, it just gets copied to the output.
160 output_lines.append(input_line)
161
Fangrui Songee4e2e72018-01-30 00:40:05 +0000162 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000163 if not m:
164 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000165 func_name = m.group(1)
166 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000167 # When filtering on a specific function, skip all others.
168 continue
169 is_in_function = is_in_function_start = True
170
171 if args.verbose:
Serge Guelton4a274782019-01-03 14:11:33 +0000172 print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000173
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000174 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000175 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000176
177
178if __name__ == '__main__':
179 main()