1 #!/usr/bin/env python2.7
3 """A test case update script.
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.
11 import os # Used to advertise this file's name ("autogenerated_note").
17 from UpdateTestChecks import asm, common
19 ADVERT = '; NOTE: Assertions have been autogenerated by '
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')
29 '--function', help='The function in the test file to update')
31 '--extra_scrub', action='store_true',
32 help='Always use additional regex to further reduce diffs between various subtargets')
34 '--x86_scrub_rip', action='store_true', default=True,
35 help='Use more regex for x86 matching to reduce diffs between various subtargets')
37 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip')
38 parser.add_argument('tests', nargs='+')
39 args = parser.parse_args()
41 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__))
43 for test in args.tests:
45 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,)
47 input_lines = [l.rstrip() for l in f]
51 m = common.TRIPLE_IR_RE.match(l)
53 triple_in_ir = m.groups()[0]
56 raw_lines = [m.group(1)
57 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m]
58 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else []
59 for l in raw_lines[1:]:
60 if run_lines[-1].endswith("\\"):
61 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l
66 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),)
68 print >>sys.stderr, ' RUN: ' + l
72 commands = [cmd.strip() for cmd in l.split('|', 1)]
76 m = common.TRIPLE_ARG_RE.search(llc_cmd)
78 triple_in_cmd = m.groups()[0]
82 filecheck_cmd = commands[1]
83 if not llc_cmd.startswith('llc '):
84 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l
87 if not filecheck_cmd.startswith('FileCheck '):
88 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l
91 llc_cmd_args = llc_cmd[len('llc'):].strip()
92 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip()
94 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd)
95 for item in m.group(1).split(',')]
96 if not check_prefixes:
97 check_prefixes = ['CHECK']
99 # FIXME: We should use multiple check prefixes to common check lines. For
100 # now, we just ignore all but the last.
101 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd))
106 for prefix in prefixes:
107 func_dict.update({prefix: dict()})
108 for prefixes, llc_args, triple_in_cmd in run_list:
110 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args
111 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes)
113 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test)
114 if not (triple_in_cmd or triple_in_ir):
115 print >>sys.stderr, "Cannot find a triple. Assume 'x86'"
117 asm.build_function_body_dictionary_for_triple(args, raw_tool_output,
118 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict)
120 is_in_function = False
121 is_in_function_start = False
123 prefix_set = set([prefix for p in run_list for prefix in p[0]])
125 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,)
127 output_lines.append(autogenerated_note)
129 for input_line in input_lines:
130 if is_in_function_start:
133 if input_line.lstrip().startswith(';'):
134 m = common.CHECK_RE.match(input_line)
135 if not m or m.group(1) not in prefix_set:
136 output_lines.append(input_line)
139 # Print out the various check lines here.
140 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name)
141 is_in_function_start = False
144 if common.should_add_line_to_output(input_line, prefix_set):
145 # This input line of the function body will go as-is into the output.
146 output_lines.append(input_line)
149 if input_line.strip() == '}':
150 is_in_function = False
153 # Discard any previous script advertising.
154 if input_line.startswith(ADVERT):
157 # If it's outside a function, it just gets copied to the output.
158 output_lines.append(input_line)
160 m = common.IR_FUNCTION_RE.match(input_line)
163 func_name = m.group(1)
164 if args.function is not None and func_name != args.function:
165 # When filtering on a specific function, skip all others.
167 is_in_function = is_in_function_start = True
170 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test)
172 with open(test, 'wb') as f:
173 f.writelines([l + '\n' for l in output_lines])
176 if __name__ == '__main__':