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