Datasets:

Modalities:
Text
Formats:
json
Size:
< 1K
Tags:
code
DOI:
Libraries:
Datasets
pandas
License:
File size: 13,052 Bytes
8d5ae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d4f4b0
8d5ae49
 
 
 
 
 
adb8cb5
d9acdca
8d5ae49
 
 
 
 
 
 
 
adb8cb5
8d5ae49
 
 
 
 
d9acdca
8d5ae49
 
 
 
 
 
 
 
 
adb8cb5
8d5ae49
d9acdca
8d5ae49
 
 
 
 
 
 
d9acdca
8d5ae49
df07b03
8d5ae49
 
 
d9acdca
8d5ae49
d631641
d9acdca
9d95ddd
8d5ae49
 
 
adb8cb5
8d5ae49
 
 
 
 
 
d9acdca
 
 
8a23f90
db159fa
d028147
73b9479
 
db159fa
27f15f9
 
3588ce3
 
cf11aa0
f868ab3
1c91cb5
4893cfc
 
416886a
cf11aa0
adb8cb5
81f5f13
b87b06a
 
 
 
 
 
 
0619a56
a9940a6
 
81f5f13
7a46411
dc185bc
 
 
 
8d5ae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3588ce3
8d5ae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/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 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,  # Cannot reproduce with alive2
    "77842": None,  # Threshold problem
    "78024": None,  # Reverted
    "79137": None,  # Cannot reproduce with alive2
    "80836": "1c10821022f1799452065fb57474e894e2562b7f",
    "81561": "97088b2ab2184ad4bd64f59fba0b92b70468b10d",
    "81793": None,  # Cannot reproduce with alive2
    "81872": None,  # Multi-commit fix
    "84807": None,  # IPO miscompilation
    "85185": None,  # Duplicate of #79742
    "85568": None,  # Object bug
    "86280": None,  # Object bug
    "87534": None,  # IPO miscompilation
    "88640": None,  # Invalid reproducer
    "88804": None,  # Duplicate of #88297
    "91417": "645fb04a3389e69801d401e669eae9ee42d70217",  # Use the second fix
    "92217": None,  # See also https://github.com/AliveToolkit/alive2/issues/1037
    "93017": None,  # Constant expr
    "96197": None,  # Cannot reproduce the crash
    "96857": None,  # miscompilation:undef
    "97702": None,  # uninit mem
    "97837": None,  # Alive2 bug e4508ba85747eb3a5e002915e544d2e08e751425
    "98133": None,  # Invalid reproducer
    "99436": None,  # Complicated fix
    "99625": None,  # Duplicate of #94328
    "102784": None,  # Multi-commit fix
    "104397": None,  # Invalid reproducer
    "104718": None,  # Test change
    "105713": None,  # Duplicate of #104714
    "106909": None,  # Cannot reproduce with alive2
    "107037": None,  # Multi-commit fix
    "107501": None,  # Complicated fix
    "108618": None,  # Multi-commit fix
    "108854": None,  # Multi-commit fix
    "108936": None,  # Codegen issue
    "109581": None,  # Too many unrelated changes
    "110440": None,  # Duplicate of #109528
    "110819": None,  # Outdated issue
    "111585": None,  # Cannot reproduce with alive2
    "111709": None,  # Cannot reproduce with alive2
    "112633": None,  # Multi-commit fix
    "113301": None,  # miscompilation:undef
    "113425": None,  # miscompilation:undef
    "113989": None,  # Cannot confirm fix with alive2
    "114181": None,  # Duplicate of #112666
    "114905": "889215a30ed60474e573f9632d1fa362dfa1b04e",  # Use the second fix
    "115976": None,  # Cannot reproduce with alive2
    "116144": None,  # Cannot reproduce with alive2
    "116668": None,  # Cannot reproduce with alive2
    "117170": None,  # Cannot reproduce with alive2
    "119173": "30f3752e54fa7cd595a434a985efbe9a7abe9b65",
    "119646": None,  # Cannot reproduce with alive2
    "120932": None,  # IPO miscompilation
    "121430": None,  # Alive2 bug https://github.com/AliveToolkit/alive2/pull/1155
    "121583": None,  # Removed fold
    "122166": None,  # Duplicate of #117308
    "122324": None,  # Cannot confirm fix with alive2
    "122430": None,  # Cannot confirm fix with alive2
    "122467": None,  # Invalid reproducer
    "122537": None,  # Clang codegen issue
    "122602": None,  # Duplicate of #122496
    "123920": None,  # Cannot reproduce with alive2
    "124213": None,  # Multi-commit fix
    "124578": None,  # OpenMP support bug
    "125259": None,  # Reproducer is too large
    "125369": None,  # Reverted
    "125374": None,  # Duplicate of #119173
    "125400": None,  # Reverted
    "126409": None,  # Reverted
    "126909": None,  # Cannot reproduce the crash
    "126974": "29f3a352068ce562bcb65e18a676c82a9991583c",
    "127173": None,  # Reverted
    "127177": None,  # Reverted
    "127220": None,  # Reverted
    "129244": None,  # Cannot reproduce the miscompilation
    "130082": None,  # Non-deterministic bug
    "130632": None,  # Reverts #108535
    "131355": None,  # Non-deterministic bug
    "133984": None,  # Not supported by Alive2
    "134411": None,  # Reverted
    "135113": None,  # Not supported by Alive2
    "135410": None,  # Cannot reproduce with Alive2
    "135531": None,  # Cannot reproduce with Alive2
    "137164": None,  # Cannot reproduce the crash as it requires llvm to build with hardened libc++
    "138178": None,  # Cannot reproduce the crash as it requires llvm to build with hardened libc++
    "138194": None,  # LTO bug
    "138509": None,  # exponential time consumption
    "138819": None,  # Alive2 timeout
    "138923": None,  # Invalid reproducer
    "139065": None,  # Duplicate of #139060
    "139289": None,  # Duplicate of #139103
    "139308": None,  # Duplicate of #139103
    "139729": None,  # Non-deterministic bug
    "140215": None,  # Cannot reproduce with Alive2
    "140238": None,  # Cannot reproduce with Alive2
    "140992": None,  # Meta issue
    "142091": None,  # Duplicate of #141968
    "148228": None,  # Alive2 cannot unroll nested loops
    "148431": None,  # Cannot reproduce the crash
    "149347": None,  # Reverted
    "149679": None,  # Duplicate of #147869
    "151456": None,  # Cannot reproduce the crash
    "152683": None,  # Cannot reproduce the crash
}

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)

# Component level
components = llvm_helper.infer_related_components(changed_files.split("\n"))
# Extract patch
patch = llvm_helper.git_execute(
    ["show", fix_commit, "--", "llvm/lib/*", "llvm/include/*"]
)
patchset = PatchSet(patch)
# Line level
bug_location_lineno = {}
for file in patchset:
    location = hints.get_line_loc(file)
    if len(location) != 0:
        bug_location_lineno[file.path] = location


# Function level

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] = sorted(modified_funcs_valid)

# Extract tests
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]+)\(")
# Workaround for invalid IR (constant expr/x86_mmx)
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})

# Extract full issue context
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,
}

# Write to file
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}")