blob: e973d517c44a540ee786fa2b865768affee4d733 [file] [log] [blame]
Chandler Carruth06a5dd62015-01-12 04:43:18 +00001#!/usr/bin/env python2.7
2
3"""A test case update script.
4
5This script is a utility to update LLVM X86 'llc' based test cases with new
6FileCheck patterns. It can either update all of the tests in the file or
7a single test function.
8"""
9
10import argparse
Sanjay Patel506fd0d2016-03-24 17:30:38 +000011import os # Used to advertise this file's name ("autogenerated_note").
Chandler Carruth06a5dd62015-01-12 04:43:18 +000012import string
13import subprocess
14import sys
Chandler Carruth06a5dd62015-01-12 04:43:18 +000015import re
16
Sanjay Patelbf623012016-03-23 21:40:53 +000017# Invoke the tool that is being tested.
Chandler Carruth06a5dd62015-01-12 04:43:18 +000018def 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 Pilgrim6b6dcc42016-01-27 21:13:18 +000022 # Fix line endings to unix CR style.
23 stdout = stdout.replace('\r\n', '\n')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000024 return stdout
25
26
Sanjay Patelbf623012016-03-23 21:40:53 +000027# RegEx: this is where the magic happens.
28
29SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
30SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
Eli Friedman1a9a8872016-12-19 23:09:51 +000031SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
32
33ASM_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 Patelbf623012016-03-23 21:40:53 +000038SCRUB_X86_SHUFFLES_RE = (
Chandler Carruth06a5dd62015-01-12 04:43:18 +000039 re.compile(
Simon Pilgrim7c2fbdc2016-07-03 13:08:29 +000040 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$',
Chandler Carruth06a5dd62015-01-12 04:43:18 +000041 flags=re.M))
Sanjay Patelbf623012016-03-23 21:40:53 +000042SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
43SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +000044SCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+')
Eli Friedman1a9a8872016-12-19 23:09:51 +000045
46ASM_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 Patelbf623012016-03-23 21:40:53 +000052
53RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
Tim Shen53ddc1d2016-12-22 20:59:39 +000054TRIPLE_ARG_RE = re.compile(r'-mtriple=([^ ]+)')
55TRIPLE_IR_RE = re.compile(r'^target\s+triple\s*=\s*"([^"]+)"$')
Sanjay Patelbf623012016-03-23 21:40:53 +000056IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +000057CHECK_PREFIX_RE = re.compile('--?check-prefix(?:es)?=(\S+)')
Sanjay Patelbf623012016-03-23 21:40:53 +000058CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000059
Tim Shen53ddc1d2016-12-22 20:59:39 +000060ASM_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
Chandler Carruth06a5dd62015-01-12 04:43:18 +000071
Eli Friedman1a9a8872016-12-19 23:09:51 +000072def scrub_asm_x86(asm):
Chandler Carruth06a5dd62015-01-12 04:43:18 +000073 # Scrub runs of whitespace out of the assembly, but leave the leading
74 # whitespace in place.
Sanjay Patelbf623012016-03-23 21:40:53 +000075 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000076 # Expand the tabs used for indentation.
77 asm = string.expandtabs(asm, 2)
78 # Detect shuffle asm comments and hide the operands in favor of the comments.
Sanjay Patelbf623012016-03-23 21:40:53 +000079 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000080 # Generically match the stack offset of a memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000081 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000082 # Generically match a RIP-relative memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000083 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +000084 # Generically match a LCP symbol.
85 asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000086 # Strip kill operands inserted into the asm.
Sanjay Patelbf623012016-03-23 21:40:53 +000087 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
Chandler Carruthe3750952015-02-04 10:46:48 +000088 # Strip trailing whitespace.
Sanjay Patelbf623012016-03-23 21:40:53 +000089 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000090 return asm
91
Tim Shen53ddc1d2016-12-22 20:59:39 +000092def scrub_asm_arm_eabi(asm):
Eli Friedman1a9a8872016-12-19 23:09:51 +000093 # Scrub runs of whitespace out of the assembly, but leave the leading
94 # whitespace in place.
95 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
96 # Expand the tabs used for indentation.
97 asm = string.expandtabs(asm, 2)
98 # Strip kill operands inserted into the asm.
99 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
100 # Strip trailing whitespace.
101 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
102 return asm
103
Tim Shen53ddc1d2016-12-22 20:59:39 +0000104def scrub_asm_powerpc64le(asm):
105 # Scrub runs of whitespace out of the assembly, but leave the leading
106 # whitespace in place.
107 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
108 # Expand the tabs used for indentation.
109 asm = string.expandtabs(asm, 2)
110 # Strip trailing whitespace.
111 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
112 return asm
113
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000114
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000115# Build up a dictionary of all the function bodies.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000116def build_function_body_dictionary(raw_tool_output, triple, prefixes, func_dict,
117 verbose):
118 target_handlers = {
119 'x86_64': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
120 'i686': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
121 'x86': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
122 'i386': (scrub_asm_x86, ASM_FUNCTION_X86_RE),
123 'arm-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Sanjay Patel588e4152017-02-24 21:47:44 +0000124 'thumb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
125 'thumbv8-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Eli Friedman7e0ce822017-02-24 03:04:11 +0000126 'armeb-eabi': (scrub_asm_arm_eabi, ASM_FUNCTION_ARM_RE),
Tim Shen53ddc1d2016-12-22 20:59:39 +0000127 'powerpc64le': (scrub_asm_powerpc64le, ASM_FUNCTION_PPC_RE),
128 }
129 handlers = None
130 for prefix, s in target_handlers.items():
131 if triple.startswith(prefix):
132 handlers = s
133 break
134 else:
135 raise KeyError('Triple %r is not supported' % (triple))
136
137 scrubber, function_re = handlers
Eli Friedman1a9a8872016-12-19 23:09:51 +0000138 for m in function_re.finditer(raw_tool_output):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000139 if not m:
140 continue
141 func = m.group('func')
Tim Shen53ddc1d2016-12-22 20:59:39 +0000142 scrubbed_body = scrubber(m.group('body'))
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000143 if func.startswith('stress'):
144 # We only use the last line of the function body for stress tests.
145 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
146 if verbose:
147 print >>sys.stderr, 'Processing function: ' + func
148 for l in scrubbed_body.splitlines():
149 print >>sys.stderr, ' ' + l
150 for prefix in prefixes:
151 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
152 if prefix == prefixes[-1]:
153 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
154 'same prefix: %r!' % (prefix,))
155 else:
156 func_dict[prefix][func] = None
157 continue
158
159 func_dict[prefix][func] = scrubbed_body
160
161
Tim Shen53ddc1d2016-12-22 20:59:39 +0000162def add_checks(output_lines, run_list, func_dict, func_name):
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000163 printed_prefixes = []
Tim Shen53ddc1d2016-12-22 20:59:39 +0000164 for p in run_list:
165 checkprefixes = p[0]
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000166 for checkprefix in checkprefixes:
167 if checkprefix in printed_prefixes:
168 break
169 if not func_dict[checkprefix][func_name]:
170 continue
171 # Add some space between different check prefixes.
172 if len(printed_prefixes) != 0:
173 output_lines.append(';')
174 printed_prefixes.append(checkprefix)
175 output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name))
176 func_body = func_dict[checkprefix][func_name].splitlines()
177 output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
178 for func_line in func_body[1:]:
179 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
180 # Add space between different check prefixes and the first line of code.
181 # output_lines.append(';')
182 break
183 return output_lines
184
185
186def should_add_line_to_output(input_line, prefix_set):
187 # Skip any blank comment lines in the IR.
188 if input_line.strip() == ';':
189 return False
190 # Skip any blank lines in the IR.
191 #if input_line.strip() == '':
192 # return False
193 # And skip any CHECK lines. We're building our own.
194 m = CHECK_RE.match(input_line)
195 if m and m.group(1) in prefix_set:
196 return False
197
198 return True
199
200
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000201def main():
202 parser = argparse.ArgumentParser(description=__doc__)
203 parser.add_argument('-v', '--verbose', action='store_true',
204 help='Show verbose output')
205 parser.add_argument('--llc-binary', default='llc',
206 help='The "llc" binary to use to generate the test case')
207 parser.add_argument(
208 '--function', help='The function in the test file to update')
209 parser.add_argument('tests', nargs='+')
210 args = parser.parse_args()
211
James Y Knight7c905062015-11-23 21:33:58 +0000212 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
Simon Pilgrim2b7c02a2016-06-11 20:39:21 +0000213 'utils/' + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000214
215 for test in args.tests:
216 if args.verbose:
217 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
218 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000219 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000220
Tim Shen53ddc1d2016-12-22 20:59:39 +0000221 triple_in_ir = None
222 for l in input_lines:
223 m = TRIPLE_IR_RE.match(l)
224 if m:
225 triple_in_ir = m.groups()[0]
226 break
227
Bryant Wong291264b2016-12-29 19:32:34 +0000228 raw_lines = [m.group(1)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000229 for m in [RUN_LINE_RE.match(l) for l in input_lines] if m]
Bryant Wong291264b2016-12-29 19:32:34 +0000230 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
231 for l in raw_lines[1:]:
Bryant Wong507256b2016-12-29 20:05:51 +0000232 if run_lines[-1].endswith("\\"):
233 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
234 else:
235 run_lines.append(l)
Bryant Wong291264b2016-12-29 19:32:34 +0000236
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000237 if args.verbose:
238 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
239 for l in run_lines:
240 print >>sys.stderr, ' RUN: ' + l
241
Tim Shen53ddc1d2016-12-22 20:59:39 +0000242 run_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000243 for l in run_lines:
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000244 commands = [cmd.strip() for cmd in l.split('|', 1)]
245 llc_cmd = commands[0]
Tim Shen53ddc1d2016-12-22 20:59:39 +0000246
247 triple_in_cmd = None
248 m = TRIPLE_ARG_RE.search(llc_cmd)
249 if m:
250 triple_in_cmd = m.groups()[0]
251
Zvi Rackover35a5acf2016-11-07 17:47:21 +0000252 filecheck_cmd = ''
253 if len(commands) > 1:
254 filecheck_cmd = commands[1]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000255 if not llc_cmd.startswith('llc '):
256 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
257 continue
258
259 if not filecheck_cmd.startswith('FileCheck '):
260 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
261 continue
262
263 llc_cmd_args = llc_cmd[len('llc'):].strip()
264 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
265
Nikolai Bozhenov33ee40e2017-01-14 09:39:35 +0000266 check_prefixes = [item for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)
267 for item in m.group(1).split(',')]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000268 if not check_prefixes:
269 check_prefixes = ['CHECK']
270
271 # FIXME: We should use multiple check prefixes to common check lines. For
272 # now, we just ignore all but the last.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000273 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000274
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000275 func_dict = {}
Tim Shen53ddc1d2016-12-22 20:59:39 +0000276 for p in run_list:
277 prefixes = p[0]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000278 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000279 func_dict.update({prefix: dict()})
Tim Shen53ddc1d2016-12-22 20:59:39 +0000280 for prefixes, llc_args, triple_in_cmd in run_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000281 if args.verbose:
282 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
283 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000284
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000285 raw_tool_output = llc(args, llc_args, test)
Tim Shen53ddc1d2016-12-22 20:59:39 +0000286 if not (triple_in_cmd or triple_in_ir):
287 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
288
289 build_function_body_dictionary(raw_tool_output,
290 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict, args.verbose)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000291
292 is_in_function = False
293 is_in_function_start = False
Zvi Rackover18082ab2016-11-07 18:08:19 +0000294 func_name = None
Tim Shen53ddc1d2016-12-22 20:59:39 +0000295 prefix_set = set([prefix for p in run_list for prefix in p[0]])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000296 if args.verbose:
297 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000298 output_lines = []
299 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000300
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000301 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000302 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000303 if input_line == '':
304 continue
305 if input_line.lstrip().startswith(';'):
306 m = CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000307 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000308 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000309 continue
310
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000311 # Print out the various check lines here.
Tim Shen53ddc1d2016-12-22 20:59:39 +0000312 output_lines = add_checks(output_lines, run_list, func_dict, func_name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000313 is_in_function_start = False
314
315 if is_in_function:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000316 if should_add_line_to_output(input_line, prefix_set) == True:
317 # This input line of the function body will go as-is into the output.
318 output_lines.append(input_line)
319 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000320 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000321 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000322 is_in_function = False
323 continue
324
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000325 if input_line == autogenerated_note:
James Y Knight7c905062015-11-23 21:33:58 +0000326 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000327
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000328 # If it's outside a function, it just gets copied to the output.
329 output_lines.append(input_line)
330
331 m = IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000332 if not m:
333 continue
Zvi Rackover18082ab2016-11-07 18:08:19 +0000334 func_name = m.group(1)
335 if args.function is not None and func_name != args.function:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000336 # When filtering on a specific function, skip all others.
337 continue
338 is_in_function = is_in_function_start = True
339
340 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000341 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
342
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000343 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000344 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000345
346
347if __name__ == '__main__':
348 main()