blob: 09b49a763b6022eb4d5945b5579ce19411ceb3b0 [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
Chandler Carruth06a5dd62015-01-12 04:43:18 +000022def main():
23 parser = argparse.ArgumentParser(description=__doc__)
24 parser.add_argument('-v', '--verbose', action='store_true',
25 help='Show verbose output')
26 parser.add_argument('--llc-binary', default='llc',
27 help='The "llc" binary to use to generate the test case')
28 parser.add_argument(
29 '--function', help='The function in the test file to update')
Sanjay Patel9db5da22017-10-24 14:32:52 +000030 parser.add_argument(
Simon Pilgrimee769442018-06-01 13:37:01 +000031 '--extra_scrub', action='store_true',
32 help='Always use additional regex to further reduce diffs between various subtargets')
Reid Kleckner980c4df2018-07-23 21:14:35 +000033 parser.add_argument(
34 '--x86_scrub_rip', action='store_true', default=True,
35 help='Use more regex for x86 matching to reduce diffs between various subtargets')
36 parser.add_argument(
37 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000038 parser.add_argument('tests', nargs='+')
39 args = parser.parse_args()
40
Fangrui Songee4e2e72018-01-30 00:40:05 +000041 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000042
43 for test in args.tests:
44 if args.verbose:
45 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
46 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000047 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000048
Tim Shen53ddc1d2016-12-22 20:59:39 +000049 triple_in_ir = None
50 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000051 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000052 if m:
53 triple_in_ir = m.groups()[0]
54 break
55
Bryant Wong291264b2016-12-29 19:32:34 +000056 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000057 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000058 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
59 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000060 if run_lines[-1].endswith("\\"):
61 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
62 else:
63 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000064
Chandler Carruth06a5dd62015-01-12 04:43:18 +000065 if args.verbose:
66 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
67 for l in run_lines:
68 print >>sys.stderr, ' RUN: ' + l
69
Tim Shen53ddc1d2016-12-22 20:59:39 +000070 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000071 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +000072 commands = [cmd.strip() for cmd in l.split('|', 1)]
73 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000074
75 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000076 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000077 if m:
78 triple_in_cmd = m.groups()[0]
79
Zvi Rackover35a5acf2016-11-07 17:47:21 +000080 filecheck_cmd = ''
81 if len(commands) > 1:
82 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000083 if not llc_cmd.startswith('llc '):
84 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
85 continue
86
87 if not filecheck_cmd.startswith('FileCheck '):
88 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
89 continue
90
91 llc_cmd_args = llc_cmd[len('llc'):].strip()
92 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
93
Fangrui Songee4e2e72018-01-30 00:40:05 +000094 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +000095 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000096 if not check_prefixes:
97 check_prefixes = ['CHECK']
98
99 # FIXME: We should use multiple check prefixes to common check lines. For
100 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000101 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000102
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000103 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000104 for p in run_list:
105 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000106 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000107 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000108 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000109 if args.verbose:
110 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
111 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000112
Fangrui Songee4e2e72018-01-30 00:40:05 +0000113 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000114 if not (triple_in_cmd or triple_in_ir):
115 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
116
Fangrui Songee4e2e72018-01-30 00:40:05 +0000117 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
118 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000119
120 is_in_function = False
121 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000122 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000123 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000124 if args.verbose:
125 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000126 output_lines = []
127 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000128
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000129 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000130 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000131 if input_line == '':
132 continue
133 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000134 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000135 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000136 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000137 continue
138
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000139 # Print out the various check lines here.
Fangrui Song4f0f4262018-02-10 05:01:33 +0000140 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000141 is_in_function_start = False
142
143 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000144 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000145 # This input line of the function body will go as-is into the output.
146 output_lines.append(input_line)
147 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000148 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000149 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000150 is_in_function = False
151 continue
152
Fangrui Songee4e2e72018-01-30 00:40:05 +0000153 # Discard any previous script advertising.
154 if input_line.startswith(ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000155 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000156
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000157 # If it's outside a function, it just gets copied to the output.
158 output_lines.append(input_line)
159
Fangrui Songee4e2e72018-01-30 00:40:05 +0000160 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000161 if not m:
162 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000163 func_name = m.group(1)
164 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000165 # When filtering on a specific function, skip all others.
166 continue
167 is_in_function = is_in_function_start = True
168
169 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000170 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
171
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000172 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000173 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000174
175
176if __name__ == '__main__':
177 main()