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 |
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 | |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 17 | # Invoke the tool that is being tested. |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 18 | def llc(args, cmd_args, ir): |
| 19 | with open(ir) as ir_file: |
| 20 | stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args, |
| 21 | shell=True, stdin=ir_file) |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 22 | # Fix line endings to unix CR style. |
| 23 | stdout = stdout.replace('\r\n', '\n') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 24 | return stdout |
| 25 | |
| 26 | |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 27 | # RegEx: this is where the magic happens. |
| 28 | |
| 29 | SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M) |
| 30 | SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M) |
Eli Friedman | 1a9a887 | 2016-12-19 23:09:51 +0000 | [diff] [blame] | 31 | SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n') |
| 32 | |
| 33 | ASM_FUNCTION_X86_RE = re.compile( |
| 34 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' |
| 35 | r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' |
| 36 | r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)', |
| 37 | flags=(re.M | re.S)) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 38 | SCRUB_X86_SHUFFLES_RE = ( |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 39 | re.compile( |
Simon Pilgrim | 7c2fbdc | 2016-07-03 13:08:29 +0000 | [diff] [blame] | 40 | r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$', |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 41 | flags=re.M)) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 42 | SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') |
| 43 | SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') |
Simon Pilgrim | 2b7c02a | 2016-06-11 20:39:21 +0000 | [diff] [blame] | 44 | SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') |
Eli Friedman | 1a9a887 | 2016-12-19 23:09:51 +0000 | [diff] [blame] | 45 | |
| 46 | ASM_FUNCTION_ARM_RE = re.compile( |
| 47 | r'^(?P<func>[0-9a-zA-Z_]+):\n' # f: (name of function) |
| 48 | r'\s+\.fnstart\n' # .fnstart |
| 49 | r'(?P<body>.*?)\n' # (body of the function) |
| 50 | r'.Lfunc_end[0-9]+:\n', # .Lfunc_end0: |
| 51 | flags=(re.M | re.S)) |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 52 | |
| 53 | RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$') |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 54 | TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)') |
| 55 | TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$') |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 56 | IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(') |
Nikolai Bozhenov | 33ee40e | 2017-01-14 09:39:35 +0000 | [diff] [blame] | 57 | CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)') |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 58 | CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:') |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 59 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 60 | ASM_FUNCTION_PPC_RE = re.compile( |
| 61 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n' |
| 62 | r'\.Lfunc_begin[0-9]+:\n' |
| 63 | r'[ \t]+.cfi_startproc\n' |
| 64 | r'(?:\.Lfunc_[gl]ep[0-9]+:\n(?:[ \t]+.*?\n)*)*' |
| 65 | r'(?P<body>.*?)\n' |
| 66 | # This list is incomplete |
| 67 | r'(?:^[ \t]*(?:\.long[ \t]+[^\n]+|\.quad[ \t]+[^\n]+)\n)*' |
| 68 | r'.Lfunc_end[0-9]+:\n', |
| 69 | flags=(re.M | re.S)) |
| 70 | |
Jonas Paulsson | f20386d | 2017-03-17 07:11:42 +0000 | [diff] [blame^] | 71 | ASM_FUNCTION_SYSTEMZ_RE = re.compile( |
| 72 | r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n' |
| 73 | r'[ \t]+.cfi_startproc\n' |
| 74 | r'(?P<body>.*?)\n' |
| 75 | r'.Lfunc_end[0-9]+:\n', |
| 76 | flags=(re.M | re.S)) |
| 77 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 78 | |
Eli Friedman | 1a9a887 | 2016-12-19 23:09:51 +0000 | [diff] [blame] | 79 | def scrub_asm_x86(asm): |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 80 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 81 | # whitespace in place. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 82 | asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 83 | # Expand the tabs used for indentation. |
| 84 | asm = string.expandtabs(asm, 2) |
| 85 | # 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] | 86 | asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 87 | # Generically match the stack offset of a memory operand. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 88 | asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 89 | # Generically match a RIP-relative memory operand. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 90 | asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) |
Simon Pilgrim | 2b7c02a | 2016-06-11 20:39:21 +0000 | [diff] [blame] | 91 | # Generically match a LCP symbol. |
| 92 | asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 93 | # Strip kill operands inserted into the asm. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 94 | asm = SCRUB_KILL_COMMENT_RE.sub('', asm) |
Chandler Carruth | e375095 | 2015-02-04 10:46:48 +0000 | [diff] [blame] | 95 | # Strip trailing whitespace. |
Sanjay Patel | bf62301 | 2016-03-23 21:40:53 +0000 | [diff] [blame] | 96 | asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 97 | return asm |
| 98 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 99 | def scrub_asm_arm_eabi(asm): |
Eli Friedman | 1a9a887 | 2016-12-19 23:09:51 +0000 | [diff] [blame] | 100 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 101 | # whitespace in place. |
| 102 | asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 103 | # Expand the tabs used for indentation. |
| 104 | asm = string.expandtabs(asm, 2) |
| 105 | # Strip kill operands inserted into the asm. |
| 106 | asm = SCRUB_KILL_COMMENT_RE.sub('', asm) |
| 107 | # Strip trailing whitespace. |
| 108 | asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 109 | return asm |
| 110 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 111 | def scrub_asm_powerpc64le(asm): |
| 112 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 113 | # whitespace in place. |
| 114 | asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 115 | # Expand the tabs used for indentation. |
| 116 | asm = string.expandtabs(asm, 2) |
| 117 | # Strip trailing whitespace. |
| 118 | asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 119 | return asm |
| 120 | |
Jonas Paulsson | f20386d | 2017-03-17 07:11:42 +0000 | [diff] [blame^] | 121 | def scrub_asm_systemz(asm): |
| 122 | # Scrub runs of whitespace out of the assembly, but leave the leading |
| 123 | # whitespace in place. |
| 124 | asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) |
| 125 | # Expand the tabs used for indentation. |
| 126 | asm = string.expandtabs(asm, 2) |
| 127 | # Strip trailing whitespace. |
| 128 | asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) |
| 129 | return asm |
| 130 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 131 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 132 | # Build up a dictionary of all the function bodies. |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 133 | def build_function_body_dictionary(raw_tool_output, triple, prefixes, func_dict, |
| 134 | verbose): |
| 135 | target_handlers = { |
| 136 | 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 137 | 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 138 | 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 139 | 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE), |
| 140 | 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
Sanjay Patel | 588e415 | 2017-02-24 21:47:44 +0000 | [diff] [blame] | 141 | 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
| 142 | 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
Eli Friedman | 7e0ce82 | 2017-02-24 03:04:11 +0000 | [diff] [blame] | 143 | 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE), |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 144 | 'powerpc64le': (scrub_asm_powerpc64le, ASM_FUNCTION_PPC_RE), |
Jonas Paulsson | f20386d | 2017-03-17 07:11:42 +0000 | [diff] [blame^] | 145 | 's390x': (scrub_asm_systemz, ASM_FUNCTION_SYSTEMZ_RE), |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 146 | } |
| 147 | handlers = None |
| 148 | for prefix, s in target_handlers.items(): |
| 149 | if triple.startswith(prefix): |
| 150 | handlers = s |
| 151 | break |
| 152 | else: |
| 153 | raise KeyError('Triple %r is not supported' % (triple)) |
| 154 | |
| 155 | scrubber, function_re = handlers |
Eli Friedman | 1a9a887 | 2016-12-19 23:09:51 +0000 | [diff] [blame] | 156 | for m in function_re.finditer(raw_tool_output): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 157 | if not m: |
| 158 | continue |
| 159 | func = m.group('func') |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 160 | scrubbed_body = scrubber(m.group('body')) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 161 | if func.startswith('stress'): |
| 162 | # We only use the last line of the function body for stress tests. |
| 163 | scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:]) |
| 164 | if verbose: |
| 165 | print >>sys.stderr, 'Processing function: ' + func |
| 166 | for l in scrubbed_body.splitlines(): |
| 167 | print >>sys.stderr, ' ' + l |
| 168 | for prefix in prefixes: |
| 169 | if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body: |
| 170 | if prefix == prefixes[-1]: |
| 171 | print >>sys.stderr, ('WARNING: Found conflicting asm under the ' |
| 172 | 'same prefix: %r!' % (prefix,)) |
| 173 | else: |
| 174 | func_dict[prefix][func] = None |
| 175 | continue |
| 176 | |
| 177 | func_dict[prefix][func] = scrubbed_body |
| 178 | |
| 179 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 180 | def add_checks(output_lines, run_list, func_dict, func_name): |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 181 | printed_prefixes = [] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 182 | for p in run_list: |
| 183 | checkprefixes = p[0] |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 184 | for checkprefix in checkprefixes: |
| 185 | if checkprefix in printed_prefixes: |
| 186 | break |
| 187 | if not func_dict[checkprefix][func_name]: |
| 188 | continue |
| 189 | # Add some space between different check prefixes. |
| 190 | if len(printed_prefixes) != 0: |
| 191 | output_lines.append(';') |
| 192 | printed_prefixes.append(checkprefix) |
| 193 | output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name)) |
| 194 | func_body = func_dict[checkprefix][func_name].splitlines() |
| 195 | output_lines.append('; %s: %s' % (checkprefix, func_body[0])) |
| 196 | for func_line in func_body[1:]: |
| 197 | output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line)) |
| 198 | # Add space between different check prefixes and the first line of code. |
| 199 | # output_lines.append(';') |
| 200 | break |
| 201 | return output_lines |
| 202 | |
| 203 | |
| 204 | def should_add_line_to_output(input_line, prefix_set): |
| 205 | # Skip any blank comment lines in the IR. |
| 206 | if input_line.strip() == ';': |
| 207 | return False |
| 208 | # Skip any blank lines in the IR. |
| 209 | #if input_line.strip() == '': |
| 210 | # return False |
| 211 | # And skip any CHECK lines. We're building our own. |
| 212 | m = CHECK_RE.match(input_line) |
| 213 | if m and m.group(1) in prefix_set: |
| 214 | return False |
| 215 | |
| 216 | return True |
| 217 | |
| 218 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 219 | def main(): |
| 220 | parser = argparse.ArgumentParser(description=__doc__) |
| 221 | parser.add_argument('-v', '--verbose', action='store_true', |
| 222 | help='Show verbose output') |
| 223 | parser.add_argument('--llc-binary', default='llc', |
| 224 | help='The "llc" binary to use to generate the test case') |
| 225 | parser.add_argument( |
| 226 | '--function', help='The function in the test file to update') |
| 227 | parser.add_argument('tests', nargs='+') |
| 228 | args = parser.parse_args() |
| 229 | |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 230 | autogenerated_note = ('; NOTE: Assertions have been autogenerated by ' |
Simon Pilgrim | 2b7c02a | 2016-06-11 20:39:21 +0000 | [diff] [blame] | 231 | 'utils/' + os.path.basename(__file__)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 232 | |
| 233 | for test in args.tests: |
| 234 | if args.verbose: |
| 235 | print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,) |
| 236 | with open(test) as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 237 | input_lines = [l.rstrip() for l in f] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 238 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 239 | triple_in_ir = None |
| 240 | for l in input_lines: |
| 241 | m = TRIPLE_IR_RE.match(l) |
| 242 | if m: |
| 243 | triple_in_ir = m.groups()[0] |
| 244 | break |
| 245 | |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 246 | raw_lines = [m.group(1) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 247 | for m in [RUN_LINE_RE.match(l) for l in input_lines] if m] |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 248 | run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] |
| 249 | for l in raw_lines[1:]: |
Bryant Wong | 507256b | 2016-12-29 20:05:51 +0000 | [diff] [blame] | 250 | if run_lines[-1].endswith("\\"): |
| 251 | run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l |
| 252 | else: |
| 253 | run_lines.append(l) |
Bryant Wong | 291264b | 2016-12-29 19:32:34 +0000 | [diff] [blame] | 254 | |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 255 | if args.verbose: |
| 256 | print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),) |
| 257 | for l in run_lines: |
| 258 | print >>sys.stderr, ' RUN: ' + l |
| 259 | |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 260 | run_list = [] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 261 | for l in run_lines: |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 262 | commands = [cmd.strip() for cmd in l.split('|', 1)] |
| 263 | llc_cmd = commands[0] |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 264 | |
| 265 | triple_in_cmd = None |
| 266 | m = TRIPLE_ARG_RE.search(llc_cmd) |
| 267 | if m: |
| 268 | triple_in_cmd = m.groups()[0] |
| 269 | |
Zvi Rackover | 35a5acf | 2016-11-07 17:47:21 +0000 | [diff] [blame] | 270 | filecheck_cmd = '' |
| 271 | if len(commands) > 1: |
| 272 | filecheck_cmd = commands[1] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 273 | if not llc_cmd.startswith('llc '): |
| 274 | print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l |
| 275 | continue |
| 276 | |
| 277 | if not filecheck_cmd.startswith('FileCheck '): |
| 278 | print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l |
| 279 | continue |
| 280 | |
| 281 | llc_cmd_args = llc_cmd[len('llc'):].strip() |
| 282 | llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() |
| 283 | |
Nikolai Bozhenov | 33ee40e | 2017-01-14 09:39:35 +0000 | [diff] [blame] | 284 | check_prefixes = [item for m in CHECK_PREFIX_RE.finditer(filecheck_cmd) |
| 285 | for item in m.group(1).split(',')] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 286 | if not check_prefixes: |
| 287 | check_prefixes = ['CHECK'] |
| 288 | |
| 289 | # FIXME: We should use multiple check prefixes to common check lines. For |
| 290 | # now, we just ignore all but the last. |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 291 | run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd)) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 292 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 293 | func_dict = {} |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 294 | for p in run_list: |
| 295 | prefixes = p[0] |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 296 | for prefix in prefixes: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 297 | func_dict.update({prefix: dict()}) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 298 | for prefixes, llc_args, triple_in_cmd in run_list: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 299 | if args.verbose: |
| 300 | print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args |
| 301 | print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 302 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 303 | raw_tool_output = llc(args, llc_args, test) |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 304 | if not (triple_in_cmd or triple_in_ir): |
| 305 | print >>sys.stderr, "Cannot find a triple. Assume 'x86'" |
| 306 | |
| 307 | build_function_body_dictionary(raw_tool_output, |
| 308 | triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict, args.verbose) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 309 | |
| 310 | is_in_function = False |
| 311 | is_in_function_start = False |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 312 | func_name = None |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 313 | 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] | 314 | if args.verbose: |
| 315 | print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,) |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 316 | output_lines = [] |
| 317 | output_lines.append(autogenerated_note) |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 318 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 319 | for input_line in input_lines: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 320 | if is_in_function_start: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 321 | if input_line == '': |
| 322 | continue |
| 323 | if input_line.lstrip().startswith(';'): |
| 324 | m = CHECK_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 325 | if not m or m.group(1) not in prefix_set: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 326 | output_lines.append(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 327 | continue |
| 328 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 329 | # Print out the various check lines here. |
Tim Shen | 53ddc1d | 2016-12-22 20:59:39 +0000 | [diff] [blame] | 330 | output_lines = add_checks(output_lines, run_list, func_dict, func_name) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 331 | is_in_function_start = False |
| 332 | |
| 333 | if is_in_function: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 334 | if should_add_line_to_output(input_line, prefix_set) == True: |
| 335 | # This input line of the function body will go as-is into the output. |
| 336 | output_lines.append(input_line) |
| 337 | else: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 338 | continue |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 339 | if input_line.strip() == '}': |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 340 | is_in_function = False |
| 341 | continue |
| 342 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 343 | if input_line == autogenerated_note: |
James Y Knight | 7c90506 | 2015-11-23 21:33:58 +0000 | [diff] [blame] | 344 | continue |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 345 | |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 346 | # If it's outside a function, it just gets copied to the output. |
| 347 | output_lines.append(input_line) |
| 348 | |
| 349 | m = IR_FUNCTION_RE.match(input_line) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 350 | if not m: |
| 351 | continue |
Zvi Rackover | 18082ab | 2016-11-07 18:08:19 +0000 | [diff] [blame] | 352 | func_name = m.group(1) |
| 353 | if args.function is not None and func_name != args.function: |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 354 | # When filtering on a specific function, skip all others. |
| 355 | continue |
| 356 | is_in_function = is_in_function_start = True |
| 357 | |
| 358 | if args.verbose: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 359 | print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test) |
| 360 | |
Simon Pilgrim | 6b6dcc4 | 2016-01-27 21:13:18 +0000 | [diff] [blame] | 361 | with open(test, 'wb') as f: |
Sanjay Patel | f3c5f46 | 2016-03-24 17:15:42 +0000 | [diff] [blame] | 362 | f.writelines([l + '\n' for l in output_lines]) |
Chandler Carruth | 06a5dd6 | 2015-01-12 04:43:18 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | if __name__ == '__main__': |
| 366 | main() |