Serge Guelton | 16228bc | 2019-01-03 15:44:24 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 2 | |
| 3 | """A test case update script. |
| 4 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 5 | This script is a utility to update LLVM 'llc' based test cases with new |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 6 | FileCheck patterns. It can either update all of the tests in the file or |
| 7 | a single test function. |
| 8 | """ |
| 9 | |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 10 | from __future__ import print_function |
| 11 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 12 | import argparse |
Simon Pilgrim | f509fe4 | 2019-03-05 10:44:37 +0000 | [diff] [blame] | 13 | import glob |
Sanjay Patel | 506fd0d | 2016-03-24 17:30:38 +0000 | [diff] [blame] | 14 | import os # Used to advertise this file's name ("autogenerated_note"). |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 15 | import string |
| 16 | import subprocess |
| 17 | import sys |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 18 | import re |
| 19 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 20 | from UpdateTestChecks import asm, common |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 21 | |
Roman Tereshin | 84c368e | 2019-09-18 23:44:17 +0000 | [diff] [blame] | 22 | ADVERT = ' NOTE: Assertions have been autogenerated by ' |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 23 | |
| 24 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 25 | def 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 Patel | 9db5da2 | 2017-10-24 14:32:52 +0000 | [diff] [blame] | 33 | parser.add_argument( |
Simon Pilgrim | ee76944 | 2018-06-01 13:37:01 +0000 | [diff] [blame] | 34 | '--extra_scrub', action='store_true', |
| 35 | help='Always use additional regex to further reduce diffs between various subtargets') |
Reid Kleckner | 980c4df | 2018-07-23 21:14:35 +0000 | [diff] [blame] | 36 | 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') |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 41 | parser.add_argument('-u', '--update-only', action='store_true', |
| 42 | help='Only update test if it was already autogened') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 43 | parser.add_argument('tests', nargs='+') |
| 44 | args = parser.parse_args() |
| 45 | |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 46 | script_name = os.path.basename(__file__) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 47 | |
Simon Pilgrim | f509fe4 | 2019-03-05 10:44:37 +0000 | [diff] [blame] | 48 | test_paths = [test for pattern in args.tests for test in glob.glob(pattern)] |
| 49 | for test in test_paths: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 50 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 51 | print('Scanning for RUN lines in test file: %s' % (test,), file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 52 | with open(test) as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 53 | input_lines = [l.rstrip() for l in f] |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 54 | |
| 55 | first_line = input_lines[0] if input_lines else "" |
| 56 | if 'autogenerated' in first_line and script_name not in first_line: |
| 57 | common.warn("Skipping test which wasn't autogenerated by " + script_name, test) |
| 58 | continue |
| 59 | |
| 60 | if args.update_only: |
| 61 | if not first_line or 'autogenerated' not in first_line: |
| 62 | common.warn("Skipping test which isn't autogenerated: " + test) |
| 63 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 64 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 65 | triple_in_ir = None |
| 66 | for l in input_lines: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 67 | m = common.TRIPLE_IR_RE.match(l) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 68 | if m: |
| 69 | triple_in_ir = m.groups()[0] |
| 70 | break |
| 71 | |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 72 | raw_lines = [m.group(1) |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 73 | for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m] |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 74 | run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] |
| 75 | for l in raw_lines[1:]: |
Bryant Wong | 507256b | 2016-12-29 20:05:51 +0000 | [diff] [blame] | 76 | if run_lines[-1].endswith("\\"): |
| 77 | run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l |
| 78 | else: |
| 79 | run_lines.append(l) |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 80 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 81 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 82 | print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 83 | for l in run_lines: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 84 | print(' RUN: ' + l, file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 85 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 86 | run_list = [] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 87 | for l in run_lines: |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 88 | if '|' not in l: |
| 89 | common.warn('Skipping unparseable RUN line: ' + l) |
| 90 | continue |
| 91 | |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 92 | commands = [cmd.strip() for cmd in l.split('|', 1)] |
| 93 | llc_cmd = commands[0] |
Roman Tereshin | 73778e9 | 2019-09-18 23:44:16 +0000 | [diff] [blame] | 94 | llc_tool = llc_cmd.split(' ')[0] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 95 | |
| 96 | triple_in_cmd = None |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 97 | m = common.TRIPLE_ARG_RE.search(llc_cmd) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 98 | if m: |
| 99 | triple_in_cmd = m.groups()[0] |
| 100 | |
Roman Lebedev | 98092f3 | 2019-05-18 13:00:03 +0000 | [diff] [blame] | 101 | march_in_cmd = None |
| 102 | m = common.MARCH_ARG_RE.search(llc_cmd) |
| 103 | if m: |
| 104 | march_in_cmd = m.groups()[0] |
| 105 | |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 106 | filecheck_cmd = '' |
| 107 | if len(commands) > 1: |
| 108 | filecheck_cmd = commands[1] |
David Bolvansky | 45be5e4 | 2019-07-29 17:41:00 +0000 | [diff] [blame] | 109 | common.verify_filecheck_prefixes(filecheck_cmd) |
Roman Tereshin | 73778e9 | 2019-09-18 23:44:16 +0000 | [diff] [blame] | 110 | if llc_tool != 'llc': |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 111 | common.warn('Skipping non-llc RUN line: ' + l) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 112 | continue |
| 113 | |
| 114 | if not filecheck_cmd.startswith('FileCheck '): |
David Bolvansky | 7169ea3 | 2019-08-07 14:44:50 +0000 | [diff] [blame] | 115 | common.warn('Skipping non-FileChecked RUN line: ' + l) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 116 | continue |
| 117 | |
Roman Tereshin | 73778e9 | 2019-09-18 23:44:16 +0000 | [diff] [blame] | 118 | llc_cmd_args = llc_cmd[len(llc_tool):].strip() |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 119 | llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() |
Roman Tereshin | 84c368e | 2019-09-18 23:44:17 +0000 | [diff] [blame] | 120 | if test.endswith('.mir'): |
| 121 | llc_cmd_args += ' -x mir' |
| 122 | comment_sym = '#' |
| 123 | check_indent = ' ' |
| 124 | else: |
| 125 | comment_sym = ';' |
| 126 | check_indent = '' |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 127 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 128 | check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd) |
Nikolai Bozhenov | 33ee40e | 2017-01-14 09:39:35 +0000 | [diff] [blame] | 129 | for item in m.group(1).split(',')] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 130 | if not check_prefixes: |
| 131 | check_prefixes = ['CHECK'] |
| 132 | |
| 133 | # FIXME: We should use multiple check prefixes to common check lines. For |
| 134 | # now, we just ignore all but the last. |
Roman Lebedev | 98092f3 | 2019-05-18 13:00:03 +0000 | [diff] [blame] | 135 | run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd, march_in_cmd)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 136 | |
Roman Tereshin | 84c368e | 2019-09-18 23:44:17 +0000 | [diff] [blame] | 137 | autogenerated_note = (comment_sym + ADVERT + 'utils/' + script_name) |
| 138 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 139 | func_dict = {} |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 140 | for p in run_list: |
| 141 | prefixes = p[0] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 142 | for prefix in prefixes: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 143 | func_dict.update({prefix: dict()}) |
Roman Lebedev | 98092f3 | 2019-05-18 13:00:03 +0000 | [diff] [blame] | 144 | for prefixes, llc_args, triple_in_cmd, march_in_cmd in run_list: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 145 | if args.verbose: |
Roman Tereshin | 73778e9 | 2019-09-18 23:44:16 +0000 | [diff] [blame] | 146 | print('Extracted LLC cmd: ' + llc_tool + ' ' + llc_args, file=sys.stderr) |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 147 | print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 148 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 149 | raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test) |
Roman Lebedev | 98092f3 | 2019-05-18 13:00:03 +0000 | [diff] [blame] | 150 | triple = triple_in_cmd or triple_in_ir |
| 151 | if not triple: |
| 152 | triple = asm.get_triple_from_march(march_in_cmd) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 153 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 154 | asm.build_function_body_dictionary_for_triple(args, raw_tool_output, |
Roman Lebedev | 98092f3 | 2019-05-18 13:00:03 +0000 | [diff] [blame] | 155 | triple, prefixes, func_dict) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 156 | |
| 157 | is_in_function = False |
| 158 | is_in_function_start = False |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 159 | func_name = None |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 160 | prefix_set = set([prefix for p in run_list for prefix in p[0]]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 161 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 162 | print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 163 | output_lines = [] |
| 164 | output_lines.append(autogenerated_note) |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 165 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 166 | for input_line in input_lines: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 167 | if is_in_function_start: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 168 | if input_line == '': |
| 169 | continue |
| 170 | if input_line.lstrip().startswith(';'): |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 171 | m = common.CHECK_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 172 | if not m or m.group(1) not in prefix_set: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 173 | output_lines.append(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 174 | continue |
| 175 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 176 | # Print out the various check lines here. |
Roman Tereshin | 84c368e | 2019-09-18 23:44:17 +0000 | [diff] [blame] | 177 | asm.add_asm_checks(output_lines, check_indent + ';', run_list, func_dict, func_name) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 178 | is_in_function_start = False |
| 179 | |
| 180 | if is_in_function: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 181 | if common.should_add_line_to_output(input_line, prefix_set): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 182 | # This input line of the function body will go as-is into the output. |
| 183 | output_lines.append(input_line) |
| 184 | else: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 185 | continue |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 186 | if input_line.strip() == '}': |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 187 | is_in_function = False |
| 188 | continue |
| 189 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 190 | # Discard any previous script advertising. |
Roman Tereshin | 84c368e | 2019-09-18 23:44:17 +0000 | [diff] [blame] | 191 | if input_line.startswith(comment_sym + ADVERT): |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 192 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 193 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 194 | # If it's outside a function, it just gets copied to the output. |
| 195 | output_lines.append(input_line) |
| 196 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 197 | m = common.IR_FUNCTION_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 198 | if not m: |
| 199 | continue |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 200 | func_name = m.group(1) |
| 201 | if args.function is not None and func_name != args.function: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 202 | # When filtering on a specific function, skip all others. |
| 203 | continue |
| 204 | is_in_function = is_in_function_start = True |
| 205 | |
| 206 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 207 | print('Writing %d lines to %s...' % (len(output_lines), test), file=sys.stderr) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 208 | |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 209 | with open(test, 'wb') as f: |
Simon Pilgrim | d82bd4d | 2019-01-30 16:15:59 +0000 | [diff] [blame] | 210 | f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 211 | |
| 212 | |
| 213 | if __name__ == '__main__': |
| 214 | main() |