File size: 6,103 Bytes
bfa61be a5cb329 bfa61be be19117 bfa61be 29fe9dd bf4a3cd 82d5002 bf4a3cd bfa61be be19117 a5cb329 be19117 29fe9dd be19117 29fe9dd be19117 29fe9dd be19117 29fe9dd be19117 bfa61be 29fe9dd bfa61be |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
#!/usr/bin/env python3
# Copyright 2025 Yingwei Zheng
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import llvm_helper
import json
import subprocess
import dateparser
from datetime import timezone
bisect_runner_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "bisect_runner.py"
)
def is_llvm_functional_change(commit: str) -> bool:
commit = commit.strip()
changed_files = llvm_helper.git_execute(
[
"show",
"--name-only",
"--format=",
commit,
"--",
"llvm/lib/*",
"llvm/include/*",
]
).strip()
return changed_files != ""
def bisect_issue(issue):
path = os.path.join(llvm_helper.dataset_dir, issue)
with open(path) as f:
data = json.load(f)
if data["bug_type"] == "hang":
# Not supported yet
return
if "bisect" in data:
return
commit = data["bisect"]
try:
commit_parsed = llvm_helper.git_execute(["rev-parse", commit]).strip()
if commit_parsed == commit:
return
except subprocess.CalledProcessError:
print(f"Previous bisect commit({commit}) is invalid. Redo bisect...")
pass
print(data["issue"]["title"])
try:
base_commit = data["base_commit"] # bad
base_commit_time = dateparser.parse(
llvm_helper.git_execute(["show", "-s", "--format=%ci", base_commit]).strip()
).astimezone(timezone.utc)
report_time = dateparser.parse(data["knowledge_cutoff"]).astimezone(
timezone.utc
)
print("Report time:", report_time)
print("Base commit time:", base_commit_time)
if report_time < base_commit_time:
commit_before_report = llvm_helper.git_execute(
[
"log",
"--before",
report_time.isoformat(),
"--oneline",
"-n",
"1",
"--no-abbrev",
"--format=%H",
]
).strip()
if (
base_commit != commit_before_report
and subprocess.run(
[bisect_runner_file, path, commit_before_report]
).returncode
== 1
):
base_commit = commit_before_report
print("Adjusted base commit to", base_commit)
else:
print("Use original base commit (unavailable)")
else:
print("Use original base commit (newer)")
good_commit = None
offset = 100
while offset <= 204800: # ~5 years
commit_sha = llvm_helper.git_execute(
["rev-parse", f"{base_commit}~{offset}"]
).strip()
if subprocess.run([bisect_runner_file, path, commit_sha]).returncode == 0:
good_commit = commit_sha
break
offset = int(offset * 1.6)
if good_commit is None:
raise RuntimeError("Cannot find a good commit")
print("BAD", base_commit, "GOOD", good_commit)
llvm_helper.git_execute(["bisect", "reset"])
llvm_helper.git_execute(
["bisect", "start", "--no-checkout", base_commit, good_commit]
)
out = subprocess.check_output(
[
"git",
"-C",
llvm_helper.llvm_dir,
"bisect",
"run",
bisect_runner_file,
path,
],
cwd=llvm_helper.llvm_dir,
timeout=600.0,
).decode()
if not out.endswith("bisect found first bad commit\n"):
raise RuntimeError("Bisect failed: " + out)
pos = out.rfind(" is the first bad commit\n")
if pos == -1:
raise RuntimeError("Bisect failed")
pos2 = out.rfind("\n", 0, pos)
if pos2 == -1:
raise RuntimeError("Bisect failed")
first_bad = out[pos2 + 1 : pos].strip()
print(first_bad)
data["bisect"] = first_bad
except subprocess.TimeoutExpired:
data["bisect"] = "Timeout"
print("Timeout")
except subprocess.CalledProcessError as e:
out = e.stdout.decode()
key = "The first bad commit could be any of:\n"
pos = out.rfind(key)
if pos == -1:
return
pos2 = out.find("We cannot bisect more!", pos)
if pos2 == -1:
return
candidates = out[pos + len(key) : pos2].strip().splitlines()
if len(candidates) > 1:
candidates = list(filter(is_llvm_functional_change, candidates))
# TODO: filter by pass name (not component name!)
if len(candidates) == 0:
data["bisect"] = "Unrelated"
elif len(candidates) == 1:
first_bad = candidates[0].strip()
print(first_bad)
data["bisect"] = first_bad
except Exception as e:
data["bisect"] = str(e)
print(e)
with open(path, "w") as f:
json.dump(data, f, indent=2)
task_list = []
if len(sys.argv) == 2:
task_list = [sys.argv[1] + ".json"]
else:
for name in os.listdir(llvm_helper.dataset_dir):
if name.endswith(".json"):
task_list.append(name)
task_list.sort(reverse=True)
for idx, task in enumerate(task_list):
print("Bisecting", idx + 1, task.removesuffix(".json"))
try:
bisect_issue(task)
except Exception as e:
print(e)
pass
|