1*9880d681SAndroid Build Coastguard Worker#!/usr/bin/env python2.7 2*9880d681SAndroid Build Coastguard Worker 3*9880d681SAndroid Build Coastguard Worker"""A test case update script. 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard WorkerThis script is a utility to update LLVM X86 'llc' based test cases with new 6*9880d681SAndroid Build Coastguard WorkerFileCheck patterns. It can either update all of the tests in the file or 7*9880d681SAndroid Build Coastguard Workera single test function. 8*9880d681SAndroid Build Coastguard Worker""" 9*9880d681SAndroid Build Coastguard Worker 10*9880d681SAndroid Build Coastguard Workerimport argparse 11*9880d681SAndroid Build Coastguard Workerimport itertools 12*9880d681SAndroid Build Coastguard Workerimport os # Used to advertise this file's name ("autogenerated_note"). 13*9880d681SAndroid Build Coastguard Workerimport string 14*9880d681SAndroid Build Coastguard Workerimport subprocess 15*9880d681SAndroid Build Coastguard Workerimport sys 16*9880d681SAndroid Build Coastguard Workerimport tempfile 17*9880d681SAndroid Build Coastguard Workerimport re 18*9880d681SAndroid Build Coastguard Worker 19*9880d681SAndroid Build Coastguard Worker# Invoke the tool that is being tested. 20*9880d681SAndroid Build Coastguard Workerdef llc(args, cmd_args, ir): 21*9880d681SAndroid Build Coastguard Worker with open(ir) as ir_file: 22*9880d681SAndroid Build Coastguard Worker stdout = subprocess.check_output(args.llc_binary + ' ' + cmd_args, 23*9880d681SAndroid Build Coastguard Worker shell=True, stdin=ir_file) 24*9880d681SAndroid Build Coastguard Worker # Fix line endings to unix CR style. 25*9880d681SAndroid Build Coastguard Worker stdout = stdout.replace('\r\n', '\n') 26*9880d681SAndroid Build Coastguard Worker return stdout 27*9880d681SAndroid Build Coastguard Worker 28*9880d681SAndroid Build Coastguard Worker 29*9880d681SAndroid Build Coastguard Worker# RegEx: this is where the magic happens. 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard WorkerSCRUB_WHITESPACE_RE = re.compile(r'(?!^(| \w))[ \t]+', flags=re.M) 32*9880d681SAndroid Build Coastguard WorkerSCRUB_TRAILING_WHITESPACE_RE = re.compile(r'[ \t]+$', flags=re.M) 33*9880d681SAndroid Build Coastguard WorkerSCRUB_X86_SHUFFLES_RE = ( 34*9880d681SAndroid Build Coastguard Worker re.compile( 35*9880d681SAndroid Build Coastguard Worker r'^(\s*\w+) [^#\n]+#+ ((?:[xyz]mm\d+|mem)( \{%k\d+\}( \{z\})?)? = .*)$', 36*9880d681SAndroid Build Coastguard Worker flags=re.M)) 37*9880d681SAndroid Build Coastguard WorkerSCRUB_X86_SP_RE = re.compile(r'\d+\(%(esp|rsp)\)') 38*9880d681SAndroid Build Coastguard WorkerSCRUB_X86_RIP_RE = re.compile(r'[.\w]+\(%rip\)') 39*9880d681SAndroid Build Coastguard WorkerSCRUB_X86_LCP_RE = re.compile(r'\.LCPI[0-9]+_[0-9]+') 40*9880d681SAndroid Build Coastguard WorkerSCRUB_KILL_COMMENT_RE = re.compile(r'^ *#+ +kill:.*\n') 41*9880d681SAndroid Build Coastguard Worker 42*9880d681SAndroid Build Coastguard WorkerRUN_LINE_RE = re.compile('^\s*;\s*RUN:\s*(.*)$') 43*9880d681SAndroid Build Coastguard WorkerIR_FUNCTION_RE = re.compile('^\s*define\s+(?:internal\s+)?[^@]*@(\w+)\s*\(') 44*9880d681SAndroid Build Coastguard WorkerASM_FUNCTION_RE = re.compile( 45*9880d681SAndroid Build Coastguard Worker r'^_?(?P<func>[^:]+):[ \t]*#+[ \t]*@(?P=func)\n[^:]*?' 46*9880d681SAndroid Build Coastguard Worker r'(?P<body>^##?[ \t]+[^:]+:.*?)\s*' 47*9880d681SAndroid Build Coastguard Worker r'^\s*(?:[^:\n]+?:\s*\n\s*\.size|\.cfi_endproc|\.globl|\.comm|\.(?:sub)?section)', 48*9880d681SAndroid Build Coastguard Worker flags=(re.M | re.S)) 49*9880d681SAndroid Build Coastguard WorkerCHECK_PREFIX_RE = re.compile('--check-prefix=(\S+)') 50*9880d681SAndroid Build Coastguard WorkerCHECK_RE = re.compile(r'^\s*;\s*([^:]+?)(?:-NEXT|-NOT|-DAG|-LABEL)?:') 51*9880d681SAndroid Build Coastguard Worker 52*9880d681SAndroid Build Coastguard Worker 53*9880d681SAndroid Build Coastguard Workerdef scrub_asm(asm): 54*9880d681SAndroid Build Coastguard Worker # Scrub runs of whitespace out of the assembly, but leave the leading 55*9880d681SAndroid Build Coastguard Worker # whitespace in place. 56*9880d681SAndroid Build Coastguard Worker asm = SCRUB_WHITESPACE_RE.sub(r' ', asm) 57*9880d681SAndroid Build Coastguard Worker # Expand the tabs used for indentation. 58*9880d681SAndroid Build Coastguard Worker asm = string.expandtabs(asm, 2) 59*9880d681SAndroid Build Coastguard Worker # Detect shuffle asm comments and hide the operands in favor of the comments. 60*9880d681SAndroid Build Coastguard Worker asm = SCRUB_X86_SHUFFLES_RE.sub(r'\1 {{.*#+}} \2', asm) 61*9880d681SAndroid Build Coastguard Worker # Generically match the stack offset of a memory operand. 62*9880d681SAndroid Build Coastguard Worker asm = SCRUB_X86_SP_RE.sub(r'{{[0-9]+}}(%\1)', asm) 63*9880d681SAndroid Build Coastguard Worker # Generically match a RIP-relative memory operand. 64*9880d681SAndroid Build Coastguard Worker asm = SCRUB_X86_RIP_RE.sub(r'{{.*}}(%rip)', asm) 65*9880d681SAndroid Build Coastguard Worker # Generically match a LCP symbol. 66*9880d681SAndroid Build Coastguard Worker asm = SCRUB_X86_LCP_RE.sub(r'{{\.LCPI.*}}', asm) 67*9880d681SAndroid Build Coastguard Worker # Strip kill operands inserted into the asm. 68*9880d681SAndroid Build Coastguard Worker asm = SCRUB_KILL_COMMENT_RE.sub('', asm) 69*9880d681SAndroid Build Coastguard Worker # Strip trailing whitespace. 70*9880d681SAndroid Build Coastguard Worker asm = SCRUB_TRAILING_WHITESPACE_RE.sub(r'', asm) 71*9880d681SAndroid Build Coastguard Worker return asm 72*9880d681SAndroid Build Coastguard Worker 73*9880d681SAndroid Build Coastguard Worker 74*9880d681SAndroid Build Coastguard Worker# Build up a dictionary of all the function bodies. 75*9880d681SAndroid Build Coastguard Workerdef build_function_body_dictionary(raw_tool_output, prefixes, func_dict, verbose): 76*9880d681SAndroid Build Coastguard Worker for m in ASM_FUNCTION_RE.finditer(raw_tool_output): 77*9880d681SAndroid Build Coastguard Worker if not m: 78*9880d681SAndroid Build Coastguard Worker continue 79*9880d681SAndroid Build Coastguard Worker func = m.group('func') 80*9880d681SAndroid Build Coastguard Worker scrubbed_body = scrub_asm(m.group('body')) 81*9880d681SAndroid Build Coastguard Worker if func.startswith('stress'): 82*9880d681SAndroid Build Coastguard Worker # We only use the last line of the function body for stress tests. 83*9880d681SAndroid Build Coastguard Worker scrubbed_body = '\n'.join(scrubbed_body.splitlines()[-1:]) 84*9880d681SAndroid Build Coastguard Worker if verbose: 85*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Processing function: ' + func 86*9880d681SAndroid Build Coastguard Worker for l in scrubbed_body.splitlines(): 87*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, ' ' + l 88*9880d681SAndroid Build Coastguard Worker for prefix in prefixes: 89*9880d681SAndroid Build Coastguard Worker if func in func_dict[prefix] and func_dict[prefix][func] != scrubbed_body: 90*9880d681SAndroid Build Coastguard Worker if prefix == prefixes[-1]: 91*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, ('WARNING: Found conflicting asm under the ' 92*9880d681SAndroid Build Coastguard Worker 'same prefix: %r!' % (prefix,)) 93*9880d681SAndroid Build Coastguard Worker else: 94*9880d681SAndroid Build Coastguard Worker func_dict[prefix][func] = None 95*9880d681SAndroid Build Coastguard Worker continue 96*9880d681SAndroid Build Coastguard Worker 97*9880d681SAndroid Build Coastguard Worker func_dict[prefix][func] = scrubbed_body 98*9880d681SAndroid Build Coastguard Worker 99*9880d681SAndroid Build Coastguard Worker 100*9880d681SAndroid Build Coastguard Workerdef add_checks(output_lines, prefix_list, func_dict, func_name): 101*9880d681SAndroid Build Coastguard Worker printed_prefixes = [] 102*9880d681SAndroid Build Coastguard Worker for checkprefixes, _ in prefix_list: 103*9880d681SAndroid Build Coastguard Worker for checkprefix in checkprefixes: 104*9880d681SAndroid Build Coastguard Worker if checkprefix in printed_prefixes: 105*9880d681SAndroid Build Coastguard Worker break 106*9880d681SAndroid Build Coastguard Worker if not func_dict[checkprefix][func_name]: 107*9880d681SAndroid Build Coastguard Worker continue 108*9880d681SAndroid Build Coastguard Worker # Add some space between different check prefixes. 109*9880d681SAndroid Build Coastguard Worker if len(printed_prefixes) != 0: 110*9880d681SAndroid Build Coastguard Worker output_lines.append(';') 111*9880d681SAndroid Build Coastguard Worker printed_prefixes.append(checkprefix) 112*9880d681SAndroid Build Coastguard Worker output_lines.append('; %s-LABEL: %s:' % (checkprefix, func_name)) 113*9880d681SAndroid Build Coastguard Worker func_body = func_dict[checkprefix][func_name].splitlines() 114*9880d681SAndroid Build Coastguard Worker output_lines.append('; %s: %s' % (checkprefix, func_body[0])) 115*9880d681SAndroid Build Coastguard Worker for func_line in func_body[1:]: 116*9880d681SAndroid Build Coastguard Worker output_lines.append('; %s-NEXT: %s' % (checkprefix, func_line)) 117*9880d681SAndroid Build Coastguard Worker # Add space between different check prefixes and the first line of code. 118*9880d681SAndroid Build Coastguard Worker # output_lines.append(';') 119*9880d681SAndroid Build Coastguard Worker break 120*9880d681SAndroid Build Coastguard Worker return output_lines 121*9880d681SAndroid Build Coastguard Worker 122*9880d681SAndroid Build Coastguard Worker 123*9880d681SAndroid Build Coastguard Workerdef should_add_line_to_output(input_line, prefix_set): 124*9880d681SAndroid Build Coastguard Worker # Skip any blank comment lines in the IR. 125*9880d681SAndroid Build Coastguard Worker if input_line.strip() == ';': 126*9880d681SAndroid Build Coastguard Worker return False 127*9880d681SAndroid Build Coastguard Worker # Skip any blank lines in the IR. 128*9880d681SAndroid Build Coastguard Worker #if input_line.strip() == '': 129*9880d681SAndroid Build Coastguard Worker # return False 130*9880d681SAndroid Build Coastguard Worker # And skip any CHECK lines. We're building our own. 131*9880d681SAndroid Build Coastguard Worker m = CHECK_RE.match(input_line) 132*9880d681SAndroid Build Coastguard Worker if m and m.group(1) in prefix_set: 133*9880d681SAndroid Build Coastguard Worker return False 134*9880d681SAndroid Build Coastguard Worker 135*9880d681SAndroid Build Coastguard Worker return True 136*9880d681SAndroid Build Coastguard Worker 137*9880d681SAndroid Build Coastguard Worker 138*9880d681SAndroid Build Coastguard Workerdef main(): 139*9880d681SAndroid Build Coastguard Worker parser = argparse.ArgumentParser(description=__doc__) 140*9880d681SAndroid Build Coastguard Worker parser.add_argument('-v', '--verbose', action='store_true', 141*9880d681SAndroid Build Coastguard Worker help='Show verbose output') 142*9880d681SAndroid Build Coastguard Worker parser.add_argument('--llc-binary', default='llc', 143*9880d681SAndroid Build Coastguard Worker help='The "llc" binary to use to generate the test case') 144*9880d681SAndroid Build Coastguard Worker parser.add_argument( 145*9880d681SAndroid Build Coastguard Worker '--function', help='The function in the test file to update') 146*9880d681SAndroid Build Coastguard Worker parser.add_argument('tests', nargs='+') 147*9880d681SAndroid Build Coastguard Worker args = parser.parse_args() 148*9880d681SAndroid Build Coastguard Worker 149*9880d681SAndroid Build Coastguard Worker autogenerated_note = ('; NOTE: Assertions have been autogenerated by ' 150*9880d681SAndroid Build Coastguard Worker 'utils/' + os.path.basename(__file__)) 151*9880d681SAndroid Build Coastguard Worker 152*9880d681SAndroid Build Coastguard Worker for test in args.tests: 153*9880d681SAndroid Build Coastguard Worker if args.verbose: 154*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Scanning for RUN lines in test file: %s' % (test,) 155*9880d681SAndroid Build Coastguard Worker with open(test) as f: 156*9880d681SAndroid Build Coastguard Worker input_lines = [l.rstrip() for l in f] 157*9880d681SAndroid Build Coastguard Worker 158*9880d681SAndroid Build Coastguard Worker run_lines = [m.group(1) 159*9880d681SAndroid Build Coastguard Worker for m in [RUN_LINE_RE.match(l) for l in input_lines] if m] 160*9880d681SAndroid Build Coastguard Worker if args.verbose: 161*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Found %d RUN lines:' % (len(run_lines),) 162*9880d681SAndroid Build Coastguard Worker for l in run_lines: 163*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, ' RUN: ' + l 164*9880d681SAndroid Build Coastguard Worker 165*9880d681SAndroid Build Coastguard Worker prefix_list = [] 166*9880d681SAndroid Build Coastguard Worker for l in run_lines: 167*9880d681SAndroid Build Coastguard Worker (llc_cmd, filecheck_cmd) = tuple([cmd.strip() for cmd in l.split('|', 1)]) 168*9880d681SAndroid Build Coastguard Worker if not llc_cmd.startswith('llc '): 169*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'WARNING: Skipping non-llc RUN line: ' + l 170*9880d681SAndroid Build Coastguard Worker continue 171*9880d681SAndroid Build Coastguard Worker 172*9880d681SAndroid Build Coastguard Worker if not filecheck_cmd.startswith('FileCheck '): 173*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'WARNING: Skipping non-FileChecked RUN line: ' + l 174*9880d681SAndroid Build Coastguard Worker continue 175*9880d681SAndroid Build Coastguard Worker 176*9880d681SAndroid Build Coastguard Worker llc_cmd_args = llc_cmd[len('llc'):].strip() 177*9880d681SAndroid Build Coastguard Worker llc_cmd_args = llc_cmd_args.replace('< %s', '').replace('%s', '').strip() 178*9880d681SAndroid Build Coastguard Worker 179*9880d681SAndroid Build Coastguard Worker check_prefixes = [m.group(1) 180*9880d681SAndroid Build Coastguard Worker for m in CHECK_PREFIX_RE.finditer(filecheck_cmd)] 181*9880d681SAndroid Build Coastguard Worker if not check_prefixes: 182*9880d681SAndroid Build Coastguard Worker check_prefixes = ['CHECK'] 183*9880d681SAndroid Build Coastguard Worker 184*9880d681SAndroid Build Coastguard Worker # FIXME: We should use multiple check prefixes to common check lines. For 185*9880d681SAndroid Build Coastguard Worker # now, we just ignore all but the last. 186*9880d681SAndroid Build Coastguard Worker prefix_list.append((check_prefixes, llc_cmd_args)) 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard Worker func_dict = {} 189*9880d681SAndroid Build Coastguard Worker for prefixes, _ in prefix_list: 190*9880d681SAndroid Build Coastguard Worker for prefix in prefixes: 191*9880d681SAndroid Build Coastguard Worker func_dict.update({prefix: dict()}) 192*9880d681SAndroid Build Coastguard Worker for prefixes, llc_args in prefix_list: 193*9880d681SAndroid Build Coastguard Worker if args.verbose: 194*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Extracted LLC cmd: llc ' + llc_args 195*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Extracted FileCheck prefixes: ' + str(prefixes) 196*9880d681SAndroid Build Coastguard Worker 197*9880d681SAndroid Build Coastguard Worker raw_tool_output = llc(args, llc_args, test) 198*9880d681SAndroid Build Coastguard Worker build_function_body_dictionary(raw_tool_output, prefixes, func_dict, args.verbose) 199*9880d681SAndroid Build Coastguard Worker 200*9880d681SAndroid Build Coastguard Worker is_in_function = False 201*9880d681SAndroid Build Coastguard Worker is_in_function_start = False 202*9880d681SAndroid Build Coastguard Worker prefix_set = set([prefix for prefixes, _ in prefix_list for prefix in prefixes]) 203*9880d681SAndroid Build Coastguard Worker if args.verbose: 204*9880d681SAndroid Build Coastguard Worker print >>sys.stderr, 'Rewriting FileCheck prefixes: %s' % (prefix_set,) 205*9880d681SAndroid Build Coastguard Worker output_lines = [] 206*9880d681SAndroid Build Coastguard Worker output_lines.append(autogenerated_note) 207*9880d681SAndroid Build Coastguard Worker 208*9880d681SAndroid Build Coastguard Worker for input_line in input_lines: 209*9880d681SAndroid Build Coastguard Worker if is_in_function_start: 210*9880d681SAndroid Build Coastguard Worker if input_line == '': 211*9880d681SAndroid Build Coastguard Worker continue 212*9880d681SAndroid Build Coastguard Worker if input_line.lstrip().startswith(';'): 213*9880d681SAndroid Build Coastguard Worker m = CHECK_RE.match(input_line) 214*9880d681SAndroid Build Coastguard Worker if not m or m.group(1) not in prefix_set: 215*9880d681SAndroid Build Coastguard Worker output_lines.append(input_line) 216*9880d681SAndroid Build Coastguard Worker continue 217*9880d681SAndroid Build Coastguard Worker 218*9880d681SAndroid Build Coastguard Worker # Print out the various check lines here. 219*9880d681SAndroid Build Coastguard Worker output_lines = add_checks(output_lines, prefix_list, func_dict, name) 220*9880d681SAndroid Build Coastguard Worker is_in_function_start = False 221*9880d681SAndroid Build Coastguard Worker 222*9880d681SAndroid Build Coastguard Worker if is_in_function: 223*9880d681SAndroid Build Coastguard Worker if should_add_line_to_output(input_line, prefix_set) == True: 224*9880d681SAndroid Build Coastguard Worker # This input line of the function body will go as-is into the output. 225*9880d681SAndroid Build Coastguard Worker output_lines.append(input_line) 226*9880d681SAndroid Build Coastguard Worker else: 227*9880d681SAndroid Build Coastguard Worker continue 228*9880d681SAndroid Build Coastguard Worker if input_line.strip() == '}': 229*9880d681SAndroid Build Coastguard Worker is_in_function = False 230*9880d681SAndroid Build Coastguard Worker continue 231*9880d681SAndroid Build Coastguard Worker 232*9880d681SAndroid Build Coastguard Worker if input_line == autogenerated_note: 233*9880d681SAndroid Build Coastguard Worker continue 234*9880d681SAndroid Build Coastguard Worker 235*9880d681SAndroid Build Coastguard Worker # If it's outside a function, it just gets copied to the output. 236*9880d681SAndroid Build Coastguard Worker output_lines.append(input_line) 237*9880d681SAndroid Build Coastguard Worker 238*9880d681SAndroid Build Coastguard Worker m = IR_FUNCTION_RE.match(input_line) 239*9880d681SAndroid Build Coastguard Worker if not m: 240*9880d681SAndroid Build Coastguard Worker continue 241*9880d681SAndroid Build Coastguard Worker name = m.group(1) 242*9880d681SAndroid Build Coastguard Worker if args.function is not None and name != args.function: 243*9880d681SAndroid Build Coastguard Worker # When filtering on a specific function, skip all others. 244*9880d681SAndroid Build Coastguard Worker continue 245*9880d681SAndroid Build Coastguard Worker is_in_function = is_in_function_start = True 246*9880d681SAndroid Build Coastguard Worker 247*9880d681SAndroid Build Coastguard Worker if args.verbose: 248*9880d681SAndroid Build Coastguard Worker print>>sys.stderr, 'Writing %d lines to %s...' % (len(output_lines), test) 249*9880d681SAndroid Build Coastguard Worker 250*9880d681SAndroid Build Coastguard Worker with open(test, 'wb') as f: 251*9880d681SAndroid Build Coastguard Worker f.writelines([l + '\n' for l in output_lines]) 252*9880d681SAndroid Build Coastguard Worker 253*9880d681SAndroid Build Coastguard Worker 254*9880d681SAndroid Build Coastguard Workerif __name__ == '__main__': 255*9880d681SAndroid Build Coastguard Worker main() 256