-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_fmuladd.py
46 lines (34 loc) · 1.49 KB
/
remove_fmuladd.py
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
import os
import re
import sys
def replace_line_f32(match):
s, A, B, C, D = match.groups()[:5]
return f"{s}%temp{A} = fmul float %{B}, %{C}\n{s}%{A} = fadd float %temp{A}, %{D}"
def replace_line_f64(match):
s, A, B, C, D = match.groups()[:5]
return f"{s}%temp{A} = fmul double %{B}, %{C}\n{s}%{A} = fadd double %temp{A}, %{D}"
def process_file(file_path):
with open(file_path, "r") as f:
lines = f.readlines()
pattern_f32 = r"(\s*)%(\w+)\s*=\s*call\s*float\s*@llvm\.fmuladd\.f32\(float\s*%(\w+),\s*float\s*%(\w+),\s*float\s*%(\w+)\)(.*)"
pattern_f64 = r"(\s*)%(\w+)\s*=\s*call\s*double\s*@llvm\.fmuladd\.f64\(double\s*%(\w+),\s*double\s*%(\w+),\s*double\s*%(\w+)\)(.*)"
modified_lines = []
for line in lines:
modified_line = re.sub(pattern_f32, replace_line_f32, line)
modified_line = re.sub(pattern_f64, replace_line_f64, modified_line)
if modified_line != line:
print(f"Modified this:\n{line}Into this:\n{modified_line}")
modified_lines.append(";" + line)
modified_lines.append(modified_line)
with open(file_path, "w") as f:
f.writelines(modified_lines)
if len(sys.argv) != 2:
print("Usage: python script.py <path_to_ll_file>")
sys.exit(1)
file_path = sys.argv[1]
if file_path[-3:] != ".ll":
print("Error: The given path is not that of a \".ll\" file.")
if not os.path.isfile(file_path):
print("Error: The specified file does not exist.")
sys.exit(1)
process_file(file_path)