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