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