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_SHUFFLES_RE = (
29 r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem) = .*)$',
31 ASM_SCRUB_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)')
32 ASM_SCRUB_RIP_RE = re.compile(r'[.\w]+\(%rip\)')
33 ASM_SCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n')
37 # Scrub runs of whitespace out of the assembly, but leave the leading
38 # whitespace in place.
39 asm = ASM_SCRUB_WHITESPACE_RE.sub(r' ', asm)
40 # Expand the tabs used for indentation.
41 asm = string.expandtabs(asm, 2)
42 # Detect shuffle asm comments and hide the operands in favor of the comments.
43 asm = ASM_SCRUB_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm)
44 # Generically match the stack offset of a memory operand.
45 asm = ASM_SCRUB_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm)
46 # Generically match a RIP-relative memory operand.
47 asm = ASM_SCRUB_RIP_RE.sub(r'{{.*}}(%rip)', asm)
48 # Strip kill operands inserted into the asm.
49 asm = ASM_SCRUB_KILL_COMMENT_RE.sub('', asm)
54 parser = argparse.ArgumentParser(description=__doc__)
55 parser.add_argument('-v', '--verbose', action='store_true',
56 help='Show verbose output')
57 parser.add_argument('--llc-binary', default='llc',
58 help='The "llc" binary to use to generate the test case')
60 '--function', help='The function in the test file to update')
61 parser.add_argument('tests', nargs='+')
62 args = parser.parse_args()
64 run_line_re = re.compile('^\s*;\s*RUN:\s*(.*)$')
65 ir_function_re = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(')
66 asm_function_re = re.compile(
67 r'^_?(?P<f>[^:]+):[ \t]*#+[ \t]*@(?P=f)\n[^:]*?'
68 r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*'
69 r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.(?:sub)?section)',
71 check_prefix_re = re.compile('--check-prefix=(\S+)')
72 check_re = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:')
74 for test in args.tests:
76 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
78 test_lines = [l.rstrip() for l in f]
80 run_lines = [m.group(1)
81 for m in [run_line_re.match(l) for l in test_lines] if m]
83 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
85 print >>sys.stderr, ' RUN: ' + l
89 (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)])
90 if not llc_cmd.startswith('llc '):
91 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
94 if not filecheck_cmd.startswith('FileCheck '):
95 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
98 llc_cmd_args = llc_cmd[len('llc'):].strip()
99 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
101 check_prefixes = [m.group(1)
102 for m in check_prefix_re.finditer(filecheck_cmd)]
103 if not check_prefixes:
104 check_prefixes = ['CHECK']
106 # FIXME: We should use multiple check prefixes to common check lines. For
107 # now, we just ignore all but the last.
108 checks.append((check_prefixes, llc_cmd_args))
111 for prefixes, _ in checks:
112 for prefix in prefixes:
113 asm.update({prefix: dict()})
114 for prefixes, llc_args in checks:
116 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
117 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
118 raw_asm = llc(args, llc_args, test)
119 # Build up a dictionary of all the function bodies.
120 for m in asm_function_re.finditer(raw_asm):
124 f_asm = scrub_asm(m.group('body'))
126 print >>sys.stderr, 'Processing asm for function: ' + f
127 for l in f_asm.splitlines():
128 print >>sys.stderr, ' ' + l
129 for prefix in prefixes:
130 if f in asm[prefix] and asm[prefix][f] != f_asm:
131 if prefix == prefixes[-1]:
132 print >>sys.stderr, ('WARNING: Found conflicting asm under the '
135 asm[prefix][f] = None
138 asm[prefix][f] = f_asm
140 is_in_function = False
141 is_in_function_start = False
142 prefix_set = set([prefix for prefixes, _ in checks for prefix in prefixes])
144 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
147 if is_in_function_start:
148 if l.lstrip().startswith(';'):
149 m = check_re.match(l)
150 if not m or m.group(1) not in prefix_set:
151 fixed_lines.append(l)
154 # Print out the various check lines here
155 printed_prefixes = []
156 for prefixes, _ in checks:
157 for prefix in prefixes:
158 if prefix in printed_prefixes:
160 if not asm[prefix][name]:
162 if len(printed_prefixes) != 0:
163 fixed_lines.append(';')
164 printed_prefixes.append(prefix)
165 fixed_lines.append('; %s-LABEL: %s:' % (prefix, name))
166 asm_lines = asm[prefix][name].splitlines()
167 fixed_lines.append('; %s: %s' % (prefix, asm_lines[0]))
168 for asm_line in asm_lines[1:]:
169 fixed_lines.append('; %s-NEXT: %s' % (prefix, asm_line))
171 is_in_function_start = False
174 # Skip any blank comment lines in the IR.
177 # And skip any CHECK lines. We'll build our own.
178 m = check_re.match(l)
179 if m and m.group(1) in prefix_set:
181 # Collect the remaining lines in the function body and look for the end
183 fixed_lines.append(l)
185 is_in_function = False
188 fixed_lines.append(l)
190 m = ir_function_re.match(l)
194 if args.function is not None and name != args.function:
195 # When filtering on a specific function, skip all others.
197 is_in_function = is_in_function_start = True
200 print>>sys.stderr, 'Writing %d fixed lines to %s...' % (
201 len(fixed_lines), test)
202 with open(test, 'w') as f:
203 f.writelines([l + '\n' for l in fixed_lines])
206 if __name__ == '__main__':