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 asm, 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 unparseable 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 common.verify_filecheck_prefixes(filecheck_cmd) 83 if llc_tool not in LLC_LIKE_TOOLS: 84 common.warn('Skipping non-llc RUN line: ' + l) 85 continue 86 87 if not filecheck_cmd.startswith('FileCheck '): 88 common.warn('Skipping non-FileChecked RUN line: ' + l) 89 continue 90 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'] 99 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)) 104 105 if ti.path.endswith('.mir'): 106 check_indent = ' ' 107 else: 108 check_indent = '' 109 110 builder = common.FunctionTestBuilder( 111 run_list=run_list, 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], 118 path=ti.path) 119 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)) 123 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 128 if not triple: 129 triple = asm.get_triple_from_march(march_in_cmd) 130 131 scrubber, function_re = asm.get_run_handler(triple) 132 builder.process_run_line(function_re, scrubber, raw_tool_output, prefixes) 133 134 func_dict = builder.finish_and_get_func_dict() 135 136 is_in_function = False 137 is_in_function_start = False 138 func_name = None 139 prefix_set = set([prefix for p in run_list for prefix in p[0]]) 140 common.debug('Rewriting FileCheck prefixes:', str(prefix_set)) 141 output_lines = [] 142 143 include_generated_funcs = common.find_arg_in_test(ti, 144 lambda args: ti.args.include_generated_funcs, 145 '--include-generated-funcs', 146 True) 147 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. 152 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, ';') 157 158 # Now generate all the checks. 159 common.add_checks_at_end(output_lines, run_list, builder.func_order(), 160 check_indent + ';', 161 lambda my_output_lines, prefixes, func: 162 asm.add_asm_checks(my_output_lines, 163 check_indent + ';', 164 prefixes, func_dict, func)) 165 else: 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: 170 if input_line == '': 171 continue 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) 176 continue 177 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 181 182 if is_in_function: 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) 186 else: 187 continue 188 if input_line.strip() == '}': 189 is_in_function = False 190 continue 191 192 # If it's outside a function, it just gets copied to the output. 193 output_lines.append(input_line) 194 195 m = common.IR_FUNCTION_RE.match(input_line) 196 if not m: 197 continue 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. 201 continue 202 is_in_function = is_in_function_start = True 203 204 common.debug('Writing %d lines to %s...' % (len(output_lines), ti.path)) 205 206 with open(ti.path, 'wb') as f: 207 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines]) 208 209 210if __name__ == '__main__': 211 main() 212