1 #!/usr/bin/env python2.7
3 """A test case update script.
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.
19 def 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)
26 ASM_SCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M)
27 ASM_SCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M)
28 ASM_SCRUB_SHUFFLES_RE = (
30 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$',
32 ASM_SCRUB_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
33 ASM_SCRUB_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
34 ASM_SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
38 # Scrub runs of whitespace out of the assembly, but leave the leading
39 # whitespace in place.
40 asm = ASM_SCRUB_WHITESPACE_RE.sub(r' ', asm)
41 # Expand the tabs used for indentation.
42 asm = string.expandtabs(asm, 2)
43 # Detect shuffle asm comments and hide the operands in favor of the comments.
44 asm = ASM_SCRUB_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
45 # Generically match the stack offset of a memory operand.
46 asm = ASM_SCRUB_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
47 # Generically match a RIP-relative memory operand.
48 asm = ASM_SCRUB_RIP_RE.sub(r'{{.*}}(%rip)', asm)
49 # Strip kill operands inserted into the asm.
50 asm = ASM_SCRUB_KILL_COMMENT_RE.sub('', asm)
51 # Strip trailing whitespace.
52 asm = ASM_SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm)
57 parser = argparse.ArgumentParser(description=__doc__)
58 parser.add_argument('-v', '--verbose', action='store_true',
59 help='Show verbose output')
60 parser.add_argument('--llc-binary', default='llc',
61 help='The "llc" binary to use to generate the test case')
63 '--function', help='The function in the test file to update')
64 parser.add_argument('tests', nargs='+')
65 args = parser.parse_args()
67 run_line_re = re.compile('^\s*;\s*RUN:\s*(.*)$')
68 ir_function_re = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
69 asm_function_re = re.compile(
70 r'^_?(?P<f>[^:]+):[ \t]*#+[ \t]*@(?P=f)\n[^:]*?'
71 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
72 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)',
74 check_prefix_re = re.compile('--check-prefix=(\S+)')
75 check_re = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
76 autogenerated_note = ('; NOTE: Assertions have been autogenerated by '
77 'utils/update_llc_test_checks.py')
79 for test in args.tests:
81 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
83 test_lines = [l.rstrip() for l in f]
85 run_lines = [m.group(1)
86 for m in [run_line_re.match(l) for l in test_lines] if m]
88 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
90 print >>sys.stderr, ' RUN: ' + l
94 (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
95 if not llc_cmd.startswith('llc '):
96 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
99 if not filecheck_cmd.startswith('FileCheck '):
100 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
103 llc_cmd_args = llc_cmd[len('llc'):].strip()
104 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
106 check_prefixes = [m.group(1)
107 for m in check_prefix_re.finditer(filecheck_cmd)]
108 if not check_prefixes:
109 check_prefixes = ['CHECK']
111 # FIXME: We should use multiple check prefixes to common check lines. For
112 # now, we just ignore all but the last.
113 checks.append((check_prefixes, llc_cmd_args))
116 for prefixes, _ in checks:
117 for prefix in prefixes:
118 asm.update({prefix: dict()})
119 for prefixes, llc_args in checks:
121 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
122 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
123 raw_asm = llc(args, llc_args, test)
124 # Build up a dictionary of all the function bodies.
125 for m in asm_function_re.finditer(raw_asm):
129 f_asm = scrub_asm(m.group('body'))
130 if f.startswith('stress'):
131 # We only use the last line of the asm for stress tests.
132 f_asm = '\n'.join(f_asm.splitlines()[-1:])
134 print >>sys.stderr, 'Processing asm for function: ' + f
135 for l in f_asm.splitlines():
136 print >>sys.stderr, ' ' + l
137 for prefix in prefixes:
138 if f in asm[prefix] and asm[prefix][f] != f_asm:
139 if prefix == prefixes[-1]:
140 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
141 'same prefix: %r!' % (prefix,))
143 asm[prefix][f] = None
146 asm[prefix][f] = f_asm
148 is_in_function = False
149 is_in_function_start = False
150 prefix_set = set([prefix for prefixes, _ in checks for prefix in prefixes])
152 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
154 fixed_lines.append(autogenerated_note)
157 if is_in_function_start:
158 if l.lstrip().startswith(';'):
159 m = check_re.match(l)
160 if not m or m.group(1) not in prefix_set:
161 fixed_lines.append(l)
164 # Print out the various check lines here
165 printed_prefixes = []
166 for prefixes, _ in checks:
167 for prefix in prefixes:
168 if prefix in printed_prefixes:
170 if not asm[prefix][name]:
172 if len(printed_prefixes) != 0:
173 fixed_lines.append(';')
174 printed_prefixes.append(prefix)
175 fixed_lines.append('; %s-LABEL: %s:' % (prefix, name))
176 asm_lines = asm[prefix][name].splitlines()
177 fixed_lines.append('; %s: %s' % (prefix, asm_lines[0]))
178 for asm_line in asm_lines[1:]:
179 fixed_lines.append('; %s-NEXT: %s' % (prefix, asm_line))
181 is_in_function_start = False
184 # Skip any blank comment lines in the IR.
187 # And skip any CHECK lines. We'll build our own.
188 m = check_re.match(l)
189 if m and m.group(1) in prefix_set:
191 # Collect the remaining lines in the function body and look for the end
193 fixed_lines.append(l)
195 is_in_function = False
198 if l == autogenerated_note:
200 fixed_lines.append(l)
202 m = ir_function_re.match(l)
206 if args.function is not None and name != args.function:
207 # When filtering on a specific function, skip all others.
209 is_in_function = is_in_function_start = True
212 print>>sys.stderr, 'Writing %d fixed lines to %s...' % (
213 len(fixed_lines), test)
214 with open(test, 'w') as f:
215 f.writelines([l + '\n' for l in fixed_lines])
218 if __name__ == '__main__':