OSDN Git Service

[dsymutil] Introduce a new CachedBinaryHolder
[android-x86/external-llvm.git] / utils / update_llc_test_checks.py
1 #!/usr/bin/env python2.7
2
3 """A test case update script.
4
5 This script is a utility to update LLVM '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.
8 """
9
10 import argparse
11 import os         # Used to advertise this file's name ("autogenerated_note").
12 import string
13 import subprocess
14 import sys
15 import re
16
17 from UpdateTestChecks import asm, common
18
19 ADVERT = '; NOTE: Assertions have been autogenerated by '
20
21
22 def main():
23   parser = argparse.ArgumentParser(description=__doc__)
24   parser.add_argument('-v', '--verbose', action='store_true',
25                       help='Show verbose output')
26   parser.add_argument('--llc-binary', default='llc',
27                       help='The "llc" binary to use to generate the test case')
28   parser.add_argument(
29       '--function', help='The function in the test file to update')
30   parser.add_argument(
31       '--extra_scrub', action='store_true',
32       help='Always use additional regex to further reduce diffs between various subtargets')
33   parser.add_argument('tests', nargs='+')
34   args = parser.parse_args()
35
36   autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
37
38   for test in args.tests:
39     if args.verbose:
40       print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
41     with open(test) as f:
42       input_lines = [l.rstrip() for l in f]
43
44     triple_in_ir = None
45     for l in input_lines:
46       m = common.TRIPLE_IR_RE.match(l)
47       if m:
48         triple_in_ir = m.groups()[0]
49         break
50
51     raw_lines = [m.group(1)
52                  for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
53     run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
54     for l in raw_lines[1:]:
55       if run_lines[-1].endswith("\\"):
56         run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
57       else:
58         run_lines.append(l)
59
60     if args.verbose:
61       print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
62       for l in run_lines:
63         print >>sys.stderr, '  RUN: ' + l
64
65     run_list = []
66     for l in run_lines:
67       commands = [cmd.strip() for cmd in l.split('|', 1)]
68       llc_cmd = commands[0]
69
70       triple_in_cmd = None
71       m = common.TRIPLE_ARG_RE.search(llc_cmd)
72       if m:
73         triple_in_cmd = m.groups()[0]
74
75       filecheck_cmd = ''
76       if len(commands) > 1:
77         filecheck_cmd = commands[1]
78       if not llc_cmd.startswith('llc '):
79         print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
80         continue
81
82       if not filecheck_cmd.startswith('FileCheck '):
83         print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
84         continue
85
86       llc_cmd_args = llc_cmd[len('llc'):].strip()
87       llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
88
89       check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
90                                for item in m.group(1).split(',')]
91       if not check_prefixes:
92         check_prefixes = ['CHECK']
93
94       # FIXME: We should use multiple check prefixes to common check lines. For
95       # now, we just ignore all but the last.
96       run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
97
98     func_dict = {}
99     for p in run_list:
100       prefixes = p[0]
101       for prefix in prefixes:
102         func_dict.update({prefix: dict()})
103     for prefixes, llc_args, triple_in_cmd in run_list:
104       if args.verbose:
105         print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
106         print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
107
108       raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
109       if not (triple_in_cmd or triple_in_ir):
110         print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
111
112       asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
113           triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
114
115     is_in_function = False
116     is_in_function_start = False
117     func_name = None
118     prefix_set = set([prefix for p in run_list for prefix in p[0]])
119     if args.verbose:
120       print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
121     output_lines = []
122     output_lines.append(autogenerated_note)
123
124     for input_line in input_lines:
125       if is_in_function_start:
126         if input_line == '':
127           continue
128         if input_line.lstrip().startswith(';'):
129           m = common.CHECK_RE.match(input_line)
130           if not m or m.group(1) not in prefix_set:
131             output_lines.append(input_line)
132             continue
133
134         # Print out the various check lines here.
135         asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
136         is_in_function_start = False
137
138       if is_in_function:
139         if common.should_add_line_to_output(input_line, prefix_set):
140           # This input line of the function body will go as-is into the output.
141           output_lines.append(input_line)
142         else:
143           continue
144         if input_line.strip() == '}':
145           is_in_function = False
146         continue
147
148       # Discard any previous script advertising.
149       if input_line.startswith(ADVERT):
150         continue
151
152       # If it's outside a function, it just gets copied to the output.
153       output_lines.append(input_line)
154
155       m = common.IR_FUNCTION_RE.match(input_line)
156       if not m:
157         continue
158       func_name = m.group(1)
159       if args.function is not None and func_name != args.function:
160         # When filtering on a specific function, skip all others.
161         continue
162       is_in_function = is_in_function_start = True
163
164     if args.verbose:
165       print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
166
167     with open(test, 'wb') as f:
168       f.writelines([l + '\n' for l in output_lines])
169
170
171 if __name__ == '__main__':
172   main()