Datasets:

Modalities:
Text
Formats:
json
Size:
< 1K
Tags:
code
DOI:
Libraries:
Datasets
pandas
License:
File size: 13,154 Bytes
8d5ae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607dd77
 
8d5ae49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607dd77
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/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 subprocess
import re
import tempfile

# NOTE: llvm-lit requires psutil
import psutil

llvm_dir = os.environ["LAB_LLVM_DIR"]
llvm_build_dir = os.environ["LAB_LLVM_BUILD_DIR"]
llvm_alive_tv = os.environ["LAB_LLVM_ALIVE_TV"]
dataset_dir = os.environ["LAB_DATASET_DIR"]
if '--quiet' not in subprocess.run(["ninja", "--help"], capture_output=True).stderr.decode("utf-8"):
    raise RuntimeError("Please update ninja to version 1.11.0 or later")

def git_execute(args):
    return subprocess.check_output(
        ["git", "-C", llvm_dir] + args, cwd=llvm_dir, stderr=subprocess.DEVNULL
    ).decode("utf-8")


def reset(commit):
    git_execute(["restore", "--staged", "."])
    git_execute(["clean", "-fdx"])
    git_execute(["checkout", "."])
    git_execute(["checkout", commit])


def infer_related_components(diff_files):
    prefixes = [
        "llvm/lib/Analysis/",
        "llvm/lib/Transforms/Scalar/",
        "llvm/lib/Transforms/Vectorize/",
        "llvm/lib/Transforms/Utils/",
        "llvm/lib/Transforms/IPO/",
        "llvm/lib/Transforms/",
        "llvm/lib/IR/",
    ]
    components = set()
    for file in diff_files:
        for prefix in prefixes:
            if file.startswith(prefix):
                component_name = (
                    file.removeprefix(prefix)
                    .split("/")[0]
                    .removesuffix(".cpp")
                    .removesuffix(".h")
                )
                if component_name != "":
                    if (
                        component_name.startswith("VPlan")
                        or component_name.startswith("LoopVectoriz")
                        or component_name.startswith("VPRecipe")
                    ):
                        component_name = "LoopVectorize"
                    if component_name.startswith("ScalarEvolution"):
                        component_name = "ScalarEvolution"
                    if component_name.startswith("ConstantFold"):
                        component_name = "ConstantFold"
                    if "AliasAnalysis" in component_name:
                        component_name = "AliasAnalysis"
                    if component_name.startswith("Attributor"):
                        component_name = "Attributor"
                    if file.startswith("llvm/lib/IR"):
                        component_name = "IR"
                    components.add(component_name)
                    break
    return components


def get_langref_desc(keywords, commit):
    langref = str(git_execute(["show", f"{commit}:llvm/docs/LangRef.rst"]))
    desc = dict()
    sep = ".. _"
    for keyword in keywords:
        matched = re.search(f"\n'``{keyword}.+\n\\^", langref)
        if matched is None:
            continue
        beg, end = matched.span()
        beg = langref.rfind(sep, None, beg)
        end = langref.find(sep, end)
        desc[keyword] = langref[beg:end]
    return desc


def decode_output(output):
    if output is None:
        return ""
    return output.decode()


def build(max_build_jobs: int):
    os.makedirs(llvm_build_dir, exist_ok=True)
    log = ""
    try:
        log += subprocess.check_output(
            [
                "cmake",
                "-S",
                llvm_dir + "/llvm",
                "-G",
                "Ninja",
                "-DBUILD_SHARED_LIBS=ON",
                "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
                "-DCMAKE_C_COMPILER_LAUNCHER=ccache",
                "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache",
                "-DLLVM_ENABLE_ASSERTIONS=ON",
                "-DLLVM_ABI_BREAKING_CHECKS=WITH_ASSERTS",
                "-DLLVM_ENABLE_WARNINGS=OFF",
                "-DLLVM_APPEND_VC_REV=OFF",
                "-DLLVM_TARGETS_TO_BUILD='X86;RISCV;AArch64;SystemZ;Hexagon'",
                "-DLLVM_PARALLEL_LINK_JOBS=4",
                "-DLLVM_INCLUDE_EXAMPLES=OFF",
            ],
            stderr=subprocess.STDOUT,
            cwd=llvm_build_dir,
        ).decode()
        log += subprocess.check_output(
            ["cmake", "--build", ".", "-j", str(max_build_jobs), "--", "--quiet"],
            stderr=subprocess.STDOUT,
            cwd=llvm_build_dir,
        ).decode()
        return (True, log)
    except subprocess.CalledProcessError as e:
        return (False, log + "\n" + decode_output(e.output))


