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 119 for prefixes, llc_tool, llc_args, preprocess_cmd, triple_in_cmd, march_in_cmd in run_list: 120 common.debug('Extracted LLC cmd:', llc_tool, llc_args) 121 common.debug('Extracted FileCheck prefixes:', str(prefixes)) 122 123 raw_tool_output = common.invoke_tool(ti.args.llc_binary or llc_tool, 124 llc_args, ti.path, preprocess_cmd, 125 verbose=ti.args.verbose) 126 triple = triple_in_cmd or triple_in_ir 127 if not triple: 128 triple = asm.get_triple_from_march(march_in_cmd) 129 130 scrubber, function_re = asm.get_run_handler(triple) 131 builder.process_run_line(function_re, scrubber, raw_tool_output, prefixes) 132 133 func_dict = builder.finish_and_get_func_dict() 134 135 is_in_function = False 136 is_in_function_start = False 137 func_name = None 138 prefix_set = set([prefix for p in run_list for prefix in p[0]]) 139 common.debug('Rewriting FileCheck prefixes:', str(prefix_set)) 140 output_lines = [] 141 142 include_generated_funcs = common.find_arg_in_test(ti, 143 lambda args: ti.args.include_generated_funcs, 144 '--include-generated-funcs', 145 True) 146 147 if include_generated_funcs: 148 # Generate the appropriate checks for each function. We need to emit 149 # these in the order according to the generated output so that CHECK-LABEL 150 # works properly. func_order provides that. 151 152 # We can't predict where various passes might insert functions so we can't 153 # be sure the input function order is maintained. Therefore, first spit 154 # out all the source lines. 155 common.dump_input_lines(output_lines, ti, prefix_set, ';') 156 157 # Now generate all the checks. 158 common.add_checks_at_end(output_lines, run_list, builder.func_order(), 159 check_indent + ';', 160 lambda my_output_lines, prefixes, func: 161 asm.add_asm_checks(my_output_lines, 162 check_indent + ';', 163 prefixes, func_dict, func)) 164 else: 165 for input_info in ti.iterlines(output_lines): 166 input_line = input_info.line 167 args = input_info.args 168 if is_in_function_start: 169 if input_line == '': 170 continue 171 if input_line.lstrip().startswith(';'): 172 m = common.CHECK_RE.match(input_line) 173 if not m or m.group(1) not in prefix_set: 174 output_lines.append(input_line) 175 continue 176 177 # Print out the various check lines here. 178 asm.add_asm_checks(output_lines, check_indent + ';', run_list, func_dict, func_name) 179 is_in_function_start = False 180 181 if is_in_function: 182 if common.should_add_line_to_output(input_line, prefix_set): 183 # This input line of the function body will go as-is into the output. 184 output_lines.append(input_line) 185 else: 186 continue 187 if input_line.strip() == '}': 188 is_in_function = False 189 continue 190 191 # If it's outside a function, it just gets copied to the output. 192 output_lines.append(input_line) 193 194 m = common.IR_FUNCTION_RE.match(input_line) 195 if not m: 196 continue 197 func_name = m.group(1) 198 if args.function is not None and func_name != args.function: 199 # When filtering on a specific function, skip all others. 200 continue 201 is_in_function = is_in_function_start = True 202 203 common.debug('Writing %d lines to %s...' % (len(output_lines), ti.path)) 204 205 with open(ti.path, 'wb') as f: 206 f.writelines(['{}\n'.format(l).encode('utf-8') for l in output_lines]) 207 208 209if __name__ == '__main__': 210 main() 211