Skip to content

Commit 7564b32

Browse files
Simplify intrinsics tool
1 parent 9f33f84 commit 7564b32

File tree

1 file changed

+30
-136
lines changed

1 file changed

+30
-136
lines changed

Diff for: tools/generate_intrinsics.py

+30-136
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def run_command(command, cwd=None):
1212
sys.exit(1)
1313

1414

15-
def clone_repository(repo_name, path, repo_url, sub_paths=None):
15+
def clone_repository(repo_name, path, repo_url, sub_paths, commit):
1616
if os.path.exists(path):
1717
while True:
1818
choice = input("There is already a `{}` folder, do you want to update it? [y/N]".format(path))
@@ -22,17 +22,17 @@ def clone_repository(repo_name, path, repo_url, sub_paths=None):
2222
elif choice.lower() == "y":
2323
print("Updating repository...")
2424
run_command(["git", "pull", "origin"], cwd=path)
25+
run_command(["git", "checkout", "origin/main",], cwd=path)
26+
run_command(["git", "checkout", commit], cwd=path)
2527
return
2628
else:
2729
print("Didn't understand answer...")
2830
print("Cloning {} repository...".format(repo_name))
29-
if sub_paths is None:
30-
run_command(["git", "clone", repo_url, "--depth", "1", path])
31-
else:
32-
run_command(["git", "clone", repo_url, "--filter=tree:0", "--no-checkout", path])
33-
run_command(["git", "sparse-checkout", "init"], cwd=path)
34-
run_command(["git", "sparse-checkout", "set", *sub_paths], cwd=path)
35-
run_command(["git", "checkout"], cwd=path)
31+
run_command(["git", "clone", repo_url, "--filter=tree:0", "--no-checkout", path])
32+
run_command(["git", "sparse-checkout", "init"], cwd=path)
33+
run_command(["git", "sparse-checkout", "set", *sub_paths], cwd=path)
34+
# run_command(["git", "checkout", "origin/main",], cwd=path)
35+
run_command(["git", "checkout", commit], cwd=path)
3636

3737

3838
def append_intrinsic(array, intrinsic_name, translation):
@@ -46,120 +46,33 @@ def convert_to_string(content):
4646

4747

4848
def extract_intrinsics_from_llvm(llvm_path, intrinsics):
49-
command = ["llvm-tblgen", "llvm/IR/Intrinsics.td"]
49+
command = ["llvm-tblgen", "llvm/IR/Intrinsics.td", "--dump-json"]
5050
cwd = os.path.join(llvm_path, "llvm/include")
5151
print("=> Running command `{}` from `{}`".format(command, cwd))
5252
p = subprocess.Popen(command, cwd=cwd, stdout=subprocess.PIPE)
5353
output, err = p.communicate()
54-
lines = convert_to_string(output).splitlines()
55-
pos = 0
56-
while pos < len(lines):
57-
line = lines[pos]
58-
if not line.startswith("def "):
59-
pos += 1
54+
content = json.loads(convert_to_string(output))
55+
for intrinsic in content:
56+
data = content[intrinsic]
57+
if not isinstance(data, dict):
6058
continue
61-
intrinsic = line.split(" ")[1].strip()
62-
content = line
63-
while pos < len(lines):
64-
line = lines[pos].split(" // ")[0].strip()
65-
content += line
66-
pos += 1
67-
if line == "}":
68-
break
69-
entries = re.findall('string ClangBuiltinName = "(\\w+)";', content)
70-
current_arch = re.findall('string TargetPrefix = "(\\w+)";', content)
71-
if len(entries) == 1 and len(current_arch) == 1:
72-
current_arch = current_arch[0]
73-
intrinsic = intrinsic.split("_")
74-
if len(intrinsic) < 2 or intrinsic[0] != "int":
75-
continue
76-
intrinsic[0] = "llvm"
77-
intrinsic = ".".join(intrinsic)
78-
if current_arch not in intrinsics:
79-
intrinsics[current_arch] = []
80-
append_intrinsic(intrinsics[current_arch], intrinsic, entries[0])
81-
82-
83-
def append_translation(json_data, p, array):
84-
it = json_data["index"][p]
85-
content = it["docs"].split('`')
86-
if len(content) != 5:
87-
return
88-
append_intrinsic(array, content[1], content[3])
89-
90-
91-
def extract_intrinsics_from_llvmint(llvmint, intrinsics):
92-
archs = [
93-
"AMDGPU",
94-
"aarch64",
95-
"arm",
96-
"cuda",
97-
"hexagon",
98-
"mips",
99-
"nvvm",
100-
"ppc",
101-
"ptx",
102-
"x86",
103-
"xcore",
104-
]
105-
106-
json_file = os.path.join(llvmint, "target/doc/llvmint.json")
107-
# We need to regenerate the documentation!
108-
run_command(
109-
["cargo", "rustdoc", "--", "-Zunstable-options", "--output-format", "json"],
110-
cwd=llvmint,
111-
)
112-
with open(json_file, "r", encoding="utf8") as f:
113-
json_data = json.loads(f.read())
114-
for p in json_data["paths"]:
115-
it = json_data["paths"][p]
116-
if it["crate_id"] != 0:
117-
# This is from an external crate.
59+
current_arch = data.get("TargetPrefix")
60+
builtin_name = data.get("ClangBuiltinName")
61+
if current_arch is None or current_arch == "" or builtin_name is None:
11862
continue
119-
if it["kind"] != "function":
120-
# We're only looking for functions.
63+
intrinsic = intrinsic.split("_")
64+
if len(intrinsic) < 2 or intrinsic[0] != "int":
12165
continue
122-
# if len(it["path"]) == 2:
123-
# # This is a "general" intrinsic, not bound to a specific arch.
124-
# append_translation(json_data, p, general)
125-
# continue
126-
if len(it["path"]) != 3 or it["path"][1] not in archs:
127-
continue
128-
arch = it["path"][1]
129-
if arch not in intrinsics:
130-
intrinsics[arch] = []
131-
append_translation(json_data, p, intrinsics[arch])
132-
133-
134-
def fill_intrinsics(intrinsics, from_intrinsics, all_intrinsics):
135-
for arch in from_intrinsics:
136-
if arch not in intrinsics:
137-
intrinsics[arch] = []
138-
for entry in from_intrinsics[arch]:
139-
if entry[0] in all_intrinsics:
140-
if all_intrinsics[entry[0]] == entry[1]:
141-
# This is a "full" duplicate, both the LLVM instruction and the GCC
142-
# translation are the same.
143-
continue
144-
intrinsics[arch].append((entry[0], entry[1], True))
145-
else:
146-
intrinsics[arch].append((entry[0], entry[1], False))
147-
all_intrinsics[entry[0]] = entry[1]
66+
intrinsic[0] = "llvm"
67+
intrinsic = ".".join(intrinsic)
68+
if current_arch not in intrinsics:
69+
intrinsics[current_arch] = []
70+
append_intrinsic(intrinsics[current_arch], intrinsic, builtin_name)
14871

