Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 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 |
Sanjay Patel | 506fd0d | 2016-03-24 17:30:38 +0000 | [diff] [blame] | 13 | import os # Used to advertise this file's name ("autogenerated_note"). |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 14 | import string |
| 15 | import subprocess |
| 16 | import sys |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 17 | import re |
| 18 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 19 | from UpdateTestChecks import asm, common |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 20 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 21 | ADVERT = '; NOTE: Assertions have been autogenerated by ' |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 22 | |
| 23 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 24 | def 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 Patel | 9db5da2 | 2017-10-24 14:32:52 +0000 | [diff] [blame] | 32 | parser.add_argument( |
Simon Pilgrim | ee76944 | 2018-06-01 13:37:01 +0000 | [diff] [blame] | 33 | '--extra_scrub', action='store_true', |
| 34 | help='Always use additional regex to further reduce diffs between various subtargets') |
Reid Kleckner | 980c4df | 2018-07-23 21:14:35 +0000 | [diff] [blame] | 35 | 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 Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 40 | parser.add_argument('tests', nargs='+') |
| 41 | args = parser.parse_args() |
| 42 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 43 | autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 44 | |
| 45 | for test in args.tests: |
| 46 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 47 | 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] | 48 | with open(test) as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 49 | input_lines = [l.rstrip() for l in f] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 50 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 51 | triple_in_ir = None |
| 52 | for l in input_lines: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 53 | m = common.TRIPLE_IR_RE.match(l) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 54 | if m: |
| 55 | triple_in_ir = m.groups()[0] |
| 56 | break |
| 57 | |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 58 | raw_lines = [m.group(1) |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 59 | 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] | 60 | run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] |
| 61 | for l in raw_lines[1:]: |
Bryant Wong | 507256b | 2016-12-29 20:05:51 +0000 | [diff] [blame] | 62 | if run_lines[-1].endswith("\\"): |
| 63 | run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l |
| 64 | else: |
| 65 | run_lines.append(l) |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 67 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 68 | print('Found %d RUN lines:' % (len(run_lines),), file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 69 | for l in run_lines: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 70 | print(' RUN: ' + l, file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 71 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 72 | run_list = [] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 73 | for l in run_lines: |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 74 | commands = [cmd.strip() for cmd in l.split('|', 1)] |
| 75 | llc_cmd = commands[0] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 76 | |
| 77 | triple_in_cmd = None |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 78 | m = common.TRIPLE_ARG_RE.search(llc_cmd) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 79 | if m: |
| 80 | triple_in_cmd = m.groups()[0] |
| 81 | |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 82 | filecheck_cmd = '' |
| 83 | if len(commands) > 1: |
| 84 | filecheck_cmd = commands[1] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 85 | if not llc_cmd.startswith('llc '): |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 86 | print('WARNING: Skipping non-llc RUN line: ' + l, file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 87 | continue |
| 88 | |
| 89 | if not filecheck_cmd.startswith('FileCheck '): |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 90 | print('WARNING: Skipping non-FileChecked RUN line: ' + l, file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 91 | 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 Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 96 | 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] | 97 | for item in m.group(1).split(',')] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 98 | 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 Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 103 | run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 104 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 105 | func_dict = {} |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 106 | for p in run_list: |
| 107 | prefixes = p[0] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 108 | for prefix in prefixes: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 109 | func_dict.update({prefix: dict()}) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 110 | for prefixes, llc_args, triple_in_cmd in run_list: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 111 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 112 | print('Extracted LLC cmd: llc ' + llc_args, file=sys.stderr) |
| 113 | print('Extracted FileCheck prefixes: ' + str(prefixes), file=sys.stderr) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 114 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 115 | raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 116 | if not (triple_in_cmd or triple_in_ir): |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 117 | print("Cannot find a triple. Assume 'x86'", file=sys.stderr) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 118 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 119 | 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 Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 121 | |
| 122 | is_in_function = False |
| 123 | is_in_function_start = False |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 124 | func_name = None |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 125 | 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] | 126 | if args.verbose: |
Serge Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 127 | print('Rewriting FileCheck prefixes: %s' % (prefix_set,), file=sys.stderr) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 128 | output_lines = [] |
| 129 | output_lines.append(autogenerated_note) |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 130 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 131 | for input_line in input_lines: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 132 | if is_in_function_start: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 133 | if input_line == '': |
| 134 | continue |
| 135 | if input_line.lstrip().startswith(';'): |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 136 | m = common.CHECK_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 137 | if not m or m.group(1) not in prefix_set: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 138 | output_lines.append(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 139 | continue |
| 140 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 141 | # Print out the various check lines here. |
Fangrui Song | 4f0f426 | 2018-02-10 05:01:33 +0000 | [diff] [blame] | 142 | asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 143 | is_in_function_start = False |
| 144 | |
| 145 | if is_in_function: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 146 | if common.should_add_line_to_output(input_line, prefix_set): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 147 | # This input line of the function body will go as-is into the output. |
| 148 | output_lines.append(input_line) |
| 149 | else: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 150 | continue |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 151 | if input_line.strip() == '}': |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 152 | is_in_function = False |
| 153 | continue |
| 154 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 155 | # Discard any previous script advertising. |
| 156 | if input_line.startswith(ADVERT): |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 157 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 158 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 159 | # If it's outside a function, it just gets copied to the output. |
| 160 | output_lines.append(input_line) |
| 161 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 162 | m = common.IR_FUNCTION_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 163 | if not m: |
| 164 | continue |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 165 | func_name = m.group(1) |
| 166 | if args.function is not None and func_name != args.function: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 167 | # 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 Guelton | 4a27478 | 2019-01-03 14:11:33 +0000 | [diff] [blame] | 172 | 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] | 173 | |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 174 | with open(test, 'wb') as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 175 | f.writelines([l + '\n' for l in output_lines]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 176 | |
| 177 | |
| 178 | if __name__ == '__main__': |
| 179 | main() |