2022-05-03 14:24:14 -05:00
|
|
|
import json
|
2022-05-03 11:59:04 -05:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
from os import walk
|
|
|
|
|
|
|
|
|
|
|
|
def run_command(command, cwd=None):
|
|
|
|
p = subprocess.Popen(command, cwd=cwd)
|
|
|
|
if p.wait() != 0:
|
|
|
|
print("command `{}` failed...".format(" ".join(command)))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2022-06-09 07:25:35 -05:00
|
|
|
def clone_repository(repo_name, path, repo_url, sub_paths=None):
|
2022-05-03 11:59:13 -05:00
|
|
|
if os.path.exists(path):
|
2022-05-03 11:59:04 -05:00
|
|
|
while True:
|
2022-05-15 08:13:48 -05:00
|
|
|
choice = input("There is already a `{}` folder, do you want to update it? [y/N]".format(path))
|
2022-05-03 11:59:04 -05:00
|
|
|
if choice == "" or choice.lower() == "n":
|
|
|
|
print("Skipping repository update.")
|
|
|
|
return
|
|
|
|
elif choice.lower() == "y":
|
|
|
|
print("Updating repository...")
|
2022-05-03 11:59:13 -05:00
|
|
|
run_command(["git", "pull", "origin"], cwd=path)
|
2022-05-03 11:59:04 -05:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
print("Didn't understand answer...")
|
2022-05-03 11:59:13 -05:00
|
|
|
print("Cloning {} repository...".format(repo_name))
|
2022-06-09 07:25:35 -05:00
|
|
|
if sub_paths is None:
|
2022-05-03 11:59:13 -05:00
|
|
|
run_command(["git", "clone", repo_url, "--depth", "1", path])
|
|
|
|
else:
|
|
|
|
run_command(["git", "clone", repo_url, "--filter=tree:0", "--no-checkout", path])
|
|
|
|
run_command(["git", "sparse-checkout", "init"], cwd=path)
|
2022-06-09 07:25:35 -05:00
|
|
|
run_command(["git", "sparse-checkout", "set", *sub_paths], cwd=path)
|
2022-05-03 11:59:13 -05:00
|
|
|
run_command(["git", "checkout"], cwd=path)
|
2022-05-03 11:59:04 -05:00
|
|
|
|
|
|
|
|
2022-05-03 14:24:14 -05:00
|
|
|
def append_intrinsic(array, intrinsic_name, translation):
|
|
|
|
array.append((intrinsic_name, translation))
|
|
|
|
|
|
|
|
|
2022-06-09 07:25:35 -05:00
|
|
|
def convert_to_string(content):
|
|
|
|
if content.__class__.__name__ == 'bytes':
|
|
|
|
return content.decode('utf-8')
|
|
|
|
return content
|
2022-05-03 11:59:04 -05:00
|
|
|
|
2022-06-09 07:25:35 -05:00
|
|
|
|
|
|
|
def extract_instrinsics_from_llvm(llvm_path, intrinsics):
|
|
|
|
p = subprocess.Popen(
|
|
|
|
["llvm-tblgen", "llvm/IR/Intrinsics.td"],
|
|
|
|
cwd=os.path.join(llvm_path, "llvm/include"),
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
output, err = p.communicate()
|
|
|
|
lines = convert_to_string(output).splitlines()
|
2022-05-03 11:59:04 -05:00
|
|
|
pos = 0
|
|
|
|
while pos < len(lines):
|
2022-06-09 07:25:35 -05:00
|
|
|
line = lines[pos]
|
|
|
|
if not line.startswith("def "):
|
|
|
|
pos += 1
|
2022-05-03 11:59:04 -05:00
|
|
|
continue
|
2022-06-09 07:25:35 -05:00
|
|
|
intrinsic = line.split(" ")[1].strip()
|
|
|
|
content = line
|
|
|
|
while pos < len(lines):
|
|
|
|
line = lines[pos].split(" // ")[0].strip()
|
|
|
|
content += line
|
|
|
|
pos += 1
|
|
|
|
if line == "}":
|
|
|
|
break
|
2022-06-23 06:27:34 -05:00
|
|
|
entries = re.findall('string ClangBuiltinName = "(\\w+)";', content)
|
2022-06-09 07:25:35 -05:00
|
|
|
current_arch = re.findall('string TargetPrefix = "(\\w+)";', content)
|
|
|
|
if len(entries) == 1 and len(current_arch) == 1:
|
|
|
|
current_arch = current_arch[0]
|
|
|
|
intrinsic = intrinsic.split("_")
|
|
|
|
if len(intrinsic) < 2 or intrinsic[0] != "int":
|
|
|
|
continue
|
|
|
|
intrinsic[0] = "llvm"
|
|
|
|
intrinsic = ".".join(intrinsic)
|
|
|
|
if current_arch not in intrinsics:
|
|
|
|
intrinsics[current_arch] = []
|
|
|
|
append_intrinsic(intrinsics[current_arch], intrinsic, entries[0])
|
2022-05-03 11:59:04 -05:00
|
|
|
|
2022-05-03 14:24:14 -05:00
|
|
|
|
|
|
|
def append_translation(json_data, p, array):
|
|
|
|
it = json_data["index"][p]
|
|
|
|
content = it["docs"].split('`')
|
|
|
|
if len(content) != 5:
|
|
|
|
return
|
|
|
|
append_intrinsic(array, content[1], content[3])
|
|
|
|
|
|
|
|
|
|
|
|
def extract_instrinsics_from_llvmint(llvmint, intrinsics):
|
|
|
|
archs = [
|
|
|
|
"AMDGPU",
|
|
|
|
"aarch64",
|
|
|
|
"arm",
|
|
|
|
"cuda",
|
|
|
|
"hexagon",
|
|
|
|
"mips",
|
|
|
|
"nvvm",
|
|
|
|
"ppc",
|
|
|
|
"ptx",
|
|
|
|
"x86",
|
|
|
|
"xcore",
|
|
|
|
]
|
|
|
|
|
|
|
|
json_file = os.path.join(llvmint, "target/doc/llvmint.json")
|
2022-05-15 07:48:52 -05:00
|
|
|
# We need to regenerate the documentation!
|
|
|
|
run_command(
|
|
|
|
["cargo", "rustdoc", "--", "-Zunstable-options", "--output-format", "json"],
|
|
|
|
cwd=llvmint,
|
|
|
|
)
|
2022-05-03 14:24:14 -05:00
|
|
|
with open(json_file, "r", encoding="utf8") as f:
|
|
|
|
json_data = json.loads(f.read())
|
|
|
|
for p in json_data["paths"]:
|
|
|
|
it = json_data["paths"][p]
|
|
|
|
if it["crate_id"] != 0:
|
|
|
|
# This is from an external crate.
|
|
|
|
continue
|
|
|
|
if it["kind"] != "function":
|
|
|
|
# We're only looking for functions.
|
|
|
|
continue
|
|
|
|
# if len(it["path"]) == 2:
|
|
|
|
# # This is a "general" intrinsic, not bound to a specific arch.
|
|
|
|
# append_translation(json_data, p, general)
|
|
|
|
# continue
|
|
|
|
if len(it["path"]) != 3 or it["path"][1] not in archs:
|
|
|
|
continue
|
|
|
|
arch = it["path"][1]
|
|
|
|
if arch not in intrinsics:
|
|
|
|
intrinsics[arch] = []
|
|
|
|
append_translation(json_data, p, intrinsics[arch])
|
|
|
|
|
|
|
|
|
2022-05-03 16:00:09 -05:00
|
|
|
def fill_intrinsics(intrinsics, from_intrinsics, all_intrinsics):
|
|
|
|
for arch in from_intrinsics:
|
|
|
|
if arch not in intrinsics:
|
|
|
|
intrinsics[arch] = []
|
|
|
|
for entry in from_intrinsics[arch]:
|
|
|
|
if entry[0] in all_intrinsics:
|
|
|
|
if all_intrinsics[entry[0]] == entry[1]:
|
|
|
|
# This is a "full" duplicate, both the LLVM instruction and the GCC
|
|
|
|
# translation are the same.
|
|
|
|
continue
|
|
|
|
intrinsics[arch].append((entry[0], entry[1], True))
|
|
|
|
else:
|
|
|
|
intrinsics[arch].append((entry[0], entry[1], False))
|
|
|
|
all_intrinsics[entry[0]] = entry[1]
|
|
|
|
|
|
|
|
|
2022-05-15 08:13:48 -05:00
|
|
|
def update_intrinsics(llvm_path, llvmint, llvmint2):
|
2022-05-03 16:00:09 -05:00
|
|
|
intrinsics_llvm = {}
|
|
|
|
intrinsics_llvmint = {}
|
2022-05-03 14:24:14 -05:00
|
|
|
all_intrinsics = {}
|
|
|
|
|
2022-05-03 16:00:09 -05:00
|
|
|
extract_instrinsics_from_llvm(llvm_path, intrinsics_llvm)
|
|
|
|
extract_instrinsics_from_llvmint(llvmint, intrinsics_llvmint)
|
2022-05-15 08:13:48 -05:00
|
|
|
extract_instrinsics_from_llvmint(llvmint2, intrinsics_llvmint)
|
2022-05-03 16:00:09 -05:00
|
|
|
|
|
|
|
intrinsics = {}
|
|
|
|
# We give priority to translations from LLVM over the ones from llvmint.
|
|
|
|
fill_intrinsics(intrinsics, intrinsics_llvm, all_intrinsics)
|
|
|
|
fill_intrinsics(intrinsics, intrinsics_llvmint, all_intrinsics)
|
2022-05-03 14:24:14 -05:00
|
|
|
|
2022-05-03 11:59:04 -05:00
|
|
|
archs = [arch for arch in intrinsics]
|
|
|
|
archs.sort()
|
2022-05-03 14:24:14 -05:00
|
|
|
|
2022-05-03 11:59:04 -05:00
|
|
|
output_file = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"../src/intrinsic/archs.rs",
|
|
|
|
)
|
|
|
|
print("Updating content of `{}`...".format(output_file))
|
|
|
|
with open(output_file, "w", encoding="utf8") as out:
|
|
|
|
out.write("// File generated by `rustc_codegen_gcc/tools/generate_intrinsics.py`\n")
|
|
|
|
out.write("// DO NOT EDIT IT!\n")
|
|
|
|
out.write("match name {\n")
|
|
|
|
for arch in archs:
|
|
|
|
if len(intrinsics[arch]) == 0:
|
|
|
|
continue
|
2022-05-03 16:00:09 -05:00
|
|
|
intrinsics[arch].sort(key=lambda x: (x[0], x[2]))
|
2022-05-03 11:59:04 -05:00
|
|
|
out.write(' // {}\n'.format(arch))
|
2022-05-03 14:24:14 -05:00
|
|
|
for entry in intrinsics[arch]:
|
2022-05-03 16:00:09 -05:00
|
|
|
if entry[2] == True: # if it is a duplicate
|
2022-05-03 14:24:14 -05:00
|
|
|
out.write(' // [DUPLICATE]: "{}" => "{}",\n'.format(entry[0], entry[1]))
|
2022-06-23 06:38:52 -05:00
|
|
|
elif "_round_mask" in entry[1]:
|
|
|
|
out.write(' // [INVALID CONVERSION]: "{}" => "{}",\n'.format(entry[0], entry[1]))
|
2022-05-03 14:24:14 -05:00
|
|
|
else:
|
|
|
|
out.write(' "{}" => "{}",\n'.format(entry[0], entry[1]))
|
2022-05-03 11:59:04 -05:00
|
|
|
out.write(' _ => unimplemented!("***** unsupported LLVM intrinsic {}", name),\n')
|
|
|
|
out.write("}\n")
|
|
|
|
print("Done!")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-05-03 11:59:13 -05:00
|
|
|
llvm_path = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"llvm-project",
|
|
|
|
)
|
2022-05-03 14:24:14 -05:00
|
|
|
llvmint_path = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"llvmint",
|
|
|
|
)
|
2022-05-15 08:13:48 -05:00
|
|
|
llvmint2_path = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
"llvmint-2",
|
|
|
|
)
|
2022-05-03 11:59:13 -05:00
|
|
|
|
2022-05-03 11:59:04 -05:00
|
|
|
# First, we clone the LLVM repository if it's not already here.
|
2022-05-03 11:59:13 -05:00
|
|
|
clone_repository(
|
|
|
|
"llvm-project",
|
|
|
|
llvm_path,
|
|
|
|
"https://github.com/llvm/llvm-project",
|
2022-06-09 07:25:35 -05:00
|
|
|
sub_paths=["llvm/include/llvm/IR", "llvm/include/llvm/CodeGen/"],
|
2022-05-03 14:24:14 -05:00
|
|
|
)
|
|
|
|
clone_repository(
|
|
|
|
"llvmint",
|
|
|
|
llvmint_path,
|
|
|
|
"https://github.com/GuillaumeGomez/llvmint",
|
|
|
|
)
|
2022-05-15 08:13:48 -05:00
|
|
|
clone_repository(
|
|
|
|
"llvmint2",
|
|
|
|
llvmint2_path,
|
|
|
|
"https://github.com/antoyo/llvmint",
|
|
|
|
)
|
|
|
|
update_intrinsics(llvm_path, llvmint_path, llvmint2_path)
|
2022-05-03 11:59:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|