-
Notifications
You must be signed in to change notification settings - Fork 13
/
generateBackendInterOperator.py
executable file
·348 lines (293 loc) · 14.1 KB
/
generateBackendInterOperator.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
#!/usr/bin/env python3
# This file is part of Verrou, a FPU instrumentation tool.
# Copyright (C) 2014-2021 EDF
# F. Févotte <[email protected]>
# B. Lathuilière <[email protected]>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307, USA.
# The GNU Lesser General Public License is contained in the file COPYING.
import sys
import re
def treatBackend(tab, soft):
if soft==False:
return tab
else:
res=["if(vr.instrument_soft){\n"]
res+=tab
res+=["}else{\n"]
res+=[line.replace("BACKENDFUNC", "BACKEND_NEAREST_FUNC").replace("CONTEXT","BACKEND_NEAREST_CONTEXT") for line in tab]
res+=["}\n"]
return res
def transformTemplateForSoftStopStart(nameRegExp,convNameRegExp,
lineTab, soft=False, only64=False):
func=[]
dicOfFunc={}
splitActive=False
tab=[]
for line in lineTab:
result=nameRegExp.match(line) #"(.*)FCTNAME\(([^,]*),([^)]*)\)(.*)")
if result!=None:
typeVal=result.group(2)
opt=result.group(3)
newName="FCTNAME("+typeVal+","+opt+")"
if splitActive:
dicOfFunc[currentName]=tab
currentName=newName
tab=[line]
continue
else:
splitActive=True
currentName=newName
result=convNameRegExp.match(line) #"(.*)FCTCONVNAME\(([^,]*),([^)]*)\)(.*)")
if result!=None:
typeVal=result.group(2)
opt=result.group(3)
newName="FCTCONVNAME("+typeVal+","+opt+")"
if splitActive:
dicOfFunc[currentName]=tab
currentName=newName
tab=[line]
continue
else:
splitActive=True
currentName=newName
if splitActive:
tab+=[line]
dicOfFunc[currentName]=tab
#end of split
res=[]
for name in dicOfFunc:
if "CONV" in name:
if ("64F" in name) or (not only64):
res+=transformTemplateForSoftStopStartOneConvFunction(dicOfFunc[name], dicOfFunc[name.replace("CONV","")], soft)
else:
if ("64F" in name) or (not only64):
res+=transformTemplateForSoftStopStartOneFunction(dicOfFunc[name], soft)
return res
def transformTemplateForSoftStopStartOneFunction(lineTab, soft):
res=[]
back=[]
status="pre"
for line in lineTab:
if "PREBACKEND" in line:
status="back"
continue
if "POSTBACKEND" in line:
res+=treatBackend(back,soft)
back=[]
status="pre"
continue
if status=="pre":
res+=[line]
if status=="back":
back+=[line]
return res
def transformTemplateForSoftStopStartOneConvFunction(lineTabConv, lineTab, soft):
if soft==False:
return transformTemplateForSoftStopStartOneFunction(lineTabConv,soft)
res=[lineTabConv[0]]
remain=lineTabConv[1:]
while remain[-1].strip()!="}":
remain=remain[0:-1]
res+=["if(vr.instrument_soft){\n"]
for x in remain[0:-1]:
if ("PREBACKEND" in x) or ("POSTBACKEND" in x):
continue
res+=[x]
res+=["}else{\n"]
remain=lineTab[1:]
while remain[-1].strip()!="}":
remain=remain[0:-1]
for x in remain[0:-1]:
if ("PREBACKEND" in x) or ("POSTBACKEND" in x):
continue
res+=[x.replace("BACKENDFUNC", "BACKEND_NEAREST_FUNC").replace("CONTEXT","BACKEND_NEAREST_CONTEXT")]
res+=["}\n}\n\n"]
return res
def generateNargs(fileOut, fileNameTemplate, listOfBackend, listOfOp, nargs, post="", roundingTab=[None], soft=False, only64=False):
commentConv=False
templateStr=open(fileNameTemplate, "r").readlines()
FctNameRegExp=re.compile("(.*)FCTNAME\(([^,]*),([^)]*)\)(.*)")
FctConvNameRegExp=re.compile("(.*)FCTCONVNAME\(([^,]*),([^)]*)\)(.*)")
templateStr=transformTemplateForSoftStopStart(FctNameRegExp, FctConvNameRegExp, templateStr, soft, only64)
BckNameRegExp=re.compile("(.*)BACKENDFUNC\(([^)]*)\)(.*)")
BckNameNearestRegExp=re.compile("(.*)BACKEND_NEAREST_FUNC\(([^)]*)\)(.*)")
if post in ["check_float_max"]:
commentConv=True
for backend in listOfBackend:
if backend=="mcaquad":
fileOut.write("#ifdef USE_VERROU_QUADMATH\n")
for op in listOfOp:
for rounding in roundingTab:
if nargs in [1,2]:
applyTemplate(fileOut, templateStr, FctNameRegExp, FctConvNameRegExp, BckNameRegExp, BckNameNearestRegExp, backend,op, post, sign=None, rounding=rounding, soft=soft, commentConv=commentConv)
if nargs==3:
sign=""
if "msub" in op:
sign="-"
applyTemplate(fileOut, templateStr, FctNameRegExp, FctConvNameRegExp, BckNameRegExp,BckNameNearestRegExp, backend, op, post, sign, rounding=rounding, soft=soft, commentConv=commentConv)
if backend=="mcaquad":
fileOut.write("#endif //USE_VERROU_QUADMATH\n")
def applyTemplate(fileOut, templateStr, FctRegExp, FctConvRegExp, BckRegExp, BckNearestRegExp, backend, op, post, sign=None, rounding=None, soft=False, commentConv=False):
fileOut.write("// generation of operation %s backend %s\n"%(op,backend))
backendFunc=backend
if rounding!=None:
backendFunc=backend+"_"+rounding
def fctName(conv,typeVal,opt):
vrPrefix="vr_"
if conv:
vrPrefix="vr_conv_"
if soft:
return vrPrefix+backendFunc+post+"_soft"+op+typeVal+opt
else:
return vrPrefix+backendFunc+post+op+typeVal+opt
def bckName(typeVal):
if sign!="-":
if rounding!=None:
return "interflop_"+backend+"_"+op+"_"+typeVal+"_"+rounding
return "interflop_"+backend+"_"+op+"_"+typeVal
else:
if rounding!=None:
return "interflop_"+backend+"_"+op.replace("sub","add")+"_"+typeVal+"_"+rounding
return "interflop_"+backend+"_"+op.replace("sub","add")+"_"+typeVal
def bckNearestName(typeVal):
if rounding!=None:
return (bckName(typeVal)).replace(rounding,"NEAREST")
if sign!="-":
return "interflop_verrou_"+op+"_"+typeVal+"_NEAREST"
else:
return "interflop_verrou_"+op.replace("sub","add")+"_"+typeVal+"_NEAREST"
def bckNamePost(typeVal):
if sign!="-":
return "interflop_"+post+"_"+op+"_"+typeVal
else:
return "interflop_"+post+"_"+op.replace("sub","add")+"_"+typeVal
contextName="backend_"+backend+"_context"
contextNamePost="backend_"+post+"_context"
contextNearestName="backend_verrou_null_context"
comment=None
def outputRes(res,localComment):
line=None
if localComment:
if res in ["","//"]:
line=res+"\n"
else:
if "/*" in res:
line="/*"+ res.replace("/*", "%%").replace("*/","%%")+"*/"
else:
line="/*"+ res+"*/\n"
else:
line=res+"\n"
fileOut.write(line)
for line in templateStr:
if "BACKEND_NEAREST_CONTEXT" in line:
line=line.replace("BACKEND_NEAREST_CONTEXT", contextNearestName)
if "CONTEXT" in line:
line=line.replace("CONTEXT", contextName)
if "SIGN" in line:
if sign!=None:
line=line.replace("SIGN", sign)
else:
print("Generation failed")
sys.exit()
result=FctRegExp.match(line)
if result!=None:
comment=False
typeVal=result.group(2)
opt=result.group(3)
if rounding=="NEAREST" and soft:
comment=True
res=result.group(1) + fctName(False,typeVal, opt) + result.group(4)
outputRes(res,comment)
continue
result=FctConvRegExp.match(line)
if result!=None:
if commentConv:
comment=True
else:
comment=False
typeVal=result.group(2)
opt=result.group(3)
res=result.group(1) + fctName(True, typeVal, opt) + result.group(4)
outputRes(res,comment)
continue
result=BckRegExp.match(line)
if result!=None:
res=result.group(1) + bckName(result.group(2)) + result.group(3)
outputRes(res,comment)
if post!="":
res=result.group(1) + bckNamePost(result.group(2)) + result.group(3)
res=res.replace(contextName, contextNamePost)
outputRes(res,comment)
continue
result=BckNearestRegExp.match(line)
if result!=None:
res=result.group(1) + bckNearestName(result.group(2)) + result.group(3)
outputRes(res,comment)
continue
if line.endswith("\n"):
line=line[0:-1]
outputRes(line,comment)
if __name__=="__main__":
fileNameOutput="vr_generated_from_templates.h"
fileOut=open(fileNameOutput,"w")
fileOut.write("//Generated by %s\n"%(str(sys.argv)[1:-1]))
roundingTab=["NEAREST", "UPWARD", "DOWNWARD", "FARTHEST", "ZERO", "AWAY_ZERO"]+[rnd + det for rnd in ["RANDOM", "AVERAGE", "PRANDOM"] for det in ["","_DET","_COMDET" ]]
roundingTab+=[rnd + det for rnd in ["RANDOM", "AVERAGE"] for det in ["_SCOMDET" ]]
roundingTab+=["SR_MONOTONIC","SR_SMONOTONIC"]
template1Args="vr_interp_operator_template_cast.h"
listOfOp1Args=["cast"]
generateNargs(fileOut,template1Args, ["verrou","mcaquad","checkdenorm"], listOfOp1Args, 1)
generateNargs(fileOut,template1Args, ["verrou","mcaquad","checkdenorm"], listOfOp1Args, 1,soft=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, post="check_float_max")
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, post="check_float_max",soft=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=roundingTab)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=roundingTab, soft=True)
template1Args="vr_interp_operator_template_1args.h"
listOfOp1Args=["sqrt"]
fileOut.write("#ifdef USE_VERROU_SQRT\n")
generateNargs(fileOut,template1Args, ["verrou","checkdenorm"], listOfOp1Args, 1)
generateNargs(fileOut,template1Args, ["verrou","checkdenorm"], listOfOp1Args, 1,soft=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, post="check_float_max")
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, post="check_float_max",soft=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=roundingTab)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=roundingTab, soft=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=["FLOAT"], only64=True)
generateNargs(fileOut,template1Args, ["verrou"], listOfOp1Args, 1, roundingTab=["FLOAT"], soft=True, only64=True)
fileOut.write("#endif\n")
template2Args="vr_interp_operator_template_2args.h"
listOfOp2Args=["add","sub","mul","div"]
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2)
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2, soft=True)
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, post="check_float_max")
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, post="check_float_max", soft=True)
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, roundingTab=roundingTab)
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, roundingTab=roundingTab, soft=True)
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, roundingTab=["FLOAT"], only64=True)
generateNargs(fileOut,template2Args, ["verrou"], listOfOp2Args, 2, roundingTab=["FLOAT"], soft=True, only64=True)
listOfOp2Args=["add","sub"]
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2, post="checkcancellation")
generateNargs(fileOut,template2Args, ["verrou","mcaquad","checkdenorm"], listOfOp2Args, 2, post="checkcancellation",soft=True)
template3Args="vr_interp_operator_template_3args.h"
listOfOp3Args=["madd","msub"]
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3)
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3, soft=True)
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3, post="checkcancellation")
generateNargs(fileOut,template3Args, ["verrou","mcaquad","checkdenorm"], listOfOp3Args, 3, post="checkcancellation",soft=True)
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, post="check_float_max")
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, post="check_float_max",soft=True)
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, roundingTab=roundingTab)
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, roundingTab=roundingTab,soft=True)
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, roundingTab=["FLOAT"], only64=True)
generateNargs(fileOut,template3Args, ["verrou"], listOfOp3Args, 3, roundingTab=["FLOAT"],soft=True, only64=True)
fileOut.close()