def is_valid_comment(comment):
    if comment["author"] == "llvmbot":
        return False
    if comment["body"].startswith("/cherry-pick"):
        return False
    return True


def apply(patch: str):
    try:
        out = subprocess.check_output(
            ["git", "-C", llvm_dir, "apply"],
            cwd=llvm_dir,
            stderr=subprocess.STDOUT,
            input=patch.encode(),
        ).decode("utf-8")
        return (True, out)
    except subprocess.CalledProcessError as e:
        return (False, str(e) + "\n" + decode_output(e.output))


def filter_out_unsupported_feats(src: str):
    return src.replace(" noalias ", " ")


def alive2_check(src: str, tgt: str, additional_args: str):
    try:
        with tempfile.NamedTemporaryFile() as src_file:
            with tempfile.NamedTemporaryFile() as tgt_file:
                src = filter_out_unsupported_feats(src)
                tgt = filter_out_unsupported_feats(tgt)
                src_file.write(src.encode())
                tgt_file.write(tgt.encode())
                src_file.flush()
                tgt_file.flush()

                args = [
                    llvm_alive_tv,
                    "--disable-undef-input",
                    src_file.name,
                    tgt_file.name,
                ]
                if additional_args != None:
                    args += additional_args.strip().split(" ")

                out = subprocess.check_output(args, stderr=subprocess.STDOUT).decode()
                success = (
                    "0 incorrect transformations" in out
                    and "0 failed-to-prove transformations" in out
                    and "0 Alive2 errors" in out
                )
                return (success, {"src": src, "tgt": tgt, "log": out})
    except subprocess.CalledProcessError as e:
        return (False, str(e) + "\n" + decode_output(e.output))


def lli_check(tgt: bytes, expected_out: str):
    try:
        out = subprocess.check_output(
            [os.path.join(llvm_build_dir, "bin/lli")],
            input=tgt,
            timeout=10.0,
            stderr=subprocess.STDOUT,
        ).decode()
        if out == expected_out:
            return (True, "success")
        return (False, f"Expected '{expected_out}', but got '{out}'")
    except subprocess.CalledProcessError as e:
        return (False, str(e) + "\n" + decode_output(e.output))
    except subprocess.TimeoutExpired as e:
        return (False, str(e) + "\n" + decode_output(e.output))


def copy_triple(input: str, out: bytes):
    triple_pattern = "target triple ="
    if triple_pattern in input:
        return input
    ref_out = out.decode()
    if triple_pattern in ref_out:
        triple_pos = ref_out.find(triple_pattern)
        triple_line = ref_out[triple_pos : ref_out.find("\n", triple_pos) + 1]
        return triple_line + input
    return input


def copy_datalayout(input: str, out: bytes):
    datalayout_pattern = "target datalayout ="
    if datalayout_pattern in input:
        return input
    ref_out = out.decode()
    if datalayout_pattern in ref_out:
        datalayout_pos = ref_out.find(datalayout_pattern)
        datalayout_line = ref_out[
            datalayout_pos : ref_out.find("\n", datalayout_pos) + 1
        ]
        return datalayout_line + input
    return input


