-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexebench_make_llvm_ir.py
More file actions
71 lines (62 loc) · 1.97 KB
/
Copy pathexebench_make_llvm_ir.py
File metadata and controls
71 lines (62 loc) · 1.97 KB
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
import base64
import pandas as pd
import subprocess
import ast
import swifter
import dask
import tempfile
import os
import re
from multiprocessing.pool import ThreadPool
dask.config.set(pool=ThreadPool(20))
def c_to_llvm_ir(c_code: str) -> str:
c_file_name = None
llvm_file_name = None
try:
c_file = tempfile.NamedTemporaryFile(suffix=".c", delete=False)
c_file.write(c_code.encode("utf-8"))
c_file_name = c_file.name
print(c_file_name)
with open(c_file_name, "r") as c_file:
c__ = c_file.read()
print(c__)
llvm_file = tempfile.NamedTemporaryFile(suffix=".ll", delete=False)
llvm_file_name = llvm_file.name
compile_command = [
"clang",
"-S",
"-emit-llvm",
c_file_name,
"-Xclang",
"-disable-O0-optnone",
"-o",
llvm_file_name,
]
try:
subprocess.run(compile_command, check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as e:
print(f"Error during compilation: {e.stderr}")
return None
with open(llvm_file_name, "r") as llvm_file:
lines = llvm_file.readlines()
llvm_ir = "".join(lines)
finally:
if c_file_name and os.path.exists(c_file_name):
os.remove(c_file_name)
if llvm_file_name and os.path.exists(llvm_file_name):
os.remove(llvm_file_name)
print("****************")
return llvm_ir
column_names = ["func_def"]
data_types = {"func_def": str}
df = pd.read_csv(
"exebench_codes.csv", header=None, names=column_names, dtype=data_types
)
df.info()
df = df.dropna(subset=["func_def"])
"""num_rows = len(df)
for i in range(num_rows):
df["llvm-ir"][i] = c_to_llvm_ir(df["func_def"][i])"""
df["llvm-ir"] = df["func_def"].apply(c_to_llvm_ir)
# Save the resulting DataFrame to a new CSV file
df.to_csv("exebench_codes_llvm_ir.csv", index=False)