14972

150-
def update_intrinsics(llvm_path, llvmint, llvmint2):
151-
intrinsics_llvm = {}
152-
intrinsics_llvmint = {}
153-
all_intrinsics = {}
154-
155-
extract_intrinsics_from_llvm(llvm_path, intrinsics_llvm)
156-
extract_intrinsics_from_llvmint(llvmint, intrinsics_llvmint)
157-
extract_intrinsics_from_llvmint(llvmint2, intrinsics_llvmint)
158-
73+
def update_intrinsics(llvm_path):
15974
intrinsics = {}
160-
# We give priority to translations from LLVM over the ones from llvmint.
161-
fill_intrinsics(intrinsics, intrinsics_llvm, all_intrinsics)
162-
fill_intrinsics(intrinsics, intrinsics_llvmint, all_intrinsics)
75+
extract_intrinsics_from_llvm(llvm_path, intrinsics)
16376

16477
archs = [arch for arch in intrinsics]
16578
archs.sort()
@@ -176,12 +89,9 @@ def update_intrinsics(llvm_path, llvmint, llvmint2):
17689
for arch in archs:
17790
if len(intrinsics[arch]) == 0:
17891
continue
179-
intrinsics[arch].sort(key=lambda x: (x[0], x[2]))
18092
out.write(' // {}\n'.format(arch))
18193
for entry in intrinsics[arch]:
182-
if entry[2] is True: # if it is a duplicate
183-
out.write(' // [DUPLICATE]: "{}" => "{}",\n'.format(entry[0], entry[1]))
184-
elif "_round_mask" in entry[1]:
94+
if "_round_mask" in entry[1]:
18595
out.write(' // [INVALID CONVERSION]: "{}" => "{}",\n'.format(entry[0], entry[1]))
18696
else:
18797
out.write(' "{}" => "{}",\n'.format(entry[0], entry[1]))
@@ -195,33 +105,17 @@ def main():
195105
os.path.dirname(os.path.abspath(__file__)),
196106
"llvm-project",
197107
)
198-
llvmint_path = os.path.join(
199-
os.path.dirname(os.path.abspath(__file__)),
200-
"llvmint",
201-
)
202-
llvmint2_path = os.path.join(
203-
os.path.dirname(os.path.abspath(__file__)),
204-
"llvmint-2",
205-
)
206108

207109
# First, we clone the LLVM repository if it's not already here.
208110
clone_repository(
209111
"llvm-project",
210112
llvm_path,
211113
"https://github.com/llvm/llvm-project",
212-
sub_paths=["llvm/include/llvm/IR", "llvm/include/llvm/CodeGen/"],
213-
)
214-
clone_repository(
215-
"llvmint",
216-
llvmint_path,
217-
"https://github.com/GuillaumeGomez/llvmint",
218-
)
219-
clone_repository(
220-
"llvmint2",
221-
llvmint2_path,
222-
"https://github.com/antoyo/llvmint",
114+
["llvm/include/llvm/IR", "llvm/include/llvm/CodeGen/"],
115+
# This is the git submodule commit of the LLVM repository used in the Rust repository.
116+
"1268e87bdbaed0693a9d782ccd5a21e2cab2de33",
223117
)
224-
update_intrinsics(llvm_path, llvmint_path, llvmint2_path)
118+
update_intrinsics(llvm_path)
225119

226120

227121
if __name__ == "__main__":

0 commit comments

Comments
 (0)