blob: 781fab54f69bec54ce2f8ab4991cb2ea47149b36 [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
12import string
13import subprocess
14import sys
15import tempfile
16import re
17
Sanjay Patelbf623012016-03-23 21:40:53 +000018# Invoke the tool that is being tested.
Chandler Carruth06a5dd62015-01-12 04:43:18 +000019def llc(args, cmd_args, ir):
20 with open(ir) as ir_file:
21 stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args,
22 shell=True, stdin=ir_file)
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +000023 # Fix line endings to unix CR style.
24 stdout = stdout.replace('\r\n', '\n')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000025 return stdout
26
27
Sanjay Patelbf623012016-03-23 21:40:53 +000028# RegEx: this is where the magic happens.
29
30SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
31SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
32SCRUB_X86_SHUFFLES_RE = (
Chandler Carruth06a5dd62015-01-12 04:43:18 +000033 re.compile(
34 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$',
35 flags=re.M))
Sanjay Patelbf623012016-03-23 21:40:53 +000036SCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
37SCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
38SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
39
40RUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$')
41IR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
42ASM_FUNCTION_RE = re.compile(
43 r'^_?(?P<f>[^:]+):[ \t]*#+[ \t]*@(?P=f)\n[^:]*?'
44 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
45 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)',
46 flags=(re.M | re.S))
47CHECK_PREFIX_RE = re.compile('--check-prefix=(\S+)')
48CHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000049
50
51def scrub_asm(asm):
52 # Scrub runs of whitespace out of the assembly, but leave the leading
53 # whitespace in place.
Sanjay Patelbf623012016-03-23 21:40:53 +000054 asm = SCRUB_WHITESPACE_RE.sub(r' ', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000055 # Expand the tabs used for indentation.
56 asm = string.expandtabs(asm, 2)
57 # Detect shuffle asm comments and hide the operands in favor of the comments.
Sanjay Patelbf623012016-03-23 21:40:53 +000058 asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000059 # Generically match the stack offset of a memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000060 asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000061 # Generically match a RIP-relative memory operand.
Sanjay Patelbf623012016-03-23 21:40:53 +000062 asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000063 # Strip kill operands inserted into the asm.
Sanjay Patelbf623012016-03-23 21:40:53 +000064 asm = SCRUB_KILL_COMMENT_RE.sub('', asm)
Chandler Carruthe3750952015-02-04 10:46:48 +000065 # Strip trailing whitespace.
Sanjay Patelbf623012016-03-23 21:40:53 +000066 asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
Chandler Carruth06a5dd62015-01-12 04:43:18 +000067 return asm
68
69
70def main():
71 parser = argparse.ArgumentParser(description=__doc__)
72 parser.add_argument('-v', '--verbose', action='store_true',
73 help='Show verbose output')
74 parser.add_argument('--llc-binary', default='llc',
75 help='The "llc" binary to use to generate the test case')
76 parser.add_argument(
77 '--function', help='The function in the test file to update')
78 parser.add_argument('tests', nargs='+')
79 args = parser.parse_args()
80
James Y Knight7c905062015-11-23 21:33:58 +000081 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
82 'utils/update_llc_test_checks.py')
Chandler Carruth06a5dd62015-01-12 04:43:18 +000083
84 for test in args.tests:
85 if args.verbose:
86 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
87 with open(test) as f:
88 test_lines = [l.rstrip() for l in f]
89
90 run_lines = [m.group(1)
Sanjay Patelbf623012016-03-23 21:40:53 +000091 for m in [RUN_LINE_RE.match(l) for l in test_lines] if m]
Chandler Carruth06a5dd62015-01-12 04:43:18 +000092 if args.verbose:
93 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
94 for l in run_lines:
95 print >>sys.stderr, ' RUN: ' + l
96
97 checks = []
98 for l in run_lines:
99 (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
100 if not llc_cmd.startswith('llc '):
101 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
102 continue
103
104 if not filecheck_cmd.startswith('FileCheck '):
105 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
106 continue
107
108 llc_cmd_args = llc_cmd[len('llc'):].strip()
109 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
110
111 check_prefixes = [m.group(1)
Sanjay Patelbf623012016-03-23 21:40:53 +0000112 for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)]
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000113 if not check_prefixes:
114 check_prefixes = ['CHECK']
115
116 # FIXME: We should use multiple check prefixes to common check lines. For
117 # now, we just ignore all but the last.
118 checks.append((check_prefixes, llc_cmd_args))
119
120 asm = {}
121 for prefixes, _ in checks:
122 for prefix in prefixes:
123 asm.update({prefix: dict()})
124 for prefixes, llc_args in checks:
125 if args.verbose:
126 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
127 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
128 raw_asm = llc(args, llc_args, test)
129 # Build up a dictionary of all the function bodies.
Sanjay Patelbf623012016-03-23 21:40:53 +0000130 for m in ASM_FUNCTION_RE.finditer(raw_asm):
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000131 if not m:
132 continue
133 f = m.group('f')
134 f_asm = scrub_asm(m.group('body'))
Chandler Carrutha4a77ed52015-02-03 21:26:45 +0000135 if f.startswith('stress'):
136 # We only use the last line of the asm for stress tests.
137 f_asm = '\n'.join(f_asm.splitlines()[-1:])
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000138 if args.verbose:
139 print >>sys.stderr, 'Processing asm for function: ' + f
140 for l in f_asm.splitlines():
141 print >>sys.stderr, ' ' + l
142 for prefix in prefixes:
143 if f in asm[prefix] and asm[prefix][f] != f_asm:
144 if prefix == prefixes[-1]:
145 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
James Y Knight7c905062015-11-23 21:33:58 +0000146 'same prefix: %r!' % (prefix,))
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000147 else:
148 asm[prefix][f] = None
149 continue
150
151 asm[prefix][f] = f_asm
152
153 is_in_function = False
154 is_in_function_start = False
155 prefix_set = set([prefix for prefixes, _ in checks for prefix in prefixes])
156 if args.verbose:
157 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
158 fixed_lines = []
James Y Knight7c905062015-11-23 21:33:58 +0000159 fixed_lines.append(autogenerated_note)
160
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000161 for l in test_lines:
162 if is_in_function_start:
163 if l.lstrip().startswith(';'):
Sanjay Patelbf623012016-03-23 21:40:53 +0000164 m = CHECK_RE.match(l)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000165 if not m or m.group(1) not in prefix_set:
166 fixed_lines.append(l)
167 continue
168
169 # Print out the various check lines here
170 printed_prefixes = []
171 for prefixes, _ in checks:
172 for prefix in prefixes:
173 if prefix in printed_prefixes:
174 break
175 if not asm[prefix][name]:
176 continue
177 if len(printed_prefixes) != 0:
178 fixed_lines.append(';')
179 printed_prefixes.append(prefix)
180 fixed_lines.append('; %s-LABEL: %s:' % (prefix, name))
181 asm_lines = asm[prefix][name].splitlines()
182 fixed_lines.append('; %s: %s' % (prefix, asm_lines[0]))
183 for asm_line in asm_lines[1:]:
184 fixed_lines.append('; %s-NEXT: %s' % (prefix, asm_line))
185 break
186 is_in_function_start = False
187
188 if is_in_function:
189 # Skip any blank comment lines in the IR.
190 if l.strip() == ';':
191 continue
192 # And skip any CHECK lines. We'll build our own.
Sanjay Patelbf623012016-03-23 21:40:53 +0000193 m = CHECK_RE.match(l)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000194 if m and m.group(1) in prefix_set:
195 continue
196 # Collect the remaining lines in the function body and look for the end
197 # of the function.
198 fixed_lines.append(l)
199 if l.strip() == '}':
200 is_in_function = False
201 continue
202
James Y Knight7c905062015-11-23 21:33:58 +0000203 if l == autogenerated_note:
204 continue
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000205 fixed_lines.append(l)
206
Sanjay Patelbf623012016-03-23 21:40:53 +0000207 m = IR_FUNCTION_RE.match(l)
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000208 if not m:
209 continue
210 name = m.group(1)
211 if args.function is not None and name != args.function:
212 # When filtering on a specific function, skip all others.
213 continue
214 is_in_function = is_in_function_start = True
215
216 if args.verbose:
217 print>>sys.stderr, 'Writing %d fixed lines to %s...' % (
218 len(fixed_lines), test)
Simon Pilgrim6b6dcc42016-01-27 21:13:18 +0000219 with open(test, 'wb') as f:
Chandler Carruth06a5dd62015-01-12 04:43:18 +0000220 f.writelines([l + '\n' for l in fixed_lines])
221
222
223if __name__ == '__main__':
224 main()