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 | |
| 10 | import argparse |
Sanjay Patel | 506fd0d | 2016-03-24 17:30:38 +0000 | [diff] [blame] | 11 | import os # Used to advertise this file's name ("autogenerated_note"). |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 12 | import string |
| 13 | import subprocess |
| 14 | import sys |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 15 | import re |
| 16 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 17 | from UpdateTestChecks import asm, common |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 18 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 19 | ADVERT = '; NOTE: Assertions have been autogenerated by ' |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 20 | |
| 21 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 22 | def add_checks(output_lines, run_list, func_dict, func_name): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 23 | printed_prefixes = [] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 24 | for p in run_list: |
| 25 | checkprefixes = p[0] |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 26 | for checkprefix in checkprefixes: |
| 27 | if checkprefix in printed_prefixes: |
| 28 | break |
| 29 | if not func_dict[checkprefix][func_name]: |
| 30 | continue |
| 31 | # Add some space between different check prefixes. |
| 32 | if len(printed_prefixes) != 0: |
| 33 | output_lines.append(';') |
| 34 | printed_prefixes.append(checkprefix) |
| 35 | output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name)) |
| 36 | func_body = func_dict[checkprefix][func_name].splitlines() |
| 37 | output_lines.append('; %s: %s' % (checkprefix, func_body[0])) |
| 38 | for func_line in func_body[1:]: |
| 39 | output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line)) |
| 40 | # Add space between different check prefixes and the first line of code. |
| 41 | # output_lines.append(';') |
| 42 | break |
| 43 | return output_lines |
| 44 | |
| 45 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 46 | def main(): |
| 47 | parser = argparse.ArgumentParser(description=__doc__) |
| 48 | parser.add_argument('-v', '--verbose', action='store_true', |
| 49 | help='Show verbose output') |
| 50 | parser.add_argument('--llc-binary', default='llc', |
| 51 | help='The "llc" binary to use to generate the test case') |
| 52 | parser.add_argument( |
| 53 | '--function', help='The function in the test file to update') |
Sanjay Patel | 9db5da2 | 2017-10-24 14:32:52 +0000 | [diff] [blame] | 54 | parser.add_argument( |
| 55 | '--x86_extra_scrub', action='store_true', |
| 56 | help='Use more regex for x86 matching to reduce diffs between various subtargets') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 57 | parser.add_argument('tests', nargs='+') |
| 58 | args = parser.parse_args() |
| 59 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 60 | autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 61 | |
| 62 | for test in args.tests: |
| 63 | if args.verbose: |
| 64 | print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,) |
| 65 | with open(test) as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 66 | input_lines = [l.rstrip() for l in f] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 67 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 68 | triple_in_ir = None |
| 69 | for l in input_lines: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 70 | m = common.TRIPLE_IR_RE.match(l) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 71 | if m: |
| 72 | triple_in_ir = m.groups()[0] |
| 73 | break |
| 74 | |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 75 | raw_lines = [m.group(1) |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 76 | 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] | 77 | run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] |
| 78 | for l in raw_lines[1:]: |
Bryant Wong | 507256b | 2016-12-29 20:05:51 +0000 | [diff] [blame] | 79 | if run_lines[-1].endswith("\\"): |
| 80 | run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l |
| 81 | else: |
| 82 | run_lines.append(l) |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 83 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 84 | if args.verbose: |
| 85 | print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),) |
| 86 | for l in run_lines: |
| 87 | print >>sys.stderr, ' RUN: ' + l |
| 88 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 89 | run_list = [] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 90 | for l in run_lines: |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 91 | commands = [cmd.strip() for cmd in l.split('|', 1)] |
| 92 | llc_cmd = commands[0] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 93 | |
| 94 | triple_in_cmd = None |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 95 | m = common.TRIPLE_ARG_RE.search(llc_cmd) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 96 | if m: |
| 97 | triple_in_cmd = m.groups()[0] |
| 98 | |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 99 | filecheck_cmd = '' |
| 100 | if len(commands) > 1: |
| 101 | filecheck_cmd = commands[1] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 102 | if not llc_cmd.startswith('llc '): |
| 103 | print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l |
| 104 | continue |
| 105 | |
| 106 | if not filecheck_cmd.startswith('FileCheck '): |
| 107 | print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l |
| 108 | continue |
| 109 | |
| 110 | llc_cmd_args = llc_cmd[len('llc'):].strip() |
| 111 | llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() |
| 112 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 113 | 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] | 114 | for item in m.group(1).split(',')] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 115 | if not check_prefixes: |
| 116 | check_prefixes = ['CHECK'] |
| 117 | |
| 118 | # FIXME: We should use multiple check prefixes to common check lines. For |
| 119 | # now, we just ignore all but the last. |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 120 | run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 121 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 122 | func_dict = {} |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 123 | for p in run_list: |
| 124 | prefixes = p[0] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 125 | for prefix in prefixes: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 126 | func_dict.update({prefix: dict()}) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 127 | for prefixes, llc_args, triple_in_cmd in run_list: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 128 | if args.verbose: |
| 129 | print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args |
| 130 | print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 131 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 132 | raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 133 | if not (triple_in_cmd or triple_in_ir): |
| 134 | print >>sys.stderr, "Cannot find a triple. Assume 'x86'" |
| 135 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 136 | asm.build_function_body_dictionary_for_triple(args, raw_tool_output, |
| 137 | triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 138 | |
| 139 | is_in_function = False |
| 140 | is_in_function_start = False |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 141 | func_name = None |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 142 | 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] | 143 | if args.verbose: |
| 144 | print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 145 | output_lines = [] |
| 146 | output_lines.append(autogenerated_note) |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 147 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 148 | for input_line in input_lines: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 149 | if is_in_function_start: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 150 | if input_line == '': |
| 151 | continue |
| 152 | if input_line.lstrip().startswith(';'): |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 153 | m = common.CHECK_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 154 | if not m or m.group(1) not in prefix_set: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 155 | output_lines.append(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 156 | continue |
| 157 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 158 | # Print out the various check lines here. |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 159 | output_lines = add_checks(output_lines, run_list, func_dict, func_name) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 160 | is_in_function_start = False |
| 161 | |
| 162 | if is_in_function: |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 163 | if common.should_add_line_to_output(input_line, prefix_set): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 164 | # This input line of the function body will go as-is into the output. |
| 165 | output_lines.append(input_line) |
| 166 | else: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 167 | continue |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 168 | if input_line.strip() == '}': |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 169 | is_in_function = False |
| 170 | continue |
| 171 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 172 | # Discard any previous script advertising. |
| 173 | if input_line.startswith(ADVERT): |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 174 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 175 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 176 | # If it's outside a function, it just gets copied to the output. |
| 177 | output_lines.append(input_line) |
| 178 | |
Fangrui Song | ee4e2e7 | 2018-01-30 00:40:05 +0000 | [diff] [blame] | 179 | m = common.IR_FUNCTION_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 180 | if not m: |
| 181 | continue |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 182 | func_name = m.group(1) |
| 183 | if args.function is not None and func_name != args.function: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 184 | # When filtering on a specific function, skip all others. |
| 185 | continue |
| 186 | is_in_function = is_in_function_start = True |
| 187 | |
| 188 | if args.verbose: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 189 | print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test) |
| 190 | |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 191 | with open(test, 'wb') as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 192 | f.writelines([l + '\n' for l in output_lines]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 193 | |
| 194 | |
| 195 | if __name__ == '__main__': |
| 196 | main() |