1#!/usr/bin/env python2.7 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 10import argparse 11import os # Used to advertise this file's name ("autogenerated_note"). 12import string 13import subprocess 14import sys 15import re 16 17from UpdateTestChecks import asm, common 18 19ADVERT = '; NOTE: Assertions have been autogenerated by ' 20 21 22def main(): 23 parser = argparse.ArgumentParser(description=__doc__) 24 parser.add_argument('-v', '--verbose', action='store_true', 25 help='Show verbose output') 26 parser.add_argument('--llc-binary', default='llc', 27 help='The "llc" binary to use to generate the test case') 28 parser.add_argument( 29 '--function', help='The function in the test file to update') 30 parser.add_argument( 31 '--extra_scrub', action='store_true', 32 help='Always use additional regex to further reduce diffs between various subtargets') 33 parser.add_argument( 34 '--x86_scrub_rip', action='store_true', default=True, 35 help='Use more regex for x86 matching to reduce diffs between various subtargets') 36 parser.add_argument( 37 '--no_x86_scrub_rip', action='store_false', dest='x86_scrub_rip') 38 parser.add_argument('tests', nargs='+') 39 args = parser.parse_args() 40 41 autogenerated_note = (ADVERT + 'utils/' + os.path.basename(__file__)) 42 43 for test in args.tests: 44 if args.verbose: 45 print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,) 46 with open(test) as f: 47 input_lines = [l.rstrip() for l in f] 48 49 triple_in_ir = None 50 for l in input_lines: 51 m = common.TRIPLE_IR_RE.match(l) 52 if m: 53 triple_in_ir = m.groups()[0] 54 break 55 56 raw_lines = [m.group(1) 57 for m in [common.RUN_LINE_RE.match(l) for l in input_lines] if m] 58 run_lines = [raw_lines[0]] if len(raw_lines) > 0 else [] 59 for l in raw_lines[1:]: 60 if run_lines[-1].endswith("\\"): 61 run_lines[-1] = run_lines[-1].rstrip("\\") + " " + l 62 else: 63 run_lines.append(l) 64 65 if args.verbose: 66 print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),) 67 for l in run_lines: 68 print >>sys.stderr, ' RUN: ' + l 69 70 run_list = [] 71 for l in run_lines: 72 commands = [cmd.strip() for cmd in l.split('|', 1)] 73 llc_cmd = commands[0] 74 75 triple_in_cmd = None 76 m = common.TRIPLE_ARG_RE.search(llc_cmd) 77 if m: 78 triple_in_cmd = m.groups()[0] 79 80 filecheck_cmd = '' 81 if len(commands) > 1: 82 filecheck_cmd = commands[1] 83 if not llc_cmd.startswith('llc '): 84 print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l 85 continue 86 87 if not filecheck_cmd.startswith('FileCheck '): 88 print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l 89 continue 90 91 llc_cmd_args = llc_cmd[len('llc'):].strip() 92 llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() 93 94 check_prefixes = [item for m in common.CHECK_PREFIX_RE.finditer(filecheck_cmd) 95 for item in m.group(1).split(',')] 96 if not check_prefixes: 97 check_prefixes = ['CHECK'] 98 99 # FIXME: We should use multiple check prefixes to common check lines. For 100 # now, we just ignore all but the last. 101 run_list.append((check_prefixes, llc_cmd_args, triple_in_cmd)) 102 103 func_dict = {} 104 for p in run_list: 105 prefixes = p[0] 106 for prefix in prefixes: 107 func_dict.update({prefix: dict()}) 108 for prefixes, llc_args, triple_in_cmd in run_list: 109 if args.verbose: 110 print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args 111 print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes) 112 113 raw_tool_output = common.invoke_tool(args.llc_binary, llc_args, test) 114 if not (triple_in_cmd or triple_in_ir): 115 print >>sys.stderr, "Cannot find a triple. Assume 'x86'" 116 117 asm.build_function_body_dictionary_for_triple(args, raw_tool_output, 118 triple_in_cmd or triple_in_ir or 'x86', prefixes, func_dict) 119 120 is_in_function = False 121 is_in_function_start = False 122 func_name = None 123 prefix_set = set([prefix for p in run_list for prefix in p[0]]) 124 if args.verbose: 125 print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,) 126 output_lines = [] 127 output_lines.append(autogenerated_note) 128 129 for input_line in input_lines: 130 if is_in_function_start: 131 if input_line == '': 132 continue 133 if input_line.lstrip().startswith(';'): 134 m = common.CHECK_RE.match(input_line) 135 if not m or m.group(1) not in prefix_set: 136 output_lines.append(input_line) 137 continue 138 139 # Print out the various check lines here. 140 asm.add_asm_checks(output_lines, ';', run_list, func_dict, func_name) 141 is_in_function_start = False 142 143 if is_in_function: 144 if common.should_add_line_to_output(input_line, prefix_set): 145 # This input line of the function body will go as-is into the output. 146 output_lines.append(input_line) 147 else: 148 continue 149 if input_line.strip() == '}': 150 is_in_function = False 151 continue 152 153 # Discard any previous script advertising. 154 if input_line.startswith(ADVERT): 155 continue 156 157 # If it's outside a function, it just gets copied to the output. 158 output_lines.append(input_line) 159 160 m = common.IR_FUNCTION_RE.match(input_line) 161 if not m: 162 continue 163 func_name = m.group(1) 164 if args.function is not None and func_name != args.function: 165 # When filtering on a specific function, skip all others. 166 continue 167 is_in_function = is_in_function_start = True 168 169 if args.verbose: 170 print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test) 171 172 with open(test, 'wb') as f: 173 f.writelines([l + '\n' for l in output_lines]) 174 175 176if __name__ == '__main__': 177 main() 178