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.
10 from __future__
import print_function
13 import os
# Used to advertise this file's name ("autogenerated_note").
15 from UpdateTestChecks
import asm
, common
17 # llc is the only llc-like in the LLVM tree but downstream forks can add
18 # additional ones here if they have them.
19 LLC_LIKE_TOOLS
= ('llc',)
22 parser
= argparse
.ArgumentParser(description
=__doc__
)
23 parser
.add_argument('--llc-binary', default
=None,
24 help='The "llc" binary to use to generate the test case')
26 '--function', help='The function in the test file to update')
28 '--extra_scrub', action
='store_true',
29 help='Always use additional regex to further reduce diffs between various subtargets')
31 '--x86_scrub_sp', action
='store_true', default
=True,
32 help='Use regex for x86 sp matching to reduce diffs between various subtargets')
34 '--no_x86_scrub_sp', action
='store_false', dest
='x86_scrub_sp')
36 '--x86_scrub_rip', action
='store_true', default
=False,
37 help='Use more regex for x86 rip matching to reduce diffs between various subtargets')
39 '--no_x86_scrub_rip', action
='store_false', dest
='x86_scrub_rip')
41 '--no_x86_scrub_mem_shuffle', action
='store_true', default
=False,
42 help='Reduce scrubbing shuffles with memory operands')
43 parser
.add_argument('tests', nargs
='+')
44 initial_args
= common
.parse_commandline_args(parser
)
46 script_name
= os
.path
.basename(__file__
)
48 for ti
in common
.itertests(initial_args
.tests
, parser
,
49 script_name
='utils/' + script_name
):
51 for l
in ti
.input_lines
:
52 m
= common
.TRIPLE_IR_RE
.match(l
)
54 triple_in_ir
= m
.groups()[0]
58 for l
in ti
.run_lines
:
60 common
.warn('Skipping unparseable RUN line: ' + l
)
63 commands
= [cmd
.strip() for cmd
in l
.split('|')]
64 assert len(commands
) >= 2
67 preprocess_cmd
= " | ".join(commands
[:-2])
68 llc_cmd
= commands
[-2]
69 filecheck_cmd
= commands
[-1]
70 llc_tool
= llc_cmd
.split(' ')[0]
73 m
= common
.TRIPLE_ARG_RE
.search(llc_cmd
)
75 triple_in_cmd
= m
.groups()[0]
78 m
= common
.MARCH_ARG_RE
.search(llc_cmd
)
80 march_in_cmd
= m
.groups()[0]
82 common
.verify_filecheck_prefixes(filecheck_cmd
)
83 if llc_tool
not in LLC_LIKE_TOOLS
:
84 common
.warn('Skipping non-llc RUN line: ' + l
)
87 if not filecheck_cmd
.startswith('FileCheck '):
88 common
.warn('Skipping non-FileChecked RUN line: ' + l
)
91 llc_cmd_args
= llc_cmd
[len(llc_tool
):].strip()
92 llc_cmd_args
= llc_cmd_args
.replace('< %s', '').replace('%s', '').strip()
93 if ti
.path
.endswith('.mir'):
94 llc_cmd_args
+= ' -x mir'
95 check_prefixes
= [item
for m
in common
.CHECK_PREFIX_RE
.finditer(filecheck_cmd
)
96 for item
in m
.group(1).split(',')]
97 if not check_prefixes
:
98 check_prefixes
= ['CHECK']
100 # FIXME: We should use multiple check prefixes to common check lines. For
101 # now, we just ignore all but the last.
102 run_list
.append((check_prefixes
, llc_tool
, llc_cmd_args
, preprocess_cmd
,
103 triple_in_cmd
, march_in_cmd
))
105 if ti
.path
.endswith('.mir'):
110 builder
= common
.FunctionTestBuilder(
112 flags
=type('', (object,), {
113 'verbose': ti
.args
.verbose
,
114 'function_signature': False,
115 'check_attributes': False,
116 'replace_value_regex': []}),
117 scrubber_args
=[ti
.args
],
120 for prefixes
, llc_tool
, llc_args
, preprocess_cmd
, triple_in_cmd
, march_in_cmd
in run_list
:
121 common
.debug('Extracted LLC cmd:', llc_tool
, llc_args
)
122 common
.debug('Extracted FileCheck prefixes:', str(prefixes
))
124 raw_tool_output
= common
.invoke_tool(ti
.args
.llc_binary
or llc_tool
,
125 llc_args
, ti
.path
, preprocess_cmd
,
126 verbose
=ti
.args
.verbose
)
127 triple
= triple_in_cmd
or triple_in_ir
129 triple
= asm
.get_triple_from_march(march_in_cmd
)
131 scrubber
, function_re
= asm
.get_run_handler(triple
)
132 builder
.process_run_line(function_re
, scrubber
, raw_tool_output
, prefixes
, True)
134 func_dict
= builder
.finish_and_get_func_dict()
136 is_in_function
= False
137 is_in_function_start
= False
139 prefix_set
= set([prefix
for p
in run_list
for prefix
in p
[0]])
140 common
.debug('Rewriting FileCheck prefixes:', str(prefix_set
))
143 include_generated_funcs
= common
.find_arg_in_test(ti
,
144 lambda args
: ti
.args
.include_generated_funcs
,
145 '--include-generated-funcs',
148 if include_generated_funcs
:
149 # Generate the appropriate checks for each function. We need to emit
150 # these in the order according to the generated output so that CHECK-LABEL
151 # works properly. func_order provides that.
153 # We can't predict where various passes might insert functions so we can't
154 # be sure the input function order is maintained. Therefore, first spit
155 # out all the source lines.
156 common
.dump_input_lines(output_lines
, ti
, prefix_set
, ';')
158 # Now generate all the checks.
159 common
.add_checks_at_end(output_lines
, run_list
, builder
.func_order(),
161 lambda my_output_lines
, prefixes
, func
:
162 asm
.add_asm_checks(my_output_lines
,
164 prefixes
, func_dict
, func
))
166 for input_info
in ti
.iterlines(output_lines
):
167 input_line
= input_info
.line
168 args
= input_info
.args
169 if is_in_function_start
:
172 if input_line
.lstrip().startswith(';'):
173 m
= common
.CHECK_RE
.match(input_line
)
174 if not m
or m
.group(1) not in prefix_set
:
175 output_lines
.append(input_line
)
178 # Print out the various check lines here.
179 asm
.add_asm_checks(output_lines
, check_indent
+ ';', run_list
, func_dict
, func_name
)
180 is_in_function_start
= False
183 if common
.should_add_line_to_output(input_line
, prefix_set
):
184 # This input line of the function body will go as-is into the output.
185 output_lines
.append(input_line
)
188 if input_line
.strip() == '}':
189 is_in_function
= False
192 # If it's outside a function, it just gets copied to the output.
193 output_lines
.append(input_line
)
195 m
= common
.IR_FUNCTION_RE
.match(input_line
)
198 func_name
= m
.group(1)
199 if args
.function
is not None and func_name
!= args
.function
:
200 # When filtering on a specific function, skip all others.
202 is_in_function
= is_in_function_start
= True
204 common
.debug('Writing %d lines to %s...' % (len(output_lines
), ti
.path
))
206 with
open(ti
.path
, 'wb') as f
:
207 f
.writelines(['{}\n'.format(l
).encode('utf-8') for l
in output_lines
])
210 if __name__
== '__main__':