blob: dd7a1d270056f3eb8f01a70e2f5fb0e950143d13 [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(
31 '--x86_extra_scrub', action='store_true',
32 help='Use more regex for x86 matching to reduce diffs between various subtargets')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000033 parser.add_argument('tests', nargs='+')
34 args = parser.parse_args()
35
Fangrui Songee4e2e72018-01-30 00:40:05 +000036 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000037
38 for test in args.tests:
39 if args.verbose:
40 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
41 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +000042 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000043
Tim Shen53ddc1d2016-12-22 20:59:39 +000044 triple_in_ir = None
45 for l in input_lines:
Fangrui Songee4e2e72018-01-30 00:40:05 +000046 m = common.TRIPLE_IR_RE.match(l)
Tim Shen53ddc1d2016-12-22 20:59:39 +000047 if m:
48 triple_in_ir = m.groups()[0]
49 break
50
Bryant Wong291264b2016-12-29 19:32:34 +000051 raw_lines = [m.group(1)
Fangrui Songee4e2e72018-01-30 00:40:05 +000052 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +000053 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
54 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +000055 if run_lines[-1].endswith("\\"):
56 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
57 else:
58 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +000059
Chandler Carruth06a5dd62015-01-12 04:43:18 +000060 if args.verbose:
61 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
62 for l in run_lines:
63 print >>sys.stderr, ' RUN: ' + l
64
Tim Shen53ddc1d2016-12-22 20:59:39 +000065 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +000066 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +000067 commands = [cmd.strip() for cmd in l.split('|', 1)]
68 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +000069
70 triple_in_cmd = None
Fangrui Songee4e2e72018-01-30 00:40:05 +000071 m = common.TRIPLE_ARG_RE.search(llc_cmd)
Tim Shen53ddc1d2016-12-22 20:59:39 +000072 if m:
73 triple_in_cmd = m.groups()[0]
74
Zvi Rackover35a5acf2016-11-07 17:47:21 +000075 filecheck_cmd = ''
76 if len(commands) > 1:
77 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000078 if not llc_cmd.startswith('llc '):
79 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
80 continue
81
82 if not filecheck_cmd.startswith('FileCheck '):
83 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
84 continue
85
86 llc_cmd_args = llc_cmd[len('llc'):].strip()
87 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
88
Fangrui Songee4e2e72018-01-30 00:40:05 +000089 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +000090 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000091 if not check_prefixes:
92 check_prefixes = ['CHECK']
93
94 # FIXME: We should use multiple check prefixes to common check lines. For
95 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +000096 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +000097
Sanjay Patelf3c5f462016-03-24 17:15:42 +000098 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +000099 for p in run_list:
100 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000101 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000102 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000103 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000104 if args.verbose:
105 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
106 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000107
Fangrui Songee4e2e72018-01-30 00:40:05 +0000108 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000109 if not (triple_in_cmd or triple_in_ir):
110 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
111
Fangrui Songee4e2e72018-01-30 00:40:05 +0000112 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
113 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000114
115 is_in_function = False
116 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000117 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000118 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000119 if args.verbose:
120 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000121 output_lines = []
122 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000123
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000124 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000125 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000126 if input_line == '':
127 continue
128 if input_line.lstrip().startswith(';'):
Fangrui Songee4e2e72018-01-30 00:40:05 +0000129 m = common.CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000130 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000131 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000132 continue
133
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000134 # Print out the various check lines here.
Fangrui Song4f0f4262018-02-10 05:01:33 +0000135 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000136 is_in_function_start = False
137
138 if is_in_function:
Fangrui Songee4e2e72018-01-30 00:40:05 +0000139 if common.should_add_line_to_output(input_line, prefix_set):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000140 # This input line of the function body will go as-is into the output.
141 output_lines.append(input_line)
142 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000143 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000144 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000145 is_in_function = False
146 continue
147
Fangrui Songee4e2e72018-01-30 00:40:05 +0000148 # Discard any previous script advertising.
149 if input_line.startswith(ADVERT):
James Y Knight7c905062015-11-23 21:33:58 +0000150 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000151
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000152 # If it's outside a function, it just gets copied to the output.
153 output_lines.append(input_line)
154
Fangrui Songee4e2e72018-01-30 00:40:05 +0000155 m = common.IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000156 if not m:
157 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000158 func_name = m.group(1)
159 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000160 # When filtering on a specific function, skip all others.
161 continue
162 is_in_function = is_in_function_start = True
163
164 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000165 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
166
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000167 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000168 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000169
170
171if __name__ == '__main__':
172 main()