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