|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os |
|
import requests |
|
import json |
|
import llvm_helper |
|
import sys |
|
import hints |
|
from unidiff import PatchSet |
|
import re |
|
import subprocess |
|
|
|
github_token = os.environ["LAB_GITHUB_TOKEN"] |
|
session = requests.Session() |
|
session.headers.update( |
|
{ |
|
"X-GitHub-Api-Version": "2022-11-28", |
|
"Authorization": f"Bearer {github_token}", |
|
"Accept": "application/vnd.github+json", |
|
} |
|
) |
|
subprocess.check_output(["llvm-extract", "--version"]) |
|
|
|
issue_id = sys.argv[1] |
|
override = False |
|
if len(sys.argv) == 3 and sys.argv[2] == "-f": |
|
print("Force override") |
|
override = True |
|
|
|
data_json_path = os.path.join(llvm_helper.dataset_dir, f"{issue_id}.json") |
|
if not override and os.path.exists(data_json_path): |
|
print(f"Item {issue_id}.json already exists") |
|
exit(0) |
|
|
|
issue_url = f"https://api.github.com/repos/llvm/llvm-project/issues/{issue_id}" |
|
print(f"Fetching {issue_url}") |
|
issue = session.get(issue_url).json() |
|
if issue["state"] != "closed" or issue["state_reason"] != "completed": |
|
print("The issue/PR should be closed") |
|
exit(1) |
|
|
|
knowledge_cutoff = issue["created_at"] |
|
timeline = session.get(issue["timeline_url"]).json() |
|
fix_commit = None |
|
fix_commit_map = { |
|
"76789": None, |
|
"78024": None, |
|
"79137": None, |
|
"80836": "1c10821022f1799452065fb57474e894e2562b7f", |
|
"81561": "97088b2ab2184ad4bd64f59fba0b92b70468b10d", |
|
"81793": None, |
|
"81872": None, |
|
"85185": None, |
|
"85568": None, |
|
"86280": None, |
|
"87534": None, |
|
"88640": None, |
|
"88804": None, |
|
"91417": "645fb04a3389e69801d401e669eae9ee42d70217", |
|
"92217": None, |
|
"93017": None, |
|
"96857": None, |
|
"97702": None, |
|
"97837": None, |
|
"98133": None, |
|
"99436": None, |
|
"99625": None, |
|
"102784": None, |
|
"104397": None, |
|
"104718": None, |
|
"105713": None, |
|
"106909": None, |
|
"107037": None, |
|
"107501": None, |
|
"108618": None, |
|
"108854": None, |
|
"109581": None, |
|
"110440": None, |
|
"110819": None, |
|
"111585": None, |
|
"111709": None, |
|
"112633": None, |
|
"113301": None, |
|
"113425": None, |
|
"113989": None, |
|
"114181": None, |
|
"114905": "889215a30ed60474e573f9632d1fa362dfa1b04e", |
|
"116144": None, |
|
"116668": None, |
|
"117170": None, |
|
"119173": "30f3752e54fa7cd595a434a985efbe9a7abe9b65", |
|
"119646": None, |
|
"121430": None, |
|
"121583": None, |
|
"122166": None, |
|
"122324": None, |
|
"122430": None, |
|
"122537": None, |
|
"122602": None, |
|
"123920": None, |
|
"124213": None, |
|
"124578": None, |
|
"125259": None, |
|
"125369": None, |
|
"125374": None, |
|
"125400": None, |
|
"126409": None, |
|
} |
|
|
|
if issue_id in fix_commit_map: |
|
fix_commit = fix_commit_map[issue_id] |
|
if fix_commit is None: |
|
print("This issue is marked as invalid") |
|
exit(0) |
|
else: |
|
for event in timeline: |
|
if event["event"] == "closed": |
|
commit_id = event["commit_id"] |
|
if commit_id is not None: |
|
fix_commit = commit_id |
|
break |
|
if event["event"] == "referenced" and fix_commit is None: |
|
commit = event["commit_id"] |
|
if llvm_helper.is_valid_fix(commit): |
|
fix_commit = commit |
|
|
|
if fix_commit is None: |
|
print("Cannot find the fix commit") |
|
exit(0) |
|
|
|
issue_type = "unknown" |
|
for label in issue["labels"]: |
|
label_name = label["name"] |
|
if label_name == "miscompilation": |
|
issue_type = "miscompilation" |
|
if "crash" in label_name: |
|
issue_type = "crash" |
|
if "hang" in label_name: |
|
issue_type = "hang" |
|
if label_name in [ |
|
"invalid", |
|
"wontfix", |
|
"duplicate", |
|
"undefined behavior", |
|
"miscompilation:undef", |
|
]: |
|
print("This issue is marked as invalid") |
|
exit(1) |
|
|
|
base_commit = llvm_helper.git_execute(["rev-parse", fix_commit + "~"]).strip() |
|
changed_files = llvm_helper.git_execute( |
|
["show", "--name-only", "--format=", fix_commit] |
|
).strip() |
|
if "/AsmParser/" in changed_files or "/Bitcode/" in changed_files: |
|
print("This issue is marked as invalid") |
|
exit(0) |
|
|
|
|
|
components = llvm_helper.infer_related_components(changed_files.split("\n")) |
|
|
|
patch = llvm_helper.git_execute( |
|
["show", fix_commit, "--", "llvm/lib/*", "llvm/include/*"] |
|
) |
|
patchset = PatchSet(patch) |
|
|
|
bug_location_lineno = {} |
|
for file in patchset: |
|
location = hints.get_line_loc(file) |
|
if len(location) != 0: |
|
bug_location_lineno[file.path] = location |
|
|
|
|
|
|
|
|
|
bug_location_funcname = {} |
|
for file in patchset.modified_files: |
|
print(f"Parsing {file.path}") |
|
source_code = llvm_helper.git_execute(["show", f"{base_commit}:{file.path}"]) |
|
modified_funcs_valid = hints.get_funcname_loc(file, source_code) |
|
if len(modified_funcs_valid) != 0: |
|
bug_location_funcname[file.path] = list(modified_funcs_valid) |
|
|
|
|
|
test_patchset = PatchSet( |
|
llvm_helper.git_execute(["show", fix_commit, "--", "llvm/test/*"]) |
|
) |
|
|
|
|
|
def remove_target_suffix(path): |
|
targets = [ |
|
"X86", |
|
"AArch64", |
|
"ARM", |
|
"Mips", |
|
"RISCV", |
|
"PowerPC", |
|
"LoongArch", |
|
"AMDGPU", |
|
"SystemZ", |
|
"Hexagon", |
|
] |
|
for target in targets: |
|
path = path.removesuffix("/" + target) |
|
return path |
|
|
|
|
|
lit_test_dir = set( |
|
map( |
|
lambda x: remove_target_suffix(os.path.dirname(x)), |
|
filter(lambda x: x.count("llvm/test/"), changed_files.split("\n")), |
|
) |
|
) |
|
tests = [] |
|
runline_pattern = re.compile(r"; RUN: (.+)\| FileCheck") |
|
testname_pattern = re.compile(r"define .+ @([.\w]+)\(") |
|
|
|
retrieve_test_from_main = { |
|
"77553", |
|
"81793", |
|
"82052", |
|
"83127", |
|
"83931", |
|
"89500", |
|
"91178", |
|
} |
|
test_commit = "origin/main" if issue_id in retrieve_test_from_main else fix_commit |
|
for file in test_patchset: |
|
test_file = llvm_helper.git_execute(["show", f"{test_commit}:{file.path}"]) |
|
commands = [] |
|
for match in re.findall(runline_pattern, test_file): |
|
commands.append(match.strip()) |
|
if issue_type != "miscompilation" and file.is_added_file: |
|
print(file.path, "full") |
|
|
|
def is_valid_test_line(line: str): |
|
line = line.strip() |
|
if ( |
|
line.startswith("; NOTE") |
|
or line.startswith("; RUN") |
|
or line.startswith("; CHECK") |
|
): |
|
return False |
|
return True |
|
|
|
normalized_body = "\n".join(filter(is_valid_test_line, test_file.splitlines())) |
|
tests.append( |
|
{ |
|
"file": file.path, |
|
"commands": commands, |
|
"tests": [{"test_name": "<module>", "test_body": normalized_body}], |
|
} |
|
) |
|
continue |
|
test_names = set() |
|
for hunk in file: |
|
matched = re.search(testname_pattern, hunk.section_header) |
|
if matched: |
|
test_names.add(matched.group(1)) |
|
for line in hunk.target: |
|
for match in re.findall(testname_pattern, line): |
|
test_names.add(match.strip()) |
|
print(file.path, test_names) |
|
subtests = [] |
|
for test_name in test_names: |
|
try: |
|
test_body = subprocess.check_output( |
|
["llvm-extract", f"--func={test_name}", "-S", "-"], |
|
input=test_file.encode(), |
|
).decode() |
|
test_body = test_body.removeprefix( |
|
"; ModuleID = '<stdin>'\nsource_filename = \"<stdin>\"\n" |
|
).removeprefix("\n") |
|
subtests.append( |
|
{ |
|
"test_name": test_name, |
|
"test_body": test_body, |
|
} |
|
) |
|
except Exception: |
|
pass |
|
if len(subtests) != 0: |
|
tests.append({"file": file.path, "commands": commands, "tests": subtests}) |
|
|
|
|
|
issue_comments = [] |
|
comments = session.get(issue["comments_url"]).json() |
|
for comment in comments: |
|
comment_obj = { |
|
"author": comment["user"]["login"], |
|
"body": comment["body"], |
|
} |
|
if llvm_helper.is_valid_comment(comment_obj): |
|
issue_comments.append(comment_obj) |
|
normalized_issue = { |
|
"title": issue["title"], |
|
"body": issue["body"], |
|
"author": issue["user"]["login"], |
|
"labels": list(map(lambda x: x["name"], issue["labels"])), |
|
"comments": issue_comments, |
|
} |
|
|
|
|
|
metadata = { |
|
"bug_id": issue_id, |
|
"issue_url": issue["html_url"], |
|
"bug_type": issue_type, |
|
"base_commit": base_commit, |
|
"knowledge_cutoff": knowledge_cutoff, |
|
"lit_test_dir": sorted(lit_test_dir), |
|
"hints": { |
|
"fix_commit": fix_commit, |
|
"components": sorted(components), |
|
"bug_location_lineno": bug_location_lineno, |
|
"bug_location_funcname": bug_location_funcname, |
|
}, |
|
"patch": patch, |
|
"tests": tests, |
|
"issue": normalized_issue, |
|
} |
|
print(json.dumps(metadata, indent=2)) |
|
with open(data_json_path, "w") as f: |
|
json.dump(metadata, f, indent=2) |
|
print(f"Saved to {data_json_path}") |
|
|