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 os # Used to advertise this file's name ("autogenerated_note"). 14 15from UpdateTestChecks import common 16 17# llc is the only llc-like in the LLVM tree but downstream forks can add 18# additional ones here if they have them. 19LLC_LIKE_TOOLS = ('llc',) 20 21def main(): 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') 25 parser.add_argument( 26 '--function', help='The function in the test file to update') 27 parser.add_argument( 28 '--extra_scrub', action='store_true', 29 help='Always use additional regex to further reduce diffs between various subtargets') 30 parser.add_argument( 31 '--x86_scrub_sp', action='store_true', default=True, 32 help='Use regex for x86 sp matching to reduce diffs between various subtargets') 33 parser.add_argument( 34 '--no_x86_scrub_sp', action='store_false', dest='x86_scrub_sp') 35 parser.add_argument( 36 '--x86_scrub_rip', action='store_true', default=False, 37 help='Use more regex for x86 rip matching to reduce diffs between various subtargets') 38 parser.add_argument( 39 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip') 40 parser.add_argument( 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) 45 46 script_name = os.path.basename(__file__) 47 48 for ti in common.itertests(initial_args.tests, parser, 49 script_name='utils/' + script_name): 50 triple_in_ir = None 51 for l in ti.input_lines: 52 m = common.TRIPLE_IR_RE.match(l) 53 if m: 54 triple_in_ir = m.groups()[0] 55 break 56 57 run_list = [] 58 for l in ti.run_lines: 59 if '|' not in l: 60 common.warn('Skipping unparsable RUN line: ' + l) 61 continue 62 63 commands = [cmd.strip() for cmd in l.split('|')] 64 assert len(commands) >= 2 65 preprocess_cmd = None 66 if 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] 71 72 triple_in_cmd = None 73 m = common.TRIPLE_ARG_RE.search(llc_cmd) 74 if m: 75 triple_in_cmd = m.groups()[0] 76 77 march_in_cmd = None 78 m = common.MARCH_ARG_RE.search(llc_cmd) 79 if m: 80 march_in_cmd = m.groups()[0] 81 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 85 else: 86 from UpdateTestChecks import asm as output_type 87 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) 91 continue 92 93 if not filecheck_cmd.startswith('FileCheck '): 94 common.warn('Skipping non-FileChecked RUN line: ' + l) 95 continue 96 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'] 105 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)) 110 111 if ti.path.endswith('.mir'): 112 check_indent = ' ' 113 else: 114 check_indent = '' 115 116 builder = common.FunctionTestBuilder( 117 run_list=run_list, 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], 125 path=ti.path) 126 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)) 130 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 135 if not triple: 136 triple = common.get_triple_from_march(march_in_cmd) 137 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) 141 142 func_dict = builder.finish_and_get_func_dict() 143 global_vars_seen_dict = {} 144 145 is_in_function = False 146 is_in_function_start = False 147 func_name = None 148 prefix_set = set([prefix for p in run_list for prefix in p[0]]) 149 common.debug('Rewriting FileCheck prefixes:', str(prefix_set)) 150 output_lines = [] 151 152 include_generated_funcs = common.find_arg_in_test(ti, 153 lambda args: ti.args.include_generated_funcs, 154 '--include-generated-funcs', 155 True) 156 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. 162 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, ';') 167 168 # Now generate all the checks. 169 generated_prefixes = common.add_checks_at_end( 170 output_lines, run_list, builder.func_order(), 171 check_indent + ';', 172 lambda my_output_lines, prefixes, func: 173 output_type.add_checks(my_output_lines, 174 check_indent + ';', 175 prefixes, func_dict, func, 176 global_vars_seen_dict, 177 is_filtered=builder.is_filtered())) 178 else: 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: 183 if input_line == '': 184 continue 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) 189 continue 190 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 197 198 if is_in_function: 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) 202 else: 203 continue 204 if input_line.strip() == '}': 205 is_in_function = False 206 continue 207 208 # If it's outside a function, it just gets copied to the output. 209 output_lines.append(input_line) 210 211 m = common.IR_FUNCTION_RE.match(input_line) 212 if not m: 213 continue 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. 217 continue 218 is_in_function = is_in_function_start = True 219 220 if ti.args.gen_unused_prefix_body: 221 output_lines.extend(ti.get_checks_for_unused_prefixes( 222 run_list, generated_prefixes)) 223 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]) 227 228 229if __name__ == '__main__': 230 main() 231