blob: 55af9f1759f7474c336c984bc53ef9a89f433b87 [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
11import itertools
Sanjay Patelf3c5f462016-03-24 17:15:42 +000012# Could be used to advertise this file's name ("autogenerated_note").
13# import os
Chandler Carruth06a5dd62015-01-12 04:43:18 +000014import string
15import subprocess
16import sys
17import tempfile
18import re
19
Sanjay Patelbf623012016-03-23 21:40:53 +000020# Invoke the tool that is being tested.
Chandler Carruth06a5dd62015-01-12 04:43:18 +000021def llc(args, cmd_args, ir):
22 with open(ir) as ir_file:
23 stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args,
24 shell=True, stdin=ir_file)
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +000025 # Fix line endings to unix CR style.
26 stdout = stdout.replace('\r\n', '\n')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000027 return stdout
28
29
Sanjay Patelbf623012016-03-23 21:40:53 +000030# RegEx: this is where the magic happens.
31
32SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
33SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
34SCRUB_X86_SHUFFLES_RE = (
Chandler Carruth06a5dd62015-01-12 04:43:18 +000035 re.compile(
36 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$',
37 flags=re.M))
Sanjay Patelbf623012016-03-23 21:40:53 +000038SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
39SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
40SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
41
42RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
43IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
44ASM_FUNCTION_RE = re.compile(
Sanjay Patelf3c5f462016-03-24 17:15:42 +000045 r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?'
Sanjay Patelbf623012016-03-23 21:40:53 +000046 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
47 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)',
48 flags=(re.M | re.S))
49CHECK_PREFIX_RE = re.compile('--check-prefix=(\S+)')
50CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000051
52
53def scrub_asm(asm):
54 # Scrub runs of whitespace out of the assembly, but leave the leading
55 # whitespace in place.
Sanjay Patelbf623012016-03-23 21:40:53 +000056 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000057 # Expand the tabs used for indentation.
58 asm = string.expandtabs(asm, 2)
59 # Detect shuffle asm comments and hide the operands in favor of the comments.
Sanjay Patelbf623012016-03-23 21:40:53 +000060 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000061 # Generically match the stack offset of a memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000062 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000063 # Generically match a RIP-relative memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000064 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000065 # Strip kill operands inserted into the asm.
Sanjay Patelbf623012016-03-23 21:40:53 +000066 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
Chandler Carruthe3750952015-02-04 10:46:48 +000067 # Strip trailing whitespace.
Sanjay Patelbf623012016-03-23 21:40:53 +000068 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000069 return asm
70
71
Sanjay Patelf3c5f462016-03-24 17:15:42 +000072# Build up a dictionary of all the function bodies.
73def build_function_body_dictionary(raw_tool_output, prefixes, func_dict, verbose):
74 for m in ASM_FUNCTION_RE.finditer(raw_tool_output):
75 if not m:
76 continue
77 func = m.group('func')
78 scrubbed_body = scrub_asm(m.group('body'))
79 if func.startswith('stress'):
80 # We only use the last line of the function body for stress tests.
81 scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:])
82 if verbose:
83 print >>sys.stderr, 'Processing function: ' + func
84 for l in scrubbed_body.splitlines():
85 print >>sys.stderr, ' ' + l
86 for prefix in prefixes:
87 if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body:
88 if prefix == prefixes[-1]:
89 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
90 'same prefix: %r!' % (prefix,))
91 else:
92 func_dict[prefix][func] = None
93 continue
94
95 func_dict[prefix][func] = scrubbed_body
96
97
98def add_checks(output_lines, prefix_list, func_dict, func_name):
99 printed_prefixes = []
100 for checkprefixes, _ in prefix_list:
101 for checkprefix in checkprefixes:
102 if checkprefix in printed_prefixes:
103 break
104 if not func_dict[checkprefix][func_name]:
105 continue
106 # Add some space between different check prefixes.
107 if len(printed_prefixes) != 0:
108 output_lines.append(';')
109 printed_prefixes.append(checkprefix)
110 output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name))
111 func_body = func_dict[checkprefix][func_name].splitlines()
112 output_lines.append('; %s: %s' % (checkprefix, func_body[0]))
113 for func_line in func_body[1:]:
114 output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line))
115 # Add space between different check prefixes and the first line of code.
116 # output_lines.append(';')
117 break
118 return output_lines
119
120
121def should_add_line_to_output(input_line, prefix_set):
122 # Skip any blank comment lines in the IR.
123 if input_line.strip() == ';':
124 return False
125 # Skip any blank lines in the IR.
126 #if input_line.strip() == '':
127 # return False
128 # And skip any CHECK lines. We're building our own.
129 m = CHECK_RE.match(input_line)
130 if m and m.group(1) in prefix_set:
131 return False
132
133 return True
134
135
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000136def main():
137 parser = argparse.ArgumentParser(description=__doc__)
138 parser.add_argument('-v', '--verbose', action='store_true',
139 help='Show verbose output')
140 parser.add_argument('--llc-binary', default='llc',
141 help='The "llc" binary to use to generate the test case')
142 parser.add_argument(
143 '--function', help='The function in the test file to update')
144 parser.add_argument('tests', nargs='+')
145 args = parser.parse_args()
146
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000147 # FIXME: we don't need to hardcode this name.
James Y Knight7c905062015-11-23 21:33:58 +0000148 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
149 'utils/update_llc_test_checks.py')
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000150 # + os.path.basename(__file__))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000151
152 for test in args.tests:
153 if args.verbose:
154 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
155 with open(test) as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000156 input_lines = [l.rstrip() for l in f]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000157
158 run_lines = [m.group(1)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000159 for m in [RUN_LINE_RE.match(l) for l in input_lines] if m]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000160 if args.verbose:
161 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
162 for l in run_lines:
163 print >>sys.stderr, ' RUN: ' + l
164
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000165 prefix_list = []
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000166 for l in run_lines:
167 (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
168 if not llc_cmd.startswith('llc '):
169 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
170 continue
171
172 if not filecheck_cmd.startswith('FileCheck '):
173 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
174 continue
175
176 llc_cmd_args = llc_cmd[len('llc'):].strip()
177 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
178
179 check_prefixes = [m.group(1)
Sanjay Patelbf623012016-03-23 21:40:53 +0000180 for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000181 if not check_prefixes:
182 check_prefixes = ['CHECK']
183
184 # FIXME: We should use multiple check prefixes to common check lines. For
185 # now, we just ignore all but the last.
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000186 prefix_list.append((check_prefixes, llc_cmd_args))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000187
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000188 func_dict = {}
189 for prefixes, _ in prefix_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000190 for prefix in prefixes:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000191 func_dict.update({prefix: dict()})
192 for prefixes, llc_args in prefix_list:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000193 if args.verbose:
194 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
195 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000196
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000197 raw_tool_output = llc(args, llc_args, test)
198 build_function_body_dictionary(raw_tool_output, prefixes, func_dict, args.verbose)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000199
200 is_in_function = False
201 is_in_function_start = False
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000202 prefix_set = set([prefix for prefixes, _ in prefix_list for prefix in prefixes])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000203 if args.verbose:
204 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000205 output_lines = []
206 output_lines.append(autogenerated_note)
James Y Knight7c905062015-11-23 21:33:58 +0000207
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000208 for input_line in input_lines:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000209 if is_in_function_start:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000210 if input_line == '':
211 continue
212 if input_line.lstrip().startswith(';'):
213 m = CHECK_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000214 if not m or m.group(1) not in prefix_set:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000215 output_lines.append(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000216 continue
217
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000218 # Print out the various check lines here.
219 output_lines = add_checks(output_lines, prefix_list, func_dict, name)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000220 is_in_function_start = False
221
222 if is_in_function:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000223 if should_add_line_to_output(input_line, prefix_set) == True:
224 # This input line of the function body will go as-is into the output.
225 output_lines.append(input_line)
226 else:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000227 continue
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000228 if input_line.strip() == '}':
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000229 is_in_function = False
230 continue
231
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000232 if input_line == autogenerated_note:
James Y Knight7c905062015-11-23 21:33:58 +0000233 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000234
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000235 # If it's outside a function, it just gets copied to the output.
236 output_lines.append(input_line)
237
238 m = IR_FUNCTION_RE.match(input_line)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000239 if not m:
240 continue
241 name = m.group(1)
242 if args.function is not None and name != args.function:
243 # When filtering on a specific function, skip all others.
244 continue
245 is_in_function = is_in_function_start = True
246
247 if args.verbose:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000248 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
249
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000250 with open(test, 'wb') as f:
Sanjay Patelf3c5f462016-03-24 17:15:42 +0000251 f.writelines([l + '\n' for l in output_lines])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000252
253
254if __name__ == '__main__':
255 main()