llvm-project
152 строки · 3.8 Кб
1#!/usr/bin/env python
2
3"""Reduces GlobalISel failures.
4
5This script is a utility to reduce tests that GlobalISel
6fails to compile.
7
8It runs llc to get the error message using a regex and creates
9a custom command to check that specific error. Then, it runs bugpoint
10with the custom command.
11
12"""
13from __future__ import print_function14import argparse15import re16import subprocess17import sys18import tempfile19import os20
21
22def log(msg):23print(msg)24
25
26def hr():27log("-" * 50)28
29
30def log_err(msg):31print("ERROR: {}".format(msg), file=sys.stderr)32
33
34def check_path(path):35if not os.path.exists(path):36log_err("{} does not exist.".format(path))37raise38return path39
40
41def check_bin(build_dir, bin_name):42file_name = "{}/bin/{}".format(build_dir, bin_name)43return check_path(file_name)44
45
46def run_llc(llc, irfile):47pr = subprocess.Popen(48[llc, "-o", "-", "-global-isel", "-pass-remarks-missed=gisel", irfile],49stdout=subprocess.PIPE,50stderr=subprocess.PIPE,51)52out, err = pr.communicate()53res = pr.wait()54if res == 0:55return 056re_err = re.compile(57r"LLVM ERROR: ([a-z\s]+):.*(G_INTRINSIC[_A-Z]* <intrinsic:@[a-zA-Z0-9\.]+>|G_[A-Z_]+)"58)59match = re_err.match(err)60if not match:61return 062else:63return [match.group(1), match.group(2)]64
65
66def run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp, ir_file):67compileCmd = "-compile-command={} -c {} {}".format(68os.path.realpath(__file__), llc_bin, tmp69)70pr = subprocess.Popen(71[72bugpoint_bin,73"-compile-custom",74compileCmd,75"-opt-command={}".format(opt_bin),76ir_file,77]78)79res = pr.wait()80if res != 0:81log_err("Unable to reduce the test.")82raise83
84
85def run_bugpoint_check():86path_to_llc = sys.argv[2]87path_to_err = sys.argv[3]88path_to_ir = sys.argv[4]89with open(path_to_err, "r") as f:90err = f.read()91res = run_llc(path_to_llc, path_to_ir)92if res == 0:93return 094log("GlobalISed failed, {}: {}".format(res[0], res[1]))95if res != err.split(";"):96return 097else:98return 199
100
101def main():102# Check if this is called by bugpoint.103if len(sys.argv) == 5 and sys.argv[1] == "-c":104sys.exit(run_bugpoint_check())105
106# Parse arguments.107parser = argparse.ArgumentParser(108description=__doc__, formatter_class=argparse.RawTextHelpFormatter109)110parser.add_argument("BuildDir", help="Path to LLVM build directory")111parser.add_argument("IRFile", help="Path to the input IR file")112args = parser.parse_args()113
114# Check if the binaries exist.115build_dir = check_path(args.BuildDir)116ir_file = check_path(args.IRFile)117llc_bin = check_bin(build_dir, "llc")118opt_bin = check_bin(build_dir, "opt")119bugpoint_bin = check_bin(build_dir, "bugpoint")120
121# Run llc to see if GlobalISel fails.122log("Running llc...")123res = run_llc(llc_bin, ir_file)124if res == 0:125log_err("Expected failure")126raise127hr()128log("GlobalISel failed, {}: {}.".format(res[0], res[1]))129tmp = tempfile.NamedTemporaryFile()130log("Writing error to {} for bugpoint.".format(tmp.name))131tmp.write(";".join(res))132tmp.flush()133hr()134
135# Run bugpoint.136log("Running bugpoint...")137run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp.name, ir_file)138hr()139log("Done!")140hr()141output_file = "bugpoint-reduced-simplified.bc"142log("Run llvm-dis to disassemble the output:")143log("$ {}/bin/llvm-dis -o - {}".format(build_dir, output_file))144log("Run llc to reproduce the problem:")145log(146"$ {}/bin/llc -o - -global-isel "147"-pass-remarks-missed=gisel {}".format(build_dir, output_file)148)149
150
151if __name__ == "__main__":152main()153