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 | |
| 5 | This script is a utility to update LLVM X86 'llc' based test cases with new |
| 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 |
| 11 | import itertools |
| 12 | import string |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | import re |
| 17 | |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 18 | # Invoke the tool that is being tested. |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 19 | def llc(args, cmd_args, ir): |
| 20 | with open(ir) as ir_file: |
| 21 | stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args, |
| 22 | shell=True, stdin=ir_file) |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 23 | # Fix line endings to unix CR style. |
| 24 | stdout = stdout.replace('\r\n', '\n') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 25 | return stdout |
| 26 | |
| 27 | |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 28 | # RegEx: this is where the magic happens. |
| 29 | |
| 30 | SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M) |
| 31 | SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M) |
| 32 | SCRUB_X86_SHUFFLES_RE = ( |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 33 | re.compile( |
| 34 | r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$', |
| 35 | flags=re.M)) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 36 | SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') |
| 37 | SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') |
| 38 | SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n') |
| 39 | |
| 40 | RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$') |
| 41 | IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(') |
| 42 | ASM_FUNCTION_RE = re.compile( |
| 43 | r'^_?(?P<f>[^:]+):[ \t]*#+[ \t]*@(?P=f)\n[^:]*?' |
| 44 | r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' |
| 45 | r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)', |
| 46 | flags=(re.M | re.S)) |
| 47 | CHECK_PREFIX_RE = re.compile('--check-prefix=(\S+)') |
| 48 | CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | def scrub_asm(asm): |
| 52 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 53 | # whitespace in place. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 54 | asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 55 | # Expand the tabs used for indentation. |
| 56 | asm = string.expandtabs(asm, 2) |
| 57 | # Detect shuffle asm comments and hide the operands in favor of the comments. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 58 | asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 59 | # Generically match the stack offset of a memory operand. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 60 | asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 61 | # Generically match a RIP-relative memory operand. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 62 | asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 63 | # Strip kill operands inserted into the asm. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 64 | asm = SCRUB_KILL_COMMENT_RE.sub('', asm) |
Chandler Carruth | e375095 | 2015-02-04 10:46:48 +0000 | [diff] [blame] | 65 | # Strip trailing whitespace. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 66 | asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 67 | return asm |
| 68 | |
| 69 | |
| 70 | def main(): |
| 71 | parser = argparse.ArgumentParser(description=__doc__) |
| 72 | parser.add_argument('-v', '--verbose', action='store_true', |
| 73 | help='Show verbose output') |
| 74 | parser.add_argument('--llc-binary', default='llc', |
| 75 | help='The "llc" binary to use to generate the test case') |
| 76 | parser.add_argument( |
| 77 | '--function', help='The function in the test file to update') |
| 78 | parser.add_argument('tests', nargs='+') |
| 79 | args = parser.parse_args() |
| 80 | |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 81 | autogenerated_note = ('; NOTE: Assertions have been autogenerated by ' |
| 82 | 'utils/update_llc_test_checks.py') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 83 | |
| 84 | for test in args.tests: |
| 85 | if args.verbose: |
| 86 | print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,) |
| 87 | with open(test) as f: |
| 88 | test_lines = [l.rstrip() for l in f] |
| 89 | |
| 90 | run_lines = [m.group(1) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 91 | for m in [RUN_LINE_RE.match(l) for l in test_lines] if m] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 92 | if args.verbose: |
| 93 | print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),) |
| 94 | for l in run_lines: |
| 95 | print >>sys.stderr, ' RUN: ' + l |
| 96 | |
| 97 | checks = [] |
| 98 | for l in run_lines: |
| 99 | (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)]) |
| 100 | if not llc_cmd.startswith('llc '): |
| 101 | print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l |
| 102 | continue |
| 103 | |
| 104 | if not filecheck_cmd.startswith('FileCheck '): |
| 105 | print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l |
| 106 | continue |
| 107 | |
| 108 | llc_cmd_args = llc_cmd[len('llc'):].strip() |
| 109 | llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() |
| 110 | |
| 111 | check_prefixes = [m.group(1) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 112 | for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 113 | if not check_prefixes: |
| 114 | check_prefixes = ['CHECK'] |
| 115 | |
| 116 | # FIXME: We should use multiple check prefixes to common check lines. For |
| 117 | # now, we just ignore all but the last. |
| 118 | checks.append((check_prefixes, llc_cmd_args)) |
| 119 | |
| 120 | asm = {} |
| 121 | for prefixes, _ in checks: |
| 122 | for prefix in prefixes: |
| 123 | asm.update({prefix: dict()}) |
| 124 | for prefixes, llc_args in checks: |
| 125 | if args.verbose: |
| 126 | print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args |
| 127 | print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes) |
| 128 | raw_asm = llc(args, llc_args, test) |
| 129 | # Build up a dictionary of all the function bodies. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 130 | for m in ASM_FUNCTION_RE.finditer(raw_asm): |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 131 | if not m: |
| 132 | continue |
| 133 | f = m.group('f') |
| 134 | f_asm = scrub_asm(m.group('body')) |
Chandler Carruth | a4a77ed5 | 2015-02-03 21:26:45 +0000 | [diff] [blame] | 135 | if f.startswith('stress'): |
| 136 | # We only use the last line of the asm for stress tests. |
| 137 | f_asm = '\n'.join(f_asm.splitlines()[-1:]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 138 | if args.verbose: |
| 139 | print >>sys.stderr, 'Processing asm for function: ' + f |
| 140 | for l in f_asm.splitlines(): |
| 141 | print >>sys.stderr, ' ' + l |
| 142 | for prefix in prefixes: |
| 143 | if f in asm[prefix] and asm[prefix][f] != f_asm: |
| 144 | if prefix == prefixes[-1]: |
| 145 | print >>sys.stderr, ('WARNING: Found conflicting asm under the ' |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 146 | 'same prefix: %r!' % (prefix,)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 147 | else: |
| 148 | asm[prefix][f] = None |
| 149 | continue |
| 150 | |
| 151 | asm[prefix][f] = f_asm |
| 152 | |
| 153 | is_in_function = False |
| 154 | is_in_function_start = False |
| 155 | prefix_set = set([prefix for prefixes, _ in checks for prefix in prefixes]) |
| 156 | if args.verbose: |
| 157 | print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,) |
| 158 | fixed_lines = [] |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 159 | fixed_lines.append(autogenerated_note) |
| 160 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 161 | for l in test_lines: |
| 162 | if is_in_function_start: |
| 163 | if l.lstrip().startswith(';'): |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 164 | m = CHECK_RE.match(l) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 165 | if not m or m.group(1) not in prefix_set: |
| 166 | fixed_lines.append(l) |
| 167 | continue |
| 168 | |
| 169 | # Print out the various check lines here |
| 170 | printed_prefixes = [] |
| 171 | for prefixes, _ in checks: |
| 172 | for prefix in prefixes: |
| 173 | if prefix in printed_prefixes: |
| 174 | break |
| 175 | if not asm[prefix][name]: |
| 176 | continue |
| 177 | if len(printed_prefixes) != 0: |
| 178 | fixed_lines.append(';') |
| 179 | printed_prefixes.append(prefix) |
| 180 | fixed_lines.append('; %s-LABEL: %s:' % (prefix, name)) |
| 181 | asm_lines = asm[prefix][name].splitlines() |
| 182 | fixed_lines.append('; %s: %s' % (prefix, asm_lines[0])) |
| 183 | for asm_line in asm_lines[1:]: |
| 184 | fixed_lines.append('; %s-NEXT: %s' % (prefix, asm_line)) |
| 185 | break |
| 186 | is_in_function_start = False |
| 187 | |
| 188 | if is_in_function: |
| 189 | # Skip any blank comment lines in the IR. |
| 190 | if l.strip() == ';': |
| 191 | continue |
| 192 | # And skip any CHECK lines. We'll build our own. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 193 | m = CHECK_RE.match(l) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 194 | if m and m.group(1) in prefix_set: |
| 195 | continue |
| 196 | # Collect the remaining lines in the function body and look for the end |
| 197 | # of the function. |
| 198 | fixed_lines.append(l) |
| 199 | if l.strip() == '}': |
| 200 | is_in_function = False |
| 201 | continue |
| 202 | |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 203 | if l == autogenerated_note: |
| 204 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 205 | fixed_lines.append(l) |
| 206 | |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 207 | m = IR_FUNCTION_RE.match(l) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 208 | if not m: |
| 209 | continue |
| 210 | name = m.group(1) |
| 211 | if args.function is not None and name != args.function: |
| 212 | # When filtering on a specific function, skip all others. |
| 213 | continue |
| 214 | is_in_function = is_in_function_start = True |
| 215 | |
| 216 | if args.verbose: |
| 217 | print>>sys.stderr, 'Writing %d fixed lines to %s...' % ( |
| 218 | len(fixed_lines), test) |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 219 | with open(test, 'wb') as f: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 220 | f.writelines([l + '\n' for l in fixed_lines]) |
| 221 | |
| 222 | |
| 223 | if __name__ == '__main__': |
| 224 | main() |