-
Notifications
You must be signed in to change notification settings - Fork 0
/
EASY.py
401 lines (352 loc) · 12 KB
/
EASY.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# This is the python code for EASY
# It reads from the successful Boogie code and simplify the arbitration in original LLVM IR
# Written by Jianyi Cheng
from __future__ import print_function
import datetime
# this is a function to search if the current arbiter port is in removing list
def search(funcAddr, indexAddr, parIndex):
j = 0
while j < len(thd):
if funcAddr == thd[j] and indexAddr == var[j] and parIndex == bnk[j]:
break
j += 1
return j
# 1.0 Read from Boogie file to generate list of arbiter ports to be removed
thd = []
var = []
bnk = []
op_buffer = []
ctr = 0
err = 0
inBoogie = open("op.bpl")
print("\n******************************************************************")
print(" Reading from Boogie output...")
print("******************************************************************")
for line in inBoogie:
if line.startswith("\tassert !t"):
for s in line.split():
num_idx = s.find("_index_")
if num_idx != -1:
thd.append(int(s[1:num_idx]))
var.append(int(s[num_idx+7:len(s)]))
par_idx = s.find("bv32")
if par_idx != -1:
bnk.append(int(s[0:par_idx]))
ctr += 1
#f.write("// This is the Boogie results indicating the arbiter ports that are never used during the execution.\n")
#f.write("// Used by EASY pass to simplify the hardware.\n")
#now = datetime.datetime.now()
#f.write("// "+str(now)+"\n\n")
#f.write("#define NEASY_PORTS "+str(ctr+1)+"\n\n")
#f.write("int thd["+str(ctr+1)+"] = {")
#for i in thd[:-1]:
# f.write(str(i)+", ")
#f.write(str(thd[-1])+"};\n")
#f.write("int var["+str(ctr+1)+"] = {")
#for i in var[:-1]:
# f.write(str(i)+", ")
#f.write(str(var[-1])+"};\n")
#f.write("int bnk["+str(ctr+1)+"] = {")
#for i in bnk[:-1]:
# f.write(str(i)+", ")
#f.write(str(bnk[-1])+"};\n")
#print("find " + str(ctr+1) + " arbiter ports to be removed.")
print("Found " + str(ctr) + " arbiter ports to be removed: \n")
print("\tThread\t=", end = " ")
for i in thd:
print(str(i), end = " ")
print("\n\tIndex\t=", end = " ")
for i in var:
print(str(i), end = " ")
print("\n\tBank\t=", end = " ")
for i in bnk:
print(str(i), end = " ")
print("\n\n")
inBoogie.close()
#open llvm ir code to do code transformation
nopInstr = " = add i8 0, 0\n"
inLLVM = open("input.ll")
f = open("output.ll", "w")
print("\n******************************************************************")
print(" Checking essential features...")
print("******************************************************************")
# find thread function name, thread number, MUX number and partition number
inLLVM.seek(0)
gFlag = 0 # previous is a getelementptr function of sub-array
indexNum = 0 # number of memory MUX in one thread
funcAddr = -1
nFlag = 0 # name of thread function found
threadNum = 0 # number of threads
partition = []
arrayName = []
# threads
for line in inLLVM:
addrPFuncHeader = line.find("original_name")
if addrPFuncHeader != -1:
threadNum += 1
if nFlag == 0:
addrPFuncName = line.find("\"", addrPFuncHeader+16)
funcName = line[addrPFuncHeader+16 : addrPFuncName]
nFlag = 1
# MUX
inLLVM.seek(0)
for line in inLLVM:
hasFuncName = line.find(funcName + "_")
isDefinition = line.find("define")
if hasFuncName != -1 and isDefinition != -1:
funcAddr += 1
if line.startswith(" %GEP_sub_") and funcAddr == 0:
# check total number of MUX
if gFlag == 0:
indexNum += 1
gFlag = 1
else:
gFlag = 0
# partitions
inLLVM.seek(0)
for line in inLLVM:
if line.startswith("@sub_"):
addrAssign = line.find(" = ")
endAddr = addrAssign-1
while line[endAddr] != "_":
endAddr -= 1
if len(arrayName) == 0:
arrayName.append(line[1:endAddr])
partition.append(1)
else:
i = 0;
findOld = 0;
while i < len(arrayName):
if arrayName[i] == line[1:endAddr]:
partition[i] += 1
findOld = 1
break
i += 1
if findOld == 0:
arrayName.append(line[1:endAddr])
partition.append(1)
if threadNum != max(thd)+1:
err += 1
print("Error: Thread number does not match: has " + str(max(thd)+1) + " in Boogie and " + str(threadNum) + " in LLVM. \n")
if indexNum != max(var)+1:
err += 1
print("Error: MUX number does not match: has " + str(max(var)+1) + " in Boogie and " + str(indexNum) + " in LLVM. \n")
print("\nFound thread function name = " + funcName + "\n")
print("Found thread number = " + str(threadNum) + "\n")
i = 0
while i < len(arrayName):
print("Found partitioned array: " + arrayName[i] + ", partition = " + str(partition[i]) + "\n")
i += 1
# analyze input code
print("\n******************************************************************")
print(" Analyzing LLVM input...")
print("******************************************************************\n")
# load input code
inLLVM.seek(0)
for line in inLLVM:
op_buffer.append(line)
process = [0] * len(op_buffer)
valid = [0] * len(thd)
gFlag = 0 # previous is a getelementptr function of sub-array
indexAddr = -1 # number of memory MUX in one thread
funcAddr = -1
i = 0
# analyze getelementptr instruction
inLLVM.seek(0)
for line in inLLVM:
hasFuncName = line.find(funcName + "_")
isDefinition = line.find("define")
if hasFuncName != -1 and isDefinition != -1:
funcAddr += 1
indexAddr = -1
if line.startswith(" %GEP_sub_"):
# check total number of MUX
if gFlag == 0:
indexAddr += 1
gFlag = 1
# start processing
addrAssign = line.find(" = ")
endAddr = addrAssign-1
while line[endAddr] != "_":
endAddr -= 1
startAddr = endAddr - 1
while line[startAddr] != "_":
startAddr -= 1
startAddr += 1
parIndex = int(line[startAddr:endAddr])
j = search(funcAddr, indexAddr, parIndex)
if j != len(thd):
valid[j] = 1
process[i] = 1 # getelementptr
else:
gFlag = 0
i += 1
if sum(valid) != len(thd):
err += 1
print("Error: " + str(len(thd) - sum(valid)) + "arbiter ports in removing list missed. (getelementptr)\n")
print("Extracted getelementptr instructions to be removed.\n ")
valid = [0] * len(thd)
lFlag1 = 0 # previous is a load function of sub-array
lFlag2 = 0 # previous is a load function of sub-array
indexAddr = -1 # number of memory MUX in one thread
funcAddr = -1
i = 0
icmpList = []
orList = []
listFunc = []
# analyze Load instruction
inLLVM.seek(0)
for line in inLLVM:
hasFuncName = line.find(funcName + "_")
isDefinition = line.find("define")
if hasFuncName != -1 and isDefinition != -1:
funcAddr += 1
indexAddr = -1
if line.startswith(" %Load_sub_"): # or line.startswith(" %Store_sub_"):
# check total number of MUX
if lFlag2 == 0:
indexAddr += 1
# start processing
addrAssign = line.find(" = ")
endAddr = addrAssign-1
while line[endAddr] != "_":
endAddr -= 1
startAddr = endAddr - 1
while line[startAddr] != "_":
startAddr -= 1
startAddr += 1
parIndex = int(line[startAddr:endAddr])
j = search(funcAddr, indexAddr, parIndex)
if j != len(thd):
valid[j] = 1
process[i] = 1 # load/store
process[i+1] = 3 # select
lFlag2 = lFlag1
lFlag1 = 1
elif process[i] == 3: # i.e. select instruction
addrAssign = line.find(" = ")
startAddr = line.find("%")
i1Addr = line.find("i1")
commaAddr = line.find(", ")
orList.append(line[startAddr:addrAssign])
icmpList.append(line[i1Addr+3:commaAddr])
listFunc.append(funcAddr)
lFlag2 = lFlag1
lFlag1 = 0
else:
lFlag2 = lFlag1
lFlag1 = 0
i += 1
if sum(valid) != len(thd):
err += 1
print("Error: " + str(len(thd) - sum(valid)) + " arbiter ports in removing list missed. (load + select)\n")
print("Extracted load + select instructions to be removed.\n ")
i = 0
print("\nFound icmp var: ")
while i < len(icmpList):
print(icmpList[i] + ", ", end =" ")
i += 1
i = 0
print("\nFound or var: ")
while i < len(orList):
print(orList[i] + ", ", end =" ")
i += 1
print("\n")
# analyze input code
print("\n******************************************************************")
print(" Transforming LLVM instructions...")
print("******************************************************************\n")
# analyze predication metadata
i = 0
inLLVM.seek(0)
funcAddr = -1
for line in inLLVM:
hasFuncName = line.find(funcName + "_")
isDefinition = line.find("define")
if hasFuncName != -1 and isDefinition != -1:
funcAddr += 1
if line.startswith(" %Load_sub_"):
if process[i] != 1:
newLoad = line.find(", !legup.ap_pred")
op_buffer[i] = line[0:newLoad] + ";" + line[newLoad:len(line)]
elif line.find("= icmp eq") != -1:
#print(line)
addrAssign = line.find(" = ")
startAddr = line.find("%")
j = 0
while j < len(icmpList):
if line[startAddr:addrAssign] == icmpList[j] and funcAddr == listFunc[j]:
process[i] = 3
j += 1
if process[i] != 3:
newIcmp = line.find(", !legup.ap_pred_id")
if newIcmp != -1:
op_buffer[i] = line[0:newIcmp] + ";" + line[newIcmp:len(line)]
i += 1
print("Transformed load + icmp instruction (including predicate metadata)\n ")
i = 0
inLLVM.seek(0)
funcAddr = -1
for line in inLLVM:
hasFuncName = line.find(funcName + "_")
isDefinition = line.find("define")
if hasFuncName != -1 and isDefinition != -1:
funcAddr += 1
if line.startswith(" call void @__legup_preserve_value_"):
if process[i-1] == 0:
newPreserve = line.find(", !legup.ap_pred_id")
if newPreserve != -1:
op_buffer[i] = line[0:newPreserve] + ";" + line[newPreserve:len(line)]
elif process[i-1] == 3:
process[i] = 1
else:
err += 1
print("Error: @__legup_preserve_value in wrong order - ")
print(op_buffer[i-1])
print(op_buffer[i])
elif line.find(" = or ") != -1:
addrAssign = line.find(" = ")
var1Addr = line.find("%", addrAssign)
commaAddr = line.find(",")
var2Addr = line.find("%", commaAddr)
j = 0
var1 = 0
var2 = 0
while j < len(orList):
if line[var1Addr:commaAddr] == orList[j] and funcAddr == listFunc[j]:
var1 = 1
if line[var2Addr:len(line)-1] == orList[j] and funcAddr == listFunc[j]:
var2 = 1
j += 1
if var1 == 1 and var2 == 1:
op_buffer[i] = line[0:var1Addr] + "0, 0\n"
elif var1 == 1:
op_buffer[i] = line[0:var1Addr] + "0" + line[commaAddr:len(line)]
elif var2 == 1:
op_buffer[i] = line[0:var2Addr] + "0\n"
i += 1
print("Transformed @__legup_preserve_value + Or instruction (including predicate metadata)\n ")
inLLVM.seek(0)
i = 0
for line in inLLVM:
if process[i] == 1 or process[i] == 2: # instruction to be removed
f.write(";" + op_buffer[i])
elif process[i] == 3: # instruction to be replaced with add
addrAssign = line.find(" = ")
startAddr = line.find("%")
f.write(";" + op_buffer[i])
f.write("\t" + line[startAddr:addrAssign] + nopInstr)
elif process[i] == 0:
f.write(op_buffer[i])
#isIcmp
else:
err += 1
print("Error: Unkown process code found: " + str(process[i]))
i += 1
print("Code transformation finished.\n\n")
f.close()
inLLVM.close()
if err == 0:
print("Transformation success! Error: " + str(err) +"\n")
else:
print("Transformation failed! Error: " + str(err) +"\n")