-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_body_replace.py
68 lines (43 loc) · 2.17 KB
/
function_body_replace.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import re
from os import walk
# I will provide a directory with 0.ll, 1.ll, 2.ll ...
if len(sys.argv) != 3:
print("Usage: python replace_markers.py <shader_dir> <shader_placeholder.ll>")
sys.exit(1)
shader_dir, shader_placeholder = sys.argv[1:3]
filenames = next(walk(shader_dir), (None, None, []))[2]
filenames = sorted(filenames)
def addrspace2fptr(content):
spirv_adapt = content.replace("ptr", "float*")
cuda_adapt = spirv_adapt.replace("addrspace(4)", "")
ocl_adapt = cuda_adapt.replace("float* addrspace(2)", "ptr addrspace(2)")
return ocl_adapt
def remove_metadata(content):
no_number_metadata = re.sub('![0-9]+', " ", content, flags=re.DOTALL)
no_loop_metadata = no_number_metadata.replace(", !llvm.loop", "")
no_tbaa_metadata = no_loop_metadata.replace(", !tbaa", "")
return no_tbaa_metadata
with open(shader_placeholder, 'r', newline='') as f:
placeholder_content = f.read()
placeholder_content = addrspace2fptr(placeholder_content)
for filename in filenames:
print("Opening ", filename)
with open(shader_dir + filename, 'r', newline='') as f:
content = f.read()
content = remove_metadata(content)
reg_res = re.finditer('(entry:)(.*?)(})', content, flags=re.DOTALL)
for r in reg_res:
extracted_body = r.group()
id = int(filename.replace(".ll", ""))
print("ID: ", id)
print("EXTRACTED BODY:", extracted_body)
pattern = 'define((?:(?!define).)+?)(asl_shade' + str(id) + '_)(.+?){.+?}'
function_definitions = re.finditer(pattern, placeholder_content, flags=re.DOTALL)
for match in function_definitions :
print("MATCH ", match.group())
modified_content = re.sub('(entry:)(.*?)(})', extracted_body, match.group(), flags=re.DOTALL)
print("MODIFIED CONTENT: ", modified_content)
placeholder_content = placeholder_content.replace(match.group(), modified_content)
with open(shader_placeholder, 'w', newline='') as f:
f.write(placeholder_content)