def verify_dispatch(
    repro: bool,
    input: str,
    args: str,
    type: str,
    additional_args: str,
    lli_expected_out: str,
):
    args_list = list(
        filter(
            lambda x: x != "",
            args.replace("< ", " ")
            .replace("%s", "-")
            .replace("2>&1", "")
            .replace("'", "")
            .replace('"', "")
            .replace("opt", os.path.join(llvm_build_dir, "bin/opt"), 1)
            .strip()
            .split(" "),
        )
    )
    try:
        out = subprocess.run(
            args_list,
            input=input.encode(),
            timeout=10.0,
            check=True,
            capture_output=True,
        )
        if type == "miscompilation":
            output = out.stdout
            if lli_expected_out is not None:
                res, log = lli_check(output, lli_expected_out)
            else:
                new_input = copy_triple(input, output)
                new_input = copy_datalayout(new_input, output)
                res, log = alive2_check(new_input, output.decode(), additional_args)
            if repro == True:
                res = not res
            if isinstance(log, str):
                log = decode_output(out.stderr) + "\n" + log
            else:
                log["opt_stderr"] = decode_output(out.stderr)
            return (res, log)
        return (not repro, "success\n" + decode_output(out.stderr))
    except subprocess.CalledProcessError as e:
        return (
            repro and type == "crash",
            str(e) + "\n" + decode_output(e.output) + "\n" + decode_output(e.stderr),
        )
    except subprocess.TimeoutExpired as e:
        return (
            repro and type == "hang",
            str(e) + "\n" + decode_output(e.output) + "\n" + decode_output(e.stderr),
        )


def verify_test_group(repro: bool, input, type: str):
    test_res = []
    overall_test_res = not repro
    for test in input:
        file = test["file"]
        commands = test["commands"]
        tests = test["tests"]
        for subtest in tests:
            name = subtest["test_name"]
            body = subtest["test_body"]
            for args in commands:
                res, log = verify_dispatch(
                    repro,
                    body,
                    args,
                    type,
                    subtest.get("additional_args"),
                    subtest.get("lli_expected_out"),
                )
                test_res.append(
                    {
                        "file": file,
                        "args": args,
                        "name": name,
                        "body": body,
                        "result": res,
                        "log": log,
                    }
                )
                if repro:
                    overall_test_res = overall_test_res or res
                else:
                    overall_test_res = overall_test_res and res
    return (overall_test_res, test_res)


def verify_lit(test_commit, dirs, max_test_jobs):
    try:
        git_execute(["checkout", test_commit, "llvm/test"])
        test_dirs = [os.path.join(llvm_dir, x) for x in dirs]
        out = subprocess.check_output(
            [
                os.path.join(llvm_build_dir, "bin/llvm-lit"),
                "--no-progress-bar",
                "-j",
                str(max_test_jobs),
                "--max-failures",
                "1",
                "--order",
                "lexical",
                "-sv",
            ]
            + test_dirs,
            stderr=subprocess.STDOUT,
            timeout=300.0,
        ).decode()
        return (True, out)
    except subprocess.CalledProcessError as e:
        return (False, str(e) + "\n" + decode_output(e.output))
    except subprocess.TimeoutExpired as e:
        return (False, str(e) + "\n" + decode_output(e.output))


def get_first_failed_test(test_result):
    for res in test_result:
        if not res["result"]:
            return res
    return None


def is_valid_fix(commit):
    if commit is None:
        return False
    try:
        branches = git_execute(["branch", "--contains", commit])
        if "main\n" not in branches:
            return False
        changed_files = (
            subprocess.check_output(
                [
                    "git",
                    "-C",
                    llvm_dir,
                    "show",
                    "--name-only",
                    "--format=",
                    commit,
                ],
                stderr=subprocess.DEVNULL,
            )
            .decode()
            .strip()
        )
        if "llvm/test/" in changed_files and (
            "llvm/lib/" in changed_files or "llvm/include/" in changed_files
        ):
            return True
    except subprocess.CalledProcessError:
        pass
    return False