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 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 m
= common
.DEBUG_ONLY_ARG_RE
.search(llc_cmd
)
83 if m
and m
.groups()[0] == 'isel':
84 from UpdateTestChecks
import isel
as output_type
86 from UpdateTestChecks
import asm
as output_type
88 common
.verify_filecheck_prefixes(filecheck_cmd
)
89 if llc_tool
not in LLC_LIKE_TOOLS
:
90 common
.warn('Skipping non-llc RUN line: ' + l
)
93 if not filecheck_cmd
.startswith('FileCheck '):
94 common
.warn('Skipping non-FileChecked RUN line: ' + l
)
97 llc_cmd_args
= llc_cmd
[len(llc_tool
):].strip()
98 llc_cmd_args
= llc_cmd_args
.replace('< %s', '').replace('%s', '').strip()
99 if ti
.path
.endswith('.mir'):
100 llc_cmd_args
+= ' -x mir'
101 check_prefixes
= [item
for m
in common
.CHECK_PREFIX_RE
.finditer(filecheck_cmd
)
102 for item
in m
.group(1).split(',')]
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 run_list
.append((check_prefixes
, llc_tool
, llc_cmd_args
, preprocess_cmd
,
109 triple_in_cmd
, march_in_cmd
))
111 if ti
.path
.endswith('.mir'):
116 builder
= common
.FunctionTestBuilder(
118 flags
=type('', (object,), {
119 'verbose': ti
.args
.verbose
,
120 'filters': ti
.args
.filters
,
121 'function_signature': False,
122 'check_attributes': False,
123 'replace_value_regex': []}),
124 scrubber_args
=[ti
.args
],
127 for prefixes
, llc_tool
, llc_args
, preprocess_cmd
, triple_in_cmd
, march_in_cmd
in run_list
:
128 common
.debug('Extracted LLC cmd:', llc_tool
, llc_args
)
129 common
.debug('Extracted FileCheck prefixes:', str(prefixes
))
131 raw_tool_output
= common
.invoke_tool(ti
.args
.llc_binary
or llc_tool
,
132 llc_args
, ti
.path
, preprocess_cmd
,
133 verbose
=ti
.args
.verbose
)
134 triple
= triple_in_cmd
or triple_in_ir
136 triple
= common
.get_triple_from_march(march_in_cmd
)
138 scrubber
, function_re
= output_type
.get_run_handler(triple
)
139 builder
.process_run_line(function_re
, scrubber
, raw_tool_output
, prefixes
, True)
140 builder
.processed_prefixes(prefixes
)
142 func_dict
= builder
.finish_and_get_func_dict()
143 global_vars_seen_dict
= {}
145 is_in_function
= False
146 is_in_function_start
= False
148 prefix_set
= set([prefix
for p
in run_list
for prefix
in p
[0]])
149 common
.debug('Rewriting FileCheck prefixes:', str(prefix_set
))
152 include_generated_funcs
= common
.find_arg_in_test(ti
,
153 lambda args
: ti
.args
.include_generated_funcs
,
154 '--include-generated-funcs',
157 generated_prefixes
= []
158 if include_generated_funcs
:
159 # Generate the appropriate checks for each function. We need to emit
160 # these in the order according to the generated output so that CHECK-LABEL
161 # works properly. func_order provides that.
163 # We can't predict where various passes might insert functions so we can't
164 # be sure the input function order is maintained. Therefore, first spit
165 # out all the source lines.
166 common
.dump_input_lines(output_lines
, ti
, prefix_set
, ';')
168 # Now generate all the checks.
169 generated_prefixes
= common
.add_checks_at_end(
170 output_lines
, run_list
, builder
.func_order(),
172 lambda my_output_lines
, prefixes
, func
:
173 output_type
.add_checks(my_output_lines
,
175 prefixes
, func_dict
, func
,
176 global_vars_seen_dict
,
177 is_filtered
=builder
.is_filtered()))
179 for input_info
in ti
.iterlines(output_lines
):
180 input_line
= input_info
.line
181 args
= input_info
.args
182 if is_in_function_start
:
185 if input_line
.lstrip().startswith(';'):
186 m
= common
.CHECK_RE
.match(input_line
)
187 if not m
or m
.group(1) not in prefix_set
:
188 output_lines
.append(input_line
)
191 # Print out the various check lines here.
192 generated_prefixes
.extend(
193 output_type
.add_checks(output_lines
, check_indent
+ ';', run_list
,
194 func_dict
, func_name
, global_vars_seen_dict
,
195 is_filtered
=builder
.is_filtered()))
196 is_in_function_start
= False
199 if common
.should_add_line_to_output(input_line
, prefix_set
):
200 # This input line of the function body will go as-is into the output.
201 output_lines
.append(input_line
)
204 if input_line
.strip() == '}':
205 is_in_function
= False
208 # If it's outside a function, it just gets copied to the output.
209 output_lines
.append(input_line
)
211 m
= common
.IR_FUNCTION_RE
.match(input_line
)
214 func_name
= m
.group(1)
215 if args
.function
is not None and func_name
!= args
.function
:
216 # When filtering on a specific function, skip all others.
218 is_in_function
= is_in_function_start
= True
220 if ti
.args
.gen_unused_prefix_body
:
221 output_lines
.extend(ti
.get_checks_for_unused_prefixes(
222 run_list
, generated_prefixes
))
224 common
.debug('Writing %d lines to %s...' % (len(output_lines
), ti
.path
))
225 with
open(ti
.path
, 'wb') as f
:
226 f
.writelines(['{}\n'.format(l
).encode('utf-8') for l
in output_lines
])
229 if __name__
== '__main__':