Skip to content

Instantly share code, notes, and snippets.

@ichn-hu
Created January 13, 2019 19:55
Show Gist options
  • Save ichn-hu/480ef707a1cedf69828893ae910a8e74 to your computer and use it in GitHub Desktop.
Save ichn-hu/480ef707a1cedf69828893ae910a8e74 to your computer and use it in GitHub Desktop.
test_and_compare
from pathlib import Path
dce_cmd = "{opt_path} -load {lib_path} -mem2reg -live {test_file} -S"
import os
import subprocess
import argparse
import pickle
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--opt_path", type=str, help="the executable for opt, if not specified will just use \"opt\"", default="opt")
arg_parser.add_argument("--lib_path", type=str, required=True, help="The path for the generated lib file")
arg_parser.add_argument("--test_dir", type=str, help="The absolute path of llvm/test, will test all the ll file under this dir", default=None)
arg_parser.add_argument("--test_file", type=str, help="If specified, will test this file only", default=None)
arg_parser.add_argument("--output_to", type=str, default=None, help="Specified a place to output the result --generated livesets and the eliminated code, if not specified will output to stdout")
args = arg_parser.parse_args()
error_list = []
def get_output(test_file):
print("Testing {} ...".format(test_file))
cmd = dce_cmd.format(opt_path=args.opt_path, lib_path=args.lib_path, test_file=test_file)
print(cmd)
try:
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
except:
# import ipdb
# ipdb.set_trace()
error_list.append(test_file)
output = "Error for {}".format(test_file)
print("Done")
return output
def get_filename_from_path(p):
concat_len = len(args.test_dir)
fn = p[concat_len:]
return fn.strip('/')
if __name__ == "__main__":
result = {}
if args.test_dir is not None:
path_list = list(Path(args.test_dir).glob('**/*.ll'))
for i, p in enumerate(path_list):
print("{:4}/{}".format(i, len(path_list)))
p = str(p)
output = get_output(p)
result[get_filename_from_path(p)] = output
else:
result[args.test_file] = get_output(args.test_file)
if args.output_to:
out_dir = os.path.dirname(args.output_to)
os.makedirs(out_dir, exist_ok=True)
pickle.dumps(result, open(args.output_to, "wb"))
else:
for key in sorted(result.keys()):
print("Result for {}: {}\n".format(key, result[key].decode('utf-8')))
if len(error_list) != 0:
import ipdb
ipdb.set_trace()
print(error_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment