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