[AArch64] Fix SDNode type mismatches between *.td files and ISel (#116523)
[llvm-project.git] / llvm / utils / bugpoint_gisel_reducer.py
blob116ec792e921d8794d82f59e41d9fc2411153acc
1 #!/usr/bin/env python
3 """Reduces GlobalISel failures.
5 This script is a utility to reduce tests that GlobalISel
6 fails to compile.
8 It runs llc to get the error message using a regex and creates
9 a custom command to check that specific error. Then, it runs bugpoint
10 with the custom command.
12 """
13 from __future__ import print_function
14 import argparse
15 import re
16 import subprocess
17 import sys
18 import tempfile
19 import os
22 def log(msg):
23 print(msg)
26 def hr():
27 log("-" * 50)
30 def log_err(msg):
31 print("ERROR: {}".format(msg), file=sys.stderr)
34 def check_path(path):
35 if not os.path.exists(path):
36 log_err("{} does not exist.".format(path))
37 raise
38 return path
41 def check_bin(build_dir, bin_name):
42 file_name = "{}/bin/{}".format(build_dir, bin_name)
43 return check_path(file_name)
46 def run_llc(llc, irfile):
47 pr = subprocess.Popen(
48 [llc, "-o", "-", "-global-isel", "-pass-remarks-missed=gisel", irfile],
49 stdout=subprocess.PIPE,
50 stderr=subprocess.PIPE,
52 out, err = pr.communicate()
53 res = pr.wait()
54 if res == 0:
55 return 0
56 re_err = re.compile(
57 r"LLVM ERROR: ([a-z\s]+):.*(G_INTRINSIC[_A-Z]* <intrinsic:@[a-zA-Z0-9\.]+>|G_[A-Z_]+)"
59 match = re_err.match(err)
60 if not match:
61 return 0
62 else:
63 return [match.group(1), match.group(2)]
66 def run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp, ir_file):
67 compileCmd = "-compile-command={} -c {} {}".format(
68 os.path.realpath(__file__), llc_bin, tmp
70 pr = subprocess.Popen(
72 bugpoint_bin,
73 "-compile-custom",
74 compileCmd,
75 "-opt-command={}".format(opt_bin),
76 ir_file,
79 res = pr.wait()
80 if res != 0:
81 log_err("Unable to reduce the test.")
82 raise
85 def run_bugpoint_check():
86 path_to_llc = sys.argv[2]
87 path_to_err = sys.argv[3]
88 path_to_ir = sys.argv[4]
89 with open(path_to_err, "r") as f:
90 err = f.read()
91 res = run_llc(path_to_llc, path_to_ir)
92 if res == 0:
93 return 0
94 log("GlobalISed failed, {}: {}".format(res[0], res[1]))
95 if res != err.split(";"):
96 return 0
97 else:
98 return 1
101 def main():
102 # Check if this is called by bugpoint.
103 if len(sys.argv) == 5 and sys.argv[1] == "-c":
104 sys.exit(run_bugpoint_check())
106 # Parse arguments.
107 parser = argparse.ArgumentParser(
108 description=__doc__, formatter_class=argparse.RawTextHelpFormatter
110 parser.add_argument("BuildDir", help="Path to LLVM build directory")
111 parser.add_argument("IRFile", help="Path to the input IR file")
112 args = parser.parse_args()
114 # Check if the binaries exist.
115 build_dir = check_path(args.BuildDir)
116 ir_file = check_path(args.IRFile)
117 llc_bin = check_bin(build_dir, "llc")
118 opt_bin = check_bin(build_dir, "opt")
119 bugpoint_bin = check_bin(build_dir, "bugpoint")
121 # Run llc to see if GlobalISel fails.
122 log("Running llc...")
123 res = run_llc(llc_bin, ir_file)
124 if res == 0:
125 log_err("Expected failure")
126 raise
127 hr()
128 log("GlobalISel failed, {}: {}.".format(res[0], res[1]))
129 tmp = tempfile.NamedTemporaryFile()
130 log("Writing error to {} for bugpoint.".format(tmp.name))
131 tmp.write(";".join(res))
132 tmp.flush()
133 hr()
135 # Run bugpoint.
136 log("Running bugpoint...")
137 run_bugpoint(bugpoint_bin, llc_bin, opt_bin, tmp.name, ir_file)
138 hr()
139 log("Done!")
140 hr()
141 output_file = "bugpoint-reduced-simplified.bc"
142 log("Run llvm-dis to disassemble the output:")
143 log("$ {}/bin/llvm-dis -o - {}".format(build_dir, output_file))
144 log("Run llc to reproduce the problem:")
145 log(
146 "$ {}/bin/llc -o - -global-isel "
147 "-pass-remarks-missed=gisel {}".format(build_dir, output_file)
151 if __name__ == "__main__":
152 main()